3 ** Copyright Kepler Project 2003 (http://www.keplerproject.org/luafilesystem)
5 ** File system manipulation library.
6 ** This library offers these functions:
7 ** lfs.attributes (filepath [, attributename])
11 ** lfs.lock (fh, mode)
12 ** lfs.lock_dir (path)
15 ** lfs.setmode (filepath, mode)
16 ** lfs.symlinkattributes (filepath [, attributename]) -- thanks to Sam Roberts
17 ** lfs.touch (filepath [, atime [, mtime]])
20 ** $Id: lfs.c,v 1.61 2009/07/04 02:10:16 mascarenhas Exp $
25 #define _FILE_OFFSET_BITS 64 /* Linux, Solaris and HP-UX */
27 #define _LARGE_FILES 1 /* AIX */
31 #define _LARGEFILE64_SOURCE
44 #include <sys/locking.h>
48 #include <sys/utime.h>
55 #include <sys/types.h>
65 #define LFS_VERSION "1.6.2"
66 #define LFS_LIBNAME "lfs"
68 #if LUA_VERSION_NUM < 502
69 # define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
72 /* Define 'strerror' for systems that do not implement it */
74 #define strerror(_) "System unable to describe the error"
77 /* Define 'getcwd' for systems that do not implement it */
79 #define getcwd(p,s) NULL
80 #define getcwd_error "Function 'getcwd' not provided by system"
82 #define getcwd_error strerror(errno)
84 /* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */
85 #define LFS_MAXPATHLEN MAX_PATH
88 #include <sys/param.h>
89 #define LFS_MAXPATHLEN MAXPATHLEN
93 #define DIR_METATABLE "directory metatable"
94 typedef struct dir_data {
98 char pattern[MAX_PATH+1];
104 #define LOCK_METATABLE "lock metatable"
108 #define lfs_setmode(L,file,m) ((void)L, setmode(_fileno(file), m))
109 #define STAT_STRUCT struct stati64
111 #define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m))
112 #define STAT_STRUCT struct _stati64
114 #define STAT_FUNC _stati64
115 #define LSTAT_FUNC STAT_FUNC
119 #define lfs_setmode(L,file,m) ((void)L, (void)file, (void)m, 0)
120 #define STAT_STRUCT struct stat
121 #define STAT_FUNC stat
122 #define LSTAT_FUNC lstat
128 static int pusherror(lua_State *L, const char *info)
132 lua_pushstring(L, strerror(errno));
134 lua_pushfstring(L, "%s: %s", info, strerror(errno));
135 lua_pushinteger(L, errno);
139 static int pushresult(lua_State *L, int i, const char *info)
142 return pusherror(L, info);
143 lua_pushinteger(L, i);
149 ** This function changes the working (current) directory
151 static int change_dir (lua_State *L) {
152 const char *path = luaL_checkstring(L, 1);
155 lua_pushfstring (L,"Unable to change working directory to '%s'\n%s\n",
159 lua_pushboolean (L, 1);
165 ** This function returns the current directory
166 ** If unable to get the current directory, it returns nil
167 ** and a string describing the error
169 static int get_dir (lua_State *L) {
171 /* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. */
172 char buf[LFS_MAXPATHLEN];
173 if ((path = getcwd(buf, LFS_MAXPATHLEN)) == NULL) {
175 lua_pushstring(L, getcwd_error);
179 lua_pushstring(L, path);
185 ** Check if the given element on the stack is a file and returns it.
187 static FILE *check_file (lua_State *L, int idx, const char *funcname) {
188 FILE **fh = (FILE **)luaL_checkudata (L, idx, "FILE*");
190 luaL_error (L, "%s: not a file", funcname);
192 } else if (*fh == NULL) {
193 luaL_error (L, "%s: closed file", funcname);
203 static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long start, long len, const char *funcname) {
206 /* lkmode valid values are:
207 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.
208 LK_NBLCK Locks the specified bytes. If the bytes cannot be locked, the constant returns an error.
209 LK_NBRLCK Same as _LK_NBLCK.
210 LK_RLCK Same as _LK_LOCK.
211 LK_UNLCK Unlocks the specified bytes, which must have been previously locked.
213 Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.
215 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp
219 case 'r': lkmode = LK_NBLCK; break;
220 case 'w': lkmode = LK_NBLCK; break;
221 case 'u': lkmode = LK_UNLCK; break;
222 default : return luaL_error (L, "%s: invalid mode", funcname);
225 fseek (fh, 0L, SEEK_END);
228 fseek (fh, start, SEEK_SET);
230 code = locking (fileno(fh), lkmode, len);
232 code = _locking (fileno(fh), lkmode, len);
237 case 'w': f.l_type = F_WRLCK; break;
238 case 'r': f.l_type = F_RDLCK; break;
239 case 'u': f.l_type = F_UNLCK; break;
240 default : return luaL_error (L, "%s: invalid mode", funcname);
242 f.l_whence = SEEK_SET;
243 f.l_start = (off_t)start;
244 f.l_len = (off_t)len;
245 code = fcntl (fileno(fh), F_SETLK, &f);
251 typedef struct lfs_Lock {
254 static int lfs_lock_dir(lua_State *L) {
255 size_t pathl; HANDLE fd;
258 const char *lockfile = "/lockfile.lfs";
259 const char *path = luaL_checklstring(L, 1, &pathl);
260 ln = (char*)malloc(pathl + strlen(lockfile) + 1);
262 lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2;
264 strcpy(ln, path); strcat(ln, lockfile);
265 if((fd = CreateFile(ln, GENERIC_WRITE, 0, NULL, CREATE_NEW,
266 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL)) == INVALID_HANDLE_VALUE) {
267 int en = GetLastError();
268 free(ln); lua_pushnil(L);
269 if(en == ERROR_FILE_EXISTS || en == ERROR_SHARING_VIOLATION)
270 lua_pushstring(L, "File exists");
272 lua_pushstring(L, strerror(en));
276 lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock));
278 luaL_getmetatable (L, LOCK_METATABLE);
279 lua_setmetatable (L, -2);
282 static int lfs_unlock_dir(lua_State *L) {
283 lfs_Lock *lock = luaL_checkudata(L, 1, LOCK_METATABLE);
284 CloseHandle(lock->fd);
288 typedef struct lfs_Lock {
291 static int lfs_lock_dir(lua_State *L) {
295 const char *lockfile = "/lockfile.lfs";
296 const char *path = luaL_checklstring(L, 1, &pathl);
297 lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock));
298 ln = (char*)malloc(pathl + strlen(lockfile) + 1);
300 lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2;
302 strcpy(ln, path); strcat(ln, lockfile);
303 if(symlink("lock", ln) == -1) {
304 free(ln); lua_pushnil(L);
305 lua_pushstring(L, strerror(errno)); return 2;
308 luaL_getmetatable (L, LOCK_METATABLE);
309 lua_setmetatable (L, -2);
312 static int lfs_unlock_dir(lua_State *L) {
313 lfs_Lock *lock = luaL_checkudata(L, 1, LOCK_METATABLE);
323 static int lfs_g_setmode (lua_State *L, FILE *f, int arg) {
324 static const int mode[] = {_O_BINARY, _O_TEXT};
325 static const char *const modenames[] = {"binary", "text", NULL};
326 int op = luaL_checkoption(L, arg, NULL, modenames);
327 int res = lfs_setmode(L, f, mode[op]);
330 lua_pushboolean(L, 1);
331 for (i = 0; modenames[i] != NULL; i++) {
332 if (mode[i] == res) {
333 lua_pushstring(L, modenames[i]);
343 lua_pushfstring(L, "%s", strerror(en));
344 lua_pushinteger(L, en);
349 static int lfs_f_setmode(lua_State *L) {
350 return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2);
355 ** @param #1 File handle.
356 ** @param #2 String with lock mode ('w'rite, 'r'ead).
357 ** @param #3 Number with start position (optional).
358 ** @param #4 Number with length (optional).
360 static int file_lock (lua_State *L) {
361 FILE *fh = check_file (L, 1, "lock");
362 const char *mode = luaL_checkstring (L, 2);
363 const long start = luaL_optlong (L, 3, 0);
364 long len = luaL_optlong (L, 4, 0);
365 if (_file_lock (L, fh, mode, start, len, "lock")) {
366 lua_pushboolean (L, 1);
370 lua_pushfstring (L, "%s", strerror(errno));
378 ** @param #1 File handle.
379 ** @param #2 Number with start position (optional).
380 ** @param #3 Number with length (optional).
382 static int file_unlock (lua_State *L) {
383 FILE *fh = check_file (L, 1, "unlock");
384 const long start = luaL_optlong (L, 2, 0);
385 long len = luaL_optlong (L, 3, 0);
386 if (_file_lock (L, fh, "u", start, len, "unlock")) {
387 lua_pushboolean (L, 1);
391 lua_pushfstring (L, "%s", strerror(errno));
399 ** @param #1 Object to link to.
400 ** @param #2 Name of link.
401 ** @param #3 True if link is symbolic (optional).
403 static int make_link(lua_State *L)
406 const char *oldpath = luaL_checkstring(L, 1);
407 const char *newpath = luaL_checkstring(L, 2);
409 (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL);
411 pusherror(L, "make_link is not supported on Windows");
417 ** Creates a directory.
418 ** @param #1 Directory path.
420 static int make_dir (lua_State *L) {
421 const char *path = luaL_checkstring (L, 1);
424 fail = _mkdir (path);
426 fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
427 S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
431 lua_pushfstring (L, "%s", strerror(errno));
434 lua_pushboolean (L, 1);
439 ** Removes a directory.
440 ** @param #1 Directory path.
442 static int remove_dir (lua_State *L) {
443 const char *path = luaL_checkstring (L, 1);
450 lua_pushfstring (L, "%s", strerror(errno));
453 lua_pushboolean (L, 1);
458 ** Directory iterator
460 static int dir_iter (lua_State *L) {
462 struct _finddata_t c_file;
464 struct dirent *entry;
466 dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE);
467 luaL_argcheck (L, d->closed == 0, 1, "closed directory");
469 if (d->hFile == 0L) { /* first entry */
470 if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) {
472 lua_pushstring (L, strerror (errno));
476 lua_pushstring (L, c_file.name);
479 } else { /* next entry */
480 if (_findnext (d->hFile, &c_file) == -1L) {
481 /* no more entries => close directory */
482 _findclose (d->hFile);
486 lua_pushstring (L, c_file.name);
491 if ((entry = readdir (d->dir)) != NULL) {
492 lua_pushstring (L, entry->d_name);
495 /* no more entries => close directory */
505 ** Closes directory iterators
507 static int dir_close (lua_State *L) {
508 dir_data *d = (dir_data *)lua_touserdata (L, 1);
510 if (!d->closed && d->hFile) {
511 _findclose (d->hFile);
514 if (!d->closed && d->dir) {
524 ** Factory of directory iterators
526 static int dir_iter_factory (lua_State *L) {
527 const char *path = luaL_checkstring (L, 1);
529 lua_pushcfunction (L, dir_iter);
530 d = (dir_data *) lua_newuserdata (L, sizeof(dir_data));
531 luaL_getmetatable (L, DIR_METATABLE);
532 lua_setmetatable (L, -2);
536 if (strlen(path) > MAX_PATH-2)
537 luaL_error (L, "path too long: %s", path);
539 sprintf (d->pattern, "%s/*", path);
541 d->dir = opendir (path);
543 luaL_error (L, "cannot open %s: %s", path, strerror (errno));
550 ** Creates directory metatable.
552 static int dir_create_meta (lua_State *L) {
553 luaL_newmetatable (L, DIR_METATABLE);
557 lua_pushcfunction (L, dir_iter);
558 lua_setfield(L, -2, "next");
559 lua_pushcfunction (L, dir_close);
560 lua_setfield(L, -2, "close");
563 lua_setfield(L, -2, "__index");
564 lua_pushcfunction (L, dir_close);
565 lua_setfield (L, -2, "__gc");
570 ** Creates lock metatable.
572 static int lock_create_meta (lua_State *L) {
573 luaL_newmetatable (L, LOCK_METATABLE);
577 lua_pushcfunction(L, lfs_unlock_dir);
578 lua_setfield(L, -2, "free");
581 lua_setfield(L, -2, "__index");
582 lua_pushcfunction(L, lfs_unlock_dir);
583 lua_setfield(L, -2, "__gc");
590 #define S_ISDIR(mode) (mode&_S_IFDIR)
593 #define S_ISREG(mode) (mode&_S_IFREG)
596 #define S_ISLNK(mode) (0)
599 #define S_ISSOCK(mode) (0)
602 #define S_ISFIFO(mode) (0)
605 #define S_ISCHR(mode) (mode&_S_IFCHR)
608 #define S_ISBLK(mode) (0)
612 ** Convert the inode protection mode to a string.
615 static const char *mode2string (unsigned short mode) {
617 static const char *mode2string (mode_t mode) {
621 else if ( S_ISDIR(mode) )
623 else if ( S_ISLNK(mode) )
625 else if ( S_ISSOCK(mode) )
627 else if ( S_ISFIFO(mode) )
629 else if ( S_ISCHR(mode) )
630 return "char device";
631 else if ( S_ISBLK(mode) )
632 return "block device";
639 ** Set access time and modification values for file
641 static int file_utime (lua_State *L) {
642 const char *file = luaL_checkstring (L, 1);
643 struct utimbuf utb, *buf;
645 if (lua_gettop (L) == 1) /* set to current date/time */
648 utb.actime = (time_t)luaL_optnumber (L, 2, 0);
649 utb.modtime = (time_t)luaL_optnumber (L, 3, utb.actime);
652 if (utime (file, buf)) {
654 lua_pushfstring (L, "%s", strerror (errno));
657 lua_pushboolean (L, 1);
662 /* inode protection mode */
663 static void push_st_mode (lua_State *L, STAT_STRUCT *info) {
664 lua_pushstring (L, mode2string (info->st_mode));
666 /* device inode resides on */
667 static void push_st_dev (lua_State *L, STAT_STRUCT *info) {
668 lua_pushnumber (L, (lua_Number)info->st_dev);
671 static void push_st_ino (lua_State *L, STAT_STRUCT *info) {
672 lua_pushnumber (L, (lua_Number)info->st_ino);
674 /* number of hard links to the file */
675 static void push_st_nlink (lua_State *L, STAT_STRUCT *info) {
676 lua_pushnumber (L, (lua_Number)info->st_nlink);
678 /* user-id of owner */
679 static void push_st_uid (lua_State *L, STAT_STRUCT *info) {
680 lua_pushnumber (L, (lua_Number)info->st_uid);
682 /* group-id of owner */
683 static void push_st_gid (lua_State *L, STAT_STRUCT *info) {
684 lua_pushnumber (L, (lua_Number)info->st_gid);
686 /* device type, for special file inode */
687 static void push_st_rdev (lua_State *L, STAT_STRUCT *info) {
688 lua_pushnumber (L, (lua_Number)info->st_rdev);
690 /* time of last access */
691 static void push_st_atime (lua_State *L, STAT_STRUCT *info) {
692 lua_pushnumber (L, info->st_atime);
694 /* time of last data modification */
695 static void push_st_mtime (lua_State *L, STAT_STRUCT *info) {
696 lua_pushnumber (L, info->st_mtime);
698 /* time of last file status change */
699 static void push_st_ctime (lua_State *L, STAT_STRUCT *info) {
700 lua_pushnumber (L, info->st_ctime);
702 /* file size, in bytes */
703 static void push_st_size (lua_State *L, STAT_STRUCT *info) {
704 lua_pushnumber (L, (lua_Number)info->st_size);
707 /* blocks allocated for file */
708 static void push_st_blocks (lua_State *L, STAT_STRUCT *info) {
709 lua_pushnumber (L, (lua_Number)info->st_blocks);
711 /* optimal file system I/O blocksize */
712 static void push_st_blksize (lua_State *L, STAT_STRUCT *info) {
713 lua_pushnumber (L, (lua_Number)info->st_blksize);
716 static void push_invalid (lua_State *L, STAT_STRUCT *info) {
717 luaL_error(L, "invalid attribute name");
719 info->st_blksize = 0; /* never reached */
724 ** Convert the inode protection mode to a permission list.
728 static const char *perm2string (unsigned short mode) {
729 static char perms[10] = "---------\0";
731 for (i=0;i<9;i++) perms[i]='-';
733 { perms[0] = 'r'; perms[3] = 'r'; perms[6] = 'r'; }
734 if (mode & _S_IWRITE)
735 { perms[1] = 'w'; perms[4] = 'w'; perms[7] = 'w'; }
737 { perms[2] = 'x'; perms[5] = 'x'; perms[8] = 'x'; }
741 static const char *perm2string (mode_t mode) {
742 static char perms[10] = "---------\0";
744 for (i=0;i<9;i++) perms[i]='-';
745 if (mode & S_IRUSR) perms[0] = 'r';
746 if (mode & S_IWUSR) perms[1] = 'w';
747 if (mode & S_IXUSR) perms[2] = 'x';
748 if (mode & S_IRGRP) perms[3] = 'r';
749 if (mode & S_IWGRP) perms[4] = 'w';
750 if (mode & S_IXGRP) perms[5] = 'x';
751 if (mode & S_IROTH) perms[6] = 'r';
752 if (mode & S_IWOTH) perms[7] = 'w';
753 if (mode & S_IXOTH) perms[8] = 'x';
758 /* permssions string */
759 static void push_st_perm (lua_State *L, STAT_STRUCT *info) {
760 lua_pushstring (L, perm2string (info->st_mode));
763 typedef void (*_push_function) (lua_State *L, STAT_STRUCT *info);
765 struct _stat_members {
770 struct _stat_members members[] = {
771 { "mode", push_st_mode },
772 { "dev", push_st_dev },
773 { "ino", push_st_ino },
774 { "nlink", push_st_nlink },
775 { "uid", push_st_uid },
776 { "gid", push_st_gid },
777 { "rdev", push_st_rdev },
778 { "access", push_st_atime },
779 { "modification", push_st_mtime },
780 { "change", push_st_ctime },
781 { "size", push_st_size },
782 { "permissions", push_st_perm },
784 { "blocks", push_st_blocks },
785 { "blksize", push_st_blksize },
787 { NULL, push_invalid }
791 ** Get file or symbolic link information
793 static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) {
796 const char *file = luaL_checkstring (L, 1);
798 if (st(file, &info)) {
800 lua_pushfstring (L, "cannot obtain information from file `%s'", file);
803 if (lua_isstring (L, 2)) {
805 const char *member = lua_tostring (L, 2);
806 if (strcmp (member, "mode") == 0) v = 0;
808 else if (strcmp (member, "blocks") == 0) v = 11;
809 else if (strcmp (member, "blksize") == 0) v = 12;
811 else /* look for member */
812 for (v = 1; members[v].name; v++)
813 if (*members[v].name == *member)
815 /* push member value and return */
816 members[v].push (L, &info);
818 } else if (!lua_istable (L, 2))
819 /* creates a table if none is given */
821 /* stores all members in table on top of the stack */
822 for (i = 0; members[i].name; i++) {
823 lua_pushstring (L, members[i].name);
824 members[i].push (L, &info);
832 ** Get file information using stat.
834 static int file_info (lua_State *L) {
835 return _file_info_ (L, STAT_FUNC);
840 ** Get symbolic link information using lstat.
842 static int link_info (lua_State *L) {
843 return _file_info_ (L, LSTAT_FUNC);
848 ** Assumes the table is on top of the stack.
850 static void set_info (lua_State *L) {
851 lua_pushliteral (L, "_COPYRIGHT");
852 lua_pushliteral (L, "Copyright (C) 2003-2012 Kepler Project");
853 lua_settable (L, -3);
854 lua_pushliteral (L, "_DESCRIPTION");
855 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");
856 lua_settable (L, -3);
857 lua_pushliteral (L, "_VERSION");
858 lua_pushliteral (L, "LuaFileSystem "LFS_VERSION);
859 lua_settable (L, -3);
863 static const struct luaL_Reg fslib[] = {
864 {"attributes", file_info},
865 {"chdir", change_dir},
866 {"currentdir", get_dir},
867 {"dir", dir_iter_factory},
871 {"rmdir", remove_dir},
872 {"symlinkattributes", link_info},
873 {"setmode", lfs_f_setmode},
874 {"touch", file_utime},
875 {"unlock", file_unlock},
876 {"lock_dir", lfs_lock_dir},
880 int luaopen_lfs (lua_State *L) {
882 lock_create_meta (L);
883 luaL_newlib (L, fslib);
884 lua_pushvalue(L, -1);
885 lua_setglobal(L, LFS_LIBNAME);