Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lfs.c File Reference
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <utime.h>
#include <sys/param.h>
#include <limits.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "lfs.h"

Go to the source code of this file.

Data Structures

struct  dir_data
 
struct  lfs_Lock
 
struct  _stat_members
 

Macros

#define _FILE_OFFSET_BITS   64 /* Linux, Solaris and HP-UX */
 
#define _LARGEFILE64_SOURCE
 
#define LFS_MAXPATHLEN   _POSIX_PATH_MAX
 
#define LFS_VERSION   "1.8.0"
 
#define LFS_LIBNAME   "lfs"
 
#define new_lib(L, l)   (lua_newtable(L), luaL_register(L, NULL, l))
 
#define DIR_METATABLE   "directory metatable"
 
#define LOCK_METATABLE   "lock metatable"
 
#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
 
#define lfs_mkdir(path)
 

Typedefs

typedef struct dir_data dir_data
 
typedef struct lfs_Lock lfs_Lock
 
typedef void(* _push_function) (lua_State *L, STAT_STRUCT *info)
 

Functions

static int pusherror (lua_State *L, const char *info)
 
static int pushresult (lua_State *L, int res, const char *info)
 
static int change_dir (lua_State *L)
 
static int get_dir (lua_State *L)
 
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 lfs_lock_dir (lua_State *L)
 
static int lfs_unlock_dir (lua_State *L)
 
static int lfs_g_setmode (lua_State *L, FILE *f, int arg)
 
static int lfs_f_setmode (lua_State *L)
 
static int file_lock (lua_State *L)
 
static int file_unlock (lua_State *L)
 
static int make_link (lua_State *L)
 
static int make_dir (lua_State *L)
 
static int remove_dir (lua_State *L)
 
static int dir_iter (lua_State *L)
 
static int dir_close (lua_State *L)
 
static int dir_iter_factory (lua_State *L)
 
static int dir_create_meta (lua_State *L)
 
static int lock_create_meta (lua_State *L)
 
static const char * mode2string (mode_t mode)
 
static int file_utime (lua_State *L)
 
static void push_st_mode (lua_State *L, STAT_STRUCT *info)
 
static void push_st_dev (lua_State *L, STAT_STRUCT *info)
 
static void push_st_ino (lua_State *L, STAT_STRUCT *info)
 
static void push_st_nlink (lua_State *L, STAT_STRUCT *info)
 
static void push_st_uid (lua_State *L, STAT_STRUCT *info)
 
static void push_st_gid (lua_State *L, STAT_STRUCT *info)
 
static void push_st_rdev (lua_State *L, STAT_STRUCT *info)
 
static void push_st_atime (lua_State *L, STAT_STRUCT *info)
 
static void push_st_mtime (lua_State *L, STAT_STRUCT *info)
 
static void push_st_ctime (lua_State *L, STAT_STRUCT *info)
 
static void push_st_size (lua_State *L, STAT_STRUCT *info)
 
static void push_st_blocks (lua_State *L, STAT_STRUCT *info)
 
static void push_st_blksize (lua_State *L, STAT_STRUCT *info)
 
static const char * perm2string (mode_t mode)
 
static void push_st_perm (lua_State *L, STAT_STRUCT *info)
 
static int _file_info_ (lua_State *L, int(*st)(const char *, STAT_STRUCT *))
 
static int file_info (lua_State *L)
 
static int push_link_target (lua_State *L)
 
static int link_info (lua_State *L)
 
static void set_info (lua_State *L)
 
LFS_EXPORT int luaopen_lfs (lua_State *L)
 

Variables

struct _stat_members members []
 
static const struct luaL_Reg fslib []
 

Macro Definition Documentation

◆ _FILE_OFFSET_BITS

#define _FILE_OFFSET_BITS   64 /* Linux, Solaris and HP-UX */

Definition at line 26 of file lfs.c.

◆ _LARGEFILE64_SOURCE

#define _LARGEFILE64_SOURCE

Definition at line 38 of file lfs.c.

◆ _O_BINARY

#define _O_BINARY   0

Definition at line 167 of file lfs.c.

Referenced by lfs_g_setmode().

◆ _O_TEXT

#define _O_TEXT   0

Definition at line 166 of file lfs.c.

Referenced by lfs_g_setmode().

◆ DIR_METATABLE

#define DIR_METATABLE   "directory metatable"

Definition at line 112 of file lfs.c.

Referenced by dir_create_meta(), dir_iter(), and dir_iter_factory().

◆ LFS_LIBNAME

#define LFS_LIBNAME   "lfs"

Definition at line 91 of file lfs.c.

Referenced by luaopen_lfs().

◆ LFS_MAXPATHLEN

#define LFS_MAXPATHLEN   _POSIX_PATH_MAX

Definition at line 79 of file lfs.c.

Referenced by get_dir().

◆ lfs_mkdir

#define lfs_mkdir ( path)
Value:
(mkdir((path), \
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH))

Definition at line 178 of file lfs.c.

178#define lfs_mkdir(path) (mkdir((path), \
179 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH))

Referenced by make_dir().

◆ lfs_setmode

#define lfs_setmode ( file,
m )   ((void)file, (void)m, 0)

Definition at line 168 of file lfs.c.

Referenced by lfs_g_setmode().

◆ LFS_VERSION

#define LFS_VERSION   "1.8.0"

Definition at line 90 of file lfs.c.

Referenced by set_info().

◆ LOCK_METATABLE

#define LOCK_METATABLE   "lock metatable"

