Path: blob/master/thirdparty/sdl/include/SDL3/SDL_filesystem.h
9912 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/**22* # CategoryFilesystem23*24* SDL offers an API for examining and manipulating the system's filesystem.25* This covers most things one would need to do with directories, except for26* actual file I/O (which is covered by [CategoryIOStream](CategoryIOStream)27* and [CategoryAsyncIO](CategoryAsyncIO) instead).28*29* There are functions to answer necessary path questions:30*31* - Where is my app's data? SDL_GetBasePath().32* - Where can I safely write files? SDL_GetPrefPath().33* - Where are paths like Downloads, Desktop, Music? SDL_GetUserFolder().34* - What is this thing at this location? SDL_GetPathInfo().35* - What items live in this folder? SDL_EnumerateDirectory().36* - What items live in this folder by wildcard? SDL_GlobDirectory().37* - What is my current working directory? SDL_GetCurrentDirectory().38*39* SDL also offers functions to manipulate the directory tree: renaming,40* removing, copying files.41*/4243#ifndef SDL_filesystem_h_44#define SDL_filesystem_h_4546#include <SDL3/SDL_stdinc.h>47#include <SDL3/SDL_error.h>4849#include <SDL3/SDL_begin_code.h>5051/* Set up for C function definitions, even when using C++ */52#ifdef __cplusplus53extern "C" {54#endif5556/**57* Get the directory where the application was run from.58*59* SDL caches the result of this call internally, but the first call to this60* function is not necessarily fast, so plan accordingly.61*62* **macOS and iOS Specific Functionality**: If the application is in a ".app"63* bundle, this function returns the Resource directory (e.g.64* MyApp.app/Contents/Resources/). This behaviour can be overridden by adding65* a property to the Info.plist file. Adding a string key with the name66* SDL_FILESYSTEM_BASE_DIR_TYPE with a supported value will change the67* behaviour.68*69* Supported values for the SDL_FILESYSTEM_BASE_DIR_TYPE property (Given an70* application in /Applications/SDLApp/MyApp.app):71*72* - `resource`: bundle resource directory (the default). For example:73* `/Applications/SDLApp/MyApp.app/Contents/Resources`74* - `bundle`: the Bundle directory. For example:75* `/Applications/SDLApp/MyApp.app/`76* - `parent`: the containing directory of the bundle. For example:77* `/Applications/SDLApp/`78*79* **Nintendo 3DS Specific Functionality**: This function returns "romfs"80* directory of the application as it is uncommon to store resources outside81* the executable. As such it is not a writable directory.82*83* The returned path is guaranteed to end with a path separator ('\\' on84* Windows, '/' on most other platforms).85*86* \returns an absolute path in UTF-8 encoding to the application data87* directory. NULL will be returned on error or when the platform88* doesn't implement this functionality, call SDL_GetError() for more89* information.90*91* \since This function is available since SDL 3.2.0.92*93* \sa SDL_GetPrefPath94*/95extern SDL_DECLSPEC const char * SDLCALL SDL_GetBasePath(void);9697/**98* Get the user-and-app-specific path where files can be written.99*100* Get the "pref dir". This is meant to be where users can write personal101* files (preferences and save games, etc) that are specific to your102* application. This directory is unique per user, per application.103*104* This function will decide the appropriate location in the native105* filesystem, create the directory if necessary, and return a string of the106* absolute path to the directory in UTF-8 encoding.107*108* On Windows, the string might look like:109*110* `C:\\Users\\bob\\AppData\\Roaming\\My Company\\My Program Name\\`111*112* On Linux, the string might look like:113*114* `/home/bob/.local/share/My Program Name/`115*116* On macOS, the string might look like:117*118* `/Users/bob/Library/Application Support/My Program Name/`119*120* You should assume the path returned by this function is the only safe place121* to write files (and that SDL_GetBasePath(), while it might be writable, or122* even the parent of the returned path, isn't where you should be writing123* things).124*125* Both the org and app strings may become part of a directory name, so please126* follow these rules:127*128* - Try to use the same org string (_including case-sensitivity_) for all129* your applications that use this function.130* - Always use a unique app string for each one, and make sure it never131* changes for an app once you've decided on it.132* - Unicode characters are legal, as long as they are UTF-8 encoded, but...133* - ...only use letters, numbers, and spaces. Avoid punctuation like "Game134* Name 2: Bad Guy's Revenge!" ... "Game Name 2" is sufficient.135*136* The returned path is guaranteed to end with a path separator ('\\' on137* Windows, '/' on most other platforms).138*139* \param org the name of your organization.140* \param app the name of your application.141* \returns a UTF-8 string of the user directory in platform-dependent142* notation. NULL if there's a problem (creating directory failed,143* etc.). This should be freed with SDL_free() when it is no longer144* needed.145*146* \since This function is available since SDL 3.2.0.147*148* \sa SDL_GetBasePath149*/150extern SDL_DECLSPEC char * SDLCALL SDL_GetPrefPath(const char *org, const char *app);151152/**153* The type of the OS-provided default folder for a specific purpose.154*155* Note that the Trash folder isn't included here, because trashing files156* usually involves extra OS-specific functionality to remember the file's157* original location.158*159* The folders supported per platform are:160*161* | | Windows | macOS/iOS | tvOS | Unix (XDG) | Haiku | Emscripten |162* | ----------- | ------- | --------- | ---- | ---------- | ----- | ---------- |163* | HOME | X | X | | X | X | X |164* | DESKTOP | X | X | | X | X | |165* | DOCUMENTS | X | X | | X | | |166* | DOWNLOADS | Vista+ | X | | X | | |167* | MUSIC | X | X | | X | | |168* | PICTURES | X | X | | X | | |169* | PUBLICSHARE | | X | | X | | |170* | SAVEDGAMES | Vista+ | | | | | |171* | SCREENSHOTS | Vista+ | | | | | |172* | TEMPLATES | X | X | | X | | |173* | VIDEOS | X | X* | | X | | |174*175* Note that on macOS/iOS, the Videos folder is called "Movies".176*177* \since This enum is available since SDL 3.2.0.178*179* \sa SDL_GetUserFolder180*/181typedef enum SDL_Folder182{183SDL_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. */184SDL_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. */185SDL_FOLDER_DOCUMENTS, /**< User document files, possibly application-specific. This is a good place to save a user's projects. */186SDL_FOLDER_DOWNLOADS, /**< Standard folder for user files downloaded from the internet. */187SDL_FOLDER_MUSIC, /**< Music files that can be played using a standard music player (mp3, ogg...). */188SDL_FOLDER_PICTURES, /**< Image files that can be displayed using a standard viewer (png, jpg...). */189SDL_FOLDER_PUBLICSHARE, /**< Files that are meant to be shared with other users on the same computer. */190SDL_FOLDER_SAVEDGAMES, /**< Save files for games. */191SDL_FOLDER_SCREENSHOTS, /**< Application screenshots. */192SDL_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. */193SDL_FOLDER_VIDEOS, /**< Video files that can be played using a standard video player (mp4, webm...). */194SDL_FOLDER_COUNT /**< Total number of types in this enum, not a folder type by itself. */195} SDL_Folder;196197/**198* Finds the most suitable user folder for a specific purpose.199*200* Many OSes provide certain standard folders for certain purposes, such as201* storing pictures, music or videos for a certain user. This function gives202* the path for many of those special locations.203*204* This function is specifically for _user_ folders, which are meant for the205* user to access and manage. For application-specific folders, meant to hold206* data for the application to manage, see SDL_GetBasePath() and207* SDL_GetPrefPath().208*209* The returned path is guaranteed to end with a path separator ('\\' on210* Windows, '/' on most other platforms).211*212* If NULL is returned, the error may be obtained with SDL_GetError().213*214* \param folder the type of folder to find.215* \returns either a null-terminated C string containing the full path to the216* folder, or NULL if an error happened.217*218* \since This function is available since SDL 3.2.0.219*/220extern SDL_DECLSPEC const char * SDLCALL SDL_GetUserFolder(SDL_Folder folder);221222223/* Abstract filesystem interface */224225/**226* Types of filesystem entries.227*228* Note that there may be other sorts of items on a filesystem: devices,229* symlinks, named pipes, etc. They are currently reported as230* SDL_PATHTYPE_OTHER.231*232* \since This enum is available since SDL 3.2.0.233*234* \sa SDL_PathInfo235*/236typedef enum SDL_PathType237{238SDL_PATHTYPE_NONE, /**< path does not exist */239SDL_PATHTYPE_FILE, /**< a normal file */240SDL_PATHTYPE_DIRECTORY, /**< a directory */241SDL_PATHTYPE_OTHER /**< something completely different like a device node (not a symlink, those are always followed) */242} SDL_PathType;243244/**245* Information about a path on the filesystem.246*247* \since This datatype is available since SDL 3.2.0.248*249* \sa SDL_GetPathInfo250* \sa SDL_GetStoragePathInfo251*/252typedef struct SDL_PathInfo253{254SDL_PathType type; /**< the path type */255Uint64 size; /**< the file size in bytes */256SDL_Time create_time; /**< the time when the path was created */257SDL_Time modify_time; /**< the last time the path was modified */258SDL_Time access_time; /**< the last time the path was read */259} SDL_PathInfo;260261/**262* Flags for path matching.263*264* \since This datatype is available since SDL 3.2.0.265*266* \sa SDL_GlobDirectory267* \sa SDL_GlobStorageDirectory268*/269typedef Uint32 SDL_GlobFlags;270271#define SDL_GLOB_CASEINSENSITIVE (1u << 0)272273/**274* Create a directory, and any missing parent directories.275*276* This reports success if `path` already exists as a directory.277*278* If parent directories are missing, it will also create them. Note that if279* this fails, it will not remove any parent directories it already made.280*281* \param path the path of the directory to create.282* \returns true on success or false on failure; call SDL_GetError() for more283* information.284*285* \since This function is available since SDL 3.2.0.286*/287extern SDL_DECLSPEC bool SDLCALL SDL_CreateDirectory(const char *path);288289/**290* Possible results from an enumeration callback.291*292* \since This enum is available since SDL 3.2.0.293*294* \sa SDL_EnumerateDirectoryCallback295*/296typedef enum SDL_EnumerationResult297{298SDL_ENUM_CONTINUE, /**< Value that requests that enumeration continue. */299SDL_ENUM_SUCCESS, /**< Value that requests that enumeration stop, successfully. */300SDL_ENUM_FAILURE /**< Value that requests that enumeration stop, as a failure. */301} SDL_EnumerationResult;302303/**304* Callback for directory enumeration.305*306* Enumeration of directory entries will continue until either all entries307* have been provided to the callback, or the callback has requested a stop308* through its return value.309*310* Returning SDL_ENUM_CONTINUE will let enumeration proceed, calling the311* callback with further entries. SDL_ENUM_SUCCESS and SDL_ENUM_FAILURE will312* terminate the enumeration early, and dictate the return value of the313* enumeration function itself.314*315* `dirname` is guaranteed to end with a path separator ('\\' on Windows, '/'316* on most other platforms).317*318* \param userdata an app-controlled pointer that is passed to the callback.319* \param dirname the directory that is being enumerated.320* \param fname the next entry in the enumeration.321* \returns how the enumeration should proceed.322*323* \since This datatype is available since SDL 3.2.0.324*325* \sa SDL_EnumerateDirectory326*/327typedef SDL_EnumerationResult (SDLCALL *SDL_EnumerateDirectoryCallback)(void *userdata, const char *dirname, const char *fname);328329/**330* Enumerate a directory through a callback function.331*332* This function provides every directory entry through an app-provided333* callback, called once for each directory entry, until all results have been334* provided or the callback returns either SDL_ENUM_SUCCESS or335* SDL_ENUM_FAILURE.336*337* This will return false if there was a system problem in general, or if a338* callback returns SDL_ENUM_FAILURE. A successful return means a callback339* returned SDL_ENUM_SUCCESS to halt enumeration, or all directory entries340* were enumerated.341*342* \param path the path of the directory to enumerate.343* \param callback a function that is called for each entry in the directory.344* \param userdata a pointer that is passed to `callback`.345* \returns true on success or false on failure; call SDL_GetError() for more346* information.347*348* \since This function is available since SDL 3.2.0.349*/350extern SDL_DECLSPEC bool SDLCALL SDL_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback callback, void *userdata);351352/**353* Remove a file or an empty directory.354*355* Directories that are not empty will fail; this function will not recursely356* delete directory trees.357*358* \param path the path to remove from the filesystem.359* \returns true on success or false on failure; call SDL_GetError() for more360* information.361*362* \since This function is available since SDL 3.2.0.363*/364extern SDL_DECLSPEC bool SDLCALL SDL_RemovePath(const char *path);365366/**367* Rename a file or directory.368*369* If the file at `newpath` already exists, it will replaced.370*371* Note that this will not copy files across filesystems/drives/volumes, as372* that is a much more complicated (and possibly time-consuming) operation.373*374* Which is to say, if this function fails, SDL_CopyFile() to a temporary file375* in the same directory as `newpath`, then SDL_RenamePath() from the376* temporary file to `newpath` and SDL_RemovePath() on `oldpath` might work377* for files. Renaming a non-empty directory across filesystems is378* dramatically more complex, however.379*380* \param oldpath the old path.381* \param newpath the new path.382* \returns true on success or false on failure; call SDL_GetError() for more383* information.384*385* \since This function is available since SDL 3.2.0.386*/387extern SDL_DECLSPEC bool SDLCALL SDL_RenamePath(const char *oldpath, const char *newpath);388389/**390* Copy a file.391*392* If the file at `newpath` already exists, it will be overwritten with the393* contents of the file at `oldpath`.394*395* This function will block until the copy is complete, which might be a396* significant time for large files on slow disks. On some platforms, the copy397* can be handed off to the OS itself, but on others SDL might just open both398* paths, and read from one and write to the other.399*400* Note that this is not an atomic operation! If something tries to read from401* `newpath` while the copy is in progress, it will see an incomplete copy of402* the data, and if the calling thread terminates (or the power goes out)403* during the copy, `newpath`'s previous contents will be gone, replaced with404* an incomplete copy of the data. To avoid this risk, it is recommended that405* the app copy to a temporary file in the same directory as `newpath`, and if406* the copy is successful, use SDL_RenamePath() to replace `newpath` with the407* temporary file. This will ensure that reads of `newpath` will either see a408* complete copy of the data, or it will see the pre-copy state of `newpath`.409*410* This function attempts to synchronize the newly-copied data to disk before411* returning, if the platform allows it, so that the renaming trick will not412* have a problem in a system crash or power failure, where the file could be413* renamed but the contents never made it from the system file cache to the414* physical disk.415*416* If the copy fails for any reason, the state of `newpath` is undefined. It417* might be half a copy, it might be the untouched data of what was already418* there, or it might be a zero-byte file, etc.419*420* \param oldpath the old path.421* \param newpath the new path.422* \returns true on success or false on failure; call SDL_GetError() for more423* information.424*425* \since This function is available since SDL 3.2.0.426*/427extern SDL_DECLSPEC bool SDLCALL SDL_CopyFile(const char *oldpath, const char *newpath);428429/**430* Get information about a filesystem path.431*432* \param path the path to query.433* \param info a pointer filled in with information about the path, or NULL to434* check for the existence of a file.435* \returns true on success or false if the file doesn't exist, or another436* failure; call SDL_GetError() for more information.437*438* \since This function is available since SDL 3.2.0.439*/440extern SDL_DECLSPEC bool SDLCALL SDL_GetPathInfo(const char *path, SDL_PathInfo *info);441442/**443* Enumerate a directory tree, filtered by pattern, and return a list.444*445* Files are filtered out if they don't match the string in `pattern`, which446* may contain wildcard characters '\*' (match everything) and '?' (match one447* character). If pattern is NULL, no filtering is done and all results are448* returned. Subdirectories are permitted, and are specified with a path449* separator of '/'. Wildcard characters '\*' and '?' never match a path450* separator.451*452* `flags` may be set to SDL_GLOB_CASEINSENSITIVE to make the pattern matching453* case-insensitive.454*455* The returned array is always NULL-terminated, for your iterating456* convenience, but if `count` is non-NULL, on return it will contain the457* number of items in the array, not counting the NULL terminator.458*459* \param path the path of the directory to enumerate.460* \param pattern the pattern that files in the directory must match. Can be461* NULL.462* \param flags `SDL_GLOB_*` bitflags that affect this search.463* \param count on return, will be set to the number of items in the returned464* array. Can be NULL.465* \returns an array of strings on success or NULL on failure; call466* SDL_GetError() for more information. This is a single allocation467* that should be freed with SDL_free() when it is no longer needed.468*469* \threadsafety It is safe to call this function from any thread.470*471* \since This function is available since SDL 3.2.0.472*/473extern SDL_DECLSPEC char ** SDLCALL SDL_GlobDirectory(const char *path, const char *pattern, SDL_GlobFlags flags, int *count);474475/**476* Get what the system believes is the "current working directory."477*478* For systems without a concept of a current working directory, this will479* still attempt to provide something reasonable.480*481* SDL does not provide a means to _change_ the current working directory; for482* platforms without this concept, this would cause surprises with file access483* outside of SDL.484*485* The returned path is guaranteed to end with a path separator ('\\' on486* Windows, '/' on most other platforms).487*488* \returns a UTF-8 string of the current working directory in489* platform-dependent notation. NULL if there's a problem. This490* should be freed with SDL_free() when it is no longer needed.491*492* \since This function is available since SDL 3.2.0.493*/494extern SDL_DECLSPEC char * SDLCALL SDL_GetCurrentDirectory(void);495496/* Ends C function definitions when using C++ */497#ifdef __cplusplus498}499#endif500#include <SDL3/SDL_close_code.h>501502#endif /* SDL_filesystem_h_ */503504505