-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathfilesystem.c
416 lines (340 loc) · 10.8 KB
/
filesystem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "filesystem.h"
#include "lutro.h"
#include "lutro_assert.h"
#include <compat/strl.h>
#include <file/file_path.h>
#include <streams/file_stream.h>
#include <vfs/vfs_implementation.h>
#include <string/stdstring.h>
// for getcwd...
#if defined(_WIN32)
# include <direct.h>
#else
# include <unistd.h>
#endif
#if WANT_PHYSFS
#include "physfs.h"
#endif
#include <stdlib.h>
#include <string.h>
int _raw_get_user_writable_dir(lua_State *L)
{
char* savedir;
if ((*settings.environ_cb)(RETRO_ENVIRONMENT_GET_SAVE_DIRECTORY, &savedir)) {
lua_pushstring(L, savedir);
return 1;
}
// in the case that no savedir is available: return nil rather than seeking a fallback.
return 0;
}
int lutro_filesystem_preload(lua_State *L)
{
static const luaL_Reg fs_funcs[] = {
{ "exists", fs_exists },
{ "read", fs_read },
{ "write", fs_write },
{ "setRequirePath", fs_setRequirePath },
{ "getRequirePath", fs_getRequirePath },
{ "load", fs_load },
{ "setIdentity", fs_setIdentity },
{ "getAppdataDirectory", fs_getAppdataDirectory },
{ "getWorkingDirectory", fs_getWorkingDirectory },
{ "isDirectory", fs_isDirectory },
{ "isFile", fs_isFile },
{ "createDirectory", fs_createDirectory },
{ "getDirectoryItems", fs_getDirectoryItems },
// internal helpers for lua-authored functions.
{ "_raw_get_user_writable_dir", _raw_get_user_writable_dir },
{NULL, NULL}
};
lutro_newlib(L, fs_funcs, "filesystem");
// lutro.filesystem._appendTrailingSlash - appends a trailing slash to match behavior of LOVE.
// Because slash or backslash is platform dependent, this implementation takes the laziest
// approach: if the incoming path has a trailing backslash then it checks if the OS environment
// is Windows (WINDIR) and if yes then it doesn't append a trailing slash. It does not attempt
// to append trialing backslashes. Windows tolerates forward slash and mixed-slash paths well.
if (1) {
int ret = luaL_dostring(L, "\
lutro.filesystem._appendTrailingSlash = function(path)\n\
if path:sub(-1) == '\\\\' then\n\
local winDir = os.getenv('WINDIR')\n\
if winDir ~= nil and winDir ~= '' then\n\
path = path .. '/'\n\
end\n\
end\n\
if path:sub(-1) ~= '/' then\n\
path = path .. '/'\n\
end\n\
return path\n\
end");
if (ret) {
// don't hard-fail - no need to block apps that might not use the function.
lutro_errorf("failed to assign lutro.filesystem.getUserDirectory: %s\n", lua_tostring(L, -1));
lua_pop(L, 1); // pop error message
}
}
// lutro.filesystem.getUserDirectory
//
// Returns the writable save directory provided by libretro frontend. The directory
// may or may not be sandboxed according to a current user, depending on the design
// and capabilities of the platform OS. Funtion may return nil if the libretro frontend
// does not provide a writable save directory.
//
// implementation notes:
// - UserDirectory should always have a trailing slash (as confirmed on Love2D)
// - Cache the UserDir to avoid costly re-calculation of env var lookups. UserDir and all env vars
// are static for the lifetime of the process.
// - https://love2d.org/wiki/love.filesystem.getUserDirectory
if (1) {
int ret = luaL_dostring(L, "\
lutro.filesystem.getUserDirectory = function()\n\
local savedir = lutro.filesystem._raw_get_user_writable_dir()\n\
if savedir == nil or savedir == '' then\n\
return nil\n\
end\n\
return lutro.filesystem._appendTrailingSlash(savedir)\n\
end");
if (ret) {
// don't hard-fail - no need to block apps that might not use the function.
lutro_errorf("failed to assign lutro.filesystem.getUserDirectory: %s\n", lua_tostring(L, -1));
lua_pop(L, 1); // pop error message
}
}
return 1;
}
void lutro_filesystem_init(void)
{
#if WANT_PHYSFS
PHYSFS_init(NULL);
#endif
}
void lutro_filesystem_deinit(void)
{
#if WANT_PHYSFS
PHYSFS_deinit();
#endif
}
/**
* contents, size = lutro.filesystem.read(name, size)
*
* https://love2d.org/wiki/love.filesystem.read
*/
int fs_read(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
char fullpath[PATH_MAX_LENGTH];
strlcpy(fullpath, settings.gamedir, sizeof(fullpath));
strlcat(fullpath, path, sizeof(fullpath));
FILE *fp = fopen(fullpath, "r");
if (!fp)
return -1;
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *string = lutro_malloc(fsize + 1);
size_t bytes_read = fread(string, 1, fsize, fp);
fclose(fp);
string[bytes_read] = 0;
lua_pushstring(L, string);
lua_pushnumber(L, bytes_read);
lutro_free(string);
return 2;
}
int fs_write(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
const char *data = luaL_checkstring(L, 2);
char fullpath[PATH_MAX_LENGTH];
strlcpy(fullpath, settings.gamedir, sizeof(fullpath));
strlcat(fullpath, path, sizeof(fullpath));
FILE *fp = fopen(fullpath, "w");
if (!fp)
return -1;
fprintf(fp, "%s", data);
fclose(fp);
lua_pushboolean(L, 1);
return 1;
}
/**
* lutro.filesystem.setRequirePath
*
* @see https://love2d.org/wiki/love.filesystem.setRequirePath
*/
int fs_setRequirePath(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
lua_getglobal(L, "package");
lua_pushstring(L, path) ;
lua_setfield(L, -2, "path");
lua_pop(L, 1);
return 0;
}
/**
* lutro.filesystem.getRequirePath
*
* @see https://love2d.org/wiki/love.filesystem.getRequirePath
*/
int fs_getRequirePath(lua_State *L)
{
const char *cur_path;
lua_getglobal(L, "package");
lua_getfield(L, -1, "path");
cur_path = lua_tostring( L, -1);
lua_pop(L, 1);
lua_pushstring(L, cur_path);
return 1;
}
int fs_load(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
char fullpath[PATH_MAX_LENGTH];
strlcpy(fullpath, settings.gamedir, sizeof(fullpath));
strlcat(fullpath, path, sizeof(fullpath));
FILE *fp = fopen(fullpath, "r");
if (!fp)
return -1;
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *string = lutro_malloc(fsize + 1);
fread(string, fsize, 1, fp);
fclose(fp);
string[fsize] = 0;
int status = luaL_loadbuffer(L, string, fsize, path);
lutro_free(string);
switch (status)
{
case LUA_ERRMEM:
return luaL_error(L, "Memory allocation error: %s\n", lua_tostring(L, -1));
case LUA_ERRSYNTAX:
return luaL_error(L, "Syntax error: %s\n", lua_tostring(L, -1));
default:
return 1;
}
}
int fs_exists(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
char fullpath[PATH_MAX_LENGTH];
strlcpy(fullpath, settings.gamedir, sizeof(fullpath));
strlcat(fullpath, path, sizeof(fullpath));
bool exists = filestream_exists(fullpath);
lua_pushboolean(L, exists);
return 1;
}
int fs_setIdentity(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
strlcpy(settings.identity, name, sizeof(settings.identity));
return 0;
}
/**
* lutro.filesystem.getAppdataDirectory
*
* Retrieves libretro's SYSTEM directory.
*
* @see https://love2d.org/wiki/love.filesystem.getAppdataDirectory
* @see RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY
*/
int fs_getAppdataDirectory(lua_State *L)
{
char* appdataDir;
if ((*settings.environ_cb)(RETRO_ENVIRONMENT_GET_SYSTEM_DIRECTORY, &appdataDir)) {
lua_pushstring(L, appdataDir);
}
else {
lua_pushstring(L, "");
}
return 1;
}
/**
* lutro.filesystem.getWorkingDirectory
*
* Retrieves the process current working directory (cwd or pwd). Lutro always returns the
* relative directory prefix "./" which is generally interpreted by filesystem APIs as the
* cwd when used as the prefix to a function. LOVE returns the CWD as an absolute path.
* Note that the concept of cwd is inherently non-portable and leads to unexpected or surprising
* behavior for users, and should only be used for local development or debug purposes.
*
* @see https://love2d.org/wiki/love.filesystem.getWorkingDirectory
*/
int fs_getWorkingDirectory(lua_State *L)
{
lua_pushstring(L, "./");
return 1;
}
int fs_isDirectory(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
char fullpath[PATH_MAX_LENGTH];
strlcpy(fullpath, settings.gamedir, sizeof(fullpath));
strlcat(fullpath, path, sizeof(fullpath));
bool res = path_is_directory(fullpath);
lua_pushboolean(L, res);
return 1;
}
int fs_isFile(lua_State *L)
{
char fullpath[PATH_MAX_LENGTH];
bool res = false;
const char *path = luaL_checkstring(L, 1);
strlcpy(fullpath, settings.gamedir, sizeof(fullpath));
strlcat(fullpath, path, sizeof(fullpath));
res = filestream_exists(fullpath) && !path_is_directory(fullpath);
lua_pushboolean(L, res);
return 1;
}
int fs_createDirectory(lua_State *L)
{
bool res;
char fullpath[PATH_MAX_LENGTH];
const char *path = luaL_checkstring(L, 1);
strlcpy(fullpath, settings.gamedir, sizeof(fullpath));
strlcat(fullpath, path, sizeof(fullpath));
res = path_mkdir(fullpath);
lua_pushboolean(L, res);
return 1;
}
int fs_getDirectoryItems(lua_State *L)
{
// Validate number of arguments.
int n = lua_gettop(L);
if (n != 1) {
return luaL_error(L, "lutro.filesystem.getDirectoryItems requires 1 argument, %d given.", n);
}
// Get the full resolved path to the desired directory.
const char *path = luaL_checkstring(L, 1);
char fullpath[PATH_MAX_LENGTH];
strlcpy(fullpath, settings.gamedir, sizeof(fullpath));
strlcat(fullpath, path, sizeof(fullpath));
// Make sure it's a directory.
if (!path_is_directory(fullpath)) {
return luaL_error(L, "The given directory of '%s' is not a directory.", path);
}
// Open up the directory.
libretro_vfs_implementation_dir* dir = retro_vfs_opendir_impl(fullpath, true);
if (!dir) {
retro_vfs_closedir_impl(dir);
return luaL_error(L, "Failed to open the '%s' directory.", path);
}
// Prepare the output table.
lua_newtable(L);
int index = 0;
// Iterate through each directory entry, ignoring the current and previous directories.
while (retro_vfs_readdir_impl(dir)) {
const char * currentDir = retro_vfs_dirent_get_name_impl(dir);
if (currentDir == NULL) {
break;
}
if (string_is_equal(currentDir, ".") || string_is_equal(currentDir, "..")) {
continue;
}
// Add the entry to the table.
lua_pushnumber(L, index++);
lua_pushstring(L, currentDir);
lua_settable(L, -3);
}
// Finally, close the opened directory, and return the table.
retro_vfs_closedir_impl(dir);
return 1;
}