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, 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, 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
186bool _vdClose(struct VDir* vd) {
187 struct VDirDE* vdde = (struct VDirDE*) vd;
188 if (closedir(vdde->de) < 0) {
189 return false;
190 }
191 free(vdde->path);
192 free(vdde);
193 return true;
194}
195
196void _vdRewind(struct VDir* vd) {
197 struct VDirDE* vdde = (struct VDirDE*) vd;
198 rewinddir(vdde->de);
199}
200
201struct VDirEntry* _vdListNext(struct VDir* vd) {
202 struct VDirDE* vdde = (struct VDirDE*) vd;
203 vdde->vde.ent = readdir(vdde->de);
204 if (vdde->vde.ent) {
205 return &vdde->vde.d;
206 }
207
208 return 0;
209}
210
211struct VFile* _vdOpenFile(struct VDir* vd, const char* path, int mode) {
212 struct VDirDE* vdde = (struct VDirDE*) vd;
213 if (!path) {
214 return 0;
215 }
216 const char* dir = vdde->path;
217 char* combined = malloc(sizeof(char) * (strlen(path) + strlen(dir) + 2));
218 sprintf(combined, "%s%c%s", dir, PATH_SEP, path);
219
220 struct VFile* file = VFileOpen(combined, mode);
221 free(combined);
222 return file;
223}
224
225const char* _vdeName(struct VDirEntry* vde) {
226 struct VDirEntryDE* vdede = (struct VDirEntryDE*) vde;
227 if (vdede->ent) {
228 return vdede->ent->d_name;
229 }
230 return 0;
231}