beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / luafilesystem / src / lfs.c
blob2b1b06cf1db0ee8636a8e07559d61592d25c950d
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)
20 ** $Id: lfs.c,v 1.61 2009/07/04 02:10:16 mascarenhas Exp $
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
31 #define _LARGEFILE64_SOURCE
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>
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
59 #include "lua.h"
60 #include "lauxlib.h"
61 #include "lualib.h"
62 #include "lfs.h"
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
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
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;
89 #define LOCK_METATABLE "lfs.lock"
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
115 ** This function changes the working (current) directory
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;
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
135 #ifndef PATH_MAX
136 #define PATH_MAX 4096
137 #endif
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;
146 else {
147 lua_pushstring(L, path);
148 return 1;
153 ** Check if the given element on the stack is a file and returns it.
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;
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.
181 Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.
183 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp
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);
192 if (!len) {
193 fseek (fh, 0L, SEEK_END);
194 len = ftell (fh);
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);
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);
218 #ifdef _WIN32
219 typedef struct lfs_Lock {
220 HANDLE fd;
221 } lfs_Lock;
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;
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;
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;
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;
255 #else
256 typedef struct lfs_Lock {
257 char *ln;
258 } lfs_Lock;
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;
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;
275 lock->ln = ln;
276 luaL_getmetatable (L, LOCK_METATABLE);
277 lua_setmetatable (L, -2);
278 return 1;
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;
287 return 0;
289 #endif
291 #ifdef _WIN32
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;
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;
317 #else
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;
323 #endif
325 static int lfs_f_setmode(lua_State *L) {
326 return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2);
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).
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;
353 ** Unlocks a file.
354 ** @param #1 File handle.
355 ** @param #2 Number with start position (optional).
356 ** @param #3 Number with length (optional).
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;
373 static int make_dir (lua_State *L) {
374 const char *path = luaL_checkstring (L, 1);
375 int fail;
376 #ifdef _WIN32
377 fail = _mkdir (path);
378 #else
379 fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
380 S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
381 #endif
382 if (fail) {
383 lua_pushnil (L);
384 lua_pushfstring (L, "%s", strerror(errno));
385 return 2;
387 lua_pushboolean (L, 1);
388 return 1;
392 ** Removes a directory.
393 ** @param #1 Directory path.
395 static int remove_dir (lua_State *L) {
396 const char *path = luaL_checkstring (L, 1);
397 int fail;
399 fail = rmdir (path);
401 if (fail) {
402 lua_pushnil (L);
403 lua_pushfstring (L, "%s", strerror(errno));
404 return 2;
406 lua_pushboolean (L, 1);
407 return 1;
411 ** Directory iterator
413 static int dir_iter (lua_State *L) {
414 #ifdef _WIN32
415 struct _finddata_t c_file;
416 #else
417 struct dirent *entry;
418 #endif
419 dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE);
420 luaL_argcheck (L, !d->closed, 1, "closed directory");
421 #ifdef _WIN32
422 if (d->hFile == 0L) { /* first entry */
423 if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) {
424 lua_pushnil (L);
425 lua_pushstring (L, strerror (errno));
426 return 2;
427 } else {
428 lua_pushstring (L, c_file.name);
429 return 1;
431 } else { /* next entry */
432 if (_findnext (d->hFile, &c_file) == -1L) {
433 /* no more entries => close directory */
434 _findclose (d->hFile);
435 d->closed = 1;
436 return 0;
437 } else {
438 lua_pushstring (L, c_file.name);
439 return 1;
442 #else
443 if ((entry = readdir (d->dir)) != NULL) {
444 lua_pushstring (L, entry->d_name);
445 return 1;
446 } else {
447 /* no more entries => close directory */
448 closedir (d->dir);
449 d->closed = 1;
450 return 0;
452 #endif
457 ** Closes directory iterators
459 static int dir_close (lua_State *L) {
460 dir_data *d = (dir_data *)lua_touserdata (L, 1);
461 #ifdef _WIN32
462 if (!d->closed && d->hFile) {
463 _findclose (d->hFile);
464 d->closed = 1;
466 #else
467 if (!d->closed && d->dir) {
468 closedir (d->dir);
469 d->closed = 1;
471 #endif
472 return 0;
477 ** Factory of directory iterators
479 static int dir_iter_factory (lua_State *L) {
480 const char *path = luaL_checkstring (L, 1);
481 dir_data *d;
482 lua_pushcfunction (L, dir_iter);
483 d = (dir_data *) lua_newuserdata (L, sizeof(dir_data));
484 d->closed = 0;
485 #ifdef _WIN32
486 d->hFile = 0L;
487 luaL_getmetatable (L, DIR_METATABLE);
488 lua_setmetatable (L, -2);
489 if (strlen(path) > MAX_DIR_LENGTH)
490 luaL_error (L, "path too long: %s", path);
491 else
492 sprintf (d->pattern, "%s/*", path);
493 #else
494 luaL_getmetatable (L, DIR_METATABLE);
495 lua_setmetatable (L, -2);
496 d->dir = opendir (path);
497 if (d->dir == NULL)
498 luaL_error (L, "cannot open %s: %s", path, strerror (errno));
499 #endif
500 return 2;
505 ** Creates directory metatable.
507 static int dir_create_meta (lua_State *L) {
508 luaL_newmetatable (L, DIR_METATABLE);
509 /* set its __gc field */
510 lua_pushstring (L, "__index");
511 lua_newtable(L);
512 lua_pushstring (L, "next");
513 lua_pushcfunction (L, dir_iter);
514 lua_settable(L, -3);
515 lua_pushstring (L, "close");
516 lua_pushcfunction (L, dir_close);
517 lua_settable(L, -3);
518 lua_settable (L, -3);
519 lua_pushstring (L, "__gc");
520 lua_pushcfunction (L, dir_close);
521 lua_settable (L, -3);
522 return 1;
526 ** Creates lock metatable.
528 static int lock_create_meta (lua_State *L) {
529 luaL_newmetatable (L, LOCK_METATABLE);
530 /* set its __gc field */
531 lua_newtable(L);
532 lua_pushcfunction(L, lfs_unlock_dir);
533 lua_setfield(L, -2, "free");
534 lua_setfield(L, -2, "__index");
535 lua_pushcfunction(L, lfs_unlock_dir);
536 lua_setfield(L, -2, "__gc");
537 return 1;
541 #ifdef _WIN32
542 #ifndef S_ISDIR
543 #define S_ISDIR(mode) (mode&_S_IFDIR)
544 #endif
545 #ifndef S_ISREG
546 #define S_ISREG(mode) (mode&_S_IFREG)
547 #endif
548 #ifndef S_ISLNK
549 #define S_ISLNK(mode) (0)
550 #endif
551 #ifndef S_ISSOCK
552 #define S_ISSOCK(mode) (0)
553 #endif
554 #ifndef S_ISFIFO
555 #define S_ISFIFO(mode) (0)
556 #endif
557 #ifndef S_ISCHR
558 #define S_ISCHR(mode) (mode&_S_IFCHR)
559 #endif
560 #ifndef S_ISBLK
561 #define S_ISBLK(mode) (0)
562 #endif
563 #endif
565 ** Convert the inode protection mode to a string.
567 #ifdef _WIN32
568 static const char *mode2string (unsigned short mode) {
569 #else
570 static const char *mode2string (mode_t mode) {
571 #endif
572 if ( S_ISREG(mode) )
573 return "file";
574 else if ( S_ISDIR(mode) )
575 return "directory";
576 else if ( S_ISLNK(mode) )
577 return "link";
578 else if ( S_ISSOCK(mode) )
579 return "socket";
580 else if ( S_ISFIFO(mode) )
581 return "named pipe";
582 else if ( S_ISCHR(mode) )
583 return "char device";
584 else if ( S_ISBLK(mode) )
585 return "block device";
586 else
587 return "other";
592 ** Set access time and modification values for file
594 static int file_utime (lua_State *L) {
595 const char *file = luaL_checkstring (L, 1);
596 struct utimbuf utb, *buf;
598 if (lua_gettop (L) == 1) /* set to current date/time */
599 buf = NULL;
600 else {
601 utb.actime = (time_t)luaL_optnumber (L, 2, 0);
602 utb.modtime = (time_t)luaL_optnumber (L, 3, utb.actime);
603 buf = &utb;
605 if (utime (file, buf)) {
606 lua_pushnil (L);
607 lua_pushfstring (L, "%s", strerror (errno));
608 return 2;
610 lua_pushboolean (L, 1);
611 return 1;
615 /* inode protection mode */
616 static void push_st_mode (lua_State *L, STAT_STRUCT *info) {
617 lua_pushstring (L, mode2string (info->st_mode));
619 /* device inode resides on */
620 static void push_st_dev (lua_State *L, STAT_STRUCT *info) {
621 lua_pushnumber (L, (lua_Number)info->st_dev);
623 /* inode's number */
624 static void push_st_ino (lua_State *L, STAT_STRUCT *info) {
625 lua_pushnumber (L, (lua_Number)info->st_ino);
627 /* number of hard links to the file */
628 static void push_st_nlink (lua_State *L, STAT_STRUCT *info) {
629 lua_pushnumber (L, (lua_Number)info->st_nlink);
631 /* user-id of owner */
632 static void push_st_uid (lua_State *L, STAT_STRUCT *info) {
633 lua_pushnumber (L, (lua_Number)info->st_uid);
635 /* group-id of owner */
636 static void push_st_gid (lua_State *L, STAT_STRUCT *info) {
637 lua_pushnumber (L, (lua_Number)info->st_gid);
639 /* device type, for special file inode */
640 static void push_st_rdev (lua_State *L, STAT_STRUCT *info) {
641 lua_pushnumber (L, (lua_Number)info->st_rdev);
643 /* time of last access */
644 static void push_st_atime (lua_State *L, STAT_STRUCT *info) {
645 lua_pushnumber (L, info->st_atime);
647 /* time of last data modification */
648 static void push_st_mtime (lua_State *L, STAT_STRUCT *info) {
649 lua_pushnumber (L, info->st_mtime);
651 /* time of last file status change */
652 static void push_st_ctime (lua_State *L, STAT_STRUCT *info) {
653 lua_pushnumber (L, info->st_ctime);
655 /* file size, in bytes */
656 static void push_st_size (lua_State *L, STAT_STRUCT *info) {
657 lua_pushnumber (L, (lua_Number)info->st_size);
659 #ifndef _WIN32
660 /* blocks allocated for file */
661 static void push_st_blocks (lua_State *L, STAT_STRUCT *info) {
662 lua_pushnumber (L, (lua_Number)info->st_blocks);
664 /* optimal file system I/O blocksize */
665 static void push_st_blksize (lua_State *L, STAT_STRUCT *info) {
666 lua_pushnumber (L, (lua_Number)info->st_blksize);
668 #endif
669 static void push_invalid (lua_State *L, STAT_STRUCT *info) {
670 luaL_error(L, "invalid attribute name");
671 #ifndef _WIN32
672 info->st_blksize = 0; /* never reached */
673 #endif
677 ** Convert the inode protection mode to a permission list.
680 #ifdef _WIN32
681 static const char *perm2string (unsigned short mode) {
682 static char perms[10] = "---------\0";
683 int i;
684 for (i=0;i<9;i++) perms[i]='-';
685 if (mode & _S_IREAD)
686 { perms[0] = 'r'; perms[3] = 'r'; perms[6] = 'r'; }
687 if (mode & _S_IWRITE)
688 { perms[1] = 'w'; perms[4] = 'w'; perms[7] = 'w'; }
689 if (mode & _S_IEXEC)
690 { perms[2] = 'x'; perms[5] = 'x'; perms[8] = 'x'; }
691 return perms;
693 #else
694 static const char *perm2string (mode_t mode) {
695 static char perms[10] = "---------\0";
696 int i;
697 for (i=0;i<9;i++) perms[i]='-';
698 if (mode & S_IRUSR) perms[0] = 'r';
699 if (mode & S_IWUSR) perms[1] = 'w';
700 if (mode & S_IXUSR) perms[2] = 'x';
701 if (mode & S_IRGRP) perms[3] = 'r';
702 if (mode & S_IWGRP) perms[4] = 'w';
703 if (mode & S_IXGRP) perms[5] = 'x';
704 if (mode & S_IROTH) perms[6] = 'r';
705 if (mode & S_IWOTH) perms[7] = 'w';
706 if (mode & S_IXOTH) perms[8] = 'x';
707 return perms;
709 #endif
711 /* permssions string */
712 static void push_st_perm (lua_State *L, STAT_STRUCT *info) {
713 lua_pushstring (L, perm2string (info->st_mode));
716 typedef void (*_push_function) (lua_State *L, STAT_STRUCT *info);
718 struct _stat_members {
719 const char *name;
720 _push_function push;
723 struct _stat_members members[] = {
724 { "mode", push_st_mode },
725 { "dev", push_st_dev },
726 { "ino", push_st_ino },
727 { "nlink", push_st_nlink },
728 { "uid", push_st_uid },
729 { "gid", push_st_gid },
730 { "rdev", push_st_rdev },
731 { "access", push_st_atime },
732 { "modification", push_st_mtime },
733 { "change", push_st_ctime },
734 { "size", push_st_size },
735 { "permissions", push_st_perm },
736 #ifndef _WIN32
737 { "blocks", push_st_blocks },
738 { "blksize", push_st_blksize },
739 #endif
740 { NULL, push_invalid }
744 ** Get file or symbolic link information
746 static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) {
747 int i;
748 STAT_STRUCT info;
749 const char *file = luaL_checkstring (L, 1);
751 if (st(file, &info)) {
752 lua_pushnil (L);
753 lua_pushfstring (L, "cannot obtain information from file `%s'", file);
754 return 2;
756 if (lua_isstring (L, 2)) {
757 int v;
758 const char *member = lua_tostring (L, 2);
759 if (strcmp (member, "mode") == 0) v = 0;
760 #ifndef _WIN32
761 else if (strcmp (member, "blocks") == 0) v = 11;
762 else if (strcmp (member, "blksize") == 0) v = 12;
763 #endif
764 else /* look for member */
765 for (v = 1; members[v].name; v++)
766 if (*members[v].name == *member)
767 break;
768 /* push member value and return */
769 members[v].push (L, &info);
770 return 1;
771 } else if (!lua_istable (L, 2))
772 /* creates a table if none is given */
773 lua_newtable (L);
774 /* stores all members in table on top of the stack */
775 for (i = 0; members[i].name; i++) {
776 lua_pushstring (L, members[i].name);
777 members[i].push (L, &info);
778 lua_rawset (L, -3);
780 return 1;
785 ** Get file information using stat.
787 static int file_info (lua_State *L) {
788 return _file_info_ (L, STAT_FUNC);
793 ** Get symbolic link information using lstat.
795 #ifndef _WIN32
796 static int link_info (lua_State *L) {
797 return _file_info_ (L, LSTAT_FUNC);
799 #else
800 static int link_info (lua_State *L) {
801 lua_pushboolean(L, 0);
802 lua_pushliteral(L, "symlinkattributes not supported on this platform");
803 return 2;
805 #endif
809 ** Assumes the table is on top of the stack.
811 static void set_info (lua_State *L) {
812 lua_pushliteral (L, "_COPYRIGHT");
813 lua_pushliteral (L, "Copyright (C) 2003-2009 Kepler Project");
814 lua_settable (L, -3);
815 lua_pushliteral (L, "_DESCRIPTION");
816 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");
817 lua_settable (L, -3);
818 lua_pushliteral (L, "_VERSION");
819 lua_pushliteral (L, "LuaFileSystem 1.5.0");
820 lua_settable (L, -3);
824 static const struct luaL_Reg fslib[] = {
825 {"attributes", file_info},
826 {"chdir", change_dir},
827 {"currentdir", get_dir},
828 {"dir", dir_iter_factory},
829 {"lock", file_lock},
830 {"mkdir", make_dir},
831 {"rmdir", remove_dir},
832 {"symlinkattributes", link_info},
833 {"setmode", lfs_f_setmode},
834 {"touch", file_utime},
835 {"unlock", file_unlock},
836 {"lock_dir", lfs_lock_dir},
837 {NULL, NULL},
840 int luaopen_lfs (lua_State *L) {
841 dir_create_meta (L);
842 lock_create_meta (L);
843 luaL_register (L, "lfs", fslib);
844 set_info (L);
845 return 1;