SDL_storage.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. /**
  19. * # CategoryStorage
  20. *
  21. * SDL storage container management.
  22. */
  23. #ifndef SDL_storage_h_
  24. #define SDL_storage_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_filesystem.h>
  28. #include <SDL3/SDL_properties.h>
  29. #include <SDL3/SDL_begin_code.h>
  30. /* Set up for C function definitions, even when using C++ */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /* !!! FIXME: Don't let this ship without async R/W support!!! */
  35. /**
  36. * Function interface for SDL_Storage.
  37. *
  38. * Apps that want to supply a custom implementation of SDL_Storage will fill
  39. * in all the functions in this struct, and then pass it to SDL_OpenStorage to
  40. * create a custom SDL_Storage object.
  41. *
  42. * It is not usually necessary to do this; SDL provides standard
  43. * implementations for many things you might expect to do with an SDL_Storage.
  44. *
  45. * This structure should be initialized using SDL_INIT_INTERFACE()
  46. *
  47. * \since This struct is available since SDL 3.0.0.
  48. *
  49. * \sa SDL_INIT_INTERFACE
  50. */
  51. typedef struct SDL_StorageInterface
  52. {
  53. /* The version of this interface */
  54. Uint32 version;
  55. /* Called when the storage is closed */
  56. bool (SDLCALL *close)(void *userdata);
  57. /* Optional, returns whether the storage is currently ready for access */
  58. bool (SDLCALL *ready)(void *userdata);
  59. /* Enumerate a directory, optional for write-only storage */
  60. bool (SDLCALL *enumerate)(void *userdata, const char *path, SDL_EnumerateDirectoryCallback callback, void *callback_userdata);
  61. /* Get path information, optional for write-only storage */
  62. bool (SDLCALL *info)(void *userdata, const char *path, SDL_PathInfo *info);
  63. /* Read a file from storage, optional for write-only storage */
  64. bool (SDLCALL *read_file)(void *userdata, const char *path, void *destination, Uint64 length);
  65. /* Write a file to storage, optional for read-only storage */
  66. bool (SDLCALL *write_file)(void *userdata, const char *path, const void *source, Uint64 length);
  67. /* Create a directory, optional for read-only storage */
  68. bool (SDLCALL *mkdir)(void *userdata, const char *path);
  69. /* Remove a file or empty directory, optional for read-only storage */
  70. bool (SDLCALL *remove)(void *userdata, const char *path);
  71. /* Rename a path, optional for read-only storage */
  72. bool (SDLCALL *rename)(void *userdata, const char *oldpath, const char *newpath);
  73. /* Copy a file, optional for read-only storage */
  74. bool (SDLCALL *copy)(void *userdata, const char *oldpath, const char *newpath);
  75. /* Get the space remaining, optional for read-only storage */
  76. Uint64 (SDLCALL *space_remaining)(void *userdata);
  77. } SDL_StorageInterface;
  78. /* Check the size of SDL_StorageInterface
  79. *
  80. * If this assert fails, either the compiler is padding to an unexpected size,
  81. * or the interface has been updated and this should be updated to match and
  82. * the code using this interface should be updated to handle the old version.
  83. */
  84. SDL_COMPILE_TIME_ASSERT(SDL_StorageInterface_SIZE,
  85. (sizeof(void *) == 4 && sizeof(SDL_StorageInterface) == 48) ||
  86. (sizeof(void *) == 8 && sizeof(SDL_StorageInterface) == 96));
  87. /**
  88. * An abstract interface for filesystem access.
  89. *
  90. * This is an opaque datatype. One can create this object using standard SDL
  91. * functions like SDL_OpenTitleStorage or SDL_OpenUserStorage, etc, or create
  92. * an object with a custom implementation using SDL_OpenStorage.
  93. *
  94. * \since This struct is available since SDL 3.0.0.
  95. */
  96. typedef struct SDL_Storage SDL_Storage;
  97. /**
  98. * Opens up a read-only container for the application's filesystem.
  99. *
  100. * \param override a path to override the backend's default title root.
  101. * \param props a property list that may contain backend-specific information.
  102. * \returns a title storage container on success or NULL on failure; call
  103. * SDL_GetError() for more information.
  104. *
  105. * \since This function is available since SDL 3.0.0.
  106. *
  107. * \sa SDL_CloseStorage
  108. * \sa SDL_GetStorageFileSize
  109. * \sa SDL_OpenUserStorage
  110. * \sa SDL_ReadStorageFile
  111. */
  112. extern SDL_DECLSPEC SDL_Storage * SDLCALL SDL_OpenTitleStorage(const char *override, SDL_PropertiesID props);
  113. /**
  114. * Opens up a container for a user's unique read/write filesystem.
  115. *
  116. * While title storage can generally be kept open throughout runtime, user
  117. * storage should only be opened when the client is ready to read/write files.
  118. * This allows the backend to properly batch file operations and flush them
  119. * when the container has been closed; ensuring safe and optimal save I/O.
  120. *
  121. * \param org the name of your organization.
  122. * \param app the name of your application.
  123. * \param props a property list that may contain backend-specific information.
  124. * \returns a user storage container on success or NULL on failure; call
  125. * SDL_GetError() for more information.
  126. *
  127. * \since This function is available since SDL 3.0.0.
  128. *
  129. * \sa SDL_CloseStorage
  130. * \sa SDL_GetStorageFileSize
  131. * \sa SDL_GetStorageSpaceRemaining
  132. * \sa SDL_OpenTitleStorage
  133. * \sa SDL_ReadStorageFile
  134. * \sa SDL_StorageReady
  135. * \sa SDL_WriteStorageFile
  136. */
  137. extern SDL_DECLSPEC SDL_Storage * SDLCALL SDL_OpenUserStorage(const char *org, const char *app, SDL_PropertiesID props);
  138. /**
  139. * Opens up a container for local filesystem storage.
  140. *
  141. * This is provided for development and tools. Portable applications should
  142. * use SDL_OpenTitleStorage() for access to game data and
  143. * SDL_OpenUserStorage() for access to user data.
  144. *
  145. * \param path the base path prepended to all storage paths, or NULL for no
  146. * base path.
  147. * \returns a filesystem storage container on success or NULL on failure; call
  148. * SDL_GetError() for more information.
  149. *
  150. * \since This function is available since SDL 3.0.0.
  151. *
  152. * \sa SDL_CloseStorage
  153. * \sa SDL_GetStorageFileSize
  154. * \sa SDL_GetStorageSpaceRemaining
  155. * \sa SDL_OpenTitleStorage
  156. * \sa SDL_OpenUserStorage
  157. * \sa SDL_ReadStorageFile
  158. * \sa SDL_WriteStorageFile
  159. */
  160. extern SDL_DECLSPEC SDL_Storage * SDLCALL SDL_OpenFileStorage(const char *path);
  161. /**
  162. * Opens up a container using a client-provided storage interface.
  163. *
  164. * Applications do not need to use this function unless they are providing
  165. * their own SDL_Storage implementation. If you just need an SDL_Storage, you
  166. * should use the built-in implementations in SDL, like SDL_OpenTitleStorage()
  167. * or SDL_OpenUserStorage().
  168. *
  169. * This function makes a copy of `iface` and the caller does not need to keep
  170. * it around after this call.
  171. *
  172. * \param iface the interface that implements this storage, initialized using
  173. * SDL_INIT_INTERFACE().
  174. * \param userdata the pointer that will be passed to the interface functions.
  175. * \returns a storage container on success or NULL on failure; call
  176. * SDL_GetError() for more information.
  177. *
  178. * \since This function is available since SDL 3.0.0.
  179. *
  180. * \sa SDL_CloseStorage
  181. * \sa SDL_GetStorageFileSize
  182. * \sa SDL_GetStorageSpaceRemaining
  183. * \sa SDL_INIT_INTERFACE
  184. * \sa SDL_ReadStorageFile
  185. * \sa SDL_StorageReady
  186. * \sa SDL_WriteStorageFile
  187. */
  188. extern SDL_DECLSPEC SDL_Storage * SDLCALL SDL_OpenStorage(const SDL_StorageInterface *iface, void *userdata);
  189. /**
  190. * Closes and frees a storage container.
  191. *
  192. * \param storage a storage container to close.
  193. * \returns true if the container was freed with no errors, false otherwise;
  194. * call SDL_GetError() for more information. Even if the function
  195. * returns an error, the container data will be freed; the error is
  196. * only for informational purposes.
  197. *
  198. * \since This function is available since SDL 3.0.0.
  199. *
  200. * \sa SDL_OpenFileStorage
  201. * \sa SDL_OpenStorage
  202. * \sa SDL_OpenTitleStorage
  203. * \sa SDL_OpenUserStorage
  204. */
  205. extern SDL_DECLSPEC bool SDLCALL SDL_CloseStorage(SDL_Storage *storage);
  206. /**
  207. * Checks if the storage container is ready to use.
  208. *
  209. * This function should be called in regular intervals until it returns true -
  210. * however, it is not recommended to spinwait on this call, as the backend may
  211. * depend on a synchronous message loop.
  212. *
  213. * \param storage a storage container to query.
  214. * \returns true if the container is ready, false otherwise.
  215. *
  216. * \since This function is available since SDL 3.0.0.
  217. */
  218. extern SDL_DECLSPEC bool SDLCALL SDL_StorageReady(SDL_Storage *storage);
  219. /**
  220. * Query the size of a file within a storage container.
  221. *
  222. * \param storage a storage container to query.
  223. * \param path the relative path of the file to query.
  224. * \param length a pointer to be filled with the file's length.
  225. * \returns true if the file could be queried or false on failure; call
  226. * SDL_GetError() for more information.
  227. *
  228. * \since This function is available since SDL 3.0.0.
  229. *
  230. * \sa SDL_ReadStorageFile
  231. * \sa SDL_StorageReady
  232. */
  233. extern SDL_DECLSPEC bool SDLCALL SDL_GetStorageFileSize(SDL_Storage *storage, const char *path, Uint64 *length);
  234. /**
  235. * Synchronously read a file from a storage container into a client-provided
  236. * buffer.
  237. *
  238. * \param storage a storage container to read from.
  239. * \param path the relative path of the file to read.
  240. * \param destination a client-provided buffer to read the file into.
  241. * \param length the length of the destination buffer.
  242. * \returns true if the file was read or false on failure; call SDL_GetError()
  243. * for more information.
  244. *
  245. * \since This function is available since SDL 3.0.0.
  246. *
  247. * \sa SDL_GetStorageFileSize
  248. * \sa SDL_StorageReady
  249. * \sa SDL_WriteStorageFile
  250. */
  251. extern SDL_DECLSPEC bool SDLCALL SDL_ReadStorageFile(SDL_Storage *storage, const char *path, void *destination, Uint64 length);
  252. /**
  253. * Synchronously write a file from client memory into a storage container.
  254. *
  255. * \param storage a storage container to write to.
  256. * \param path the relative path of the file to write.
  257. * \param source a client-provided buffer to write from.
  258. * \param length the length of the source buffer.
  259. * \returns true if the file was written or false on failure; call
  260. * SDL_GetError() for more information.
  261. *
  262. * \since This function is available since SDL 3.0.0.
  263. *
  264. * \sa SDL_GetStorageSpaceRemaining
  265. * \sa SDL_ReadStorageFile
  266. * \sa SDL_StorageReady
  267. */
  268. extern SDL_DECLSPEC bool SDLCALL SDL_WriteStorageFile(SDL_Storage *storage, const char *path, const void *source, Uint64 length);
  269. /**
  270. * Create a directory in a writable storage container.
  271. *
  272. * \param storage a storage container.
  273. * \param path the path of the directory to create.
  274. * \returns true on success or false on failure; call SDL_GetError() for more
  275. * information.
  276. *
  277. * \since This function is available since SDL 3.0.0.
  278. *
  279. * \sa SDL_StorageReady
  280. */
  281. extern SDL_DECLSPEC bool SDLCALL SDL_CreateStorageDirectory(SDL_Storage *storage, const char *path);
  282. /**
  283. * Enumerate a directory in a storage container through a callback function.
  284. *
  285. * This function provides every directory entry through an app-provided
  286. * callback, called once for each directory entry, until all results have been
  287. * provided or the callback returns <= 0.
  288. *
  289. * This will return false if there was a system problem in general, or if a
  290. * callback returns -1. A successful return means a callback returned 1 to
  291. * halt enumeration, or all directory entries were enumerated.
  292. *
  293. * \param storage a storage container.
  294. * \param path the path of the directory to enumerate.
  295. * \param callback a function that is called for each entry in the directory.
  296. * \param userdata a pointer that is passed to `callback`.
  297. * \returns true on success or false on failure; call SDL_GetError() for more
  298. * information.
  299. *
  300. * \since This function is available since SDL 3.0.0.
  301. *
  302. * \sa SDL_StorageReady
  303. */
  304. extern SDL_DECLSPEC bool SDLCALL SDL_EnumerateStorageDirectory(SDL_Storage *storage, const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
  305. /**
  306. * Remove a file or an empty directory in a writable storage container.
  307. *
  308. * \param storage a storage container.
  309. * \param path the path of the directory to enumerate.
  310. * \returns true on success or false on failure; call SDL_GetError() for more
  311. * information.
  312. *
  313. * \since This function is available since SDL 3.0.0.
  314. *
  315. * \sa SDL_StorageReady
  316. */
  317. extern SDL_DECLSPEC bool SDLCALL SDL_RemoveStoragePath(SDL_Storage *storage, const char *path);
  318. /**
  319. * Rename a file or directory in a writable storage container.
  320. *
  321. * \param storage a storage container.
  322. * \param oldpath the old path.
  323. * \param newpath the new path.
  324. * \returns true on success or false on failure; call SDL_GetError() for more
  325. * information.
  326. *
  327. * \since This function is available since SDL 3.0.0.
  328. *
  329. * \sa SDL_StorageReady
  330. */
  331. extern SDL_DECLSPEC bool SDLCALL SDL_RenameStoragePath(SDL_Storage *storage, const char *oldpath, const char *newpath);
  332. /**
  333. * Copy a file in a writable storage container.
  334. *
  335. * \param storage a storage container.
  336. * \param oldpath the old path.
  337. * \param newpath the new path.
  338. * \returns true on success or false on failure; call SDL_GetError() for more
  339. * information.
  340. *
  341. * \since This function is available since SDL 3.0.0.
  342. *
  343. * \sa SDL_StorageReady
  344. */
  345. extern SDL_DECLSPEC bool SDLCALL SDL_CopyStorageFile(SDL_Storage *storage, const char *oldpath, const char *newpath);
  346. /**
  347. * Get information about a filesystem path in a storage container.
  348. *
  349. * \param storage a storage container.
  350. * \param path the path to query.
  351. * \param info a pointer filled in with information about the path, or NULL to
  352. * check for the existence of a file.
  353. * \returns true on success or false if the file doesn't exist, or another
  354. * failure; call SDL_GetError() for more information.
  355. *
  356. * \since This function is available since SDL 3.0.0.
  357. *
  358. * \sa SDL_StorageReady
  359. */
  360. extern SDL_DECLSPEC bool SDLCALL SDL_GetStoragePathInfo(SDL_Storage *storage, const char *path, SDL_PathInfo *info);
  361. /**
  362. * Queries the remaining space in a storage container.
  363. *
  364. * \param storage a storage container to query.
  365. * \returns the amount of remaining space, in bytes.
  366. *
  367. * \since This function is available since SDL 3.0.0.
  368. *
  369. * \sa SDL_StorageReady
  370. * \sa SDL_WriteStorageFile
  371. */
  372. extern SDL_DECLSPEC Uint64 SDLCALL SDL_GetStorageSpaceRemaining(SDL_Storage *storage);
  373. /**
  374. * Enumerate a directory tree, filtered by pattern, and return a list.
  375. *
  376. * Files are filtered out if they don't match the string in `pattern`, which
  377. * may contain wildcard characters '*' (match everything) and '?' (match one
  378. * character). If pattern is NULL, no filtering is done and all results are
  379. * returned. Subdirectories are permitted, and are specified with a path
  380. * separator of '/'. Wildcard characters '*' and '?' never match a path
  381. * separator.
  382. *
  383. * `flags` may be set to SDL_GLOB_CASEINSENSITIVE to make the pattern matching
  384. * case-insensitive.
  385. *
  386. * The returned array is always NULL-terminated, for your iterating
  387. * convenience, but if `count` is non-NULL, on return it will contain the
  388. * number of items in the array, not counting the NULL terminator.
  389. *
  390. * \param storage a storage container.
  391. * \param path the path of the directory to enumerate.
  392. * \param pattern the pattern that files in the directory must match. Can be
  393. * NULL.
  394. * \param flags `SDL_GLOB_*` bitflags that affect this search.
  395. * \param count on return, will be set to the number of items in the returned
  396. * array. Can be NULL.
  397. * \returns an array of strings on success or NULL on failure; call
  398. * SDL_GetError() for more information. The caller should pass the
  399. * returned pointer to SDL_free when done with it. This is a single
  400. * allocation that should be freed with SDL_free() when it is no
  401. * longer needed.
  402. *
  403. * \threadsafety It is safe to call this function from any thread, assuming
  404. * the `storage` object is thread-safe.
  405. *
  406. * \since This function is available since SDL 3.0.0.
  407. */
  408. extern SDL_DECLSPEC char ** SDLCALL SDL_GlobStorageDirectory(SDL_Storage *storage, const char *path, const char *pattern, SDL_GlobFlags flags, int *count);
  409. /* Ends C function definitions when using C++ */
  410. #ifdef __cplusplus
  411. }
  412. #endif
  413. #include <SDL3/SDL_close_code.h>
  414. #endif /* SDL_storage_h_ */