diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-01-18 19:25:33 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2021-01-18 19:25:33 +0200 |
commit | d828264ebb1f021a7e4bb3abbb5719dc637c2fa3 (patch) | |
tree | ecba09668692c0023bdaf0912eff5a9c0ed3ccf8 /src/visited.c | |
parent | e8412f167ec2c8d72055181c2c5e1df09699a94e (diff) |
Fixed URL visit time serialization
The timestamps written to "visited.txt" were being shifted by GMT offset on each write/read, rendering them increasingly incorrect.
Since these timestamps cannot be trusted, ignore the old file and keep proper timestamp values in "visited2.txt" from now on.
It is also more efficient to not convert each timestamp to a date when serializing.
Diffstat (limited to 'src/visited.c')
-rw-r--r-- | src/visited.c | 36 |
1 files changed, 12 insertions, 24 deletions
diff --git a/src/visited.c b/src/visited.c index 3e034399..6fcc23f7 100644 --- a/src/visited.c +++ b/src/visited.c | |||
@@ -74,21 +74,14 @@ void deinit_Visited(iVisited *d) { | |||
74 | 74 | ||
75 | void save_Visited(const iVisited *d, const char *dirPath) { | 75 | void save_Visited(const iVisited *d, const char *dirPath) { |
76 | iString *line = new_String(); | 76 | iString *line = new_String(); |
77 | iFile *f = newCStr_File(concatPath_CStr(dirPath, "visited.txt")); | 77 | iFile *f = newCStr_File(concatPath_CStr(dirPath, "visited2.txt")); |
78 | if (open_File(f, writeOnly_FileMode | text_FileMode)) { | 78 | if (open_File(f, writeOnly_FileMode | text_FileMode)) { |
79 | lock_Mutex(d->mtx); | 79 | lock_Mutex(d->mtx); |
80 | iConstForEach(Array, i, &d->visited.values) { | 80 | iConstForEach(Array, i, &d->visited.values) { |
81 | const iVisitedUrl *item = i.value; | 81 | const iVisitedUrl *item = i.value; |
82 | iDate date; | ||
83 | init_Date(&date, &item->when); | ||
84 | format_String(line, | 82 | format_String(line, |
85 | "%04d-%02d-%02dT%02d:%02d:%02d %04x %s\n", | 83 | "%llu %04x %s\n", |
86 | date.year, | 84 | (unsigned long long) integralSeconds_Time(&item->when), |
87 | date.month, | ||
88 | date.day, | ||
89 | date.hour, | ||
90 | date.minute, | ||
91 | date.second, | ||
92 | item->flags, | 85 | item->flags, |
93 | cstr_String(&item->url)); | 86 | cstr_String(&item->url)); |
94 | writeData_File(f, cstr_String(line), size_String(line)); | 87 | writeData_File(f, cstr_String(line), size_String(line)); |
@@ -100,7 +93,7 @@ void save_Visited(const iVisited *d, const char *dirPath) { | |||
100 | } | 93 | } |
101 | 94 | ||
102 | void load_Visited(iVisited *d, const char *dirPath) { | 95 | void load_Visited(iVisited *d, const char *dirPath) { |
103 | iFile *f = newCStr_File(concatPath_CStr(dirPath, "visited.txt")); | 96 | iFile *f = newCStr_File(concatPath_CStr(dirPath, "visited2.txt")); |
104 | if (open_File(f, readOnly_FileMode | text_FileMode)) { | 97 | if (open_File(f, readOnly_FileMode | text_FileMode)) { |
105 | lock_Mutex(d->mtx); | 98 | lock_Mutex(d->mtx); |
106 | const iRangecc src = range_Block(collect_Block(readAll_File(f))); | 99 | const iRangecc src = range_Block(collect_Block(readAll_File(f))); |
@@ -108,23 +101,18 @@ void load_Visited(iVisited *d, const char *dirPath) { | |||
108 | iTime now; | 101 | iTime now; |
109 | initCurrent_Time(&now); | 102 | initCurrent_Time(&now); |
110 | while (nextSplit_Rangecc(src, "\n", &line)) { | 103 | while (nextSplit_Rangecc(src, "\n", &line)) { |
111 | if (size_Range(&line) < 22) continue; | 104 | if (size_Range(&line) < 8) continue; |
112 | int y, m, D, H, M, S; | 105 | char *endp = NULL; |
113 | sscanf(line.start, "%04d-%02d-%02dT%02d:%02d:%02d ", &y, &m, &D, &H, &M, &S); | 106 | const unsigned long long ts = strtoull(line.start, &endp, 10); |
114 | if (!y) break; | 107 | if (ts == 0) break; |
108 | const uint32_t flags = strtoul(skipSpace_CStr(endp), &endp, 16); | ||
109 | const char *urlStart = skipSpace_CStr(endp); | ||
115 | iVisitedUrl item; | 110 | iVisitedUrl item; |
116 | init_VisitedUrl(&item); | 111 | item.when.ts = (struct timespec){ .tv_sec = ts }; |
117 | const char *urlStart = line.start + 20; | ||
118 | if (*urlStart == '0' && size_Range(&line) >= 25) { | ||
119 | item.flags = strtoul(line.start + 20, NULL, 16); | ||
120 | urlStart += 5; | ||
121 | } | ||
122 | init_Time( | ||
123 | &item.when, | ||
124 | &(iDate){ .year = y, .month = m, .day = D, .hour = H, .minute = M, .second = S }); | ||
125 | if (secondsSince_Time(&now, &item.when) > maxAge_Visited) { | 112 | if (secondsSince_Time(&now, &item.when) > maxAge_Visited) { |
126 | continue; /* Too old. */ | 113 | continue; /* Too old. */ |
127 | } | 114 | } |
115 | item.flags = flags; | ||
128 | initRange_String(&item.url, (iRangecc){ urlStart, line.end }); | 116 | initRange_String(&item.url, (iRangecc){ urlStart, line.end }); |
129 | insert_SortedArray(&d->visited, &item); | 117 | insert_SortedArray(&d->visited, &item); |
130 | } | 118 | } |