fileAPI.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2020 to 2022 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. // Include fileAPI without falling back on local syntax implicitly.
  24. // This prevents any local syntax from being implied in functions that are supposed to use variable pathSyntax.
  25. #define NO_IMPLICIT_PATH_SYNTAX
  26. #include "fileAPI.h"
  27. #ifdef USE_MICROSOFT_WINDOWS
  28. #include <windows.h>
  29. #else
  30. #include <unistd.h>
  31. #include <sys/stat.h>
  32. #include <dirent.h>
  33. #endif
  34. #include <fstream>
  35. #include <cstdlib>
  36. #include "bufferAPI.h"
  37. namespace dsr {
  38. constexpr const char32_t* getPathSeparator(PathSyntax pathSyntax) {
  39. if (pathSyntax == PathSyntax::Windows) {
  40. return U"\\";
  41. } else if (pathSyntax == PathSyntax::Posix) {
  42. return U"/";
  43. } else {
  44. return U"?";
  45. }
  46. }
  47. #ifdef USE_MICROSOFT_WINDOWS
  48. using NativeChar = wchar_t; // UTF-16
  49. static const CharacterEncoding nativeEncoding = CharacterEncoding::BOM_UTF16LE;
  50. #define FILE_ACCESS_FUNCTION _wfopen
  51. #define FILE_ACCESS_SELECTION (write ? L"wb" : L"rb")
  52. List<String> file_impl_getInputArguments() {
  53. // Get a pointer to static memory with the command
  54. LPWSTR cmd = GetCommandLineW();
  55. // Split the arguments into an array of arguments
  56. int argc = 0;
  57. LPWSTR *argv = CommandLineToArgvW(cmd, &argc);
  58. // Convert the arguments into dsr::List<dsr::String>
  59. List<String> args = file_impl_convertInputArguments(argc, (void**)argv);
  60. // Free the native list of arguments
  61. LocalFree(argv);
  62. return args;
  63. }
  64. #else
  65. using NativeChar = char; // UTF-8
  66. static const CharacterEncoding nativeEncoding = CharacterEncoding::BOM_UTF8;
  67. #define FILE_ACCESS_FUNCTION fopen
  68. #define FILE_ACCESS_SELECTION (write ? "wb" : "rb")
  69. List<String> file_impl_getInputArguments() { return List<String>(); }
  70. #endif
  71. // Length of fixed size buffers.
  72. const int maxLength = 512;
  73. static const NativeChar* toNativeString(const ReadableString &filename, Buffer &buffer) {
  74. buffer = string_saveToMemory(filename, nativeEncoding, LineEncoding::CrLf, false, true);
  75. return (const NativeChar*)buffer_dangerous_getUnsafeData(buffer);
  76. }
  77. static String fromNativeString(const NativeChar *text) {
  78. return string_dangerous_decodeFromData(text, nativeEncoding);
  79. }
  80. List<String> file_impl_convertInputArguments(int argn, void **argv) {
  81. List<String> result;
  82. result.reserve(argn);
  83. for (int a = 0; a < argn; a++) {
  84. result.push(fromNativeString((NativeChar*)(argv[a])));
  85. }
  86. return result;
  87. }
  88. static FILE* accessFile(const ReadableString &filename, bool write) {
  89. Buffer buffer;
  90. return FILE_ACCESS_FUNCTION(toNativeString(filename, buffer), FILE_ACCESS_SELECTION);
  91. }
  92. Buffer file_loadBuffer(const ReadableString& filename, bool mustExist) {
  93. String modifiedFilename = file_optimizePath(filename, LOCAL_PATH_SYNTAX);
  94. FILE *file = accessFile(modifiedFilename, false);
  95. if (file != nullptr) {
  96. // Get the file's size by going to the end, measuring, and going back
  97. fseek(file, 0L, SEEK_END);
  98. int64_t fileSize = ftell(file);
  99. rewind(file);
  100. // Allocate a buffer of the file's size
  101. Buffer buffer = buffer_create(fileSize);
  102. fread((void*)buffer_dangerous_getUnsafeData(buffer), fileSize, 1, file);
  103. fclose(file);
  104. return buffer;
  105. } else {
  106. if (mustExist) {
  107. throwError(U"Failed to load ", modifiedFilename, ".\n");
  108. }
  109. // If the file cound not be found and opened, an empty buffer is returned
  110. return Buffer();
  111. }
  112. }
  113. void file_saveBuffer(const ReadableString& filename, Buffer buffer) {
  114. String modifiedFilename = file_optimizePath(filename, LOCAL_PATH_SYNTAX);
  115. if (!buffer_exists(buffer)) {
  116. throwError(U"buffer_save: Can't save a buffer that don't exist to a file.\n");
  117. } else {
  118. FILE *file = accessFile(modifiedFilename, true);
  119. if (file != nullptr) {
  120. fwrite((void*)buffer_dangerous_getUnsafeData(buffer), buffer_getSize(buffer), 1, file);
  121. fclose(file);
  122. } else {
  123. throwError("Failed to save ", modifiedFilename, ".\n");
  124. }
  125. }
  126. }
  127. const char32_t* file_separator() {
  128. return getPathSeparator(LOCAL_PATH_SYNTAX);
  129. }
  130. inline bool isSeparator(DsrChar c) {
  131. return c == U'\\' || c == U'/';
  132. }
  133. // Returns the index of the first / or \ in path, or defaultIndex if none existed.
  134. static int64_t getFirstSeparator(const ReadableString &path, int64_t defaultIndex) {
  135. for (int64_t i = 0; i < string_length(path); i++) {
  136. DsrChar c = path[i];
  137. if (isSeparator(c)) {
  138. return i;
  139. }
  140. }
  141. return defaultIndex;
  142. }
  143. // Returns the index of the last / or \ in path, or defaultIndex if none existed.
  144. static int64_t getLastSeparator(const ReadableString &path, int64_t defaultIndex) {
  145. for (int64_t i = string_length(path) - 1; i >= 0; i--) {
  146. DsrChar c = path[i];
  147. if (isSeparator(c)) {
  148. return i;
  149. }
  150. }
  151. return defaultIndex;
  152. }
  153. String file_optimizePath(const ReadableString &path, PathSyntax pathSyntax) {
  154. String result; // The final output being appended.
  155. String currentEntry; // The current entry.
  156. bool hadSeparator = false;
  157. bool hadContent = false;
  158. int64_t inputLength = string_length(path);
  159. string_reserve(result, inputLength);
  160. // Read null terminator from one element outside of the path to allow concluding an entry not followed by any separator.
  161. // The null terminator is not actually stored, but reading out of bound gives a null terminator.
  162. for (int64_t i = 0; i <= inputLength; i++) {
  163. DsrChar c = path[i];
  164. bool separator = isSeparator(c);
  165. if (separator || i == inputLength) {
  166. bool appendEntry = true;
  167. bool appendSeparator = separator;
  168. if (hadSeparator) {
  169. if (hadContent && string_length(currentEntry) == 0) {
  170. // Reduce non-leading // into / by skipping "" entries.
  171. // Any leading multiples of slashes have their count preserved, because some systems use them to indicate special use cases.
  172. appendEntry = false;
  173. appendSeparator = false;
  174. } else if (string_match(currentEntry, U".")) {
  175. // Reduce /./ into / by skipping "." entries.
  176. appendEntry = false;
  177. appendSeparator = false;
  178. } else if (string_match(currentEntry, U"..")) {
  179. // Reduce the parent directory against the reverse ".." entry.
  180. result = file_getRelativeParentFolder(result, pathSyntax);
  181. if (string_match(result, U"?")) {
  182. return U"?";
  183. }
  184. appendEntry = false;
  185. }
  186. }
  187. if (appendEntry) {
  188. string_append(result, string_removeOuterWhiteSpace(currentEntry));
  189. }
  190. if (appendSeparator) {
  191. string_append(result, getPathSeparator(pathSyntax));
  192. }
  193. currentEntry = U"";
  194. if (separator) {
  195. hadSeparator = true;
  196. }
  197. } else {
  198. string_appendChar(currentEntry, c);
  199. hadContent = true;
  200. }
  201. }
  202. // Remove trailing separators if we had content.
  203. if (hadSeparator && hadContent) {
  204. int64_t lastNonSeparator = -1;
  205. for (int64_t i = string_length(result) - 1; i >= 0; i--) {
  206. if (!isSeparator(result[i])) {
  207. lastNonSeparator = i;
  208. break;
  209. }
  210. }
  211. result = string_until(result, lastNonSeparator);
  212. }
  213. return result;
  214. }
  215. ReadableString file_getPathlessName(const ReadableString &path) {
  216. return string_after(path, getLastSeparator(path, -1));
  217. }
  218. String file_getRelativeParentFolder(const ReadableString &path, PathSyntax pathSyntax) {
  219. String optimizedPath = file_optimizePath(path, pathSyntax);
  220. if (string_length(optimizedPath) == 0) {
  221. // Use .. to go outside of the current directory.
  222. return U"..";
  223. } else if (string_match(file_getPathlessName(optimizedPath), U"?")) {
  224. // From unknown to unknown.
  225. return U"?";
  226. } else if (file_isRoot(optimizedPath, false, pathSyntax)) {
  227. // If it's the known true root, then we know that it does not have a parent and must fail.
  228. return U"?";
  229. } else if (file_isRoot(optimizedPath, true, pathSyntax)) {
  230. // If it's an alias for an arbitrary folder, use .. to leave it.
  231. return file_combinePaths(optimizedPath, U"..", pathSyntax);
  232. } else if (string_match(file_getPathlessName(optimizedPath), U"..")) {
  233. // Add more dots to the path.
  234. return file_combinePaths(optimizedPath, U"..", pathSyntax);
  235. } else {
  236. // Inside of something.
  237. int64_t lastSeparator = getLastSeparator(optimizedPath, 0);
  238. if (pathSyntax == PathSyntax::Windows) {
  239. // Return everything before the last separator.
  240. return string_before(optimizedPath, lastSeparator);
  241. } else { // PathSyntax::Posix
  242. if (file_hasRoot(path, false, pathSyntax) && lastSeparator == 0) {
  243. // Keep the absolute root.
  244. return U"/";
  245. } else {
  246. // Keep everything before the last separator.
  247. return string_before(optimizedPath, lastSeparator);
  248. }
  249. }
  250. }
  251. }
  252. String file_getTheoreticalAbsoluteParentFolder(const ReadableString &path, const ReadableString &currentPath, PathSyntax pathSyntax) {
  253. if (file_hasRoot(path, true, LOCAL_PATH_SYNTAX)) {
  254. // Absolute paths should be treated the same as a theoretical path.
  255. return file_getRelativeParentFolder(path, pathSyntax);
  256. } else {
  257. // If the input is not absolute, convert it before taking the parent directory.
  258. return file_getRelativeParentFolder(file_getTheoreticalAbsolutePath(path, currentPath, pathSyntax), pathSyntax);
  259. }
  260. }
  261. String file_getAbsoluteParentFolder(const ReadableString &path) {
  262. return file_getTheoreticalAbsoluteParentFolder(path, file_getCurrentPath(), LOCAL_PATH_SYNTAX);
  263. }
  264. bool file_isRoot(const ReadableString &path, bool treatHomeFolderAsRoot, PathSyntax pathSyntax) {
  265. ReadableString cleanPath = string_removeOuterWhiteSpace(path);
  266. int64_t length = string_length(cleanPath);
  267. if (length == 0) {
  268. // Relative path is not a root.
  269. return false;
  270. } else if (length == 1) {
  271. DsrChar c = cleanPath[0];
  272. if (pathSyntax == PathSyntax::Windows) {
  273. return c == U'\\'; // Implicit drive root.
  274. } else { // PathSyntax::Posix
  275. return c == U'/' || (c == U'~' && treatHomeFolderAsRoot); // Root over all drives or home folder.
  276. }
  277. } else {
  278. if (pathSyntax == PathSyntax::Windows && cleanPath[length - 1] == U':') {
  279. // C:, D:, ...
  280. return true;
  281. } else {
  282. return false;
  283. }
  284. }
  285. }
  286. bool file_hasRoot(const ReadableString &path, bool treatHomeFolderAsRoot, PathSyntax pathSyntax) {
  287. int64_t firstSeparator = getFirstSeparator(path, -1);
  288. if (firstSeparator == -1) {
  289. // If there is no separator, path has a root if it is a root.
  290. return file_isRoot(path, treatHomeFolderAsRoot, pathSyntax);
  291. } else if (firstSeparator == 0) {
  292. // Starting with a separator. Either an implicit drive on Windows or the whole system's root on Posix.
  293. return true;
  294. } else {
  295. // Has a root if the first entry before the first slash is a root.
  296. return file_isRoot(string_before(path, firstSeparator), treatHomeFolderAsRoot, pathSyntax);
  297. }
  298. }
  299. bool file_setCurrentPath(const ReadableString &path) {
  300. Buffer buffer;
  301. const NativeChar *nativePath = toNativeString(file_optimizePath(path, LOCAL_PATH_SYNTAX), buffer);
  302. #ifdef USE_MICROSOFT_WINDOWS
  303. return SetCurrentDirectoryW(nativePath);
  304. #else
  305. return chdir(nativePath) == 0;
  306. #endif
  307. }
  308. String file_getCurrentPath() {
  309. #ifdef USE_MICROSOFT_WINDOWS
  310. NativeChar resultBuffer[maxLength + 1] = {0};
  311. GetCurrentDirectoryW(maxLength, resultBuffer);
  312. return fromNativeString(resultBuffer);
  313. #else
  314. NativeChar resultBuffer[maxLength + 1] = {0};
  315. getcwd(resultBuffer, maxLength);
  316. return fromNativeString(resultBuffer);
  317. #endif
  318. }
  319. String file_followSymbolicLink(const ReadableString &path, bool mustExist) {
  320. String modifiedPath = file_optimizePath(path, LOCAL_PATH_SYNTAX);
  321. Buffer buffer;
  322. const NativeChar *nativePath = toNativeString(modifiedPath, buffer);
  323. #ifdef USE_MICROSOFT_WINDOWS
  324. // TODO: Is there anything that can be used as a symbolic link on Windows?
  325. #else
  326. NativeChar resultBuffer[maxLength + 1] = {0};
  327. if (readlink(nativePath, resultBuffer, maxLength) != -1) {
  328. return fromNativeString(resultBuffer);
  329. }
  330. #endif
  331. if (mustExist) { throwError(U"The symbolic link ", path, " could not be found!\n"); }
  332. return U"?";
  333. }
  334. String file_getApplicationFolder(bool allowFallback) {
  335. #ifdef USE_MICROSOFT_WINDOWS
  336. NativeChar resultBuffer[maxLength + 1] = {0};
  337. GetModuleFileNameW(nullptr, resultBuffer, maxLength);
  338. return file_getRelativeParentFolder(fromNativeString(resultBuffer), LOCAL_PATH_SYNTAX);
  339. #else
  340. NativeChar resultBuffer[maxLength + 1] = {0};
  341. if (readlink("/proc/self/exe", resultBuffer, maxLength) != -1) {
  342. // Linux detected
  343. return file_getAbsoluteParentFolder(fromNativeString(resultBuffer));
  344. } else if (readlink("/proc/curproc/file", resultBuffer, maxLength) != -1) {
  345. // BSD detected
  346. return file_getAbsoluteParentFolder(fromNativeString(resultBuffer));
  347. } else if (readlink("/proc/self/path/a.out", resultBuffer, maxLength) != -1) {
  348. // Solaris detected
  349. return file_getAbsoluteParentFolder(fromNativeString(resultBuffer));
  350. } else if (allowFallback) {
  351. return file_getCurrentPath();
  352. } else {
  353. throwError("file_getApplicationFolder is not implemented for the current system!\n");
  354. return U"";
  355. }
  356. #endif
  357. }
  358. String file_combinePaths(const ReadableString &a, const ReadableString &b, PathSyntax pathSyntax) {
  359. ReadableString cleanA = string_removeOuterWhiteSpace(a);
  360. ReadableString cleanB = string_removeOuterWhiteSpace(b);
  361. int64_t lengthA = string_length(cleanA);
  362. int64_t lengthB = string_length(cleanB);
  363. if (file_hasRoot(b, true, pathSyntax)) {
  364. // Restarting from root or home folder.
  365. return cleanB;
  366. } else if (lengthA == 0) {
  367. // Ignoring initial relative path, so that relative paths are not suddenly moved to the root by a new separator.
  368. return cleanB;
  369. } else if (lengthB == 0) {
  370. // Ignoring initial relative path, so that relative paths are not suddenly moved to the root by a new separator.
  371. return cleanA;
  372. } else {
  373. if (isSeparator(a[lengthA - 1])) {
  374. // Already ending with a separator.
  375. return string_combine(cleanA, cleanB);
  376. } else {
  377. // Combine using a separator.
  378. return string_combine(cleanA, getPathSeparator(pathSyntax), cleanB);
  379. }
  380. }
  381. }
  382. // Returns path with the drive letter applied from currentPath if missing in path.
  383. // Used for converting drive relative paths into true absolute paths on MS-Windows.
  384. static String applyDriveLetter(const ReadableString &path, const ReadableString &currentPath) {
  385. // Convert implicit drive into a named drive.
  386. printText("applyDriveLetter(", path, ", ", currentPath, ")\n");
  387. if (path[0] == U'\\') {
  388. printText(" Implicit drive.\n");
  389. int64_t colonIndex = string_findFirst(currentPath, U':', -1);
  390. if (colonIndex == -1) {
  391. printText(" No colon found!\n");
  392. return U"?";
  393. } else {
  394. // Get the drive letter from the current path.
  395. String drive = string_until(currentPath, colonIndex);
  396. printText(" drive = ", drive, "\n");
  397. return string_combine(drive, path);
  398. }
  399. } else {
  400. printText(" Already absolute.\n");
  401. // Already absolute.
  402. return path;
  403. }
  404. }
  405. String file_getTheoreticalAbsolutePath(const ReadableString &path, const ReadableString &currentPath, PathSyntax pathSyntax) {
  406. // Home folders are absolute enough, because we don't want to lose the account ambiguity by mangling it into hardcoded usernames.
  407. if (file_hasRoot(path, true, pathSyntax)) {
  408. if (pathSyntax == PathSyntax::Windows) {
  409. // Make sure that no drive letter is missing.
  410. return applyDriveLetter(file_optimizePath(path, pathSyntax), currentPath);
  411. } else {
  412. // Already absolute.
  413. return file_optimizePath(path, pathSyntax);
  414. }
  415. } else {
  416. // Convert from relative path.
  417. return file_optimizePath(file_combinePaths(currentPath, path, pathSyntax), pathSyntax);
  418. }
  419. }
  420. String file_getAbsolutePath(const ReadableString &path) {
  421. return file_getTheoreticalAbsolutePath(path, file_getCurrentPath(), LOCAL_PATH_SYNTAX);
  422. }
  423. int64_t file_getFileSize(const ReadableString& filename) {
  424. int64_t result = -1;
  425. String modifiedFilename = file_optimizePath(filename, LOCAL_PATH_SYNTAX);
  426. Buffer buffer;
  427. const NativeChar *nativePath = toNativeString(modifiedFilename, buffer);
  428. #ifdef USE_MICROSOFT_WINDOWS
  429. LARGE_INTEGER fileSize;
  430. HANDLE fileHandle = CreateFileW(nativePath, 0, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  431. if (fileHandle != INVALID_HANDLE_VALUE) {
  432. if (GetFileSizeEx(fileHandle, &fileSize)) {
  433. result = fileSize.QuadPart;
  434. }
  435. CloseHandle(fileHandle);
  436. }
  437. #else
  438. struct stat info;
  439. if (stat(nativePath, &info) == 0) {
  440. result = info.st_size;
  441. }
  442. #endif
  443. return result;
  444. }
  445. String& string_toStreamIndented(String& target, const EntryType& source, const ReadableString& indentation) {
  446. string_append(target, indentation);
  447. if (source == EntryType::NotFound) {
  448. string_append(target, U"not found");
  449. } else if (source == EntryType::File) {
  450. string_append(target, U"a file");
  451. } else if (source == EntryType::Folder) {
  452. string_append(target, U"a folder");
  453. } else if (source == EntryType::SymbolicLink) {
  454. string_append(target, U"a symbolic link");
  455. } else {
  456. string_append(target, U"unhandled");
  457. }
  458. return target;
  459. }
  460. EntryType file_getEntryType(const ReadableString &path) {
  461. EntryType result = EntryType::NotFound;
  462. String optimizedPath = file_optimizePath(path, LOCAL_PATH_SYNTAX);
  463. Buffer buffer;
  464. const NativeChar *nativePath = toNativeString(optimizedPath, buffer);
  465. #ifdef USE_MICROSOFT_WINDOWS
  466. DWORD dwAttrib = GetFileAttributesW(nativePath);
  467. if (dwAttrib != INVALID_FILE_ATTRIBUTES) {
  468. if (dwAttrib & FILE_ATTRIBUTE_DIRECTORY) {
  469. result = EntryType::Folder;
  470. } else {
  471. result = EntryType::File;
  472. }
  473. }
  474. #else
  475. struct stat info;
  476. int errorCode = stat(nativePath, &info);
  477. if (errorCode == 0) {
  478. if (S_ISDIR(info.st_mode)) {
  479. result = EntryType::Folder;
  480. } else if (S_ISREG(info.st_mode)) {
  481. result = EntryType::File;
  482. } else if (S_ISLNK(info.st_mode)) {
  483. result = EntryType::SymbolicLink;
  484. } else {
  485. result = EntryType::UnhandledType;
  486. }
  487. }
  488. #endif
  489. return result;
  490. }
  491. bool file_getFolderContent(const ReadableString& folderPath, std::function<void(const ReadableString& entryPath, const ReadableString& entryName, EntryType entryType)> action) {
  492. String optimizedPath = file_optimizePath(folderPath, LOCAL_PATH_SYNTAX);
  493. #ifdef USE_MICROSOFT_WINDOWS
  494. String pattern = file_combinePaths(optimizedPath, U"*.*", LOCAL_PATH_SYNTAX);
  495. Buffer buffer;
  496. const NativeChar *nativePattern = toNativeString(pattern, buffer);
  497. WIN32_FIND_DATAW findData;
  498. HANDLE findHandle = FindFirstFileW(nativePattern, &findData);
  499. if (findHandle == INVALID_HANDLE_VALUE) {
  500. return false;
  501. } else {
  502. while (true) {
  503. String entryName = fromNativeString(findData.cFileName);
  504. if (!string_match(entryName, U".") && !string_match(entryName, U"..")) {
  505. String entryPath = file_combinePaths(optimizedPath, entryName, LOCAL_PATH_SYNTAX);
  506. EntryType entryType = EntryType::UnhandledType;
  507. if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  508. entryType = EntryType::Folder;
  509. } else {
  510. entryType = EntryType::File;
  511. }
  512. action(entryPath, entryName, entryType);
  513. }
  514. if (!FindNextFileW(findHandle, &findData)) { break; }
  515. }
  516. FindClose(findHandle);
  517. }
  518. #else
  519. Buffer buffer;
  520. const NativeChar *nativePath = toNativeString(optimizedPath, buffer);
  521. DIR *directory = opendir(nativePath);
  522. if (directory == nullptr) {
  523. return false;
  524. } else {
  525. while (true) {
  526. dirent *entry = readdir(directory);
  527. if (entry != nullptr) {
  528. String entryName = fromNativeString(entry->d_name);
  529. if (!string_match(entryName, U".") && !string_match(entryName, U"..")) {
  530. String entryPath = file_combinePaths(optimizedPath, entryName, LOCAL_PATH_SYNTAX);
  531. EntryType entryType = file_getEntryType(entryPath);
  532. action(entryPath, entryName, entryType);
  533. }
  534. } else {
  535. break;
  536. }
  537. }
  538. }
  539. closedir(directory);
  540. #endif
  541. return true;
  542. }
  543. }