FilesystemWindows.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Util/Filesystem.h>
  6. #include <AnKi/Util/Assert.h>
  7. #include <AnKi/Util/Logger.h>
  8. #include <AnKi/Util/Win32Minimal.h>
  9. namespace anki {
  10. static const U MAX_PATH_LEN = MAX_PATH - 1;
  11. Bool fileExists(const CString& filename)
  12. {
  13. DWORD dwAttrib = GetFileAttributesA(&filename[0]);
  14. return dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  15. }
  16. Bool directoryExists(const CString& filename)
  17. {
  18. DWORD dwAttrib = GetFileAttributesA(filename.cstr());
  19. return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
  20. }
  21. Error removeDirectory(const CString& dirname, GenericMemoryPoolAllocator<U8> alloc)
  22. {
  23. // For some reason dirname should be double null terminated
  24. if(dirname.getLength() > MAX_PATH - 2)
  25. {
  26. ANKI_UTIL_LOGE("Path too big");
  27. return Error::FUNCTION_FAILED;
  28. }
  29. Array<char, MAX_PATH> dirname2;
  30. memcpy(&dirname2[0], &dirname[0], dirname.getLength() + 1);
  31. dirname2[dirname.getLength() + 1] = '\0';
  32. Error err = Error::NONE;
  33. SHFILEOPSTRUCTA fileOperation = {
  34. NULL, FO_DELETE, &dirname2[0], "", FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT, false, 0, ""};
  35. I result = SHFileOperationA(&fileOperation);
  36. if(result != 0)
  37. {
  38. ANKI_UTIL_LOGE("Could not delete directory %s", &dirname[0]);
  39. err = Error::FUNCTION_FAILED;
  40. }
  41. return err;
  42. }
  43. Error createDirectory(const CString& dir)
  44. {
  45. Error err = Error::NONE;
  46. if(CreateDirectoryA(dir.cstr(), NULL) == 0)
  47. {
  48. ANKI_UTIL_LOGE("Failed to create directory %s", dir.cstr());
  49. err = Error::FUNCTION_FAILED;
  50. }
  51. return err;
  52. }
  53. Error getHomeDirectory(StringAuto& out)
  54. {
  55. char path[MAX_PATH];
  56. if(SHGetFolderPathA(NULL, CSIDL_PROFILE, nullptr, 0, path) != S_OK)
  57. {
  58. ANKI_UTIL_LOGE("SHGetFolderPath() failed");
  59. return Error::FUNCTION_FAILED;
  60. }
  61. out.create(path);
  62. return Error::NONE;
  63. }
  64. Error getTempDirectory(StringAuto& out)
  65. {
  66. char path[MAX_PATH + 1];
  67. const DWORD len = GetTempPathA(sizeof(path), path);
  68. if(len == 0)
  69. {
  70. ANKI_UTIL_LOGE("GetTempPathA() failed");
  71. return Error::FUNCTION_FAILED;
  72. }
  73. out.create(path);
  74. return Error::NONE;
  75. }
  76. static Error walkDirectoryTreeRecursive(const CString& dir, const Function<Error(const CString&, Bool)>& callback,
  77. U baseDirLen)
  78. {
  79. // Append something to the path
  80. if(dir.getLength() > MAX_PATH_LEN - 2)
  81. {
  82. ANKI_UTIL_LOGE("Path too long");
  83. return Error::FUNCTION_FAILED;
  84. }
  85. Array<char, MAX_PATH> dir2;
  86. memcpy(&dir2[0], &dir[0], dir.getLength());
  87. if(dir[dir.getLength() - 1] != '/')
  88. {
  89. dir2[dir.getLength() + 0] = '/';
  90. dir2[dir.getLength() + 1] = '*';
  91. dir2[dir.getLength() + 2] = '\0';
  92. }
  93. else
  94. {
  95. dir2[dir.getLength() + 0] = '*';
  96. dir2[dir.getLength() + 1] = '\0';
  97. }
  98. // Find files
  99. HANDLE handle = INVALID_HANDLE_VALUE;
  100. WIN32_FIND_DATAA find;
  101. handle = FindFirstFileA(&dir2[0], &find);
  102. if(handle == INVALID_HANDLE_VALUE)
  103. {
  104. ANKI_UTIL_LOGE("FindFirstFile() failed");
  105. return Error::FUNCTION_FAILED;
  106. }
  107. // Remove '*' from dir2
  108. dir2[strlen(&dir2[0]) - 1] = '\0';
  109. do
  110. {
  111. CString filename(find.cFileName);
  112. if(filename != "." && filename != "..")
  113. {
  114. Bool isDir = find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
  115. // Compute new path
  116. const PtrSize oldLen = strlen(&dir2[0]);
  117. if(oldLen + filename.getLength() > MAX_PATH_LEN)
  118. {
  119. ANKI_UTIL_LOGE("Path too long");
  120. return Error::FUNCTION_FAILED;
  121. }
  122. strcat(&dir2[0], &filename[0]);
  123. const Error err = callback(&dir2[0] + baseDirLen, isDir);
  124. if(err)
  125. {
  126. FindClose(handle);
  127. return err;
  128. }
  129. // Move to next dir
  130. if(isDir)
  131. {
  132. const Error err = walkDirectoryTreeRecursive(&dir2[0], callback, baseDirLen);
  133. if(err)
  134. {
  135. FindClose(handle);
  136. return err;
  137. }
  138. }
  139. dir2[oldLen] = '\0';
  140. }
  141. } while(FindNextFileA(handle, &find) != 0);
  142. if(GetLastError() != ERROR_NO_MORE_FILES)
  143. {
  144. ANKI_UTIL_LOGE("Unknown error");
  145. FindClose(handle);
  146. return Error::FUNCTION_FAILED;
  147. }
  148. FindClose(handle);
  149. return Error::NONE;
  150. }
  151. Error walkDirectoryTreeInternal(const CString& dir, const Function<Error(const CString&, Bool)>& callback)
  152. {
  153. U baseDirLen = 0;
  154. const U len = dir.getLength();
  155. if(dir[len - 1] == '/')
  156. {
  157. baseDirLen = len;
  158. }
  159. else
  160. {
  161. baseDirLen = len + 1;
  162. }
  163. return walkDirectoryTreeRecursive(dir, callback, baseDirLen);
  164. }
  165. } // end namespace anki