VFS: VFileOpen can now have a swappable backend

This commit is contained in:
Jeffrey Pfau 2015-06-20 03:11:11 -07:00
parent 2bb16fd0a8
commit 43f9f2dfd3
4 changed files with 37 additions and 1 deletions

View File

@ -5,6 +5,36 @@
* 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) {

View File

@ -53,6 +53,8 @@ struct VDir {
};
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);

View File

@ -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;
}

View File

@ -7,6 +7,7 @@
#include "util/memory.h"
#include <errno.h>
#include <stdio.h>
struct VFileFILE {
@ -28,6 +29,9 @@ struct VFile* VFileFOpen(const char* path, const char* 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);
}