Giter Site home page Giter Site logo

Comments (3)

robertlzj avatar robertlzj commented on June 15, 2024

Hi, in higher version, I think lfs.symlinkattributes(filepath).target is the real path? - subject path if filepath is symbolic path.

from luafilesystem.

tst2005 avatar tst2005 commented on June 15, 2024

@robertlzj no, the target value is the (absolute or relative path) of the symlink.

You can have a foo1 symlink pointing to bar or a foo2 symlink pointing to /home/xyz/bar

in /home/xyz/
foo1 -> bar
foo2 -> /home/xyz/bar

The realpath is use to travel in all "directories" and get out of any symlink to get a real uniq storage path.
It is usefull in more complicated case like

d /storage/
d /storage/users/
d /storage/users/xyz
l /home -> /storage/users
  • in this case the realpath of /home/xyz is /storage/users/xyz
  • in this case the realpath of /home/xyz/foo1 is /storage/users/xyz/bar and not /home/xyz/bar
  • in this case the realpath of /home/xyz/foo2 is /storage/users/xyz/bar and not /home/xyz/bar

If we want to implement the realpath job in lua, we need to

  • get the full path (the target value if it is absolute, or the current path + the target value), /home/xyz/bar in the current sample
  • start at the first left directory, if it is a link, substitute the value of the first directory by the symlink target value BUT before we also need to get the real path of this value.
  • do the same thing with the next item in the path

It is a little complex ... the system realpath know how to do this job perfectly !

Regards,

from luafilesystem.

robertlzj avatar robertlzj commented on June 15, 2024

Hi, thinks for your introduce about about realpath - the final path without symbolic.
I think it's easy? to iterate each directory and expand if it is symlink then recurs iterate, at last make up the realpath.
If there is realpath in system would be nice of cause.

And in windows, I had just compare lfs.link/lfs.symlinkattributes.target and ln.exe.
target also make sense on hardlink, if it is in virtual volume (which is treated as symlink/junction directory?), it points to actual directory of virtual volume.
target of hardlink in file property

Other test on multi levels of symlink, substitute virtual volume, and multi symlink parts in path.
ln lfs symlink symbolic link hard link source target destination multiple level
I had thought final source above is realptah.

local lfs=require'lfs'
local vb=[[G:\]]--virtual base (on virtual volume)
local ab=[[E:\VirtualVolume\]]--actual base
if not lfs.attributes(vb) then
	assert(lfs.attributes(ab))
	os.execute('subst '..vb..' '..ab)
end
lfs.chdir(vb)
assert(not lfs.attributes'D' and not lfs.attributes'S')
os.execute('mkdir D')--origin
assert(lfs.link('D','S',true))--symlink directory
--
lfs.chdir(vb..[[S\]])
local E={close=function()end}
assert(not (io.open'O.txt' or E):close() and not (io.open'S.txt' or E):close() and not (io.open'S2.txt' or E):close() and not (io.open'S2b.txt' or E):close() and not (io.open'H.txt' or E):close() and not (io.open'Hb.txt' or E):close() and not (io.open'H2.txt' or E):close() and not (io.open'H2b.txt' or E):close())
----O: origin, H: config as hardlink, S: symlink
io.open('O.txt','w'):close()
lfs.link('O.txt','S.txt',true)
--hardlink
lfs.link('S.txt','H.txt')
os.execute('ln >nul 2>nul S.txt Hb.txt')
--symlink level 2
lfs.link('S.txt','S2.txt',true)
os.execute('ln >nul 2>nul --symbolic S.txt S2b.txt')
--hardlink level 2
lfs.link('S2.txt','H2.txt')
os.execute('ln >nul 2>nul S2.txt H2b.txt')
----
assert(lfs.symlinkattributes'H.txt'.mode=='link')
assert(lfs.symlinkattributes'Hb.txt'.mode=='link')
assert(lfs.symlinkattributes'S.txt'.mode=='link')
assert(lfs.symlinkattributes'S2.txt'.mode=='link')
assert(lfs.symlinkattributes'S2b.txt'.mode=='link')
assert(lfs.symlinkattributes'H2.txt'.mode=='link')
assert(lfs.symlinkattributes'H2b.txt'.mode=='link')
----
local function ln_target(fn)--show the target of a symbolic link
	local r=io.popen('ln --symbolic '..fn)
	return string.match(r:read'a','%-> (.+)\n')
end
assert(ln_target'H.txt'=='O.txt')
assert(ln_target'Hb.txt'=='O.txt')
assert(ln_target'S.txt'=='O.txt')
assert(ln_target'S2.txt'=='S.txt')
assert(ln_target'S2b.txt'=='S.txt')
assert(ln_target'H2.txt'=='S.txt')
assert(ln_target'H2b.txt'=='S.txt')
----
local t=(lfs.attributes(ab) and ab or vb)..[[D\O.txt]]
assert(lfs.symlinkattributes'H.txt'.target==t)
assert(lfs.symlinkattributes'Hb.txt'.target==t)
assert(lfs.symlinkattributes'S.txt'.target==t)
assert(lfs.symlinkattributes'S2.txt'.target==t)
assert(lfs.symlinkattributes'S2b.txt'.target==t)
assert(lfs.symlinkattributes'H2.txt'.target==t)
assert(lfs.symlinkattributes'H2b.txt'.target==t)
----
os.execute('del O.txt S.txt S2.txt S2b.txt H.txt Hb.txt H2.txt H2b.txt')
lfs.chdir(vb)
--
os.execute('rmdir D')
os.execute('rmdir S')
print'test done.'

from luafilesystem.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.