Debian Codegraph @

addons/physfs/a5_physfs_dir.c

raw
1 /*
2 * Filesystem driver for the PhysFS addon.
3 *
4 * By Elias Pschernig.
5 */
6
7 #include <physfs.h>
8 #include "allegro5/allegro.h"
9 #include "allegro5/allegro_physfs.h"
10
11 #include "allegro_physfs_intern.h"
12
13 #if defined(ENOTSUP)
14 #define NOTSUP ENOTSUP
15 #elif defined(ENOSYS)
16 #define NOTSUP ENOSYS
17 #else
18 #define NOTSUP EINVAL
19 #endif
20
21
22 typedef struct ALLEGRO_FS_ENTRY_PHYSFS ALLEGRO_FS_ENTRY_PHYSFS;
23
24 struct ALLEGRO_FS_ENTRY_PHYSFS
25 {
26 ALLEGRO_FS_ENTRY fs_entry; /* must be first */
27 ALLEGRO_PATH *path;
28 const char *path_cstr;
29
30 /* For directory listing. */
31 char **file_list;
32 char **file_list_pos;
33 bool is_dir_open;
34 };
35
36 /* forward declaration */
37 static const ALLEGRO_FS_INTERFACE fs_phys_vtable;
38
39 /* current working directory */
40 /* We cannot use ALLEGRO_USTR because we have nowhere to free it. */
41 static char fs_phys_cwd[1024] = "/";
42
43 static bool path_is_absolute(const char *path)
44 {
45 return (path && path[0] == '/');
46 }
47
48 static void ensure_trailing_slash(ALLEGRO_USTR *us)
49 {
50 int pos = al_ustr_size(us);
51 if (al_ustr_prev_get(us, &pos) != '/') {
52 al_ustr_append_chr(us, '/');
53 }
54 }
55
56 ALLEGRO_USTR *_al_physfs_process_path(const char *path)
57 {
58 ALLEGRO_USTR *us;
59 ALLEGRO_PATH *p = al_create_path(path);
60 ALLEGRO_PATH *cwd = al_create_path(fs_phys_cwd);
61 al_rebase_path(cwd, p);
62 al_destroy_path(cwd);
63 /* PHYSFS always uses / separator. */
64 us = al_ustr_dup(al_path_ustr(p, '/'));
65 al_destroy_path(p);
66 return us;
67 }
68
69 static ALLEGRO_FS_ENTRY *fs_phys_create_entry(const char *path)
70 {
71 ALLEGRO_FS_ENTRY_PHYSFS *e;
72 ALLEGRO_USTR *us;
73
74 e = al_calloc(1, sizeof *e);
75 if (!e)
76 return NULL;
77 e->fs_entry.vtable = &fs_phys_vtable;
78
79 us = _al_physfs_process_path(path);
80 e->path = al_create_path(al_cstr(us));
81 al_ustr_free(us);
82 if (!e->path) {
83 al_free(e);
84 return NULL;
85 }
86 e->path_cstr = al_path_cstr(e->path, '/');
87 return &e->fs_entry;
88 }
89
90 static char *fs_phys_get_current_directory(void)
91 {
92 size_t size = strlen(fs_phys_cwd) + 1;
93 char *s = al_malloc(size);
94 if (s) {
95 memcpy(s, fs_phys_cwd, size);
96 }
97 return s;
98 }
99
100 static bool fs_phys_change_directory(const char *path)
101 {
102 ALLEGRO_USTR *us;
103 bool ret;
104 PHYSFS_Stat stat;
105
106 /* '/' root is guaranteed to exist but PHYSFS won't find it. */
107 if (path[0] == '/' && path[1] == '\0') {
108 fs_phys_cwd[0] = '/';
109 fs_phys_cwd[1] = '\0';
110 return true;
111 }
112
113 /* Figure out which directory we are trying to change to. */
114 if (path_is_absolute(path))
115 us = al_ustr_new(path);
116 else
117 us = _al_physfs_process_path(path);
118
119 ret = false;
120
121 if (!PHYSFS_stat(al_cstr(us), &stat))
122 return false;
123
124 if (stat.filetype == PHYSFS_FILETYPE_DIRECTORY ) {
125 ensure_trailing_slash(us);
126
127 /* Copy to static buffer. */
128 if ((size_t) al_ustr_size(us) < sizeof(fs_phys_cwd)) {
129 al_ustr_to_buffer(us, fs_phys_cwd, sizeof(fs_phys_cwd));
130 ret = true;
131 }
132 }
133
134 al_ustr_free(us);
135
136 return ret;
137 }
138
139 static bool fs_phys_filename_exists(const char *path)
140 {
141 ALLEGRO_USTR *us;
142 bool ret;
143
144 us = _al_physfs_process_path(path);
145 ret = PHYSFS_exists(al_cstr(us)) ? true : false;
146 al_ustr_free(us);
147 return ret;
148 }
149
150 static bool fs_phys_remove_filename(const char *path)
151 {
152 ALLEGRO_USTR *us;
153 bool ret;
154
155 us = _al_physfs_process_path(path);
156 ret = PHYSFS_delete(al_cstr(us)) ? true : false;
157 al_ustr_free(us);
158 return ret;
159 }
160
161 static bool fs_phys_make_directory(const char *path)
162 {
163 ALLEGRO_USTR *us;
164 bool ret;
165
166 us = _al_physfs_process_path(path);
167 ret = PHYSFS_mkdir(al_cstr(us)) ? true : false;
168 al_ustr_free(us);
169 return ret;
170 }
171
172 static const char *fs_phys_entry_name(ALLEGRO_FS_ENTRY *fse)
173 {
174 ALLEGRO_FS_ENTRY_PHYSFS *e = (ALLEGRO_FS_ENTRY_PHYSFS *)fse;
175 return al_path_cstr(e->path, '/');
176 }
177
178 static bool fs_phys_update_entry(ALLEGRO_FS_ENTRY *fse)
179 {
180 (void)fse;
181 return true;
182 }
183
184 static off_t fs_phys_entry_size(ALLEGRO_FS_ENTRY *fse)
185 {
186 ALLEGRO_FS_ENTRY_PHYSFS *e = (ALLEGRO_FS_ENTRY_PHYSFS *)fse;
187 PHYSFS_file *f = PHYSFS_openRead(e->path_cstr);
188 if (f) {
189 off_t s = PHYSFS_fileLength(f);
190 PHYSFS_close(f);
191 return s;
192 }
193 return 0;
194 }
195
196 static uint32_t fs_phys_entry_mode(ALLEGRO_FS_ENTRY *fse)
197 {
198 ALLEGRO_FS_ENTRY_PHYSFS *e = (ALLEGRO_FS_ENTRY_PHYSFS *)fse;
199 uint32_t mode = ALLEGRO_FILEMODE_READ;
200 PHYSFS_Stat stat;
201 if (!PHYSFS_stat(e->path_cstr, &stat))
202 return mode;
203
204 if (stat.filetype == PHYSFS_FILETYPE_DIRECTORY )
205 mode |= ALLEGRO_FILEMODE_ISDIR | ALLEGRO_FILEMODE_EXECUTE;
206 else
207 mode |= ALLEGRO_FILEMODE_ISFILE;
208 return mode;
209 }
210
211 static time_t fs_phys_entry_mtime(ALLEGRO_FS_ENTRY *fse)
212 {
213 ALLEGRO_FS_ENTRY_PHYSFS *e = (ALLEGRO_FS_ENTRY_PHYSFS *)fse;
214 PHYSFS_Stat stat;
215 if (!PHYSFS_stat(e->path_cstr, &stat))
216 return -1;
217 return stat.modtime;
218 }
219
220 static bool fs_phys_entry_exists(ALLEGRO_FS_ENTRY *fse)
221 {
222 ALLEGRO_FS_ENTRY_PHYSFS *e = (ALLEGRO_FS_ENTRY_PHYSFS *)fse;
223 return PHYSFS_exists(e->path_cstr) != 0;
224 }
225
226 static bool fs_phys_remove_entry(ALLEGRO_FS_ENTRY *fse)
227 {
228 ALLEGRO_FS_ENTRY_PHYSFS *e = (ALLEGRO_FS_ENTRY_PHYSFS *)fse;
229 return PHYSFS_delete(e->path_cstr) != 0;
230 }
231
232 static bool fs_phys_open_directory(ALLEGRO_FS_ENTRY *fse)
233 {
234 ALLEGRO_FS_ENTRY_PHYSFS *e = (ALLEGRO_FS_ENTRY_PHYSFS *)fse;
235
236 e->file_list = PHYSFS_enumerateFiles(e->path_cstr);
237 e->file_list_pos = e->file_list;
238
239 e->is_dir_open = true;
240 return true;
241 }
242
243 static ALLEGRO_FS_ENTRY *fs_phys_read_directory(ALLEGRO_FS_ENTRY *fse)
244 {
245 ALLEGRO_FS_ENTRY_PHYSFS *e = (ALLEGRO_FS_ENTRY_PHYSFS *)fse;
246 ALLEGRO_FS_ENTRY *next;
247 ALLEGRO_USTR *tmp;
248
249 if (!e->file_list_pos)
250 return NULL;
251 if (!*e->file_list_pos)
252 return NULL;
253
254 tmp = al_ustr_new(e->path_cstr);
255 ensure_trailing_slash(tmp);
256 al_ustr_append_cstr(tmp, *e->file_list_pos);
257 next = fs_phys_create_entry(al_cstr(tmp));
258 al_ustr_free(tmp);
259
260 e->file_list_pos++;
261
262 return next;
263 }
264
265 static bool fs_phys_close_directory(ALLEGRO_FS_ENTRY *fse)
266 {
267 ALLEGRO_FS_ENTRY_PHYSFS *e = (ALLEGRO_FS_ENTRY_PHYSFS *)fse;
268 PHYSFS_freeList(e->file_list);
269 e->file_list = NULL;
270 e->is_dir_open = false;
271 return true;
272 }
273
274 static void fs_phys_destroy_entry(ALLEGRO_FS_ENTRY *fse)
275 {
276 ALLEGRO_FS_ENTRY_PHYSFS *e = (ALLEGRO_FS_ENTRY_PHYSFS *)fse;
277 if (e->is_dir_open)
278 fs_phys_close_directory(fse);
279 al_destroy_path(e->path);
280 al_free(e);
281 }
282
283 static ALLEGRO_FILE *fs_phys_open_file(ALLEGRO_FS_ENTRY *fse, const char *mode)
284 {
285 return al_fopen_interface(_al_get_phys_vtable(), fs_phys_entry_name(fse),
286 mode);
287 }
288
289 static const ALLEGRO_FS_INTERFACE fs_phys_vtable =
290 {
291 fs_phys_create_entry,
292 fs_phys_destroy_entry,
293 fs_phys_entry_name,
294 fs_phys_update_entry,
295 fs_phys_entry_mode,
296 fs_phys_entry_mtime,
297 fs_phys_entry_mtime,
298 fs_phys_entry_mtime,
299 fs_phys_entry_size,
300 fs_phys_entry_exists,
301 fs_phys_remove_entry,
302
303 fs_phys_open_directory,
304 fs_phys_read_directory,
305 fs_phys_close_directory,
306
307 fs_phys_filename_exists,
308 fs_phys_remove_filename,
309 fs_phys_get_current_directory,
310 fs_phys_change_directory,
311 fs_phys_make_directory,
312
313 fs_phys_open_file
314 };
315
316 void _al_set_physfs_fs_interface(void)
317 {
318 al_set_fs_interface(&fs_phys_vtable);
319 }
320
321 /* vim: set sts=3 sw=3 et: */