all repos — mgba @ eb6cedde2e11184c560dc69717891d0c00c96452

mGBA Game Boy Advance Emulator

src/util/vfs/vfs-fd.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "util/vfs.h"
  7
  8#include <fcntl.h>
  9#include <sys/stat.h>
 10#ifndef _WIN32
 11#include <sys/mman.h>
 12#include <sys/time.h>
 13#else
 14#include <windows.h>
 15#endif
 16
 17struct VFileFD {
 18	struct VFile d;
 19	int fd;
 20#ifdef _WIN32
 21	HANDLE hMap;
 22#endif
 23};
 24
 25static bool _vfdClose(struct VFile* vf);
 26static off_t _vfdSeek(struct VFile* vf, off_t offset, int whence);
 27static ssize_t _vfdRead(struct VFile* vf, void* buffer, size_t size);
 28static ssize_t _vfdWrite(struct VFile* vf, const void* buffer, size_t size);
 29static void* _vfdMap(struct VFile* vf, size_t size, int flags);
 30static void _vfdUnmap(struct VFile* vf, void* memory, size_t size);
 31static void _vfdTruncate(struct VFile* vf, size_t size);
 32static ssize_t _vfdSize(struct VFile* vf);
 33static bool _vfdSync(struct VFile* vf, const void* buffer, size_t size);
 34
 35struct VFile* VFileOpenFD(const char* path, int flags) {
 36	if (!path) {
 37		return 0;
 38	}
 39#ifdef _WIN32
 40	flags |= O_BINARY;
 41	wchar_t wpath[PATH_MAX];
 42	MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, sizeof(wpath) / sizeof(*wpath));
 43	int fd = _wopen(wpath, flags, 0666);
 44#else
 45	int fd = open(path, flags, 0666);
 46#endif
 47	return VFileFromFD(fd);
 48}
 49
 50struct VFile* VFileFromFD(int fd) {
 51	if (fd < 0) {
 52		return 0;
 53	}
 54
 55	struct VFileFD* vfd = malloc(sizeof(struct VFileFD));
 56	if (!vfd) {
 57		return 0;
 58	}
 59
 60	vfd->fd = fd;
 61	vfd->d.close = _vfdClose;
 62	vfd->d.seek = _vfdSeek;
 63	vfd->d.read = _vfdRead;
 64	vfd->d.readline = VFileReadline;
 65	vfd->d.write = _vfdWrite;
 66	vfd->d.map = _vfdMap;
 67	vfd->d.unmap = _vfdUnmap;
 68	vfd->d.truncate = _vfdTruncate;
 69	vfd->d.size = _vfdSize;
 70	vfd->d.sync = _vfdSync;
 71
 72	return &vfd->d;
 73}
 74
 75bool _vfdClose(struct VFile* vf) {
 76	struct VFileFD* vfd = (struct VFileFD*) vf;
 77	if (close(vfd->fd) < 0) {
 78		return false;
 79	}
 80	free(vfd);
 81	return true;
 82}
 83
 84off_t _vfdSeek(struct VFile* vf, off_t offset, int whence) {
 85	struct VFileFD* vfd = (struct VFileFD*) vf;
 86	return lseek(vfd->fd, offset, whence);
 87}
 88
 89ssize_t _vfdRead(struct VFile* vf, void* buffer, size_t size) {
 90	struct VFileFD* vfd = (struct VFileFD*) vf;
 91	return read(vfd->fd, buffer, size);
 92}
 93
 94ssize_t _vfdWrite(struct VFile* vf, const void* buffer, size_t size) {
 95	struct VFileFD* vfd = (struct VFileFD*) vf;
 96	return write(vfd->fd, buffer, size);
 97}
 98
 99#ifndef _WIN32
100static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
101	struct VFileFD* vfd = (struct VFileFD*) vf;
102	int mmapFlags = MAP_PRIVATE;
103	if (flags & MAP_WRITE) {
104		mmapFlags = MAP_SHARED;
105	}
106	return mmap(0, size, PROT_READ | PROT_WRITE, mmapFlags, vfd->fd, 0);
107}
108
109static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
110	UNUSED(vf);
111	munmap(memory, size);
112}
113#else
114static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
115	struct VFileFD* vfd = (struct VFileFD*) vf;
116	int createFlags = PAGE_WRITECOPY;
117	int mapFiles = FILE_MAP_COPY;
118	if (flags & MAP_WRITE) {
119		createFlags = PAGE_READWRITE;
120		mapFiles = FILE_MAP_WRITE;
121	}
122	size_t fileSize;
123	struct stat stat;
124	if (fstat(vfd->fd, &stat) < 0) {
125		return 0;
126	}
127	fileSize = stat.st_size;
128	if (size > fileSize) {
129		size = fileSize;
130	}
131	vfd->hMap = CreateFileMapping((HANDLE) _get_osfhandle(vfd->fd), 0, createFlags, 0, size & 0xFFFFFFFF, 0);
132	return MapViewOfFile(vfd->hMap, mapFiles, 0, 0, size);
133}
134
135static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
136	UNUSED(size);
137	struct VFileFD* vfd = (struct VFileFD*) vf;
138	UnmapViewOfFile(memory);
139	CloseHandle(vfd->hMap);
140	vfd->hMap = 0;
141}
142#endif
143
144static void _vfdTruncate(struct VFile* vf, size_t size) {
145	struct VFileFD* vfd = (struct VFileFD*) vf;
146	ftruncate(vfd->fd, size);
147}
148
149static ssize_t _vfdSize(struct VFile* vf) {
150	struct VFileFD* vfd = (struct VFileFD*) vf;
151	struct stat stat;
152	if (fstat(vfd->fd, &stat) < 0) {
153		return -1;
154	}
155	return stat.st_size;
156}
157
158static bool _vfdSync(struct VFile* vf, const void* buffer, size_t size) {
159	UNUSED(buffer);
160	UNUSED(size);
161	struct VFileFD* vfd = (struct VFileFD*) vf;
162#ifndef _WIN32
163	futimes(vfd->fd, NULL);
164	return fsync(vfd->fd) == 0;
165#else
166	HANDLE h = (HANDLE) _get_osfhandle(vfd->fd);
167	FILETIME ft;
168	SYSTEMTIME st;
169	GetSystemTime(&st);
170	SystemTimeToFileTime(&st, &ft);
171	SetFileTime(h, NULL, &ft, &ft);
172	return FlushFileBuffers(h);
173#endif
174}