FilesystemWindows.cpp 4.4 KB

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