fix incorrect FindFirstFile logic on Windows

master
Daniel Kolesa 2016-07-06 19:51:39 +02:00
parent 85d98780ce
commit dd35b0bf05
1 changed files with 12 additions and 4 deletions

View File

@ -366,21 +366,29 @@ struct DirectoryStream {
} }
bool open(ConstCharRange path) { bool open(ConstCharRange path) {
if (p_handle || (path.size() >= 1024)) return false; if (p_handle != INVALID_HANDLE_VALUE)
char buf[1024]; return false;
if ((path.size() >= 1024) || !path.size())
return false;
char buf[1026];
memcpy(buf, &path[0], path.size()); 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); p_handle = FindFirstFile(buf, &p_data);
if (p_handle == INVALID_HANDLE_VALUE) if (p_handle == INVALID_HANDLE_VALUE)
return false; return false;
while (!strcmp(p_data.cFileName, ".") || while (!strcmp(p_data.cFileName, ".") ||
!strcmp(p_data.cFileName, "..")) !strcmp(p_data.cFileName, "..")) {
if (!FindNextFile(p_handle, &p_data)) { if (!FindNextFile(p_handle, &p_data)) {
FindClose(p_handle); FindClose(p_handle);
p_handle = INVALID_HANDLE_VALUE; p_handle = INVALID_HANDLE_VALUE;
p_data.cFileName[0] = '\0'; p_data.cFileName[0] = '\0';
return false; return false;
} }
}
p_path = path; p_path = path;
return true; return true;
} }