From 530c9140c7628f7062e2caff73c94e547ea0c45f Mon Sep 17 00:00:00 2001 From: Reuben Thomas Date: Thu, 9 Jun 2011 14:01:37 +0100 Subject: Add trivial link_info support on Windows (using STAT_FUNC). --- doc/us/manual.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'doc') diff --git a/doc/us/manual.html b/doc/us/manual.html index 217c249..a5843ed 100644 --- a/doc/us/manual.html +++ b/doc/us/manual.html @@ -228,8 +228,8 @@ LuaFileSystem offers the following functions:
lfs.symlinkattributes (filepath [, aname])
Identical to lfs.attributes except that it obtains information about the link itself (not the file it refers to). - This function is not available in Windows so you may want to make sure that - lfs.symlinkattributes exists before using it. + On Windows this function does not yet support links, and is identical to + lfs.attributes.
lfs.touch (filepath [, atime [, mtime]])
-- cgit v1.2.3-55-g6feb From 361cede4d08073183d3546a78763e7b62d6be9ef Mon Sep 17 00:00:00 2001 From: Reuben Thomas Date: Thu, 9 Jun 2011 14:23:44 +0100 Subject: Add trivial implementation of setmode on non-Windows platforms. --- doc/us/manual.html | 4 ++-- src/lfs.c | 15 +++------------ tests/test.lua | 16 +++++++--------- 3 files changed, 12 insertions(+), 23 deletions(-) (limited to 'doc') diff --git a/doc/us/manual.html b/doc/us/manual.html index a5843ed..48a8e5b 100644 --- a/doc/us/manual.html +++ b/doc/us/manual.html @@ -221,8 +221,8 @@ LuaFileSystem offers the following functions:
lfs.setmode (file, mode)
Sets the writing mode for a file. The mode string can be either binary or text. - Returns the previous mode string for the file. This function is only available in Windows, so you may want to make sure that - lfs.setmode exists before using it. + Returns the previous mode string for the file. On non-Windows platforms, where the two modes are identical, + setting the mode has no effect, and the mode is always returned as binary.
lfs.symlinkattributes (filepath [, aname])
diff --git a/src/lfs.c b/src/lfs.c index 63d2dbd..e78aae6 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -100,8 +100,7 @@ typedef struct dir_data { #else #define _O_TEXT 0 #define _O_BINARY 0 -#define lfs_setmode(L,file,m) ((void)((void)file,m), \ - luaL_error(L, LUA_QL("setmode") " not supported on this platform"), -1) +#define lfs_setmode(L,file,m) 0 #define STAT_STRUCT struct stat #define STAT_FUNC stat #define LSTAT_FUNC lstat @@ -281,10 +280,9 @@ static int lfs_unlock_dir(lua_State *L) { } #endif -#ifdef _WIN32 static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { - static const int mode[] = {_O_TEXT, _O_BINARY}; - static const char *const modenames[] = {"text", "binary", NULL}; + static const int mode[] = {_O_BINARY, _O_TEXT}; + static const char *const modenames[] = {"binary", "text", NULL}; int op = luaL_checkoption(L, arg, NULL, modenames); int res = lfs_setmode(L, f, mode[op]); if (res != -1) { @@ -307,13 +305,6 @@ static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { return 3; } } -#else -static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { - lua_pushboolean(L, 0); - lua_pushliteral(L, "setmode not supported on this platform"); - return 2; -} -#endif static int lfs_f_setmode(lua_State *L) { return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2); diff --git a/tests/test.lua b/tests/test.lua index 133bac2..6eb9f70 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -76,15 +76,13 @@ if (os.execute ("ln -s "..tmpfile.." _a_link_for_test_")) then assert (os.remove"_a_link_for_test_") end -if lfs.setmode then - -- Checking text/binary modes (works only in Windows) - local f = io.open(tmpfile, "w") - local result, mode = lfs.setmode(f, "binary") - assert((result and mode == "text") or (not result and mode == "setmode not supported on this platform")) - result, mode = lfs.setmode(f, "text") - assert((result and mode == "binary") or (not result and mode == "setmode not supported on this platform")) - f:close() -end +-- Checking text/binary modes (only has an effect in Windows) +local f = io.open(tmpfile, "w") +local result, mode = lfs.setmode(f, "binary") +assert(result) -- on non-Windows platforms, mode is always returned as "binary" +result, mode = lfs.setmode(f, "text") +assert(result and mode == "binary") +f:close() -- Restore access time to current value assert (lfs.touch (tmpfile, attrib.access, attrib.modification)) -- cgit v1.2.3-55-g6feb From fbdff8f1a56c229fd461b2318e755d16d88adcb4 Mon Sep 17 00:00:00 2001 From: Reuben Thomas Date: Thu, 9 Jun 2011 15:04:17 +0100 Subject: Fix typo in manual. --- doc/us/manual.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'doc') diff --git a/doc/us/manual.html b/doc/us/manual.html index 48a8e5b..a7e2efb 100644 --- a/doc/us/manual.html +++ b/doc/us/manual.html @@ -174,7 +174,7 @@ LuaFileSystem offers the following functions:
lfs.lock_dir(path, [seconds_stale])
Creates a lockfile (called lockfile.lfs) in path if it does not - exist and returns the lock. If the lock already exists checks it + exist and returns the lock. If the lock already exists checks if it's stale, using the second parameter (default for the second parameter is INT_MAX, which in practice means the lock will never be stale. To free the the lock call lock:free().
-- cgit v1.2.3-55-g6feb From 3cea6c6e9612ca2b04b4742c04721d24f4b316be Mon Sep 17 00:00:00 2001 From: Reuben Thomas Date: Thu, 9 Jun 2011 19:49:29 +0100 Subject: Add lfs.link. --- doc/us/manual.html | 7 +++++++ src/lfs.c | 43 +++++++++++++++++++++++++++++++++++++++++++ tests/test.lua | 7 +++++-- 3 files changed, 55 insertions(+), 2 deletions(-) (limited to 'doc') diff --git a/doc/us/manual.html b/doc/us/manual.html index a7e2efb..996dc1f 100644 --- a/doc/us/manual.html +++ b/doc/us/manual.html @@ -206,6 +206,13 @@ LuaFileSystem offers the following functions: Returns true if the operation was successful; in case of error, it returns nil plus an error string.
+ +
lfs.link (old, new[, symlink])
+
Creates a link. The first argument is the object to link to + and the second is the name of the link. If the optional third + argument is true, the link will by a symbolic link (by default, a + hard link is created). +
lfs.mkdir (dirname)
Creates a new directory. The argument is the name of the new diff --git a/src/lfs.c b/src/lfs.c index dd41df8..f33b5ae 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -106,6 +106,29 @@ typedef struct dir_data { #define LSTAT_FUNC lstat #endif +/* +** Utility functions +*/ +static int pusherror(lua_State *L, const char *info) +{ + lua_pushnil(L); + if (info==NULL) + lua_pushstring(L, strerror(errno)); + else + lua_pushfstring(L, "%s: %s", info, strerror(errno)); + lua_pushinteger(L, errno); + return 3; +} + +static int pushresult(lua_State *L, int i, const char *info) +{ + if (i==-1) + return pusherror(L, info); + lua_pushinteger(L, i); + return 1; +} + + /* ** This function changes the working (current) directory */ @@ -354,6 +377,25 @@ static int file_unlock (lua_State *L) { } +/* +** Creates a link. +** @param #1 Object to link to. +** @param #2 Name of link. +** @param #3 True if link is symbolic (optional). +*/ +static int make_link(lua_State *L) +{ +#ifndef _WIN32 + const char *oldpath = luaL_checkstring(L, 1); + const char *newpath = luaL_checkstring(L, 2); + return pushresult(L, + (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL); +#else + pusherror(L, "make_link is not supported on Windows"); +#endif +} + + /* ** Creates a directory. ** @param #1 Directory path. @@ -763,6 +805,7 @@ static const struct luaL_Reg fslib[] = { {"chdir", change_dir}, {"currentdir", get_dir}, {"dir", dir_iter_factory}, + {"link", make_link}, {"lock", file_lock}, {"mkdir", make_dir}, {"rmdir", remove_dir}, diff --git a/tests/test.lua b/tests/test.lua index 81a0ab6..c4911c9 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -69,11 +69,14 @@ local new_att = assert (lfs.attributes (tmpfile)) assert (new_att.access == testdate2, "could not set access time") assert (new_att.modification == testdate1, "could not set modification time") --- Checking symbolic link information (does not work in Windows) -if (os.execute ("ln -s "..tmpfile.." _a_link_for_test_")) then +-- Checking link (does not work on Windows) +if lfs.link (tmpfile, "_a_link_for_test_", true) then assert (lfs.attributes"_a_link_for_test_".mode == "file") assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link") + assert (lfs.link (tmpfile, "_a_hard_link_for_test_")) + assert (lfs.attributes (tmpfile, "nlink") == 2) assert (os.remove"_a_link_for_test_") + assert (os.remove"_a_hard_link_for_test_") end -- Checking text/binary modes (only has an effect in Windows) -- cgit v1.2.3-55-g6feb