From 3cea6c6e9612ca2b04b4742c04721d24f4b316be Mon Sep 17 00:00:00 2001 From: Reuben Thomas Date: Thu, 9 Jun 2011 19:49:29 +0100 Subject: Add lfs.link. --- src/lfs.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src') diff --git a/src/lfs.c b/src/lfs.c index dd41df8..f33b5ae 100644 --- a/src/lfs.c +++ b/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}, -- cgit v1.2.3-55-g6feb