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