SDL_sysfsops.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if defined(SDL_FSOPS_WINDOWS)
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. // System dependent filesystem routines
  22. #include "../../core/windows/SDL_windows.h"
  23. #include "../SDL_sysfilesystem.h"
  24. bool SDL_SYS_EnumerateDirectory(const char *path, const char *dirname, SDL_EnumerateDirectoryCallback cb, void *userdata)
  25. {
  26. int result = 1;
  27. if (*path == '\0') { // if empty (completely at the root), we need to enumerate drive letters.
  28. const DWORD drives = GetLogicalDrives();
  29. char name[3] = { 0, ':', '\0' };
  30. for (int i = 'A'; (result == 1) && (i <= 'Z'); i++) {
  31. if (drives & (1 << (i - 'A'))) {
  32. name[0] = (char) i;
  33. result = cb(userdata, dirname, name);
  34. }
  35. }
  36. } else {
  37. const size_t patternlen = SDL_strlen(path) + 3;
  38. char *pattern = (char *) SDL_malloc(patternlen);
  39. if (!pattern) {
  40. return -1;
  41. }
  42. // you need a wildcard to enumerate through FindFirstFileEx(), but the wildcard is only checked in the
  43. // filename element at the end of the path string, so always tack on a "\\*" to get everything, and
  44. // also prevent any wildcards inserted by the app from being respected.
  45. SDL_snprintf(pattern, patternlen, "%s\\*", path);
  46. WCHAR *wpattern = WIN_UTF8ToStringW(pattern);
  47. SDL_free(pattern);
  48. if (!wpattern) {
  49. return -1;
  50. }
  51. WIN32_FIND_DATAW entw;
  52. HANDLE dir = FindFirstFileExW(wpattern, FindExInfoStandard, &entw, FindExSearchNameMatch, NULL, 0);
  53. SDL_free(wpattern);
  54. if (dir == INVALID_HANDLE_VALUE) {
  55. WIN_SetError("Failed to enumerate directory");
  56. return -1;
  57. }
  58. do {
  59. const WCHAR *fn = entw.cFileName;
  60. if (fn[0] == '.') { // ignore "." and ".."
  61. if ((fn[1] == '\0') || ((fn[1] == '.') && (fn[2] == '\0'))) {
  62. continue;
  63. }
  64. }
  65. char *utf8fn = WIN_StringToUTF8W(fn);
  66. if (!utf8fn) {
  67. result = -1;
  68. } else {
  69. result = cb(userdata, dirname, utf8fn);
  70. SDL_free(utf8fn);
  71. }
  72. } while ((result == 1) && (FindNextFileW(dir, &entw) != 0));
  73. FindClose(dir);
  74. }
  75. return (result >= 0);
  76. }
  77. bool SDL_SYS_RemovePath(const char *path)
  78. {
  79. WCHAR *wpath = WIN_UTF8ToStringW(path);
  80. if (!wpath) {
  81. return false;
  82. }
  83. WIN32_FILE_ATTRIBUTE_DATA info;
  84. if (!GetFileAttributesExW(wpath, GetFileExInfoStandard, &info)) {
  85. SDL_free(wpath);
  86. if (GetLastError() == ERROR_FILE_NOT_FOUND) {
  87. // Note that ERROR_PATH_NOT_FOUND means a parent dir is missing, and we consider that an error.
  88. return true; // thing is already gone, call it a success.
  89. }
  90. return WIN_SetError("Couldn't get path's attributes");
  91. }
  92. const int isdir = (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  93. const BOOL rc = isdir ? RemoveDirectoryW(wpath) : DeleteFileW(wpath);
  94. SDL_free(wpath);
  95. if (!rc) {
  96. return WIN_SetError("Couldn't remove path");
  97. }
  98. return true;
  99. }
  100. bool SDL_SYS_RenamePath(const char *oldpath, const char *newpath)
  101. {
  102. WCHAR *woldpath = WIN_UTF8ToStringW(oldpath);
  103. if (!woldpath) {
  104. return false;
  105. }
  106. WCHAR *wnewpath = WIN_UTF8ToStringW(newpath);
  107. if (!wnewpath) {
  108. SDL_free(woldpath);
  109. return false;
  110. }
  111. const BOOL rc = MoveFileExW(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING);
  112. SDL_free(wnewpath);
  113. SDL_free(woldpath);
  114. if (!rc) {
  115. return WIN_SetError("Couldn't rename path");
  116. }
  117. return true;
  118. }
  119. bool SDL_SYS_CopyFile(const char *oldpath, const char *newpath)
  120. {
  121. WCHAR *woldpath = WIN_UTF8ToStringW(oldpath);
  122. if (!woldpath) {
  123. return false;
  124. }
  125. WCHAR *wnewpath = WIN_UTF8ToStringW(newpath);
  126. if (!wnewpath) {
  127. SDL_free(woldpath);
  128. return false;
  129. }
  130. const BOOL rc = CopyFileExW(woldpath, wnewpath, NULL, NULL, NULL, COPY_FILE_ALLOW_DECRYPTED_DESTINATION|COPY_FILE_NO_BUFFERING);
  131. SDL_free(wnewpath);
  132. SDL_free(woldpath);
  133. if (!rc) {
  134. return WIN_SetError("Couldn't copy path");
  135. }
  136. return true;
  137. }
  138. bool SDL_SYS_CreateDirectory(const char *path)
  139. {
  140. WCHAR *wpath = WIN_UTF8ToStringW(path);
  141. if (!wpath) {
  142. return false;
  143. }
  144. DWORD rc = CreateDirectoryW(wpath, NULL);
  145. if (!rc && (GetLastError() == ERROR_ALREADY_EXISTS)) {
  146. WIN32_FILE_ATTRIBUTE_DATA winstat;
  147. if (GetFileAttributesExW(wpath, GetFileExInfoStandard, &winstat)) {
  148. if (winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  149. rc = 1; // exists and is already a directory: cool.
  150. }
  151. }
  152. }
  153. SDL_free(wpath);
  154. if (!rc) {
  155. return WIN_SetError("Couldn't create directory");
  156. }
  157. return true;
  158. }
  159. bool SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info)
  160. {
  161. WCHAR *wpath = WIN_UTF8ToStringW(path);
  162. if (!wpath) {
  163. return false;
  164. }
  165. WIN32_FILE_ATTRIBUTE_DATA winstat;
  166. const BOOL rc = GetFileAttributesExW(wpath, GetFileExInfoStandard, &winstat);
  167. SDL_free(wpath);
  168. if (!rc) {
  169. return WIN_SetError("Can't stat");
  170. }
  171. if (winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  172. info->type = SDL_PATHTYPE_DIRECTORY;
  173. info->size = 0;
  174. } else if (winstat.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_DEVICE)) {
  175. info->type = SDL_PATHTYPE_OTHER;
  176. info->size = ((((Uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow);
  177. } else {
  178. info->type = SDL_PATHTYPE_FILE;
  179. info->size = ((((Uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow);
  180. }
  181. info->create_time = SDL_TimeFromWindows(winstat.ftCreationTime.dwLowDateTime, winstat.ftCreationTime.dwHighDateTime);
  182. info->modify_time = SDL_TimeFromWindows(winstat.ftLastWriteTime.dwLowDateTime, winstat.ftLastWriteTime.dwHighDateTime);
  183. info->access_time = SDL_TimeFromWindows(winstat.ftLastAccessTime.dwLowDateTime, winstat.ftLastAccessTime.dwHighDateTime);
  184. return true;
  185. }
  186. #endif // SDL_FSOPS_WINDOWS