fix handling of symlinks to a nonexistent target
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
diff --git a/lib/worktree.c b/lib/worktree.c
index 1f4dfd3..03438c0 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1007,8 +1007,9 @@ install_symlink(struct got_worktree *worktree, const char *ondisk_path,
}
/* Only allow symlinks pointing at paths within the work tree. */
- if (!got_path_is_child(resolved_path ? resolved_path : target_path,
- worktree->root_path, strlen(worktree->root_path))) {
+ if (!got_path_is_child(resolved_path ? resolved_path : (abspath ?
+ abspath : target_path), worktree->root_path,
+ strlen(worktree->root_path))) {
/* install as a regular file */
got_object_blob_rewind(blob);
err = install_blob(worktree, ondisk_path, path,
diff --git a/regress/cmdline/checkout.sh b/regress/cmdline/checkout.sh
index 8bb0b0d..6cde66b 100755
--- a/regress/cmdline/checkout.sh
+++ b/regress/cmdline/checkout.sh
@@ -509,8 +509,10 @@ function test_checkout_symlink {
(cd $testroot/repo && ln -s alpha alpha.link)
(cd $testroot/repo && ln -s epsilon epsilon.link)
(cd $testroot/repo && ln -s /etc/passwd passwd.link)
+ (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
+ (cd $testroot/repo && ln -s nonexistent nonexistent.link)
(cd $testroot/repo && git add .)
- git_commit $testroot/repo -m "add a symlink"
+ git_commit $testroot/repo -m "add symlinks"
got checkout $testroot/repo $testroot/wt > /dev/null
ret="$?"
@@ -565,9 +567,28 @@ function test_checkout_symlink {
ret="$?"
if [ "$ret" != "0" ]; then
diff -u $testroot/content.expected $testroot/content
+ test_done "$testroot" "$ret"
+ return 1
fi
- test_done "$testroot" "$ret"
+ readlink $testroot/wt/epsilon/beta.link > $testroot/stdout
+ echo "../beta" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ readlink $testroot/wt/nonexistent.link > $testroot/stdout
+ echo "nonexistent" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
}
run_test test_checkout_basic