aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/us/manual.html7
-rw-r--r--src/lfs.c43
-rw-r--r--tests/test.lua7
3 files changed, 55 insertions, 2 deletions
diff --git a/doc/us/manual.html b/doc/us/manual.html
index a7e2efb..996dc1f 100644
--- a/doc/us/manual.html
+++ b/doc/us/manual.html
@@ -206,6 +206,13 @@ LuaFileSystem offers the following functions:
206 Returns <code>true</code> if the operation was successful; in 206 Returns <code>true</code> if the operation was successful; in
207 case of error, it returns <code>nil</code> plus an error string. 207 case of error, it returns <code>nil</code> plus an error string.
208 </dd> 208 </dd>
209
210 <dt><a name="link"></a><strong><code>lfs.link (old, new[, symlink])</code></strong></dt>
211 <dd>Creates a link. The first argument is the object to link to
212 and the second is the name of the link. If the optional third
213 argument is true, the link will by a symbolic link (by default, a
214 hard link is created).
215 </dd>
209 216
210 <dt><a name="mkdir"></a><strong><code>lfs.mkdir (dirname)</code></strong></dt> 217 <dt><a name="mkdir"></a><strong><code>lfs.mkdir (dirname)</code></strong></dt>
211 <dd>Creates a new directory. The argument is the name of the new 218 <dd>Creates a new directory. The argument is the name of the new
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},
diff --git a/tests/test.lua b/tests/test.lua
index 81a0ab6..c4911c9 100644
--- a/tests/test.lua
+++ b/tests/test.lua
@@ -69,11 +69,14 @@ local new_att = assert (lfs.attributes (tmpfile))
69assert (new_att.access == testdate2, "could not set access time") 69assert (new_att.access == testdate2, "could not set access time")
70assert (new_att.modification == testdate1, "could not set modification time") 70assert (new_att.modification == testdate1, "could not set modification time")
71 71
72-- Checking symbolic link information (does not work in Windows) 72-- Checking link (does not work on Windows)
73if (os.execute ("ln -s "..tmpfile.." _a_link_for_test_")) then 73if lfs.link (tmpfile, "_a_link_for_test_", true) then
74 assert (lfs.attributes"_a_link_for_test_".mode == "file") 74 assert (lfs.attributes"_a_link_for_test_".mode == "file")
75 assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link") 75 assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link")
76 assert (lfs.link (tmpfile, "_a_hard_link_for_test_"))
77 assert (lfs.attributes (tmpfile, "nlink") == 2)
76 assert (os.remove"_a_link_for_test_") 78 assert (os.remove"_a_link_for_test_")
79 assert (os.remove"_a_hard_link_for_test_")
77end 80end
78 81
79-- Checking text/binary modes (only has an effect in Windows) 82-- Checking text/binary modes (only has an effect in Windows)