diff options
| author | Fabio Mascarenhas <mascarenhas@gmail.com> | 2011-06-09 14:09:31 -0700 |
|---|---|---|
| committer | Fabio Mascarenhas <mascarenhas@gmail.com> | 2011-06-09 14:09:31 -0700 |
| commit | 149e0fb8ec6295325e1e448bbe79b7e9bf2285aa (patch) | |
| tree | 4d761ac2e47869a23e1958e144e1dfe4a4818479 /src | |
| parent | d292b3243f90ba21450c0599b42aa89e999524ee (diff) | |
| parent | 83faa7e05d288dcb543109509e17de25110203ef (diff) | |
| download | luafilesystem-149e0fb8ec6295325e1e448bbe79b7e9bf2285aa.tar.gz luafilesystem-149e0fb8ec6295325e1e448bbe79b7e9bf2285aa.tar.bz2 luafilesystem-149e0fb8ec6295325e1e448bbe79b7e9bf2285aa.zip | |
Merge pull request #8 from rrthomas/master
New lfs.link and other goodies
Diffstat (limited to 'src')
| -rw-r--r-- | src/.gitignore | 2 | ||||
| -rw-r--r-- | src/lfs.c | 101 |
2 files changed, 68 insertions, 35 deletions
diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..9d22eb4 --- /dev/null +++ b/src/.gitignore | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | *.o | ||
| 2 | *.so | ||
| @@ -96,17 +96,40 @@ typedef struct dir_data { | |||
| 96 | #define STAT_STRUCT struct _stati64 | 96 | #define STAT_STRUCT struct _stati64 |
| 97 | #endif | 97 | #endif |
| 98 | #define STAT_FUNC _stati64 | 98 | #define STAT_FUNC _stati64 |
| 99 | #define LSTAT_FUNC STAT_FUNC | ||
| 99 | #else | 100 | #else |
| 100 | #define _O_TEXT 0 | 101 | #define _O_TEXT 0 |
| 101 | #define _O_BINARY 0 | 102 | #define _O_BINARY 0 |
| 102 | #define lfs_setmode(L,file,m) ((void)((void)file,m), \ | 103 | #define lfs_setmode(L,file,m) 0 |
| 103 | luaL_error(L, LUA_QL("setmode") " not supported on this platform"), -1) | ||
| 104 | #define STAT_STRUCT struct stat | 104 | #define STAT_STRUCT struct stat |
| 105 | #define STAT_FUNC stat | 105 | #define STAT_FUNC stat |
| 106 | #define LSTAT_FUNC lstat | 106 | #define LSTAT_FUNC lstat |
| 107 | #endif | 107 | #endif |
| 108 | 108 | ||
| 109 | /* | 109 | /* |
| 110 | ** Utility functions | ||
| 111 | */ | ||
| 112 | static int pusherror(lua_State *L, const char *info) | ||
| 113 | { | ||
| 114 | lua_pushnil(L); | ||
| 115 | if (info==NULL) | ||
| 116 | lua_pushstring(L, strerror(errno)); | ||
| 117 | else | ||
| 118 | lua_pushfstring(L, "%s: %s", info, strerror(errno)); | ||
| 119 | lua_pushinteger(L, errno); | ||
| 120 | return 3; | ||
| 121 | } | ||
| 122 | |||
| 123 | static int pushresult(lua_State *L, int i, const char *info) | ||
| 124 | { | ||
| 125 | if (i==-1) | ||
| 126 | return pusherror(L, info); | ||
| 127 | lua_pushinteger(L, i); | ||
| 128 | return 1; | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | /* | ||
| 110 | ** This function changes the working (current) directory | 133 | ** This function changes the working (current) directory |
| 111 | */ | 134 | */ |
| 112 | static int change_dir (lua_State *L) { | 135 | static int change_dir (lua_State *L) { |
| @@ -280,10 +303,9 @@ static int lfs_unlock_dir(lua_State *L) { | |||
| 280 | } | 303 | } |
| 281 | #endif | 304 | #endif |
| 282 | 305 | ||
| 283 | #ifdef _WIN32 | ||
| 284 | static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { | 306 | static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { |
| 285 | static const int mode[] = {_O_TEXT, _O_BINARY}; | 307 | static const int mode[] = {_O_BINARY, _O_TEXT}; |
| 286 | static const char *const modenames[] = {"text", "binary", NULL}; | 308 | static const char *const modenames[] = {"binary", "text", NULL}; |
| 287 | int op = luaL_checkoption(L, arg, NULL, modenames); | 309 | int op = luaL_checkoption(L, arg, NULL, modenames); |
| 288 | int res = lfs_setmode(L, f, mode[op]); | 310 | int res = lfs_setmode(L, f, mode[op]); |
| 289 | if (res != -1) { | 311 | if (res != -1) { |
| @@ -306,13 +328,6 @@ static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { | |||
| 306 | return 3; | 328 | return 3; |
| 307 | } | 329 | } |
| 308 | } | 330 | } |
| 309 | #else | ||
| 310 | static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { | ||
| 311 | lua_pushboolean(L, 0); | ||
| 312 | lua_pushliteral(L, "setmode not supported on this platform"); | ||
| 313 | return 2; | ||
| 314 | } | ||
| 315 | #endif | ||
| 316 | 331 | ||
| 317 | static int lfs_f_setmode(lua_State *L) { | 332 | static int lfs_f_setmode(lua_State *L) { |
| 318 | return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2); | 333 | return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2); |
| @@ -362,6 +377,29 @@ static int file_unlock (lua_State *L) { | |||
| 362 | } | 377 | } |
| 363 | 378 | ||
| 364 | 379 | ||
| 380 | /* | ||
| 381 | ** Creates a link. | ||
| 382 | ** @param #1 Object to link to. | ||
| 383 | ** @param #2 Name of link. | ||
| 384 | ** @param #3 True if link is symbolic (optional). | ||
| 385 | */ | ||
| 386 | static int make_link(lua_State *L) | ||
| 387 | { | ||
| 388 | #ifndef _WIN32 | ||
| 389 | const char *oldpath = luaL_checkstring(L, 1); | ||
| 390 | const char *newpath = luaL_checkstring(L, 2); | ||
| 391 | return pushresult(L, | ||
| 392 | (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL); | ||
| 393 | #else | ||
| 394 | pusherror(L, "make_link is not supported on Windows"); | ||
| 395 | #endif | ||
| 396 | } | ||
| 397 | |||
| 398 | |||
| 399 | /* | ||
| 400 | ** Creates a directory. | ||
| 401 | ** @param #1 Directory path. | ||
| 402 | */ | ||
| 365 | static int make_dir (lua_State *L) { | 403 | static int make_dir (lua_State *L) { |
| 366 | const char *path = luaL_checkstring (L, 1); | 404 | const char *path = luaL_checkstring (L, 1); |
| 367 | int fail; | 405 | int fail; |
| @@ -473,21 +511,19 @@ static int dir_iter_factory (lua_State *L) { | |||
| 473 | dir_data *d; | 511 | dir_data *d; |
| 474 | lua_pushcfunction (L, dir_iter); | 512 | lua_pushcfunction (L, dir_iter); |
| 475 | d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); | 513 | d = (dir_data *) lua_newuserdata (L, sizeof(dir_data)); |
| 514 | luaL_getmetatable (L, DIR_METATABLE); | ||
| 515 | lua_setmetatable (L, -2); | ||
| 476 | d->closed = 0; | 516 | d->closed = 0; |
| 477 | #ifdef _WIN32 | 517 | #ifdef _WIN32 |
| 478 | d->hFile = 0L; | 518 | d->hFile = 0L; |
| 479 | luaL_getmetatable (L, DIR_METATABLE); | ||
| 480 | lua_setmetatable (L, -2); | ||
| 481 | if (strlen(path) > MAX_PATH-2) | 519 | if (strlen(path) > MAX_PATH-2) |
| 482 | luaL_error (L, "path too long: %s", path); | 520 | luaL_error (L, "path too long: %s", path); |
| 483 | else | 521 | else |
| 484 | sprintf (d->pattern, "%s/*", path); | 522 | sprintf (d->pattern, "%s/*", path); |
| 485 | #else | 523 | #else |
| 486 | luaL_getmetatable (L, DIR_METATABLE); | ||
| 487 | lua_setmetatable (L, -2); | ||
| 488 | d->dir = opendir (path); | 524 | d->dir = opendir (path); |
| 489 | if (d->dir == NULL) | 525 | if (d->dir == NULL) |
| 490 | luaL_error (L, "cannot open %s: %s", path, strerror (errno)); | 526 | luaL_error (L, "cannot open %s: %s", path, strerror (errno)); |
| 491 | #endif | 527 | #endif |
| 492 | return 2; | 528 | return 2; |
| 493 | } | 529 | } |
| @@ -498,19 +534,18 @@ static int dir_iter_factory (lua_State *L) { | |||
| 498 | */ | 534 | */ |
| 499 | static int dir_create_meta (lua_State *L) { | 535 | static int dir_create_meta (lua_State *L) { |
| 500 | luaL_newmetatable (L, DIR_METATABLE); | 536 | luaL_newmetatable (L, DIR_METATABLE); |
| 501 | /* set its __gc field */ | 537 | |
| 502 | lua_pushstring (L, "__index"); | 538 | /* Method table */ |
| 503 | lua_newtable(L); | 539 | lua_newtable(L); |
| 504 | lua_pushstring (L, "next"); | ||
| 505 | lua_pushcfunction (L, dir_iter); | 540 | lua_pushcfunction (L, dir_iter); |
| 506 | lua_settable(L, -3); | 541 | lua_setfield(L, -2, "next"); |
| 507 | lua_pushstring (L, "close"); | ||
| 508 | lua_pushcfunction (L, dir_close); | 542 | lua_pushcfunction (L, dir_close); |
| 509 | lua_settable(L, -3); | 543 | lua_setfield(L, -2, "close"); |
| 510 | lua_settable (L, -3); | 544 | |
| 511 | lua_pushstring (L, "__gc"); | 545 | /* Metamethods */ |
| 546 | lua_setfield(L, -2, "__index"); | ||
| 512 | lua_pushcfunction (L, dir_close); | 547 | lua_pushcfunction (L, dir_close); |
| 513 | lua_settable (L, -3); | 548 | lua_setfield (L, -2, "__gc"); |
| 514 | return 1; | 549 | return 1; |
| 515 | } | 550 | } |
| 516 | 551 | ||
| @@ -519,10 +554,13 @@ static int dir_create_meta (lua_State *L) { | |||
| 519 | */ | 554 | */ |
| 520 | static int lock_create_meta (lua_State *L) { | 555 | static int lock_create_meta (lua_State *L) { |
| 521 | luaL_newmetatable (L, LOCK_METATABLE); | 556 | luaL_newmetatable (L, LOCK_METATABLE); |
| 522 | /* set its __gc field */ | 557 | |
| 558 | /* Method table */ | ||
| 523 | lua_newtable(L); | 559 | lua_newtable(L); |
| 524 | lua_pushcfunction(L, lfs_unlock_dir); | 560 | lua_pushcfunction(L, lfs_unlock_dir); |
| 525 | lua_setfield(L, -2, "free"); | 561 | lua_setfield(L, -2, "free"); |
| 562 | |||
| 563 | /* Metamethods */ | ||
| 526 | lua_setfield(L, -2, "__index"); | 564 | lua_setfield(L, -2, "__index"); |
| 527 | lua_pushcfunction(L, lfs_unlock_dir); | 565 | lua_pushcfunction(L, lfs_unlock_dir); |
| 528 | lua_setfield(L, -2, "__gc"); | 566 | lua_setfield(L, -2, "__gc"); |
| @@ -743,17 +781,9 @@ static int file_info (lua_State *L) { | |||
| 743 | /* | 781 | /* |
| 744 | ** Get symbolic link information using lstat. | 782 | ** Get symbolic link information using lstat. |
| 745 | */ | 783 | */ |
| 746 | #ifndef _WIN32 | ||
| 747 | static int link_info (lua_State *L) { | 784 | static int link_info (lua_State *L) { |
| 748 | return _file_info_ (L, LSTAT_FUNC); | 785 | return _file_info_ (L, LSTAT_FUNC); |
| 749 | } | 786 | } |
| 750 | #else | ||
| 751 | static int link_info (lua_State *L) { | ||
| 752 | lua_pushboolean(L, 0); | ||
| 753 | lua_pushliteral(L, "symlinkattributes not supported on this platform"); | ||
| 754 | return 2; | ||
| 755 | } | ||
| 756 | #endif | ||
| 757 | 787 | ||
| 758 | 788 | ||
| 759 | /* | 789 | /* |
| @@ -777,6 +807,7 @@ static const struct luaL_Reg fslib[] = { | |||
| 777 | {"chdir", change_dir}, | 807 | {"chdir", change_dir}, |
| 778 | {"currentdir", get_dir}, | 808 | {"currentdir", get_dir}, |
| 779 | {"dir", dir_iter_factory}, | 809 | {"dir", dir_iter_factory}, |
| 810 | {"link", make_link}, | ||
| 780 | {"lock", file_lock}, | 811 | {"lock", file_lock}, |
| 781 | {"mkdir", make_dir}, | 812 | {"mkdir", make_dir}, |
| 782 | {"rmdir", remove_dir}, | 813 | {"rmdir", remove_dir}, |
