SDL_filesystem.h 22 KB

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