Commit e272f567 authored by David Reid's avatar David Reid

Update fs.

parent 1a83b0ba
...@@ -2105,8 +2105,15 @@ FS_API fs_result fs_mkdir(fs* pFS, const char* pPath) ...@@ -2105,8 +2105,15 @@ FS_API fs_result fs_mkdir(fs* pFS, const char* pPath)
char* pRunningPath = pRunningPathStack; char* pRunningPath = pRunningPathStack;
size_t runningPathLen = 0; size_t runningPathLen = 0;
fs_path_iterator iSegment; fs_path_iterator iSegment;
const fs_backend* pBackend;
pBackend = fs_get_backend_or_default(pFS);
if (pBackend == NULL) {
return FS_INVALID_ARGS;
}
if (pFS == NULL || pPath == NULL) { if (pPath == NULL) {
return FS_INVALID_ARGS; return FS_INVALID_ARGS;
} }
...@@ -2144,7 +2151,7 @@ FS_API fs_result fs_mkdir(fs* pFS, const char* pPath) ...@@ -2144,7 +2151,7 @@ FS_API fs_result fs_mkdir(fs* pFS, const char* pPath)
runningPathLen += iSegment.segmentLength; runningPathLen += iSegment.segmentLength;
pRunningPath[runningPathLen] = '\0'; pRunningPath[runningPathLen] = '\0';
result = fs_backend_mkdir(pFS->pBackend, pFS, pRunningPath); result = fs_backend_mkdir(pBackend, pFS, pRunningPath);
/* We just pretend to be successful if the directory already exists. */ /* We just pretend to be successful if the directory already exists. */
if (result == FS_ALREADY_EXISTS) { if (result == FS_ALREADY_EXISTS) {
...@@ -3040,6 +3047,7 @@ FS_API fs_result fs_file_open_or_info(fs* pFS, const char* pFilePath, int openMo ...@@ -3040,6 +3047,7 @@ FS_API fs_result fs_file_open_or_info(fs* pFS, const char* pFilePath, int openMo
We'll need to iterate over every mount point and keep track of the mount point with the longest We'll need to iterate over every mount point and keep track of the mount point with the longest
prefix that matches the start of the file path. prefix that matches the start of the file path.
*/ */
if (pFS != NULL) {
fs_mount_list_iterator iMountPoint; fs_mount_list_iterator iMountPoint;
fs_mount_point* pBestMountPoint = NULL; fs_mount_point* pBestMountPoint = NULL;
const char* pBestMountPointPath = NULL; const char* pBestMountPointPath = NULL;
...@@ -3129,6 +3137,21 @@ FS_API fs_result fs_file_open_or_info(fs* pFS, const char* pFilePath, int openMo ...@@ -3129,6 +3137,21 @@ FS_API fs_result fs_file_open_or_info(fs* pFS, const char* pFilePath, int openMo
} else { } else {
return FS_DOES_NOT_EXIST; /* Couldn't find an appropriate mount point. */ return FS_DOES_NOT_EXIST; /* Couldn't find an appropriate mount point. */
} }
} else {
/*
No "fs" object was supplied. Open using the default backend without using mount points. This is as if you were
opening a file using `fopen()`.
*/
if ((openMode & FS_ONLY_MOUNTS) == 0) {
return fs_file_alloc_if_necessary_and_open_or_info(pFS, pFilePath, openMode, ppFile, pInfo);
} else {
/*
Getting here means only the mount points can be used to open the file (cannot open straight from
the file system natively).
*/
return FS_DOES_NOT_EXIST;
}
}
} else { } else {
/* Opening in read mode. */ /* Opening in read mode. */
fs_mount_list_iterator iMountPoint; fs_mount_list_iterator iMountPoint;
......
...@@ -267,9 +267,14 @@ be saved: ...@@ -267,9 +267,14 @@ be saved:
```c ```c
fs_file_open(pFS, "config/game.cfg", FS_WRITE, &pFile); // Prefixed with "config", so will use the "config" mount point. fs_file_open(pFS, "config/game.cfg", FS_WRITE, &pFile); // Prefixed with "config", so will use the "config" mount point.
fs_file_open(pFs, "saves/save0.sav", FS_WRITE, &pFile); // Prefixed with "saves", so will use the "saves" mount point. fs_file_open(pFS, "saves/save0.sav", FS_WRITE, &pFile); // Prefixed with "saves", so will use the "saves" mount point.
``` ```
When opening a file for writing, if you pass in NULL for the `pFS` parameter it will open the file
like normal using the standard file system. That is it'll work exactly as if you were using stdio
`fopen()` and you will not be able use mount points. Keep in mind that there is no notion of a
"current directory" in this library so you'll be stuck with the initial working directory.
By default, you can move outside the mount point with ".." segments. If you want to disable this By default, you can move outside the mount point with ".." segments. If you want to disable this
functionality, you can use the `FS_NO_ABOVE_ROOT_NAVIGATION` flag: functionality, you can use the `FS_NO_ABOVE_ROOT_NAVIGATION` flag:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment