mirror of
https://github.com/lunarmodules/luafilesystem.git
synced 2026-07-22 02:53:06 +00:00
indent -kr -nut -i2 src/lfs.c src/lfs.h
This commit is contained in:
312
src/lfs.c
312
src/lfs.c
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
** LuaFileSystem
|
||||
** Copyright Kepler Project 2003 - 2017 (http://keplerproject.github.io/luafilesystem)
|
||||
** Copyright Kepler Project 2003 - 2020
|
||||
** (http://keplerproject.github.io/luafilesystem)
|
||||
**
|
||||
** File system manipulation library.
|
||||
** This library offers these functions:
|
||||
@@ -45,31 +46,39 @@
|
||||
#include <sys/stat.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include <direct.h>
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#include <sys/locking.h>
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#include <utime.h>
|
||||
#else
|
||||
#include <sys/utime.h>
|
||||
#endif
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
/* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */
|
||||
#define LFS_MAXPATHLEN MAX_PATH
|
||||
|
||||
#else
|
||||
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <utime.h>
|
||||
#include <sys/param.h> /* for MAXPATHLEN */
|
||||
|
||||
#ifdef MAXPATHLEN
|
||||
#define LFS_MAXPATHLEN MAXPATHLEN
|
||||
#else
|
||||
#include <limits.h> /* for _POSIX_PATH_MAX */
|
||||
#define LFS_MAXPATHLEN _POSIX_PATH_MAX
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#include <lua.h>
|
||||
@@ -114,6 +123,7 @@ typedef struct dir_data {
|
||||
#define LOCK_METATABLE "lock metatable"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#ifdef __BORLANDC__
|
||||
#define lfs_setmode(file, m) (setmode(_fileno(file), m))
|
||||
#define STAT_STRUCT struct stati64
|
||||
@@ -121,6 +131,7 @@ typedef struct dir_data {
|
||||
#define lfs_setmode(file, m) (_setmode(_fileno(file), m))
|
||||
#define STAT_STRUCT struct _stati64
|
||||
#endif
|
||||
|
||||
#ifndef _S_IFLNK
|
||||
#define _S_IFLNK 0x400
|
||||
#endif
|
||||
@@ -149,13 +160,16 @@ typedef struct dir_data {
|
||||
|
||||
#define STAT_FUNC _stati64
|
||||
#define LSTAT_FUNC lfs_win32_lstat
|
||||
|
||||
#else
|
||||
|
||||
#define _O_TEXT 0
|
||||
#define _O_BINARY 0
|
||||
#define lfs_setmode(file, m) ((void)file, (void)m, 0)
|
||||
#define STAT_STRUCT struct stat
|
||||
#define STAT_FUNC stat
|
||||
#define LSTAT_FUNC lstat
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -211,6 +225,7 @@ int lfs_win32_lstat(const char *path, STAT_STRUCT *buffer)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
@@ -227,7 +242,8 @@ static int pusherror(lua_State *L, const char *info)
|
||||
return 3;
|
||||
}
|
||||
|
||||
static int pushresult(lua_State *L, int res, const char *info) {
|
||||
static int pushresult(lua_State * L, int res, const char *info)
|
||||
{
|
||||
if (res == -1) {
|
||||
return pusherror(L, info);
|
||||
} else {
|
||||
@@ -240,7 +256,8 @@ static int pushresult(lua_State *L, int res, const char *info) {
|
||||
/*
|
||||
** This function changes the working (current) directory
|
||||
*/
|
||||
static int change_dir (lua_State *L) {
|
||||
static int change_dir(lua_State * L)
|
||||
{
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
if (chdir(path)) {
|
||||
lua_pushnil(L);
|
||||
@@ -258,19 +275,21 @@ static int change_dir (lua_State *L) {
|
||||
** If unable to get the current directory, it returns nil
|
||||
** and a string describing the error
|
||||
*/
|
||||
static int get_dir (lua_State *L) {
|
||||
static int get_dir(lua_State * L)
|
||||
{
|
||||
#ifdef NO_GETCWD
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "Function 'getcwd' not provided by system");
|
||||
return 2;
|
||||
#else
|
||||
char *path = NULL;
|
||||
/* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. */
|
||||
/* Passing (NULL, 0) is not guaranteed to work.
|
||||
Use a temp buffer and size instead. */
|
||||
size_t size = LFS_MAXPATHLEN; /* initial buffer size */
|
||||
int result;
|
||||
while (1) {
|
||||
char *path2 = realloc(path, size);
|
||||
if (!path2) /* failed to allocate */ {
|
||||
if (!path2) { /* failed to allocate */
|
||||
result = pusherror(L, "get_dir realloc() failed");
|
||||
break;
|
||||
}
|
||||
@@ -296,7 +315,8 @@ static int get_dir (lua_State *L) {
|
||||
/*
|
||||
** Check if the given element on the stack is a file and returns it.
|
||||
*/
|
||||
static FILE *check_file (lua_State *L, int idx, const char *funcname) {
|
||||
static FILE *check_file(lua_State * L, int idx, const char *funcname)
|
||||
{
|
||||
#if LUA_VERSION_NUM == 501
|
||||
FILE **fh = (FILE **) luaL_checkudata(L, idx, "FILE*");
|
||||
if (*fh == NULL) {
|
||||
@@ -320,26 +340,41 @@ static FILE *check_file (lua_State *L, int idx, const char *funcname) {
|
||||
/*
|
||||
**
|
||||
*/
|
||||
static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long start, long len, const char *funcname) {
|
||||
static int _file_lock(lua_State * L, FILE * fh, const char *mode,
|
||||
const long start, long len, const char *funcname)
|
||||
{
|
||||
int code;
|
||||
#ifdef _WIN32
|
||||
/* lkmode valid values are:
|
||||
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.
|
||||
LK_NBLCK Locks the specified bytes. If the bytes cannot be locked, the constant returns an error.
|
||||
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.
|
||||
LK_NBLCK Locks the specified bytes. If the bytes cannot be locked,
|
||||
the constant returns an error.
|
||||
LK_NBRLCK Same as _LK_NBLCK.
|
||||
LK_RLCK Same as _LK_LOCK.
|
||||
LK_UNLCK Unlocks the specified bytes, which must have been previously locked.
|
||||
LK_UNLCK Unlocks the specified bytes, which must have been
|
||||
previously locked.
|
||||
|
||||
Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.
|
||||
Regions should be locked only briefly and should be unlocked
|
||||
before closing a file or exiting the program.
|
||||
|
||||
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp
|
||||
*/
|
||||
int lkmode;
|
||||
switch (*mode) {
|
||||
case 'r': lkmode = LK_NBLCK; break;
|
||||
case 'w': lkmode = LK_NBLCK; break;
|
||||
case 'u': lkmode = LK_UNLCK; break;
|
||||
default : return luaL_error (L, "%s: invalid mode", funcname);
|
||||
case 'r':
|
||||
lkmode = LK_NBLCK;
|
||||
break;
|
||||
case 'w':
|
||||
lkmode = LK_NBLCK;
|
||||
break;
|
||||
case 'u':
|
||||
lkmode = LK_UNLCK;
|
||||
break;
|
||||
default:
|
||||
return luaL_error(L, "%s: invalid mode", funcname);
|
||||
}
|
||||
if (!len) {
|
||||
fseek(fh, 0L, SEEK_END);
|
||||
@@ -354,10 +389,17 @@ static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long star
|
||||
#else
|
||||
struct flock f;
|
||||
switch (*mode) {
|
||||
case 'w': f.l_type = F_WRLCK; break;
|
||||
case 'r': f.l_type = F_RDLCK; break;
|
||||
case 'u': f.l_type = F_UNLCK; break;
|
||||
default : return luaL_error (L, "%s: invalid mode", funcname);
|
||||
case 'w':
|
||||
f.l_type = F_WRLCK;
|
||||
break;
|
||||
case 'r':
|
||||
f.l_type = F_RDLCK;
|
||||
break;
|
||||
case 'u':
|
||||
f.l_type = F_UNLCK;
|
||||
break;
|
||||
default:
|
||||
return luaL_error(L, "%s: invalid mode", funcname);
|
||||
}
|
||||
f.l_whence = SEEK_SET;
|
||||
f.l_start = (off_t) start;
|
||||
@@ -371,17 +413,22 @@ static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long star
|
||||
typedef struct lfs_Lock {
|
||||
HANDLE fd;
|
||||
} lfs_Lock;
|
||||
static int lfs_lock_dir(lua_State *L) {
|
||||
size_t pathl; HANDLE fd;
|
||||
static int lfs_lock_dir(lua_State * L)
|
||||
{
|
||||
size_t pathl;
|
||||
HANDLE fd;
|
||||
lfs_Lock *lock;
|
||||
char *ln;
|
||||
const char *lockfile = "/lockfile.lfs";
|
||||
const char *path = luaL_checklstring(L, 1, &pathl);
|
||||
ln = (char *) malloc(pathl + strlen(lockfile) + 1);
|
||||
if (!ln) {
|
||||
lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2;
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
strcpy(ln, path); strcat(ln, lockfile);
|
||||
strcpy(ln, path);
|
||||
strcat(ln, lockfile);
|
||||
fd = CreateFile(ln, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
|
||||
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL);
|
||||
free(ln);
|
||||
@@ -394,7 +441,9 @@ static int lfs_lock_dir(lua_State *L) {
|
||||
lua_setmetatable(L, -2);
|
||||
return 1;
|
||||
}
|
||||
static int lfs_unlock_dir(lua_State *L) {
|
||||
|
||||
static int lfs_unlock_dir(lua_State * L)
|
||||
{
|
||||
lfs_Lock *lock = (lfs_Lock *) luaL_checkudata(L, 1, LOCK_METATABLE);
|
||||
if (lock->fd != INVALID_HANDLE_VALUE) {
|
||||
CloseHandle(lock->fd);
|
||||
@@ -406,7 +455,8 @@ static int lfs_unlock_dir(lua_State *L) {
|
||||
typedef struct lfs_Lock {
|
||||
char *ln;
|
||||
} lfs_Lock;
|
||||
static int lfs_lock_dir(lua_State *L) {
|
||||
static int lfs_lock_dir(lua_State * L)
|
||||
{
|
||||
lfs_Lock *lock;
|
||||
size_t pathl;
|
||||
char *ln;
|
||||
@@ -415,19 +465,26 @@ static int lfs_lock_dir(lua_State *L) {
|
||||
lock = (lfs_Lock *) lua_newuserdata(L, sizeof(lfs_Lock));
|
||||
ln = (char *) malloc(pathl + strlen(lockfile) + 1);
|
||||
if (!ln) {
|
||||
lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2;
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
strcpy(ln, path); strcat(ln, lockfile);
|
||||
strcpy(ln, path);
|
||||
strcat(ln, lockfile);
|
||||
if (symlink("lock", ln) == -1) {
|
||||
free(ln); lua_pushnil(L);
|
||||
lua_pushstring(L, strerror(errno)); return 2;
|
||||
free(ln);
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, strerror(errno));
|
||||
return 2;
|
||||
}
|
||||
lock->ln = ln;
|
||||
luaL_getmetatable(L, LOCK_METATABLE);
|
||||
lua_setmetatable(L, -2);
|
||||
return 1;
|
||||
}
|
||||
static int lfs_unlock_dir(lua_State *L) {
|
||||
|
||||
static int lfs_unlock_dir(lua_State * L)
|
||||
{
|
||||
lfs_Lock *lock = (lfs_Lock *) luaL_checkudata(L, 1, LOCK_METATABLE);
|
||||
if (lock->ln) {
|
||||
unlink(lock->ln);
|
||||
@@ -438,7 +495,8 @@ static int lfs_unlock_dir(lua_State *L) {
|
||||
}
|
||||
#endif
|
||||
|
||||
static int lfs_g_setmode (lua_State *L, FILE *f, int arg) {
|
||||
static int lfs_g_setmode(lua_State * L, FILE * f, int arg)
|
||||
{
|
||||
static const int mode[] = { _O_BINARY, _O_TEXT };
|
||||
static const char *const modenames[] = { "binary", "text", NULL };
|
||||
int op = luaL_checkoption(L, arg, NULL, modenames);
|
||||
@@ -459,7 +517,8 @@ static int lfs_g_setmode (lua_State *L, FILE *f, int arg) {
|
||||
}
|
||||
}
|
||||
|
||||
static int lfs_f_setmode(lua_State *L) {
|
||||
static int lfs_f_setmode(lua_State * L)
|
||||
{
|
||||
return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2);
|
||||
}
|
||||
|
||||
@@ -470,7 +529,8 @@ static int lfs_f_setmode(lua_State *L) {
|
||||
** @param #3 Number with start position (optional).
|
||||
** @param #4 Number with length (optional).
|
||||
*/
|
||||
static int file_lock (lua_State *L) {
|
||||
static int file_lock(lua_State * L)
|
||||
{
|
||||
FILE *fh = check_file(L, 1, "lock");
|
||||
const char *mode = luaL_checkstring(L, 2);
|
||||
const long start = (long) luaL_optinteger(L, 3, 0);
|
||||
@@ -492,7 +552,8 @@ static int file_lock (lua_State *L) {
|
||||
** @param #2 Number with start position (optional).
|
||||
** @param #3 Number with length (optional).
|
||||
*/
|
||||
static int file_unlock (lua_State *L) {
|
||||
static int file_unlock(lua_State * L)
|
||||
{
|
||||
FILE *fh = check_file(L, 1, "unlock");
|
||||
const long start = (long) luaL_optinteger(L, 2, 0);
|
||||
long len = (long) luaL_optinteger(L, 3, 0);
|
||||
@@ -519,7 +580,9 @@ static int make_link(lua_State *L)
|
||||
const char *newpath = luaL_checkstring(L, 2);
|
||||
#ifndef _WIN32
|
||||
return pushresult(L,
|
||||
(lua_toboolean(L, 3) ? symlink : link)(oldpath, newpath), NULL);
|
||||
(lua_toboolean(L, 3) ? symlink : link) (oldpath,
|
||||
newpath),
|
||||
NULL);
|
||||
#else
|
||||
int symbolic = lua_toboolean(L, 3);
|
||||
STAT_STRUCT oldpathinfo;
|
||||
@@ -529,7 +592,8 @@ static int make_link(lua_State *L)
|
||||
}
|
||||
if (!symbolic && is_dir) {
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, "hard links to directories are not supported on Windows");
|
||||
lua_pushstring(L,
|
||||
"hard links to directories are not supported on Windows");
|
||||
return 2;
|
||||
}
|
||||
|
||||
@@ -539,7 +603,8 @@ static int make_link(lua_State *L)
|
||||
if (result) {
|
||||
return pushresult(L, result, NULL);
|
||||
} else {
|
||||
lua_pushnil(L); lua_pushstring(L, symbolic ? "make_link CreateSymbolicLink() failed"
|
||||
lua_pushnil(L);
|
||||
lua_pushstring(L, symbolic ? "make_link CreateSymbolicLink() failed"
|
||||
: "make_link CreateHardLink() failed");
|
||||
return 2;
|
||||
}
|
||||
@@ -551,7 +616,8 @@ static int make_link(lua_State *L)
|
||||
** Creates a directory.
|
||||
** @param #1 Directory path.
|
||||
*/
|
||||
static int make_dir (lua_State *L) {
|
||||
static int make_dir(lua_State * L)
|
||||
{
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
return pushresult(L, lfs_mkdir(path), NULL);
|
||||
}
|
||||
@@ -561,7 +627,8 @@ static int make_dir (lua_State *L) {
|
||||
** Removes a directory.
|
||||
** @param #1 Directory path.
|
||||
*/
|
||||
static int remove_dir (lua_State *L) {
|
||||
static int remove_dir(lua_State * L)
|
||||
{
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
return pushresult(L, rmdir(path), NULL);
|
||||
}
|
||||
@@ -570,7 +637,8 @@ static int remove_dir (lua_State *L) {
|
||||
/*
|
||||
** Directory iterator
|
||||
*/
|
||||
static int dir_iter (lua_State *L) {
|
||||
static int dir_iter(lua_State * L)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
struct _finddata_t c_file;
|
||||
#else
|
||||
@@ -617,7 +685,8 @@ static int dir_iter (lua_State *L) {
|
||||
/*
|
||||
** Closes directory iterators
|
||||
*/
|
||||
static int dir_close (lua_State *L) {
|
||||
static int dir_close(lua_State * L)
|
||||
{
|
||||
dir_data *d = (dir_data *) lua_touserdata(L, 1);
|
||||
#ifdef _WIN32
|
||||
if (!d->closed && d->hFile) {
|
||||
@@ -636,7 +705,8 @@ static int dir_close (lua_State *L) {
|
||||
/*
|
||||
** Factory of directory iterators
|
||||
*/
|
||||
static int dir_iter_factory (lua_State *L) {
|
||||
static int dir_iter_factory(lua_State * L)
|
||||
{
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
dir_data *d;
|
||||
lua_pushcfunction(L, dir_iter);
|
||||
@@ -662,7 +732,8 @@ static int dir_iter_factory (lua_State *L) {
|
||||
/*
|
||||
** Creates directory metatable.
|
||||
*/
|
||||
static int dir_create_meta (lua_State *L) {
|
||||
static int dir_create_meta(lua_State * L)
|
||||
{
|
||||
luaL_newmetatable(L, DIR_METATABLE);
|
||||
|
||||
/* Method table */
|
||||
@@ -683,7 +754,8 @@ static int dir_create_meta (lua_State *L) {
|
||||
/*
|
||||
** Creates lock metatable.
|
||||
*/
|
||||
static int lock_create_meta (lua_State *L) {
|
||||
static int lock_create_meta(lua_State * L)
|
||||
{
|
||||
luaL_newmetatable(L, LOCK_METATABLE);
|
||||
|
||||
/* Method table */
|
||||
@@ -703,9 +775,11 @@ static int lock_create_meta (lua_State *L) {
|
||||
** Convert the inode protection mode to a string.
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
static const char *mode2string (unsigned short mode) {
|
||||
static const char *mode2string(unsigned short mode)
|
||||
{
|
||||
#else
|
||||
static const char *mode2string (mode_t mode) {
|
||||
static const char *mode2string(mode_t mode)
|
||||
{
|
||||
#endif
|
||||
if (S_ISREG(mode))
|
||||
return "file";
|
||||
@@ -732,7 +806,8 @@ static const char *mode2string (mode_t mode) {
|
||||
** @param #2 Access time in seconds, current time is used if missing.
|
||||
** @param #3 Modification time in seconds, access time is used if missing.
|
||||
*/
|
||||
static int file_utime (lua_State *L) {
|
||||
static int file_utime(lua_State * L)
|
||||
{
|
||||
const char *file = luaL_checkstring(L, 1);
|
||||
struct utimbuf utb, *buf;
|
||||
|
||||
@@ -749,56 +824,81 @@ static int file_utime (lua_State *L) {
|
||||
|
||||
|
||||
/* inode protection mode */
|
||||
static void push_st_mode (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_mode(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushstring(L, mode2string(info->st_mode));
|
||||
}
|
||||
|
||||
/* device inode resides on */
|
||||
static void push_st_dev (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_dev(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_dev);
|
||||
}
|
||||
|
||||
/* inode's number */
|
||||
static void push_st_ino (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_ino(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_ino);
|
||||
}
|
||||
|
||||
/* number of hard links to the file */
|
||||
static void push_st_nlink (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_nlink(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_nlink);
|
||||
}
|
||||
|
||||
/* user-id of owner */
|
||||
static void push_st_uid (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_uid(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_uid);
|
||||
}
|
||||
|
||||
/* group-id of owner */
|
||||
static void push_st_gid (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_gid(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_gid);
|
||||
}
|
||||
|
||||
/* device type, for special file inode */
|
||||
static void push_st_rdev (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_rdev(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_rdev);
|
||||
}
|
||||
|
||||
/* time of last access */
|
||||
static void push_st_atime (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_atime(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_atime);
|
||||
}
|
||||
|
||||
/* time of last data modification */
|
||||
static void push_st_mtime (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_mtime(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_mtime);
|
||||
}
|
||||
|
||||
/* time of last file status change */
|
||||
static void push_st_ctime (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_ctime(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_ctime);
|
||||
}
|
||||
|
||||
/* file size, in bytes */
|
||||
static void push_st_size (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_size(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_size);
|
||||
}
|
||||
|
||||
#ifndef _WIN32
|
||||
/* blocks allocated for file */
|
||||
static void push_st_blocks (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_blocks(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_blocks);
|
||||
}
|
||||
|
||||
/* optimal file system I/O blocksize */
|
||||
static void push_st_blksize (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_blksize(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushinteger(L, (lua_Integer) info->st_blksize);
|
||||
}
|
||||
#endif
|
||||
@@ -808,38 +908,61 @@ static void push_st_blksize (lua_State *L, STAT_STRUCT *info) {
|
||||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
static const char *perm2string (unsigned short mode) {
|
||||
static const char *perm2string(unsigned short mode)
|
||||
{
|
||||
static char perms[10] = "---------";
|
||||
int i;
|
||||
for (i=0;i<9;i++) perms[i]='-';
|
||||
if (mode & _S_IREAD)
|
||||
{ perms[0] = 'r'; perms[3] = 'r'; perms[6] = 'r'; }
|
||||
if (mode & _S_IWRITE)
|
||||
{ perms[1] = 'w'; perms[4] = 'w'; perms[7] = 'w'; }
|
||||
if (mode & _S_IEXEC)
|
||||
{ perms[2] = 'x'; perms[5] = 'x'; perms[8] = 'x'; }
|
||||
for (i = 0; i < 9; i++)
|
||||
perms[i] = '-';
|
||||
if (mode & _S_IREAD) {
|
||||
perms[0] = 'r';
|
||||
perms[3] = 'r';
|
||||
perms[6] = 'r';
|
||||
}
|
||||
if (mode & _S_IWRITE) {
|
||||
perms[1] = 'w';
|
||||
perms[4] = 'w';
|
||||
perms[7] = 'w';
|
||||
}
|
||||
if (mode & _S_IEXEC) {
|
||||
perms[2] = 'x';
|
||||
perms[5] = 'x';
|
||||
perms[8] = 'x';
|
||||
}
|
||||
return perms;
|
||||
}
|
||||
#else
|
||||
static const char *perm2string (mode_t mode) {
|
||||
static const char *perm2string(mode_t mode)
|
||||
{
|
||||
static char perms[10] = "---------";
|
||||
int i;
|
||||
for (i=0;i<9;i++) perms[i]='-';
|
||||
if (mode & S_IRUSR) perms[0] = 'r';
|
||||
if (mode & S_IWUSR) perms[1] = 'w';
|
||||
if (mode & S_IXUSR) perms[2] = 'x';
|
||||
if (mode & S_IRGRP) perms[3] = 'r';
|
||||
if (mode & S_IWGRP) perms[4] = 'w';
|
||||
if (mode & S_IXGRP) perms[5] = 'x';
|
||||
if (mode & S_IROTH) perms[6] = 'r';
|
||||
if (mode & S_IWOTH) perms[7] = 'w';
|
||||
if (mode & S_IXOTH) perms[8] = 'x';
|
||||
for (i = 0; i < 9; i++)
|
||||
perms[i] = '-';
|
||||
if (mode & S_IRUSR)
|
||||
perms[0] = 'r';
|
||||
if (mode & S_IWUSR)
|
||||
perms[1] = 'w';
|
||||
if (mode & S_IXUSR)
|
||||
perms[2] = 'x';
|
||||
if (mode & S_IRGRP)
|
||||
perms[3] = 'r';
|
||||
if (mode & S_IWGRP)
|
||||
perms[4] = 'w';
|
||||
if (mode & S_IXGRP)
|
||||
perms[5] = 'x';
|
||||
if (mode & S_IROTH)
|
||||
perms[6] = 'r';
|
||||
if (mode & S_IWOTH)
|
||||
perms[7] = 'w';
|
||||
if (mode & S_IXOTH)
|
||||
perms[8] = 'x';
|
||||
return perms;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* permssions string */
|
||||
static void push_st_perm (lua_State *L, STAT_STRUCT *info) {
|
||||
static void push_st_perm(lua_State * L, STAT_STRUCT * info)
|
||||
{
|
||||
lua_pushstring(L, perm2string(info->st_mode));
|
||||
}
|
||||
|
||||
@@ -873,14 +996,17 @@ struct _stat_members members[] = {
|
||||
/*
|
||||
** Get file or symbolic link information
|
||||
*/
|
||||
static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) {
|
||||
static int _file_info_(lua_State * L,
|
||||
int (*st)(const char *, STAT_STRUCT *))
|
||||
{
|
||||
STAT_STRUCT info;
|
||||
const char *file = luaL_checkstring(L, 1);
|
||||
int i;
|
||||
|
||||
if (st(file, &info)) {
|
||||
lua_pushnil(L);
|
||||
lua_pushfstring(L, "cannot obtain information from file '%s': %s", file, strerror(errno));
|
||||
lua_pushfstring(L, "cannot obtain information from file '%s': %s",
|
||||
file, strerror(errno));
|
||||
lua_pushinteger(L, errno);
|
||||
return 3;
|
||||
}
|
||||
@@ -914,7 +1040,8 @@ static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) {
|
||||
/*
|
||||
** Get file information using stat.
|
||||
*/
|
||||
static int file_info (lua_State *L) {
|
||||
static int file_info(lua_State * L)
|
||||
{
|
||||
return _file_info_(L, STAT_FUNC);
|
||||
}
|
||||
|
||||
@@ -925,7 +1052,8 @@ static int file_info (lua_State *L) {
|
||||
** Returns 1 if successful (with the target on top of the stack),
|
||||
** 0 on failure (with stack unchanged, and errno set).
|
||||
*/
|
||||
static int push_link_target(lua_State *L) {
|
||||
static int push_link_target(lua_State * L)
|
||||
{
|
||||
const char *file = luaL_checkstring(L, 1);
|
||||
#ifdef _WIN32
|
||||
HANDLE h = CreateFile(file, GENERIC_READ,
|
||||
@@ -979,7 +1107,8 @@ static int push_link_target(lua_State *L) {
|
||||
/*
|
||||
** Get symbolic link information using lstat.
|
||||
*/
|
||||
static int link_info (lua_State *L) {
|
||||
static int link_info(lua_State * L)
|
||||
{
|
||||
int ret;
|
||||
if (lua_isstring(L, 2) && (strcmp(lua_tostring(L, 2), "target") == 0)) {
|
||||
int ok = push_link_target(L);
|
||||
@@ -999,10 +1128,14 @@ static int link_info (lua_State *L) {
|
||||
/*
|
||||
** Assumes the table is on top of the stack.
|
||||
*/
|
||||
static void set_info (lua_State *L) {
|
||||
static void set_info(lua_State * L)
|
||||
{
|
||||
lua_pushliteral(L, "Copyright (C) 2003-2017 Kepler Project");
|
||||
lua_setfield(L, -2, "_COPYRIGHT");
|
||||
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");
|
||||
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");
|
||||
lua_setfield(L, -2, "_DESCRIPTION");
|
||||
lua_pushliteral(L, "LuaFileSystem " LFS_VERSION);
|
||||
lua_setfield(L, -2, "_VERSION");
|
||||
@@ -1026,7 +1159,8 @@ static const struct luaL_Reg fslib[] = {
|
||||
{ NULL, NULL },
|
||||
};
|
||||
|
||||
LFS_EXPORT int luaopen_lfs (lua_State *L) {
|
||||
LFS_EXPORT int luaopen_lfs(lua_State * L)
|
||||
{
|
||||
dir_create_meta(L);
|
||||
lock_create_meta(L);
|
||||
new_lib(L, fslib);
|
||||
|
||||
Reference in New Issue
Block a user