From dd35b0bf05f15305ed2d4867ca568feaebd81afa Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 6 Jul 2016 19:51:39 +0200 Subject: [PATCH] fix incorrect FindFirstFile logic on Windows --- ostd/filesystem.hh | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ostd/filesystem.hh b/ostd/filesystem.hh index a3c5f4f..12694cc 100644 --- a/ostd/filesystem.hh +++ b/ostd/filesystem.hh @@ -366,21 +366,29 @@ struct DirectoryStream { } bool open(ConstCharRange path) { - if (p_handle || (path.size() >= 1024)) return false; - char buf[1024]; + if (p_handle != INVALID_HANDLE_VALUE) + return false; + if ((path.size() >= 1024) || !path.size()) + return false; + char buf[1026]; memcpy(buf, &path[0], path.size()); - buf[path.size()] = '\0'; + char *bptr = &buf[path.size()]; + /* if path ends with a slash, replace it */ + bptr -= ((*(bptr - 1) == '\\') || (*(bptr - 1) == '/')); + /* include trailing zero */ + memcpy(bptr, "\\*", 3); p_handle = FindFirstFile(buf, &p_data); if (p_handle == INVALID_HANDLE_VALUE) return false; while (!strcmp(p_data.cFileName, ".") || - !strcmp(p_data.cFileName, "..")) + !strcmp(p_data.cFileName, "..")) { if (!FindNextFile(p_handle, &p_data)) { FindClose(p_handle); p_handle = INVALID_HANDLE_VALUE; p_data.cFileName[0] = '\0'; return false; } + } p_path = path; return true; }