src/util/vfs.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 "vfs.h"
7
8#include "util/string.h"
9
10#ifdef PSP2
11#include "platform/psp2/sce-vfs.h"
12#endif
13#ifdef _3DS
14#include "platform/3ds/3ds-vfs.h"
15#endif
16
17struct VFile* VFileOpen(const char* path, int flags) {
18#ifdef USE_VFS_FILE
19 const char* chflags;
20 switch (flags & O_ACCMODE) {
21 case O_WRONLY:
22 if (flags & O_APPEND) {
23 chflags = "ab";
24 } else {
25 chflags = "wb";
26 }
27 break;
28 case O_RDWR:
29 if (flags & O_APPEND) {
30 chflags = "a+b";
31 } else if (flags & O_TRUNC) {
32 chflags = "w+b";
33 } else {
34 chflags = "r+b";
35 }
36 break;
37 case O_RDONLY:
38 chflags = "rb";
39 break;
40 }
41 return VFileFOpen(path, chflags);
42#elif defined(PSP2)
43 int sceFlags = PSP2_O_RDONLY;
44 switch (flags & O_ACCMODE) {
45 case O_WRONLY:
46 sceFlags = PSP2_O_WRONLY;
47 break;
48 case O_RDWR:
49 sceFlags = PSP2_O_RDWR;
50 break;
51 case O_RDONLY:
52 sceFlags = PSP2_O_RDONLY;
53 break;
54 }
55
56 if (flags & O_APPEND) {
57 sceFlags |= PSP2_O_APPEND;
58 }
59 if (flags & O_TRUNC) {
60 sceFlags |= PSP2_O_TRUNC;
61 }
62 if (flags & O_CREAT) {
63 sceFlags |= PSP2_O_CREAT;
64 }
65 return VFileOpenSce(path, sceFlags, 0666);
66#elif defined(USE_VFS_3DS)
67 int ctrFlags = FS_OPEN_READ;
68 switch (flags & O_ACCMODE) {
69 case O_WRONLY:
70 ctrFlags = FS_OPEN_WRITE;
71 break;
72 case O_RDWR:
73 ctrFlags = FS_OPEN_READ | FS_OPEN_WRITE;
74 break;
75 case O_RDONLY:
76 ctrFlags = FS_OPEN_READ;
77 break;
78 }
79
80 if (flags & O_CREAT) {
81 ctrFlags |= FS_OPEN_CREATE;
82 }
83 struct VFile* vf = VFileOpen3DS(&sdmcArchive, path, ctrFlags);
84 if (!vf) {
85 return 0;
86 }
87 if (flags & O_TRUNC) {
88 vf->truncate(vf, 0);
89 }
90 if (flags & O_APPEND) {
91 vf->seek(vf, vf->size(vf), SEEK_SET);
92 }
93 return vf;
94#else
95 return VFileOpenFD(path, flags);
96#endif
97}
98
99struct VDir* VDirOpenArchive(const char* path) {
100 struct VDir* dir = 0;
101#if defined(USE_LIBZIP) || defined(USE_ZLIB)
102 if (!dir) {
103 dir = VDirOpenZip(path, 0);
104 }
105#endif
106#if USE_LZMA
107 if (!dir) {
108 dir = VDirOpen7z(path, 0);
109 }
110#endif
111 return dir;
112}
113
114ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size) {
115 size_t bytesRead = 0;
116 while (bytesRead < size - 1) {
117 ssize_t newRead = vf->read(vf, &buffer[bytesRead], 1);
118 if (newRead <= 0) {
119 break;
120 }
121 bytesRead += newRead;
122 if (buffer[bytesRead - newRead] == '\n') {
123 break;
124 }
125 }
126 buffer[bytesRead] = '\0';
127 return bytesRead;
128}
129
130ssize_t VFileWrite32LE(struct VFile* vf, int32_t word) {
131 uint32_t leword;
132 STORE_32LE(word, 0, &leword);
133 return vf->write(vf, &leword, 4);
134}
135
136ssize_t VFileWrite16LE(struct VFile* vf, int16_t hword) {
137 uint16_t lehword;
138 STORE_16LE(hword, 0, &lehword);
139 return vf->write(vf, &lehword, 2);
140}
141
142ssize_t VFileRead32LE(struct VFile* vf, void* word) {
143 uint32_t leword;
144 ssize_t r = vf->read(vf, &leword, 4);
145 if (r == 4) {
146 STORE_32LE(leword, 0, word);
147 }
148 return r;
149}
150
151ssize_t VFileRead16LE(struct VFile* vf, void* hword) {
152 uint16_t lehword;
153 ssize_t r = vf->read(vf, &lehword, 2);
154 if (r == 2) {
155 STORE_16LE(lehword, 0, hword);
156 }
157 return r;
158}
159
160void separatePath(const char* path, char* dirname, char* basename, char* extension) {
161 if (!path) {
162 return;
163 }
164 char* dotPoint = strrchr(path, '.');
165 char* separatorPoint = strnrstr(path, PATH_SEP, strlen(path));
166 if (separatorPoint) {
167 if (dirname) {
168 ptrdiff_t len = separatorPoint - path;
169 if (PATH_MAX <= len) {
170 len = PATH_MAX - 1;
171 }
172 strncpy(dirname, path, len);
173 dirname[len] = '\0';
174 }
175 path = separatorPoint + 1;
176 } else if (dirname) {
177 strcpy(dirname, ".");
178 }
179 if (basename) {
180 size_t len;
181 if (dotPoint) {
182 len = dotPoint - path;
183 } else {
184 len = strlen(path);
185 }
186 if (PATH_MAX <= len) {
187 len = PATH_MAX - 1;
188 }
189 strncpy(basename, path, len);
190 basename[len] = '\0';
191 }
192 if (extension) {
193 if (dotPoint) {
194 ++dotPoint;
195 size_t len = strlen(dotPoint);
196 if (PATH_MAX <= len) {
197 len = PATH_MAX - 1;
198 }
199 strncpy(extension, dotPoint, len);
200 extension[len] = '\0';
201 } else {
202 extension[0] = '\0';
203 }
204 }
205}
206
207struct VFile* VDirFindFirst(struct VDir* dir, bool (*filter)(struct VFile*)) {
208 dir->rewind(dir);
209 struct VDirEntry* dirent = dir->listNext(dir);
210 while (dirent) {
211 struct VFile* vf = dir->openFile(dir, dirent->name(dirent), O_RDONLY);
212 if (!vf) {
213 dirent = dir->listNext(dir);
214 continue;
215 }
216 if (filter(vf)) {
217 return vf;
218 }
219 vf->close(vf);
220 dirent = dir->listNext(dir);
221 }
222 return 0;
223}
224
225struct VFile* VDirFindNextAvailable(struct VDir* dir, const char* basename, const char* infix, const char* suffix, int mode) {
226 if (!dir) {
227 return 0;
228 }
229 dir->rewind(dir);
230 struct VDirEntry* dirent;
231 size_t prefixLen = strlen(basename);
232 size_t infixLen = strlen(infix);
233 char path[PATH_MAX];
234 unsigned next = 0;
235 while ((dirent = dir->listNext(dir))) {
236 const char* filename = dirent->name(dirent);
237 const char* dotPoint = strrchr(filename, '.');
238 size_t len = strlen(filename);
239 if (dotPoint) {
240 len = (dotPoint - filename);
241 }
242 const char* separator = strnrstr(filename, infix, len);
243 if (!separator) {
244 continue;
245 }
246 len = separator - filename;
247 if (len != prefixLen) {
248 continue;
249 }
250 if (strncmp(filename, basename, prefixLen) == 0) {
251 int nlen;
252 separator += infixLen;
253 snprintf(path, PATH_MAX - 1, "%%u%s%%n", suffix);
254 unsigned increment;
255 if (sscanf(separator, path, &increment, &nlen) < 1) {
256 continue;
257 }
258 len = strlen(separator);
259 if (nlen < (ssize_t) len) {
260 continue;
261 }
262 if (next <= increment) {
263 next = increment + 1;
264 }
265 }
266 }
267 snprintf(path, PATH_MAX - 1, "%s%s%u%s", basename, infix, next, suffix);
268 path[PATH_MAX - 1] = '\0';
269 return dir->openFile(dir, path, mode);
270}