SDL_genericstorage.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. #include "../SDL_sysstorage.h"
  20. static char *GENERIC_INTERNAL_CreateFullPath(const char *base, const char *relative)
  21. {
  22. const char *rel = relative;
  23. #ifdef SDL_PLATFORM_ANDROID
  24. if (rel) {
  25. // Removes any leading slash
  26. if (rel[0] == '/' || rel[0] == '\\') {
  27. rel += 1;
  28. }
  29. }
  30. #endif
  31. char *result = NULL;
  32. SDL_asprintf(&result, "%s%s", base ? base : "", rel ? rel : "");
  33. return result;
  34. }
  35. static bool GENERIC_CloseStorage(void *userdata)
  36. {
  37. SDL_free(userdata);
  38. return true;
  39. }
  40. typedef struct GenericEnumerateData
  41. {
  42. size_t base_len;
  43. SDL_EnumerateDirectoryCallback real_callback;
  44. void *real_userdata;
  45. } GenericEnumerateData;
  46. static SDL_EnumerationResult SDLCALL GENERIC_EnumerateDirectory(void *userdata, const char *dirname, const char *fname)
  47. {
  48. // SDL_EnumerateDirectory will return the full path, so for Storage we
  49. // can take the base directory and add its length to the dirname string,
  50. // effectively trimming the root without having to strdup anything.
  51. const GenericEnumerateData *wrap_data = (GenericEnumerateData *)userdata;
  52. dirname += wrap_data->base_len; // skip the base, just return the part inside of the Storage.
  53. #ifdef SDL_PLATFORM_WINDOWS
  54. char *dirnamecpy = NULL;
  55. const size_t slen = SDL_strlen(dirname);
  56. if (slen && (dirname[slen - 1] == '\\')) {
  57. dirnamecpy = SDL_strdup(dirname);
  58. if (!dirnamecpy) {
  59. return SDL_ENUM_FAILURE;
  60. }
  61. dirnamecpy[slen - 1] = '/'; // storage layer always uses '/' path separators.
  62. dirname = dirnamecpy;
  63. }
  64. const SDL_EnumerationResult retval = wrap_data->real_callback(wrap_data->real_userdata, dirname, fname);
  65. SDL_free(dirnamecpy);
  66. return retval;
  67. #else
  68. return wrap_data->real_callback(wrap_data->real_userdata, dirname, fname);
  69. #endif
  70. }
  71. static bool GENERIC_EnumerateStorageDirectory(void *userdata, const char *path, SDL_EnumerateDirectoryCallback callback, void *callback_userdata)
  72. {
  73. bool result = false;
  74. GenericEnumerateData wrap_data;
  75. char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
  76. if (fullpath) {
  77. wrap_data.base_len = userdata ? SDL_strlen((char *)userdata) : 0;
  78. wrap_data.real_callback = callback;
  79. wrap_data.real_userdata = callback_userdata;
  80. result = SDL_EnumerateDirectory(fullpath, GENERIC_EnumerateDirectory, &wrap_data);
  81. SDL_free(fullpath);
  82. }
  83. return result;
  84. }
  85. static bool GENERIC_GetStoragePathInfo(void *userdata, const char *path, SDL_PathInfo *info)
  86. {
  87. bool result = false;
  88. char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
  89. if (fullpath) {
  90. result = SDL_GetPathInfo(fullpath, info);
  91. SDL_free(fullpath);
  92. }
  93. return result;
  94. }
  95. static bool GENERIC_ReadStorageFile(void *userdata, const char *path, void *destination, Uint64 length)
  96. {
  97. bool result = false;
  98. if (length > SDL_SIZE_MAX) {
  99. return SDL_SetError("Read size exceeds SDL_SIZE_MAX");
  100. }
  101. char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
  102. if (fullpath) {
  103. SDL_IOStream *stream = SDL_IOFromFile(fullpath, "rb");
  104. if (stream) {
  105. // FIXME: Should SDL_ReadIO use u64 now...?
  106. if (SDL_ReadIO(stream, destination, (size_t)length) == length) {
  107. result = true;
  108. } else {
  109. SDL_SetError("File length did not exactly match the destination length");
  110. }
  111. SDL_CloseIO(stream);
  112. }
  113. SDL_free(fullpath);
  114. }
  115. return result;
  116. }
  117. static bool GENERIC_WriteStorageFile(void *userdata, const char *path, const void *source, Uint64 length)
  118. {
  119. // TODO: Recursively create subdirectories with SDL_CreateDirectory
  120. bool result = false;
  121. if (length > SDL_SIZE_MAX) {
  122. return SDL_SetError("Write size exceeds SDL_SIZE_MAX");
  123. }
  124. char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
  125. if (fullpath) {
  126. SDL_IOStream *stream = SDL_IOFromFile(fullpath, "wb");
  127. if (stream) {
  128. // FIXME: Should SDL_WriteIO use u64 now...?
  129. if (SDL_WriteIO(stream, source, (size_t)length) == length) {
  130. result = true;
  131. } else {
  132. SDL_SetError("Resulting file length did not exactly match the source length");
  133. }
  134. SDL_CloseIO(stream);
  135. }
  136. SDL_free(fullpath);
  137. }
  138. return result;
  139. }
  140. static bool GENERIC_CreateStorageDirectory(void *userdata, const char *path)
  141. {
  142. // TODO: Recursively create subdirectories with SDL_CreateDirectory
  143. bool result = false;
  144. char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
  145. if (fullpath) {
  146. result = SDL_CreateDirectory(fullpath);
  147. SDL_free(fullpath);
  148. }
  149. return result;
  150. }
  151. static bool GENERIC_RemoveStoragePath(void *userdata, const char *path)
  152. {
  153. bool result = false;
  154. char *fullpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, path);
  155. if (fullpath) {
  156. result = SDL_RemovePath(fullpath);
  157. SDL_free(fullpath);
  158. }
  159. return result;
  160. }
  161. static bool GENERIC_RenameStoragePath(void *userdata, const char *oldpath, const char *newpath)
  162. {
  163. bool result = false;
  164. char *fulloldpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, oldpath);
  165. char *fullnewpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, newpath);
  166. if (fulloldpath && fullnewpath) {
  167. result = SDL_RenamePath(fulloldpath, fullnewpath);
  168. }
  169. SDL_free(fulloldpath);
  170. SDL_free(fullnewpath);
  171. return result;
  172. }
  173. static bool GENERIC_CopyStorageFile(void *userdata, const char *oldpath, const char *newpath)
  174. {
  175. bool result = false;
  176. char *fulloldpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, oldpath);
  177. char *fullnewpath = GENERIC_INTERNAL_CreateFullPath((char *)userdata, newpath);
  178. if (fulloldpath && fullnewpath) {
  179. result = SDL_CopyFile(fulloldpath, fullnewpath);
  180. }
  181. SDL_free(fulloldpath);
  182. SDL_free(fullnewpath);
  183. return result;
  184. }
  185. static Uint64 GENERIC_GetStorageSpaceRemaining(void *userdata)
  186. {
  187. // TODO: There's totally a way to query a folder root's quota...
  188. return SDL_MAX_UINT64;
  189. }
  190. static const SDL_StorageInterface GENERIC_title_iface = {
  191. sizeof(SDL_StorageInterface),
  192. GENERIC_CloseStorage,
  193. NULL, // ready
  194. GENERIC_EnumerateStorageDirectory,
  195. GENERIC_GetStoragePathInfo,
  196. GENERIC_ReadStorageFile,
  197. NULL, // write_file
  198. NULL, // mkdir
  199. NULL, // remove
  200. NULL, // rename
  201. NULL, // copy
  202. NULL // space_remaining
  203. };
  204. static SDL_Storage *GENERIC_Title_Create(const char *override, SDL_PropertiesID props)
  205. {
  206. SDL_Storage *result = NULL;
  207. char *basepath = NULL;
  208. if (override != NULL) {
  209. const size_t slen = SDL_strlen(override);
  210. if (slen > 0) {
  211. // make sure override has a path separator at the end. If you're not on Windows and used '\\', that's on you.
  212. const bool need_sep = ((override[slen - 1] != '/') && (override[slen - 1] != '\\'));
  213. if (SDL_asprintf(&basepath, "%s%s", override, need_sep ? "/" : "") == -1) {
  214. return NULL;
  215. }
  216. } else {
  217. // override == "" -> empty base (not "/")
  218. basepath = SDL_strdup("");
  219. }
  220. } else {
  221. const char *base = SDL_GetBasePath();
  222. // On Android, SDL_GetBasePath() can be NULL: use empty base.
  223. #ifdef SDL_PLATFORM_ANDROID
  224. basepath = base ? SDL_strdup(base) : SDL_strdup("");
  225. #else
  226. basepath = base ? SDL_strdup(base) : NULL;
  227. #endif
  228. }
  229. if (basepath != NULL) {
  230. result = SDL_OpenStorage(&GENERIC_title_iface, basepath);
  231. if (result == NULL) {
  232. SDL_free(basepath); // otherwise CloseStorage will free it.
  233. }
  234. }
  235. return result;
  236. }
  237. TitleStorageBootStrap GENERIC_titlebootstrap = {
  238. "generic",
  239. "SDL generic title storage driver",
  240. GENERIC_Title_Create
  241. };
  242. static const SDL_StorageInterface GENERIC_user_iface = {
  243. sizeof(SDL_StorageInterface),
  244. GENERIC_CloseStorage,
  245. NULL, // ready
  246. GENERIC_EnumerateStorageDirectory,
  247. GENERIC_GetStoragePathInfo,
  248. GENERIC_ReadStorageFile,
  249. GENERIC_WriteStorageFile,
  250. GENERIC_CreateStorageDirectory,
  251. GENERIC_RemoveStoragePath,
  252. GENERIC_RenameStoragePath,
  253. GENERIC_CopyStorageFile,
  254. GENERIC_GetStorageSpaceRemaining
  255. };
  256. static SDL_Storage *GENERIC_User_Create(const char *org, const char *app, SDL_PropertiesID props)
  257. {
  258. SDL_Storage *result;
  259. char *prefpath = SDL_GetPrefPath(org, app);
  260. if (prefpath == NULL) {
  261. return NULL;
  262. }
  263. result = SDL_OpenStorage(&GENERIC_user_iface, prefpath);
  264. if (result == NULL) {
  265. SDL_free(prefpath); // otherwise CloseStorage will free it.
  266. }
  267. return result;
  268. }
  269. UserStorageBootStrap GENERIC_userbootstrap = {
  270. "generic",
  271. "SDL generic user storage driver",
  272. GENERIC_User_Create
  273. };
  274. static const SDL_StorageInterface GENERIC_file_iface = {
  275. sizeof(SDL_StorageInterface),
  276. GENERIC_CloseStorage,
  277. NULL, // ready
  278. GENERIC_EnumerateStorageDirectory,
  279. GENERIC_GetStoragePathInfo,
  280. GENERIC_ReadStorageFile,
  281. GENERIC_WriteStorageFile,
  282. GENERIC_CreateStorageDirectory,
  283. GENERIC_RemoveStoragePath,
  284. GENERIC_RenameStoragePath,
  285. GENERIC_CopyStorageFile,
  286. GENERIC_GetStorageSpaceRemaining
  287. };
  288. SDL_Storage *GENERIC_OpenFileStorage(const char *path)
  289. {
  290. SDL_Storage *result;
  291. char *basepath = NULL;
  292. char *prepend = NULL;
  293. bool is_absolute = false;
  294. if (!path || !*path) {
  295. #ifdef SDL_PLATFORM_WINDOWS
  296. path = "C:/";
  297. #else
  298. path = "/";
  299. #endif
  300. }
  301. #ifdef SDL_PLATFORM_WINDOWS
  302. const char ch = (char) SDL_toupper(path[0]);
  303. is_absolute = (ch == '/') || // some sort of absolute Unix-style path.
  304. (ch == '\\') || // some sort of absolute Windows-style path.
  305. (((ch >= 'A') && (ch <= 'Z')) && (path[1] == ':') && ((path[2] == '\\') || (path[2] == '/'))); // an absolute path with a drive letter.
  306. #else
  307. is_absolute = (path[0] == '/'); // some sort of absolute Unix-style path.
  308. #endif
  309. if (!is_absolute) {
  310. prepend = SDL_GetCurrentDirectory();
  311. if (!prepend) {
  312. return NULL;
  313. }
  314. }
  315. const size_t len = SDL_strlen(path);
  316. const char *appended_separator = "";
  317. #ifdef SDL_PLATFORM_WINDOWS
  318. if ((path[len-1] != '/') && (path[len-1] != '\\')) {
  319. appended_separator = "/";
  320. }
  321. #else
  322. if (path[len-1] != '/') {
  323. appended_separator = "/";
  324. }
  325. #endif
  326. const int rc = SDL_asprintf(&basepath, "%s%s%s", prepend ? prepend : "", path, appended_separator);
  327. SDL_free(prepend);
  328. if (rc < 0) {
  329. return NULL;
  330. }
  331. result = SDL_OpenStorage(&GENERIC_file_iface, basepath);
  332. if (result == NULL) {
  333. SDL_free(basepath);
  334. }
  335. return result;
  336. }