Definition at line 123 of file lfs.c.

Referenced by lfs_lock_dir(), lfs_unlock_dir(), and lock_create_meta().

◆ LSTAT_FUNC

#define LSTAT_FUNC   lstat

Definition at line 171 of file lfs.c.

Referenced by link_info().

◆ new_lib

#define new_lib ( L,
l )   (lua_newtable(L), luaL_register(L, NULL, l))

Definition at line 104 of file lfs.c.

Referenced by luaopen_lfs().

◆ STAT_FUNC

#define STAT_FUNC   stat

Definition at line 170 of file lfs.c.

Referenced by file_info(), and make_link().

◆ STAT_STRUCT

#define STAT_STRUCT   struct stat

Definition at line 169 of file lfs.c.

Referenced by _file_info_(), and make_link().

Typedef Documentation

◆ _push_function

typedef void(* _push_function) (lua_State *L, STAT_STRUCT *info)

Definition at line 980 of file lfs.c.

◆ dir_data

typedef struct dir_data dir_data

◆ lfs_Lock

typedef struct lfs_Lock lfs_Lock

Function Documentation

◆ _file_info_()

static int _file_info_ ( lua_State * L,
int(*)(const char *, STAT_STRUCT *) st )
static

Definition at line 1010 of file lfs.c.

1012{
1013 STAT_STRUCT info;
1014 const char *file = luaL_checkstring(L, 1);
1015 int i;
1016
1017 if (st(file, &info)) {
1018 lua_pushnil(L);
1019 lua_pushfstring(L, "cannot obtain information from file '%s': %s",
1020 file, strerror(errno));
1021 lua_pushinteger(L, errno);
1022 return 3;
1023 }
1024 if (lua_isstring(L, 2)) {
1025 const char *member = lua_tostring(L, 2);
1026 for (i = 0; members[i].name; i++) {
1027 if (strcmp(members[i].name, member) == 0) {
1028 /* push member value and return */
1029 members[i].push(L, &info);
1030 return 1;
1031 }
1032 }
1033 /* member not found */
1034 return luaL_error(L, "invalid attribute name '%s'", member);
1035 }
1036 /* creates a table if none is given, removes extra arguments */
1037 lua_settop(L, 2);
1038 if (!lua_istable(L, 2)) {
1039 lua_newtable(L);
1040 }
1041 /* stores all members in table on top of the stack */
1042 for (i = 0; members[i].name; i++) {
1044 members[i].push(L, &info);
1045 lua_rawset(L, -3);
1046 }
1047 return 1;
1048}
#define STAT_STRUCT
Definition lfs.c:169
struct _stat_members members[]
Definition lfs.c:987
const char * name
Definition lsqlite3.c:2154
LUA_API void lua_pushstring(lua_State *L, const char *s)
LUA_API void lua_pushnil(lua_State *L)
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
LUA_API int lua_isstring(lua_State *L, int idx)
LUA_API void lua_settop(lua_State *L, int idx)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUA_API void lua_rawset(lua_State *L, int idx)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define luaL_checkstring(L, n)
#define lua_istable(L, n)
#define lua_newtable(L)
#define lua_tostring(L, i)
const char * name
Definition lfs.c:983
_push_function push
Definition lfs.c:984

References lua_isstring(), lua_istable, lua_newtable, lua_pushfstring(), lua_pushinteger(), lua_pushnil(), lua_pushstring(), lua_rawset(), lua_settop(), lua_tostring, luaL_checkstring, luaL_error(), members, _stat_members::name, name, _stat_members::push, and STAT_STRUCT.

Referenced by file_info(), and link_info().

◆ _file_lock()

static int _file_lock ( lua_State * L,
FILE * fh,
const char * mode,
const long start,
long len,
const char * funcname )
static

Definition at line 343 of file lfs.c.

345{
346 int code;
347#ifdef _WIN32
348 /* lkmode valid values are:
349 LK_LOCK Locks the specified bytes. If the bytes cannot be locked,
350 the program immediately tries again after 1 second.
351 If, after 10 attempts, the bytes cannot be locked,
352 the constant returns an error.
353 LK_NBLCK Locks the specified bytes. If the bytes cannot be locked,
354 the constant returns an error.
355 LK_NBRLCK Same as _LK_NBLCK.
356 LK_RLCK Same as _LK_LOCK.
357 LK_UNLCK Unlocks the specified bytes, which must have been
358 previously locked.
359
360 Regions should be locked only briefly and should be unlocked
361 before closing a file or exiting the program.
362
363 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp
364 */
365 int lkmode;
366 switch (*mode) {
367 case 'r':
368 lkmode = LK_NBLCK;
369 break;
370 case 'w':
371 lkmode = LK_NBLCK;
372 break;
373 case 'u':
374 lkmode = LK_UNLCK;
375 break;
376 default:
377 return luaL_error(L, "%s: invalid mode", funcname);
378 }
379 if (!len) {
380 fseek(fh, 0L, SEEK_END);
381 len = ftell(fh);
382 }
383 fseek(fh, start, SEEK_SET);
384#ifdef __BORLANDC__
385 code = locking(fileno(fh), lkmode, len);
386#else
387 code = _locking(fileno(fh), lkmode, len);
388#endif
389#else
390 struct flock f;
391 switch (*mode) {
392 case 'w':
393 f.l_type = F_WRLCK;
394 break;
395 case 'r':
396 f.l_type = F_RDLCK;
397 break;
398 case 'u':
399 f.l_type = F_UNLCK;
400 break;
401 default:
402 return luaL_error(L, "%s: invalid mode", funcname);
403 }
404 f.l_whence = SEEK_SET;
405 f.l_start = (off_t) start;
406 f.l_len = (off_t) len;
407 code = fcntl(fileno(fh), F_SETLK, &f);
408#endif
409 return (code != -1);
410}
static int funcname(LexState *ls, expdesc *v)

