diff --git a/lib/pwl.vala b/lib/pwl.vala index 2c46dd5a..7054bb4d 100644 --- a/lib/pwl.vala +++ b/lib/pwl.vala @@ -27,6 +27,7 @@ * do so, delete this exception statement from your version. */ +using Posix; using Gnu; /** @@ -121,6 +122,7 @@ bool is_title_case(string word) { public class EnchantPWL { public string? filename; + public time_t file_changed; public HashTable words; private EnchantPWL() {} @@ -149,6 +151,7 @@ public class EnchantPWL { return null; EnchantPWL pwl = new EnchantPWL(); pwl.filename = file; + pwl.file_changed = 0; pwl.refresh_from_file(); return pwl; @@ -172,6 +175,9 @@ public class EnchantPWL { /* Since this method does not signal I/O errors, only use return values to avoid doing things that seem futile. */ lock_file(f); + Posix.Stat stats; + if (Posix.stat(this.filename, out stats) == 0) + this.file_changed = stats.st_mtime; /* Add a newline if the file doesn't end with one. */ if (f.seek(-1, FileSeek.END) == 0) { @@ -236,6 +242,10 @@ public class EnchantPWL { } } + Posix.Stat stats; + if (Posix.stat(this.filename, out stats) == 0) + this.file_changed = stats.st_mtime; + unlock_file(f); } } @@ -268,12 +278,19 @@ public class EnchantPWL { if (this.filename == null) return; + Posix.Stat stats; + if (Posix.stat(this.filename, out stats) == -1) + return; /* presumably won't be able to open the file either */ + if (this.file_changed == stats.st_mtime) /* nothing changed since last read */ + return; + this.words = new HashTable(str_hash, str_equal); FileStream? f = FileStream.open(this.filename, "r"); if (f == null) return; + this.file_changed = stats.st_mtime; lock_file(f); size_t line_number = 1; diff --git a/tests/EnchantDictionaryTestFixture.h b/tests/EnchantDictionaryTestFixture.h index 9f1b327c..d9be606f 100644 --- a/tests/EnchantDictionaryTestFixture.h +++ b/tests/EnchantDictionaryTestFixture.h @@ -259,6 +259,9 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture static void ExternalAddWordToFile(const std::string& word, const std::string& filename) { + sleep(1); // FAT systems have a 2 second resolution + // NTFS is appreciably faster but no specs on what it is exactly + // c runtime library's time_t has a 1 second resolution FILE * f = g_fopen(filename.c_str(), "a"); if(f) { @@ -270,6 +273,9 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture void ExternalAddNewLineToDictionary() { + sleep(1); // FAT systems have a 2 second resolution + // NTFS is appreciably faster but no specs on what it is exactly + // c runtime library's time_t has a 1 second resolution FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "a"); if(f) { @@ -280,6 +286,9 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture void ExternalAddWordsToDictionary(const std::vector& sWords) { + sleep(1); // FAT systems have a 2 second resolution + // NTFS is appreciably faster but no specs on what it is exactly + // c runtime library's time_t has a 1 second resolution FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "a"); if(f) { diff --git a/tests/pwl/pwl.i b/tests/pwl/pwl.i index 9ecfbe1f..bdddc213 100644 --- a/tests/pwl/pwl.i +++ b/tests/pwl/pwl.i @@ -67,6 +67,9 @@ TEST_FIXTURE(EnchantPwl_TestFixture, { const char* Utf8Bom = "\xef\xbb\xbf"; + sleep(1); // FAT systems have a 2 second resolution + // NTFS is appreciably faster but no specs on what it is exactly + // c runtime library's time_t has a 1 second resolution FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "a"); if(f) { @@ -120,6 +123,9 @@ TEST_FIXTURE(EnchantPwl_TestFixture, sWords.push_back("bat"); sWords.push_back("tot"); + sleep(1); // FAT systems have a 2 second resolution + // NTFS is appreciably faster but no specs on what it is exactly + // c runtime library's time_t has a 1 second resolution FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "a"); if(f) { @@ -553,6 +559,9 @@ TEST_FIXTURE(EnchantPwl_TestFixture, sWords.push_back("cat"); sWords.push_back("hat"); + sleep(1); // FAT systems have a 2 second resolution + // NTFS is appreciably faster but no specs on what it is exactly + // c runtime library's time_t has a 1 second resolution FILE * f = g_fopen(GetPersonalDictFileName().c_str(), "a"); if(f) { fputs(Utf8Bom, f);