mirror of
https://github.com/lunarmodules/luafilesystem.git
synced 2026-07-22 02:53:06 +00:00
Acrescimo da funcao lfs.touch (lfs.c e test.lua).
Pequenas correcoes para evitar warnings de redefinicao de macros.
This commit is contained in:
4
Makefile
4
Makefile
@@ -1,10 +1,10 @@
|
||||
# $Id: Makefile,v 1.10 2005/01/18 09:36:11 tomas Exp $
|
||||
# $Id: Makefile,v 1.11 2005/01/18 10:48:02 tomas Exp $
|
||||
|
||||
T= lfs
|
||||
|
||||
include ./config
|
||||
|
||||
V= 1.0b
|
||||
V= 1.1b
|
||||
DIST_DIR= luafilesystem-$V
|
||||
TAR_FILE= $(DIST_DIR).tar.gz
|
||||
ZIP_FILE= $(DIST_DIR).zip
|
||||
|
||||
45
src/lfs.c
45
src/lfs.c
@@ -7,9 +7,10 @@
|
||||
** lfs.dir (path)
|
||||
** lfs.mkdir (path)
|
||||
** lfs.lock (fh, mode)
|
||||
** lfs.touch (filepath [, atime [, mtime]])
|
||||
** lfs.unlock (fh)
|
||||
**
|
||||
** $Id: lfs.c,v 1.14 2004/11/17 14:08:04 tomas Exp $
|
||||
** $Id: lfs.c,v 1.15 2005/01/18 10:48:02 tomas Exp $
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
@@ -17,6 +18,7 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <utime.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#include <direct.h>
|
||||
@@ -131,7 +133,7 @@ static int _file_lock (lua_State *L, FILE *fh, const char *mode, const long star
|
||||
case 'r': lkmode = _LK_NBLCK; break;
|
||||
case 'w': lkmode = _LK_NBLCK; break;
|
||||
case 'u': lkmode = _LK_UNLCK; break;
|
||||
default : luaL_error (L, "%s: invalid mode", funcname);
|
||||
default : return luaL_error (L, "%s: invalid mode", funcname);
|
||||
}
|
||||
if (!len) {
|
||||
fseek (fh, 0L, SEEK_END);
|
||||
@@ -345,13 +347,27 @@ static int dir_create_meta (lua_State *L) {
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifndef S_ISDIR
|
||||
#define S_ISDIR(mode) (mode&_S_IFDIR)
|
||||
#endif
|
||||
#ifndef S_ISREG
|
||||
#define S_ISREG(mode) (mode&_S_IFREG)
|
||||
#endif
|
||||
#ifndef S_ISLNK
|
||||
#define S_ISLNK(mode) (0)
|
||||
#endif
|
||||
#ifndef S_ISSOCK
|
||||
#define S_ISSOCK(mode) (0)
|
||||
#endif
|
||||
#ifndef S_ISFIFO
|
||||
#define S_ISFIFO(mode) (0)
|
||||
#endif
|
||||
#ifndef S_ISCHR
|
||||
#define S_ISCHR(mode) (mode&_S_IFCHR)
|
||||
#endif
|
||||
#ifndef S_ISBLK
|
||||
#define S_ISBLK(mode) (0)
|
||||
#endif
|
||||
#endif
|
||||
/*
|
||||
** Convert the inode protection mode to a string.
|
||||
@@ -380,6 +396,30 @@ static const char *mode2string (mode_t mode) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Set access time and modification values for file
|
||||
*/
|
||||
static int file_utime (lua_State *L) {
|
||||
const char *file = luaL_checkstring (L, 1);
|
||||
struct utimbuf utb, *buf;
|
||||
|
||||
if (lua_gettop (L) == 1) /* set to current date/time */
|
||||
buf = NULL;
|
||||
else {
|
||||
utb.actime = (time_t)luaL_optnumber (L, 2, 0);
|
||||
utb.modtime = (time_t)luaL_optnumber (L, 3, utb.actime);
|
||||
buf = &utb;
|
||||
}
|
||||
if (utime (file, buf)) {
|
||||
lua_pushnil (L);
|
||||
lua_pushfstring (L, "%s", strerror (errno));
|
||||
return 2;
|
||||
}
|
||||
lua_pushboolean (L, 1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** Get file information
|
||||
*/
|
||||
@@ -478,6 +518,7 @@ static const struct luaL_reg fslib[] = {
|
||||
{"dir", dir_iter_factory},
|
||||
{"lock", file_lock},
|
||||
{"mkdir", make_dir},
|
||||
{"touch", file_utime},
|
||||
{"unlock", file_unlock},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
@@ -32,9 +32,31 @@ 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") == nil, "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")
|
||||
local tmpdir = tmp.."/lfs_tmp_dir"
|
||||
assert (lfs.mkdir (tmpdir), "could not make a new directory")
|
||||
local attrib, errmsg = lfs.attributes (tmpdir)
|
||||
if not attrib then
|
||||
error ("could not get attributes of file `"..tmpdir.."':\n"..errmsg)
|
||||
else
|
||||
-- Change access time
|
||||
assert (lfs.touch (tmpdir, 11))
|
||||
local new_att = assert (lfs.attributes (tmpdir))
|
||||
assert (new_att.access == 11, "could not set access time")
|
||||
assert (new_att.modification == 11, "could not set modification time")
|
||||
-- Change access and modification time
|
||||
assert (lfs.touch (tmpdir, 33, 22))
|
||||
local new_att = assert (lfs.attributes (tmpdir))
|
||||
assert (new_att.access == 33, "could not set access time")
|
||||
assert (new_att.modification == 22, "could not set modification time")
|
||||
-- Restore access time to current value
|
||||
assert (lfs.touch (tmpdir))
|
||||
new_att = assert (lfs.attributes (tmpdir))
|
||||
assert (new_att.access == attrib.access)
|
||||
assert (new_att.modification == attrib.modification)
|
||||
end
|
||||
assert (os.remove (tmpdir), "could not remove new directory")
|
||||
assert (lfs.mkdir (tmpdir.."/lfs_tmp_dir") == false, "could create a directory inside a non-existent one")
|
||||
--
|
||||
assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file")
|
||||
assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory")
|
||||
print"Ok!"
|
||||
|
||||
Reference in New Issue
Block a user