mirror of
https://github.com/lunarmodules/luafilesystem.git
synced 2026-07-24 20:33:06 +00:00
Pequenas correcoes nos valores de retorno de algumas funcoes.
Acrescimo do arquivo de testes.
This commit is contained in:
@@ -62,6 +62,8 @@ LuaFileSystem offers the following functions:
|
|||||||
<li> <b><tt>lfs.chdir (path)</tt></b> <br>
|
<li> <b><tt>lfs.chdir (path)</tt></b> <br>
|
||||||
Changes the current
|
Changes the current
|
||||||
working directory to the given <tt>path</tt>.
|
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>
|
<a name="getcwd"></a>
|
||||||
<li> <b><tt>lfs.currentdir ()</tt></b> <br>
|
<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
|
The optional arguments <code>start</code> and <code>length</code> can be
|
||||||
used to specify a starting point and its length;
|
used to specify a starting point and its length;
|
||||||
both should be numbers.
|
both should be numbers.
|
||||||
This function returns a boolean indicating if the operation was successful;
|
Returns a boolean indicating if the operation was successful;
|
||||||
in case of error, it returns <code>false</code> plus a string describing the
|
in case of error, it returns <code>false</code> plus an error string.
|
||||||
error.
|
|
||||||
|
|
||||||
<a name="mkdir"></a>
|
<a name="mkdir"></a>
|
||||||
<li> <b><tt>lfs.mkdir (dirname)</tt></b> <br>
|
<li> <b><tt>lfs.mkdir (dirname)</tt></b> <br>
|
||||||
Creates a new directory.
|
Creates a new directory.
|
||||||
The argument is the name of the 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>
|
<a name="unlock"></a>
|
||||||
<li> <b><tt>lfs.unlock (filehandle[, start[, length]])</tt></b> <br>
|
<li> <b><tt>lfs.unlock (filehandle[, start[, length]])</tt></b> <br>
|
||||||
@@ -165,7 +167,7 @@ attrdir (".")
|
|||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
<small>
|
<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>
|
</small>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
15
src/lfs.c
15
src/lfs.c
@@ -9,7 +9,7 @@
|
|||||||
** lfs.lock (fh, mode)
|
** lfs.lock (fh, mode)
|
||||||
** lfs.unlock (fh)
|
** 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>
|
#include <errno.h>
|
||||||
@@ -64,10 +64,15 @@ typedef struct dir_data {
|
|||||||
*/
|
*/
|
||||||
static int change_dir (lua_State *L) {
|
static int change_dir (lua_State *L) {
|
||||||
const char *path = luaL_checkstring(L, 1);
|
const char *path = luaL_checkstring(L, 1);
|
||||||
if (chdir(path))
|
if (chdir(path)) {
|
||||||
luaL_error(L,"Unable to change working directory to '%s'\n%s\n",
|
lua_pushnil (L);
|
||||||
|
lua_pushfstring (L,"Unable to change working directory to '%s'\n%s\n",
|
||||||
path, chdir_error);
|
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 );
|
S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
|
||||||
#endif
|
#endif
|
||||||
lua_pushboolean (L, !fail);
|
lua_pushboolean (L, !fail);
|
||||||
|
if (fail)
|
||||||
|
lua_pushfstring (L, "%s", strerror(errno));
|
||||||
umask (oldmask);
|
umask (oldmask);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
26
teste.lua
26
teste.lua
@@ -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
40
tests/test.lua
Normal 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")
|
||||||
Reference in New Issue
Block a user