Skip to content

Commit

Permalink
VFS: Change semantics of VFile.sync on mapped files (fixes #1730)
Browse files Browse the repository at this point in the history
  • Loading branch information
endrift committed Aug 20, 2020
1 parent 2a2f208 commit 8a3a2bf
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Misc:
- Qt: Add transformation matrix info to sprite view
- Qt: Memory viewer now supports editing decimal values directly (closes mgba.io/i/1705)
- Util: Reset vector size on deinit
- VFS: Change semantics of VFile.sync on mapped files (fixes mgba.io/i/1730)

0.8.3: (2020-08-03)
Emulation fixes:
Expand Down
2 changes: 1 addition & 1 deletion include/mgba-util/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct VFile {
void (*unmap)(struct VFile* vf, void* memory, size_t size);
void (*truncate)(struct VFile* vf, size_t size);
ssize_t (*size)(struct VFile* vf);
bool (*sync)(struct VFile* vf, const void* buffer, size_t size);
bool (*sync)(struct VFile* vf, void* buffer, size_t size);
};

struct VDirEntry {
Expand Down
8 changes: 3 additions & 5 deletions src/platform/3ds/3ds-vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static void* _vf3dMap(struct VFile* vf, size_t size, int flags);
static void _vf3dUnmap(struct VFile* vf, void* memory, size_t size);
static void _vf3dTruncate(struct VFile* vf, size_t size);
static ssize_t _vf3dSize(struct VFile* vf);
static bool _vf3dSync(struct VFile* vf, const void* buffer, size_t size);
static bool _vf3dSync(struct VFile* vf, void* buffer, size_t size);

static bool _vd3dClose(struct VDir* vd);
static void _vd3dRewind(struct VDir* vd);
Expand Down Expand Up @@ -160,14 +160,12 @@ ssize_t _vf3dSize(struct VFile* vf) {
return size;
}

static bool _vf3dSync(struct VFile* vf, const void* buffer, size_t size) {
static bool _vf3dSync(struct VFile* vf, void* buffer, size_t size) {
struct VFile3DS* vf3d = (struct VFile3DS*) vf;
if (buffer) {
u32 sizeWritten;
Result res = FSFILE_Write(vf3d->handle, &sizeWritten, 0, buffer, size, FS_WRITE_FLUSH);
if (res) {
return false;
}
return R_SUCCEEDED(res);
}
FSFILE_Flush(vf3d->handle);
return true;
Expand Down
9 changes: 5 additions & 4 deletions src/platform/psp2/sce-vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void* _vfsceMap(struct VFile* vf, size_t size, int flags);
static void _vfsceUnmap(struct VFile* vf, void* memory, size_t size);
static void _vfsceTruncate(struct VFile* vf, size_t size);
static ssize_t _vfsceSize(struct VFile* vf);
static bool _vfsceSync(struct VFile* vf, const void* memory, size_t size);
static bool _vfsceSync(struct VFile* vf, void* memory, size_t size);

static bool _vdsceClose(struct VDir* vd);
static void _vdsceRewind(struct VDir* vd);
Expand Down Expand Up @@ -146,13 +146,14 @@ ssize_t _vfsceSize(struct VFile* vf) {
return end;
}

bool _vfsceSync(struct VFile* vf, const void* buffer, size_t size) {
bool _vfsceSync(struct VFile* vf, void* buffer, size_t size) {
struct VFileSce* vfsce = (struct VFileSce*) vf;
if (buffer && size) {
SceOff cur = sceIoLseek(vfsce->fd, 0, SEEK_CUR);
sceIoLseek(vfsce->fd, 0, SEEK_SET);
sceIoWrite(vfsce->fd, buffer, size);
int res = sceIoWrite(vfsce->fd, buffer, size);
sceIoLseek(vfsce->fd, cur, SEEK_SET);
return res == size;
}
return sceIoSyncByFd(vfsce->fd) >= 0;
}
Expand Down Expand Up @@ -362,4 +363,4 @@ bool VDirCreate(const char* path) {
// TODO: Verify vitasdk explanation of return values
sceIoMkdir(path, 0777);
return true;
}
}
3 changes: 2 additions & 1 deletion src/platform/python/mgba/vfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ def _vfpSync(vf, buffer, size):
if buffer and size:
pos = f.tell()
f.seek(0, os.SEEK_SET)
_vfpWrite(vf, buffer, size)
res = _vfpWrite(vf, buffer, size)
f.seek(pos, os.SEEK_SET)
return res == size
f.flush()
os.fsync()
return True
Expand Down
2 changes: 1 addition & 1 deletion src/platform/python/vfs-py.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ PYEXPORT void* _vfpMap(struct VFile* vf, size_t size, int flags);
PYEXPORT void _vfpUnmap(struct VFile* vf, void* memory, size_t size);
PYEXPORT void _vfpTruncate(struct VFile* vf, size_t size);
PYEXPORT ssize_t _vfpSize(struct VFile* vf);
PYEXPORT bool _vfpSync(struct VFile* vf, const void* buffer, size_t size);
PYEXPORT bool _vfpSync(struct VFile* vf, void* buffer, size_t size);
8 changes: 4 additions & 4 deletions src/util/vfs/vfs-fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ 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 _vfdSync(struct VFile* vf, const void* buffer, size_t size);
static bool _vfdSync(struct VFile* vf, void* buffer, size_t size);

struct VFile* VFileOpenFD(const char* path, int flags) {
if (!path) {
Expand Down Expand Up @@ -195,7 +195,7 @@ static ssize_t _vfdSize(struct VFile* vf) {
return stat.st_size;
}

static bool _vfdSync(struct VFile* vf, const void* buffer, size_t size) {
static bool _vfdSync(struct VFile* vf, void* buffer, size_t size) {
UNUSED(buffer);
UNUSED(size);
struct VFileFD* vfd = (struct VFileFD*) vf;
Expand All @@ -206,7 +206,7 @@ static bool _vfdSync(struct VFile* vf, const void* buffer, size_t size) {
futimes(vfd->fd, NULL);
#endif
if (buffer && size) {
return msync(buffer, size, MS_SYNC) == 0;
return msync(buffer, size, MS_ASYNC) == 0;
}
return fsync(vfd->fd) == 0;
#else
Expand All @@ -217,7 +217,7 @@ static bool _vfdSync(struct VFile* vf, const void* buffer, size_t size) {
SystemTimeToFileTime(&st, &ft);
SetFileTime(h, NULL, &ft, &ft);
if (buffer && size) {
FlushViewOfFile(buffer, size);
return FlushViewOfFile(buffer, size);
}
return FlushFileBuffers(h);
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/util/vfs/vfs-fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void* _vffMap(struct VFile* vf, size_t size, int flags);
static void _vffUnmap(struct VFile* vf, void* memory, size_t size);
static void _vffTruncate(struct VFile* vf, size_t size);
static ssize_t _vffSize(struct VFile* vf);
static bool _vffSync(struct VFile* vf, const void* buffer, size_t size);
static bool _vffSync(struct VFile* vf, void* buffer, size_t size);

struct VFile* VFileFIFO(struct CircleBuffer* backing) {
if (!backing) {
Expand Down Expand Up @@ -94,7 +94,7 @@ static ssize_t _vffSize(struct VFile* vf) {
return CircleBufferSize(vff->backing);
}

static bool _vffSync(struct VFile* vf, const void* buffer, size_t size) {
static bool _vffSync(struct VFile* vf, void* buffer, size_t size) {
UNUSED(vf);
UNUSED(buffer);
UNUSED(size);
Expand Down
7 changes: 4 additions & 3 deletions src/util/vfs/vfs-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static void* _vffMap(struct VFile* vf, size_t size, int flags);
static void _vffUnmap(struct VFile* vf, void* memory, size_t size);
static void _vffTruncate(struct VFile* vf, size_t size);
static ssize_t _vffSize(struct VFile* vf);
static bool _vffSync(struct VFile* vf, const void* buffer, size_t size);
static bool _vffSync(struct VFile* vf, void* buffer, size_t size);

struct VFile* VFileFOpen(const char* path, const char* mode) {
if (!path && !mode) {
Expand Down Expand Up @@ -144,13 +144,14 @@ static ssize_t _vffSize(struct VFile* vf) {
return size;
}

static bool _vffSync(struct VFile* vf, const void* buffer, size_t size) {
static bool _vffSync(struct VFile* vf, void* buffer, size_t size) {
struct VFileFILE* vff = (struct VFileFILE*) vf;
if (buffer && size) {
long pos = ftell(vff->file);
fseek(vff->file, 0, SEEK_SET);
fwrite(buffer, size, 1, vff->file);
size_t res = fwrite(buffer, size, 1, vff->file);
fseek(vff->file, pos, SEEK_SET);
return res == 1;
}
return fflush(vff->file) == 0;
}
4 changes: 2 additions & 2 deletions src/util/vfs/vfs-lzma.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static void* _vf7zMap(struct VFile* vf, size_t size, int flags);
static void _vf7zUnmap(struct VFile* vf, void* memory, size_t size);
static void _vf7zTruncate(struct VFile* vf, size_t size);
static ssize_t _vf7zSize(struct VFile* vf);
static bool _vf7zSync(struct VFile* vf, const void* buffer, size_t size);
static bool _vf7zSync(struct VFile* vf, void* buffer, size_t size);

static bool _vd7zClose(struct VDir* vd);
static void _vd7zRewind(struct VDir* vd);
Expand Down Expand Up @@ -327,7 +327,7 @@ bool _vd7zDeleteFile(struct VDir* vd, const char* path) {
return false;
}

bool _vf7zSync(struct VFile* vf, const void* memory, size_t size) {
bool _vf7zSync(struct VFile* vf, void* memory, size_t size) {
UNUSED(vf);
UNUSED(memory);
UNUSED(size);
Expand Down
4 changes: 2 additions & 2 deletions src/util/vfs/vfs-mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static void _vfmUnmap(struct VFile* vf, void* memory, size_t size);
static void _vfmTruncate(struct VFile* vf, size_t size);
static void _vfmTruncateNoop(struct VFile* vf, size_t size);
static ssize_t _vfmSize(struct VFile* vf);
static bool _vfmSync(struct VFile* vf, const void* buffer, size_t size);
static bool _vfmSync(struct VFile* vf, void* buffer, size_t size);

struct VFile* VFileFromMemory(void* mem, size_t size) {
if (!mem || !size) {
Expand Down Expand Up @@ -297,7 +297,7 @@ ssize_t _vfmSize(struct VFile* vf) {
return vfm->size;
}

bool _vfmSync(struct VFile* vf, const void* buffer, size_t size) {
bool _vfmSync(struct VFile* vf, void* buffer, size_t size) {
UNUSED(vf);
UNUSED(buffer);
UNUSED(size);
Expand Down
6 changes: 3 additions & 3 deletions src/util/vfs/vfs-zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ 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 _vfzSync(struct VFile* vf, const void* buffer, size_t size);
static bool _vfzSync(struct VFile* vf, void* buffer, size_t size);

static bool _vdzClose(struct VDir* vd);
static void _vdzRewind(struct VDir* vd);
Expand Down Expand Up @@ -426,7 +426,7 @@ bool _vdzDeleteFile(struct VDir* vd, const char* path) {
return false;
}

bool _vfzSync(struct VFile* vf, const void* memory, size_t size) {
bool _vfzSync(struct VFile* vf, void* memory, size_t size) {
UNUSED(vf);
UNUSED(memory);
UNUSED(size);
Expand Down Expand Up @@ -654,7 +654,7 @@ bool _vdzDeleteFile(struct VDir* vd, const char* path) {
return false;
}

bool _vfzSync(struct VFile* vf, const void* memory, size_t size) {
bool _vfzSync(struct VFile* vf, void* memory, size_t size) {
UNUSED(vf);
UNUSED(memory);
UNUSED(size);
Expand Down

0 comments on commit 8a3a2bf

Please sign in to comment.