-
Notifications
You must be signed in to change notification settings - Fork 1
/
lfs.c
560 lines (478 loc) · 11.3 KB
/
lfs.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
* Callisto - standalone scripting platform for Lua 5.4
* Copyright (c) 2023-2024 Jeremy Baxter.
*/
/***
* Files, directories and file system manipulation.
*
* @module fs
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <lua/lauxlib.h>
#include <lua/lua.h>
#include "util.h"
/***
* Returns the last component of the given path,
* removing any trailing '/' characters. If the given
* path consists entirely of '/' characters, the string
* `"/"` is returned. If *path* is an empty string,
* the string `"."` is returned.
*
* This is purely a string manipulation function and
* depends on no outside state.
*
* @function basename
* @usage
print("name of script file is " .. fs.basename(arg[0]))
assert(fs.basename("/etc/fstab") == "fstab")
* @tparam string path The path to process.
*/
static int
fs_basename(lua_State *L)
{
const char *ret;
char *path; /* parameter 1 (string) */
path = strndup(luaL_checkstring(L, 1), lua_rawlen(L, 1));
ret = basename(path);
if (ret == NULL) /* failed? */
return lfail(L);
lua_pushstring(L, ret);
free(path);
return 1;
}
/***
* Copies the contents of the file *source* to
* the file *target*. *target* will be overwritten
* if it already exists.
*
* @function copy
* @usage
io.output("hello"):write("hello world")
fs.copy("hello", "world")
assert(io.input("world"):read("a") == "hello world")
* @tparam string source The source file to copy.
* @tparam string target The destination file.
*/
static int
fs_copy(lua_State *L)
{
struct stat sb;
const char *source; /* parameter 1 (string) */
const char *target; /* parameter 2 (string) */
char readbuf[512];
ssize_t ret;
int sfd, tfd;
source = luaL_checkstring(L, 1);
target = luaL_checkstring(L, 2);
/* get the source file's mode */
if (stat(source, &sb) == -1)
return lfail(L);
sfd = open(source, O_RDONLY);
tfd = open(target, O_WRONLY | O_CREAT | O_TRUNC, sb.st_mode);
if (sfd == -1 || tfd == -1)
return lfail(L);
for (;;) {
ret = read(sfd, readbuf, 512);
switch (ret) {
case 0: /* end-of-file */
goto finish;
break;
case -1: /* error */
close(sfd);
close(tfd);
return lfail(L);
break;
}
ret = write(tfd, readbuf, ret);
if (ret == -1) {
close(sfd);
close(tfd);
return lfail(L);
}
}
finish:
close(sfd);
close(tfd);
lua_pushboolean(L, 1);
return 1;
}
/***
* Returns the parent directory of the pathn
* given. Any trailing '/' characters are not
* counted as part of the directory name.
* If the given path is an empty string or contains
* no '/' characters, the string `"."` is returned,
* signifying the current directory.
*
* This is purely a string manipulation function and
* depends on no outside state.
*
* @function dirname
* @usage assert(fs.dirname("/usr/local/bin") == "/usr/local")
* @tparam string path The path to process.
*/
static int
fs_dirname(lua_State *L)
{
const char *ret;
char *path; /* parameter 1 (string) */
path = strndup(luaL_checkstring(L, 1), lua_rawlen(L, 1));
ret = dirname(path);
if (ret == NULL) /* failed? */
return lfail(L);
lua_pushstring(L, ret);
free(path);
return 1;
}
/***
* Returns true if the given pathname exists
* in the file system.
*
* @function exists
* @usage
if fs.exists("/etc/fstab") then
-- ...
end
* @tparam string path The path of the file to look for.
*/
static int
fs_exists(lua_State *L)
{
const char *path; /* parameter 1 (string) */
int ret;
path = luaL_checkstring(L, 1);
ret = access(path, F_OK); /* check if file exists */
if (ret == -1) /* failed? */
return lfail(L);
lua_pushboolean(L, ret == 0);
return 1;
}
static int
ismode(lua_State *L, mode_t mode)
{
struct stat sb;
const char *path; /* parameter 1 (string) */
path = luaL_checkstring(L, 1);
if (stat(path, &sb) != -1) {
lua_pushboolean(L, sb.st_mode & mode);
return 1;
}
switch (errno) {
case ENOTDIR:
case ENOENT:
lua_pushboolean(L, 0);
return 1;
break;
default:
return lfail(L);
}
}
/***
* Returns true if the given path specifies a directory.
* Will return false if either the given path does not
* specify a directory or the path does not exist at all.
*
* On error returns nil, an error message and a
* platform-dependent error code.
*
* @function isdirectory
* @usage
if not fs.isdirectory("/usr") then
print("something's wrong")
end
* @tparam string path The path to check.
*/
static int
fs_isdirectory(lua_State *L)
{
return ismode(L, S_IFDIR);
}
/***
* Returns true if the given path specifies a file.
* Will return false if either the given path
* does not specify a directory or the path
* does not exist at all.
*
* On error returns nil, an error message and a
* platform-dependent error code.
*
* @function isfile
* @usage
if not fs.isfile("/sbin/init") then
print("something's wrong")
end
* @tparam string path The path to check.
*/
static int
fs_isfile(lua_State *L)
{
return ismode(L, S_IFREG);
}
/*
* Taken from OpenBSD mkdir(1)
* mkpath -- create directories.
* path - path
* mode - file mode of terminal directory
* dir_mode - file mode of intermediate directories
*/
static int
mkpath(char *path, mode_t mode, mode_t dir_mode)
{
struct stat sb;
char *slash;
int done;
slash = path;
for (;;) {
slash += strspn(slash, "/");
slash += strcspn(slash, "/");
done = (*slash == '\0');
*slash = '\0';
if (mkdir(path, done ? mode : dir_mode) == 0) {
if (mode > 0777 && chmod(path, mode) == -1)
return -1;
} else {
int mkdir_errno = errno;
if (stat(path, &sb) == -1) {
/* Not there; use mkdir()s errno */
errno = mkdir_errno;
return -1;
}
if (!S_ISDIR(sb.st_mode)) {
/* Is there, but isn't a directory */
errno = ENOTDIR;
return -1;
}
}
if (done)
break;
*slash = '/';
}
return 0;
}
/***
* Creates a new directory.
*
* If *recursive* is true, creates intermediate directories
* (parent directories) as required; behaves as POSIX
* `mkdir -p` would.
*
* On success, returns true. Otherwise returns nil, an error
* message and a platform-dependent error code.
*
* @function mkdir
* @usage fs.mkdir("/usr/local/bin")
* @tparam string dir The path of the directory to create.
* @tparam[opt] boolean recursive Whether to create intermediate directories.
*/
static int
fs_mkdir(lua_State *L)
{
char *dir; /* parameter 1 (string) */
int recursive; /* parameter 2 (boolean) */
int ret;
dir = strdup(luaL_checkstring(L, 1));
if (!lua_isboolean(L, 2) && !lua_isnoneornil(L, 2))
luaL_typeerror(L, 2, "boolean");
recursive = lua_toboolean(L, 2);
if (recursive)
ret = mkpath(dir, 0777, 0777);
else
ret = mkdir(dir, 0777);
if (ret == 0) {
lua_pushboolean(L, 1);
return 1;
}
return lfail(L);
}
/***
* Moves the item at path *src* to path *dest*.
* If *dest* exists, it is overwritten. Both *src* and *dest*
* must be of the same type (that is, both directories or both
* non-directories) and must reside on the same file system.
*
* (If you need to move files across different file systems,
* consider using `fs.copy` instead.)
*
* On success, returns true. Otherwise returns nil, an error
* message and a platform-dependent error code.
*
* @function move
* @usage fs.move("srcfile", "destfile")
* @tparam string src The pathname of the file to move.
* @tparam string dest The destination pathname.
*/
static int
fs_move(lua_State *L)
{
int ret;
const char *src; /* parameter 1 (string) */
const char *dest; /* parameter 2 (string) */
src = luaL_checkstring(L, 1);
dest = luaL_checkstring(L, 2);
ret = rename(src, dest); /* move file */
if (ret == 0) { /* check for success */
lua_pushboolean(L, 1);
return 1;
}
return lfail(L);
}
static int
recursiveremove(lua_State *L, const char *path)
{
DIR *d;
struct dirent *ent;
char *fullname;
size_t len, nlen, plen;
if ((d = opendir(path)) == NULL) {
if (errno != ENOTDIR)
return lfail(L);
/* if it's a file... */
if (remove(path) == -1)
return lfail(L);
lua_pushboolean(L, 1);
return 1;
}
while ((ent = readdir(d)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
plen = strlen(path);
nlen = strlen(ent->d_name);
/* path + name + slash in between + null terminator */
len = plen + nlen + 2;
fullname = malloc(len * sizeof(char));
strbcpy(fullname, path, len);
fullname[plen] = '/';
fullname[plen + 1] = 0;
strbcat(fullname, ent->d_name, len);
if (ent->d_type == DT_DIR) {
/* if rmdir succeeded, free fullname and
* proceed with next entry */
if (rmdir(fullname) != -1)
goto next;
if (errno == ENOTEMPTY) /* if the directory is not empty... */
recursiveremove(L, fullname);
else /* if some other error occurred */
return lfail(L);
} else {
if (remove(fullname) == -1)
return lfail(L);
}
next:
free(fullname);
}
closedir(d);
if (rmdir(path) == -1)
return lfail(L);
lua_pushboolean(L, 1);
return 1;
}
/***
* Removes a file or directory.
*
* On success, returns true. Otherwise returns nil, an error
* message and a platform-dependent error code.
*
* @function remove
* @usage fs.remove("path/to/file")
* @tparam string dir The path to remove.
*/
static int
fs_remove(lua_State *L)
{
const char *path; /* parameter 1 (string) */
path = luaL_checkstring(L, 1);
return recursiveremove(L, path);
}
/***
* Removes an empty directory.
*
* On success, returns true. Otherwise returns nil, an error
* message and a platform-dependent error code.
*
* @function rmdir
* @usage fs.rmdir("yourdirectory")
* @tparam string dir The path of the directory to remove.
*/
static int
fs_rmdir(lua_State *L)
{
const char *dir; /* parameter 1 (string) */
int ret;
dir = luaL_checkstring(L, 1);
ret = rmdir(dir);
if (ret == 0) {
lua_pushboolean(L, 1);
return 1;
}
return lfail(L);
}
/***
* Returns or sets the current working directory.
*
* If *dir* is nil, returns the current working
* directory. Otherwise, sets the current working
* directory to *dir*.
*
* On success, returns true. Otherwise returns nil, an error
* message and a platform-dependent error code.
*
* @function workdir
* @usage
-- Store the current working directory in a variable:
local wd = fs.workdir()
-- Set the current working directory to the value of
-- the HOME environment variable:
fs.workdir(environ["HOME"])
* @tparam[opt] string dir The directory to set as the current working directory.
*/
static int
fs_workdir(lua_State *L)
{
const char *workdir; /* parameter 1 (string) */
char buffer[512]; /* buffer used by getcwd() */
char *ret;
if (lua_isnoneornil(L, 1)) { /* if first argument was not given... */
ret = getcwd(buffer, 512);
if (ret != NULL) {
lua_pushstring(L, buffer);
return 1;
}
return lfail(L);
}
workdir = luaL_checkstring(L, 1);
if (chdir(workdir) == 0) {
lua_pushboolean(L, 1);
return 1;
}
return lfail(L);
}
/* clang-format off */
static const luaL_Reg fslib[] = {
{"basename", fs_basename},
{"copy", fs_copy},
{"dirname", fs_dirname},
{"exists", fs_exists},
{"isdirectory", fs_isdirectory},
{"isfile", fs_isfile},
{"mkdir", fs_mkdir},
{"move", fs_move},
{"remove", fs_remove},
{"rmdir", fs_rmdir},
{"workdir", fs_workdir},
{NULL, NULL}
};
int
luaopen_fs(lua_State *L)
{
luaL_newlib(L, fslib);
return 1;
}