all repos — mgba @ a6001496bce5ff9c2ef2f93df2ab4fe396f48266

mGBA Game Boy Advance Emulator

src/util/vfs.c (view raw)

  1/* Copyright (c) 2013-2014 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 "util/string.h"
  9
 10#include <fcntl.h>
 11#include <dirent.h>
 12#include <sys/stat.h>
 13
 14#ifndef _WIN32
 15#include <sys/mman.h>
 16#define PATH_SEP '/'
 17#else
 18#include <io.h>
 19#include <windows.h>
 20#define PATH_SEP '\\'
 21#endif
 22
 23struct VFileFD {
 24	struct VFile d;
 25	int fd;
 26#ifdef _WIN32
 27	HANDLE hMap;
 28#endif
 29};
 30
 31static bool _vfdClose(struct VFile* vf);
 32static off_t _vfdSeek(struct VFile* vf, off_t offset, int whence);
 33static ssize_t _vfdRead(struct VFile* vf, void* buffer, size_t size);
 34static ssize_t _vfdReadline(struct VFile* vf, char* buffer, size_t size);
 35static ssize_t _vfdWrite(struct VFile* vf, const void* buffer, size_t size);
 36static void* _vfdMap(struct VFile* vf, size_t size, int flags);
 37static void _vfdUnmap(struct VFile* vf, void* memory, size_t size);
 38static void _vfdTruncate(struct VFile* vf, size_t size);
 39static ssize_t _vfdSize(struct VFile* vf);
 40
 41static bool _vdClose(struct VDir* vd);
 42static void _vdRewind(struct VDir* vd);
 43static struct VDirEntry* _vdListNext(struct VDir* vd);
 44static struct VFile* _vdOpenFile(struct VDir* vd, const char* path, int mode);
 45
 46static const char* _vdeName(struct VDirEntry* vde);
 47
 48struct VFile* VFileOpen(const char* path, int flags) {
 49	if (!path) {
 50		return 0;
 51	}
 52#ifdef _WIN32
 53	flags |= O_BINARY;
 54#endif
 55	int fd = open(path, flags, 0666);
 56	return VFileFromFD(fd);
 57}
 58
 59struct VFile* VFileFromFD(int fd) {
 60	if (fd < 0) {
 61		return 0;
 62	}
 63
 64	struct VFileFD* vfd = malloc(sizeof(struct VFileFD));
 65	if (!vfd) {
 66		return 0;
 67	}
 68
 69	vfd->fd = fd;
 70	vfd->d.close = _vfdClose;
 71	vfd->d.seek = _vfdSeek;
 72	vfd->d.read = _vfdRead;
 73	vfd->d.readline = _vfdReadline;
 74	vfd->d.write = _vfdWrite;
 75	vfd->d.map = _vfdMap;
 76	vfd->d.unmap = _vfdUnmap;
 77	vfd->d.truncate = _vfdTruncate;
 78	vfd->d.size = _vfdSize;
 79
 80	return &vfd->d;
 81}
 82
 83bool _vfdClose(struct VFile* vf) {
 84	struct VFileFD* vfd = (struct VFileFD*) vf;
 85	if (close(vfd->fd) < 0) {
 86		return false;
 87	}
 88	free(vfd);
 89	return true;
 90}
 91
 92off_t _vfdSeek(struct VFile* vf, off_t offset, int whence) {
 93	struct VFileFD* vfd = (struct VFileFD*) vf;
 94	return lseek(vfd->fd, offset, whence);
 95}
 96
 97ssize_t _vfdRead(struct VFile* vf, void* buffer, size_t size) {
 98	struct VFileFD* vfd = (struct VFileFD*) vf;
 99	return read(vfd->fd, buffer, size);
100}
101
102ssize_t _vfdReadline(struct VFile* vf, char* buffer, size_t size) {
103	struct VFileFD* vfd = (struct VFileFD*) vf;
104	size_t bytesRead = 0;
105	while (bytesRead < size - 1) {
106		size_t newRead = read(vfd->fd, &buffer[bytesRead], 1);
107		bytesRead += newRead;
108		if (!newRead || buffer[bytesRead] == '\n') {
109			break;
110		}
111	}
112	return buffer[bytesRead] = '\0';
113}
114
115ssize_t _vfdWrite(struct VFile* vf, const void* buffer, size_t size) {
116	struct VFileFD* vfd = (struct VFileFD*) vf;
117	return write(vfd->fd, buffer, size);
118}
119
120#ifndef _WIN32
121static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
122	struct VFileFD* vfd = (struct VFileFD*) vf;
123	int mmapFlags = MAP_PRIVATE;
124	if (flags & MAP_WRITE) {
125		mmapFlags = MAP_SHARED;
126	}
127	return mmap(0, size, PROT_READ | PROT_WRITE, mmapFlags, vfd->fd, 0);
128}
129
130static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
131	UNUSED(vf);
132	munmap(memory, size);
133}
134#else
135static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
136	struct VFileFD* vfd = (struct VFileFD*) vf;
137	int createFlags = PAGE_WRITECOPY;
138	int mapFiles = FILE_MAP_COPY;
139	if (flags & MAP_WRITE) {
140		createFlags = PAGE_READWRITE;
141		mapFiles = FILE_MAP_WRITE;
142	}
143	size_t fileSize;
144	struct stat stat;
145	if (fstat(vfd->fd, &stat) < 0) {
146		return 0;
147	}
148	fileSize = stat.st_size;
149	if (size > fileSize) {
150		size = fileSize;
151	}
152	vfd->hMap = CreateFileMapping((HANDLE) _get_osfhandle(vfd->fd), 0, createFlags, 0, size & 0xFFFFFFFF, 0);
153	return MapViewOfFile(vfd->hMap, mapFiles, 0, 0, size);
154}
155
156static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
157	UNUSED(size);
158	struct VFileFD* vfd = (struct VFileFD*) vf;
159	UnmapViewOfFile(memory);
160	CloseHandle(vfd->hMap);
161	vfd->hMap = 0;
162}
163#endif
164
165static void _vfdTruncate(struct VFile* vf, size_t size) {
166	struct VFileFD* vfd = (struct VFileFD*) vf;
167	ftruncate(vfd->fd, size);
168}
169
170static ssize_t _vfdSize(struct VFile* vf) {
171	struct VFileFD* vfd = (struct VFileFD*) vf;
172	struct stat stat;
173	if (fstat(vfd->fd, &stat) < 0) {
174		return -1;
175	}
176	return stat.st_size;
177}
178
179struct VDirEntryDE {
180	struct VDirEntry d;
181	struct dirent* ent;
182};
183
184struct VDirDE {
185	struct VDir d;
186	DIR* de;
187	struct VDirEntryDE vde;
188	char* path;
189};
190
191struct VDir* VDirOpen(const char* path) {
192	DIR* de = opendir(path);
193	if (!de) {
194		return 0;
195	}
196
197	struct VDirDE* vd = malloc(sizeof(struct VDirDE));
198	if (!vd) {
199		return 0;
200	}
201
202	vd->d.close = _vdClose;
203	vd->d.rewind = _vdRewind;
204	vd->d.listNext = _vdListNext;
205	vd->d.openFile = _vdOpenFile;
206	vd->path = strdup(path);
207	vd->de = de;
208
209	vd->vde.d.name = _vdeName;
210
211	return &vd->d;
212}
213
214struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, int mode) {
215	char path[PATH_MAX];
216	path[PATH_MAX - 1] = '\0';
217	struct VFile* vf;
218	if (!dir) {
219		if (!realPath) {
220			return 0;
221		}
222		char* dotPoint = strrchr(realPath, '.');
223		if (dotPoint - realPath + 1 >= PATH_MAX - 1) {
224			return 0;
225		}
226		if (dotPoint > strrchr(realPath, '/')) {
227			int len = dotPoint - realPath;
228			strncpy(path, realPath, len);
229			path[len] = 0;
230			strncat(path + len, suffix, PATH_MAX - len - 1);
231		} else {
232			snprintf(path, PATH_MAX - 1, "%s%s", realPath, suffix);
233		}
234		vf = VFileOpen(path, mode);
235	} else {
236		snprintf(path, PATH_MAX - 1, "%s%s", prefix, suffix);
237		vf = dir->openFile(dir, path, mode);
238	}
239	return vf;
240}
241
242struct VFile* VDirOptionalOpenIncrementFile(struct VDir* dir, const char* realPath, const char* prefix, const char* infix, const char* suffix, int mode) {
243	char path[PATH_MAX];
244	path[PATH_MAX - 1] = '\0';
245	char realPrefix[PATH_MAX];
246	realPrefix[PATH_MAX - 1] = '\0';
247	if (!dir) {
248		if (!realPath) {
249			return 0;
250		}
251		const char* separatorPoint = strrchr(realPath, '/');
252		const char* dotPoint;
253		size_t len;
254		if (!separatorPoint) {
255			strcpy(path, "./");
256			separatorPoint = realPath;
257			dotPoint = strrchr(realPath, '.');
258		} else {
259			path[0] = '\0';
260			dotPoint = strrchr(separatorPoint, '.');
261
262			if (separatorPoint - realPath + 1 >= PATH_MAX - 1) {
263				return 0;
264			}
265
266			len = separatorPoint - realPath;
267			strncat(path, realPath, len);
268			path[len] = '\0';
269			++separatorPoint;
270		}
271
272		if (dotPoint - realPath + 1 >= PATH_MAX - 1) {
273			return 0;
274		}
275
276		if (dotPoint >= separatorPoint) {
277			len = dotPoint - separatorPoint;
278		} else {
279			len = PATH_MAX - 1;
280		}
281
282		strncpy(realPrefix, separatorPoint, len);
283		realPrefix[len] = '\0';
284
285		prefix = realPrefix;
286		dir = VDirOpen(path);
287	}
288	if (!dir) {
289		// This shouldn't be possible
290		return 0;
291	}
292	dir->rewind(dir);
293	struct VDirEntry* dirent;
294	size_t prefixLen = strlen(prefix);
295	size_t infixLen = strlen(infix);
296	unsigned next = 0;
297	while ((dirent = dir->listNext(dir))) {
298		const char* filename = dirent->name(dirent);
299		char* dotPoint = strrchr(filename, '.');
300		size_t len = strlen(filename);
301		if (dotPoint) {
302			len = (dotPoint - filename);
303		}
304		const char* separator = strnrstr(filename, infix, len);
305		if (!separator) {
306			continue;
307		}
308		len = separator - filename;
309		if (len != prefixLen) {
310			continue;
311		}
312		if (strncmp(filename, prefix, prefixLen) == 0) {
313			int nlen;
314			separator += infixLen;
315			snprintf(path, PATH_MAX - 1, "%%u%s%%n", suffix);
316			unsigned increment;
317			if (sscanf(separator, path, &increment, &nlen) < 1) {
318				continue;
319			}
320			len = strlen(separator);
321			if (nlen < (ssize_t) len) {
322				continue;
323			}
324			if (next <= increment) {
325				next = increment + 1;
326			}
327		}
328	}
329	snprintf(path, PATH_MAX - 1, "%s%s%u%s", prefix, infix, next, suffix);
330	path[PATH_MAX - 1] = '\0';
331	return dir->openFile(dir, path, mode);
332}
333
334bool _vdClose(struct VDir* vd) {
335	struct VDirDE* vdde = (struct VDirDE*) vd;
336	if (closedir(vdde->de) < 0) {
337		return false;
338	}
339	free(vdde->path);
340	free(vdde);
341	return true;
342}
343
344void _vdRewind(struct VDir* vd) {
345	struct VDirDE* vdde = (struct VDirDE*) vd;
346	rewinddir(vdde->de);
347}
348
349struct VDirEntry* _vdListNext(struct VDir* vd) {
350	struct VDirDE* vdde = (struct VDirDE*) vd;
351	vdde->vde.ent = readdir(vdde->de);
352	if (vdde->vde.ent) {
353		return &vdde->vde.d;
354	}
355
356	return 0;
357}
358
359struct VFile* _vdOpenFile(struct VDir* vd, const char* path, int mode) {
360	struct VDirDE* vdde = (struct VDirDE*) vd;
361	if (!path) {
362		return 0;
363	}
364	const char* dir = vdde->path;
365	char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + 2));
366	sprintf(combined, "%s%c%s", dir, PATH_SEP, path);
367
368	struct VFile* file = VFileOpen(combined, mode);
369	free(combined);
370	return file;
371}
372
373const char* _vdeName(struct VDirEntry* vde) {
374	struct VDirEntryDE* vdede = (struct VDirEntryDE*) vde;
375	if (vdede->ent) {
376		return vdede->ent->d_name;
377	}
378	return 0;
379}