diff --git a/binaries/changlog.txt b/binaries/changlog.txt index 587df6a..bfd17e6 100644 --- a/binaries/changlog.txt +++ b/binaries/changlog.txt @@ -17,3 +17,5 @@ Add sleep() and SwitchToThread() to limit spinning v3.3: Fix bug slots used before initialization,compare and swap is protecting updating committed, but it is not protecting the memory initialization. Adding atomic_flag commit_lock to protect against that Fix bug multiple threads committing at the same time, fixed by using atomic_flag commit_lock and re-checking committed after acquiring the lock Reorder helper functions + +v3.4: Rewriting hash_worker() to export file_hashes.txt diff --git a/binaries/file_hasher_v3.4.exe b/binaries/file_hasher_v3.4.exe new file mode 100644 index 0000000..3ad25de Binary files /dev/null and b/binaries/file_hasher_v3.4.exe differ diff --git a/platform_windows.c b/platform_windows.c index 7107f7c..82dcc57 100644 --- a/platform_windows.c +++ b/platform_windows.c @@ -408,19 +408,64 @@ static void xxh3_hash_file_stream(const char *path, char *out_hex) { } // ------------------------- Hash worker -------------------------------- +// Functions without writing to disk +// static DWORD WINAPI hash_worker(LPVOID arg) { +// MPMCQueue *q = (MPMCQueue *)arg; +// +// for (;;) { +// +// FileEntry *fe = mpmc_pop(q); +// +// if (!fe) +// break; // poison pill +// +// char hash[HASH_STRLEN]; +// xxh3_hash_file_stream(fe->path, hash); +// +// atomic_fetch_add(&g_files_hashed, 1); +// +// free(fe->path); +// free(fe); +// } +// +// return 0; +// } + static DWORD WINAPI hash_worker(LPVOID arg) { MPMCQueue *q = (MPMCQueue *)arg; + static CRITICAL_SECTION append_cs; + static LONG init = 0; + + if (InterlockedCompareExchange(&init, 1, 0) == 0) { + InitializeCriticalSection(&append_cs); + } + for (;;) { - FileEntry *fe = mpmc_pop(q); - if (!fe) - break; // poison pill + break; char hash[HASH_STRLEN]; xxh3_hash_file_stream(fe->path, hash); + char created[32], modified[32]; + format_time(fe->created_time, created, sizeof(created)); + format_time(fe->modified_time, modified, sizeof(modified)); + + double size_kib = (double)fe->size_bytes / 1024.0; + + EnterCriticalSection(&append_cs); + + FILE *hf = fopen(FILE_HASHES_TXT, "a"); + if (hf) { + fprintf(hf, "%s\t%s\t%.2f\t%s\t%s\t%s\n", hash, fe->path, size_kib, + created, modified, fe->owner); + fclose(hf); + } + + LeaveCriticalSection(&append_cs); + atomic_fetch_add(&g_files_hashed, 1); free(fe->path);