References funcname(), and luaL_error().

Referenced by file_lock(), and file_unlock().

◆ change_dir()

static int change_dir ( lua_State * L)
static

Definition at line 259 of file lfs.c.

260{
261 const char *path = luaL_checkstring(L, 1);
262 if (chdir(path)) {
263 lua_pushnil(L);
264 lua_pushfstring(L, "Unable to change working directory to '%s'\n%s\n",
265 path, chdir_error);
266 return 2;
267 } else {
268 lua_pushboolean(L, 1);
269 return 1;
270 }
271}
#define chdir_error
Definition lfs.h:12
LUA_API void lua_pushboolean(lua_State *L, int b)

References chdir_error, lua_pushboolean(), lua_pushfstring(), lua_pushnil(), and luaL_checkstring.

◆ check_file()

static FILE * check_file ( lua_State * L,
int idx,
const char * funcname )
static

Definition at line 318 of file lfs.c.

319{
320#if LUA_VERSION_NUM == 501
321 FILE **fh = (FILE **) luaL_checkudata(L, idx, "FILE*");
322 if (*fh == NULL) {
323 luaL_error(L, "%s: closed file", funcname);
324 return 0;
325 } else
326 return *fh;
327#elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 504
328 luaL_Stream *fh = (luaL_Stream *) luaL_checkudata(L, idx, "FILE*");
329 if (fh->closef == 0 || fh->f == NULL) {
330 luaL_error(L, "%s: closed file", funcname);
331 return 0;
332 } else
333 return fh->f;
334#else
335#error unsupported Lua version
336#endif
337}
#define NULL
Definition gmacros.h:924
LUALIB_API void * luaL_checkudata(lua_State *L, int ud, const char *tname)
lua_CFunction closef

References funcname(), luaL_checkudata(), luaL_error(), and NULL.

Referenced by file_lock(), file_unlock(), and lfs_f_setmode().

◆ dir_close()

static int dir_close ( lua_State * L)
static

Definition at line 688 of file lfs.c.

689{
690 dir_data *d = (dir_data *) lua_touserdata(L, 1);
691#ifdef _WIN32
692 if (!d->closed && d->hFile) {
693 _findclose(d->hFile);
694 }
695#else
696 if (!d->closed && d->dir) {
697 closedir(d->dir);
698 }
699#endif
700 d->closed = 1;
701 return 0;
702}
LUA_API void * lua_touserdata(lua_State *L, int idx)
DIR * dir
Definition lfs.c:119
int closed
Definition lfs.c:114

References dir_data::closed, dir_data::dir, and lua_touserdata().

Referenced by dir_create_meta().

◆ dir_create_meta()

static int dir_create_meta ( lua_State * L)
static

Definition at line 741 of file lfs.c.

742{
744
745 /* Method table */
746 lua_newtable(L);
748 lua_setfield(L, -2, "next");
750 lua_setfield(L, -2, "close");
751
752 /* Metamethods */
753 lua_setfield(L, -2, "__index");
755 lua_setfield(L, -2, "__gc");
756
757#if LUA_VERSION_NUM >= 504
759 lua_setfield(L, -2, "__close");
760#endif
761 return 1;
762}
#define DIR_METATABLE
Definition lfs.c:112
static int dir_close(lua_State *L)
Definition lfs.c:688
static int dir_iter(lua_State *L)
Definition lfs.c:640
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
#define lua_pushcfunction(L, f)

References dir_close(), dir_iter(), DIR_METATABLE, lua_newtable, lua_pushcfunction, lua_setfield(), and luaL_newmetatable().

Referenced by luaopen_lfs().

◆ dir_iter()

static int dir_iter ( lua_State * L)
static

Definition at line 640 of file lfs.c.

641{
642#ifdef _WIN32
643 struct _finddata_t c_file;
644#else
645 struct dirent *entry;
646#endif
648 luaL_argcheck(L, d->closed == 0, 1, "closed directory");
649#ifdef _WIN32
650 if (d->hFile == 0L) { /* first entry */
651 if ((d->hFile = _findfirst(d->pattern, &c_file)) == -1L) {
652 lua_pushnil(L);
653 lua_pushstring(L, strerror(errno));
654 d->closed = 1;
655 return 2;
656 } else {
657 lua_pushstring(L, c_file.name);
658 return 1;
659 }
660 } else { /* next entry */
661 if (_findnext(d->hFile, &c_file) == -1L) {
662 /* no more entries => close directory */
663 _findclose(d->hFile);
664 d->closed = 1;
665 return 0;
666 } else {
667 lua_pushstring(L, c_file.name);
668 return 1;
669 }
670 }
671#else
672 if ((entry = readdir(d->dir)) != NULL) {
673 lua_pushstring(L, entry->d_name);
674 return 1;
675 } else {
676 /* no more entries => close directory */
677 closedir(d->dir);
678 d->closed = 1;
679 return 0;
680 }
681#endif
682}
#define luaL_argcheck(L, cond, numarg, extramsg)

