aboutsummaryrefslogtreecommitdiff
path: root/src/lfs.c
diff options
context:
space:
mode:
authorNiteHawk <n1tehawk@users.noreply.github.com>2016-07-16 15:35:27 +0200
committerNiteHawk <n1tehawk@users.noreply.github.com>2016-07-16 15:35:27 +0200
commit32b355a9902e6ce7aae68926fc3f2d05856fc299 (patch)
treeffcda689b162126d3793a1f0b62ab05eec287250 /src/lfs.c
parent080f74f785b2f30708ae797c15fca95a418b028a (diff)
downloadluafilesystem-32b355a9902e6ce7aae68926fc3f2d05856fc299.tar.gz
luafilesystem-32b355a9902e6ce7aae68926fc3f2d05856fc299.tar.bz2
luafilesystem-32b355a9902e6ce7aae68926fc3f2d05856fc299.zip
Add a 'target' field for symlinkattributes.
It returns the resolved path of the symlink. Original version by Hisham <hisham@gobolinux.org>, modified to use a different strategy for sizing the readlink() buffer.
Diffstat (limited to 'src/lfs.c')
-rw-r--r--src/lfs.c49
1 files changed, 48 insertions, 1 deletions
diff --git a/src/lfs.c b/src/lfs.c
index 446373c..93c1419 100644
--- a/src/lfs.c
+++ b/src/lfs.c
@@ -850,10 +850,57 @@ static int file_info (lua_State *L) {
850 850
851 851
852/* 852/*
853** Push the symlink target to the top of the stack.
854** Assumes the file name is at position 1 of the stack.
855** Returns 1 if successful (with the target on top of the stack),
856** 0 on failure (with stack unchanged, and errno set).
857*/
858static int push_link_target(lua_State *L) {
859#ifdef _WIN32
860 errno = ENOSYS;
861 return 0;
862#else
863 const char *file = luaL_checkstring(L, 1);
864 char *target = NULL;
865 int tsize, size = 256; /* size = initial buffer capacity */
866 while (1) {
867 target = realloc(target, size);
868 if (!target) /* failed to allocate */
869 return 0;
870 tsize = readlink(file, target, size);
871 if (tsize < 0) { /* a readlink() error occurred */
872 free(target);
873 return 0;
874 }
875 if (tsize < size)
876 break;
877 /* possibly truncated readlink() result, double size and retry */
878 size *= 2;
879 }
880 target[tsize] = '\0';
881 lua_pushlstring(L, target, tsize);
882 free(target);
883 return 1;
884#endif
885}
886
887/*
853** Get symbolic link information using lstat. 888** Get symbolic link information using lstat.
854*/ 889*/
855static int link_info (lua_State *L) { 890static int link_info (lua_State *L) {
856 return _file_info_ (L, LSTAT_FUNC); 891 int ret;
892 if (lua_isstring (L, 2) && (strcmp(lua_tostring(L, 2), "target") == 0)) {
893 int ok = push_link_target(L);
894 return ok ? 1 : pusherror(L, "could not obtain link target");
895 }
896 ret = _file_info_ (L, LSTAT_FUNC);
897 if (ret == 1 && lua_type(L, -1) == LUA_TTABLE) {
898 int ok = push_link_target(L);
899 if (ok) {
900 lua_setfield(L, -2, "target");
901 }
902 }
903 return ret;
857} 904}
858 905
859 906