Skip to content

Commit

Permalink
Revert "Remove check for mtime being changed when reading PWLs"
Browse files Browse the repository at this point in the history
This reverts commit 208cbac.
  • Loading branch information
rrthomas committed Jun 13, 2024
1 parent 75e77d1 commit 35b1bb4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/pwl.vala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* do so, delete this exception statement from your version.
*/

using Posix;
using Gnu;

/**
Expand Down Expand Up @@ -121,6 +122,7 @@ bool is_title_case(string word) {

public class EnchantPWL {
public string? filename;
public time_t file_changed;
public HashTable<string, string> words;

private EnchantPWL() {}
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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<string, string>(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;
Expand Down
9 changes: 9 additions & 0 deletions tests/EnchantDictionaryTestFixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -280,6 +286,9 @@ struct EnchantDictionaryTestFixture : EnchantBrokerTestFixture

void ExternalAddWordsToDictionary(const std::vector<std::string>& 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)
{
Expand Down
9 changes: 9 additions & 0 deletions tests/pwl/pwl.i
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 35b1bb4

Please sign in to comment.