References dir_data::closed, dir_data::dir, DIR_METATABLE, lua_pushnil(), lua_pushstring(), luaL_argcheck, luaL_checkudata(), and NULL.

Referenced by dir_create_meta(), and dir_iter_factory().

◆ dir_iter_factory()

static int dir_iter_factory ( lua_State * L)
static

Definition at line 708 of file lfs.c.

709{
710 const char *path = luaL_checkstring(L, 1);
711 dir_data *d;
713 d = (dir_data *) lua_newuserdata(L, sizeof(dir_data));
715 lua_setmetatable(L, -2);
716 d->closed = 0;
717#ifdef _WIN32
718 d->hFile = 0L;
719 if (strlen(path) > MAX_PATH - 2)
720 luaL_error(L, "path too long: %s", path);
721 else
722 sprintf(d->pattern, "%s/*", path);
723#else
724 d->dir = opendir(path);
725 if (d->dir == NULL)
726 luaL_error(L, "cannot open %s: %s", path, strerror(errno));
727#endif
728#if LUA_VERSION_NUM >= 504
729 lua_pushnil(L);
730 lua_pushvalue(L, -2);
731 return 4;
732#else
733 return 2;
734#endif
735}
LUA_API int lua_setmetatable(lua_State *L, int objindex)
LUA_API void lua_pushvalue(lua_State *L, int idx)
#define luaL_getmetatable(L, n)
#define lua_newuserdata(L, s)

References dir_data::closed, dir_data::dir, dir_iter(), DIR_METATABLE, lua_newuserdata, lua_pushcfunction, lua_pushnil(), lua_pushvalue(), lua_setmetatable(), luaL_checkstring, luaL_error(), luaL_getmetatable, and NULL.

◆ file_info()

static int file_info ( lua_State * L)
static

Definition at line 1054 of file lfs.c.

1055{
1056 return _file_info_(L, STAT_FUNC);
1057}
static int _file_info_(lua_State *L, int(*st)(const char *, STAT_STRUCT *))
Definition lfs.c:1010
#define STAT_FUNC
Definition lfs.c:170

References _file_info_(), and STAT_FUNC.

◆ file_lock()

static int file_lock ( lua_State * L)
static

Definition at line 532 of file lfs.c.

533{
534 FILE *fh = check_file(L, 1, "lock");
535 const char *mode = luaL_checkstring(L, 2);
536 const long start = (long) luaL_optinteger(L, 3, 0);
537 long len = (long) luaL_optinteger(L, 4, 0);
538 if (_file_lock(L, fh, mode, start, len, "lock")) {
539 lua_pushboolean(L, 1);
540 return 1;
541 } else {
542 lua_pushnil(L);
543 lua_pushfstring(L, "%s", strerror(errno));
544 return 2;
545 }
546}
static FILE * check_file(lua_State *L, int idx, const char *funcname)
Definition lfs.c:318
static int _file_lock(lua_State *L, FILE *fh, const char *mode, const long start, long len, const char *funcname)
Definition lfs.c:343
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg, lua_Integer def)

References _file_lock(), check_file(), lua_pushboolean(), lua_pushfstring(), lua_pushnil(), luaL_checkstring, and luaL_optinteger().

◆ file_unlock()

static int file_unlock ( lua_State * L)
static

Definition at line 555 of file lfs.c.

556{
557 FILE *fh = check_file(L, 1, "unlock");
558 const long start = (long) luaL_optinteger(L, 2, 0);
559 long len = (long) luaL_optinteger(L, 3, 0);
560 if (_file_lock(L, fh, "u", start, len, "unlock")) {
561 lua_pushboolean(L, 1);
562 return 1;
563 } else {
564 lua_pushnil(L);
565 lua_pushfstring(L, "%s", strerror(errno));
566 return 2;
567 }
568}

References _file_lock(), check_file(), lua_pushboolean(), lua_pushfstring(), lua_pushnil(), and luaL_optinteger().

◆ file_utime()

static int file_utime ( lua_State * L)
static

Definition at line 820 of file lfs.c.

821{
822 const char *file = luaL_checkstring(L, 1);
823 struct utimbuf utb, *buf;
824
825 if (lua_gettop(L) == 1) /* set to current date/time */
826 buf = NULL;
827 else {
828 utb.actime = (time_t) luaL_optnumber(L, 2, 0);
829 utb.modtime = (time_t) luaL_optinteger(L, 3, utb.actime);
830 buf = &utb;
831 }
832
833 return pushresult(L, utime(file, buf), NULL);
834}
static int pushresult(lua_State *L, int res, const char *info)
Definition lfs.c:245
LUA_API int lua_gettop(lua_State *L)
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int narg, lua_Number def)

References lua_gettop(), luaL_checkstring, luaL_optinteger(), luaL_optnumber(), NULL, and pushresult().

◆ get_dir()

static int get_dir ( lua_State * L)
static

Definition at line 278 of file lfs.c.

