2
0

FileSystem.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  1. #include "FileSystem.h"
  2. #include "Logging.h"
  3. #include "Path.h"
  4. #include "Unicode.h"
  5. #include <sys/stat.h>
  6. #if GP_PLATFORM_WINDOWS
  7. # define NOMINMAX
  8. # include <Windows.h>
  9. # include <PathCch.h>
  10. # include <Shlwapi.h>
  11. # include <shellapi.h>
  12. # include <cwctype>
  13. # include <io.h>
  14. # include <winternl.h>
  15. #else
  16. # include <sys/sendfile.h>
  17. # include <sys/types.h>
  18. # include <cerrno>
  19. # include <climits>
  20. # include <dirent.h>
  21. # include <fcntl.h>
  22. # include <libgen.h>
  23. # include <sstream>
  24. # include <unistd.h>
  25. # include <vector>
  26. #endif
  27. #include <algorithm>
  28. #include <atomic>
  29. #include <cstdio>
  30. #include <cstring>
  31. #include <ctime>
  32. #include <list>
  33. #include <map>
  34. #include <mutex>
  35. #include <string>
  36. #include <thread>
  37. #if GP_PLATFORM_WINDOWS
  38. # define strncasecmp(x, y, z) _strnicmp(x, y, z)
  39. # define MY_FILENO _fileno
  40. #else
  41. # define MY_FILENO fileno
  42. #endif
  43. namespace gameplay
  44. {
  45. enum class FileOp
  46. {
  47. NONE,
  48. READ,
  49. WRITE,
  50. };
  51. struct File
  52. {
  53. FILE* handle;
  54. FileMode mode;
  55. FileStreamStatus streamStatus;
  56. FileOp lastOp;
  57. };
  58. struct FileSystem::Impl
  59. {
  60. std::string appExecutablePath{""};
  61. std::string appDirectoryPath{""};
  62. char* cwd{nullptr};
  63. void update_cwd(const char* cwd);
  64. };
  65. typedef uint32_t WalkFlags;
  66. constexpr WalkFlags WALK_FLAGS_RECURSIVE = (1 << 0);
  67. constexpr WalkFlags WALK_FLAGS_SYMLINKS_ARE_FILES = (1 << 1);
  68. // utility functions
  69. #if GP_PLATFORM_WINDOWS
  70. static const size_t PATH_BUFFER_LEN = 32768;
  71. static void __convert_To_lower(std::wstring& str);
  72. static std::string __winapi_errorcode_to_string(DWORD errorCode);
  73. static time_t __filetime_to_timet(FILETIME const& ft);
  74. typedef VisitAction (*OnVisitDirectoryItemFnWindows)(const std::wstring& path, DirectoryInfo* info, void* userPtr);
  75. static VisitAction __walk_directory_windows(const std::wstring& pathAbsW, const std::wstring& parentW, OnVisitDirectoryItemFnWindows fn, void* userPtr,
  76. WalkFlags flags, std::list<std::wstring>* files, std::list<std::wstring>* directories);
  77. #elif GP_PLATFORM_LINUX
  78. static const size_t PATH_BUFFER_LEN = PATH_MAX + 1;;
  79. std::vector<std::string> __split_and_fix_linux_path(const std::string& path);
  80. static VisitAction __walk_directory_linux(const std::string& pathAbs, const std::string& parent, FileSystem::OnVisitDirectoryItemFn fn , void* userPtr,
  81. WalkFlags flags, std::list<std::string>* files, std::list<std::string>* directories);
  82. #endif
  83. static std::string __resolve_path(FileSystem* fileSystem, const char* relativeOrAbsolutePath, const char* base);
  84. static void __remove_duplicated_slashes(std::string& path);
  85. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  86. // impl.
  87. FileSystem::FileSystem()
  88. {
  89. _impl = std::make_unique<FileSystem::Impl>();
  90. }
  91. FileSystem::~FileSystem()
  92. {
  93. }
  94. void FileSystem::set_app_executable_path(const char* path)
  95. {
  96. _impl->appExecutablePath = path;
  97. _impl->appDirectoryPath = Path(path).get_parent();
  98. }
  99. const char* FileSystem::get_app_executable_path() const
  100. {
  101. return _impl->appExecutablePath.c_str();
  102. }
  103. const char* FileSystem::get_app_directory_path() const
  104. {
  105. return _impl->appDirectoryPath.c_str();
  106. }
  107. void FileSystem::Impl::update_cwd(const char* path)
  108. {
  109. if (cwd == nullptr)
  110. {
  111. cwd = static_cast<char*>(GP_MALLOC(PATH_BUFFER_LEN * sizeof(char)));
  112. if (cwd == nullptr)
  113. {
  114. GP_LOG_ERROR("Failed to allocate a buffer to hold the current working directory.");
  115. return;
  116. }
  117. }
  118. size_t cwdLen = strlen(path);
  119. memcpy(cwd, path, (cwdLen + 1) * sizeof(char));
  120. }
  121. bool FileSystem::set_current_directory_path(const char* path)
  122. {
  123. #if GP_PLATFORM_WINDOWS
  124. std::string pathAbs;
  125. std::wstring winPath;
  126. BOOL success;
  127. pathAbs = __resolve_path(this, path, nullptr);
  128. winPath = Path::convert_utf8_to_windows_path(pathAbs);
  129. success = ::SetCurrentDirectoryW(winPath.c_str());
  130. if (!success)
  131. {
  132. GP_LOG_ERROR("Failed to set the current working directory to '{}'. error = " PRIu32 "{}", pathAbs.c_str(),::GetLastError());
  133. return false;
  134. }
  135. _impl->update_cwd(const_cast<char*>(pathAbs.c_str()));
  136. return true;
  137. #elif GP_PLATFORM_LINUX
  138. int result;
  139. result = chdir(path);
  140. if (result == 0)
  141. {
  142. _impl->update_cwd(path);
  143. }
  144. else
  145. {
  146. GP_LOG_ERROR("Failed to set the current working directory to '{}'.", path);
  147. return false;
  148. }
  149. return true;
  150. #endif
  151. }
  152. const char* FileSystem::get_current_directory_path()
  153. {
  154. #if GP_PLATFORM_WINDOWS
  155. size_t lengthNeeded;
  156. std::vector<wchar_t> buf;
  157. lengthNeeded = MAX_PATH;
  158. do
  159. {
  160. buf.resize(lengthNeeded);
  161. lengthNeeded = ::GetCurrentDirectoryW(static_cast<DWORD>(buf.size()), buf.data());
  162. if (lengthNeeded == 0)
  163. {
  164. GP_LOG_ERROR("Failed to retrieve the working directory.");
  165. return 0;
  166. }
  167. } while (lengthNeeded > buf.size());
  168. std::string pathStr = Path::convert_windows_to_utf8_path(buf.data());
  169. _impl->update_cwd(pathStr.c_str());
  170. return _impl->cwd;
  171. #elif GP_PLATFORM_LINUX
  172. char pathBuffer[PATH_MAX + 1];
  173. if (getcwd(pathBuffer, PATH_MAX) == nullptr)
  174. {
  175. GP_LOG_ERROR("Failed to retrieve the working directory.");
  176. return nullptr;
  177. }
  178. _impl->update_cwd(pathBuffer);
  179. return _impl->cwd;
  180. #endif
  181. }
  182. bool FileSystem::exists(const char* path)
  183. {
  184. if (!path)
  185. {
  186. GP_LOG_ERROR("Invalid path with nullptr");
  187. return false;
  188. }
  189. bool exists = false;
  190. std::string pathAbs = __resolve_path(this, path, nullptr);
  191. #if GP_PLATFORM_WINDOWS
  192. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  193. if (pathAbsW.length() == 2 && pathAbsW[1] == L':')
  194. {
  195. pathAbsW += L"\\";
  196. }
  197. if (pathAbsW.length() == 3 && pathAbsW[1] == L':' && pathAbsW[2] == L'\\')
  198. {
  199. // check for drive existence (e.g. D: or D:\)
  200. auto ret = GetDriveTypeW(pathAbsW.c_str());
  201. exists = ret != DRIVE_NO_ROOT_DIR && ret != DRIVE_UNKNOWN;
  202. }
  203. else
  204. {
  205. // check for directory for file existence
  206. exists = GetFileAttributesW(pathAbsW.c_str()) != INVALID_FILE_ATTRIBUTES;
  207. }
  208. #else
  209. exists = access(pathAbs.c_str(), F_OK) == 0;
  210. #endif
  211. return exists;
  212. }
  213. bool FileSystem::is_directory(const char* path)
  214. {
  215. if (!path)
  216. {
  217. GP_LOG_ERROR("Invalid path with nullptr");
  218. return false;
  219. }
  220. bool isDir = false;
  221. std::string pathAbs = __resolve_path(this, path, nullptr);
  222. #if GP_PLATFORM_WINDOWS
  223. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  224. const DWORD attribs = ::GetFileAttributesW(pathAbsW.c_str());
  225. isDir = (attribs != INVALID_FILE_ATTRIBUTES && (attribs & FILE_ATTRIBUTE_DIRECTORY));
  226. #else
  227. struct stat fileStat;
  228. if (stat(pathAbs.c_str(), &fileStat) == 0)
  229. {
  230. isDir = fileStat.st_mode & S_IFDIR;
  231. }
  232. #endif
  233. return isDir;
  234. }
  235. bool FileSystem::is_writable(const char* path)
  236. {
  237. if (!path)
  238. {
  239. GP_LOG_ERROR("Invalid path with nullptr");
  240. return false;
  241. }
  242. if (strncasecmp("file:", path, 5) == 0)
  243. {
  244. path += 5;
  245. }
  246. std::string pathAbs = __resolve_path(this, path, nullptr);
  247. bool isDirectory = is_directory(pathAbs.c_str());
  248. if (!isDirectory && !exists(pathAbs.c_str()))
  249. {
  250. // the file doesn't exist, we need to check the folder.
  251. pathAbs = Path(pathAbs).get_parent();
  252. // path was just the basename, so there will be no parent
  253. if (pathAbs.empty())
  254. {
  255. pathAbs = ".";
  256. }
  257. isDirectory = is_directory(pathAbs.c_str());
  258. if (!isDirectory)
  259. {
  260. // parent should be a directory. If it's not a directory, so we can't write file to it.
  261. return false;
  262. }
  263. }
  264. #if GP_PLATFORM_WINDOWS
  265. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  266. {
  267. const SECURITY_INFORMATION securityInfo =
  268. OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
  269. DWORD length = 0;
  270. if (!::GetFileSecurityW(pathAbsW.c_str(), securityInfo, nullptr, 0, &length))
  271. {
  272. if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER)
  273. {
  274. return false;
  275. }
  276. }
  277. std::unique_ptr<char[]> buffer(new (std::nothrow) char[length]);
  278. PSECURITY_DESCRIPTOR security = (PSECURITY_DESCRIPTOR)buffer.get();
  279. if (!security)
  280. {
  281. GP_LOG_CRITICAL("Failed memory allocation.");
  282. return false;
  283. }
  284. if (!::GetFileSecurity(pathAbsW.c_str(), securityInfo, security, length, &length))
  285. {
  286. return false;
  287. }
  288. HANDLE token;
  289. DWORD desiredAccess = TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ;
  290. if (!::OpenThreadToken(::GetCurrentThread(), desiredAccess, TRUE, &token))
  291. {
  292. if (!::OpenProcessToken(::GetCurrentProcess(), desiredAccess, &token))
  293. {
  294. ::CloseHandle(token);
  295. return false;
  296. }
  297. }
  298. bool result = false;
  299. HANDLE duplicateToken;
  300. if (::DuplicateToken(token, SecurityImpersonation, &duplicateToken))
  301. {
  302. PRIVILEGE_SET privileges = {};
  303. DWORD grantedAccess = 0;
  304. DWORD privilegesLength = sizeof(privileges);
  305. BOOL accessStatus = FALSE;
  306. GENERIC_MAPPING mapping;
  307. mapping.GenericRead = FILE_GENERIC_READ;
  308. mapping.GenericWrite = FILE_GENERIC_WRITE;
  309. mapping.GenericExecute = FILE_GENERIC_EXECUTE;
  310. mapping.GenericAll = FILE_ALL_ACCESS;
  311. DWORD accessMask = FILE_GENERIC_WRITE;
  312. ::MapGenericMask(&accessMask, &mapping);
  313. if (::AccessCheck(security, duplicateToken, accessMask, &mapping, &privileges, &privilegesLength, &grantedAccess, &accessStatus))
  314. {
  315. if (accessStatus)
  316. {
  317. result = true;
  318. }
  319. }
  320. ::CloseHandle(duplicateToken);
  321. }
  322. ::CloseHandle(token);
  323. if (!result)
  324. {
  325. return false;
  326. }
  327. }
  328. if (!isDirectory)
  329. {
  330. DWORD attr = ::GetFileAttributesW(pathAbsW.c_str());
  331. if (attr != INVALID_FILE_ATTRIBUTES)
  332. {
  333. if (attr & FILE_ATTRIBUTE_READONLY)
  334. {
  335. return false;
  336. }
  337. }
  338. }
  339. #else
  340. return access(pathAbs.c_str(), W_OK) == 0;
  341. #endif
  342. return true;
  343. }
  344. time_t FileSystem::get_create_time(const char* path)
  345. {
  346. std::string pathAbs = __resolve_path(this, path, nullptr);
  347. #if GP_PLATFORM_WINDOWS
  348. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  349. WIN32_FILE_ATTRIBUTE_DATA data;
  350. if (!GetFileAttributesExW(pathAbsW.c_str(), GET_FILEEX_INFO_LEVELS::GetFileExInfoStandard, &data))
  351. {
  352. DWORD err = GetLastError();
  353. GP_LOG_ERROR("Unable to get_create_time() for '%s' (GetFileAttributesExW error: %d/%s)", pathAbs.c_str(), err,
  354. __winapi_errorcode_to_string(err).c_str());
  355. return 0;
  356. }
  357. SYSTEMTIME st;
  358. FileTimeToSystemTime(&data.ftCreationTime, &st);
  359. std::tm tm;
  360. tm.tm_sec = st.wSecond;
  361. tm.tm_min = st.wMinute;
  362. tm.tm_hour = st.wHour;
  363. tm.tm_mday = st.wDay;
  364. tm.tm_mon = st.wMonth - 1;
  365. tm.tm_year = st.wYear - 1900;
  366. tm.tm_isdst = -1;
  367. return std::mktime(&tm);
  368. #elif GP_PLATFORM_LINUX
  369. struct stat info;
  370. stat(pathAbs.c_str(), &info);
  371. return info.st_ctime;
  372. #endif
  373. }
  374. time_t FileSystem::get_mod_time(const char* path)
  375. {
  376. std::string pathAbs = __resolve_path(this, path, nullptr);
  377. #if GP_PLATFORM_WINDOWS
  378. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  379. WIN32_FILE_ATTRIBUTE_DATA data;
  380. if (!GetFileAttributesExW(pathAbsW.c_str(), GET_FILEEX_INFO_LEVELS::GetFileExInfoStandard, &data))
  381. {
  382. DWORD errorcode = GetLastError();
  383. GP_LOG_ERROR("Unable to get_mod_time() for: {} (GetFileAttributesExW failed: {}-{})", path, errorcode,
  384. __winapi_errorcode_to_string(errorcode).c_str());
  385. return 0;
  386. }
  387. SYSTEMTIME st;
  388. FileTimeToSystemTime(&data.ftLastWriteTime, &st);
  389. std::tm tm;
  390. tm.tm_sec = st.wSecond;
  391. tm.tm_min = st.wMinute;
  392. tm.tm_hour = st.wHour;
  393. tm.tm_mday = st.wDay;
  394. tm.tm_mon = st.wMonth - 1;
  395. tm.tm_year = st.wYear - 1900;
  396. tm.tm_isdst = -1;
  397. return std::mktime(&tm);
  398. #elif GP_PLATFORM_LINUX
  399. struct stat info;
  400. stat(pathAbs.c_str(), &info);
  401. return info.st_mtime;
  402. #endif
  403. }
  404. std::string FileSystem::get_canonical_path(const char* path, const char* base)
  405. {
  406. if (!path)
  407. {
  408. return "";
  409. }
  410. const std::string resolvedPath = __resolve_path(this, path, base);
  411. std::string canonicalPath;
  412. #if GP_PLATFORM_WINDOWS
  413. const std::wstring pathNormW = Path::convert_utf8_to_windows_path(path);
  414. std::wstring pathCanonicalW = Path::get_windows_canonical_path(pathNormW);
  415. __convert_To_lower(pathCanonicalW);
  416. if (::GetFileAttributesW(pathCanonicalW.c_str()) != INVALID_FILE_ATTRIBUTES)
  417. {
  418. canonicalPath = Path::convert_windows_to_utf8_path(pathCanonicalW);
  419. }
  420. #else
  421. char buffer[PATH_MAX];
  422. if (::realpath(resolvedPath.c_str(), buffer) != nullptr)
  423. {
  424. canonicalPath = buffer;
  425. }
  426. #endif
  427. __remove_duplicated_slashes(canonicalPath);
  428. return canonicalPath;
  429. }
  430. std::string FileSystem::make_temp_directory()
  431. {
  432. std::string tempDir;
  433. #if GP_PLATFORM_WINDOWS
  434. wchar_t buffer[L_tmpnam_s];
  435. _wtmpnam_s(buffer, L_tmpnam_s);
  436. bool success = ::CreateDirectoryW(buffer, nullptr);
  437. tempDir = Path::convert_windows_to_utf8_path(buffer);
  438. #else
  439. char buffer[] = "/tmp/gameplay.XXXXXX";
  440. char* tempName = mkdtemp(buffer);
  441. bool success = (tempName != nullptr);
  442. tempDir = tempName;
  443. #endif
  444. if (!success)
  445. {
  446. GP_LOG_ERROR("Failed to create temporary directory '{}'.", tempDir.c_str());
  447. return "";
  448. }
  449. else
  450. {
  451. return tempDir;
  452. }
  453. }
  454. bool FileSystem::make_directory(const char* path, bool createMissingDirectories)
  455. {
  456. if (!createMissingDirectories)
  457. {
  458. std::string pathAbs = __resolve_path(this, path, nullptr);
  459. #if GP_PLATFORM_WINDOWS
  460. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  461. bool success = ::CreateDirectoryW(pathAbsW.c_str(), nullptr);
  462. #else
  463. bool success = mkdir(pathAbs.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0;
  464. #endif
  465. if (!success)
  466. {
  467. GP_LOG_ERROR("Failed to make directory '%s'.", path);
  468. }
  469. return success;
  470. }
  471. else
  472. {
  473. std::string pathAbs = __resolve_path(this, path, nullptr);
  474. #if GP_PLATFORM_WINDOWS
  475. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(get_canonical_path(pathAbs.c_str()));
  476. wchar_t* slash = &pathAbsW[0];
  477. if (pathAbsW.size() > 4 && wcsncmp(slash, L"\\\\?\\", 4) == 0)
  478. {
  479. // long path
  480. slash += 4;
  481. }
  482. if (wcslen(slash) > 3 && slash[1] == L':' && slash[2] == L'\\')
  483. {
  484. // absolute path with drive letter
  485. slash += 3;
  486. }
  487. bool done = false;
  488. while (!done)
  489. {
  490. // get path component
  491. slash += wcsspn(slash, L"\\");
  492. slash += wcscspn(slash, L"\\");
  493. done = (*slash == L'\0');
  494. *slash = L'\0';
  495. DWORD dwAttrib = ::GetFileAttributesW(pathAbsW.c_str());
  496. if (dwAttrib == INVALID_FILE_ATTRIBUTES)
  497. {
  498. // there is no such directory, try to create
  499. if (!::CreateDirectoryW(pathAbsW.c_str(), nullptr))
  500. {
  501. GP_LOG_ERROR("Failed to make directory'{}'.",
  502. Path::convert_windows_to_utf8_path(pathAbsW).c_str());
  503. return false;
  504. }
  505. }
  506. else if (!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY))
  507. {
  508. // there is a file with the same name
  509. GP_LOG_ERROR("Failed to make directory '%s'. File already exists on this path.",
  510. Path::convert_windows_to_utf8_path(pathAbsW).c_str());
  511. return false;
  512. }
  513. *slash = L'\\';
  514. }
  515. #else
  516. // posix realpath doesn't work for paths that don't exist, so the path has to be canonicalized manually
  517. std::string currentPath;
  518. for (auto& it : __split_and_fix_linux_path(pathAbs))
  519. {
  520. currentPath += it + '/';
  521. struct stat fileStat;
  522. if (stat(currentPath.c_str(), &fileStat) == -1)
  523. {
  524. // There is no such directory, try to create
  525. if (mkdir(currentPath.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
  526. {
  527. GP_LOG_ERROR("Failed to make directory '%s'", currentPath.c_str());
  528. return false;
  529. }
  530. }
  531. else if (!(fileStat.st_mode & S_IFDIR))
  532. {
  533. GP_LOG_ERROR("Failed to make directory '%s' File already exists on this path", currentPath.c_str());
  534. return false;
  535. }
  536. }
  537. #endif
  538. return true;
  539. }
  540. }
  541. bool FileSystem::remove_directory(const char* path)
  542. {
  543. #if GP_PLATFORM_WINDOWS
  544. std::string pathAbs = __resolve_path(this, path, nullptr);
  545. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  546. std::list<std::wstring> files;
  547. std::list<std::wstring> directories;
  548. __walk_directory_windows(
  549. pathAbsW, pathAbsW, nullptr, nullptr, WALK_FLAGS_RECURSIVE | WALK_FLAGS_SYMLINKS_ARE_FILES, &files, &directories);
  550. for (std::wstring& file : files)
  551. {
  552. if (::DeleteFileW(file.c_str()) == 0 && ::RemoveDirectoryW(file.c_str()) == 0)
  553. {
  554. GP_LOG_ERROR("Failed to delete file '%s'.", Path::convert_windows_to_utf8_path(file).c_str());
  555. return false;
  556. }
  557. }
  558. directories.emplace_front(pathAbsW);
  559. directories.reverse();
  560. for (std::wstring& directory : directories)
  561. {
  562. if (::RemoveDirectoryW(directory.c_str()) == 0)
  563. {
  564. GP_LOG_ERROR("Failed to delete directory '%s'.", Path::convert_windows_to_utf8_path(directory).c_str());
  565. return false;
  566. }
  567. }
  568. return true;
  569. #elif GP_PLATFORM_LINUX
  570. std::list<std::string> files;
  571. std::list<std::string> directories;
  572. __walk_directory_linux(
  573. path, path, nullptr, _impl, WALK_FLAGS_RECURSIVE | WALK_FLAGS_SYMLINKS_ARE_FILES, &files, &directories);
  574. for (std::string& file : files)
  575. {
  576. if (unlink(file.c_str()) != 0)
  577. {
  578. GP_LOG_ERROR("Failed to delete the file '%s'.", file.c_str());
  579. return false;
  580. }
  581. }
  582. directories.emplace_front(path);
  583. directories.reverse();
  584. for (std::string& directory : directories)
  585. {
  586. if (rmdir(directory.c_str()) != 0)
  587. {
  588. GP_LOG_ERROR("Failed to remove the directory '%s'.", directory.c_str());
  589. return false;
  590. }
  591. }
  592. return true;
  593. #endif
  594. }
  595. bool FileSystem::remove_file(const char* path)
  596. {
  597. bool success = false;
  598. std::string pathAbs = __resolve_path(this, path, nullptr);
  599. #if GP_PLATFORM_WINDOWS
  600. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  601. pathAbsW.push_back(L'\0');
  602. pathAbsW.push_back(L'\0');
  603. SHFILEOPSTRUCTW fileOperation;
  604. fileOperation.wFunc = FO_DELETE;
  605. fileOperation.pFrom = pathAbsW.c_str();
  606. fileOperation.fFlags = FOF_NO_UI | FOF_NOCONFIRMATION;
  607. int result = ::SHFileOperationW(&fileOperation);
  608. if (result != 0)
  609. {
  610. GP_LOG_ERROR("Failed to delete file: '%s' SHFileOperationW failed (error code: %d)", path, result);
  611. }
  612. else
  613. {
  614. success = true;
  615. }
  616. #elif GP_PLATFORM_LINUX
  617. success = remove(pathAbs.c_str()) == 0;
  618. #endif
  619. return success;
  620. }
  621. bool FileSystem::move(const char* src, const char* dst)
  622. {
  623. std::string srcAbs = __resolve_path(this, src, nullptr);
  624. std::string dstAbs = __resolve_path(this, dst, nullptr);
  625. #if GP_PLATFORM_WINDOWS
  626. std::wstring srcAbsW = Path::convert_utf8_to_windows_path(srcAbs);
  627. std::wstring dstAbsW = Path::convert_utf8_to_windows_path(dstAbs);
  628. if (!::MoveFileW(srcAbsW.c_str(), dstAbsW.c_str()))
  629. {
  630. GP_LOG_ERROR("Failed to move file: '%s' -> '%s' (error code %" PRIu32 ")", src, dst, ::GetLastError());
  631. return false;
  632. }
  633. return true;
  634. #elif GP_PLATFORM_LINUX
  635. if (rename(srcAbs.c_str(), dstAbs.c_str()) < 0)
  636. {
  637. GP_LOG_ERROR("Failed to move file: '%s' -> '%s' (%s)", srcAbs.c_str(), dstAbs.c_str(), strerror(errno));
  638. return false;
  639. }
  640. return true;
  641. #else
  642. return false;
  643. #endif
  644. }
  645. bool FileSystem::copy(const char* src, const char* dst)
  646. {
  647. std::string srcAbs = __resolve_path(this, src, nullptr);
  648. std::string dstAbs = __resolve_path(this, dst, nullptr);
  649. #if GP_PLATFORM_WINDOWS
  650. std::wstring srcAbsW = Path::convert_utf8_to_windows_path(srcAbs);
  651. std::wstring dstAbsW = Path::convert_utf8_to_windows_path(dstAbs);
  652. bool ok = ::CopyFileW(srcAbsW.c_str(), dstAbsW.c_str(), true);
  653. if (!ok)
  654. {
  655. GP_LOG_ERROR("Failed to copy file: '%s' -> '%s' (error code %" PRIu32 ")", src, dst, ::GetLastError());
  656. }
  657. return ok;
  658. #elif GP_PLATFORM_LINUX
  659. int input = open(srcAbs.c_str(), O_RDONLY);
  660. if (input == -1)
  661. {
  662. GP_LOG_ERROR("Failed to copy file. Failed opening file '%s' for reading (%s)", srcAbs.c_str(), strerror(errno));
  663. return false;
  664. }
  665. bool success = true;
  666. off_t offset = 0;
  667. struct stat fileinfo = {};
  668. if (fstat(input, &fileinfo) < 0)
  669. {
  670. GP_LOG_ERROR("Failed to copy file. Failed to get file size for file '%s' (%s)", srcAbs.c_str(), strerror(errno));
  671. }
  672. int output = open(dstAbs.c_str(), O_WRONLY | O_CREAT | O_EXCL, fileinfo.st_mode);
  673. if (output == -1)
  674. {
  675. GP_LOG_ERROR("Failed to copy file. Failed to create file '%s' for writing (%s)", dstAbs.c_str(), strerror(errno));
  676. close(input);
  677. return false;
  678. }
  679. while (offset < fileinfo.st_size)
  680. {
  681. ssize_t result = sendfile(output, input, &offset, fileinfo.st_size);
  682. if (result == -1)
  683. {
  684. GP_LOG_ERROR("Failed to copy file: '%s' -> '%s' (%s)", srcAbs.c_str(), dstAbs.c_str(), strerror(errno));
  685. success = false;
  686. break;
  687. }
  688. }
  689. close(input);
  690. close(output);
  691. return success;
  692. #else
  693. return false;
  694. #endif
  695. }
  696. #if GP_PLATFORM_LINUX
  697. static bool __get_file_info_linux(const char* path, FileInfo* info)
  698. {
  699. struct stat buf;
  700. if (stat(path, &buf) != 0)
  701. {
  702. return false;
  703. }
  704. info->type = !S_ISDIR(buf.st_mode) ? DirectoryItemType::FILE : DirectoryItemType::DIRECTORY;
  705. info->modTime = buf.st_mtime;
  706. info->createTime = buf.st_ctime;
  707. info->size = size_t(buf.st_size);
  708. // Use lstat to determine if it's a link
  709. if (lstat(path, &buf) != 0)
  710. {
  711. info->symlink = false;
  712. }
  713. else
  714. {
  715. info->symlink = !!S_ISLNK(buf.st_mode);
  716. }
  717. return true;
  718. }
  719. #endif
  720. bool FileSystem::get_file_info(const char* path, FileInfo* info)
  721. {
  722. std::string pathAbs = __resolve_path(this, path, nullptr);
  723. #if GP_PLATFORM_WINDOWS
  724. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  725. WIN32_FILE_ATTRIBUTE_DATA winInfo = {};
  726. if (!::GetFileAttributesExW(pathAbsW.c_str(), GetFileExInfoStandard, &winInfo))
  727. {
  728. return false;
  729. }
  730. info->type = !(winInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? DirectoryItemType::FILE :
  731. DirectoryItemType::DIRECTORY;
  732. info->modTime = __filetime_to_timet(winInfo.ftLastWriteTime);
  733. info->createTime = __filetime_to_timet(winInfo.ftCreationTime);
  734. info->size = (size_t(winInfo.nFileSizeHigh) << 32) + winInfo.nFileSizeLow;
  735. info->symlink = !!(winInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT);
  736. return true;
  737. #elif GP_PLATFORM_LINUX
  738. return __get_file_info_linux(path, info);
  739. #endif
  740. }
  741. File* FileSystem::open_file(const char* path, FileMode mode)
  742. {
  743. if (!path)
  744. {
  745. GP_LOG_ERROR("Invalid path with nullptr.");
  746. return nullptr;
  747. }
  748. std::string pathAbs = __resolve_path(this, path, nullptr);
  749. const char* modeStr;
  750. switch (mode)
  751. {
  752. case FileMode::READ:
  753. modeStr = "rb";
  754. break;
  755. case FileMode::WRITE:
  756. modeStr = "wb";
  757. break;
  758. case FileMode::APPEND:
  759. modeStr = "ab";
  760. break;
  761. case FileMode::READ_WRITE:
  762. modeStr = exists(pathAbs.c_str()) ? "rb+" : "wb+";
  763. break;
  764. default:
  765. GP_LOG_ERROR("Unknown file mode %d.", static_cast<int>(mode));
  766. return nullptr;
  767. }
  768. #if GP_PLATFORM_WINDOWS
  769. int shareMode = _SH_DENYNO;
  770. wchar_t modeW[64];
  771. MultiByteToWideChar(CP_UTF8, 0, modeStr, -1, modeW, GP_COUNTOF32(modeW));
  772. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  773. FILE* handle = _wfsopen(pathAbsW.c_str(), modeW, shareMode);
  774. #else
  775. FILE* handle = fopen(pathAbs.c_str(), modeStr);
  776. #endif
  777. if (handle == nullptr)
  778. {
  779. if (errno != ENOENT)
  780. GP_LOG_WARN("Failed to open file '{}'. errno {}", pathAbs.c_str(), errno);
  781. else
  782. GP_LOG_WARN("File '{}' does not exist", pathAbs.c_str());
  783. return nullptr;
  784. }
  785. return new File{ handle, mode, FileStreamStatus::STREAM_OK, FileOp::NONE };
  786. }
  787. void FileSystem::close_file(File* file)
  788. {
  789. GP_ASSERT(file);
  790. int res = fclose(file->handle);
  791. if (res)
  792. {
  793. GP_LOG_ERROR("Failed to close file");
  794. }
  795. delete file;
  796. }
  797. static void __get_file_stat(File* file, struct stat* info)
  798. {
  799. // flush the stream so that the most up-to-date information is queried. Without doing this,
  800. // the file size and modification time will not be returned correctly if there is still
  801. // buffered data on the stream.
  802. fflush(file->handle);
  803. fstat(MY_FILENO(file->handle), info);
  804. }
  805. size_t FileSystem::get_file_size(File* file)
  806. {
  807. struct stat info;
  808. __get_file_stat(file, &info);
  809. return info.st_size;
  810. }
  811. time_t FileSystem::get_file_create_time(File* file)
  812. {
  813. struct stat info;
  814. __get_file_stat(file, &info);
  815. return info.st_ctime;
  816. }
  817. time_t FileSystem::get_file_mod_time(File* file)
  818. {
  819. struct stat info;
  820. __get_file_stat(file, &info);
  821. return info.st_mtime;
  822. }
  823. static void __resync_file_stream(FileSystem* fileSystem, File* file, FileOp newOp)
  824. {
  825. FileOp lastOp = file->lastOp;
  826. // not in read/write mode -> nothing to do => succeed.
  827. if (file->mode != FileMode::READ_WRITE)
  828. return;
  829. file->lastOp = newOp;
  830. // the new operation does not need a resync given the previous operation => succeed.
  831. if (lastOp == FileOp::NONE || lastOp == newOp)
  832. return;
  833. // in read/write mode, each mix between a read and write operation must have an intervening
  834. // seek operation to prevent immediate failure. The seek operation will perform a sync
  835. // operation on the file stream and cause the buffered state to be revalidated even if the
  836. // seek is just a no-op. If the last operation was different from the new operation and
  837. // we're in read/write mode, we'll just do a no-op seek here to force the stream sync.
  838. fileSystem->set_file_pos(file, 0, FileWhence::CURRENT);
  839. }
  840. static void __update_file_operation_status(File* file, size_t result)
  841. {
  842. // the read or write operation didn't fail => succeed. Note that the operation may still have
  843. // reached a condition such as end-of-file by returning a short item count, but the actual
  844. // end-of-file condition won't be set or returned until the next operation.
  845. if (result != 0)
  846. {
  847. file->streamStatus = FileStreamStatus::STREAM_OK;
  848. return;
  849. }
  850. if (ferror(file->handle))
  851. file->streamStatus = FileStreamStatus::STREAM_ERROR;
  852. else if (feof(file->handle))
  853. file->streamStatus = FileStreamStatus::STREAM_EOF;
  854. clearerr(file->handle);
  855. }
  856. size_t FileSystem::read_file_chunk(File* file, void* chunk, size_t chunkSize)
  857. {
  858. size_t bytesRead;
  859. __resync_file_stream(this, file, FileOp::READ);
  860. bytesRead = fread(chunk, 1, chunkSize, file->handle);
  861. __update_file_operation_status(file, bytesRead);
  862. return bytesRead;
  863. }
  864. size_t FileSystem::write_file_chunk(File* file, void* chunk, size_t chunkSize)
  865. {
  866. size_t bytesWrote;
  867. __resync_file_stream(this, file, FileOp::WRITE);
  868. bytesWrote = fwrite(chunk, 1, chunkSize, file->handle);
  869. __update_file_operation_status(file, bytesWrote);
  870. return bytesWrote;
  871. }
  872. char* FileSystem::read_file_line(File* file, char* line, size_t maxLineSize)
  873. {
  874. GP_ASSERT(file);
  875. GP_ASSERT(line);
  876. GP_ASSERT(maxLineSize);
  877. __resync_file_stream(this, file, FileOp::READ);
  878. char* stringRead = fgets(line, int(maxLineSize), file->handle);
  879. __update_file_operation_status(file, static_cast<size_t>(reinterpret_cast<uintptr_t>(stringRead)));
  880. // likely line termination will be located in the end of string
  881. if (stringRead)
  882. {
  883. size_t end = strlen(stringRead) - 1;
  884. if ('\n' == stringRead[end])
  885. {
  886. stringRead[end] = '\0';
  887. if (end > 0 && '\r' == stringRead[end - 1])
  888. {
  889. stringRead[end - 1] = '\0';
  890. }
  891. }
  892. }
  893. return stringRead;
  894. }
  895. bool FileSystem::write_file_line(File* file, const char* line)
  896. {
  897. size_t lineLen = strlen(line);
  898. __resync_file_stream(this, file, FileOp::WRITE);
  899. size_t bytesWrote = fwrite(line, 1, lineLen, file->handle);
  900. __update_file_operation_status(file, bytesWrote);
  901. // failed the bulk of the write => fail and don't write the newline.
  902. if (bytesWrote != lineLen)
  903. {
  904. return false;
  905. }
  906. const int LF = '\n';
  907. int ret = fputc(LF, file->handle);
  908. if (ret != LF)
  909. {
  910. file->streamStatus = FileStreamStatus::STREAM_ERROR;
  911. clearerr(file->handle);
  912. return false;
  913. }
  914. return true;
  915. }
  916. void FileSystem::flush_file(File* file)
  917. {
  918. GP_ASSERT(file);
  919. fflush(file->handle);
  920. }
  921. int64_t FileSystem::get_file_pos(File* file)
  922. {
  923. GP_ASSERT(file);
  924. #if GP_PLATFORM_WINDOWS
  925. // ftell() only returns a 32-bit value on Windows even in a 64-bit build since the 'long'
  926. // type remains 32-bit. To get the file's actual size (if large), we need to call the
  927. // Windows specific 64-bit variant instead.
  928. return _ftelli64(file->handle);
  929. #elif GP_PLATFORM_LINUX
  930. return static_cast<int64_t>(ftello(file->handle));
  931. #else
  932. return static_cast<int64_t>(ftell(file->handle));
  933. #endif
  934. }
  935. bool FileSystem::set_file_pos(File* file, int64_t offsetFromWhence, FileWhence whence)
  936. {
  937. GP_ASSERT(file);
  938. int location;
  939. int result;
  940. switch (whence)
  941. {
  942. default:
  943. case FileWhence::BEGIN:
  944. location = SEEK_SET;
  945. if (offsetFromWhence < 0)
  946. {
  947. return false;
  948. }
  949. break;
  950. case FileWhence::CURRENT:
  951. location = SEEK_CUR;
  952. break;
  953. case FileWhence::END:
  954. location = SEEK_END;
  955. break;
  956. }
  957. #if GP_PLATFORM_WINDOWS
  958. result = _fseeki64(file->handle, static_cast<long long>(offsetFromWhence), location);
  959. #elif GP_PLATFORM_LINUX
  960. result = fseeko(file->handle, static_cast<off_t>(offsetFromWhence), location);
  961. #else
  962. result = fseek(file->handle, offsetFromWhence, location);
  963. #endif
  964. if (result != 0)
  965. {
  966. file->streamStatus = FileStreamStatus::STREAM_ERROR;
  967. return false;
  968. }
  969. clearerr(file->handle);
  970. file->streamStatus = FileStreamStatus::STREAM_OK;
  971. return true;
  972. }
  973. bool FileSystem::set_file_pos_begin(File* file)
  974. {
  975. return set_file_pos(file, 0, FileWhence::BEGIN);
  976. }
  977. bool FileSystem::set_file_pos_end(File* file)
  978. {
  979. return set_file_pos(file, 0, FileWhence::END);
  980. }
  981. bool FileSystem::truncate_file_at_current_pos(File* file)
  982. {
  983. int result;
  984. int64_t pos = get_file_pos(file);
  985. if (pos == -1)
  986. {
  987. return false;
  988. }
  989. #if GP_PLATFORM_WINDOWS
  990. result = _chsize_s(_fileno(file->handle), pos);
  991. #else
  992. result = ftruncate(fileno(file->handle), pos);
  993. #endif
  994. file->streamStatus = (result == 0 ? FileStreamStatus::STREAM_OK : FileStreamStatus::STREAM_ERROR);
  995. clearerr(file->handle);
  996. return (result == 0);
  997. }
  998. FileStreamStatus FileSystem::get_file_stream_status(File* file)
  999. {
  1000. return file->streamStatus;
  1001. }
  1002. void FileSystem::for_each_directory_item(const char* path, OnVisitDirectoryItemFn fn, void* userPtr, bool recursive)
  1003. {
  1004. std::string pathAbs = __resolve_path(this, path, nullptr);
  1005. std::string parent = pathAbs;
  1006. WalkFlags flags = recursive ? WALK_FLAGS_RECURSIVE : 0;
  1007. #if GP_PLATFORM_WINDOWS
  1008. std::wstring pathAbsW = Path::convert_utf8_to_windows_path(pathAbs);
  1009. struct WindowsUserData
  1010. {
  1011. OnVisitDirectoryItemFn fn;
  1012. void* userPtr;
  1013. } windowsUserData{ fn, userPtr };
  1014. auto windowsFn = [](const std::wstring& pathW, DirectoryInfo* info, void* userPtr) -> VisitAction {
  1015. auto windowsUserData = static_cast<WindowsUserData*>(userPtr);
  1016. auto path = Path::convert_windows_to_utf8_path(pathW);
  1017. info->path = path.c_str();
  1018. return windowsUserData->fn(info, windowsUserData->userPtr);
  1019. };
  1020. __walk_directory_windows(pathAbsW, pathAbsW, windowsFn, &windowsUserData, flags, nullptr, nullptr);
  1021. #elif GP_PLATFORM_LINUX
  1022. __walk_directory_linux(pathAbs, pathAbs, fn, userPtr, flags, nullptr, nullptr);
  1023. #endif
  1024. }
  1025. //////////////////////////////////////////////////////////////////////////////
  1026. // platform utility impl.
  1027. #if GP_PLATFORM_WINDOWS
  1028. void __convert_To_lower(std::wstring& str)
  1029. {
  1030. std::transform(str.begin(), str.end(), str.begin(), std::towlower);
  1031. }
  1032. std::string __winapi_errorcode_to_string(DWORD errorCode)
  1033. {
  1034. if (errorCode == 0)
  1035. {
  1036. return std::string();
  1037. }
  1038. LPWSTR resultMessageBuffer = nullptr;
  1039. const DWORD kFormatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1040. FORMAT_MESSAGE_FROM_SYSTEM |
  1041. FORMAT_MESSAGE_IGNORE_INSERTS;
  1042. const DWORD dwFormatResultCode = ::FormatMessageW(kFormatFlags, nullptr, errorCode,
  1043. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  1044. reinterpret_cast<LPWSTR>(&resultMessageBuffer), 0, nullptr);
  1045. if (dwFormatResultCode == 0)
  1046. {
  1047. const DWORD operationErrorCode = ::GetLastError();
  1048. GP_LOG_ERROR("{} couldn't translate error code {" PRIu32 "}, `FormatMessage` error code is '{" PRIu32 "}'",
  1049. __func__, errorCode, operationErrorCode);
  1050. return std::to_string(errorCode);
  1051. }
  1052. assert(resultMessageBuffer);
  1053. const auto localMemDeleter = [](LPWSTR str)
  1054. {
  1055. if (str)
  1056. {
  1057. ::LocalFree(str);
  1058. }
  1059. };
  1060. std::unique_ptr<WCHAR, decltype(localMemDeleter)> systemBuffKeeper(resultMessageBuffer, localMemDeleter);
  1061. const std::string result = Unicode::convert_wide_to_utf8(resultMessageBuffer);
  1062. return result;
  1063. }
  1064. std::wstring __get_windows_canonical_path(const std::wstring& pathW)
  1065. {
  1066. wchar_t* canonical = nullptr;
  1067. if (::PathAllocCanonicalize(pathW.c_str(), PATHCCH_ALLOW_LONG_PATHS, &canonical) == S_OK)
  1068. {
  1069. std::wstring result = canonical;
  1070. LocalFree(canonical);
  1071. return result;
  1072. }
  1073. GP_LOG_WARN("path '{}' could not be canonicalized!", Path::convert_windows_to_utf8_path(pathW).c_str());
  1074. return pathW;
  1075. }
  1076. time_t __filetime_to_timet(FILETIME const& ft)
  1077. {
  1078. ULARGE_INTEGER ull;
  1079. ull.LowPart = ft.dwLowDateTime;
  1080. ull.HighPart = ft.dwHighDateTime;
  1081. // "11644473600" is the number of seconds between the Windows epoch of midnight January 1,
  1082. // 1601 and the Unix epoch of midnight January 1, 1970 (including 89 leap days). Note that
  1083. // this does not include any leap seconds since they were not introduced into the UTC
  1084. // calendar until January 1, 1972 (despite them being introduced at that time with 10 leap
  1085. // seconds already retroactively applied).
  1086. return ull.QuadPart / 10000000ULL - 11644473600ULL;
  1087. }
  1088. VisitAction __walk_directory_windows(const std::wstring& pathAbsW, const std::wstring& parentW, OnVisitDirectoryItemFnWindows fn, void* userPtr,
  1089. WalkFlags flags, std::list<std::wstring>* files, std::list<std::wstring>* directories)
  1090. {
  1091. WIN32_FIND_DATA findData;
  1092. std::wstring searchPathW = pathAbsW + L"\\*";
  1093. HANDLE handle = ::FindFirstFileW(searchPathW.c_str(), &findData);
  1094. BOOL success;
  1095. VisitAction action = VisitAction::CONTINUE;
  1096. // failed to start the directory enumeration -> nothing to do => fail.
  1097. if (handle == INVALID_HANDLE_VALUE)
  1098. {
  1099. return action;
  1100. }
  1101. do
  1102. {
  1103. std::wstring fileNameW = findData.cFileName;
  1104. std::wstring pathW = fileNameW;
  1105. if (!parentW.empty())
  1106. {
  1107. pathW = parentW;
  1108. pathW.append(L"\\");
  1109. pathW.append(fileNameW);
  1110. }
  1111. pathW = Path::fix_windows_path_prefixes(pathW);
  1112. bool const isDirectory = !!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  1113. bool const isSymlink = !!(findData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT);
  1114. if (isDirectory && ((flags & WALK_FLAGS_SYMLINKS_ARE_FILES) == 0 || !isSymlink))
  1115. {
  1116. if (fileNameW != L"." && fileNameW != L"..")
  1117. {
  1118. if (directories)
  1119. {
  1120. directories->emplace_back(pathW);
  1121. }
  1122. action = VisitAction::CONTINUE;
  1123. if (fn)
  1124. {
  1125. DirectoryInfo info;
  1126. info.path = nullptr;
  1127. info.type = DirectoryItemType::DIRECTORY;
  1128. info.modTime = __filetime_to_timet(findData.ftLastWriteTime);
  1129. info.createTime = __filetime_to_timet(findData.ftCreationTime);
  1130. info.size = (findData.nFileSizeHigh * ((size_t)MAXDWORD + 1)) + findData.nFileSizeLow;
  1131. info.symlink = isSymlink;
  1132. if ((action = fn(pathW, &info, userPtr)) == VisitAction::STOP)
  1133. {
  1134. break;
  1135. }
  1136. }
  1137. if ((flags & WALK_FLAGS_RECURSIVE) != 0 && action == VisitAction::CONTINUE)
  1138. {
  1139. std::wstring childPathAbsW = pathAbsW;
  1140. childPathAbsW.append(L"\\");
  1141. childPathAbsW.append(fileNameW);
  1142. childPathAbsW = Path::fix_windows_path_prefixes(childPathAbsW);
  1143. if ((action = __walk_directory_windows(childPathAbsW, pathW, fn, userPtr, flags, files, directories)) == VisitAction::STOP)
  1144. {
  1145. break;
  1146. }
  1147. }
  1148. }
  1149. }
  1150. else
  1151. {
  1152. if (files)
  1153. {
  1154. files->emplace_back(pathW);
  1155. }
  1156. if (fn)
  1157. {
  1158. DirectoryInfo info;
  1159. info.path = nullptr;
  1160. info.type = DirectoryItemType::FILE;
  1161. info.modTime = __filetime_to_timet(findData.ftLastWriteTime);
  1162. info.createTime = __filetime_to_timet(findData.ftCreationTime);
  1163. info.size = (findData.nFileSizeHigh * ((size_t)MAXDWORD + 1)) + findData.nFileSizeLow;
  1164. info.symlink = isSymlink;
  1165. if ((action = fn(pathW, &info, userPtr)) != VisitAction::CONTINUE)
  1166. {
  1167. break;
  1168. }
  1169. }
  1170. }
  1171. success = ::FindNextFileW(handle, &findData);
  1172. if (!success)
  1173. {
  1174. DWORD errorCode = GetLastError();
  1175. if (errorCode != ERROR_NO_MORE_FILES)
  1176. {
  1177. GP_LOG_ERROR("FindNextFileW returned error code" PRIu32"{} inside '{}'", errorCode,
  1178. Path::convert_windows_to_utf8_path(pathAbsW).c_str());
  1179. }
  1180. }
  1181. } while (success);
  1182. // When the search handle is no longer needed, close it by using the FindClose function, not CloseHandle.
  1183. if (!::FindClose(handle))
  1184. {
  1185. GP_LOG_ERROR("FindClose returned error code " PRIu32"{} inside '{}'", ::GetLastError(),
  1186. Path::convert_windows_to_utf8_path(pathAbsW).c_str());
  1187. }
  1188. return action;
  1189. }
  1190. #endif
  1191. #if GP_PLATFORM_LINUX
  1192. std::vector<std::string> __split_and_fix_linux_path(const std::string& path)
  1193. {
  1194. std::vector<std::string> components;
  1195. std::istringstream iss(path);
  1196. std::string s;
  1197. while (getline(iss, s, '/'))
  1198. {
  1199. // remove extra slashes in the middle or end of the path
  1200. if (s.empty() && !components.empty())
  1201. {
  1202. continue;
  1203. }
  1204. // skip current directory
  1205. if (s == ".")
  1206. {
  1207. continue;
  1208. }
  1209. // go to the parent directory
  1210. if (s == "..")
  1211. {
  1212. // only if there is one
  1213. if (components.empty())
  1214. {
  1215. components.push_back(s);
  1216. }
  1217. else
  1218. {
  1219. components.pop_back();
  1220. }
  1221. }
  1222. else
  1223. {
  1224. components.push_back(s);
  1225. }
  1226. }
  1227. return components;
  1228. }
  1229. VisitAction __walk_directory_linux(const std::string& pathAbs,
  1230. const std::string& parent,
  1231. FileSystem::OnVisitDirectoryItemFn fn,
  1232. void* userPtr,
  1233. WalkFlags flags,
  1234. std::list<std::string>* files = nullptr,
  1235. std::list<std::string>* directories = nullptr)
  1236. {
  1237. struct dirent* entry;
  1238. DirectoryInfo info;
  1239. DIR* dir;
  1240. dir = opendir(pathAbs.c_str());
  1241. if (dir == nullptr)
  1242. {
  1243. switch (errno)
  1244. {
  1245. case ENOENT:
  1246. GP_LOG_INFO("Directory '{}' does not exist", pathAbs.c_str());
  1247. break;
  1248. case EACCES:
  1249. GP_LOG_INFO("Insufficient permissions for directory '{}'", pathAbs.c_str());
  1250. break;
  1251. default:
  1252. GP_LOG_ERROR("Failed to opendir() on '{}'. errno = {}", pathAbs.c_str(), errno);
  1253. break;
  1254. }
  1255. return VisitAction::CONTINUE;
  1256. }
  1257. VisitAction action = VisitAction::CONTINUE;
  1258. while ((entry = readdir(dir)) != nullptr)
  1259. {
  1260. std::string fileName = entry->d_name;
  1261. std::string path = fileName;
  1262. if (!parent.empty())
  1263. {
  1264. path = parent;
  1265. path.append("/");
  1266. path.append(fileName);
  1267. }
  1268. bool const isSymlink = (entry->d_type == DT_LNK);
  1269. if (!(flags & WALK_FLAGS_SYMLINKS_ARE_FILES) && isSymlink)
  1270. {
  1271. struct stat s;
  1272. int r = stat(path.c_str(), &s);
  1273. if (r != 0)
  1274. {
  1275. switch (errno)
  1276. {
  1277. case ENOENT:
  1278. GP_LOG_WARN("Broken symlink '{}'", path.c_str());
  1279. break;
  1280. case EACCES:
  1281. GP_LOG_INFO("Insufficient permissions for symlink '{}'", path.c_str());
  1282. break;
  1283. default:
  1284. GP_LOG_ERROR("Failed stat() on '{}'. errno = {}", path.c_str(), errno);
  1285. break;
  1286. }
  1287. }
  1288. else if (S_ISDIR(s.st_mode))
  1289. {
  1290. entry->d_type = DT_DIR;
  1291. }
  1292. }
  1293. if (entry->d_type == DT_DIR)
  1294. {
  1295. if (fileName != "." && fileName != "..")
  1296. {
  1297. if (directories != nullptr)
  1298. {
  1299. directories->emplace_back(path);
  1300. }
  1301. action = VisitAction::CONTINUE;
  1302. if (fn != nullptr)
  1303. {
  1304. // retrieve the directory's info.
  1305. if (!__get_file_info_linux(path.c_str(), &info))
  1306. {
  1307. continue;
  1308. }
  1309. info.symlink = isSymlink;
  1310. // perform the callback for the directory.
  1311. if ((action = fn(&info, userPtr)) == VisitAction::STOP)
  1312. {
  1313. break;
  1314. }
  1315. }
  1316. if ((flags & WALK_FLAGS_RECURSIVE) != 0 && action == VisitAction::CONTINUE)
  1317. {
  1318. std::string childPathAbs = pathAbs;
  1319. childPathAbs.append("/");
  1320. childPathAbs.append(fileName);
  1321. if ((action = __walk_directory_linux(childPathAbs, path, fn, userPtr, flags, files, directories)) == VisitAction::STOP)
  1322. {
  1323. break;
  1324. }
  1325. }
  1326. }
  1327. }
  1328. else if (entry->d_type == DT_REG || entry->d_type == DT_CHR || entry->d_type == DT_LNK)
  1329. {
  1330. if (files != nullptr)
  1331. {
  1332. files->emplace_back(path);
  1333. }
  1334. if (fn != nullptr)
  1335. {
  1336. // retrieve the file's info.
  1337. if (!__get_file_info_linux(path.c_str(), &info))
  1338. continue;
  1339. info.symlink = isSymlink;
  1340. // perform the callback for the file.
  1341. if ((action = fn(&info, userPtr)) != VisitAction::CONTINUE)
  1342. {
  1343. break;
  1344. }
  1345. }
  1346. }
  1347. else
  1348. {
  1349. GP_LOG_WARN("Unknown type for the object '{}' d_type = {:08x}", path.c_str(), entry->d_type);
  1350. }
  1351. }
  1352. if (closedir(dir) != 0)
  1353. {
  1354. GP_LOG_ERROR("Failed to close the directory '{}'. errno = {}", pathAbs.c_str(), errno);
  1355. }
  1356. return action;
  1357. }
  1358. #endif
  1359. std::string __resolve_path(FileSystem* fileSystem, const char* relativeOrAbsolutePath, const char* base)
  1360. {
  1361. std::string path = std::string(relativeOrAbsolutePath);
  1362. if (base != nullptr && base[0] == 0)
  1363. {
  1364. return path;
  1365. }
  1366. if (path.empty())
  1367. {
  1368. if (base != nullptr)
  1369. {
  1370. return std::string(base);
  1371. }
  1372. return fileSystem->get_current_directory_path();
  1373. }
  1374. #if GP_PLATFORM_WINDOWS
  1375. if (Path(path.c_str()).is_relative())
  1376. {
  1377. if (base != nullptr)
  1378. {
  1379. return std::string(base) + "/" + path;
  1380. }
  1381. std::string cwd = fileSystem->get_current_directory_path();
  1382. return cwd + "/" + path;
  1383. }
  1384. #else
  1385. if (base != nullptr && Path(path.c_str()).is_relative())
  1386. {
  1387. return std::string(base) + "/" + path;
  1388. }
  1389. #endif
  1390. return path;
  1391. }
  1392. void __remove_duplicated_slashes(std::string& path)
  1393. {
  1394. path.erase(std::unique(path.begin(), path.end(), [](char a, char b) { return a == '/' && b == '/'; }), path.end());
  1395. }
  1396. uint32_t FileSystem::subscribe_to_change_events(const char* path, OnChangeEventFn fn, void* userPtr)
  1397. {
  1398. // todo
  1399. return 0;
  1400. }
  1401. void FileSystem::unsubscribe_to_change_events(uint32_t id)
  1402. {
  1403. // todo
  1404. }
  1405. }