SDL 3.0
SDL_filesystem.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 * # CategoryFilesystem
24 *
25 * SDL Filesystem API.
26 */
27
28#ifndef SDL_filesystem_h_
29#define SDL_filesystem_h_
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33
34#include <SDL3/SDL_begin_code.h>
35
36/* Set up for C function definitions, even when using C++ */
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
42 * Get the directory where the application was run from.
43 *
44 * SDL caches the result of this call internally, but the first call to this
45 * function is not necessarily fast, so plan accordingly.
46 *
47 * **macOS and iOS Specific Functionality**: If the application is in a ".app"
48 * bundle, this function returns the Resource directory (e.g.
49 * MyApp.app/Contents/Resources/). This behaviour can be overridden by adding
50 * a property to the Info.plist file. Adding a string key with the name
51 * SDL_FILESYSTEM_BASE_DIR_TYPE with a supported value will change the
52 * behaviour.
53 *
54 * Supported values for the SDL_FILESYSTEM_BASE_DIR_TYPE property (Given an
55 * application in /Applications/SDLApp/MyApp.app):
56 *
57 * - `resource`: bundle resource directory (the default). For example:
58 * `/Applications/SDLApp/MyApp.app/Contents/Resources`
59 * - `bundle`: the Bundle directory. For example:
60 * `/Applications/SDLApp/MyApp.app/`
61 * - `parent`: the containing directory of the bundle. For example:
62 * `/Applications/SDLApp/`
63 *
64 * **Nintendo 3DS Specific Functionality**: This function returns "romfs"
65 * directory of the application as it is uncommon to store resources outside
66 * the executable. As such it is not a writable directory.
67 *
68 * The returned path is guaranteed to end with a path separator ('\\' on
69 * Windows, '/' on most other platforms).
70 *
71 * \returns an absolute path in UTF-8 encoding to the application data
72 * directory. NULL will be returned on error or when the platform
73 * doesn't implement this functionality, call SDL_GetError() for more
74 * information.
75 *
76 * \since This function is available since SDL 3.1.3.
77 *
78 * \sa SDL_GetPrefPath
79 */
80extern SDL_DECLSPEC const char * SDLCALL SDL_GetBasePath(void);
81
82/**
83 * Get the user-and-app-specific path where files can be written.
84 *
85 * Get the "pref dir". This is meant to be where users can write personal
86 * files (preferences and save games, etc) that are specific to your
87 * application. This directory is unique per user, per application.
88 *
89 * This function will decide the appropriate location in the native
90 * filesystem, create the directory if necessary, and return a string of the
91 * absolute path to the directory in UTF-8 encoding.
92 *
93 * On Windows, the string might look like:
94 *
95 * `C:\\Users\\bob\\AppData\\Roaming\\My Company\\My Program Name\\`
96 *
97 * On Linux, the string might look like:
98 *
99 * `/home/bob/.local/share/My Program Name/`
100 *
101 * On macOS, the string might look like:
102 *
103 * `/Users/bob/Library/Application Support/My Program Name/`
104 *
105 * You should assume the path returned by this function is the only safe place
106 * to write files (and that SDL_GetBasePath(), while it might be writable, or
107 * even the parent of the returned path, isn't where you should be writing
108 * things).
109 *
110 * Both the org and app strings may become part of a directory name, so please
111 * follow these rules:
112 *
113 * - Try to use the same org string (_including case-sensitivity_) for all
114 * your applications that use this function.
115 * - Always use a unique app string for each one, and make sure it never
116 * changes for an app once you've decided on it.
117 * - Unicode characters are legal, as long as they are UTF-8 encoded, but...
118 * - ...only use letters, numbers, and spaces. Avoid punctuation like "Game
119 * Name 2: Bad Guy's Revenge!" ... "Game Name 2" is sufficient.
120 *
121 * The returned path is guaranteed to end with a path separator ('\\' on
122 * Windows, '/' on most other platforms).
123 *
124 * \param org the name of your organization.
125 * \param app the name of your application.
126 * \returns a UTF-8 string of the user directory in platform-dependent
127 * notation. NULL if there's a problem (creating directory failed,
128 * etc.). This should be freed with SDL_free() when it is no longer
129 * needed.
130 *
131 * \since This function is available since SDL 3.1.3.
132 *
133 * \sa SDL_GetBasePath
134 */
135extern SDL_DECLSPEC char * SDLCALL SDL_GetPrefPath(const char *org, const char *app);
136
137/**
138 * The type of the OS-provided default folder for a specific purpose.
139 *
140 * Note that the Trash folder isn't included here, because trashing files
141 * usually involves extra OS-specific functionality to remember the file's
142 * original location.
143 *
144 * The folders supported per platform are:
145 *
146 * | | Windows | macOS/iOS | tvOS | Unix (XDG) | Haiku | Emscripten |
147 * | ----------- | ------- | --------- | ---- | ---------- | ----- | ---------- |
148 * | HOME | X | X | | X | X | X |
149 * | DESKTOP | X | X | | X | X | |
150 * | DOCUMENTS | X | X | | X | | |
151 * | DOWNLOADS | Vista+ | X | | X | | |
152 * | MUSIC | X | X | | X | | |
153 * | PICTURES | X | X | | X | | |
154 * | PUBLICSHARE | | X | | X | | |
155 * | SAVEDGAMES | Vista+ | | | | | |
156 * | SCREENSHOTS | Vista+ | | | | | |
157 * | TEMPLATES | X | X | | X | | |
158 * | VIDEOS | X | X* | | X | | |
159 *
160 * Note that on macOS/iOS, the Videos folder is called "Movies".
161 *
162 * \since This enum is available since SDL 3.1.3.
163 *
164 * \sa SDL_GetUserFolder
165 */
166typedef enum SDL_Folder
167{
168 SDL_FOLDER_HOME, /**< The folder which contains all of the current user's data, preferences, and documents. It usually contains most of the other folders. If a requested folder does not exist, the home folder can be considered a safe fallback to store a user's documents. */
169
170 SDL_FOLDER_DESKTOP, /**< The folder of files that are displayed on the desktop. Note that the existence of a desktop folder does not guarantee that the system does show icons on its desktop; certain GNU/Linux distros with a graphical environment may not have desktop icons. */
171
172 SDL_FOLDER_DOCUMENTS, /**< User document files, possibly application-specific. This is a good place to save a user's projects. */
173
174 SDL_FOLDER_DOWNLOADS, /**< Standard folder for user files downloaded from the internet. */
175
176 SDL_FOLDER_MUSIC, /**< Music files that can be played using a standard music player (mp3, ogg...). */
177
178 SDL_FOLDER_PICTURES, /**< Image files that can be displayed using a standard viewer (png, jpg...). */
179
180 SDL_FOLDER_PUBLICSHARE, /**< Files that are meant to be shared with other users on the same computer. */
181
182 SDL_FOLDER_SAVEDGAMES, /**< Save files for games. */
183
184 SDL_FOLDER_SCREENSHOTS, /**< Application screenshots. */
185
186 SDL_FOLDER_TEMPLATES, /**< Template files to be used when the user requests the desktop environment to create a new file in a certain folder, such as "New Text File.txt". Any file in the Templates folder can be used as a starting point for a new file. */
187
188 SDL_FOLDER_VIDEOS, /**< Video files that can be played using a standard video player (mp4, webm...). */
189
190 SDL_FOLDER_COUNT /**< Total number of types in this enum, not a folder type by itself. */
191
193
194/**
195 * Finds the most suitable user folder for a specific purpose.
196 *
197 * Many OSes provide certain standard folders for certain purposes, such as
198 * storing pictures, music or videos for a certain user. This function gives
199 * the path for many of those special locations.
200 *
201 * This function is specifically for _user_ folders, which are meant for the
202 * user to access and manage. For application-specific folders, meant to hold
203 * data for the application to manage, see SDL_GetBasePath() and
204 * SDL_GetPrefPath().
205 *
206 * The returned path is guaranteed to end with a path separator ('\\' on
207 * Windows, '/' on most other platforms).
208 *
209 * If NULL is returned, the error may be obtained with SDL_GetError().
210 *
211 * \param folder the type of folder to find.
212 * \returns either a null-terminated C string containing the full path to the
213 * folder, or NULL if an error happened.
214 *
215 * \since This function is available since SDL 3.1.3.
216 */
217extern SDL_DECLSPEC const char * SDLCALL SDL_GetUserFolder(SDL_Folder folder);
218
219
220/* Abstract filesystem interface */
221
222typedef enum SDL_PathType
223{
224 SDL_PATHTYPE_NONE, /**< path does not exist */
225 SDL_PATHTYPE_FILE, /**< a normal file */
226 SDL_PATHTYPE_DIRECTORY, /**< a directory */
227 SDL_PATHTYPE_OTHER /**< something completely different like a device node (not a symlink, those are always followed) */
229
230typedef struct SDL_PathInfo
231{
232 SDL_PathType type; /**< the path type */
233 Uint64 size; /**< the file size in bytes */
234 SDL_Time create_time; /**< the time when the path was created */
235 SDL_Time modify_time; /**< the last time the path was modified */
236 SDL_Time access_time; /**< the last time the path was read */
238
239/**
240 * Flags for path matching
241 *
242 * \since This datatype is available since SDL 3.1.3.
243 *
244 * \sa SDL_GlobDirectory
245 * \sa SDL_GlobStorageDirectory
246 */
248
249#define SDL_GLOB_CASEINSENSITIVE (1u << 0)
250
251/**
252 * Create a directory, and any missing parent directories.
253 *
254 * This reports success if `path` already exists as a directory.
255 *
256 * If parent directories are missing, it will also create them. Note that if
257 * this fails, it will not remove any parent directories it already made.
258 *
259 * \param path the path of the directory to create.
260 * \returns true on success or false on failure; call SDL_GetError() for more
261 * information.
262 *
263 * \since This function is available since SDL 3.1.3.
264 */
265extern SDL_DECLSPEC bool SDLCALL SDL_CreateDirectory(const char *path);
266
267/**
268 * Possible results from an enumeration callback.
269 *
270 * \since This enum is available since SDL 3.1.3.
271 *
272 * \sa SDL_EnumerateDirectoryCallback
273 */
275{
276 SDL_ENUM_CONTINUE, /**< Value that requests that enumeration continue. */
277 SDL_ENUM_SUCCESS, /**< Value that requests that enumeration stop, successfully. */
278 SDL_ENUM_FAILURE /**< Value that requests that enumeration stop, as a failure. */
280
281/**
282 * Callback for directory enumeration.
283 *
284 * Enumeration of directory entries will continue until either all entries
285 * have been provided to the callback, or the callback has requested a stop
286 * through its return value.
287 *
288 * Returning SDL_ENUM_CONTINUE will let enumeration proceed, calling the
289 * callback with further entries. SDL_ENUM_SUCCESS and SDL_ENUM_FAILURE will
290 * terminate the enumeration early, and dictate the return value of the
291 * enumeration function itself.
292 *
293 * \param userdata an app-controlled pointer that is passed to the callback.
294 * \param dirname the directory that is being enumerated.
295 * \param fname the next entry in the enumeration.
296 * \returns how the enumeration should proceed.
297 *
298 * \since This datatype is available since SDL 3.1.3.
299 *
300 * \sa SDL_EnumerateDirectory
301 */
302typedef SDL_EnumerationResult (SDLCALL *SDL_EnumerateDirectoryCallback)(void *userdata, const char *dirname, const char *fname);
303
304/**
305 * Enumerate a directory through a callback function.
306 *
307 * This function provides every directory entry through an app-provided
308 * callback, called once for each directory entry, until all results have been
309 * provided or the callback returns either SDL_ENUM_SUCCESS or
310 * SDL_ENUM_FAILURE.
311 *
312 * This will return false if there was a system problem in general, or if a
313 * callback returns SDL_ENUM_FAILURE. A successful return means a callback
314 * returned SDL_ENUM_SUCCESS to halt enumeration, or all directory entries
315 * were enumerated.
316 *
317 * \param path the path of the directory to enumerate.
318 * \param callback a function that is called for each entry in the directory.
319 * \param userdata a pointer that is passed to `callback`.
320 * \returns true on success or false on failure; call SDL_GetError() for more
321 * information.
322 *
323 * \since This function is available since SDL 3.1.3.
324 */
325extern SDL_DECLSPEC bool SDLCALL SDL_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);
326
327/**
328 * Remove a file or an empty directory.
329 *
330 * Directories that are not empty will fail; this function will not recursely
331 * delete directory trees.
332 *
333 * \param path the path to remove from the filesystem.
334 * \returns true on success or false on failure; call SDL_GetError() for more
335 * information.
336 *
337 * \since This function is available since SDL 3.1.3.
338 */
339extern SDL_DECLSPEC bool SDLCALL SDL_RemovePath(const char *path);
340
341/**
342 * Rename a file or directory.
343 *
344 * If the file at `newpath` already exists, it will replaced.
345 *
346 * Note that this will not copy files across filesystems/drives/volumes, as
347 * that is a much more complicated (and possibly time-consuming) operation.
348 *
349 * Which is to say, if this function fails, SDL_CopyFile() to a temporary file
350 * in the same directory as `newpath`, then SDL_RenamePath() from the
351 * temporary file to `newpath` and SDL_RemovePath() on `oldpath` might work
352 * for files. Renaming a non-empty directory across filesystems is
353 * dramatically more complex, however.
354 *
355 * \param oldpath the old path.
356 * \param newpath the new path.
357 * \returns true on success or false on failure; call SDL_GetError() for more
358 * information.
359 *
360 * \since This function is available since SDL 3.1.3.
361 */
362extern SDL_DECLSPEC bool SDLCALL SDL_RenamePath(const char *oldpath, const char *newpath);
363
364/**
365 * Copy a file.
366 *
367 * If the file at `newpath` already exists, it will be overwritten with the
368 * contents of the file at `oldpath`.
369 *
370 * This function will block until the copy is complete, which might be a
371 * significant time for large files on slow disks. On some platforms, the copy
372 * can be handed off to the OS itself, but on others SDL might just open both
373 * paths, and read from one and write to the other.
374 *
375 * Note that this is not an atomic operation! If something tries to read from
376 * `newpath` while the copy is in progress, it will see an incomplete copy of
377 * the data, and if the calling thread terminates (or the power goes out)
378 * during the copy, `newpath`'s previous contents will be gone, replaced with
379 * an incomplete copy of the data. To avoid this risk, it is recommended that
380 * the app copy to a temporary file in the same directory as `newpath`, and if
381 * the copy is successful, use SDL_RenamePath() to replace `newpath` with the
382 * temporary file. This will ensure that reads of `newpath` will either see a
383 * complete copy of the data, or it will see the pre-copy state of `newpath`.
384 *
385 * This function attempts to synchronize the newly-copied data to disk before
386 * returning, if the platform allows it, so that the renaming trick will not
387 * have a problem in a system crash or power failure, where the file could be
388 * renamed but the contents never made it from the system file cache to the
389 * physical disk.
390 *
391 * If the copy fails for any reason, the state of `newpath` is undefined. It
392 * might be half a copy, it might be the untouched data of what was already
393 * there, or it might be a zero-byte file, etc.
394 *
395 * \param oldpath the old path.
396 * \param newpath the new path.
397 * \returns true on success or false on failure; call SDL_GetError() for more
398 * information.
399 *
400 * \since This function is available since SDL 3.1.3.
401 */
402extern SDL_DECLSPEC bool SDLCALL SDL_CopyFile(const char *oldpath, const char *newpath);
403
404/**
405 * Get information about a filesystem path.
406 *
407 * \param path the path to query.
408 * \param info a pointer filled in with information about the path, or NULL to
409 * check for the existence of a file.
410 * \returns true on success or false if the file doesn't exist, or another
411 * failure; call SDL_GetError() for more information.
412 *
413 * \since This function is available since SDL 3.1.3.
414 */
415extern SDL_DECLSPEC bool SDLCALL SDL_GetPathInfo(const char *path, SDL_PathInfo *info);
416
417/**
418 * Enumerate a directory tree, filtered by pattern, and return a list.
419 *
420 * Files are filtered out if they don't match the string in `pattern`, which
421 * may contain wildcard characters '*' (match everything) and '?' (match one
422 * character). If pattern is NULL, no filtering is done and all results are
423 * returned. Subdirectories are permitted, and are specified with a path
424 * separator of '/'. Wildcard characters '*' and '?' never match a path
425 * separator.
426 *
427 * `flags` may be set to SDL_GLOB_CASEINSENSITIVE to make the pattern matching
428 * case-insensitive.
429 *
430 * The returned array is always NULL-terminated, for your iterating
431 * convenience, but if `count` is non-NULL, on return it will contain the
432 * number of items in the array, not counting the NULL terminator.
433 *
434 * \param path the path of the directory to enumerate.
435 * \param pattern the pattern that files in the directory must match. Can be
436 * NULL.
437 * \param flags `SDL_GLOB_*` bitflags that affect this search.
438 * \param count on return, will be set to the number of items in the returned
439 * array. Can be NULL.
440 * \returns an array of strings on success or NULL on failure; call
441 * SDL_GetError() for more information. This is a single allocation
442 * that should be freed with SDL_free() when it is no longer needed.
443 *
444 * \threadsafety It is safe to call this function from any thread.
445 *
446 * \since This function is available since SDL 3.1.3.
447 */
448extern SDL_DECLSPEC char ** SDLCALL SDL_GlobDirectory(const char *path, const char *pattern, SDL_GlobFlags flags, int *count);
449
450/* Ends C function definitions when using C++ */
451#ifdef __cplusplus
452}
453#endif
454#include <SDL3/SDL_close_code.h>
455
456#endif /* SDL_filesystem_h_ */
char ** SDL_GlobDirectory(const char *path, const char *pattern, SDL_GlobFlags flags, int *count)
bool SDL_CreateDirectory(const char *path)
const char * SDL_GetUserFolder(SDL_Folder folder)
SDL_Folder
@ SDL_FOLDER_COUNT
@ SDL_FOLDER_DOCUMENTS
@ SDL_FOLDER_TEMPLATES
@ SDL_FOLDER_DOWNLOADS
@ SDL_FOLDER_HOME
@ SDL_FOLDER_SAVEDGAMES
@ SDL_FOLDER_DESKTOP
@ SDL_FOLDER_PICTURES
@ SDL_FOLDER_PUBLICSHARE
@ SDL_FOLDER_SCREENSHOTS
@ SDL_FOLDER_VIDEOS
@ SDL_FOLDER_MUSIC
bool SDL_RemovePath(const char *path)
bool SDL_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata)
bool SDL_RenamePath(const char *oldpath, const char *newpath)
bool SDL_CopyFile(const char *oldpath, const char *newpath)
SDL_EnumerationResult
@ SDL_ENUM_CONTINUE
@ SDL_ENUM_SUCCESS
@ SDL_ENUM_FAILURE
Uint32 SDL_GlobFlags
SDL_EnumerationResult(* SDL_EnumerateDirectoryCallback)(void *userdata, const char *dirname, const char *fname)
char * SDL_GetPrefPath(const char *org, const char *app)
const char * SDL_GetBasePath(void)
bool SDL_GetPathInfo(const char *path, SDL_PathInfo *info)
SDL_PathType
@ SDL_PATHTYPE_FILE
@ SDL_PATHTYPE_NONE
@ SDL_PATHTYPE_DIRECTORY
@ SDL_PATHTYPE_OTHER
uint64_t Uint64
Definition SDL_stdinc.h:392
uint32_t Uint32
Definition SDL_stdinc.h:370
Sint64 SDL_Time
Definition SDL_stdinc.h:409
SDL_Time create_time
SDL_Time modify_time
SDL_Time access_time
SDL_PathType type