IO: improves code quality & reliablity

This commit is contained in:
李通洲
2026-04-28 09:03:24 +08:00
parent 414f7ce067
commit 4ee7dd72d0
2 changed files with 15 additions and 9 deletions
+3 -5
View File
@@ -252,14 +252,14 @@ bool ffSuppressIO(bool suppress) {
}
void listFilesRecursively(uint32_t baseLength, FFstrbuf* folder, uint8_t indentation, const char* folderName, bool pretty) {
FF_AUTO_CLOSE_FD int dfd = open(folder->chars, O_RDONLY | O_CLOEXEC);
int dfd = open(folder->chars, O_RDONLY | O_CLOEXEC | O_DIRECTORY); // Ownership of dfd will be transformed to dir
if (dfd < 0) {
return;
}
DIR* dir = fdopendir(dfd);
FF_AUTO_CLOSE_DIR DIR* dir = fdopendir(dfd);
if (dir == NULL) {
return;
return; // Should not happen
}
uint32_t folderLength = folder->length;
@@ -310,8 +310,6 @@ void listFilesRecursively(uint32_t baseLength, FFstrbuf* folder, uint8_t indenta
puts(entry->d_name);
}
closedir(dir);
}
void ffListFilesRecursively(const char* path, bool pretty) {
+12 -4
View File
@@ -387,10 +387,12 @@ void ffListFilesRecursively(const char* path, bool pretty) {
const char* ffGetTerminalResponse(const char* request, int nParams, const char* format, ...) {
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
FF_AUTO_CLOSE_FD HANDLE hConin = INVALID_HANDLE_VALUE;
DWORD inputMode;
if (!GetConsoleMode(hInput, &inputMode)) {
DWORD inputMode = 0;
bool hasInputMode = !!GetConsoleMode(hInput, &inputMode);
if (!hasInputMode) {
hConin = CreateFileW(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, NULL);
hInput = hConin;
hasInputMode = !!GetConsoleMode(hInput, &inputMode);
}
SetConsoleMode(hInput, 0);
@@ -439,12 +441,16 @@ const char* ffGetTerminalResponse(const char* request, int nParams, const char*
while (true) {
DWORD bytes = 0;
if (!ReadFile(hInput, buffer, sizeof(buffer) - 1, &bytes, NULL) || bytes == 0) {
if (!ReadFile(hInput, buffer + bytesRead, (DWORD)(sizeof(buffer) - 1 - bytesRead), &bytes, NULL) || bytes == 0) {
va_end(args);
return "ReadFile() failed";
}
bytesRead += bytes;
if (__builtin_expect(bytesRead >= sizeof(buffer) - 1, false)) {
va_end(args);
return "terminal response buffer overflow";
}
buffer[bytesRead] = '\0';
va_list cargs;
@@ -461,7 +467,9 @@ const char* ffGetTerminalResponse(const char* request, int nParams, const char*
}
}
SetConsoleMode(hInput, inputMode);
if (hasInputMode) {
SetConsoleMode(hInput, inputMode);
}
va_end(args);