From 4d5d1e75fa5950b39873b46b3ab21fd39028e4f1 Mon Sep 17 00:00:00 2001 From: tomas Date: Mon, 1 Nov 2004 15:27:13 +0000 Subject: Pequenas correcoes nos valores de retorno de algumas funcoes. Acrescimo do arquivo de testes. --- doc/us/manual.html | 10 ++++++---- src/lfs.c | 15 +++++++++++---- teste.lua | 26 -------------------------- tests/test.lua | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 34 deletions(-) delete mode 100644 teste.lua create mode 100644 tests/test.lua diff --git a/doc/us/manual.html b/doc/us/manual.html index ae20fcc..5ca685d 100644 --- a/doc/us/manual.html +++ b/doc/us/manual.html @@ -62,6 +62,8 @@ LuaFileSystem offers the following functions:
  • lfs.chdir (path)
    Changes the current working directory to the given path. + Returns true in case of success or nil plus an error + string.
  • lfs.currentdir ()
    @@ -83,14 +85,14 @@ LuaFileSystem offers the following functions: The optional arguments start and length can be used to specify a starting point and its length; both should be numbers. - This function returns a boolean indicating if the operation was successful; - in case of error, it returns false plus a string describing the - error. + Returns a boolean indicating if the operation was successful; + in case of error, it returns false plus an error string.
  • lfs.mkdir (dirname)
    Creates a new directory. The argument is the name of the new directory. + Returns a boolean indicating whether the operation succeeds or not.
  • lfs.unlock (filehandle[, start[, length]])
    @@ -165,7 +167,7 @@ attrdir (".")
    -$Id: manual.html,v 1.4 2004/10/29 16:15:59 tomas Exp $ +$Id: manual.html,v 1.5 2004/11/01 15:27:13 tomas Exp $ diff --git a/src/lfs.c b/src/lfs.c index e1dfb74..01f4076 100644 --- a/src/lfs.c +++ b/src/lfs.c @@ -9,7 +9,7 @@ ** lfs.lock (fh, mode) ** lfs.unlock (fh) ** -** $Id: lfs.c,v 1.7 2004/11/01 08:57:56 tomas Exp $ +** $Id: lfs.c,v 1.8 2004/11/01 15:27:13 tomas Exp $ */ #include @@ -64,10 +64,15 @@ typedef struct dir_data { */ static int change_dir (lua_State *L) { const char *path = luaL_checkstring(L, 1); - if (chdir(path)) - luaL_error(L,"Unable to change working directory to '%s'\n%s\n", + if (chdir(path)) { + lua_pushnil (L); + lua_pushfstring (L,"Unable to change working directory to '%s'\n%s\n", path, chdir_error); - return 0; + return 2; + } else { + lua_pushboolean (L, 1); + return 1; + } } /* @@ -234,6 +239,8 @@ static int make_dir (lua_State *L) { S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH ); #endif lua_pushboolean (L, !fail); + if (fail) + lua_pushfstring (L, "%s", strerror(errno)); umask (oldmask); return 1; } diff --git a/teste.lua b/teste.lua deleted file mode 100644 index 556cab0..0000000 --- a/teste.lua +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/local/bin/lua -i - -require"lfs" - -print(lfs.version) - -function p () - local fh = assert (io.open ("teste", 'r')) - assert (lfs.lock (fh, 'r')) - print (fh:read"*a") - fh:close () -end - -function wr () - fh = assert (io.open ("teste", 'w')) - assert (lfs.lock (fh, 'w')) -end - -function op () - fh = assert (io.open ("teste", 'r')) - assert (lfs.lock (fh, 'r')) -end - -function fw (x) - assert (fh:write (x)) -end diff --git a/tests/test.lua b/tests/test.lua new file mode 100644 index 0000000..5703bfb --- /dev/null +++ b/tests/test.lua @@ -0,0 +1,40 @@ +#!/usr/local/bin/lua + +local tmp = "/tmp" +local sep = "/" +local upper = ".." + +require"lfs" + +function attrdir (path) + for file in lfs.dir(path) do + if file ~= "." and file ~= ".." then + local f = path..'/'..file + print ("\t=> "..f.." <=") + local attr = lfs.attributes (f) + assert (type(attr) == "table") + if attr.mode == "directory" then + attrdir (f) + else + for name, value in pairs(attr) do + print (name, value) + end + end + end + end +end + +-- Checking changing directories +local current = assert (lfs.currentdir()) +local reldir = string.gsub (current, "^.*%"..sep.."([^"..sep.."])$", "%1") +assert (lfs.chdir (upper), "could not change to upper directory") +assert (lfs.chdir (reldir), "could not change back to current directory") +assert (lfs.currentdir() == current, "error trying to change directories") +assert (lfs.chdir ("this couldn't be an actual directory") == false, "could change to a non-existent directory") +-- Changing creating and removing directories +assert (lfs.mkdir (tmp.."/lfs_tmp_dir"), "could not make a new directory") +assert (os.remove (tmp.."/lfs_tmp_dir"), "could not remove new directory") +assert (lfs.mkdir (tmp.."/lfs_tmp_dir/lfs_tmp_dir") == false, "could create a directory inside a non-existent one") +-- +assert (lfs.attributes ("this couldn't be an actual file") == false, "could get attributes of a non-existent file") +assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory") -- cgit v1.2.3-55-g6feb