/src/httrack/src/htschanges.c
Line | Count | Source |
1 | | /* ------------------------------------------------------------ */ |
2 | | /* |
3 | | HTTrack Website Copier, Offline Browser for Windows and Unix |
4 | | Copyright (C) 2026 Xavier Roche and other contributors |
5 | | |
6 | | SPDX-License-Identifier: GPL-3.0-or-later |
7 | | |
8 | | This program is free software: you can redistribute it and/or modify |
9 | | it under the terms of the GNU General Public License as published by |
10 | | the Free Software Foundation, either version 3 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | | |
21 | | Ethical use: we kindly ask that you NOT use this software to harvest email |
22 | | addresses or to collect any other private information about people. Doing so |
23 | | would dishonor our work and waste the many hours we have spent on it. |
24 | | |
25 | | Please visit our Website: http://www.httrack.com |
26 | | */ |
27 | | |
28 | | /* ------------------------------------------------------------ */ |
29 | | /* File: htschanges.c subroutines: */ |
30 | | /* --changes: what this crawl changed vs. the previous */ |
31 | | /* mirror (hts-changes.json) */ |
32 | | /* Author: Xavier Roche */ |
33 | | /* ------------------------------------------------------------ */ |
34 | | |
35 | | #include "htschanges.h" |
36 | | |
37 | | #include "htscache.h" |
38 | | #include "htscharset.h" |
39 | | #include "htscore.h" |
40 | | #include "htslib.h" |
41 | | #include "htsio.h" |
42 | | #include "htsmd5.h" |
43 | | #include "htssafe.h" |
44 | | #include "htsarrays.h" |
45 | | #include "htsthread.h" |
46 | | #include "htstools.h" |
47 | | #include "coucal/coucal.h" |
48 | | #include "md5.h" |
49 | | |
50 | | #include <stdlib.h> |
51 | | #include <string.h> |
52 | | #include <time.h> |
53 | | |
54 | 0 | #define DIGEST_SIZE 16 |
55 | | |
56 | | typedef struct hts_changes hts_changes; |
57 | | |
58 | | /* One mirrored file, keyed by path because a redirect shares its save name. */ |
59 | | typedef struct { |
60 | | char *url; /* absolute URL, "" for an engine-generated file */ |
61 | | char *file; /* mirror-relative path, as listed in new.lst */ |
62 | | hts_boolean rewritten; /* the crawl wrote over the local copy */ |
63 | | hts_boolean not_updated; /* transfer signal: the server reported no change */ |
64 | | hts_boolean existed; /* the file was part of the previous mirror */ |
65 | | hts_boolean on_disk; /* a copy was on disk when the crawl first saw it */ |
66 | | hts_boolean listed_prev; /* the previous mirror's index listed this file */ |
67 | | hts_boolean has_prev; /* prev_digest holds the previous payload's digest */ |
68 | | hts_boolean has_new; /* new_digest was taken from the payload, not disk */ |
69 | | unsigned char prev_digest[DIGEST_SIZE]; |
70 | | unsigned char new_digest[DIGEST_SIZE]; |
71 | | LLint prev_size; /* -1 when unknown */ |
72 | | LLint size; /* size after the crawl, -1 when unknown */ |
73 | | hts_change_bucket bucket; |
74 | | } changes_entry; |
75 | | |
76 | | struct hts_changes { |
77 | | coucal index; /* mirror-relative path -> entry slot + 1 */ |
78 | | TypedArray(changes_entry) entries; |
79 | | hts_boolean has_index; /* the run keeps a mirror index (the cache is on) */ |
80 | | hts_boolean old_index; /* the previous mirror's index was read */ |
81 | | hts_boolean overflow; /* an allocation failed; the report is partial */ |
82 | | hts_boolean closed; /* reported: late notifies are dropped, not recorded */ |
83 | | }; |
84 | | |
85 | | /* ------------------------------------------------------------ */ |
86 | | /* Accumulator */ |
87 | | /* ------------------------------------------------------------ */ |
88 | | |
89 | 0 | static hts_changes *changes_new(void) { |
90 | 0 | hts_changes *changes = calloct(1, sizeof(*changes)); |
91 | |
|
92 | 0 | if (changes == NULL) |
93 | 0 | return NULL; |
94 | 0 | changes->index = coucal_new(0); |
95 | 0 | if (changes->index == NULL) { |
96 | 0 | freet(changes); |
97 | 0 | return NULL; |
98 | 0 | } |
99 | 0 | return changes; |
100 | 0 | } |
101 | | |
102 | 0 | static void changes_free(hts_changes **pchanges) { |
103 | 0 | hts_changes *changes = *pchanges; |
104 | 0 | size_t i; |
105 | |
|
106 | 0 | if (changes == NULL) |
107 | 0 | return; |
108 | 0 | for (i = 0; i < TypedArraySize(changes->entries); i++) { |
109 | 0 | freet(TypedArrayNth(changes->entries, i).url); |
110 | 0 | freet(TypedArrayNth(changes->entries, i).file); |
111 | 0 | } |
112 | 0 | TypedArrayFree(changes->entries); |
113 | 0 | if (changes->index != NULL) |
114 | 0 | coucal_delete(&changes->index); |
115 | 0 | freet(changes); |
116 | 0 | *pchanges = NULL; |
117 | 0 | } |
118 | | |
119 | | /* Slot of `file`, or -1 when it has not been recorded. */ |
120 | 0 | static intptr_t changes_find(const hts_changes *changes, const char *file) { |
121 | 0 | intptr_t slot = 0; |
122 | | |
123 | | // the bounds test is for the analyzer, the index holds only valid slots |
124 | 0 | return coucal_read(changes->index, file, &slot) && slot > 0 && |
125 | 0 | (size_t) slot <= TypedArraySize(changes->entries) |
126 | 0 | ? slot - 1 |
127 | 0 | : -1; |
128 | 0 | } |
129 | | |
130 | | /* Slot of `file`, appending a fresh entry when it is new; -1 if allocation |
131 | | failed, which flags the report partial. */ |
132 | | static intptr_t changes_slot(hts_changes *changes, const char *file, |
133 | 0 | hts_boolean *is_new) { |
134 | 0 | intptr_t slot = 0; |
135 | 0 | changes_entry *entry; |
136 | |
|
137 | 0 | *is_new = HTS_FALSE; |
138 | 0 | if (coucal_read(changes->index, file, &slot)) |
139 | 0 | return slot - 1; |
140 | | |
141 | 0 | if (!TypedArrayReserve(changes->entries, 1)) { |
142 | 0 | changes->overflow = HTS_TRUE; |
143 | 0 | return -1; |
144 | 0 | } |
145 | 0 | slot = (intptr_t) TypedArraySize(changes->entries); |
146 | 0 | entry = &TypedArrayTail(changes->entries); |
147 | 0 | memset(entry, 0, sizeof(*entry)); |
148 | 0 | entry->file = strdupt(file); |
149 | 0 | entry->prev_size = -1; |
150 | 0 | entry->size = -1; |
151 | 0 | if (entry->file == NULL) { |
152 | 0 | changes->overflow = HTS_TRUE; |
153 | 0 | return -1; |
154 | 0 | } |
155 | 0 | TypedArraySize(changes->entries)++; |
156 | 0 | coucal_write(changes->index, file, slot + 1); |
157 | 0 | *is_new = HTS_TRUE; |
158 | 0 | return slot; |
159 | 0 | } |
160 | | |
161 | | /* MD5 of the file at `path`, HTS_FALSE if unreadable. Never call it holding |
162 | | changes_mutex. */ |
163 | | static hts_boolean digest_file(const char *path, |
164 | 0 | unsigned char digest[DIGEST_SIZE]) { |
165 | 0 | struct MD5Context ctx; |
166 | 0 | char BIGSTK buffer[32768]; |
167 | 0 | FILE *fp = FOPEN(path, "rb"); |
168 | 0 | size_t nread; |
169 | |
|
170 | 0 | if (fp == NULL) |
171 | 0 | return HTS_FALSE; |
172 | 0 | hts_md5_init(&ctx); |
173 | 0 | do { |
174 | 0 | nread = fread(buffer, 1, sizeof(buffer), fp); |
175 | 0 | MD5Update(&ctx, (const unsigned char *) buffer, (unsigned int) nread); |
176 | 0 | } while (nread == sizeof(buffer)); |
177 | 0 | if (ferror(fp) != 0) { |
178 | 0 | fclose(fp); |
179 | 0 | return HTS_FALSE; |
180 | 0 | } |
181 | 0 | fclose(fp); |
182 | 0 | MD5Final(digest, &ctx); |
183 | 0 | return HTS_TRUE; |
184 | 0 | } |
185 | | |
186 | 0 | static void digest_mem(hts_span data, unsigned char digest[DIGEST_SIZE]) { |
187 | 0 | domd5mem(data, hts_buf_at((char *) digest, DIGEST_SIZE), HTS_FALSE); |
188 | 0 | } |
189 | | |
190 | | hts_change_bucket hts_changes_classify(hts_boolean rewritten, |
191 | | hts_boolean existed, |
192 | | hts_boolean not_updated, |
193 | | hts_boolean have_digests, |
194 | 0 | hts_boolean digests_equal) { |
195 | 0 | if (!rewritten) |
196 | 0 | return HTS_CHANGE_UNCHANGED; /* the crawl left the copy alone */ |
197 | 0 | if (!existed) |
198 | 0 | return HTS_CHANGE_NEW; |
199 | 0 | if (have_digests) |
200 | 0 | return digests_equal ? HTS_CHANGE_UNCHANGED : HTS_CHANGE_CHANGED; |
201 | | /* No digest to compare: a server with no validators answers 200 with the |
202 | | same bytes, so this signal alone over-reports. */ |
203 | 0 | return not_updated ? HTS_CHANGE_UNCHANGED : HTS_CHANGE_CHANGED; |
204 | 0 | } |
205 | | |
206 | | /* ------------------------------------------------------------ */ |
207 | | /* Engine hooks */ |
208 | | /* ------------------------------------------------------------ */ |
209 | | |
210 | | /* FTP transfers reach file_notify() from a thread the crawl never joins, so |
211 | | every entry point below, report and teardown included, takes this lock. */ |
212 | | static htsmutex changes_mutex = HTSMUTEX_INIT; |
213 | | |
214 | | /* The live accumulator, created on first use; NULL when --changes is off or |
215 | | the report is already written. Call under the lock. */ |
216 | 0 | static hts_changes *changes_get(httrackp *opt) { |
217 | 0 | hts_changes *changes = (hts_changes *) opt->changes_state; |
218 | |
|
219 | 0 | if (!opt->changes) |
220 | 0 | return NULL; |
221 | 0 | if (changes == NULL) { |
222 | 0 | changes = changes_new(); |
223 | 0 | opt->changes_state = changes; |
224 | 0 | } |
225 | 0 | return changes != NULL && !changes->closed ? changes : NULL; |
226 | 0 | } |
227 | | |
228 | | void hts_changes_notify(httrackp *opt, const char *adr, const char *fil, |
229 | | const char *save, hts_boolean rewritten, |
230 | 0 | hts_boolean not_updated) { |
231 | 0 | hts_changes *changes; |
232 | 0 | char BIGSTK file[HTS_URLMAXSIZE * 2]; |
233 | 0 | char BIGSTK url[HTS_URLMAXSIZE * 4 + 8]; /* holds adr + fil + a scheme */ |
234 | 0 | unsigned char prev_digest[DIGEST_SIZE]; |
235 | 0 | hts_boolean has_prev = HTS_FALSE; |
236 | 0 | hts_boolean is_new = HTS_FALSE; |
237 | 0 | intptr_t slot = -1; |
238 | 0 | LLint prev_size; |
239 | |
|
240 | 0 | if (!opt->changes) |
241 | 0 | return; |
242 | | /* Engine-generated scaffolding (the top index) carries no URL and is not a |
243 | | mirrored resource. */ |
244 | 0 | if (save == NULL || !strnotempty(save) || adr == NULL || fil == NULL || |
245 | 0 | (!strnotempty(adr) && !strnotempty(fil))) |
246 | 0 | return; |
247 | | |
248 | 0 | hts_savename_listed(StringBuff(opt->path_html), save, HTS_BUF(file)); |
249 | |
|
250 | 0 | hts_mutexlock(&changes_mutex); |
251 | 0 | changes = changes_get(opt); |
252 | 0 | if (changes != NULL) { |
253 | 0 | slot = changes_slot(changes, file, &is_new); |
254 | 0 | if (slot >= 0 && !is_new) { |
255 | 0 | changes_entry *const entry = &TypedArrayAt(changes->entries, slot); |
256 | | |
257 | | /* Keep the first sample, because only it saw the pre-run copy. */ |
258 | 0 | entry->rewritten = entry->rewritten || rewritten; |
259 | 0 | } |
260 | 0 | } |
261 | 0 | hts_mutexrelease(&changes_mutex); |
262 | 0 | if (slot < 0 || !is_new) |
263 | 0 | return; |
264 | | |
265 | 0 | url[0] = '\0'; |
266 | 0 | if (!link_has_authority(adr)) |
267 | 0 | strlcatbuff(url, "http://", sizeof(url)); |
268 | 0 | strlcatbuff(url, adr, sizeof(url)); |
269 | 0 | strlcatbuff(url, fil, sizeof(url)); |
270 | | |
271 | | /* Hash unlocked, because a large file would stall every other connection. */ |
272 | 0 | prev_size = fsize_utf8(save); |
273 | 0 | if (prev_size >= 0 && rewritten) |
274 | 0 | has_prev = digest_file(save, prev_digest); |
275 | |
|
276 | 0 | hts_mutexlock(&changes_mutex); |
277 | 0 | changes = changes_get(opt); |
278 | | /* Append-only entries keep the slot index stable, but the array moves. */ |
279 | 0 | if (changes != NULL && (size_t) slot < TypedArraySize(changes->entries)) { |
280 | 0 | changes_entry *const entry = &TypedArrayNth(changes->entries, slot); |
281 | |
|
282 | 0 | entry->url = strdupt(url); |
283 | 0 | if (entry->url == NULL) |
284 | 0 | changes->overflow = HTS_TRUE; |
285 | 0 | entry->rewritten = entry->rewritten || rewritten; |
286 | 0 | entry->not_updated = not_updated; |
287 | 0 | entry->prev_size = prev_size; |
288 | 0 | entry->on_disk = prev_size >= 0; |
289 | | /* hts_changes_html() compares payloads, and its digests win. */ |
290 | 0 | if (has_prev && !entry->has_new) { |
291 | 0 | entry->has_prev = HTS_TRUE; |
292 | 0 | memcpy(entry->prev_digest, prev_digest, DIGEST_SIZE); |
293 | 0 | } |
294 | 0 | } |
295 | 0 | hts_mutexrelease(&changes_mutex); |
296 | 0 | } |
297 | | |
298 | | void hts_changes_html(httrackp *opt, cache_back *cache, const htsblk *r, |
299 | 0 | const char *adr, const char *fil, const char *save) { |
300 | 0 | hts_changes *changes; |
301 | 0 | char BIGSTK file[HTS_URLMAXSIZE * 2]; |
302 | 0 | char BIGSTK location[HTS_URLMAXSIZE * 2]; |
303 | 0 | unsigned char new_digest[DIGEST_SIZE]; |
304 | 0 | unsigned char prev_digest[DIGEST_SIZE]; |
305 | 0 | hts_boolean has_prev = HTS_FALSE; |
306 | 0 | intptr_t slot; |
307 | 0 | htsblk prev; |
308 | |
|
309 | 0 | if (!opt->changes) |
310 | 0 | return; |
311 | 0 | if (r->adr == NULL || r->size < 0 || save == NULL || !strnotempty(save)) |
312 | 0 | return; |
313 | | |
314 | | /* The page on disk has rewritten links, so compare against the cache. */ |
315 | 0 | digest_mem(hts_blk_body(r), new_digest); |
316 | 0 | prev = cache_read_ro(opt, cache, adr, fil, "", HTS_BUF(location)); |
317 | 0 | if (HTTP_IS_OK(prev.statuscode) && prev.adr != NULL && prev.size >= 0) { |
318 | 0 | digest_mem(hts_blk_body(&prev), prev_digest); |
319 | 0 | has_prev = HTS_TRUE; |
320 | 0 | } |
321 | 0 | freet(prev.adr); |
322 | | |
323 | | /* Lock only now, because cache_read_ro() can move the entries array. */ |
324 | 0 | hts_mutexlock(&changes_mutex); |
325 | 0 | changes = changes_get(opt); |
326 | | /* file_notify() records the entry; this call only refines it. */ |
327 | 0 | if (changes != NULL) { |
328 | 0 | hts_savename_listed(StringBuff(opt->path_html), save, HTS_BUF(file)); |
329 | 0 | slot = changes_find(changes, file); |
330 | 0 | if (slot >= 0) { |
331 | 0 | changes_entry *const entry = &TypedArrayNth(changes->entries, slot); |
332 | |
|
333 | 0 | entry->has_new = HTS_TRUE; |
334 | 0 | memcpy(entry->new_digest, new_digest, DIGEST_SIZE); |
335 | 0 | entry->has_prev = has_prev; |
336 | 0 | if (has_prev) |
337 | 0 | memcpy(entry->prev_digest, prev_digest, DIGEST_SIZE); |
338 | 0 | } |
339 | 0 | } |
340 | 0 | hts_mutexrelease(&changes_mutex); |
341 | 0 | } |
342 | | |
343 | 0 | void hts_changes_dropped(httrackp *opt, const char *file, hts_boolean kept) { |
344 | 0 | hts_changes *changes; |
345 | 0 | hts_boolean is_new; |
346 | 0 | intptr_t slot; |
347 | |
|
348 | 0 | if (!opt->changes || file == NULL || !strnotempty(file)) |
349 | 0 | return; |
350 | 0 | hts_mutexlock(&changes_mutex); |
351 | 0 | changes = changes_get(opt); |
352 | 0 | if (changes != NULL) { |
353 | 0 | slot = changes_slot(changes, file, &is_new); |
354 | 0 | if (slot >= 0 && is_new) { |
355 | 0 | changes_entry *const entry = &TypedArrayNth(changes->entries, slot); |
356 | |
|
357 | 0 | entry->url = strdupt(""); |
358 | 0 | entry->listed_prev = HTS_TRUE; |
359 | | /* A kept file leaves rewritten clear, which resolves to unchanged. */ |
360 | 0 | if (!kept) |
361 | 0 | entry->bucket = HTS_CHANGE_GONE; |
362 | 0 | } |
363 | 0 | } |
364 | 0 | hts_mutexrelease(&changes_mutex); |
365 | 0 | } |
366 | | |
367 | 0 | void hts_changes_previous(httrackp *opt, const char *file) { |
368 | 0 | hts_changes *changes; |
369 | 0 | intptr_t slot; |
370 | |
|
371 | 0 | if (!opt->changes || file == NULL) |
372 | 0 | return; |
373 | 0 | hts_mutexlock(&changes_mutex); |
374 | 0 | changes = changes_get(opt); |
375 | 0 | if (changes != NULL) { |
376 | 0 | changes->has_index = HTS_TRUE; |
377 | 0 | changes->old_index = HTS_TRUE; |
378 | 0 | slot = changes_find(changes, file); |
379 | 0 | if (slot >= 0) |
380 | 0 | TypedArrayNth(changes->entries, slot).listed_prev = HTS_TRUE; |
381 | 0 | } |
382 | 0 | hts_mutexrelease(&changes_mutex); |
383 | 0 | } |
384 | | |
385 | 0 | void hts_changes_indexed(httrackp *opt) { |
386 | 0 | hts_changes *changes; |
387 | |
|
388 | 0 | if (!opt->changes) |
389 | 0 | return; |
390 | 0 | hts_mutexlock(&changes_mutex); |
391 | 0 | changes = changes_get(opt); |
392 | 0 | if (changes != NULL) |
393 | 0 | changes->has_index = HTS_TRUE; |
394 | 0 | hts_mutexrelease(&changes_mutex); |
395 | 0 | } |
396 | | |
397 | | /* ------------------------------------------------------------ */ |
398 | | /* Report */ |
399 | | /* ------------------------------------------------------------ */ |
400 | | |
401 | | /* Assign each entry its final bucket from the bytes now on disk. */ |
402 | 0 | static void changes_resolve(hts_changes *changes, httrackp *opt) { |
403 | 0 | char catbuff[CATBUFF_SIZE]; |
404 | 0 | size_t i; |
405 | |
|
406 | 0 | for (i = 0; i < TypedArraySize(changes->entries); i++) { |
407 | 0 | changes_entry *const entry = &TypedArrayNth(changes->entries, i); |
408 | 0 | const char *path; |
409 | 0 | unsigned char digest[DIGEST_SIZE]; |
410 | 0 | hts_boolean have_digests = HTS_FALSE; |
411 | 0 | hts_boolean digests_equal = HTS_FALSE; |
412 | |
|
413 | 0 | if (entry->bucket == HTS_CHANGE_GONE) |
414 | 0 | continue; |
415 | | |
416 | | /* The previous index decides what existed, because a failed attempt's |
417 | | partial is on disk but was never mirrored. */ |
418 | 0 | entry->existed = changes->old_index ? entry->listed_prev : entry->on_disk; |
419 | | /* a saved name too long to join reads as missing */ |
420 | 0 | path = hts_fpath_join(HTS_BUF(catbuff), StringBuff(opt->path_html), |
421 | 0 | entry->file) |
422 | 0 | ? catbuff |
423 | 0 | : ""; |
424 | 0 | entry->size = fsize_utf8(path); |
425 | 0 | if (entry->rewritten && entry->existed) { |
426 | | /* The size shortcut holds only for on-disk digests, because a parsed |
427 | | page's size moves with its links. */ |
428 | 0 | if (!entry->has_new && entry->size >= 0 && entry->prev_size >= 0 && |
429 | 0 | entry->size != entry->prev_size) { |
430 | 0 | have_digests = HTS_TRUE; /* different lengths: no need to hash */ |
431 | 0 | digests_equal = HTS_FALSE; |
432 | 0 | } else if (entry->has_prev && |
433 | 0 | (entry->has_new |
434 | 0 | ? (memcpy(digest, entry->new_digest, DIGEST_SIZE), 1) |
435 | 0 | : digest_file(path, digest))) { |
436 | 0 | have_digests = HTS_TRUE; |
437 | 0 | digests_equal = memcmp(digest, entry->prev_digest, DIGEST_SIZE) == 0 |
438 | 0 | ? HTS_TRUE |
439 | 0 | : HTS_FALSE; |
440 | 0 | } |
441 | 0 | } |
442 | 0 | entry->bucket = |
443 | 0 | hts_changes_classify(entry->rewritten, entry->existed, |
444 | 0 | entry->not_updated, have_digests, digests_equal); |
445 | 0 | } |
446 | 0 | } |
447 | | |
448 | | static const char *const bucket_names[HTS_CHANGE_BUCKETS] = { |
449 | | "new", "changed", "unchanged", "gone"}; |
450 | | |
451 | | /* Serialize the report. Call under changes_mutex. */ |
452 | 0 | static void changes_serialize(httrackp *opt, String *out) { |
453 | 0 | hts_changes *const changes = (hts_changes *) opt->changes_state; |
454 | 0 | size_t counts[HTS_CHANGE_BUCKETS]; |
455 | 0 | char date[32]; |
456 | 0 | char scratch[64]; |
457 | 0 | hts_change_bucket bucket; |
458 | 0 | size_t i; |
459 | |
|
460 | 0 | StringClear(*out); |
461 | 0 | if (changes == NULL) |
462 | 0 | return; |
463 | 0 | changes_resolve(changes, opt); |
464 | |
|
465 | 0 | memset(counts, 0, sizeof(counts)); |
466 | 0 | for (i = 0; i < TypedArraySize(changes->entries); i++) |
467 | 0 | counts[TypedArrayNth(changes->entries, i).bucket]++; |
468 | |
|
469 | 0 | hts_now_iso8601(date); |
470 | 0 | StringCat(*out, "{\n \"schema\": "); |
471 | 0 | snprintf(scratch, sizeof(scratch), "%d", HTS_CHANGES_SCHEMA); |
472 | 0 | StringCat(*out, scratch); |
473 | 0 | StringCat(*out, ",\n \"generator\": "); |
474 | 0 | hts_json_string(out, "HTTrack Website Copier/" HTTRACK_VERSION); |
475 | 0 | StringCat(*out, ",\n \"date\": "); |
476 | 0 | hts_json_string(out, date); |
477 | 0 | StringCat(*out, ",\n \"first_crawl\": "); |
478 | 0 | StringCat(*out, !changes->has_index |
479 | 0 | ? "null" |
480 | 0 | : (changes->old_index ? "false" : "true")); |
481 | 0 | StringCat(*out, ",\n \"partial\": "); |
482 | 0 | StringCat(*out, changes->overflow ? "true" : "false"); |
483 | 0 | StringCat(*out, ",\n \"purged\": "); |
484 | | /* a held-back purge deleted nothing, whatever --purge-old asked for */ |
485 | 0 | StringCat(*out, opt->delete_old && !opt->links_unqueued ? "true" : "false"); |
486 | |
|
487 | 0 | StringCat(*out, ",\n \"counts\": {"); |
488 | 0 | for (bucket = 0; bucket < HTS_CHANGE_BUCKETS; bucket++) { |
489 | 0 | StringCat(*out, bucket != 0 ? ", " : " "); |
490 | 0 | hts_json_string(out, bucket_names[bucket]); |
491 | 0 | snprintf(scratch, sizeof(scratch), ": %d", (int) counts[bucket]); |
492 | 0 | StringCat(*out, scratch); |
493 | 0 | } |
494 | 0 | StringCat(*out, " }"); |
495 | |
|
496 | 0 | for (bucket = 0; bucket < HTS_CHANGE_BUCKETS; bucket++) { |
497 | 0 | hts_boolean first = HTS_TRUE; |
498 | |
|
499 | 0 | StringCat(*out, ",\n "); |
500 | 0 | hts_json_string(out, bucket_names[bucket]); |
501 | 0 | StringCat(*out, ": ["); |
502 | 0 | for (i = 0; i < TypedArraySize(changes->entries); i++) { |
503 | 0 | const changes_entry *const entry = &TypedArrayNth(changes->entries, i); |
504 | |
|
505 | 0 | if (entry->bucket != bucket) |
506 | 0 | continue; |
507 | 0 | StringCat(*out, first ? "\n { \"url\": " : ",\n { \"url\": "); |
508 | 0 | first = HTS_FALSE; |
509 | 0 | hts_json_string(out, entry->url != NULL ? entry->url : ""); |
510 | 0 | StringCat(*out, ", \"file\": "); |
511 | 0 | hts_json_string(out, entry->file); |
512 | 0 | if (entry->size >= 0) { |
513 | 0 | snprintf(scratch, sizeof(scratch), ", \"size\": " LLintP, |
514 | 0 | (LLint) entry->size); |
515 | 0 | StringCat(*out, scratch); |
516 | 0 | } |
517 | 0 | if (bucket == HTS_CHANGE_CHANGED && entry->prev_size >= 0) { |
518 | 0 | snprintf(scratch, sizeof(scratch), ", \"previous_size\": " LLintP, |
519 | 0 | (LLint) entry->prev_size); |
520 | 0 | StringCat(*out, scratch); |
521 | 0 | } |
522 | 0 | StringCat(*out, " }"); |
523 | 0 | } |
524 | 0 | StringCat(*out, first ? "]" : "\n ]"); |
525 | 0 | } |
526 | 0 | StringCat(*out, "\n}\n"); |
527 | 0 | } |
528 | | |
529 | 0 | void hts_changes_report(httrackp *opt, String *out) { |
530 | 0 | hts_mutexlock(&changes_mutex); |
531 | 0 | changes_serialize(opt, out); |
532 | 0 | hts_mutexrelease(&changes_mutex); |
533 | 0 | } |
534 | | |
535 | 0 | void hts_changes_close_opt(httrackp *opt) { |
536 | 0 | char catbuff[CATBUFF_SIZE]; |
537 | 0 | const char *path; |
538 | 0 | String report = STRING_EMPTY; |
539 | 0 | size_t counts[HTS_CHANGE_BUCKETS]; |
540 | 0 | size_t total; |
541 | 0 | hts_boolean has_index, old_index; |
542 | 0 | FILE *fp; |
543 | |
|
544 | 0 | hts_mutexlock(&changes_mutex); |
545 | 0 | { |
546 | | /* An empty crawl still replaces the previous run's report. */ |
547 | 0 | hts_changes *const changes = changes_get(opt); |
548 | 0 | size_t i; |
549 | |
|
550 | 0 | if (changes == NULL) { |
551 | 0 | hts_mutexrelease(&changes_mutex); |
552 | 0 | return; |
553 | 0 | } |
554 | 0 | changes_serialize(opt, &report); |
555 | 0 | memset(counts, 0, sizeof(counts)); |
556 | 0 | for (i = 0; i < TypedArraySize(changes->entries); i++) |
557 | 0 | counts[TypedArrayNth(changes->entries, i).bucket]++; |
558 | 0 | total = TypedArraySize(changes->entries); |
559 | 0 | has_index = changes->has_index; |
560 | 0 | old_index = changes->old_index; |
561 | | /* Sticky: whatever the crawl's stragglers do next is not in this report. */ |
562 | 0 | changes->closed = HTS_TRUE; |
563 | 0 | } |
564 | 0 | hts_mutexrelease(&changes_mutex); |
565 | | |
566 | | /* too long a path fails the open below, which reports it */ |
567 | 0 | path = hts_fpath_join(HTS_BUF(catbuff), StringBuff(opt->path_log), |
568 | 0 | HTS_CHANGES_FILE) |
569 | 0 | ? catbuff |
570 | 0 | : ""; |
571 | 0 | fp = FOPEN(path, "wb"); |
572 | 0 | if (fp != NULL) { |
573 | 0 | const size_t len = StringLength(report); |
574 | |
|
575 | 0 | if (len != 0 && !hts_fwrite_exact(StringBuff(report), len, fp)) |
576 | 0 | hts_log_print(opt, LOG_ERROR | LOG_ERRNO, |
577 | 0 | "Unable to write the change report %s", path); |
578 | 0 | fclose(fp); |
579 | 0 | } else { |
580 | 0 | hts_log_print(opt, LOG_ERROR | LOG_ERRNO, |
581 | 0 | "Unable to create the change report %s", path); |
582 | 0 | } |
583 | |
|
584 | 0 | if (!has_index) { |
585 | 0 | hts_log_print(opt, LOG_NOTICE, |
586 | 0 | "Change report: no mirror index (the cache is off), %d files " |
587 | 0 | "mirrored, deletions not detected (%s)", |
588 | 0 | (int) total, HTS_CHANGES_FILE); |
589 | 0 | } else if (!old_index) { |
590 | 0 | hts_log_print(opt, LOG_NOTICE, |
591 | 0 | "Change report: first crawl, %d files mirrored, nothing to " |
592 | 0 | "compare against (%s)", |
593 | 0 | (int) total, HTS_CHANGES_FILE); |
594 | 0 | } else { |
595 | 0 | hts_log_print(opt, LOG_NOTICE, |
596 | 0 | "Change report: %d new, %d changed, %d unchanged, %d gone " |
597 | 0 | "(%s)", |
598 | 0 | (int) counts[HTS_CHANGE_NEW], |
599 | 0 | (int) counts[HTS_CHANGE_CHANGED], |
600 | 0 | (int) counts[HTS_CHANGE_UNCHANGED], |
601 | 0 | (int) counts[HTS_CHANGE_GONE], HTS_CHANGES_FILE); |
602 | 0 | } |
603 | |
|
604 | 0 | StringFree(report); |
605 | 0 | } |
606 | | |
607 | 0 | void hts_changes_free_opt(httrackp *opt) { |
608 | 0 | hts_changes *changes; |
609 | |
|
610 | 0 | hts_mutexlock(&changes_mutex); |
611 | 0 | changes = (hts_changes *) opt->changes_state; |
612 | 0 | changes_free(&changes); |
613 | | opt->changes_state = NULL; |
614 | 0 | hts_mutexrelease(&changes_mutex); |
615 | 0 | } |