VFS: VFileOpen can now have a swappable backend
Jeffrey Pfau jeffrey@endrift.com
Sat, 20 Jun 2015 03:11:11 -0700
4 files changed,
37 insertions(+),
1 deletions(-)
M
src/util/vfs.c
→
src/util/vfs.c
@@ -5,6 +5,36 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "vfs.h" +struct VFile* VFileOpen(const char* path, int flags) { +#ifdef USE_VFS_FILE + const char* chflags; + switch (flags & O_ACCMODE) { + case O_WRONLY: + if (flags & O_APPEND) { + chflags = "ab"; + } else { + chflags = "wb"; + } + break; + case O_RDWR: + if (flags & O_APPEND) { + chflags = "a+b"; + } else if (flags & O_TRUNC) { + chflags = "w+b"; + } else { + chflags = "r+b"; + } + break; + case O_RDONLY: + chflags = "rb"; + break; + } + return VFileFOpen(path, chflags); +#else + return VFileOpenFD(path, flags); +#endif +} + ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size) { ssize_t bytesRead = 0; while (bytesRead < size - 1) {
M
src/util/vfs.h
→
src/util/vfs.h
@@ -53,6 +53,8 @@ struct VFile* (*openFile)(struct VDir* vd, const char* name, int mode);
}; struct VFile* VFileOpen(const char* path, int flags); + +struct VFile* VFileOpenFD(const char* path, int flags); struct VFile* VFileFOpen(const char* path, const char* mode); struct VFile* VFileFromFD(int fd); struct VFile* VFileFromMemory(void* mem, size_t size);
M
src/util/vfs/vfs-fd.c
→
src/util/vfs/vfs-fd.c
@@ -29,7 +29,7 @@ static void _vfdUnmap(struct VFile* vf, void* memory, size_t size);
static void _vfdTruncate(struct VFile* vf, size_t size); static ssize_t _vfdSize(struct VFile* vf); -struct VFile* VFileOpen(const char* path, int flags) { +struct VFile* VFileOpenFD(const char* path, int flags) { if (!path) { return 0; }
M
src/util/vfs/vfs-file.c
→
src/util/vfs/vfs-file.c
@@ -7,6 +7,7 @@ #include "util/vfs.h"
#include "util/memory.h" +#include <errno.h> #include <stdio.h> struct VFileFILE {@@ -28,6 +29,9 @@ if (!path && !mode) {
return 0; } FILE* file = fopen(path, mode); + if (!file && errno == ENOENT && strcmp(mode, "r+b") == 0) { + file = fopen(path, "w+b"); + } return VFileFromFILE(file); }