Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement lfs.realpath() #85

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
** lfs.lock (fh, mode)
** lfs.lock_dir (path)
** lfs.mkdir (path)
** lfs.realpath (path)
** lfs.rmdir (path)
** lfs.setmode (filepath, mode)
** lfs.symlinkattributes (filepath [, attributename])
Expand Down Expand Up @@ -909,6 +910,35 @@ static int link_info (lua_State *L) {
return ret;
}

/*
** Retrieve canonicalized absolute pathname using realpath()
** Upon success the function returns the resolved path, otherwise the return
** values are `nil`, an error message (string) and the error number.
**
** Note that the Windows implementation differs from the POSIX function,
** for example it doesn't test if the resolved path is actually reachable.
** E.g. "C:\foo\..\bar" will happily resolve to "C:\bar",
** even if neither C:\foo nor C:\bar exist.
*/
static int get_realpath (lua_State *L) {
const char *path = luaL_checkstring (L, 1);
char *resolved;
#ifdef _WIN32
resolved = _fullpath(NULL, path, 0);
#else
resolved = realpath(path, NULL);
#endif
if (resolved) {
lua_pushstring(L, resolved);
free(resolved);
return 1;
}
/* an error occured */
lua_pushnil(L);
lua_pushfstring(L, "lfs.realpath('%s') failed: %s", path, strerror(errno));
lua_pushinteger(L, errno);
return 3;
}

/*
** Assumes the table is on top of the stack.
Expand All @@ -931,6 +961,7 @@ static const struct luaL_Reg fslib[] = {
{"link", make_link},
{"lock", file_lock},
{"mkdir", make_dir},
{"realpath", get_realpath},
{"rmdir", remove_dir},
{"symlinkattributes", link_info},
{"setmode", lfs_f_setmode},
Expand Down
15 changes: 15 additions & 0 deletions tests/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ f:close()
io.write(".")
io.flush()

-- Check that realpath can resolve the parent of tmpdir (= "current")
assert(lfs.realpath(tmpdir..sep.."..") == current)

-- Non-existent paths will error on POSIX (errno 2), but not on Windows!
local ok, err = lfs.realpath("C:\\foo\\..\\bar")
if not ok then
assert(err:match("No such file or directory"))
else
assert(ok == "C:\\bar")
end

io.write(".")
io.flush()

-- Change access time
local testdate = os.time({ year = 2007, day = 10, month = 2, hour=0})
assert (lfs.touch (tmpfile, testdate))
Expand Down Expand Up @@ -95,6 +109,7 @@ if lfs.link (tmpfile, "_a_link_for_test_", true) then
assert (lfs.symlinkattributes("_a_link_for_test_", "target") == tmpfile)
assert (lfs.link (tmpfile, "_a_hard_link_for_test_"))
assert (lfs.attributes (tmpfile, "nlink") == 2)
assert (lfs.realpath"_a_link_for_test_" == tmpfile)
assert (os.remove"_a_link_for_test_")
assert (os.remove"_a_hard_link_for_test_")
end
Expand Down