Move VFile to vfs.h and add VDirs
Jeffrey Pfau jeffrey@endrift.com
Wed, 16 Jul 2014 23:55:09 -0700
8 files changed,
106 insertions(+),
9 deletions(-)
M
src/gba/gba-savedata.c
→
src/gba/gba-savedata.c
@@ -3,7 +3,7 @@
#include "gba.h" #include "util/memory.h" -#include "util/vfile.h" +#include "util/vfs.h" #include <errno.h> #include <fcntl.h>
M
src/gba/gba-serialize.c
→
src/gba/gba-serialize.c
@@ -5,7 +5,7 @@ #include "gba-io.h"
#include "gba-thread.h" #include "util/memory.h" -#include "util/vfile.h" +#include "util/vfs.h" #include <fcntl.h>
M
src/gba/gba-thread.c
→
src/gba/gba-thread.c
@@ -7,7 +7,7 @@
#include "debugger/debugger.h" #include "util/patch.h" -#include "util/vfile.h" +#include "util/vfs.h" #include <signal.h>
M
src/gba/gba.c
→
src/gba/gba.c
@@ -7,7 +7,7 @@ #include "gba-thread.h"
#include "util/memory.h" #include "util/patch.h" -#include "util/vfile.h" +#include "util/vfs.h" const uint32_t GBA_ARM7TDMI_FREQUENCY = 0x1000000; const uint32_t GBA_COMPONENT_MAGIC = 0x1000000;
M
src/util/patch-ips.c
→
src/util/patch-ips.c
@@ -1,7 +1,7 @@
#include "util/patch-ips.h" #include "util/patch.h" -#include "util/vfile.h" +#include "util/vfs.h" static size_t _IPSOutputSize(struct Patch* patch, size_t inSize); static bool _IPSApplyPatch(struct Patch* patch, void* out, size_t outSize);
M
src/util/patch-ups.c
→
src/util/patch-ups.c
@@ -2,7 +2,7 @@ #include "util/patch-ips.h"
#include "util/crc32.h" #include "util/patch.h" -#include "util/vfile.h" +#include "util/vfs.h" enum { IN_CHECKSUM = -12,
M
src/util/vfile.c
→
src/util/vfs.c
@@ -1,12 +1,15 @@
-#include "util/vfile.h" +#include "util/vfs.h" #include <fcntl.h> +#include <dirent.h> #ifndef _WIN32 #include <sys/mman.h> +#define PATH_SEP '/' #else #include <io.h> #include <Windows.h> +#define PATH_SEP '\\' #endif struct VFileFD {@@ -25,6 +28,12 @@ static ssize_t _vfdWrite(struct VFile* vf, void* buffer, size_t size);
static void* _vfdMap(struct VFile* vf, size_t size, int flags); static void _vfdUnmap(struct VFile* vf, void* memory, size_t size); static void _vfdTruncate(struct VFile* vf, size_t size); + +static bool _vdClose(struct VDir* vd); +static struct VDirEntry* _vdListNext(struct VDir* vd); +static struct VFile* _vdOpenFile(struct VDir* vd, const char* path, int mode); + +static const char* _vdeName(struct VDirEntry* vde); struct VFile* VFileOpen(const char* path, int flags) { int fd = open(path, flags, 0666);@@ -137,3 +146,79 @@ static void _vfdTruncate(struct VFile* vf, size_t size) {
struct VFileFD* vfd = (struct VFileFD*) vf; ftruncate(vfd->fd, size); } + +struct VDirEntryDE { + struct VDirEntry d; + struct dirent* ent; +}; + +struct VDirDE { + struct VDir d; + DIR* de; + struct VDirEntryDE vde; + char* path; +}; + +struct VDir* VDirOpen(const char* path) { + DIR* de = opendir(path); + if (!de) { + return 0; + } + + struct VDirDE* vd = malloc(sizeof(struct VDirDE)); + if (!vd) { + return 0; + } + + vd->d.close = _vdClose; + vd->d.listNext = _vdListNext; + vd->d.openFile = _vdOpenFile; + vd->path = strdup(path); + vd->de = de; + + vd->vde.d.name = _vdeName; + + return &vd->d; +} + +bool _vdClose(struct VDir* vd) { + struct VDirDE* vdde = (struct VDirDE*) vd; + if (closedir(vdde->de) < 0) { + return false; + } + free(vdde->path); + free(vdde); + return true; +} + +struct VDirEntry* _vdListNext(struct VDir* vd) { + struct VDirDE* vdde = (struct VDirDE*) vd; + vdde->vde.ent = readdir(vdde->de); + if (vdde->vde.ent) { + return &vdde->vde.d; + } + + return 0; +} + +struct VFile* _vdOpenFile(struct VDir* vd, const char* path, int mode) { + struct VDirDE* vdde = (struct VDirDE*) vd; + if (!path) { + return 0; + } + const char* dir = vdde->path; + char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + 2)); + sprintf(combined, "%s%c%s", dir, PATH_SEP, path); + + struct VFile* file = VFileOpen(combined, mode); + free(combined); + return file; +} + +const char* _vdeName(struct VDirEntry* vde) { + struct VDirEntryDE* vdede = (struct VDirEntryDE*) vde; + if (vdede->ent) { + return vdede->ent->d_name; + } + return 0; +}
M
src/util/vfile.h
→
src/util/vfs.h
@@ -1,5 +1,5 @@
-#ifndef VFILE_H -#define VFILE_H +#ifndef VFS_H +#define VFS_H #include "common.h"@@ -16,7 +16,19 @@ void (*unmap)(struct VFile* vf, void* memory, size_t size);
void (*truncate)(struct VFile* vf, size_t size); }; +struct VDirEntry { + const char* (*name)(struct VDirEntry* vde); +}; + +struct VDir { + bool (*close)(struct VDir* vd); + struct VDirEntry* (*listNext)(struct VDir* vd); + struct VFile* (*openFile)(struct VDir* vd, const char* name, int mode); +}; + struct VFile* VFileOpen(const char* path, int flags); struct VFile* VFileFromFD(int fd); + +struct VDir* VDirOpen(const char* path); #endif