SDL_storage.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include "../filesystem/SDL_sysfilesystem.h"
  21. // Available title storage drivers
  22. static TitleStorageBootStrap *titlebootstrap[] = {
  23. &GENERIC_titlebootstrap,
  24. NULL
  25. };
  26. // Available user storage drivers
  27. static UserStorageBootStrap *userbootstrap[] = {
  28. #ifdef SDL_STORAGE_STEAM
  29. &STEAM_userbootstrap,
  30. #endif
  31. #ifdef SDL_STORAGE_PRIVATE
  32. &PRIVATE_userbootstrap,
  33. #endif
  34. &GENERIC_userbootstrap,
  35. NULL
  36. };
  37. struct SDL_Storage
  38. {
  39. SDL_StorageInterface iface;
  40. void *userdata;
  41. };
  42. #define CHECK_STORAGE_MAGIC() \
  43. if (!storage) { \
  44. return SDL_SetError("Invalid storage container"); \
  45. }
  46. #define CHECK_STORAGE_MAGIC_RET(result) \
  47. if (!storage) { \
  48. SDL_SetError("Invalid storage container"); \
  49. return result; \
  50. }
  51. // we don't make any effort to convert path separators here, because a)
  52. // everything including Windows will accept a '/' separator and b) that
  53. // conversion should probably happen in the storage backend anyhow.
  54. static bool ValidateStoragePath(const char *path)
  55. {
  56. if (SDL_strchr(path, '\\')) {
  57. return SDL_SetError("Windows-style path separators ('\\') not permitted, use '/' instead.");
  58. }
  59. const char *ptr;
  60. const char *prev = path;
  61. while ((ptr = SDL_strchr(prev, '/')) != NULL) {
  62. if ((SDL_strncmp(prev, "./", 2) == 0) || (SDL_strncmp(prev, "../", 3) == 0)) {
  63. return SDL_SetError("Relative paths not permitted");
  64. }
  65. prev = ptr + 1;
  66. }
  67. // check the last path element (or the only path element).
  68. if ((SDL_strcmp(prev, ".") == 0) || (SDL_strcmp(prev, "..") == 0)) {
  69. return SDL_SetError("Relative paths not permitted");
  70. }
  71. return true;
  72. }
  73. SDL_Storage *SDL_OpenTitleStorage(const char *override, SDL_PropertiesID props)
  74. {
  75. SDL_Storage *storage = NULL;
  76. int i = 0;
  77. // Select the proper storage driver
  78. const char *driver_name = SDL_GetHint(SDL_HINT_STORAGE_TITLE_DRIVER);
  79. if (driver_name && *driver_name != 0) {
  80. const char *driver_attempt = driver_name;
  81. while (driver_attempt && *driver_attempt != 0 && !storage) {
  82. const char *driver_attempt_end = SDL_strchr(driver_attempt, ',');
  83. size_t driver_attempt_len = (driver_attempt_end) ? (driver_attempt_end - driver_attempt)
  84. : SDL_strlen(driver_attempt);
  85. for (i = 0; titlebootstrap[i]; ++i) {
  86. if ((driver_attempt_len == SDL_strlen(titlebootstrap[i]->name)) &&
  87. (SDL_strncasecmp(titlebootstrap[i]->name, driver_attempt, driver_attempt_len) == 0)) {
  88. storage = titlebootstrap[i]->create(override, props);
  89. break;
  90. }
  91. }
  92. driver_attempt = (driver_attempt_end) ? (driver_attempt_end + 1) : NULL;
  93. }
  94. } else {
  95. for (i = 0; titlebootstrap[i]; ++i) {
  96. storage = titlebootstrap[i]->create(override, props);
  97. if (storage) {
  98. break;
  99. }
  100. }
  101. }
  102. if (!storage) {
  103. if (driver_name) {
  104. SDL_SetError("%s not available", driver_name);
  105. } else {
  106. SDL_SetError("No available title storage driver");
  107. }
  108. }
  109. return storage;
  110. }
  111. SDL_Storage *SDL_OpenUserStorage(const char *org, const char *app, SDL_PropertiesID props)
  112. {
  113. SDL_Storage *storage = NULL;
  114. int i = 0;
  115. // Select the proper storage driver
  116. const char *driver_name = SDL_GetHint(SDL_HINT_STORAGE_USER_DRIVER);
  117. if (driver_name && *driver_name != 0) {
  118. const char *driver_attempt = driver_name;
  119. while (driver_attempt && *driver_attempt != 0 && !storage) {
  120. const char *driver_attempt_end = SDL_strchr(driver_attempt, ',');
  121. size_t driver_attempt_len = (driver_attempt_end) ? (driver_attempt_end - driver_attempt)
  122. : SDL_strlen(driver_attempt);
  123. for (i = 0; userbootstrap[i]; ++i) {
  124. if ((driver_attempt_len == SDL_strlen(userbootstrap[i]->name)) &&
  125. (SDL_strncasecmp(userbootstrap[i]->name, driver_attempt, driver_attempt_len) == 0)) {
  126. storage = userbootstrap[i]->create(org, app, props);
  127. break;
  128. }
  129. }
  130. driver_attempt = (driver_attempt_end) ? (driver_attempt_end + 1) : NULL;
  131. }
  132. } else {
  133. for (i = 0; userbootstrap[i]; ++i) {
  134. storage = userbootstrap[i]->create(org, app, props);
  135. if (storage) {
  136. break;
  137. }
  138. }
  139. }
  140. if (!storage) {
  141. if (driver_name) {
  142. SDL_SetError("%s not available", driver_name);
  143. } else {
  144. SDL_SetError("No available user storage driver");
  145. }
  146. }
  147. return storage;
  148. }
  149. SDL_Storage *SDL_OpenFileStorage(const char *path)
  150. {
  151. return GENERIC_OpenFileStorage(path);
  152. }
  153. SDL_Storage *SDL_OpenStorage(const SDL_StorageInterface *iface, void *userdata)
  154. {
  155. SDL_Storage *storage;
  156. if (!iface) {
  157. SDL_InvalidParamError("iface");
  158. return NULL;
  159. }
  160. if (iface->version < sizeof(*iface)) {
  161. // Update this to handle older versions of this interface
  162. SDL_SetError("Invalid interface, should be initialized with SDL_INIT_INTERFACE()");
  163. return NULL;
  164. }
  165. storage = (SDL_Storage *)SDL_calloc(1, sizeof(*storage));
  166. if (storage) {
  167. SDL_copyp(&storage->iface, iface);
  168. storage->userdata = userdata;
  169. }
  170. return storage;
  171. }
  172. bool SDL_CloseStorage(SDL_Storage *storage)
  173. {
  174. bool result = true;
  175. CHECK_STORAGE_MAGIC()
  176. if (storage->iface.close) {
  177. result = storage->iface.close(storage->userdata);
  178. }
  179. SDL_free(storage);
  180. return result;
  181. }
  182. bool SDL_StorageReady(SDL_Storage *storage)
  183. {
  184. CHECK_STORAGE_MAGIC_RET(false)
  185. if (storage->iface.ready) {
  186. return storage->iface.ready(storage->userdata);
  187. }
  188. return true;
  189. }
  190. bool SDL_GetStorageFileSize(SDL_Storage *storage, const char *path, Uint64 *length)
  191. {
  192. SDL_PathInfo info;
  193. if (SDL_GetStoragePathInfo(storage, path, &info)) {
  194. if (length) {
  195. *length = info.size;
  196. }
  197. return true;
  198. } else {
  199. if (length) {
  200. *length = 0;
  201. }
  202. return false;
  203. }
  204. }
  205. bool SDL_ReadStorageFile(SDL_Storage *storage, const char *path, void *destination, Uint64 length)
  206. {
  207. CHECK_STORAGE_MAGIC()
  208. if (!path) {
  209. return SDL_InvalidParamError("path");
  210. } else if (!ValidateStoragePath(path)) {
  211. return false;
  212. } else if (!storage->iface.read_file) {
  213. return SDL_Unsupported();
  214. }
  215. return storage->iface.read_file(storage->userdata, path, destination, length);
  216. }
  217. bool SDL_WriteStorageFile(SDL_Storage *storage, const char *path, const void *source, Uint64 length)
  218. {
  219. CHECK_STORAGE_MAGIC()
  220. if (!path) {
  221. return SDL_InvalidParamError("path");
  222. } else if (!ValidateStoragePath(path)) {
  223. return false;
  224. } else if (!storage->iface.write_file) {
  225. return SDL_Unsupported();
  226. }
  227. return storage->iface.write_file(storage->userdata, path, source, length);
  228. }
  229. bool SDL_CreateStorageDirectory(SDL_Storage *storage, const char *path)
  230. {
  231. CHECK_STORAGE_MAGIC()
  232. if (!path) {
  233. return SDL_InvalidParamError("path");
  234. } else if (!ValidateStoragePath(path)) {
  235. return false;
  236. } else if (!storage->iface.mkdir) {
  237. return SDL_Unsupported();
  238. }
  239. return storage->iface.mkdir(storage->userdata, path);
  240. }
  241. bool SDL_EnumerateStorageDirectory(SDL_Storage *storage, const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata)
  242. {
  243. CHECK_STORAGE_MAGIC()
  244. if (!path) {
  245. path = ""; // we allow NULL to mean "root of the storage tree".
  246. }
  247. if (!ValidateStoragePath(path)) {
  248. return false;
  249. } else if (!storage->iface.enumerate) {
  250. return SDL_Unsupported();
  251. }
  252. return storage->iface.enumerate(storage->userdata, path, callback, userdata);
  253. }
  254. bool SDL_RemoveStoragePath(SDL_Storage *storage, const char *path)
  255. {
  256. CHECK_STORAGE_MAGIC()
  257. if (!path) {
  258. return SDL_InvalidParamError("path");
  259. } else if (!ValidateStoragePath(path)) {
  260. return false;
  261. } else if (!storage->iface.remove) {
  262. return SDL_Unsupported();
  263. }
  264. return storage->iface.remove(storage->userdata, path);
  265. }
  266. bool SDL_RenameStoragePath(SDL_Storage *storage, const char *oldpath, const char *newpath)
  267. {
  268. CHECK_STORAGE_MAGIC()
  269. if (!oldpath) {
  270. return SDL_InvalidParamError("oldpath");
  271. } else if (!newpath) {
  272. return SDL_InvalidParamError("newpath");
  273. } else if (!ValidateStoragePath(oldpath)) {
  274. return false;
  275. } else if (!ValidateStoragePath(newpath)) {
  276. return false;
  277. } else if (!storage->iface.rename) {
  278. return SDL_Unsupported();
  279. }
  280. return storage->iface.rename(storage->userdata, oldpath, newpath);
  281. }
  282. bool SDL_CopyStorageFile(SDL_Storage *storage, const char *oldpath, const char *newpath)
  283. {
  284. CHECK_STORAGE_MAGIC()
  285. if (!oldpath) {
  286. return SDL_InvalidParamError("oldpath");
  287. } else if (!newpath) {
  288. return SDL_InvalidParamError("newpath");
  289. } else if (!ValidateStoragePath(oldpath)) {
  290. return false;
  291. } else if (!ValidateStoragePath(newpath)) {
  292. return false;
  293. } else if (!storage->iface.copy) {
  294. return SDL_Unsupported();
  295. }
  296. return storage->iface.copy(storage->userdata, oldpath, newpath);
  297. }
  298. bool SDL_GetStoragePathInfo(SDL_Storage *storage, const char *path, SDL_PathInfo *info)
  299. {
  300. SDL_PathInfo dummy;
  301. if (!info) {
  302. info = &dummy;
  303. }
  304. SDL_zerop(info);
  305. CHECK_STORAGE_MAGIC()
  306. if (!path) {
  307. return SDL_InvalidParamError("path");
  308. } else if (!ValidateStoragePath(path)) {
  309. return false;
  310. } else if (!storage->iface.info) {
  311. return SDL_Unsupported();
  312. }
  313. return storage->iface.info(storage->userdata, path, info);
  314. }
  315. Uint64 SDL_GetStorageSpaceRemaining(SDL_Storage *storage)
  316. {
  317. CHECK_STORAGE_MAGIC_RET(0)
  318. if (!storage->iface.space_remaining) {
  319. SDL_Unsupported();
  320. return 0;
  321. }
  322. return storage->iface.space_remaining(storage->userdata);
  323. }
  324. static bool GlobStorageDirectoryGetPathInfo(const char *path, SDL_PathInfo *info, void *userdata)
  325. {
  326. return SDL_GetStoragePathInfo((SDL_Storage *) userdata, path, info);
  327. }
  328. static bool GlobStorageDirectoryEnumerator(const char *path, SDL_EnumerateDirectoryCallback cb, void *cbuserdata, void *userdata)
  329. {
  330. return SDL_EnumerateStorageDirectory((SDL_Storage *) userdata, path, cb, cbuserdata);
  331. }
  332. char **SDL_GlobStorageDirectory(SDL_Storage *storage, const char *path, const char *pattern, SDL_GlobFlags flags, int *count)
  333. {
  334. CHECK_STORAGE_MAGIC_RET(NULL)
  335. if (!path) {
  336. path = ""; // we allow NULL to mean "root of the storage tree".
  337. }
  338. if (!ValidateStoragePath(path)) {
  339. return NULL;
  340. }
  341. return SDL_InternalGlobDirectory(path, pattern, flags, count, GlobStorageDirectoryEnumerator, GlobStorageDirectoryGetPathInfo, storage);
  342. }