279{
280#ifdef NO_GETCWD
281 lua_pushnil(L);
282 lua_pushstring(L, "Function 'getcwd' not provided by system");
283 return 2;
284#else
285 char *path = NULL;
286 /* Passing (NULL, 0) is not guaranteed to work.
287 Use a temp buffer and size instead. */
288 size_t size = LFS_MAXPATHLEN; /* initial buffer size */
289 int result;
290 while (1) {
291 char *path2 = realloc(path, size);
292 if (!path2) { /* failed to allocate */
293 result = pusherror(L, "get_dir realloc() failed");
294 break;
295 }
296 path = path2;
297 if (getcwd(path, size) != NULL) {
298 /* success, push the path to the Lua stack */
299 lua_pushstring(L, path);
300 result = 1;
301 break;
302 }
303 if (errno != ERANGE) { /* unexpected error */
304 result = pusherror(L, "get_dir getcwd() failed");
305 break;
306 }
307 /* ERANGE = insufficient buffer capacity, double size and retry */
308 size *= 2;
309 }
310 free(path);
311 return result;
312#endif
313}
#define realloc
Definition civetweb.c:1541
#define free
Definition civetweb.c:1542
static int pusherror(lua_State *L, const char *info)
Definition lfs.c:234
#define LFS_MAXPATHLEN
Definition lfs.c:79

References free, LFS_MAXPATHLEN, lua_pushnil(), lua_pushstring(), NULL, pusherror(), and realloc.

◆ lfs_f_setmode()

static int lfs_f_setmode ( lua_State * L)
static

Definition at line 520 of file lfs.c.

521{
522 return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2);
523}
static int lfs_g_setmode(lua_State *L, FILE *f, int arg)
Definition lfs.c:498

References check_file(), and lfs_g_setmode().

◆ lfs_g_setmode()

static int lfs_g_setmode ( lua_State * L,
FILE * f,
int arg )
static

Definition at line 498 of file lfs.c.

499{
500 static const int mode[] = { _O_BINARY, _O_TEXT };
501 static const char *const modenames[] = { "binary", "text", NULL };
502 int op = luaL_checkoption(L, arg, NULL, modenames);
503 int res = lfs_setmode(f, mode[op]);
504 if (res != -1) {
505 int i;
506 lua_pushboolean(L, 1);
507 for (i = 0; modenames[i] != NULL; i++) {
508 if (mode[i] == res) {
509 lua_pushstring(L, modenames[i]);
510 return 2;
511 }
512 }
513 lua_pushnil(L);
514 return 2;
515 } else {
516 return pusherror(L, NULL);
517 }
518}
CURL_EXTERN int void * arg
Definition curl.h:2622
#define _O_BINARY
Definition lfs.c:167
#define _O_TEXT
Definition lfs.c:166
#define lfs_setmode(file, m)
Definition lfs.c:168
LUALIB_API int luaL_checkoption(lua_State *L, int narg, const char *def, const char *const lst[])

References _O_BINARY, _O_TEXT, arg, lfs_setmode, lua_pushboolean(), lua_pushnil(), lua_pushstring(), luaL_checkoption(), NULL, and pusherror().

Referenced by lfs_f_setmode().

◆ lfs_lock_dir()

static int lfs_lock_dir ( lua_State * L)
static

Definition at line 458 of file lfs.c.

459{
460 lfs_Lock *lock;
461 size_t pathl;
462 char *ln;
463 const char *lockfile = "/lockfile.lfs";
464 const char *path = luaL_checklstring(L, 1, &pathl);
465 lock = (lfs_Lock *) lua_newuserdata(L, sizeof(lfs_Lock));
466 ln = (char *) malloc(pathl + strlen(lockfile) + 1);
467 if (!ln) {
468 lua_pushnil(L);
469 lua_pushstring(L, strerror(errno));
470 return 2;
471 }
472 strcpy(ln, path);
473 strcat(ln, lockfile);
474 if (symlink("lock", ln) == -1) {
475 free(ln);
476 lua_pushnil(L);
477 lua_pushstring(L, strerror(errno));
478 return 2;
479 }
480 lock->ln = ln;
482 lua_setmetatable(L, -2);
483 return 1;
484}
#define malloc
Definition civetweb.c:1539
#define LOCK_METATABLE
Definition lfs.c:123
LUALIB_API const char * luaL_checklstring(lua_State *L, int narg, size_t *len)
char * ln
Definition lfs.c:456

References free, lfs_Lock::ln, LOCK_METATABLE, lua_newuserdata, lua_pushnil(), lua_pushstring(), lua_setmetatable(), luaL_checklstring(), luaL_getmetatable, and malloc.

◆ lfs_unlock_dir()

static int lfs_unlock_dir ( lua_State * L)
static

Definition at line 486 of file lfs.c.

487{
489 if (lock->ln) {
490 unlink(lock->ln);
491 free(lock->ln);
492 lock->ln = NULL;
493 }
494 return 0;
495}

References free, lfs_Lock::ln, LOCK_METATABLE, luaL_checkudata(), and NULL.

Referenced by lock_create_meta().

◆ link_info()

static int link_info ( lua_State * L)
static

Definition at line 1121 of file lfs.c.

1122{
1123 int ret;
1124 if (lua_isstring(L, 2) && (strcmp(lua_tostring(L, 2), "target") == 0)) {
1125 int ok = push_link_target(L);
1126 return ok ? 1 : pusherror(L, "could not obtain link target");
1127 }
1128 ret = _file_info_(L, LSTAT_FUNC);
1129 if (ret == 1 && lua_type(L, -1) == LUA_TTABLE) {
1130 int ok = push_link_target(L);
1131 if (ok) {
1132 lua_setfield(L, -2, "target");
1133 }
1134 }
1135 return ret;
1136}
static int push_link_target(lua_State *L)
Definition lfs.c:1066
#define LSTAT_FUNC
Definition lfs.c:171
LUA_API int lua_type(lua_State *L, int idx)
#define LUA_TTABLE

References _file_info_(), LSTAT_FUNC, lua_isstring(), lua_setfield(), lua_tostring, LUA_TTABLE, lua_type(), push_link_target(), and pusherror().

