| 1 |
/* |
| 2 |
** LuaFileSystem |
| 3 |
** Copyright Kepler Project 2003 - 2017 (http://keplerproject.github.io/luafilesystem) |
| 4 |
** |
| 5 |
** File system manipulation library. |
| 6 |
** This library offers these functions: |
| 7 |
** lfs.attributes (filepath [, attributename | attributetable]) |
| 8 |
** lfs.chdir (path) |
| 9 |
** lfs.currentdir () |
| 10 |
** lfs.dir (path) |
| 11 |
** lfs.link (old, new[, symlink]) |
| 12 |
** lfs.lock (fh, mode) |
| 13 |
** lfs.lock_dir (path) |
| 14 |
** lfs.mkdir (path) |
| 15 |
** lfs.rmdir (path) |
| 16 |
** lfs.setmode (filepath, mode) |
| 17 |
** lfs.symlinkattributes (filepath [, attributename]) |
| 18 |
** lfs.touch (filepath [, atime [, mtime]]) |
| 19 |
** lfs.unlock (fh) |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LFS_DO_NOT_USE_LARGE_FILE |
| 23 |
#ifndef _WIN32 |
| 24 |
#ifndef _AIX |
| 25 |
#define _FILE_OFFSET_BITS 64 /* Linux, Solaris and HP-UX */ |
| 26 |
#else |
| 27 |
#define _LARGE_FILES 1 /* AIX */ |
| 28 |
#endif |
| 29 |
#endif |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifndef LFS_DO_NOT_USE_LARGE_FILE |
| 33 |
#define _LARGEFILE64_SOURCE |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <errno.h> |
| 37 |
#include <stdio.h> |
| 38 |
#include <string.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <time.h> |
| 41 |
#include <sys/stat.h> |
| 42 |
|
| 43 |
#ifdef _WIN32 |
| 44 |
#include <direct.h> |
| 45 |
#include <windows.h> |
| 46 |
#include <io.h> |
| 47 |
#include <sys/locking.h> |
| 48 |
#ifdef __BORLANDC__ |
| 49 |
#include <utime.h> |
| 50 |
#else |
| 51 |
#include <sys/utime.h> |
| 52 |
#endif |
| 53 |
#include <fcntl.h> |
| 54 |
/* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */ |
| 55 |
#define LFS_MAXPATHLEN MAX_PATH |
| 56 |
#else |
| 57 |
#include <unistd.h> |
| 58 |
#include <dirent.h> |
| 59 |
#include <fcntl.h> |
| 60 |
#include <sys/types.h> |
| 61 |
#include <utime.h> |
| 62 |
#include <sys/param.h> /* for MAXPATHLEN */ |
| 63 |
#ifdef MAXPATHLEN |
| 64 |
#define LFS_MAXPATHLEN MAXPATHLEN |
| 65 |
#else |
| 66 |
#define LFS_MAXPATHLEN 128 |
| 67 |
#endif |
| 68 |
#endif |
| 69 |
|
| 70 |
#include <lua.h> |
| 71 |
#include <lauxlib.h> |
| 72 |
#include <lualib.h> |
| 73 |
|
| 74 |
#include "lfs.h" |
| 75 |
|
| 76 |
#define LFS_VERSION "1.7.0" |
| 77 |
#define LFS_LIBNAME "lfs" |
| 78 |
|
| 79 |
#if LUA_VERSION_NUM >= 503 /* Lua 5.3 */ |
| 80 |
|
| 81 |
#ifndef luaL_optlong |
| 82 |
#define luaL_optlong luaL_optinteger |
| 83 |
#endif |
| 84 |
|
| 85 |
#endif |
| 86 |
|
| 87 |
#if LUA_VERSION_NUM >= 502 |
| 88 |
# define new_lib(L, l) (luaL_newlib(L, l)) |
| 89 |
#else |
| 90 |
# define new_lib(L, l) (lua_newtable(L), luaL_register(L, NULL, l)) |
| 91 |
#endif |
| 92 |
|
| 93 |
/* Define 'strerror' for systems that do not implement it */ |
| 94 |
#ifdef NO_STRERROR |
| 95 |
#define strerror(_) "System unable to describe the error" |
| 96 |
#endif |
| 97 |
|
| 98 |
#define DIR_METATABLE "directory metatable" |
| 99 |
typedef struct dir_data { |
| 100 |
int closed; |
| 101 |
#ifdef _WIN32 |
| 102 |
intptr_t hFile; |
| 103 |
char pattern[MAX_PATH+1]; |
| 104 |
#else |
| 105 |
DIR *dir; |
| 106 |
#endif |
| 107 |
} dir_data; |
| 108 |
|
| 109 |
#define LOCK_METATABLE "lock metatable" |
| 110 |
|
| 111 |
#ifdef _WIN32 |
| 112 |
#ifdef __BORLANDC__ |
| 113 |
#define lfs_setmode(file, m) (setmode(_fileno(file), m)) |
| 114 |
#define STAT_STRUCT struct stati64 |
| 115 |
#else |
| 116 |
#define lfs_setmode(file, m) (_setmode(_fileno(file), m)) |
| 117 |
#define STAT_STRUCT struct _stati64 |
| 118 |
#endif |
| 119 |
#define STAT_FUNC _stati64 |
| 120 |
#define LSTAT_FUNC STAT_FUNC |
| 121 |
#else |
| 122 |
#define _O_TEXT 0 |
| 123 |
#define _O_BINARY 0 |
| 124 |
#define lfs_setmode(file, m) ((void)file, (void)m, 0) |
| 125 |
#define STAT_STRUCT struct stat |
| 126 |
#define STAT_FUNC stat |
| 127 |
#define LSTAT_FUNC lstat |
| 128 |
#endif |
| 129 |
|
| 130 |
#ifdef _WIN32 |
| 131 |
#define lfs_mkdir _mkdir |
| 132 |
#else |
| 133 |
#define lfs_mkdir(path) (mkdir((path), \ |
| 134 |
S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH)) |
| 135 |
#endif |
| 136 |
|
| 137 |
/* |
| 138 |
** Utility functions |
| 139 |
*/ |
| 140 |
static int pusherror(lua_State *L, const char *info) |
| 141 |
{ |
| 142 |
lua_pushnil(L); |
| 143 |
if (info==NULL) |
| 144 |
lua_pushstring(L, strerror(errno)); |
| 145 |
else |
| 146 |
lua_pushfstring(L, "%s: %s", info, strerror(errno)); |
| 147 |
lua_pushinteger(L, errno); |
| 148 |
return 3; |
| 149 |
} |
| 150 |
|
| 151 |
static int pushresult(lua_State *L, int res, const char *info) { |
| 152 |
if (res == -1) { |
| 153 |
return pusherror(L, info); |
| 154 |
} else { |
| 155 |
lua_pushboolean(L, 1); |
| 156 |
return 1; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
/* |
| 162 |
** This function changes the working (current) directory |
| 163 |
*/ |
| 164 |
static int change_dir (lua_State *L) { |
| 165 |
const char *path = luaL_checkstring(L, 1); |
| 166 |
if (chdir(path)) { |
| 167 |
lua_pushnil (L); |
| 168 |
lua_pushfstring (L,"Unable to change working directory to '%s'\n%s\n", |
| 169 |
path, chdir_error); |
| 170 |
return 2; |
| 171 |
} else { |
| 172 |
lua_pushboolean (L, 1); |
| 173 |
return 1; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/* |
| 178 |
** This function returns the current directory |
| 179 |
** If unable to get the current directory, it returns nil |
| 180 |
** and a string describing the error |
| 181 |
*/ |
| 182 |
static int get_dir (lua_State *L) { |
| 183 |
#ifdef NO_GETCWD |
| 184 |
lua_pushnil(L); |
| 185 |
lua_pushstring(L, "Function 'getcwd' not provided by system"); |
| 186 |
return 2; |
| 187 |
#else |
| 188 |
char *path = NULL; |
| 189 |
/* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. */ |
| 190 |
size_t size = LFS_MAXPATHLEN; /* initial buffer size */ |
| 191 |
int result; |
| 192 |
while (1) { |
| 193 |
path = realloc(path, size); |
| 194 |
if (!path) /* failed to allocate */ |
| 195 |
return pusherror(L, "get_dir realloc() failed"); |
| 196 |
if (getcwd(path, size) != NULL) { |
| 197 |
/* success, push the path to the Lua stack */ |
| 198 |
lua_pushstring(L, path); |
| 199 |
result = 1; |
| 200 |
break; |
| 201 |
} |
| 202 |
if (errno != ERANGE) { /* unexpected error */ |
| 203 |
result = pusherror(L, "get_dir getcwd() failed"); |
| 204 |
break; |
| 205 |
} |
| 206 |
/* ERANGE = insufficient buffer capacity, double size and retry */ |
| 207 |
size *= 2; |
| 208 |
} |
| 209 |
free(path); |
| 210 |
return result; |
| 211 |
#endif |
| 212 |
} |
| 213 |
|
| 214 |
/* |
| 215 |
** Check if the given element on the stack is a file and returns it. |
| 216 |
*/ |
| 217 |
static FILE *check_file (lua_State *L, int idx, const char *funcname) { |
| 218 |
#if LUA_VERSION_NUM == 501 |
| 219 |
FILE **fh = (FILE **)luaL_checkudata (L, idx, "FILE*"); |
| 220 |
if (*fh == NULL) { |
| 221 |
luaL_error (L, "%s: closed file", funcname); |
| 222 |
return 0; |
| 223 |
} else |
| 224 |
return *fh; |
| 225 |
#elif LUA_VERSION_NUM >= 502 && LUA_VERSION_NUM <= 503 |
| 226 |
luaL_Stream *fh = (luaL_Stream *)luaL_checkudata (L, idx, "FILE*"); |
| 227 |
if (fh->closef == 0 || fh->f == NULL) { |
| 228 |
luaL_error (L, "%s: closed file", funcname); |
| 229 |
return 0; |
| 230 |
} else |
| 231 |
return fh->f; |
| 232 |
#else |
| 233 |
#error unsupported Lua version |
| 234 |
#endif |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
/* |
| 239 |
** |
| 240 |
*/ |
| 241 |
static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long start, long len, const char *funcname) { |
| 242 |
int code; |
| 243 |
#ifdef _WIN32 |
| 244 |
/* lkmode valid values are: |
| 245 |
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. |
| 246 |
LK_NBLCK Locks the specified bytes. If the bytes cannot be locked, the constant returns an error. |
| 247 |
LK_NBRLCK Same as _LK_NBLCK. |
| 248 |
LK_RLCK Same as _LK_LOCK. |
| 249 |
LK_UNLCK Unlocks the specified bytes, which must have been previously locked. |
| 250 |
|
| 251 |
Regions should be locked only briefly and should be unlocked before closing a file or exiting the program. |
| 252 |
|
| 253 |
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp |
| 254 |
*/ |
| 255 |
int lkmode; |
| 256 |
switch (*mode) { |
| 257 |
case 'r': lkmode = LK_NBLCK; break; |
| 258 |
case 'w': lkmode = LK_NBLCK; break; |
| 259 |
case 'u': lkmode = LK_UNLCK; break; |
| 260 |
default : return luaL_error (L, "%s: invalid mode", funcname); |
| 261 |
} |
| 262 |
if (!len) { |
| 263 |
fseek (fh, 0L, SEEK_END); |
| 264 |
len = ftell (fh); |
| 265 |
} |
| 266 |
fseek (fh, start, SEEK_SET); |
| 267 |
#ifdef __BORLANDC__ |
| 268 |
code = locking (fileno(fh), lkmode, len); |
| 269 |
#else |
| 270 |
code = _locking (fileno(fh), lkmode, len); |
| 271 |
#endif |
| 272 |
#else |
| 273 |
struct flock f; |
| 274 |
switch (*mode) { |
| 275 |
case 'w': f.l_type = F_WRLCK; break; |
| 276 |
case 'r': f.l_type = F_RDLCK; break; |
| 277 |
case 'u': f.l_type = F_UNLCK; break; |
| 278 |
default : return luaL_error (L, "%s: invalid mode", funcname); |
| 279 |
} |
| 280 |
f.l_whence = SEEK_SET; |
| 281 |
f.l_start = (off_t)start; |
| 282 |
f.l_len = (off_t)len; |
| 283 |
code = fcntl (fileno(fh), F_SETLK, &f); |
| 284 |
#endif |
| 285 |
return (code != -1); |
| 286 |
} |
| 287 |
|
| 288 |
#ifdef _WIN32 |
| 289 |
typedef struct lfs_Lock { |
| 290 |
HANDLE fd; |
| 291 |
} lfs_Lock; |
| 292 |
static int lfs_lock_dir(lua_State *L) { |
| 293 |
size_t pathl; HANDLE fd; |
| 294 |
lfs_Lock *lock; |
| 295 |
char *ln; |
| 296 |
const char *lockfile = "/lockfile.lfs"; |
| 297 |
const char *path = luaL_checklstring(L, 1, &pathl); |
| 298 |
ln = (char*)malloc(pathl + strlen(lockfile) + 1); |
| 299 |
if(!ln) { |
| 300 |
lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2; |
| 301 |
} |
| 302 |
strcpy(ln, path); strcat(ln, lockfile); |
| 303 |
if((fd = CreateFile(ln, GENERIC_WRITE, 0, NULL, CREATE_NEW, |
| 304 |
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL)) == INVALID_HANDLE_VALUE) { |
| 305 |
int en = GetLastError(); |
| 306 |
free(ln); lua_pushnil(L); |
| 307 |
if(en == ERROR_FILE_EXISTS || en == ERROR_SHARING_VIOLATION) |
| 308 |
lua_pushstring(L, "File exists"); |
| 309 |
else |
| 310 |
lua_pushstring(L, strerror(en)); |
| 311 |
return 2; |
| 312 |
} |
| 313 |
free(ln); |
| 314 |
lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock)); |
| 315 |
lock->fd = fd; |
| 316 |
luaL_getmetatable (L, LOCK_METATABLE); |
| 317 |
lua_setmetatable (L, -2); |
| 318 |
return 1; |
| 319 |
} |
| 320 |
static int lfs_unlock_dir(lua_State *L) { |
| 321 |
lfs_Lock *lock = (lfs_Lock *)luaL_checkudata(L, 1, LOCK_METATABLE); |
| 322 |
if(lock->fd != INVALID_HANDLE_VALUE) { |
| 323 |
CloseHandle(lock->fd); |
| 324 |
lock->fd=INVALID_HANDLE_VALUE; |
| 325 |
} |
| 326 |
return 0; |
| 327 |
} |
| 328 |
#else |
| 329 |
typedef struct lfs_Lock { |
| 330 |
char *ln; |
| 331 |
} lfs_Lock; |
| 332 |
static int lfs_lock_dir(lua_State *L) { |
| 333 |
lfs_Lock *lock; |
| 334 |
size_t pathl; |
| 335 |
char *ln; |
| 336 |
const char *lockfile = "/lockfile.lfs"; |
| 337 |
const char *path = luaL_checklstring(L, 1, &pathl); |
| 338 |
lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock)); |
| 339 |
ln = (char*)malloc(pathl + strlen(lockfile) + 1); |
| 340 |
if(!ln) { |
| 341 |
lua_pushnil(L); lua_pushstring(L, strerror(errno)); return 2; |
| 342 |
} |
| 343 |
strcpy(ln, path); strcat(ln, lockfile); |
| 344 |
if(symlink("lock", ln) == -1) { |
| 345 |
free(ln); lua_pushnil(L); |
| 346 |
lua_pushstring(L, strerror(errno)); return 2; |
| 347 |
} |
| 348 |
lock->ln = ln; |
| 349 |
luaL_getmetatable (L, LOCK_METATABLE); |
| 350 |
lua_setmetatable (L, -2); |
| 351 |
return 1; |
| 352 |
} |
| 353 |
static int lfs_unlock_dir(lua_State *L) { |
| 354 |
lfs_Lock *lock = (lfs_Lock *)luaL_checkudata(L, 1, LOCK_METATABLE); |
| 355 |
if(lock->ln) { |
| 356 |
unlink(lock->ln); |
| 357 |
free(lock->ln); |
| 358 |
lock->ln = NULL; |
| 359 |
} |
| 360 |
return 0; |
| 361 |
} |
| 362 |
#endif |
| 363 |
|
| 364 |
static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { |
| 365 |
static const int mode[] = {_O_BINARY, _O_TEXT}; |
| 366 |
static const char *const modenames[] = {"binary", "text", NULL}; |
| 367 |
int op = luaL_checkoption(L, arg, NULL, modenames); |
| 368 |
int res = lfs_setmode(f, mode[op]); |
| 369 |
if (res != -1) { |
| 370 |
int i; |
| 371 |
lua_pushboolean(L, 1); |
| 372 |
for (i = 0; modenames[i] != NULL; i++) { |
| 373 |
if (mode[i] == res) { |
| 374 |
lua_pushstring(L, modenames[i]); |
| 375 |
return 2; |
| 376 |
} |
| 377 |
} |
| 378 |
lua_pushnil(L); |
| 379 |
return 2; |
| 380 |
} else { |
| 381 |
return pusherror(L, NULL); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
static int lfs_f_setmode(lua_State *L) { |
| 386 |
return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2); |
| 387 |
} |
| 388 |
|
| 389 |
/* |
| 390 |
** Locks a file. |
| 391 |
** @param #1 File handle. |
| 392 |
** @param #2 String with lock mode ('w'rite, 'r'ead). |
| 393 |
** @param #3 Number with start position (optional). |
| 394 |
** @param #4 Number with length (optional). |
| 395 |
*/ |
| 396 |
static int file_lock (lua_State *L) { |
| 397 |
FILE *fh = check_file (L, 1, "lock"); |
| 398 |
const char *mode = luaL_checkstring (L, 2); |
| 399 |
const long start = (long) luaL_optinteger (L, 3, 0); |
| 400 |
long len = (long) luaL_optinteger (L, 4, 0); |
| 401 |
if (_file_lock (L, fh, mode, start, len, "lock")) { |
| 402 |
lua_pushboolean (L, 1); |
| 403 |
return 1; |
| 404 |
} else { |
| 405 |
lua_pushnil (L); |
| 406 |
lua_pushfstring (L, "%s", strerror(errno)); |
| 407 |
return 2; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
/* |
| 413 |
** Unlocks a file. |
| 414 |
** @param #1 File handle. |
| 415 |
** @param #2 Number with start position (optional). |
| 416 |
** @param #3 Number with length (optional). |
| 417 |
*/ |
| 418 |
static int file_unlock (lua_State *L) { |
| 419 |
FILE *fh = check_file (L, 1, "unlock"); |
| 420 |
const long start = (long) luaL_optinteger (L, 2, 0); |
| 421 |
long len = (long) luaL_optinteger (L, 3, 0); |
| 422 |
if (_file_lock (L, fh, "u", start, len, "unlock")) { |
| 423 |
lua_pushboolean (L, 1); |
| 424 |
return 1; |
| 425 |
} else { |
| 426 |
lua_pushnil (L); |
| 427 |
lua_pushfstring (L, "%s", strerror(errno)); |
| 428 |
return 2; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
/* |
| 434 |
** Creates a link. |
| 435 |
** @param #1 Object to link to. |
| 436 |
** @param #2 Name of link. |
| 437 |
** @param #3 True if link is symbolic (optional). |
| 438 |
*/ |
| 439 |
static int make_link (lua_State *L) { |
| 440 |
#ifndef _WIN32 |
| 441 |
const char *oldpath = luaL_checkstring(L, 1); |
| 442 |
const char *newpath = luaL_checkstring(L, 2); |
| 443 |
int res = (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath); |
| 444 |
if (res == -1) { |
| 445 |
return pusherror(L, NULL); |
| 446 |
} else { |
| 447 |
lua_pushinteger(L, 0); |
| 448 |
return 1; |
| 449 |
} |
| 450 |
#else |
| 451 |
errno = ENOSYS; /* = "Function not implemented" */ |
| 452 |
return pushresult(L, -1, "make_link is not supported on Windows"); |
| 453 |
#endif |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
/* |
| 458 |
** Creates a directory. |
| 459 |
** @param #1 Directory path. |
| 460 |
*/ |
| 461 |
static int make_dir (lua_State *L) { |
| 462 |
const char *path = luaL_checkstring(L, 1); |
| 463 |
return pushresult(L, lfs_mkdir(path), NULL); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
/* |
| 468 |
** Removes a directory. |
| 469 |
** @param #1 Directory path. |
| 470 |
*/ |
| 471 |
static int remove_dir (lua_State *L) { |
| 472 |
const char *path = luaL_checkstring(L, 1); |
| 473 |
return pushresult(L, rmdir(path), NULL); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
/* |
| 478 |
** Directory iterator |
| 479 |
*/ |
| 480 |
static int dir_iter (lua_State *L) { |
| 481 |
#ifdef _WIN32 |
| 482 |
struct _finddata_t c_file; |
| 483 |
#else |
| 484 |
struct dirent *entry; |
| 485 |
#endif |
| 486 |
dir_data *d = (dir_data *)luaL_checkudata (L, 1, DIR_METATABLE); |
| 487 |
luaL_argcheck (L, d->closed == 0, 1, "closed directory"); |
| 488 |
#ifdef _WIN32 |
| 489 |
if (d->hFile == 0L) { /* first entry */ |
| 490 |
if ((d->hFile = _findfirst (d->pattern, &c_file)) == -1L) { |
| 491 |
lua_pushnil (L); |
| 492 |
lua_pushstring (L, strerror (errno)); |
| 493 |
d->closed = 1; |
| 494 |
return 2; |
| 495 |
} else { |
| 496 |
lua_pushstring (L, c_file.name); |
| 497 |
return 1; |
| 498 |
} |
| 499 |
} else { /* next entry */ |
| 500 |
if (_findnext (d->hFile, &c_file) == -1L) { |
| 501 |
/* no more entries => close directory */ |
| 502 |
_findclose (d->hFile); |
| 503 |
d->closed = 1; |
| 504 |
return 0; |
| 505 |
} else { |
| 506 |
lua_pushstring (L, c_file.name); |
| 507 |
return 1; |
| 508 |
} |
| 509 |
} |
| 510 |
#else |
| 511 |
if ((entry = readdir (d->dir)) != NULL) { |
| 512 |
lua_pushstring (L, entry->d_name); |
| 513 |
return 1; |
| 514 |
} else { |
| 515 |
/* no more entries => close directory */ |
| 516 |
closedir (d->dir); |
| 517 |
d->closed = 1; |
| 518 |
return 0; |
| 519 |
} |
| 520 |
#endif |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
/* |
| 525 |
** Closes directory iterators |
| 526 |
*/ |
| 527 |
static int dir_close (lua_State *L) { |
| 528 |
dir_data *d = (dir_data *)lua_touserdata (L, 1); |
| 529 |
#ifdef _WIN32 |
| 530 |
if (!d->closed && d->hFile) { |
| 531 |
_findclose (d->hFile); |
| 532 |
} |
| 533 |
#else |
| 534 |
if (!d->closed && d->dir) { |
| 535 |
closedir (d->dir); |
| 536 |
} |
| 537 |
#endif |
| 538 |
d->closed = 1; |
| 539 |
return 0; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
/* |
| 544 |
** Factory of directory iterators |
| 545 |
*/ |
| 546 |
static int dir_iter_factory (lua_State *L) { |
| 547 |
const char *path = luaL_checkstring (L, 1); |
| 548 |
dir_data *d; |
| 549 |
lua_pushcfunction (L, dir_iter); |
| 550 |
d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); |
| 551 |
luaL_getmetatable (L, DIR_METATABLE); |
| 552 |
lua_setmetatable (L, -2); |
| 553 |
d->closed = 0; |
| 554 |
#ifdef _WIN32 |
| 555 |
d->hFile = 0L; |
| 556 |
if (strlen(path) > MAX_PATH-2) |
| 557 |
luaL_error (L, "path too long: %s", path); |
| 558 |
else |
| 559 |
sprintf (d->pattern, "%s/*", path); |
| 560 |
#else |
| 561 |
d->dir = opendir (path); |
| 562 |
if (d->dir == NULL) |
| 563 |
luaL_error (L, "cannot open %s: %s", path, strerror (errno)); |
| 564 |
#endif |
| 565 |
return 2; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
/* |
| 570 |
** Creates directory metatable. |
| 571 |
*/ |
| 572 |
static int dir_create_meta (lua_State *L) { |
| 573 |
luaL_newmetatable (L, DIR_METATABLE); |
| 574 |
|
| 575 |
/* Method table */ |
| 576 |
lua_newtable(L); |
| 577 |
lua_pushcfunction (L, dir_iter); |
| 578 |
lua_setfield(L, -2, "next"); |
| 579 |
lua_pushcfunction (L, dir_close); |
| 580 |
lua_setfield(L, -2, "close"); |
| 581 |
|
| 582 |
/* Metamethods */ |
| 583 |
lua_setfield(L, -2, "__index"); |
| 584 |
lua_pushcfunction (L, dir_close); |
| 585 |
lua_setfield (L, -2, "__gc"); |
| 586 |
return 1; |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
/* |
| 591 |
** Creates lock metatable. |
| 592 |
*/ |
| 593 |
static int lock_create_meta (lua_State *L) { |
| 594 |
luaL_newmetatable (L, LOCK_METATABLE); |
| 595 |
|
| 596 |
/* Method table */ |
| 597 |
lua_newtable(L); |
| 598 |
lua_pushcfunction(L, lfs_unlock_dir); |
| 599 |
lua_setfield(L, -2, "free"); |
| 600 |
|
| 601 |
/* Metamethods */ |
| 602 |
lua_setfield(L, -2, "__index"); |
| 603 |
lua_pushcfunction(L, lfs_unlock_dir); |
| 604 |
lua_setfield(L, -2, "__gc"); |
| 605 |
return 1; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
#ifdef _WIN32 |
| 610 |
#ifndef S_ISDIR |
| 611 |
#define S_ISDIR(mode) (mode&_S_IFDIR) |
| 612 |
#endif |
| 613 |
#ifndef S_ISREG |
| 614 |
#define S_ISREG(mode) (mode&_S_IFREG) |
| 615 |
#endif |
| 616 |
#ifndef S_ISLNK |
| 617 |
#define S_ISLNK(mode) (0) |
| 618 |
#endif |
| 619 |
#ifndef S_ISSOCK |
| 620 |
#define S_ISSOCK(mode) (0) |
| 621 |
#endif |
| 622 |
#ifndef S_ISFIFO |
| 623 |
#define S_ISFIFO(mode) (0) |
| 624 |
#endif |
| 625 |
#ifndef S_ISCHR |
| 626 |
#define S_ISCHR(mode) (mode&_S_IFCHR) |
| 627 |
#endif |
| 628 |
#ifndef S_ISBLK |
| 629 |
#define S_ISBLK(mode) (0) |
| 630 |
#endif |
| 631 |
#endif |
| 632 |
/* |
| 633 |
** Convert the inode protection mode to a string. |
| 634 |
*/ |
| 635 |
#ifdef _WIN32 |
| 636 |
static const char *mode2string (unsigned short mode) { |
| 637 |
#else |
| 638 |
static const char *mode2string (mode_t mode) { |
| 639 |
#endif |
| 640 |
if ( S_ISREG(mode) ) |
| 641 |
return "file"; |
| 642 |
else if ( S_ISDIR(mode) ) |
| 643 |
return "directory"; |
| 644 |
else if ( S_ISLNK(mode) ) |
| 645 |
return "link"; |
| 646 |
else if ( S_ISSOCK(mode) ) |
| 647 |
return "socket"; |
| 648 |
else if ( S_ISFIFO(mode) ) |
| 649 |
return "named pipe"; |
| 650 |
else if ( S_ISCHR(mode) ) |
| 651 |
return "char device"; |
| 652 |
else if ( S_ISBLK(mode) ) |
| 653 |
return "block device"; |
| 654 |
else |
| 655 |
return "other"; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
/* |
| 660 |
** Set access time and modification values for a file. |
| 661 |
** @param #1 File path. |
| 662 |
** @param #2 Access time in seconds, current time is used if missing. |
| 663 |
** @param #3 Modification time in seconds, access time is used if missing. |
| 664 |
*/ |
| 665 |
static int file_utime (lua_State *L) { |
| 666 |
const char *file = luaL_checkstring(L, 1); |
| 667 |
struct utimbuf utb, *buf; |
| 668 |
|
| 669 |
if (lua_gettop (L) == 1) /* set to current date/time */ |
| 670 |
buf = NULL; |
| 671 |
else { |
| 672 |
utb.actime = (time_t) luaL_optnumber(L, 2, 0); |
| 673 |
utb.modtime = (time_t) luaL_optinteger(L, 3, utb.actime); |
| 674 |
buf = &utb; |
| 675 |
} |
| 676 |
|
| 677 |
return pushresult(L, utime(file, buf), NULL); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
/* inode protection mode */ |
| 682 |
static void push_st_mode (lua_State *L, STAT_STRUCT *info) { |
| 683 |
lua_pushstring (L, mode2string (info->st_mode)); |
| 684 |
} |
| 685 |
/* device inode resides on */ |
| 686 |
static void push_st_dev (lua_State *L, STAT_STRUCT *info) { |
| 687 |
lua_pushinteger (L, (lua_Integer) info->st_dev); |
| 688 |
} |
| 689 |
/* inode's number */ |
| 690 |
static void push_st_ino (lua_State *L, STAT_STRUCT *info) { |
| 691 |
lua_pushinteger (L, (lua_Integer) info->st_ino); |
| 692 |
} |
| 693 |
/* number of hard links to the file */ |
| 694 |
static void push_st_nlink (lua_State *L, STAT_STRUCT *info) { |
| 695 |
lua_pushinteger (L, (lua_Integer)info->st_nlink); |
| 696 |
} |
| 697 |
/* user-id of owner */ |
| 698 |
static void push_st_uid (lua_State *L, STAT_STRUCT *info) { |
| 699 |
lua_pushinteger (L, (lua_Integer)info->st_uid); |
| 700 |
} |
| 701 |
/* group-id of owner */ |
| 702 |
static void push_st_gid (lua_State *L, STAT_STRUCT *info) { |
| 703 |
lua_pushinteger (L, (lua_Integer)info->st_gid); |
| 704 |
} |
| 705 |
/* device type, for special file inode */ |
| 706 |
static void push_st_rdev (lua_State *L, STAT_STRUCT *info) { |
| 707 |
lua_pushinteger (L, (lua_Integer) info->st_rdev); |
| 708 |
} |
| 709 |
/* time of last access */ |
| 710 |
static void push_st_atime (lua_State *L, STAT_STRUCT *info) { |
| 711 |
lua_pushinteger (L, (lua_Integer) info->st_atime); |
| 712 |
} |
| 713 |
/* time of last data modification */ |
| 714 |
static void push_st_mtime (lua_State *L, STAT_STRUCT *info) { |
| 715 |
lua_pushinteger (L, (lua_Integer) info->st_mtime); |
| 716 |
} |
| 717 |
/* time of last file status change */ |
| 718 |
static void push_st_ctime (lua_State *L, STAT_STRUCT *info) { |
| 719 |
lua_pushinteger (L, (lua_Integer) info->st_ctime); |
| 720 |
} |
| 721 |
/* file size, in bytes */ |
| 722 |
static void push_st_size (lua_State *L, STAT_STRUCT *info) { |
| 723 |
lua_pushinteger (L, (lua_Integer)info->st_size); |
| 724 |
} |
| 725 |
#ifndef _WIN32 |
| 726 |
/* blocks allocated for file */ |
| 727 |
static void push_st_blocks (lua_State *L, STAT_STRUCT *info) { |
| 728 |
lua_pushinteger (L, (lua_Integer)info->st_blocks); |
| 729 |
} |
| 730 |
/* optimal file system I/O blocksize */ |
| 731 |
static void push_st_blksize (lua_State *L, STAT_STRUCT *info) { |
| 732 |
lua_pushinteger (L, (lua_Integer)info->st_blksize); |
| 733 |
} |
| 734 |
#endif |
| 735 |
|
| 736 |
/* |
| 737 |
** Convert the inode protection mode to a permission list. |
| 738 |
*/ |
| 739 |
|
| 740 |
#ifdef _WIN32 |
| 741 |
static const char *perm2string (unsigned short mode) { |
| 742 |
static char perms[10] = "---------"; |
| 743 |
int i; |
| 744 |
for (i=0;i<9;i++) perms[i]='-'; |
| 745 |
if (mode & _S_IREAD) |
| 746 |
{ perms[0] = 'r'; perms[3] = 'r'; perms[6] = 'r'; } |
| 747 |
if (mode & _S_IWRITE) |
| 748 |
{ perms[1] = 'w'; perms[4] = 'w'; perms[7] = 'w'; } |
| 749 |
if (mode & _S_IEXEC) |
| 750 |
{ perms[2] = 'x'; perms[5] = 'x'; perms[8] = 'x'; } |
| 751 |
return perms; |
| 752 |
} |
| 753 |
#else |
| 754 |
static const char *perm2string (mode_t mode) { |
| 755 |
static char perms[10] = "---------"; |
| 756 |
int i; |
| 757 |
for (i=0;i<9;i++) perms[i]='-'; |
| 758 |
if (mode & S_IRUSR) perms[0] = 'r'; |
| 759 |
if (mode & S_IWUSR) perms[1] = 'w'; |
| 760 |
if (mode & S_IXUSR) perms[2] = 'x'; |
| 761 |
if (mode & S_IRGRP) perms[3] = 'r'; |
| 762 |
if (mode & S_IWGRP) perms[4] = 'w'; |
| 763 |
if (mode & S_IXGRP) perms[5] = 'x'; |
| 764 |
if (mode & S_IROTH) perms[6] = 'r'; |
| 765 |
if (mode & S_IWOTH) perms[7] = 'w'; |
| 766 |
if (mode & S_IXOTH) perms[8] = 'x'; |
| 767 |
return perms; |
| 768 |
} |
| 769 |
#endif |
| 770 |
|
| 771 |
/* permssions string */ |
| 772 |
static void push_st_perm (lua_State *L, STAT_STRUCT *info) { |
| 773 |
lua_pushstring (L, perm2string (info->st_mode)); |
| 774 |
} |
| 775 |
|
| 776 |
typedef void (*_push_function) (lua_State *L, STAT_STRUCT *info); |
| 777 |
|
| 778 |
struct _stat_members { |
| 779 |
const char *name; |
| 780 |
_push_function push; |
| 781 |
}; |
| 782 |
|
| 783 |
struct _stat_members members[] = { |
| 784 |
{ "mode", push_st_mode }, |
| 785 |
{ "dev", push_st_dev }, |
| 786 |
{ "ino", push_st_ino }, |
| 787 |
{ "nlink", push_st_nlink }, |
| 788 |
{ "uid", push_st_uid }, |
| 789 |
{ "gid", push_st_gid }, |
| 790 |
{ "rdev", push_st_rdev }, |
| 791 |
{ "access", push_st_atime }, |
| 792 |
{ "modification", push_st_mtime }, |
| 793 |
{ "change", push_st_ctime }, |
| 794 |
{ "size", push_st_size }, |
| 795 |
{ "permissions", push_st_perm }, |
| 796 |
#ifndef _WIN32 |
| 797 |
{ "blocks", push_st_blocks }, |
| 798 |
{ "blksize", push_st_blksize }, |
| 799 |
#endif |
| 800 |
{ NULL, NULL } |
| 801 |
}; |
| 802 |
|
| 803 |
/* |
| 804 |
** Get file or symbolic link information |
| 805 |
*/ |
| 806 |
static int _file_info_ (lua_State *L, int (*st)(const char*, STAT_STRUCT*)) { |
| 807 |
STAT_STRUCT info; |
| 808 |
const char *file = luaL_checkstring (L, 1); |
| 809 |
int i; |
| 810 |
|
| 811 |
if (st(file, &info)) { |
| 812 |
lua_pushnil(L); |
| 813 |
lua_pushfstring(L, "cannot obtain information from file '%s': %s", file, strerror(errno)); |
| 814 |
lua_pushinteger(L, errno); |
| 815 |
return 3; |
| 816 |
} |
| 817 |
if (lua_isstring (L, 2)) { |
| 818 |
const char *member = lua_tostring (L, 2); |
| 819 |
for (i = 0; members[i].name; i++) { |
| 820 |
if (strcmp(members[i].name, member) == 0) { |
| 821 |
/* push member value and return */ |
| 822 |
members[i].push (L, &info); |
| 823 |
return 1; |
| 824 |
} |
| 825 |
} |
| 826 |
/* member not found */ |
| 827 |
return luaL_error(L, "invalid attribute name '%s'", member); |
| 828 |
} |
| 829 |
/* creates a table if none is given, removes extra arguments */ |
| 830 |
lua_settop(L, 2); |
| 831 |
if (!lua_istable (L, 2)) { |
| 832 |
lua_newtable (L); |
| 833 |
} |
| 834 |
/* stores all members in table on top of the stack */ |
| 835 |
for (i = 0; members[i].name; i++) { |
| 836 |
lua_pushstring (L, members[i].name); |
| 837 |
members[i].push (L, &info); |
| 838 |
lua_rawset (L, -3); |
| 839 |
} |
| 840 |
return 1; |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
/* |
| 845 |
** Get file information using stat. |
| 846 |
*/ |
| 847 |
static int file_info (lua_State *L) { |
| 848 |
return _file_info_ (L, STAT_FUNC); |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
/* |
| 853 |
** Push the symlink target to the top of the stack. |
| 854 |
** Assumes the file name is at position 1 of the stack. |
| 855 |
** Returns 1 if successful (with the target on top of the stack), |
| 856 |
** 0 on failure (with stack unchanged, and errno set). |
| 857 |
*/ |
| 858 |
static int push_link_target(lua_State *L) { |
| 859 |
#ifdef _WIN32 |
| 860 |
errno = ENOSYS; |
| 861 |
return 0; |
| 862 |
#else |
| 863 |
const char *file = luaL_checkstring(L, 1); |
| 864 |
char *target = NULL; |
| 865 |
int tsize, size = 256; /* size = initial buffer capacity */ |
| 866 |
while (1) { |
| 867 |
target = realloc(target, size); |
| 868 |
if (!target) /* failed to allocate */ |
| 869 |
return 0; |
| 870 |
tsize = readlink(file, target, size); |
| 871 |
if (tsize < 0) { /* a readlink() error occurred */ |
| 872 |
free(target); |
| 873 |
return 0; |
| 874 |
} |
| 875 |
if (tsize < size) |
| 876 |
break; |
| 877 |
/* possibly truncated readlink() result, double size and retry */ |
| 878 |
size *= 2; |
| 879 |
} |
| 880 |
target[tsize] = '\0'; |
| 881 |
lua_pushlstring(L, target, tsize); |
| 882 |
free(target); |
| 883 |
return 1; |
| 884 |
#endif |
| 885 |
} |
| 886 |
|
| 887 |
/* |
| 888 |
** Get symbolic link information using lstat. |
| 889 |
*/ |
| 890 |
static int link_info (lua_State *L) { |
| 891 |
int ret; |
| 892 |
if (lua_isstring (L, 2) && (strcmp(lua_tostring(L, 2), "target") == 0)) { |
| 893 |
int ok = push_link_target(L); |
| 894 |
return ok ? 1 : pusherror(L, "could not obtain link target"); |
| 895 |
} |
| 896 |
ret = _file_info_ (L, LSTAT_FUNC); |
| 897 |
if (ret == 1 && lua_type(L, -1) == LUA_TTABLE) { |
| 898 |
int ok = push_link_target(L); |
| 899 |
if (ok) { |
| 900 |
lua_setfield(L, -2, "target"); |
| 901 |
} |
| 902 |
} |
| 903 |
return ret; |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
/* |
| 908 |
** Assumes the table is on top of the stack. |
| 909 |
*/ |
| 910 |
static void set_info (lua_State *L) { |
| 911 |
lua_pushliteral(L, "Copyright (C) 2003-2017 Kepler Project"); |
| 912 |
lua_setfield(L, -2, "_COPYRIGHT"); |
| 913 |
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"); |
| 914 |
lua_setfield(L, -2, "_DESCRIPTION"); |
| 915 |
lua_pushliteral(L, "LuaFileSystem " LFS_VERSION); |
| 916 |
lua_setfield(L, -2, "_VERSION"); |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
static const struct luaL_Reg fslib[] = { |
| 921 |
{"attributes", file_info}, |
| 922 |
{"chdir", change_dir}, |
| 923 |
{"currentdir", get_dir}, |
| 924 |
{"dir", dir_iter_factory}, |
| 925 |
{"link", make_link}, |
| 926 |
{"lock", file_lock}, |
| 927 |
{"mkdir", make_dir}, |
| 928 |
{"rmdir", remove_dir}, |
| 929 |
{"symlinkattributes", link_info}, |
| 930 |
{"setmode", lfs_f_setmode}, |
| 931 |
{"touch", file_utime}, |
| 932 |
{"unlock", file_unlock}, |
| 933 |
{"lock_dir", lfs_lock_dir}, |
| 934 |
{NULL, NULL}, |
| 935 |
}; |
| 936 |
|
| 937 |
LFS_EXPORT int luaopen_lfs (lua_State *L) { |
| 938 |
dir_create_meta (L); |
| 939 |
lock_create_meta (L); |
| 940 |
new_lib (L, fslib); |
| 941 |
lua_pushvalue(L, -1); |
| 942 |
lua_setglobal(L, LFS_LIBNAME); |
| 943 |
set_info (L); |
| 944 |
return 1; |
| 945 |
} |