local lfs=require'lfs'
lfs.chdir[[G:\]]
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())
----
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=[[G:\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')
print'test done.'