◆ lock_create_meta()

static int lock_create_meta ( lua_State * L)
static

Definition at line 768 of file lfs.c.

769{
771
772 /* Method table */
773 lua_newtable(L);
775 lua_setfield(L, -2, "free");
776
777 /* Metamethods */
778 lua_setfield(L, -2, "__index");
780 lua_setfield(L, -2, "__gc");
781 return 1;
782}
static int lfs_unlock_dir(lua_State *L)
Definition lfs.c:486

References lfs_unlock_dir(), LOCK_METATABLE, lua_newtable, lua_pushcfunction, lua_setfield(), and luaL_newmetatable().

Referenced by luaopen_lfs().

◆ luaopen_lfs()

LFS_EXPORT int luaopen_lfs ( lua_State * L)

Definition at line 1173 of file lfs.c.

1174{
1175 dir_create_meta(L);
1177 new_lib(L, fslib);
1178 lua_pushvalue(L, -1);
1180 set_info(L);
1181 return 1;
1182}
static int dir_create_meta(lua_State *L)
Definition lfs.c:741
static const struct luaL_Reg fslib[]
Definition lfs.c:1156
static int lock_create_meta(lua_State *L)
Definition lfs.c:768
#define new_lib(L, l)
Definition lfs.c:104
static void set_info(lua_State *L)
Definition lfs.c:1142
#define LFS_LIBNAME
Definition lfs.c:91
#define lua_setglobal(L, s)

References dir_create_meta(), fslib, LFS_LIBNAME, lock_create_meta(), lua_pushvalue(), lua_setglobal, new_lib, and set_info().

◆ make_dir()

static int make_dir ( lua_State * L)
static

Definition at line 619 of file lfs.c.

620{
621 const char *path = luaL_checkstring(L, 1);
622 return pushresult(L, lfs_mkdir(path), NULL);
623}
#define lfs_mkdir(path)
Definition lfs.c:178

References lfs_mkdir, luaL_checkstring, NULL, and pushresult().

◆ make_link()

static int make_link ( lua_State * L)
static

Definition at line 577 of file lfs.c.

578{
579 const char *oldpath = luaL_checkstring(L, 1);
580 const char *newpath = luaL_checkstring(L, 2);
581#ifndef _WIN32
582 return pushresult(L,
583 (lua_toboolean(L, 3) ? symlink : link) (oldpath,
584 newpath),
585 NULL);
586#else
587 int symbolic = lua_toboolean(L, 3);
588 STAT_STRUCT oldpathinfo;
589 int is_dir = 0;
590 if (STAT_FUNC(oldpath, &oldpathinfo) == 0) {
591 is_dir = S_ISDIR(oldpathinfo.st_mode) != 0;
592 }
593 if (!symbolic && is_dir) {
594 lua_pushnil(L);
596 "hard links to directories are not supported on Windows");
597 return 2;
598 }
599
600 int result = symbolic ? CreateSymbolicLink(newpath, oldpath, is_dir)
601 : CreateHardLink(newpath, oldpath, NULL);
602
603 if (result) {
604 return pushresult(L, result, NULL);
605 } else {
606 lua_pushnil(L);
607 lua_pushstring(L, symbolic ? "make_link CreateSymbolicLink() failed"
608 : "make_link CreateHardLink() failed");
609 return 2;
610 }
611#endif
612}
LUA_API int lua_toboolean(lua_State *L, int idx)

References lua_pushnil(), lua_pushstring(), lua_toboolean(), luaL_checkstring, NULL, pushresult(), STAT_FUNC, and STAT_STRUCT.

◆ mode2string()

static const char * mode2string ( mode_t mode)
static

Definition at line 792 of file lfs.c.

793{
794#endif
795 if (S_ISREG(mode))
796 return "file";
797 else if (S_ISDIR(mode))
798 return "directory";
799 else if (S_ISLNK(mode))
800 return "link";
801 else if (S_ISSOCK(mode))
802 return "socket";
803 else if (S_ISFIFO(mode))
804 return "named pipe";
805 else if (S_ISCHR(mode))
806 return "char device";
807 else if (S_ISBLK(mode))
808 return "block device";
809 else
810 return "other";
811}

Referenced by push_st_mode().

◆ perm2string()

static const char * perm2string ( mode_t mode)
static

Definition at line 946 of file lfs.c.

947{
948 static char perms[10] = "---------";
949 int i;
950 for (i = 0; i < 9; i++)
951 perms[i] = '-';
952 if (mode & S_IRUSR)
953 perms[0] = 'r';
954 if (mode & S_IWUSR)
955 perms[1] = 'w';
956 if (mode & S_IXUSR)
957 perms[2] = 'x';
958 if (mode & S_IRGRP)
959 perms[3] = 'r';
960 if (mode & S_IWGRP)
961 perms[4] = 'w';
962 if (mode & S_IXGRP)
963 perms[5] = 'x';
964 if (mode & S_IROTH)
965 perms[6] = 'r';
966 if (mode & S_IWOTH)
967 perms[7] = 'w';
968 if (mode & S_IXOTH)
969 perms[8] = 'x';
970 return perms;
971}

Referenced by push_st_perm().

◆ push_link_target()

static int push_link_target ( lua_State * L)
static

Definition at line 1066 of file lfs.c.

