From 278b17e56fc79c94774472198694149e15d9a3f8 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Tue, 25 Aug 2015 22:41:47 -0700 Subject: [PATCH] VFS: Add VDirEntry.type --- src/platform/3ds/3ds-vfs.c | 10 ++++++++++ src/platform/psp2/sce-vfs.c | 10 ++++++++++ src/util/vfs.h | 7 +++++++ src/util/vfs/vfs-dirent.c | 24 ++++++++++++++++++++++++ src/util/vfs/vfs-lzma.c | 20 +++++++++++++++----- src/util/vfs/vfs-zip.c | 17 ++++++++++++----- 6 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/platform/3ds/3ds-vfs.c b/src/platform/3ds/3ds-vfs.c index 0538b846a..852706849 100644 --- a/src/platform/3ds/3ds-vfs.c +++ b/src/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 @@ struct VDir* VDirOpen(const char* path) { vd3d->d.openFile = _vd3dOpenFile; vd3d->vde.d.name = _vd3deName; + vd3d->vde.d.type = _vd3deType; vd3d->vde.utf8Name = 0; return &vd3d->d; @@ -245,4 +247,12 @@ static const char* _vd3deName(struct VDirEntry* vde) { } 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 diff --git a/src/platform/psp2/sce-vfs.c b/src/platform/psp2/sce-vfs.c index e8bb8929f..05ac20d35 100644 --- a/src/platform/psp2/sce-vfs.c +++ b/src/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 @@ struct VDir* VDirOpen(const char* path) { 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; +} diff --git a/src/util/vfs.h b/src/util/vfs.h index 6fca9013c..8f9fbb534 100644 --- a/src/util/vfs.h +++ b/src/util/vfs.h @@ -29,6 +29,12 @@ enum { 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 VFile { struct VDirEntry { const char* (*name)(struct VDirEntry* vde); + enum VFSType (*type)(struct VDirEntry* vde); }; struct VDir { diff --git a/src/util/vfs/vfs-dirent.c b/src/util/vfs/vfs-dirent.c index 9632a7e3a..d18a7195a 100644 --- a/src/util/vfs/vfs-dirent.c +++ b/src/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 @@ struct VDir* VDirOpen(const char* path) { vd->de = de; vd->vde.d.name = _vdeName; + vd->vde.d.type = _vdeType; return &vd->d; } @@ -190,3 +192,25 @@ const char* _vdeName(struct VDirEntry* vde) { } 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 +} diff --git a/src/util/vfs/vfs-lzma.c b/src/util/vfs/vfs-lzma.c index c090ac921..97b711deb 100644 --- a/src/util/vfs/vfs-lzma.c +++ b/src/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 @@ struct VDir* VDirOpen7z(const char* path, int flags) { 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 @@ struct VFile* _vd7zOpenFile(struct VDir* vd, const char* path, int mode) { 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 @@ const char* _vde7zName(struct VDirEntry* vde) { 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 diff --git a/src/util/vfs/vfs-zip.c b/src/util/vfs/vfs-zip.c index feda1cea5..11ed8fbe8 100644 --- a/src/util/vfs/vfs-zip.c +++ b/src/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 @@ struct VDir* VDirOpenZip(const char* path, int flags) { 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 @@ struct VFile* _vdzOpenFile(struct VDir* vd, const char* path, int mode) { 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 @@ const char* _vdezName(struct VDirEntry* vde) { 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