Skip to content

Commit

Permalink
sys/base/readlink: bugfix involving resolution of symlinks in $PWD
Browse files Browse the repository at this point in the history
Symlinks in the present working directory were not always resolved
when canonicalising a path using -f/-e/-m; if a relative path was
given that contained only one directory component (or only has an
initial ./), then readlink failed to use 'chdir' to resolve symlinks
in $PWD (the present working directory).

lib/modernish/mdl/sys/base/readlink.mm:
- Bugfix. Generally, '.' path components must be skipped so that
  'readlink -m' works correctly. This erroneously skipped an
  (implicit or given) initial './' so the PWD was not
  canonicalised. So add a line to special-case that.
      In that case, also make sure $PWD exists, and refuse to
  canonicalise (even with -m) if not; this behaviour matches GNU
  'readlink'.

lib/modernish/tst/sys.t:
- Add regression test that catches this bug.
  • Loading branch information
McDutchie committed May 15, 2020
1 parent b5b8d3d commit 73649a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/modernish/mdl/sys/base/readlink.mm
Expand Up @@ -100,6 +100,7 @@
# Canonicalise the path using 'chdir' to convert to physical path.
# If -m given, emulate traversal of nonexistent paths.
str in "$1" '/' || set -- "./$1"
str begin "$1" './' && { str ne "${PWD:-.}" '.' && is -L dir "$PWD" && chdir -f -- "$PWD" || \exit 0; }
{ str end "$1" '/.' || str end "$1" '/..'; } && set -- "$1/"
unset -v _Msh_nonexist
IFS='/'
Expand Down
13 changes: 13 additions & 0 deletions lib/modernish/tst/sys.t
Expand Up @@ -49,6 +49,19 @@ TEST title="readlink: perms enforced whl traversing?"
return 1
ENDT

TEST title='readlink: symlinks in $PWD are resolved'
# chdir into a symlink to a directory, canonicalise a path within it, check the symlink in $PWD is resolved.
v=$(
umask 077
mkdir -p $tempdir/sym/d1/d2
ln -s d1 $tempdir/sym/sym2dir
chdir -fL $tempdir/sym/sym2dir || exit 1
readlink -f d2
) || return 1
failmsg=$v
str eq $v $tempdir/sym/d1/d2
ENDT

# ... sys/base/tac ...

TEST title='tac: default'
Expand Down

0 comments on commit 73649a6

Please sign in to comment.