1067{
1068 const char *file = luaL_checkstring(L, 1);
1069#ifdef _WIN32
1070 HANDLE h = CreateFile(file, GENERIC_READ,
1071 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1072 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1073 if (h == INVALID_HANDLE_VALUE) {
1074 return lfs_win32_pusherror(L);
1075 }
1076#endif
1077 char *target = NULL;
1078 int tsize, size = 256; /* size = initial buffer capacity */
1079 int ok = 0;
1080 while (!ok) {
1081 char *target2 = realloc(target, size);
1082 if (!target2) { /* failed to allocate */
1083 break;
1084 }
1085 target = target2;
1086#ifdef _WIN32
1087 tsize = GetFinalPathNameByHandle(h, target, size, FILE_NAME_OPENED);
1088#else
1089 tsize = readlink(file, target, size);
1090#endif
1091 if (tsize < 0) { /* a readlink() error occurred */
1092 break;
1093 }
1094 if (tsize < size) {
1095#ifdef _WIN32
1096 if (tsize > 4 && strncmp(target, "\\\\?\\", 4) == 0) {
1097 memmove_s(target, tsize - 3, target + 4, tsize - 3);
1098 tsize -= 4;
1099 }
1100#endif
1101 ok = 1;
1102 break;
1103 }
1104 /* possibly truncated readlink() result, double size and retry */
1105 size *= 2;
1106 }
1107 if (ok) {
1108 target[tsize] = '\0';
1109 lua_pushlstring(L, target, tsize);
1110 }
1111#ifdef _WIN32
1112 CloseHandle(h);
1113#endif
1114 free(target);
1115 return ok;
1116}
LUA_API void lua_pushlstring(lua_State *L, const char *s, size_t len)

References free, lua_pushlstring(), luaL_checkstring, NULL, and realloc.

Referenced by link_info().

◆ push_st_atime()

static void push_st_atime ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 880 of file lfs.c.

881{
882 lua_pushinteger(L, (lua_Integer) info->st_atime);
883}
LUA_INTEGER lua_Integer

References lua_pushinteger().

◆ push_st_blksize()

static void push_st_blksize ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 911 of file lfs.c.

912{
913 lua_pushinteger(L, (lua_Integer) info->st_blksize);
914}

References lua_pushinteger().

◆ push_st_blocks()

static void push_st_blocks ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 905 of file lfs.c.

906{
907 lua_pushinteger(L, (lua_Integer) info->st_blocks);
908}

References lua_pushinteger().

◆ push_st_ctime()

static void push_st_ctime ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 892 of file lfs.c.

893{
894 lua_pushinteger(L, (lua_Integer) info->st_ctime);
895}

References lua_pushinteger().

◆ push_st_dev()

static void push_st_dev ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 844 of file lfs.c.

845{
846 lua_pushinteger(L, (lua_Integer) info->st_dev);
847}

References lua_pushinteger().

◆ push_st_gid()

static void push_st_gid ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 868 of file lfs.c.

869{
870 lua_pushinteger(L, (lua_Integer) info->st_gid);
871}

References lua_pushinteger().

◆ push_st_ino()

static void push_st_ino ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 850 of file lfs.c.

851{
852 lua_pushinteger(L, (lua_Integer) info->st_ino);
853}

References lua_pushinteger().

◆ push_st_mode()

static void push_st_mode ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 838 of file lfs.c.

839{
840 lua_pushstring(L, mode2string(info->st_mode));
841}
static const char * mode2string(mode_t mode)
Definition lfs.c:792

References lua_pushstring(), and mode2string().

◆ push_st_mtime()

static void push_st_mtime ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 886 of file lfs.c.

887{
888 lua_pushinteger(L, (lua_Integer) info->st_mtime);
889}

References lua_pushinteger().

◆ push_st_nlink()

static void push_st_nlink ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 856 of file lfs.c.

857{
858 lua_pushinteger(L, (lua_Integer) info->st_nlink);
859}

References lua_pushinteger().

◆ push_st_perm()

static void push_st_perm ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 975 of file lfs.c.

976{
977 lua_pushstring(L, perm2string(info->st_mode));
978}
static const char * perm2string(mode_t mode)
Definition lfs.c:946

References lua_pushstring(), and perm2string().

◆ push_st_rdev()

static void push_st_rdev ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 874 of file lfs.c.

875{
876 lua_pushinteger(L, (lua_Integer) info->st_rdev);
877}

References lua_pushinteger().

◆ push_st_size()

static void push_st_size ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 898 of file lfs.c.

899{
900 lua_pushinteger(L, (lua_Integer) info->st_size);
901}

References lua_pushinteger().

◆ push_st_uid()

static void push_st_uid ( lua_State * L,
STAT_STRUCT * info )
static

Definition at line 862 of file lfs.c.

863{
864 lua_pushinteger(L, (lua_Integer) info->st_uid);
865}

References lua_pushinteger().

◆ pusherror()

static int pusherror ( lua_State * L,
const char * info )
static

Definition at line 234 of file lfs.c.

235{
236 lua_pushnil(L);
237 if (info == NULL)
238 lua_pushstring(L, strerror(errno));
239 else
240 lua_pushfstring(L, "%s: %s", info, strerror(errno));
241 lua_pushinteger(L, errno);
242 return 3;
243}

References lua_pushfstring(), lua_pushinteger(), lua_pushnil(), lua_pushstring(), and NULL.

Referenced by get_dir(), lfs_g_setmode(), link_info(), and pushresult().

◆ pushresult()

static int pushresult ( lua_State * L,
int res,
const char * info )
static

Definition at line 245 of file lfs.c.

246{
247 if (res == -1) {
248 return pusherror(L, info);
249 } else {
250 lua_pushboolean(L, 1);
251 return 1;
252 }
253}

References lua_pushboolean(), and pusherror().

Referenced by file_utime(), make_dir(), make_link(), and remove_dir().

◆ remove_dir()

static int remove_dir ( lua_State * L)
static

Definition at line 630 of file lfs.c.

631{
632 const char *path = luaL_checkstring(L, 1);
633 return pushresult(L, rmdir(path), NULL);
634}

References luaL_checkstring, NULL, and pushresult().

◆ set_info()

static void set_info ( lua_State * L)
static

Definition at line 1142 of file lfs.c.

1143{
1144 lua_pushliteral(L, "Copyright (C) 2003-2017 Kepler Project");
1145 lua_setfield(L, -2, "_COPYRIGHT");
1147 "LuaFileSystem is a Lua library developed to complement "
1148 "the set of functions related to file systems offered by "
1149 "the standard Lua distribution");
1150 lua_setfield(L, -2, "_DESCRIPTION");
1151 lua_pushliteral(L, "LuaFileSystem " LFS_VERSION);
1152 lua_setfield(L, -2, "_VERSION");
1153}
#define LFS_VERSION
Definition lfs.c:90
#define lua_pushliteral(L, s)

