aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorReuben Thomas <rrt@sc3d.org>2011-06-09 19:49:29 +0100
committerReuben Thomas <rrt@sc3d.org>2011-06-09 19:53:41 +0100
commit3cea6c6e9612ca2b04b4742c04721d24f4b316be (patch)
treea3ad538475091c20bf9e4766afb6064a72c855fb /src
parent7e979318bd3d237b4fbf8da17b3ece75ac0343ba (diff)
downloadluafilesystem-3cea6c6e9612ca2b04b4742c04721d24f4b316be.tar.gz
luafilesystem-3cea6c6e9612ca2b04b4742c04721d24f4b316be.tar.bz2
luafilesystem-3cea6c6e9612ca2b04b4742c04721d24f4b316be.zip
Add lfs.link.
Diffstat (limited to 'src')
-rw-r--r--src/lfs.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/lfs.c b/src/lfs.c
index dd41df8..f33b5ae 100644
--- a/src/lfs.c
+++ b/src/lfs.c
@@ -107,6 +107,29 @@ typedef struct dir_data {
107#endif 107#endif
108 108
109/* 109/*
110** Utility functions
111*/
112static int pusherror(lua_State *L, const char *info)
113{
114 lua_pushnil(L);
115 if (info==NULL)
116 lua_pushstring(L, strerror(errno));
117 else
118 lua_pushfstring(L, "%s: %s", info, strerror(errno));
119 lua_pushinteger(L, errno);
120 return 3;
121}
122
123static int pushresult(lua_State *L, int i, const char *info)
124{
125 if (i==-1)
126 return pusherror(L, info);
127 lua_pushinteger(L, i);
128 return 1;
129}
130
131
132/*
110** This function changes the working (current) directory 133** This function changes the working (current) directory
111*/ 134*/
112static int change_dir (lua_State *L) { 135static int change_dir (lua_State *L) {
@@ -355,6 +378,25 @@ static int file_unlock (lua_State *L) {
355 378
356 379
357/* 380/*
381** Creates a link.
382** @param #1 Object to link to.
383** @param #2 Name of link.
384** @param #3 True if link is symbolic (optional).
385*/
386static int make_link(lua_State *L)
387{
388#ifndef _WIN32
389 const char *oldpath = luaL_checkstring(L, 1);
390 const char *newpath = luaL_checkstring(L, 2);
391 return pushresult(L,
392 (lua_toboolean(L,3) ? symlink : link)(oldpath, newpath), NULL);
393#else
394 pusherror(L, "make_link is not supported on Windows");
395#endif
396}
397
398
399/*
358** Creates a directory. 400** Creates a directory.
359** @param #1 Directory path. 401** @param #1 Directory path.
360*/ 402*/
@@ -763,6 +805,7 @@ static const struct luaL_Reg fslib[] = {
763 {"chdir", change_dir}, 805 {"chdir", change_dir},
764 {"currentdir", get_dir}, 806 {"currentdir", get_dir},
765 {"dir", dir_iter_factory}, 807 {"dir", dir_iter_factory},
808 {"link", make_link},
766 {"lock", file_lock}, 809 {"lock", file_lock},
767 {"mkdir", make_dir}, 810 {"mkdir", make_dir},
768 {"rmdir", remove_dir}, 811 {"rmdir", remove_dir},