all repos — mgba @ 278b17e56fc79c94774472198694149e15d9a3f8

mGBA Game Boy Advance Emulator

VFS: Add VDirEntry.type
Jeffrey Pfau jeffrey@endrift.com
Tue, 25 Aug 2015 22:41:47 -0700
commit

278b17e56fc79c94774472198694149e15d9a3f8

parent

a1eb021af5ef63072e2c71af57f6e7a7da3d2585

M src/platform/3ds/3ds-vfs.csrc/platform/3ds/3ds-vfs.c

@@ -46,6 +46,7 @@ static struct VDirEntry* _vd3dListNext(struct VDir* vd);

static struct VFile* _vd3dOpenFile(struct VDir* vd, const char* path, int mode); static const char* _vd3deName(struct VDirEntry* vde); +static enum VFSType _vd3deType(struct VDirEntry* vde); struct VFile* VFileOpen3DS(FS_archive* archive, const char* path, int flags) { struct VFile3DS* vf3d = malloc(sizeof(struct VFile3DS));

@@ -186,6 +187,7 @@ vd3d->d.listNext = _vd3dListNext; //// Crashes here for no good reason

vd3d->d.openFile = _vd3dOpenFile; vd3d->vde.d.name = _vd3deName; + vd3d->vde.d.type = _vd3deType; vd3d->vde.utf8Name = 0; return &vd3d->d;

@@ -244,5 +246,13 @@ if (!vd3de->utf8Name) {

vd3de->utf8Name = utf16to8(vd3de->ent.name, sizeof(vd3de->ent.name) / 2); } return vd3de->utf8Name; +} + +static enum VFSType _vd3deType(struct VDirEntry* vde) { + struct VDirEntry3DS* vd3de = (struct VDirEntry3DS*) vde; + if (vd3de->ent.isDirectory) { + return VFS_DIRECTORY; + } + return VFS_FILE; } #endif
M src/platform/psp2/sce-vfs.csrc/platform/psp2/sce-vfs.c

@@ -44,6 +44,7 @@ static struct VDirEntry* _vdsceListNext(struct VDir* vd);

static struct VFile* _vdsceOpenFile(struct VDir* vd, const char* path, int mode); static const char* _vdesceName(struct VDirEntry* vde); +static enum VFSType _vdesceType(struct VDirEntry* vde); struct VFile* VFileOpenSce(const char* path, int flags, SceMode mode) { struct VFileSce* vfsce = malloc(sizeof(struct VFileSce));

@@ -147,6 +148,7 @@ vd->d.openFile = _vdsceOpenFile;

vd->path = strdup(path); vd->de.d.name = _vdesceName; + vd->de.d.type = _vdesceType; return &vd->d; }

@@ -193,3 +195,11 @@ static const char* _vdesceName(struct VDirEntry* vde) {

struct VDirEntrySce* vdesce = (struct VDirEntrySce*) vde; return vdesce->ent.d_name; } + +static enum VFSType _vdesceType(struct VDirEntry* vde) { + struct VDirEntrySce* vdesce = (struct VDirEntrySce*) vde; + if (PSP2_S_ISDIR(vdesce->ent.d_stat.st_mode)) { + return VFS_DIRECTORY; + } + return VFS_FILE; +}
M src/util/vfs.hsrc/util/vfs.h

@@ -29,6 +29,12 @@ MAP_READ = 1,

MAP_WRITE = 2 }; +enum VFSType { + VFS_UNKNOWN = 0, + VFS_FILE, + VFS_DIRECTORY +}; + struct VFile { bool (*close)(struct VFile* vf); off_t (*seek)(struct VFile* vf, off_t offset, int whence);

@@ -44,6 +50,7 @@ };

struct VDirEntry { const char* (*name)(struct VDirEntry* vde); + enum VFSType (*type)(struct VDirEntry* vde); }; struct VDir {
M src/util/vfs/vfs-dirent.csrc/util/vfs/vfs-dirent.c

@@ -15,6 +15,7 @@ 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); +static enum VFSType _vdeType(struct VDirEntry* vde); struct VDirEntryDE { struct VDirEntry d;

@@ -48,6 +49,7 @@ vd->path = strdup(path);

vd->de = de; vd->vde.d.name = _vdeName; + vd->vde.d.type = _vdeType; return &vd->d; }

