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