SDL_storage.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. * \file SDL_storage.h
  20. *
  21. * Include file for storage container SDL API functions
  22. */
  23. #ifndef SDL_storage_h_
  24. #define SDL_storage_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_filesystem.h>
  27. #include <SDL3/SDL_properties.h>
  28. #include <SDL3/SDL_begin_code.h>
  29. /* Set up for C function definitions, even when using C++ */
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* !!! FIXME: Don't let this ship without async R/W support!!! */
  34. typedef struct SDL_StorageInterface
  35. {
  36. /* Called when the storage is closed */
  37. int (SDLCALL *close)(void *userdata);
  38. /* Optional, returns whether the storage is currently ready for access */
  39. SDL_bool (SDLCALL *ready)(void *userdata);
  40. /* Enumerate a directory, optional for write-only storage */
  41. int (SDLCALL *enumerate)(void *userdata, const char *path, SDL_EnumerateDirectoryCallback callback, void *callback_userdata);
  42. /* Get path information, optional for write-only storage */
  43. int (SDLCALL *info)(void *userdata, const char *path, SDL_PathInfo *info);
  44. /* Read a file from storage, optional for write-only storage */
  45. int (SDLCALL *read_file)(void *userdata, const char *path, void *destination, Uint64 length);
  46. /* Write a file to storage, optional for read-only storage */
  47. int (SDLCALL *write_file)(void *userdata, const char *path, const void *source, Uint64 length);
  48. /* Create a directory, optional for read-only storage */
  49. int (SDLCALL *mkdir)(void *userdata, const char *path);
  50. /* Remove a file or empty directory, optional for read-only storage */
  51. int (SDLCALL *remove)(void *userdata, const char *path);
  52. /* Rename a path, optional for read-only storage */
  53. int (SDLCALL *rename)(void *userdata, const char *oldpath, const char *newpath);
  54. /* Get the space remaining, optional for read-only storage */
  55. Uint64 (SDLCALL *space_remaining)(void *userdata);
  56. } SDL_StorageInterface;
  57. typedef struct SDL_Storage SDL_Storage;
  58. /**
  59. * Opens up a read-only container for the application's filesystem.
  60. *
  61. * \param override a path to override the backend's default title root
  62. * \param props a property list that may contain backend-specific information
  63. * \returns a title storage container on success or NULL on failure; call
  64. * SDL_GetError() for more information.
  65. *
  66. * \since This function is available since SDL 3.0.0.
  67. *
  68. * \sa SDL_OpenUserStorage
  69. * \sa SDL_OpenStorage
  70. * \sa SDL_TitleStorageReady
  71. * \sa SDL_CloseStorage
  72. * \sa SDL_StorageReady
  73. * \sa SDL_GetStorageFileSize
  74. * \sa SDL_ReadStorageFile
  75. * \sa SDL_WriteStorageFile
  76. * \sa SDL_GetStorageSpaceRemaining
  77. */
  78. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenTitleStorage(const char *override, SDL_PropertiesID props);
  79. /**
  80. * Opens up a container for a user's unique read/write filesystem.
  81. *
  82. * While title storage can generally be kept open throughout runtime, user
  83. * storage should only be opened when the client is ready to read/write files.
  84. * This allows the backend to properly batch file operations and flush them when
  85. * the container has been closed; ensuring safe and optimal save I/O.
  86. *
  87. * \param org the name of your organization
  88. * \param app the name of your application
  89. * \param props a property list that may contain backend-specific information
  90. * \returns a user storage container on success or NULL on failure; call
  91. * SDL_GetError() for more information.
  92. *
  93. * \since This function is available since SDL 3.0.0.
  94. *
  95. * \sa SDL_OpenTitleStorage
  96. * \sa SDL_OpenStorage
  97. * \sa SDL_CloseStorage
  98. * \sa SDL_StorageReady
  99. * \sa SDL_GetStorageFileSize
  100. * \sa SDL_ReadStorageFile
  101. * \sa SDL_WriteStorageFile
  102. * \sa SDL_GetStorageSpaceRemaining
  103. */
  104. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenUserStorage(const char *org, const char *app, SDL_PropertiesID props);
  105. /**
  106. * Opens up a container for local filesystem storage.
  107. *
  108. * This is provided for development and tools. Portable applications should use SDL_OpenTitleStorage() for access to game data and SDL_OpenUserStorage() for access to user data.
  109. *
  110. * \param path the base path prepended to all storage paths, or NULL for no base path
  111. * \returns a filesystem storage container on success or NULL on failure; call SDL_GetError() for more information.
  112. *
  113. * \since This function is available since SDL 3.0.0.
  114. *
  115. * \sa SDL_OpenStorage
  116. * \sa SDL_CloseStorage
  117. * \sa SDL_StorageReady
  118. * \sa SDL_GetStorageFileSize
  119. * \sa SDL_ReadStorageFile
  120. * \sa SDL_WriteStorageFile
  121. * \sa SDL_GetStorageSpaceRemaining
  122. */
  123. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenFileStorage(const char *path);
  124. /**
  125. * Opens up a container using a client-provided storage interface.
  126. *
  127. * Applications do not need to use this function unless they are providing
  128. * their own SDL_Storage implementation. If you just need an SDL_Storage, you
  129. * should use the built-in implementations in SDL, like SDL_OpenTitleStorage()
  130. * or SDL_OpenUserStorage().
  131. *
  132. * \param iface the function table to be used by this container
  133. * \param userdata the pointer that will be passed to the store interface
  134. * \returns a storage container on success or NULL on failure; call
  135. * SDL_GetError() for more information.
  136. *
  137. * \since This function is available since SDL 3.0.0.
  138. *
  139. * \sa SDL_OpenTitleStorage
  140. * \sa SDL_OpenUserStorage
  141. * \sa SDL_CloseStorage
  142. * \sa SDL_StorageReady
  143. * \sa SDL_GetStorageFileSize
  144. * \sa SDL_ReadStorageFile
  145. * \sa SDL_WriteStorageFile
  146. * \sa SDL_GetStorageSpaceRemaining
  147. */
  148. extern DECLSPEC SDL_Storage *SDLCALL SDL_OpenStorage(const SDL_StorageInterface *iface, void *userdata);
  149. /**
  150. * Closes and frees a storage container.
  151. *
  152. * \param storage a storage container to close
  153. * \returns 0 if the container was freed with no errors, a negative value
  154. * otherwise; call SDL_GetError() for more information. Even if the
  155. * function returns an error, the container data will be freed; the
  156. * error is only for informational purposes.
  157. *
  158. * \since This function is available since SDL 3.0.0.
  159. *
  160. * \sa SDL_OpenTitleStorage
  161. * \sa SDL_OpenUserStorage
  162. * \sa SDL_OpenStorage
  163. * \sa SDL_StorageReady
  164. * \sa SDL_GetStorageFileSize
  165. * \sa SDL_ReadStorageFile
  166. * \sa SDL_WriteStorageFile
  167. * \sa SDL_GetStorageSpaceRemaining
  168. */
  169. extern DECLSPEC int SDLCALL SDL_CloseStorage(SDL_Storage *storage);
  170. /**
  171. * Checks if the storage container is ready to use.
  172. *
  173. * This function should be called in regular intervals until it returns
  174. * SDL_TRUE - however, it is not recommended to spinwait on this call, as the
  175. * backend may depend on a synchronous message loop.
  176. *
  177. * \param storage a storage container to query
  178. * \returns SDL_TRUE if the container is ready, SDL_FALSE otherwise
  179. *
  180. * \since This function is available since SDL 3.0.0.
  181. *
  182. * \sa SDL_OpenTitleStorage
  183. * \sa SDL_OpenUserStorage
  184. * \sa SDL_OpenStorage
  185. * \sa SDL_CloseStorage
  186. * \sa SDL_GetStorageFileSize
  187. * \sa SDL_ReadStorageFile
  188. * \sa SDL_WriteStorageFile
  189. * \sa SDL_GetStorageSpaceRemaining
  190. */
  191. extern DECLSPEC SDL_bool SDLCALL SDL_StorageReady(SDL_Storage *storage);
  192. /**
  193. * Query the size of a file within a storage container.
  194. *
  195. * \param storage a storage container to query
  196. * \param path the relative path of the file to query
  197. * \param length a pointer to be filled with the file's length
  198. * \returns 0 if the file could be queried, a negative value otherwise; call
  199. * SDL_GetError() for more information.
  200. *
  201. * \since This function is available since SDL 3.0.0.
  202. *
  203. * \sa SDL_OpenTitleStorage
  204. * \sa SDL_OpenUserStorage
  205. * \sa SDL_OpenStorage
  206. * \sa SDL_CloseStorage
  207. * \sa SDL_StorageReady
  208. * \sa SDL_ReadStorageFile
  209. * \sa SDL_WriteStorageFile
  210. * \sa SDL_GetStorageSpaceRemaining
  211. */
  212. extern DECLSPEC int SDLCALL SDL_GetStorageFileSize(SDL_Storage *storage, const char *path, Uint64 *length);
  213. /**
  214. * Synchronously read a file from a storage container into a client-provided
  215. * buffer.
  216. *
  217. * \param storage a storage container to read from
  218. * \param path the relative path of the file to read
  219. * \param destination a client-provided buffer to read the file into
  220. * \param length the length of the destination buffer
  221. * \returns 0 if the file was read, a negative value otherwise; call
  222. * SDL_GetError() for more information.
  223. *
  224. * \since This function is available since SDL 3.0.0.
  225. *
  226. * \sa SDL_OpenTitleStorage
  227. * \sa SDL_OpenUserStorage
  228. * \sa SDL_OpenStorage
  229. * \sa SDL_CloseStorage
  230. * \sa SDL_StorageReady
  231. * \sa SDL_GetStorageFileSize
  232. * \sa SDL_WriteStorageFile
  233. * \sa SDL_GetStorageSpaceRemaining
  234. */
  235. extern DECLSPEC int SDLCALL SDL_ReadStorageFile(SDL_Storage *storage, const char *path, void *destination, Uint64 length);
  236. /**
  237. * Synchronously write a file from client memory into a storage container.
  238. *
  239. * \param storage a storage container to write to
  240. * \param path the relative path of the file to write
  241. * \param source a client-provided buffer to write from
  242. * \param length the length of the source buffer
  243. * \returns 0 if the file was written, a negative value otherwise; call
  244. * SDL_GetError() for more information.
  245. *
  246. * \since This function is available since SDL 3.0.0.
  247. *
  248. * \sa SDL_OpenTitleStorage
  249. * \sa SDL_OpenUserStorage
  250. * \sa SDL_OpenStorage
  251. * \sa SDL_CloseStorage
  252. * \sa SDL_StorageReady
  253. * \sa SDL_GetStorageFileSize
  254. * \sa SDL_ReadStorageFile
  255. * \sa SDL_GetStorageSpaceRemaining
  256. */
  257. extern DECLSPEC int SDL_WriteStorageFile(SDL_Storage *storage, const char *path, const void *source, Uint64 length);
  258. /**
  259. * Create a directory in a writable storage container.
  260. *
  261. * \param storage a storage container
  262. * \param path the path of the directory to create
  263. * \returns 0 on success or a negative error code on failure; call
  264. * SDL_GetError() for more information.
  265. *
  266. * \since This function is available since SDL 3.0.0.
  267. */
  268. extern DECLSPEC int SDLCALL SDL_CreateStorageDirectory(SDL_Storage *storage, const char *path);
  269. /**
  270. * Enumerate a directory in a storage container.
  271. *
  272. * \param storage a storage container
  273. * \param path the path of the directory to enumerate
  274. * \param callback a function that is called for each entry in the directory
  275. * \param userdata a pointer that is passed to `callback`
  276. * \returns 0 on success or a negative error code on failure; call
  277. * SDL_GetError() for more information.
  278. *
  279. * \since This function is available since SDL 3.0.0.
  280. */
  281. extern DECLSPEC int SDLCALL SDL_EnumerateStorageDirectory(SDL_Storage *storage, const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
  282. /**
  283. * Remove a file or an empty directory in a writable storage container.
  284. *
  285. * \param storage a storage container
  286. * \param path the path of the directory to enumerate
  287. * \returns 0 on success or a negative error code on failure; call
  288. * SDL_GetError() for more information.
  289. *
  290. * \since This function is available since SDL 3.0.0.
  291. */
  292. extern DECLSPEC int SDLCALL SDL_RemoveStoragePath(SDL_Storage *storage, const char *path);
  293. /**
  294. * Rename a file or directory in a writable storage container.
  295. *
  296. * \param storage a storage container
  297. * \param oldpath the old path
  298. * \param newpath the new path
  299. * \returns 0 on success or a negative error code on failure; call
  300. * SDL_GetError() for more information.
  301. *
  302. * \since This function is available since SDL 3.0.0.
  303. */
  304. extern DECLSPEC int SDLCALL SDL_RenameStoragePath(SDL_Storage *storage, const char *oldpath, const char *newpath);
  305. /**
  306. * Get information about a filesystem path in a storage container.
  307. *
  308. * \param storage a storage container
  309. * \param path the path to query
  310. * \param info a pointer filled in with information about the path
  311. * \returns 0 on success or a negative error code on failure; call
  312. * SDL_GetError() for more information.
  313. *
  314. * \since This function is available since SDL 3.0.0.
  315. */
  316. extern DECLSPEC int SDLCALL SDL_GetStoragePathInfo(SDL_Storage *storage, const char *path, SDL_PathInfo *info);
  317. /**
  318. * Queries the remaining space in a storage container.
  319. *
  320. * \param storage a storage container to query
  321. * \returns the amount of remaining space, in bytes
  322. *
  323. * \since This function is available since SDL 3.0.0.
  324. *
  325. * \sa SDL_OpenTitleStorage
  326. * \sa SDL_OpenUserStorage
  327. * \sa SDL_OpenStorage
  328. * \sa SDL_CloseStorage
  329. * \sa SDL_StorageReady
  330. * \sa SDL_GetStorageFileSize
  331. * \sa SDL_ReadStorageFile
  332. * \sa SDL_WriteStorageFile
  333. */
  334. extern DECLSPEC Uint64 SDLCALL SDL_GetStorageSpaceRemaining(SDL_Storage *storage);
  335. /* Ends C function definitions when using C++ */
  336. #ifdef __cplusplus
  337. }
  338. #endif
  339. #include <SDL3/SDL_close_code.h>
  340. #endif /* SDL_storage_h_ */