all repos — mgba @ a6001496bce5ff9c2ef2f93df2ab4fe396f48266

mGBA Game Boy Advance Emulator

Util: Add VFile.size function (fixes #153)
Jeffrey Pfau jeffrey@endrift.com
Sun, 11 Jan 2015 00:35:22 -0800
commit

a6001496bce5ff9c2ef2f93df2ab4fe396f48266

parent

c8be60f88b651539a224f35054b0856a08d1a76f

M src/gba/gba-savedata.csrc/gba/gba-savedata.c

@@ -142,7 +142,7 @@ if (!savedata->vf) {

end = 0; savedata->data = anonymousMemoryMap(SIZE_CART_FLASH1M); } else { - end = savedata->vf->seek(savedata->vf, 0, SEEK_END); + end = savedata->vf->size(savedata->vf); if (end < SIZE_CART_FLASH512) { savedata->vf->truncate(savedata->vf, SIZE_CART_FLASH1M); flashSize = SIZE_CART_FLASH1M;

@@ -168,7 +168,7 @@ if (!savedata->vf) {

end = 0; savedata->data = anonymousMemoryMap(SIZE_CART_EEPROM); } else { - end = savedata->vf->seek(savedata->vf, 0, SEEK_END); + end = savedata->vf->size(savedata->vf); if (end < SIZE_CART_EEPROM) { savedata->vf->truncate(savedata->vf, SIZE_CART_EEPROM); }

@@ -191,7 +191,7 @@ if (!savedata->vf) {

end = 0; savedata->data = anonymousMemoryMap(SIZE_CART_SRAM); } else { - end = savedata->vf->seek(savedata->vf, 0, SEEK_END); + end = savedata->vf->size(savedata->vf); if (end < SIZE_CART_SRAM) { savedata->vf->truncate(savedata->vf, SIZE_CART_SRAM); }
M src/gba/gba.csrc/gba/gba.c

@@ -445,7 +445,7 @@ }

void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) { gba->romVf = vf; - gba->pristineRomSize = vf->seek(vf, 0, SEEK_END); + gba->pristineRomSize = vf->size(vf); vf->seek(vf, 0, SEEK_SET); if (gba->pristineRomSize > SIZE_CART0) { gba->pristineRomSize = SIZE_CART0;
M src/platform/qt/VFileDevice.cppsrc/platform/qt/VFileDevice.cpp

@@ -27,9 +27,5 @@ return m_vf->write(m_vf, data, maxSize);

} qint64 VFileDevice::size() const { - // TODO: Add size method to VFile so this can be actually const - ssize_t pos = m_vf->seek(m_vf, 0, SEEK_CUR); - qint64 size = m_vf->seek(m_vf, 0, SEEK_END); - m_vf->seek(m_vf, pos, SEEK_SET); - return size; + return m_vf->size(m_vf); }
M src/util/patch-ups.csrc/util/patch-ups.c

@@ -31,7 +31,7 @@ if (memcmp(buffer, "UPS1", 4) != 0) {

return false; } - size_t filesize = patch->vf->seek(patch->vf, 0, SEEK_END); + size_t filesize = patch->vf->size(patch->vf); uint32_t goodCrc32; patch->vf->seek(patch->vf, PATCH_CHECKSUM, SEEK_END);

@@ -61,7 +61,7 @@

bool _UPSApplyPatch(struct Patch* patch, void* out, size_t outSize) { // TODO: Input checksum - size_t filesize = patch->vf->seek(patch->vf, 0, SEEK_END); + size_t filesize = patch->vf->size(patch->vf); patch->vf->seek(patch->vf, 4, SEEK_SET); _UPSDecodeLength(patch->vf); // Discard input size if (_UPSDecodeLength(patch->vf) != outSize) {
M src/util/vfs.csrc/util/vfs.c

@@ -9,6 +9,7 @@ #include "util/string.h"

#include <fcntl.h> #include <dirent.h> +#include <sys/stat.h> #ifndef _WIN32 #include <sys/mman.h>

@@ -35,6 +36,7 @@ static ssize_t _vfdWrite(struct VFile* vf, const 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 ssize_t _vfdSize(struct VFile* vf); static bool _vdClose(struct VDir* vd); static void _vdRewind(struct VDir* vd);

@@ -73,6 +75,7 @@ vfd->d.write = _vfdWrite;

vfd->d.map = _vfdMap; vfd->d.unmap = _vfdUnmap; vfd->d.truncate = _vfdTruncate; + vfd->d.size = _vfdSize; return &vfd->d; }

@@ -137,9 +140,12 @@ if (flags & MAP_WRITE) {

createFlags = PAGE_READWRITE; mapFiles = FILE_MAP_WRITE; } - size_t location = lseek(vfd->fd, 0, SEEK_CUR); - size_t fileSize = lseek(vfd->fd, 0, SEEK_END); - lseek(vfd->fd, location, SEEK_SET); + size_t fileSize; + struct stat stat; + if (fstat(vfd->fd, &stat) < 0) { + return 0; + } + fileSize = stat.st_size; if (size > fileSize) { size = fileSize; }

@@ -159,6 +165,15 @@

static void _vfdTruncate(struct VFile* vf, size_t size) { struct VFileFD* vfd = (struct VFileFD*) vf; ftruncate(vfd->fd, size); +} + +static ssize_t _vfdSize(struct VFile* vf) { + struct VFileFD* vfd = (struct VFileFD*) vf; + struct stat stat; + if (fstat(vfd->fd, &stat) < 0) { + return -1; + } + return stat.st_size; } struct VDirEntryDE {
M src/util/vfs.hsrc/util/vfs.h

@@ -22,6 +22,7 @@ ssize_t (*write)(struct VFile* vf, const void* buffer, size_t size);

void* (*map)(struct VFile* vf, size_t size, int flags); void (*unmap)(struct VFile* vf, void* memory, size_t size); void (*truncate)(struct VFile* vf, size_t size); + ssize_t (*size)(struct VFile* vf); }; struct VDirEntry {
M src/util/vfs/vfs-zip.csrc/util/vfs/vfs-zip.c

@@ -43,6 +43,7 @@ static ssize_t _vfzWrite(struct VFile* vf, const void* buffer, size_t size);

static void* _vfzMap(struct VFile* vf, size_t size, int flags); static void _vfzUnmap(struct VFile* vf, void* memory, size_t size); static void _vfzTruncate(struct VFile* vf, size_t size); +static ssize_t _vfzSize(struct VFile* vf); static bool _vdzClose(struct VDir* vd); static void _vdzRewind(struct VDir* vd);

@@ -229,6 +230,11 @@ UNUSED(vf);

UNUSED(size); } +ssize_t _vfzSize(struct VFile* vf) { + struct VFileZip* vfz = (struct VFileZip*) vf; + return vfz->fileSize; +} + bool _vdzClose(struct VDir* vd) { struct VDirZip* vdz = (struct VDirZip*) vd; if (zip_close(vdz->z) < 0) {

@@ -295,6 +301,7 @@ vfz->d.write = _vfzWrite;

vfz->d.map = _vfzMap; vfz->d.unmap = _vfzUnmap; vfz->d.truncate = _vfzTruncate; + vfz->d.size = _vfzSize; return &vfz->d; }