From 6c2ea4bce90b9673325e11249b558fdc8c31f26f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 13 Nov 2025 00:45:51 +0000 Subject: [PATCH] Fix DirectoryScanner implementation bugs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed several bugs in the DirectoryScanner implementation: Windows fixes: - Fixed snprintf call missing buffer size parameter - Fixed FindFirstFileA to use pattern (with wildcard) instead of path - Changed sys_GetLastError() to GetLastError() - Added scanner->first = false after using first result - Removed TODO comment from cFileName usage (it's the correct field) Linux fixes: - Fixed typo: sys_reddir → sys_readdir - Added missing 'done' field to DirectoryScanner struct - Initialize scanner->done = false on successful opendir - Fixed return value when scanner->done (should return 1, not -1) --- src/file_system.c | 17 ++++++++++------- src/file_system.h | 1 + 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/file_system.c b/src/file_system.c index 91cef4a..8f23a6c 100644 --- a/src/file_system.c +++ b/src/file_system.c @@ -274,13 +274,13 @@ int file_read_all(string path, string *data) int directory_scanner_init(DirectoryScanner *scanner, string path) { char pattern[PATH_MAX]; - int ret = snprintf(pattern, "%.*s\\*", path.len, path.ptr); + int ret = snprintf(pattern, sizeof(pattern), "%.*s\\*", path.len, path.ptr); if (ret < 0 || ret >= sizeof(pattern)) return -1; - scanner->handle = sys_FindFirstFileA(path, &scanner->find_data); + scanner->handle = sys_FindFirstFileA(pattern, &scanner->find_data); if (scanner->handle == INVALID_HANDLE_VALUE) { - if (sys_GetLastError() == ERROR_FILE_NOT_FOUND) { + if (GetLastError() == ERROR_FILE_NOT_FOUND) { scanner->done = true; return 0; } @@ -301,13 +301,15 @@ int directory_scanner_next(DirectoryScanner *scanner, string *name) BOOL ok = sys_FindNextFileA(scanner->handle, &scanner->find_data); if (!ok) { scanner->done = true; - if (sys_GetLastError() == ERROR_NO_MORE_FILES) + if (GetLastError() == ERROR_NO_MORE_FILES) return 1; return -1; } + } else { + scanner->first = false; } - char *p = scanner->find_data.cFileName;// TODO: is this the right field? + char *p = scanner->find_data.cFileName; *name = (string) { p, strlen(p) }; return 0; } @@ -333,15 +335,16 @@ int directory_scanner_init(DirectoryScanner *scanner, string path) return -1; } + scanner->done = false; return 0; } int directory_scanner_next(DirectoryScanner *scanner, string *name) { if (scanner->done) - return -1; + return 1; - scanner->e = sys_reddir(scanner->d); + scanner->e = sys_readdir(scanner->d); if (scanner->e == NULL) { scanner->done = true; return 1; diff --git a/src/file_system.h b/src/file_system.h index f86ec2f..19b9c67 100644 --- a/src/file_system.h +++ b/src/file_system.h @@ -18,6 +18,7 @@ typedef struct { typedef struct { DIR *d; struct dirent *e; + bool done; } DirectoryScanner; #endif