static inline bool f_scandir_impl(const char* path, f_scandir_response_t response) { DIR* dir = opendir(path); if (dir == NULL) { return false; } struct dirent* dent = NULL; int err = 0; while (readdir_wrapper(dir, &dent, &err)) { if (!is_dots(dent->d_name)) { check_oom(sl_append_f(response->dents, (const char*)dent, dent->d_reclen)); } } bool ok = (err == 0); closedir(dir); return ok; }
static BOOL find_portable_dir(LPCWSTR base, LPWSTR *result, BOOL *existing) { WCHAR buf[4*MAX_PATH] = {0}; _snwprintf_s(buf, 4*MAX_PATH, _TRUNCATE, L"%s\\calibre-portable.exe", base); *existing = true; if (file_exists(buf)) { *result = _wcsdup(base); if (*result == NULL) { show_error(L"Out of memory"); return false; } return true; } WIN32_FIND_DATA fdFile; HANDLE hFind = NULL; _snwprintf_s(buf, 4*MAX_PATH, _TRUNCATE, L"%s\\*", base); if((hFind = FindFirstFileEx(buf, FindExInfoStandard, &fdFile, FindExSearchLimitToDirectories, NULL, 0)) != INVALID_HANDLE_VALUE) { do { if(is_dots(fdFile.cFileName)) continue; if(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { _snwprintf_s(buf, 4*MAX_PATH, _TRUNCATE, L"%s\\%s\\calibre-portable.exe", base, fdFile.cFileName); if (file_exists(buf)) { *result = _wcsdup(buf); if (*result == NULL) { show_error(L"Out of memory"); return false; } PathRemoveFileSpec(*result); FindClose(hFind); return true; } } } while(FindNextFile(hFind, &fdFile)); FindClose(hFind); } *existing = false; _snwprintf_s(buf, 4*MAX_PATH, _TRUNCATE, L"%s\\Calibre Portable", base); if (!CreateDirectory(buf, NULL) && GetLastError() != ERROR_ALREADY_EXISTS) { show_last_error(L"Failed to create Calibre Portable folder"); return false; } *result = _wcsdup(buf); if (*result == NULL) { show_error(L"Out of memory"); return false; } return true; }