mirror of
https://github.com/lunarmodules/luafilesystem.git
synced 2026-07-22 02:53:06 +00:00
Add lfs.link.
This commit is contained in:
@@ -206,6 +206,13 @@ LuaFileSystem offers the following functions:
|
||||
Returns <code>true</code> if the operation was successful; in
|
||||
case of error, it returns <code>nil</code> plus an error string.
|
||||
</dd>
|
||||
|
||||
<dt><a name="link"></a><strong><code>lfs.link (old, new[, symlink])</code></strong></dt>
|
||||
<dd>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).
|
||||
</dd>
|
||||
|
||||
<dt><a name="mkdir"></a><strong><code>lfs.mkdir (dirname)</code></strong></dt>
|
||||
<dd>Creates a new directory. The argument is the name of the new
|
||||
|
||||
43
src/lfs.c
43
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},
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user