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 | |
| 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
| -rw-r--r-- | doc/us/manual.html | 17 | ||||
| -rw-r--r-- | src/.gitignore | 2 | ||||
| -rw-r--r-- | src/lfs.c | 101 | ||||
| -rw-r--r-- | tests/test.lua | 35 |
4 files changed, 97 insertions, 58 deletions
diff --git a/doc/us/manual.html b/doc/us/manual.html index 217c249..996dc1f 100644 --- a/doc/us/manual.html +++ b/doc/us/manual.html | |||
| @@ -174,7 +174,7 @@ LuaFileSystem offers the following functions: | |||
| 174 | 174 | ||
| 175 | <dt><a name="chdir"></a><strong><code>lfs.lock_dir(path, [seconds_stale])</code></strong></dt> | 175 | <dt><a name="chdir"></a><strong><code>lfs.lock_dir(path, [seconds_stale])</code></strong></dt> |
| 176 | <dd>Creates a lockfile (called lockfile.lfs) in <code>path</code> if it does not | 176 | <dd>Creates a lockfile (called lockfile.lfs) in <code>path</code> if it does not |
| 177 | exist and returns the lock. If the lock already exists checks it | 177 | exist and returns the lock. If the lock already exists checks if |
| 178 | it's stale, using the second parameter (default for the second | 178 | it's stale, using the second parameter (default for the second |
| 179 | parameter is <code>INT_MAX</code>, which in practice means the lock will never | 179 | parameter is <code>INT_MAX</code>, which in practice means the lock will never |
| 180 | be stale. To free the the lock call <code>lock:free()</code>. <br/> | 180 | be stale. To free the the lock call <code>lock:free()</code>. <br/> |
| @@ -206,6 +206,13 @@ LuaFileSystem offers the following functions: | |||
| 206 | Returns <code>true</code> if the operation was successful; in | 206 | Returns <code>true</code> if the operation was successful; in |
| 207 | case of error, it returns <code>nil</code> plus an error string. | 207 | case of error, it returns <code>nil</code> plus an error string. |
| 208 | </dd> | 208 | </dd> |
| 209 | |||
| 210 | <dt><a name="link"></a><strong><code>lfs.link (old, new[, symlink])</code></strong></dt> | ||
| 211 | <dd>Creates a link. The first argument is the object to link to | ||
| 212 | and the second is the name of the link. If the optional third | ||
| 213 | argument is true, the link will by a symbolic link (by default, a | ||
| 214 | hard link is created). | ||
| 215 | </dd> | ||
| 209 | 216 | ||
| 210 | <dt><a name="mkdir"></a><strong><code>lfs.mkdir (dirname)</code></strong></dt> | 217 | <dt><a name="mkdir"></a><strong><code>lfs.mkdir (dirname)</code></strong></dt> |
| 211 | <dd>Creates a new directory. The argument is the name of the new | 218 | <dd>Creates a new directory. The argument is the name of the new |
| @@ -221,15 +228,15 @@ LuaFileSystem offers the following functions: | |||
| 221 | 228 | ||
| 222 | <dt><a name="setmode"></a><strong><code>lfs.setmode (file, mode)</code></strong></dt> | 229 | <dt><a name="setmode"></a><strong><code>lfs.setmode (file, mode)</code></strong></dt> |
| 223 | <dd>Sets the writing mode for a file. The mode string can be either <code>binary</code> or <code>text</code>. | 230 | <dd>Sets the writing mode for a file. The mode string can be either <code>binary</code> or <code>text</code>. |
| 224 | Returns the previous mode string for the file. This function is only available in Windows, so you may want to make sure that | 231 | Returns the previous mode string for the file. On non-Windows platforms, where the two modes are identical, |
| 225 | <code>lfs.setmode</code> exists before using it. | 232 | setting the mode has no effect, and the mode is always returned as <code>binary</code>. |
| 226 | </dd> | 233 | </dd> |
| 227 | 234 | ||
| 228 | <dt><a name="symlinkattributes"></a><strong><code>lfs.symlinkattributes (filepath [, aname])</code></strong></dt> | 235 | <dt><a name="symlinkattributes"></a><strong><code>lfs.symlinkattributes (filepath [, aname])</code></strong></dt> |
| 229 | <dd>Identical to <a href="#attributes">lfs.attributes</a> except that | 236 | <dd>Identical to <a href="#attributes">lfs.attributes</a> except that |
| 230 | it obtains information about the link itself (not the file it refers to). | 237 | it obtains information about the link itself (not the file it refers to). |
| 231 | This function is not available in Windows so you may want to make sure that | 238 | On Windows this function does not yet support links, and is identical to |
| 232 | <code>lfs.symlinkattributes</code> exists before using it. | 239 | <code>lfs.attributes</code>. |
| 233 | </dd> | 240 | </dd> |
| 234 | 241 | ||
| 235 | <dt><a name="touch"></a><strong><code>lfs.touch (filepath [, atime [, mtime]])</code></strong></dt> | 242 | <dt><a name="touch"></a><strong><code>lfs.touch (filepath [, atime [, mtime]])</code></strong></dt> |
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}, |
diff --git a/tests/test.lua b/tests/test.lua index 7111074..c4911c9 100644 --- a/tests/test.lua +++ b/tests/test.lua | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | #!/usr/local/bin/lua5.1 | 1 | #!/usr/bin/env lua5.1 |
| 2 | 2 | ||
| 3 | local tmp = "/tmp" | 3 | local tmp = "/tmp" |
| 4 | local sep = "/" | 4 | local sep = string.match (package.config, "[^\n]+") |
| 5 | local upper = ".." | 5 | local upper = ".." |
| 6 | 6 | ||
| 7 | require"lfs" | 7 | require"lfs" |
| @@ -69,24 +69,23 @@ local new_att = assert (lfs.attributes (tmpfile)) | |||
| 69 | assert (new_att.access == testdate2, "could not set access time") | 69 | assert (new_att.access == testdate2, "could not set access time") |
| 70 | assert (new_att.modification == testdate1, "could not set modification time") | 70 | assert (new_att.modification == testdate1, "could not set modification time") |
| 71 | 71 | ||
| 72 | local res, err = lfs.symlinkattributes(tmpfile) | 72 | -- Checking link (does not work on Windows) |
| 73 | if err ~= "symlinkattributes not supported on this platform" then | 73 | if lfs.link (tmpfile, "_a_link_for_test_", true) then |
| 74 | -- Checking symbolic link information (does not work in Windows) | 74 | assert (lfs.attributes"_a_link_for_test_".mode == "file") |
| 75 | assert (os.execute ("ln -s "..tmpfile.." _a_link_for_test_")) | 75 | assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link") |
| 76 | assert (lfs.attributes"_a_link_for_test_".mode == "file") | 76 | assert (lfs.link (tmpfile, "_a_hard_link_for_test_")) |
| 77 | assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link") | 77 | assert (lfs.attributes (tmpfile, "nlink") == 2) |
| 78 | assert (os.remove"_a_link_for_test_") | 78 | assert (os.remove"_a_link_for_test_") |
| 79 | assert (os.remove"_a_hard_link_for_test_") | ||
| 79 | end | 80 | end |
| 80 | 81 | ||
| 81 | if lfs.setmode then | 82 | -- Checking text/binary modes (only has an effect in Windows) |
| 82 | -- Checking text/binary modes (works only in Windows) | 83 | local f = io.open(tmpfile, "w") |
| 83 | local f = io.open(tmpfile, "w") | 84 | local result, mode = lfs.setmode(f, "binary") |
| 84 | local result, mode = lfs.setmode(f, "binary") | 85 | assert(result) -- on non-Windows platforms, mode is always returned as "binary" |
| 85 | assert((result and mode == "text") or (not result and mode == "setmode not supported on this platform")) | 86 | result, mode = lfs.setmode(f, "text") |
| 86 | result, mode = lfs.setmode(f, "text") | 87 | assert(result and mode == "binary") |
| 87 | assert((result and mode == "binary") or (not result and mode == "setmode not supported on this platform")) | 88 | f:close() |
| 88 | f:close() | ||
| 89 | end | ||
| 90 | 89 | ||
| 91 | -- Restore access time to current value | 90 | -- Restore access time to current value |
| 92 | assert (lfs.touch (tmpfile, attrib.access, attrib.modification)) | 91 | assert (lfs.touch (tmpfile, attrib.access, attrib.modification)) |
