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>
64 /* Define 'strerror' for systems that do not implement it */
66 #define strerror(_) "System unable to describe the error"
69 /* Define 'getcwd' for systems that do not implement it */
71 #define getcwd(p,s) NULL
72 #define getcwd_error "Function 'getcwd' not provided by system"
74 #define getcwd_error strerror(errno)
77 #define DIR_METATABLE "lfs.directory"
78 #define MAX_DIR_LENGTH 1023
79 typedef struct dir_data
{
83 char pattern
[MAX_DIR_LENGTH
+1];
89 #define LOCK_METATABLE "lfs.lock"
93 #define lfs_setmode(L,file,m) ((void)L, setmode(_fileno(file), m))
94 #define STAT_STRUCT struct stati64
96 #define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m))
97 #define STAT_STRUCT struct _stati64
99 #define STAT_FUNC _stati64
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
115 ** This function changes the working (current) directory
117 static int change_dir (lua_State
*L
) {
118 const char *path
= luaL_checkstring(L
, 1);
121 lua_pushfstring (L
,"Unable to change working directory to '%s'\n%s\n",
125 lua_pushboolean (L
, 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
136 #define PATH_MAX 4096
139 static int get_dir (lua_State
*L
) {
141 if (getcwd((char *)path
, PATH_MAX
) == NULL
) {
143 lua_pushstring(L
, getcwd_error
);
147 lua_pushstring(L
, path
);
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*");
158 luaL_error (L
, "%s: not a file", funcname
);
160 } else if (*fh
== NULL
) {
161 luaL_error (L
, "%s: closed file", funcname
);
171 static int _file_lock (lua_State
*L
, FILE *fh
, const char *mode
, const long start
, long len
, const char *funcname
) {
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
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
);
193 fseek (fh
, 0L, SEEK_END
);
196 fseek (fh
, start
, SEEK_SET
);
198 code
= locking (fileno(fh
), lkmode
, len
);
200 code
= _locking (fileno(fh
), lkmode
, len
);
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
);
219 typedef struct lfs_Lock
{
222 static int lfs_lock_dir(lua_State
*L
) {
223 size_t pathl
; HANDLE fd
;
226 const char *lockfile
= "/lockfile.lfs";
227 const char *path
= luaL_checklstring(L
, 1, &pathl
);
228 ln
= (char*)malloc(pathl
+ strlen(lockfile
) + 1);
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");
240 lua_pushstring(L
, strerror(en
));
244 lock
= (lfs_Lock
*)lua_newuserdata(L
, sizeof(lfs_Lock
));
246 luaL_getmetatable (L
, LOCK_METATABLE
);
247 lua_setmetatable (L
, -2);
250 static int lfs_unlock_dir(lua_State
*L
) {
251 lfs_Lock
*lock
= luaL_checkudata(L
, 1, LOCK_METATABLE
);
252 CloseHandle(lock
->fd
);
256 typedef struct lfs_Lock
{
259 static int lfs_lock_dir(lua_State
*L
) {
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);
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;
276 luaL_getmetatable (L
, LOCK_METATABLE
);
277 lua_setmetatable (L
, -2);
280 static int lfs_unlock_dir(lua_State
*L
) {
281 lfs_Lock
*lock
= luaL_checkudata(L
, 1, LOCK_METATABLE
);
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
]);
299 lua_pushboolean(L
, 1);
300 for (i
= 0; modenames
[i
] != NULL
; i
++) {
301 if (mode
[i
] == res
) {
302 lua_pushstring(L
, modenames
[i
]);
312 lua_pushfstring(L
, "%s", strerror(en
));
313 lua_pushinteger(L
, en
);
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");
325 static int lfs_f_setmode(lua_State
*L
) {
326 return lfs_g_setmode(L
, check_file(L
, 1, "setmode"), 2);
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);
346 lua_pushfstring (L
, "%s", strerror(errno
));
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);
367 lua_pushfstring (L
, "%s", strerror(errno
));
373 static int make_dir (lua_State
*L
) {
374 const char *path
= luaL_checkstring (L
, 1);
377 fail
= _mkdir (path
);
379 fail
= mkdir (path
, S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
|
380 S_IWGRP
| S_IXGRP
| S_IROTH
| S_IXOTH
);
384 lua_pushfstring (L
, "%s", strerror(errno
));
387 lua_pushboolean (L
, 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);
403 lua_pushfstring (L
, "%s", strerror(errno
));
406 lua_pushboolean (L
, 1);
411 ** Directory iterator
413 static int dir_iter (lua_State
*L
) {
415 struct _finddata_t c_file
;
417 struct dirent
*entry
;
419 dir_data
*d
= (dir_data
*)luaL_checkudata (L
, 1, DIR_METATABLE
);
420 luaL_argcheck (L
, !d
->closed
, 1, "closed directory");
422 if (d
->hFile
== 0L) { /* first entry */
423 if ((d
->hFile
= _findfirst (d
->pattern
, &c_file
)) == -1L) {
425 lua_pushstring (L
, strerror (errno
));
428 lua_pushstring (L
, c_file
.name
);
431 } else { /* next entry */
432 if (_findnext (d
->hFile
, &c_file
) == -1L) {
433 /* no more entries => close directory */
434 _findclose (d
->hFile
);
438 lua_pushstring (L
, c_file
.name
);
443 if ((entry
= readdir (d
->dir
)) != NULL
) {
444 lua_pushstring (L
, entry
->d_name
);
447 /* no more entries => close directory */
457 ** Closes directory iterators
459 static int dir_close (lua_State
*L
) {
460 dir_data
*d
= (dir_data
*)lua_touserdata (L
, 1);
462 if (!d
->closed
&& d
->hFile
) {
463 _findclose (d
->hFile
);
467 if (!d
->closed
&& d
->dir
) {
477 ** Factory of directory iterators
479 static int dir_iter_factory (lua_State
*L
) {
480 const char *path
= luaL_checkstring (L
, 1);
482 lua_pushcfunction (L
, dir_iter
);
483 d
= (dir_data
*) lua_newuserdata (L
, sizeof(dir_data
));
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
);
492 sprintf (d
->pattern
, "%s/*", path
);
494 luaL_getmetatable (L
, DIR_METATABLE
);
495 lua_setmetatable (L
, -2);
496 d
->dir
= opendir (path
);
498 luaL_error (L
, "cannot open %s: %s", path
, strerror (errno
));
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");
512 lua_pushstring (L
, "next");
513 lua_pushcfunction (L
, dir_iter
);
515 lua_pushstring (L
, "close");
516 lua_pushcfunction (L
, dir_close
);
518 lua_settable (L
, -3);
519 lua_pushstring (L
, "__gc");
520 lua_pushcfunction (L
, dir_close
);
521 lua_settable (L
, -3);
526 ** Creates lock metatable.
528 static int lock_create_meta (lua_State
*L
) {
529 luaL_newmetatable (L
, LOCK_METATABLE
);
530 /* set its __gc field */
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");
543 #define S_ISDIR(mode) (mode&_S_IFDIR)
546 #define S_ISREG(mode) (mode&_S_IFREG)
549 #define S_ISLNK(mode) (0)
552 #define S_ISSOCK(mode) (0)
555 #define S_ISFIFO(mode) (0)
558 #define S_ISCHR(mode) (mode&_S_IFCHR)
561 #define S_ISBLK(mode) (0)
565 ** Convert the inode protection mode to a string.
568 static const char *mode2string (unsigned short mode
) {
570 static const char *mode2string (mode_t mode
) {
574 else if ( S_ISDIR(mode
) )
576 else if ( S_ISLNK(mode
) )
578 else if ( S_ISSOCK(mode
) )
580 else if ( S_ISFIFO(mode
) )
582 else if ( S_ISCHR(mode
) )
583 return "char device";
584 else if ( S_ISBLK(mode
) )
585 return "block device";
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 */
601 utb
.actime
= (time_t)luaL_optnumber (L
, 2, 0);
602 utb
.modtime
= (time_t)luaL_optnumber (L
, 3, utb
.actime
);
605 if (utime (file
, buf
)) {
607 lua_pushfstring (L
, "%s", strerror (errno
));
610 lua_pushboolean (L
, 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
);
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
);
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
);
669 static void push_invalid (lua_State
*L
, STAT_STRUCT
*info
) {
670 luaL_error(L
, "invalid attribute name");
672 info
->st_blksize
= 0; /* never reached */
677 ** Convert the inode protection mode to a permission list.
681 static const char *perm2string (unsigned short mode
) {
682 static char perms
[10] = "---------\0";
684 for (i
=0;i
<9;i
++) perms
[i
]='-';
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'; }
690 { perms
[2] = 'x'; perms
[5] = 'x'; perms
[8] = 'x'; }
694 static const char *perm2string (mode_t mode
) {
695 static char perms
[10] = "---------\0";
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';
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
{
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
},
737 { "blocks", push_st_blocks
},
738 { "blksize", push_st_blksize
},
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
*)) {
749 const char *file
= luaL_checkstring (L
, 1);
751 if (st(file
, &info
)) {
753 lua_pushfstring (L
, "cannot obtain information from file `%s'", file
);
756 if (lua_isstring (L
, 2)) {
758 const char *member
= lua_tostring (L
, 2);
759 if (strcmp (member
, "mode") == 0) v
= 0;
761 else if (strcmp (member
, "blocks") == 0) v
= 11;
762 else if (strcmp (member
, "blksize") == 0) v
= 12;
764 else /* look for member */
765 for (v
= 1; members
[v
].name
; v
++)
766 if (*members
[v
].name
== *member
)
768 /* push member value and return */
769 members
[v
].push (L
, &info
);
771 } else if (!lua_istable (L
, 2))
772 /* creates a table if none is given */
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
);
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.
796 static int link_info (lua_State
*L
) {
797 return _file_info_ (L
, LSTAT_FUNC
);
800 static int link_info (lua_State
*L
) {
801 lua_pushboolean(L
, 0);
802 lua_pushliteral(L
, "symlinkattributes not supported on this platform");
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
},
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
},
840 int luaopen_lfs (lua_State
*L
) {
842 lock_create_meta (L
);
843 luaL_register (L
, "lfs", fslib
);