Pequenas correcoes nos valores de retorno de algumas funcoes.

Acrescimo do arquivo de testes.
This commit is contained in:
tomas
2004-11-01 15:27:13 +00:00
parent 3219f4618d
commit 4d5d1e75fa
4 changed files with 57 additions and 34 deletions

View File

@@ -62,6 +62,8 @@ LuaFileSystem offers the following functions:
<li> <b><tt>lfs.chdir (path)</tt></b> <br>
Changes the current
working directory to the given <tt>path</tt>.
Returns <tt>true</tt> in case of success or <tt>nil</tt> plus an error
string.
<a name="getcwd"></a>
<li> <b><tt>lfs.currentdir ()</tt></b> <br>
@@ -83,14 +85,14 @@ LuaFileSystem offers the following functions:
The optional arguments <code>start</code> and <code>length</code> 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 <code>false</code> plus a string describing the
error.
Returns a boolean indicating if the operation was successful;
in case of error, it returns <code>false</code> plus an error string.
<a name="mkdir"></a>
<li> <b><tt>lfs.mkdir (dirname)</tt></b> <br>
Creates a new directory.
The argument is the name of the new directory.
Returns a boolean indicating whether the operation succeeds or not.
<a name="unlock"></a>
<li> <b><tt>lfs.unlock (filehandle[, start[, length]])</tt></b> <br>
@@ -165,7 +167,7 @@ attrdir (".")
<hr>
<small>
$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 $
</small>
</body>

View File

@@ -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 <errno.h>
@@ -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;
}

View File

@@ -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

40
tests/test.lua Normal file
View File

@@ -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")