SDL_sysfsops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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_POSIX)
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. // System dependent filesystem routines
  22. #include "../SDL_sysfilesystem.h"
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <dirent.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #ifdef SDL_PLATFORM_ANDROID
  30. #include "../../core/android/SDL_android.h"
  31. #endif
  32. bool SDL_SYS_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback cb, void *userdata)
  33. {
  34. #ifdef SDL_PLATFORM_ANDROID
  35. if (*path != '/') {
  36. char *apath = NULL;
  37. SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
  38. if (!apath) {
  39. return false;
  40. }
  41. const bool retval = SDL_SYS_EnumerateDirectory(apath, cb, userdata);
  42. SDL_free(apath);
  43. if (retval) {
  44. return true;
  45. }
  46. }
  47. #endif
  48. #ifdef SDL_PLATFORM_IOS
  49. if (*path != '/') {
  50. char *base = SDL_GetPrefPath("", "");
  51. if (!base) {
  52. return false;
  53. }
  54. char *apath = NULL;
  55. SDL_asprintf(&apath, "%s%s", base, path);
  56. SDL_free(base);
  57. if (!apath) {
  58. return false;
  59. }
  60. const bool retval = SDL_SYS_EnumerateDirectory(apath, cb, userdata);
  61. SDL_free(apath);
  62. if (retval) {
  63. return true;
  64. }
  65. }
  66. #endif
  67. char *pathwithsep = NULL;
  68. int pathwithseplen = SDL_asprintf(&pathwithsep, "%s/", path);
  69. if ((pathwithseplen == -1) || (!pathwithsep)) {
  70. return false;
  71. }
  72. // trim down to a single path separator at the end, in case the caller added one or more.
  73. pathwithseplen--;
  74. while ((pathwithseplen > 0) && (pathwithsep[pathwithseplen] == '/')) {
  75. pathwithsep[pathwithseplen--] = '\0';
  76. }
  77. DIR *dir = opendir(pathwithsep);
  78. if (!dir) {
  79. #ifdef SDL_PLATFORM_ANDROID // Maybe it's an asset...?
  80. const bool retval = Android_JNI_EnumerateAssetDirectory(pathwithsep, cb, userdata);
  81. SDL_free(pathwithsep);
  82. return retval;
  83. #else
  84. SDL_free(pathwithsep);
  85. return SDL_SetError("Can't open directory: %s", strerror(errno));
  86. #endif
  87. }
  88. // make sure there's a path separator at the end now for the actual callback.
  89. pathwithsep[++pathwithseplen] = '/';
  90. pathwithsep[++pathwithseplen] = '\0';
  91. SDL_EnumerationResult result = SDL_ENUM_CONTINUE;
  92. struct dirent *ent;
  93. while ((result == SDL_ENUM_CONTINUE) && ((ent = readdir(dir)) != NULL)) {
  94. const char *name = ent->d_name;
  95. if ((SDL_strcmp(name, ".") == 0) || (SDL_strcmp(name, "..") == 0)) {
  96. continue;
  97. }
  98. result = cb(userdata, pathwithsep, name);
  99. }
  100. closedir(dir);
  101. SDL_free(pathwithsep);
  102. return (result != SDL_ENUM_FAILURE);
  103. }
  104. bool SDL_SYS_RemovePath(const char *path)
  105. {
  106. int rc;
  107. #ifdef SDL_PLATFORM_ANDROID
  108. if (*path == '/') {
  109. rc = remove(path);
  110. } else {
  111. char *apath = NULL;
  112. SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
  113. if (!apath) {
  114. return false;
  115. }
  116. rc = remove(apath);
  117. SDL_free(apath);
  118. }
  119. #elif defined(SDL_PLATFORM_IOS)
  120. if (*path == '/') {
  121. rc = remove(path);
  122. } else {
  123. char *base = SDL_GetPrefPath("", "");
  124. if (!base) {
  125. return false;
  126. }
  127. char *apath = NULL;
  128. SDL_asprintf(&apath, "%s%s", base, path);
  129. SDL_free(base);
  130. if (!apath) {
  131. return false;
  132. }
  133. rc = remove(apath);
  134. SDL_free(apath);
  135. }
  136. #else
  137. rc = remove(path);
  138. #endif
  139. if (rc < 0) {
  140. if (errno == ENOENT) {
  141. // It's already gone, this is a success
  142. return true;
  143. }
  144. return SDL_SetError("Can't remove path: %s", strerror(errno));
  145. }
  146. return true;
  147. }
  148. bool SDL_SYS_RenamePath(const char *oldpath, const char *newpath)
  149. {
  150. int rc;
  151. #ifdef SDL_PLATFORM_ANDROID
  152. char *aoldpath = NULL;
  153. char *anewpath = NULL;
  154. if (*oldpath != '/') {
  155. SDL_asprintf(&aoldpath, "%s/%s", SDL_GetAndroidInternalStoragePath(), oldpath);
  156. if (!aoldpath) {
  157. return false;
  158. }
  159. oldpath = aoldpath;
  160. }
  161. if (*newpath != '/') {
  162. SDL_asprintf(&anewpath, "%s/%s", SDL_GetAndroidInternalStoragePath(), newpath);
  163. if (!anewpath) {
  164. SDL_free(aoldpath);
  165. return false;
  166. }
  167. newpath = anewpath;
  168. }
  169. rc = rename(oldpath, newpath);
  170. SDL_free(aoldpath);
  171. SDL_free(anewpath);
  172. #elif defined(SDL_PLATFORM_IOS)
  173. char *base = NULL;
  174. if (*oldpath != '/' || *newpath != '/') {
  175. base = SDL_GetPrefPath("", "");
  176. if (!base) {
  177. return false;
  178. }
  179. }
  180. char *aoldpath = NULL;
  181. char *anewpath = NULL;
  182. if (*oldpath != '/') {
  183. SDL_asprintf(&aoldpath, "%s%s", base, oldpath);
  184. if (!aoldpath) {
  185. SDL_free(base);
  186. return false;
  187. }
  188. oldpath = aoldpath;
  189. }
  190. if (*newpath != '/') {
  191. SDL_asprintf(&anewpath, "%s%s", base, newpath);
  192. if (!anewpath) {
  193. SDL_free(base);
  194. SDL_free(aoldpath);
  195. return false;
  196. }
  197. newpath = anewpath;
  198. }
  199. rc = rename(oldpath, newpath);
  200. SDL_free(base);
  201. SDL_free(aoldpath);
  202. SDL_free(anewpath);
  203. #else
  204. rc = rename(oldpath, newpath);
  205. #endif
  206. if (rc < 0) {
  207. return SDL_SetError("Can't rename path: %s", strerror(errno));
  208. }
  209. return true;
  210. }
  211. bool SDL_SYS_CopyFile(const char *oldpath, const char *newpath)
  212. {
  213. char *buffer = NULL;
  214. SDL_IOStream *input = NULL;
  215. SDL_IOStream *output = NULL;
  216. const size_t maxlen = 4096;
  217. size_t len;
  218. bool result = false;
  219. input = SDL_IOFromFile(oldpath, "rb");
  220. if (!input) {
  221. goto done;
  222. }
  223. output = SDL_IOFromFile(newpath, "wb");
  224. if (!output) {
  225. goto done;
  226. }
  227. buffer = (char *)SDL_malloc(maxlen);
  228. if (!buffer) {
  229. goto done;
  230. }
  231. while ((len = SDL_ReadIO(input, buffer, maxlen)) > 0) {
  232. if (SDL_WriteIO(output, buffer, len) < len) {
  233. goto done;
  234. }
  235. }
  236. if (SDL_GetIOStatus(input) != SDL_IO_STATUS_EOF) {
  237. goto done;
  238. }
  239. SDL_CloseIO(input);
  240. input = NULL;
  241. if (!SDL_FlushIO(output)) {
  242. goto done;
  243. }
  244. result = SDL_CloseIO(output);
  245. output = NULL; // it's gone, even if it failed.
  246. done:
  247. if (output) {
  248. SDL_CloseIO(output);
  249. }
  250. if (input) {
  251. SDL_CloseIO(input);
  252. }
  253. SDL_free(buffer);
  254. return result;
  255. }
  256. bool SDL_SYS_CreateDirectory(const char *path)
  257. {
  258. int rc;
  259. #ifdef SDL_PLATFORM_ANDROID
  260. if (*path == '/') {
  261. rc = mkdir(path, 0770);
  262. } else {
  263. char *apath = NULL;
  264. SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
  265. if (!apath) {
  266. return false;
  267. }
  268. rc = mkdir(apath, 0770);
  269. SDL_free(apath);
  270. }
  271. #elif defined(SDL_PLATFORM_IOS)
  272. if (*path == '/') {
  273. rc = mkdir(path, 0770);
  274. } else {
  275. char *base = SDL_GetPrefPath("", "");
  276. if (!base) {
  277. return false;
  278. }
  279. char *apath = NULL;
  280. SDL_asprintf(&apath, "%s%s", base, path);
  281. SDL_free(base);
  282. if (!apath) {
  283. return false;
  284. }
  285. rc = mkdir(apath, 0770);
  286. SDL_free(apath);
  287. }
  288. #else
  289. rc = mkdir(path, 0770);
  290. #endif
  291. if (rc < 0) {
  292. const int origerrno = errno;
  293. if (origerrno == EEXIST) {
  294. struct stat statbuf;
  295. if ((stat(path, &statbuf) == 0) && (S_ISDIR(statbuf.st_mode))) {
  296. return true; // it already exists and it's a directory, consider it success.
  297. }
  298. }
  299. return SDL_SetError("Can't create directory: %s", strerror(origerrno));
  300. }
  301. return true;
  302. }
  303. bool SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info)
  304. {
  305. struct stat statbuf;
  306. int rc;
  307. #ifdef SDL_PLATFORM_ANDROID
  308. if (*path == '/') {
  309. rc = stat(path, &statbuf);
  310. } else {
  311. char *apath = NULL;
  312. SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path);
  313. if (!apath) {
  314. return false;
  315. }
  316. rc = stat(apath, &statbuf);
  317. SDL_free(apath);
  318. }
  319. if (rc < 0) {
  320. return Android_JNI_GetAssetPathInfo(path, info);
  321. }
  322. #elif defined(SDL_PLATFORM_IOS)
  323. if (*path == '/') {
  324. rc = stat(path, &statbuf);
  325. } else {
  326. char *base = SDL_GetPrefPath("", "");
  327. if (!base) {
  328. return false;
  329. }
  330. char *apath = NULL;
  331. SDL_asprintf(&apath, "%s%s", base, path);
  332. SDL_free(base);
  333. if (!apath) {
  334. return false;
  335. }
  336. rc = stat(apath, &statbuf);
  337. SDL_free(apath);
  338. if (rc < 0) {
  339. rc = stat(path, &statbuf);
  340. }
  341. }
  342. #else
  343. rc = stat(path, &statbuf);
  344. #endif
  345. if (rc < 0) {
  346. return SDL_SetError("Can't stat: %s", strerror(errno));
  347. } else if (S_ISREG(statbuf.st_mode)) {
  348. info->type = SDL_PATHTYPE_FILE;
  349. info->size = (Uint64) statbuf.st_size;
  350. } else if (S_ISDIR(statbuf.st_mode)) {
  351. info->type = SDL_PATHTYPE_DIRECTORY;
  352. info->size = 0;
  353. } else {
  354. info->type = SDL_PATHTYPE_OTHER;
  355. info->size = (Uint64) statbuf.st_size;
  356. }
  357. #if defined(HAVE_ST_MTIM)
  358. // POSIX.1-2008 standard
  359. info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctim.tv_sec) + statbuf.st_ctim.tv_nsec;
  360. info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtim.tv_sec) + statbuf.st_mtim.tv_nsec;
  361. info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atim.tv_sec) + statbuf.st_atim.tv_nsec;
  362. #elif defined(SDL_PLATFORM_APPLE)
  363. /* Apple platform stat structs use 'st_*timespec' naming. */
  364. info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctimespec.tv_sec) + statbuf.st_ctimespec.tv_nsec;
  365. info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtimespec.tv_sec) + statbuf.st_mtimespec.tv_nsec;
  366. info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atimespec.tv_sec) + statbuf.st_atimespec.tv_nsec;
  367. #else
  368. info->create_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_ctime);
  369. info->modify_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_mtime);
  370. info->access_time = (SDL_Time)SDL_SECONDS_TO_NS(statbuf.st_atime);
  371. #endif
  372. return true;
  373. }
  374. // Note that this is actually part of filesystem, not fsops, but everything that uses posix fsops uses this implementation, even with separate filesystem code.
  375. char *SDL_SYS_GetCurrentDirectory(void)
  376. {
  377. size_t buflen = 64;
  378. char *buf = NULL;
  379. while (true) {
  380. void *ptr = SDL_realloc(buf, buflen);
  381. if (!ptr) {
  382. SDL_free(buf);
  383. return NULL;
  384. }
  385. buf = (char *) ptr;
  386. if (getcwd(buf, buflen-1) != NULL) {
  387. break; // we got it!
  388. }
  389. if (errno == ERANGE) {
  390. buflen *= 2; // try again with a bigger buffer.
  391. continue;
  392. }
  393. SDL_free(buf);
  394. SDL_SetError("getcwd failed: %s", strerror(errno));
  395. return NULL;
  396. }
  397. // make sure there's a path separator at the end.
  398. SDL_assert(SDL_strlen(buf) < (buflen + 2));
  399. buflen = SDL_strlen(buf);
  400. if ((buflen == 0) || (buf[buflen-1] != '/')) {
  401. buf[buflen] = '/';
  402. buf[buflen + 1] = '\0';
  403. }
  404. return buf;
  405. }
  406. #endif // SDL_FSOPS_POSIX