@@ -190,3 +192,25 @@ return vdede->ent->d_name;

} return 0; } + +static enum VFSType _vdeType(struct VDirEntry* vde) { + struct VDirEntryDE* vdede = (struct VDirEntryDE*) vde; +#ifndef WIN32 + if (vdede->ent->d_type == DT_DIR) { + return VFS_DIRECTORY; + } + return VFS_FILE; +#else + const char* dir = vdde->path; + char* combined = malloc(sizeof(char) * (strlen(vdede->ent->d_name) + strlen(dir) + 2)); + sprintf(combined, "%s%s%s", dir, PATH_SEP, vdede->ent->d_name); + struct stat sb; + stat(combined, &sb); + free(combined); + + if (S_ISDIR(sb.st_mode)) { + return VFS_DIRECTORY; + } + return VFS_FILE; +#endif +}
M src/util/vfs/vfs-lzma.csrc/util/vfs/vfs-lzma.c

@@ -64,6 +64,7 @@ static struct VDirEntry* _vd7zListNext(struct VDir* vd);

static struct VFile* _vd7zOpenFile(struct VDir* vd, const char* path, int mode); static const char* _vde7zName(struct VDirEntry* vde); +static enum VFSType _vde7zType(struct VDirEntry* vde); struct VDir* VDirOpen7z(const char* path, int flags) { if (flags & O_WRONLY || flags & O_CREAT) {

@@ -104,6 +105,7 @@ vd->dirent.index = 0;

vd->dirent.utf8 = 0; vd->dirent.vd = vd; vd->dirent.d.name = _vde7zName; + vd->dirent.d.type = _vde7zType; vd->d.close = _vd7zClose; vd->d.rewind = _vd7zRewind;

@@ -299,6 +301,13 @@

return &vf->d; } +bool _vf7zSync(struct VFile* vf, const void* memory, size_t size) { + UNUSED(vf); + UNUSED(memory); + UNUSED(size); + return false; +} + const char* _vde7zName(struct VDirEntry* vde) { struct VDirEntry7z* vde7z = (struct VDirEntry7z*) vde; if (!vde7z->utf8) {

@@ -312,11 +321,12 @@

return vde7z->utf8; } -bool _vf7zSync(struct VFile* vf, const void* memory, size_t size) { - UNUSED(vf); - UNUSED(memory); - UNUSED(size); - return false; +static enum VFSType _vde7zType(struct VDirEntry* vde) { + struct VDirEntry7z* vde7z = (struct VDirEntry7z*) vde; + if (SzArEx_IsDir(&vde7z->vd->db, vde7z->index)) { + return VFS_DIRECTORY; + } + return VFS_FILE; } #endif
M src/util/vfs/vfs-zip.csrc/util/vfs/vfs-zip.c

@@ -51,6 +51,7 @@ static struct VDirEntry* _vdzListNext(struct VDir* vd);

static struct VFile* _vdzOpenFile(struct VDir* vd, const char* path, int mode); static const char* _vdezName(struct VDirEntry* vde); +static enum VFSType _vdezType(struct VDirEntry* vde); struct VDir* VDirOpenZip(const char* path, int flags) { int zflags = 0;

@@ -74,6 +75,7 @@ vd->d.openFile = _vdzOpenFile;

vd->z = z; vd->dirent.d.name = _vdezName; + vd->dirent.d.type = _vdezType; vd->dirent.index = -1; vd->dirent.z = z;

@@ -295,6 +297,13 @@

return &vfz->d; } +bool _vfzSync(struct VFile* vf, const void* memory, size_t size) { + UNUSED(vf); + UNUSED(memory); + UNUSED(size); + return false; +} + const char* _vdezName(struct VDirEntry* vde) { struct VDirEntryZip* vdez = (struct VDirEntryZip*) vde; struct zip_stat s;

@@ -304,11 +313,9 @@ }

return s.name; } -bool _vfzSync(struct VFile* vf, const void* memory, size_t size) { - UNUSED(vf); - UNUSED(memory); - UNUSED(size); - return false; +static enum VFSType _vdezType(struct VDirEntry* vde) { + struct VDirEntryZip* vdez = (struct VDirEntryZip*) vde; + return VFS_UNKNOWN; } #endif