src/util/vfs.c (view raw)
1#include "util/vfs.h"
2
3#include <fcntl.h>
4#include <dirent.h>
5
6#ifndef _WIN32
7#include <sys/mman.h>
8#define PATH_SEP '/'
9#else
10#include <io.h>
11#include <Windows.h>
12#define PATH_SEP '\\'
13#endif
14
15struct VFileFD {
16 struct VFile d;
17 int fd;
18#ifdef _WIN32
19 HANDLE hMap;
20#endif
21};
22
23static bool _vfdClose(struct VFile* vf);
24static off_t _vfdSeek(struct VFile* vf, off_t offset, int whence);
25static ssize_t _vfdRead(struct VFile* vf, void* buffer, size_t size);
26static ssize_t _vfdReadline(struct VFile* vf, char* buffer, size_t size);
27static ssize_t _vfdWrite(struct VFile* vf, const void* buffer, size_t size);
28static void* _vfdMap(struct VFile* vf, size_t size, int flags);
29static void _vfdUnmap(struct VFile* vf, void* memory, size_t size);
30static void _vfdTruncate(struct VFile* vf, size_t size);
31
32static bool _vdClose(struct VDir* vd);
33static void _vdRewind(struct VDir* vd);
34static struct VDirEntry* _vdListNext(struct VDir* vd);
35static struct VFile* _vdOpenFile(struct VDir* vd, const char* path, int mode);
36
37static const char* _vdeName(struct VDirEntry* vde);
38
39struct VFile* VFileOpen(const char* path, int flags) {
40 int fd = open(path, flags, 0666);
41 return VFileFromFD(fd);
42}
43
44struct VFile* VFileFromFD(int fd) {
45 if (fd < 0) {
46 return 0;
47 }
48
49 struct VFileFD* vfd = malloc(sizeof(struct VFileFD));
50 if (!vfd) {
51 return 0;
52 }
53
54 vfd->fd = fd;
55 vfd->d.close = _vfdClose;
56 vfd->d.seek = _vfdSeek;
57 vfd->d.read = _vfdRead;
58 vfd->d.readline = _vfdReadline;
59 vfd->d.write = _vfdWrite;
60 vfd->d.map = _vfdMap;
61 vfd->d.unmap = _vfdUnmap;
62 vfd->d.truncate = _vfdTruncate;
63
64 return &vfd->d;
65}
66
67bool _vfdClose(struct VFile* vf) {
68 struct VFileFD* vfd = (struct VFileFD*) vf;
69 if (close(vfd->fd) < 0) {
70 return false;
71 }
72 free(vfd);
73 return true;
74}
75
76off_t _vfdSeek(struct VFile* vf, off_t offset, int whence) {
77 struct VFileFD* vfd = (struct VFileFD*) vf;
78 return lseek(vfd->fd, offset, whence);
79}
80
81ssize_t _vfdRead(struct VFile* vf, void* buffer, size_t size) {
82 struct VFileFD* vfd = (struct VFileFD*) vf;
83 return read(vfd->fd, buffer, size);
84}
85
86ssize_t _vfdReadline(struct VFile* vf, char* buffer, size_t size) {
87 struct VFileFD* vfd = (struct VFileFD*) vf;
88 size_t bytesRead = 0;
89 while (bytesRead < size - 1) {
90 size_t newRead = read(vfd->fd, &buffer[bytesRead], 1);
91 bytesRead += newRead;
92 if (!newRead || buffer[bytesRead] == '\n') {
93 break;
94 }
95 }
96 return buffer[bytesRead] = '\0';
97}
98
99ssize_t _vfdWrite(struct VFile* vf, const void* buffer, size_t size) {
100 struct VFileFD* vfd = (struct VFileFD*) vf;
101 return write(vfd->fd, buffer, size);
102}
103
104#ifndef _WIN32
105static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
106 struct VFileFD* vfd = (struct VFileFD*) vf;
107 int mmapFlags = MAP_PRIVATE;
108 if (flags & MAP_WRITE) {
109 mmapFlags = MAP_SHARED;
110 }
111 return mmap(0, size, PROT_READ | PROT_WRITE, mmapFlags, vfd->fd, 0);
112}
113
114static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
115 UNUSED(vf);
116 munmap(memory, size);
117}
118#else
119static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
120 struct VFileFD* vfd = (struct VFileFD*) vf;
121 int createFlags = PAGE_WRITECOPY;
122 int mapFiles = FILE_MAP_COPY;
123 if (flags & MAP_WRITE) {
124 createFlags = PAGE_READWRITE;
125 mapFiles = FILE_MAP_WRITE;
126 }
127 size_t location = lseek(vfd->fd, 0, SEEK_CUR);
128 size_t fileSize = lseek(vfd->fd, 0, SEEK_END);
129 lseek(vfd->fd, location, SEEK_SET);
130 if (size > fileSize) {
131 size = fileSize;
132 }
133 vfd->hMap = CreateFileMapping((HANDLE) _get_osfhandle(vfd->fd), 0, createFlags, 0, size & 0xFFFFFFFF, 0);
134 return MapViewOfFile(hMap, mapFiles, 0, 0, size);
135}
136
137static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
138 UNUSED(size);
139 struct VFileFD* vfd = (struct VFileFD*) vf;
140 UnmapViewOfFile(memory);
141 CloseHandle(vfd->hMap);
142 vfd->hMap = 0;
143}
144#endif
145
146static void _vfdTruncate(struct VFile* vf, size_t size) {
147 struct VFileFD* vfd = (struct VFileFD*) vf;
148 ftruncate(vfd->fd, size);
149}
150
151struct VDirEntryDE {
152 struct VDirEntry d;
153 struct dirent* ent;
154};
155
156struct VDirDE {
157 struct VDir d;
158 DIR* de;
159 struct VDirEntryDE vde;
160 char* path;
161};
162
163struct VDir* VDirOpen(const char* path) {
164 DIR* de = opendir(path);
165 if (!de) {
166 return 0;
167 }
168
169 struct VDirDE* vd = malloc(sizeof(struct VDirDE));
170 if (!vd) {
171 return 0;
172 }
173
174 vd->d.close = _vdClose;
175 vd->d.rewind = _vdRewind;
176 vd->d.listNext = _vdListNext;
177 vd->d.openFile = _vdOpenFile;
178 vd->path = strdup(path);
179 vd->de = de;
180
181 vd->vde.d.name = _vdeName;
182
183 return &vd->d;
184}
185
186struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, int mode) {
187 char path[PATH_MAX];
188 path[PATH_MAX - 1] = '\0';
189 struct VFile* vf;
190 if (!dir) {
191 if (!realPath) {
192 return 0;
193 }
194 char* dotPoint = strrchr(realPath, '.');
195 if (dotPoint - realPath + 1 >= PATH_MAX - 1) {
196 return 0;
197 }
198 if (dotPoint > strrchr(realPath, '/')) {
199 int len = dotPoint - realPath;
200 strncpy(path, realPath, len);
201 path[len] = 0;
202 strncat(path + len, suffix, PATH_MAX - len - 1);
203 } else {
204 snprintf(path, PATH_MAX - 1, "%s%s", realPath, suffix);
205 }
206 vf = VFileOpen(path, mode);
207 } else {
208 snprintf(path, PATH_MAX - 1, "%s%s", prefix, suffix);
209 vf = dir->openFile(dir, path, mode);
210 }
211 return vf;
212}
213
214bool _vdClose(struct VDir* vd) {
215 struct VDirDE* vdde = (struct VDirDE*) vd;
216 if (closedir(vdde->de) < 0) {
217 return false;
218 }
219 free(vdde->path);
220 free(vdde);
221 return true;
222}
223
224void _vdRewind(struct VDir* vd) {
225 struct VDirDE* vdde = (struct VDirDE*) vd;
226 rewinddir(vdde->de);
227}
228
229struct VDirEntry* _vdListNext(struct VDir* vd) {
230 struct VDirDE* vdde = (struct VDirDE*) vd;
231 vdde->vde.ent = readdir(vdde->de);
232 if (vdde->vde.ent) {
233 return &vdde->vde.d;
234 }
235
236 return 0;
237}
238
239struct VFile* _vdOpenFile(struct VDir* vd, const char* path, int mode) {
240 struct VDirDE* vdde = (struct VDirDE*) vd;
241 if (!path) {
242 return 0;
243 }
244 const char* dir = vdde->path;
245 char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + 2));
246 sprintf(combined, "%s%c%s", dir, PATH_SEP, path);
247
248 struct VFile* file = VFileOpen(combined, mode);
249 free(combined);
250 return file;
251}
252
253const char* _vdeName(struct VDirEntry* vde) {
254 struct VDirEntryDE* vdede = (struct VDirEntryDE*) vde;
255 if (vdede->ent) {
256 return vdede->ent->d_name;
257 }
258 return 0;
259}