References LFS_VERSION, lua_pushliteral, and lua_setfield().

Referenced by luaopen_lfs().

Variable Documentation

◆ fslib

const struct luaL_Reg fslib[]
static
Initial value:
= {
{ "attributes", file_info },
{ "chdir", change_dir },
{ "currentdir", get_dir },
{ "dir", dir_iter_factory },
{ "link", make_link },
{ "lock", file_lock },
{ "mkdir", make_dir },
{ "rmdir", remove_dir },
{ "symlinkattributes", link_info },
{ "setmode", lfs_f_setmode },
{ "touch", file_utime },
{ "unlock", file_unlock },
{ "lock_dir", lfs_lock_dir },
{ NULL, NULL },
}
static int remove_dir(lua_State *L)
Definition lfs.c:630
static int lfs_f_setmode(lua_State *L)
Definition lfs.c:520
static int file_lock(lua_State *L)
Definition lfs.c:532
static int make_link(lua_State *L)
Definition lfs.c:577
static int change_dir(lua_State *L)
Definition lfs.c:259
static int file_utime(lua_State *L)
Definition lfs.c:820
static int file_info(lua_State *L)
Definition lfs.c:1054
static int link_info(lua_State *L)
Definition lfs.c:1121
static int lfs_lock_dir(lua_State *L)
Definition lfs.c:458
static int get_dir(lua_State *L)
Definition lfs.c:278
static int dir_iter_factory(lua_State *L)
Definition lfs.c:708
static int file_unlock(lua_State *L)
Definition lfs.c:555
static int make_dir(lua_State *L)
Definition lfs.c:619

Definition at line 1156 of file lfs.c.

1156 {
1157 { "attributes", file_info },
1158 { "chdir", change_dir },
1159 { "currentdir", get_dir },
1160 { "dir", dir_iter_factory },
1161 { "link", make_link },
1162 { "lock", file_lock },
1163 { "mkdir", make_dir },
1164 { "rmdir", remove_dir },
1165 { "symlinkattributes", link_info },
1166 { "setmode", lfs_f_setmode },
1167 { "touch", file_utime },
1168 { "unlock", file_unlock },
1169 { "lock_dir", lfs_lock_dir },
1170 { NULL, NULL },
1171};

Referenced by luaopen_lfs().

◆ members

struct _stat_members members[]
Initial value:
= {
{ "mode", push_st_mode },
{ "dev", push_st_dev },
{ "ino", push_st_ino },
{ "nlink", push_st_nlink },
{ "uid", push_st_uid },
{ "gid", push_st_gid },
{ "rdev", push_st_rdev },
{ "access", push_st_atime },
{ "modification", push_st_mtime },
{ "change", push_st_ctime },
{ "size", push_st_size },
{ "permissions", push_st_perm },
{ "blocks", push_st_blocks },
{ "blksize", push_st_blksize },
{ NULL, NULL }
}
static void push_st_size(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:898
static void push_st_mode(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:838
static void push_st_blksize(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:911
static void push_st_rdev(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:874
static void push_st_blocks(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:905
static void push_st_perm(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:975
static void push_st_dev(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:844
static void push_st_ctime(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:892
static void push_st_atime(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:880
static void push_st_uid(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:862
static void push_st_mtime(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:886
static void push_st_nlink(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:856
static void push_st_ino(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:850
static void push_st_gid(lua_State *L, STAT_STRUCT *info)
Definition lfs.c:868

Definition at line 987 of file lfs.c.

987 {
988 { "mode", push_st_mode },
989 { "dev", push_st_dev },
990 { "ino", push_st_ino },
991 { "nlink", push_st_nlink },
992 { "uid", push_st_uid },
993 { "gid", push_st_gid },
994 { "rdev", push_st_rdev },
995 { "access", push_st_atime },
996 { "modification", push_st_mtime },
997 { "change", push_st_ctime },
998 { "size", push_st_size },
999 { "permissions", push_st_perm },
1000#ifndef _WIN32
1001 { "blocks", push_st_blocks },
1002 { "blksize", push_st_blksize },
1003#endif
1004 { NULL, NULL }
1005};

Referenced by _file_info_().