1 /*
2 ** LuaFileSystem
3 ** Copyright Kepler Project 2003 (http://www.keplerproject.org/luafilesystem)
4 **
5 ** File system manipulation library.
6 ** This library offers these functions:
7 ** lfs.attributes (filepath [, attributename])
8 ** lfs.chdir (path)
9 ** lfs.currentdir ()
10 ** lfs.dir (path)
11 ** lfs.lock (fh, mode)
12 ** lfs.lock_dir (path)
13 ** lfs.mkdir (path)
14 ** lfs.rmdir (path)
15 ** lfs.setmode (filepath, mode)
16 ** lfs.symlinkattributes (filepath [, attributename]) -- thanks to Sam Roberts
17 ** lfs.touch (filepath [, atime [, mtime]])
18 ** lfs.unlock (fh)
19 **
20 ** $Id: lfs.c,v 1.61 2009/07/04 02:10:16 mascarenhas Exp $
21 */
22
23 #ifndef _WIN32
24 #ifndef _AIX
25 #define _FILE_OFFSET_BITS 64 /* Linux, Solaris and HP-UX */
26 #else
27 #define _LARGE_FILES 1 /* AIX */
28 #endif
29 #endif
30
31 #define _LARGEFILE64_SOURCE
32
33 #include <errno.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <time.h>
38 #include <sys/stat.h>
39
40 #ifdef _WIN32
41 #include <direct.h>
42 #include <windows.h>
43 #include <io.h>
44 #include <sys/locking.h>
45 #ifdef __BORLANDC__
46 #include <utime.h>
47 #else
48 #include <sys/utime.h>
49 #endif
50 #include <fcntl.h>
51 #else
52 #include <unistd.h>
53 #include <dirent.h>
54 #include <fcntl.h>
55 #include <sys/types.h>
56 #include <utime.h>
57 #endif
58
59 #include "lua.h"
60 #include "lauxlib.h"
61 #include "lualib.h"
62 #include "lfs.h"
63
64 /* Define 'strerror' for systems that do not implement it */
65 #ifdef NO_STRERROR
66 #define strerror(_) "System unable to describe the error"
67 #endif
68
69 /* Define 'getcwd' for systems that do not implement it */
70 #ifdef NO_GETCWD
71 #define getcwd(p,s) NULL
72 #define getcwd_error "Function 'getcwd' not provided by system"
73 #else
74 #define getcwd_error strerror(errno)
75 #endif
76
77 #define DIR_METATABLE "lfs.directory"
78 #define MAX_DIR_LENGTH 1023
79 typedef struct dir_data {
80 int closed;
81 #ifdef _WIN32
82 long hFile;
83 char pattern[MAX_DIR_LENGTH+1];
84 #else
85 DIR *dir;
86 #endif
87 } dir_data;
88
89 #define LOCK_METATABLE "lfs.lock"
90
91 #ifdef _WIN32
92 #ifdef __BORLANDC__
93 #define lfs_setmode(L,file,m) ((void)L, setmode(_fileno(file), m))
94 #define STAT_STRUCT struct stati64
95 #else
96 #define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m))
97 #define STAT_STRUCT struct _stati64
98 #endif
99 #define STAT_FUNC _stati64
100 #else
101 #ifndef _O_TEXT
102 #define _O_TEXT 0
103 #endif
104 #ifndef _O_BINARY
105 #define _O_BINARY 0
106 #endif
107 #define lfs_setmode(L,file,m) ((void)((void)file,m), \
108 luaL_error(L, LUA_QL("setmode") " not supported on this platform"), -1)
109 #define STAT_STRUCT struct stat
110 #define STAT_FUNC stat
111 #define LSTAT_FUNC lstat
112 #endif
113
114 /*
115 ** This function changes the working (current) directory
116 */
change_dir(lua_State * L)117 static int change_dir (lua_State *L) {
118 const char *path = luaL_checkstring(L, 1);
119 if (chdir(path)) {
120 lua_pushnil (L);
121 lua_pushfstring (L,"Unable to change working directory to '%s'\n%s\n",
122 path, chdir_error);
123 return 2;
124 } else {
125 lua_pushboolean (L, 1);
126 return 1;
127 }
128 }
129
130 /*
131 ** This function returns the current directory
132 ** If unable to get the current directory, it returns nil
133 ** and a string describing the error
134 */
135 #ifndef PATH_MAX
136 #define PATH_MAX 4096
137 #endif
138
get_dir(lua_State * L)139 static int get_dir (lua_State *L) {
140 char path[PATH_MAX];
141 if (getcwd((char *)path, PATH_MAX) == NULL) {
142 lua_pushnil(L);
143 lua_pushstring(L, getcwd_error);
144 return 2;
145 }
146 else {
147 lua_pushstring(L, path);
148 return 1;
149 }
150 }
151
152 /*
153 ** Check if the given element on the stack is a file and returns it.
154 */
check_file(lua_State * L,int idx,const char * funcname)155 static FILE *check_file (lua_State *L, int idx, const char *funcname) {
156 FILE **fh = (FILE **)luaL_checkudata (L, idx, "FILE*");
157 if (fh == NULL) {
158 luaL_error (L, "%s: not a file", funcname);
159 return 0;
160 } else if (*fh == NULL) {
161 luaL_error (L, "%s: closed file", funcname);
162 return 0;
163 } else
164 return *fh;
165 }
166
167
168 /*
169 **
170 */
_file_lock(lua_State * L,FILE * fh,const char * mode,const long start,long len,const char * funcname)171 static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long start, long len, const char *funcname) {
172 int code;
173 #ifdef _WIN32
174 /* lkmode valid values are:
175 LK_LOCK Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error.
176 LK_NBLCK Locks the specified bytes. If the bytes cannot be locked, the constant returns an error.
177 LK_NBRLCK Same as _LK_NBLCK.
178 LK_RLCK Same as _LK_LOCK.
179 LK_UNLCK Unlocks the specified bytes, which must have been previously locked.
180
181 Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.
182
183 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp
184 */
185 int lkmode;
186 switch (*mode) {
187 case 'r': lkmode = LK_NBLCK; break;
188 case 'w': lkmode = LK_NBLCK; break;
189 case 'u': lkmode = LK_UNLCK; break;
190 default : return luaL_error (L, "%s: invalid mode", funcname);
191 }
192 if (!len) {
193 fseek (fh, 0L, SEEK_END);
194 len = ftell (fh);
195 }
196 fseek (fh, start, SEEK_SET);
197 #ifdef __BORLANDC__
198 code = locking (fileno(fh), lkmode, len);
199 #else
200 code = _locking (fileno(fh), lkmode, len);
201 #endif
202 #else
203 struct flock f;
204 switch (*mode) {
205 case 'w': f.l_type = F_WRLCK; break;
206 case 'r': f.l_type = F_RDLCK; break;
207 case 'u': f.l_type = F_UNLCK; break;
208 default : return luaL_error (L, "%s: invalid mode", funcname);
209 }
210 f.l_whence = SEEK_SET;
211 f.l_start = (off_t)start;
212 f.l_len = (off_t)len;
213 code = fcntl (fileno(fh), F_SETLK, &f);
214 #endif
215 return (code != -1);
216 }
217
218 #ifdef _WIN32
219 typedef struct lfs_Lock {
220 HANDLE fd;
221 } lfs_Lock;
lfs_lock_dir(lua_State * L)222 static int lfs_lock_dir(lua_State *L) {
223 size_t pathl; HANDLE fd;
224 lfs_Lock *lock;
225 char *ln;
226 const char *lockfile = "/lockfile.lfs";
227 const char *path = luaL_checklstring(L, 1, &pathl);
228 ln = (char*)malloc(pathl + strlen(lockfile) + 1);
229 if(!ln) {
230 lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2;
231 }
232 strcpy(ln, path); strcat(ln, lockfile);
233 if((fd = CreateFile(ln, GENERIC_WRITE, 0, NULL, CREATE_NEW,
234 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL)) == INVALID_HANDLE_VALUE) {
235 int en = GetLastError();
236 free(ln); lua_pushnil(L);
237 if(en == ERROR_FILE_EXISTS || en == ERROR_SHARING_VIOLATION)
238 lua_pushstring(L, "File exists");
239 else
240 lua_pushstring(L, strerror(en));
241 return 2;
242 }
243 free(ln);
244 lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock));
245 lock->fd = fd;
246 luaL_getmetatable (L, LOCK_METATABLE);
247 lua_setmetatable (L, -2);
248 return 1;
249 }
lfs_unlock_dir(lua_State * L)250 static int lfs_unlock_dir(lua_State *L) {
251 lfs_Lock *lock = luaL_checkudata(L, 1, LOCK_METATABLE);
252 CloseHandle(lock->fd);
253 return 0;
254 }
255 #else
256 typedef struct lfs_Lock {
257 char *ln;
258 } lfs_Lock;
lfs_lock_dir(lua_State * L)259 static int lfs_lock_dir(lua_State *L) {
260 lfs_Lock *lock;
261 size_t pathl;
262 char *ln;
263 const char *lockfile = "/lockfile.lfs";
264 const char *path = luaL_checklstring(L, 1, &pathl);
265 lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock));
266 ln = (char*)malloc(pathl + strlen(lockfile) + 1);
267 if(!ln) {
268 lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2;
269 }
270 strcpy(ln, path); strcat(ln, lockfile);
271 if(symlink("lock", ln) == -1) {
272 free(ln); lua_pushnil(L);
273 lua_pushstring(L, strerror(errno)); return 2;
274 }
275 lock->ln = ln;
276 luaL_getmetatable (L, LOCK_METATABLE);
277 lua_setmetatable (L, -2);
278 return 1;
279 }
lfs_unlock_dir(lua_State * L)280 static int lfs_unlock_dir(lua_State *L) {
281 lfs_Lock *lock = luaL_checkudata(L, 1, LOCK_METATABLE);
282 if(lock->ln) {
283 unlink(lock->ln);
284 free(lock->ln);
285 lock->ln = NULL;
286 }
287 return 0;
288 }
289 #endif
290
291 #ifdef _WIN32
lfs_g_setmode(lua_State * L,FILE * f,int arg)292 static int lfs_g_setmode (lua_State *L, FILE *f, int arg) {
293 static const int mode[] = {_O_TEXT, _O_BINARY};
294 static const char *const modenames[] = {"text", "binary", NULL};
295 int op = luaL_checkoption(L, arg, NULL, modenames);
296 int res = lfs_setmode(L, f, mode[op]);
297 if (res != -1) {
298 int i;
299 lua_pushboolean(L, 1);
300 for (i = 0; modenames[i] != NULL; i++) {
301 if (mode[i] == res) {
302 lua_pushstring(L, modenames[i]);
303 goto exit;
304 }
305 }
306 lua_pushnil(L);
307 exit:
308 return 2;
309 } else {
310 int en = errno;
311 lua_pushnil(L);
312 lua_pushfstring(L, "%s", strerror(en));
313 lua_pushinteger(L, en);
314 return 3;
315 }
316 }
317 #else
lfs_g_setmode(lua_State * L,FILE * f,int arg)318 static int lfs_g_setmode (lua_State *L, FILE *f, int arg) {
319 lua_pushboolean(L, 0);
320 lua_pushliteral(L, "setmode not supported on this platform");
321 return 2;
322 }
323 #endif
324
lfs_f_setmode(lua_State * L)325 static int lfs_f_setmode(lua_State *L) {
326 return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2);
327 }
328
329 /*
330 ** Locks a file.
331 ** @param #1 File handle.
332 ** @param #2 String with lock mode ('w'rite, 'r'ead).
333 ** @param #3 Number with start position (optional).
334 ** @param #4 Number with length (optional).
335 */
file_lock(lua_State * L)336 static int file_lock (lua_State *L) {
337 FILE *fh = check_file (L, 1, "lock");
338 const char *mode = luaL_checkstring (L, 2);
339 const long start = luaL_optlong (L, 3, 0);
340 long len = luaL_optlong (L, 4, 0);
341 if (_file_lock (L, fh, mode, start, len, "lock")) {
342 lua_pushboolean (L, 1);
343 return 1;
344 } else {
345 lua_pushnil (L);
346 lua_pushfstring (L, "%s", strerror(errno));
347 return 2;
348 }
349 }
350
351
352 /*
353 ** Unlocks a file.
354 ** @param #1 File handle.
355 ** @param #2 Number with start position (optional).
356 ** @param #3 Number with length (optional).
357 */
file_unlock(lua_State * L)358 static int file_unlock (lua_State *L) {
359 FILE *fh = check_file (L, 1, "unlock");
360 const long start = luaL_optlong (L, 2, 0);
361 long len = luaL_optlong (L, 3, 0);
362 if (_file_lock (L, fh, "u", start, len, "unlock")) {
363 lua_pushboolean (L, 1);
364 return 1;
365 } else {
366 lua_pushnil (L);
367 lua_pushfstring (L, "%s", strerror(errno));
368 return 2;
369 }
370 }
371
372
make_dir(lua_State * L)373 static int make_dir (lua_State *L) {
374 const char *path = luaL_checkstring (L, 1);
375 int fail;
376 #ifdef _WIN32
377 int oldmask = umask (0);
378 fail = _mkdir (path);
379 #else
380 mode_t oldmask = umask( (mode_t)0 );
381 fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
382 S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
383 #endif
384 if (fail) {
385 lua_pushnil (L);
386 lua_pushfstring (L, "%s", strerror(errno));
387 return 2;
388 }
389 umask (oldmask);
390 lua_pushboolean (L, 1);
391 return 1;
392 }
393
394 /*
395 ** Removes a directory.
396 ** @param #1 Directory path.
397 */
remove_dir(lua_State * L)398 static int remove_dir (lua_State *L) {
399 const char *path = luaL_checkstring (L, 1);
400 int fail;
401
402 fail = rmdir (path);
403
404 if (fail) {
405 lua_pushnil (L);
406 lua_pushfstring (L, "%s", strerror(errno));
407 return 2;
408 }
409 lua_pushboolean (L, 1);
410 return 1;
411 }
412
413 /*
414 ** Directory iterator
415 */
dir_iter(lua_State * L)416 static int dir_iter (lua_State *L) {
417 #ifdef _WIN32
418 struct _finddata_t c_file;
419 #else
420 struct dirent *entry;
421 #endif
422 dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE);
423 luaL_argcheck (L, !d->closed, 1, "closed directory");
424 #ifdef _WIN32
425 if (d->hFile == 0L) { /* first entry */
426 if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) {
427 lua_pushnil (L);
428 lua_pushstring (L, strerror (errno));
429 return 2;
430 } else {
431 lua_pushstring (L, c_file.name);
432 return 1;
433 }
434 } else { /* next entry */
435 if (_findnext (d->hFile, &c_file) == -1L) {
436 /* no more entries => close directory */
437 _findclose (d->hFile);
438 d->closed = 1;
439 return 0;
440 } else {
441 lua_pushstring (L, c_file.name);
442 return 1;
443 }
444 }
445 #else
446 if ((entry = readdir (d->dir)) != NULL) {
447 lua_pushstring (L, entry->d_name);
448 return 1;
449 } else {
450 /* no more entries => close directory */
451 closedir (d->dir);
452 d->closed = 1;
453 return 0;
454 }
455 #endif
456 }
457
458
459 /*
460 ** Closes directory iterators
461 */
dir_close(lua_State * L)462 static int dir_close (lua_State *L) {
463 dir_data *d = (dir_data *)lua_touserdata (L, 1);
464 #ifdef _WIN32
465 if (!d->closed && d->hFile) {
466 _findclose (d->hFile);
467 d->closed = 1;
468 }
469 #else
470 if (!d->closed && d->dir) {
471 closedir (d->dir);
472 d->closed = 1;
473 }
474 #endif
475 return 0;
476 }
477
478
479 /*
480 ** Factory of directory iterators
481 */
dir_iter_factory(lua_State * L)482 static int dir_iter_factory (lua_State *L) {
483 const char *path = luaL_checkstring (L, 1);
484 dir_data *d;
485 lua_pushcfunction (L, dir_iter);
486 d = (dir_data *) lua_newuserdata (L, sizeof(dir_data));
487 d->closed = 0;
488 #ifdef _WIN32
489 d->hFile = 0L;
490 luaL_getmetatable (L, DIR_METATABLE);
491 lua_setmetatable (L, -2);
492 if (strlen(path) > MAX_DIR_LENGTH)
493 luaL_error (L, "path too long: %s", path);
494 else
495 sprintf (d->pattern, "%s/*", path);
496 #else
497 luaL_getmetatable (L, DIR_METATABLE);
498 lua_setmetatable (L, -2);
499 d->dir = opendir (path);
500 if (d->dir == NULL)
501 luaL_error (L, "cannot open %s: %s", path, strerror (errno));
502 #endif
503 return 2;
504 }
505
506
507 /*
508 ** Creates directory metatable.
509 */
dir_create_meta(lua_State * L)510 static int dir_create_meta (lua_State *L) {
511 luaL_newmetatable (L, DIR_METATABLE);
512 /* set its __gc field */
513 lua_pushstring (L, "__index");
514 lua_newtable(L);
515 lua_pushstring (L, "next");
516 lua_pushcfunction (L, dir_iter);
517 lua_settable(L, -3);
518 lua_pushstring (L, "close");
519 lua_pushcfunction (L, dir_close);
520 lua_settable(L, -3);
521 lua_settable (L, -3);
522 lua_pushstring (L, "__gc");
523 lua_pushcfunction (L, dir_close);
524 lua_settable (L, -3);
525 return 1;
526 }
527
528 /*
529 ** Creates lock metatable.
530 */
lock_create_meta(lua_State * L)531 static int lock_create_meta (lua_State *L) {
532 luaL_newmetatable (L, LOCK_METATABLE);
533 /* set its __gc field */
534 lua_newtable(L);
535 lua_pushcfunction(L, lfs_unlock_dir);
536 lua_setfield(L, -2, "free");
537 lua_setfield(L, -2, "__index");
538 lua_pushcfunction(L, lfs_unlock_dir);
539 lua_setfield(L, -2, "__gc");
540 return 1;
541 }
542
543
544 #ifdef _WIN32
545 #ifndef S_ISDIR
546 #define S_ISDIR(mode) (mode&_S_IFDIR)
547 #endif
548 #ifndef S_ISREG
549 #define S_ISREG(mode) (mode&_S_IFREG)
550 #endif
551 #ifndef S_ISLNK
552 #define S_ISLNK(mode) (0)
553 #endif
554 #ifndef S_ISSOCK
555 #define S_ISSOCK(mode) (0)
556 #endif
557 #ifndef S_ISFIFO
558 #define S_ISFIFO(mode) (0)
559 #endif
560 #ifndef S_ISCHR
561 #define S_ISCHR(mode) (mode&_S_IFCHR)
562 #endif
563 #ifndef S_ISBLK
564 #define S_ISBLK(mode) (0)
565 #endif
566 #endif
567 /*
568 ** Convert the inode protection mode to a string.
569 */
570 #ifdef _WIN32
mode2string(unsigned short mode)571 static const char *mode2string (unsigned short mode) {
572 #else
573 static const char *mode2string (mode_t mode) {
574 #endif
575 if ( S_ISREG(mode) )
576 return "file";
577 else if ( S_ISDIR(mode) )
578 return "directory";
579 else if ( S_ISLNK(mode) )
580 return "link";
581 else if ( S_ISSOCK(mode) )
582 return "socket";
583 else if ( S_ISFIFO(mode) )
584 return "named pipe";
585 else if ( S_ISCHR(mode) )
586 return "char device";
587 else if ( S_ISBLK(mode) )
588 return "block device";
589 else
590 return "other";
591 }
592
593
594 /*
595 ** Set access time and modification values for file
596 */
597 static int file_utime (lua_State *L) {
598 const char *file = luaL_checkstring (L, 1);
599 struct utimbuf utb, *buf;
600
601 if (lua_gettop (L) == 1) /* set to current date/time */
602 buf = NULL;
603 else {
604 utb.actime = (time_t)luaL_optnumber (L, 2, 0);
605 utb.modtime = (time_t)luaL_optnumber (L, 3, utb.actime);
606 buf = &utb;
607 }
608 if (utime (file, buf)) {
609 lua_pushnil (L);
610 lua_pushfstring (L, "%s", strerror (errno));
611 return 2;
612 }
613 lua_pushboolean (L, 1);
614 return 1;
615 }
616
617
618 /* inode protection mode */
619 static void push_st_mode (lua_State *L, STAT_STRUCT *info) {
620 lua_pushstring (L, mode2string (info->st_mode));
621 }
622 /* device inode resides on */
623 static void push_st_dev (lua_State *L, STAT_STRUCT *info) {
624 lua_pushnumber (L, (lua_Number)info->st_dev);
625 }
626 /* inode's number */
627 static void push_st_ino (lua_State *L, STAT_STRUCT *info) {
628 lua_pushnumber (L, (lua_Number)info->st_ino);
629 }
630 /* number of hard links to the file */
631 static void push_st_nlink (lua_State *L, STAT_STRUCT *info) {
632 lua_pushnumber (L, (lua_Number)info->st_nlink);
633 }
634 /* user-id of owner */
635 static void push_st_uid (lua_State *L, STAT_STRUCT *info) {
636 lua_pushnumber (L, (lua_Number)info->st_uid);
637 }
638 /* group-id of owner */
639 static void push_st_gid (lua_State *L, STAT_STRUCT *info) {
640 lua_pushnumber (L, (lua_Number)info->st_gid);
641 }
642 /* device type, for special file inode */
643 static void push_st_rdev (lua_State *L, STAT_STRUCT *info) {
644 lua_pushnumber (L, (lua_Number)info->st_rdev);
645 }
646 /* time of last access */
647 static void push_st_atime (lua_State *L, STAT_STRUCT *info) {
648 lua_pushnumber (L, info->st_atime);
649 }
650 /* time of last data modification */
651 static void push_st_mtime (lua_State *L, STAT_STRUCT *info) {
652 lua_pushnumber (L, info->st_mtime);
653 }
654 /* time of last file status change */
655 static void push_st_ctime (lua_State *L, STAT_STRUCT *info) {
656 lua_pushnumber (L, info->st_ctime);
657 }
658 /* file size, in bytes */
659 static void push_st_size (lua_State *L, STAT_STRUCT *info) {
660 lua_pushnumber (L, (lua_Number)info->st_size);
661 }
662 #ifndef _WIN32
663 /* blocks allocated for file */
664 static void push_st_blocks (lua_State *L, STAT_STRUCT *info) {
665 lua_pushnumber (L, (lua_Number)info->st_blocks);
666 }
667 /* optimal file system I/O blocksize */
668 static void push_st_blksize (lua_State *L, STAT_STRUCT *info) {
669 lua_pushnumber (L, (lua_Number)info->st_blksize);
670 }
671 #endif
672 static void push_invalid (lua_State *L, STAT_STRUCT *info) {
673 luaL_error(L, "invalid attribute name");
674 #ifndef _WIN32
675 info->st_blksize = 0; /* never reached */
676 #endif
677 }
678
679 /*
680 ** Convert the inode protection mode to a permission list.
681 */
682
683 #ifdef _WIN32
684 static const char *perm2string (unsigned short mode) {
685 static char perms[10] = "---------\0";
686 int i;
687 for (i=0;i<9;i++) perms[i]='-';
688 if (mode & _S_IREAD)
689 { perms[0] = 'r'; perms[3] = 'r'; perms[6] = 'r'; }
690 if (mode & _S_IWRITE)
691 { perms[1] = 'w'; perms[4] = 'w'; perms[7] = 'w'; }
692 if (mode & _S_IEXEC)
693 { perms[2] = 'x'; perms[5] = 'x'; perms[8] = 'x'; }
694 return perms;
695 }
696 #else
697 static const char *perm2string (mode_t mode) {
698 static char perms[10] = "---------\0";
699 int i;
700 for (i=0;i<9;i++) perms[i]='-';
701 if (mode & S_IRUSR) perms[0] = 'r';
702 if (mode & S_IWUSR) perms[1] = 'w';
703 if (mode & S_IXUSR) perms[2] = 'x';
704 if (mode & S_IRGRP) perms[3] = 'r';
705 if (mode & S_IWGRP) perms[4] = 'w';
706 if (mode & S_IXGRP) perms[5] = 'x';
707 if (mode & S_IROTH) perms[6] = 'r';
708 if (mode & S_IWOTH) perms[7] = 'w';
709 if (mode & S_IXOTH) perms[8] = 'x';
710 return perms;
711 }
712 #endif
713
714 /* permssions string */
715 static void push_st_perm (lua_State *L, STAT_STRUCT *info) {
716 lua_pushstring (L, perm2string (info->st_mode));
717 }
718
719 typedef void (*_push_function) (lua_State *L, STAT_STRUCT *info);
720
721 struct _stat_members {
722 const char *name;
723 _push_function push;
724 };
725
726 struct _stat_members members[] = {
727 { "mode", push_st_mode },
728 { "dev", push_st_dev },
729 { "ino", push_st_ino },
730 { "nlink", push_st_nlink },
731 { "uid", push_st_uid },
732 { "gid", push_st_gid },
733 { "rdev", push_st_rdev },
734 { "access", push_st_atime },
735 { "modification", push_st_mtime },
736 { "change", push_st_ctime },
737 { "size", push_st_size },
738 { "permissions", push_st_perm },
739 #ifndef _WIN32
740 { "blocks", push_st_blocks },
741 { "blksize", push_st_blksize },
742 #endif
743 { NULL, push_invalid }
744 };
745
746 /*
747 ** Get file or symbolic link information
748 */
749 static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) {
750 int i;
751 STAT_STRUCT info;
752 const char *file = luaL_checkstring (L, 1);
753
754 if (st(file, &info)) {
755 lua_pushnil (L);
756 lua_pushfstring (L, "cannot obtain information from file `%s'", file);
757 return 2;
758 }
759 if (lua_isstring (L, 2)) {
760 int v;
761 const char *member = lua_tostring (L, 2);
762 if (strcmp (member, "mode") == 0) v = 0;
763 #ifndef _WIN32
764 else if (strcmp (member, "blocks") == 0) v = 11;
765 else if (strcmp (member, "blksize") == 0) v = 12;
766 #endif
767 else /* look for member */
768 for (v = 1; members[v].name; v++)
769 if (*members[v].name == *member)
770 break;
771 /* push member value and return */
772 members[v].push (L, &info);
773 return 1;
774 } else if (!lua_istable (L, 2))
775 /* creates a table if none is given */
776 lua_newtable (L);
777 /* stores all members in table on top of the stack */
778 for (i = 0; members[i].name; i++) {
779 lua_pushstring (L, members[i].name);
780 members[i].push (L, &info);
781 lua_rawset (L, -3);
782 }
783 return 1;
784 }
785
786
787 /*
788 ** Get file information using stat.
789 */
790 static int file_info (lua_State *L) {
791 return _file_info_ (L, STAT_FUNC);
792 }
793
794
795 /*
796 ** Get symbolic link information using lstat.
797 */
798 #ifndef _WIN32
799 static int link_info (lua_State *L) {
800 return _file_info_ (L, LSTAT_FUNC);
801 }
802 #else
803 static int link_info (lua_State *L) {
804 lua_pushboolean(L, 0);
805 lua_pushliteral(L, "symlinkattributes not supported on this platform");
806 return 2;
807 }
808 #endif
809
810
811 /*
812 ** Assumes the table is on top of the stack.
813 */
814 static void set_info (lua_State *L) {
815 lua_pushliteral (L, "_COPYRIGHT");
816 lua_pushliteral (L, "Copyright (C) 2003-2009 Kepler Project");
817 lua_settable (L, -3);
818 lua_pushliteral (L, "_DESCRIPTION");
819 lua_pushliteral (L, "LuaFileSystem is a Lua library developed to complement the set of functions related to file systems offered by the standard Lua distribution");
820 lua_settable (L, -3);
821 lua_pushliteral (L, "_VERSION");
822 lua_pushliteral (L, "LuaFileSystem 1.5.0");
823 lua_settable (L, -3);
824 }
825
826
827 static const struct luaL_Reg fslib[] = {
828 {"attributes", file_info},
829 {"chdir", change_dir},
830 {"currentdir", get_dir},
831 {"dir", dir_iter_factory},
832 {"lock", file_lock},
833 {"mkdir", make_dir},
834 {"rmdir", remove_dir},
835 {"symlinkattributes", link_info},
836 {"setmode", lfs_f_setmode},
837 {"touch", file_utime},
838 {"unlock", file_unlock},
839 {"lock_dir", lfs_lock_dir},
840 {NULL, NULL},
841 };
842
843 int luaopen_lfs (lua_State *L) {
844 dir_create_meta (L);
845 lock_create_meta (L);
846 luaL_register (L, "lfs", fslib);
847 set_info (L);
848 return 1;
849 }
850