Skip to content

Commit fe7b01f

Browse files
Kubuxuwhyrusleeping
authored andcommitted
Resolve symlink if it is directly referenced in cli (ipfs#2897)
* Resolve symlink if it is directly referenced in cli test: Directly referenced symlink should be resolved License: MIT Signed-off-by: Jakub Sztandera <[email protected]> * sharness: add test for symlink in the middle License: MIT Signed-off-by: Jakub Sztandera <[email protected]>
1 parent d72c9fc commit fe7b01f

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

commands/cli/parse.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,10 @@ func appendFile(fpath string, argDef *cmds.Argument, recursive, hidden bool) (fi
388388
}
389389

390390
fpath = filepath.ToSlash(filepath.Clean(fpath))
391-
391+
fpath, err := filepath.EvalSymlinks(fpath)
392+
if err != nil {
393+
return nil, err
394+
}
392395
stat, err := os.Lstat(fpath)
393396
if err != nil {
394397
return nil, err

test/sharness/t0044-add-symlink.sh

+17-6
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ test_description="Test add -w"
1111
test_expect_success "creating files succeeds" '
1212
mkdir -p files/foo &&
1313
mkdir -p files/bar &&
14+
mkdir -p files/badin
1415
echo "some text" > files/foo/baz &&
15-
ln -s files/foo/baz files/bar/baz &&
16-
ln -s files/does/not/exist files/bad
16+
ln -s ../foo/baz files/bar/baz &&
17+
ln -s files/does/not/exist files/badin/bad &&
18+
mkdir -p files2/a/b/c &&
19+
echo "some other text" > files2/a/b/c/foo &&
20+
ln -s b files2/a/d
1721
'
1822

1923
test_add_symlinks() {
@@ -23,27 +27,34 @@ test_add_symlinks() {
2327
'
2428

2529
test_expect_success "output looks good" '
26-
echo QmWdiHKoeSW8G1u7ATCgpx4yMoUhYaJBQGkyPLkS9goYZ8 > filehash_exp &&
30+
echo QmQRgZT6xVFKJLVVpJDu3WcPkw2iqQ1jqK1F9jmdeq9zAv > filehash_exp &&
2731
test_cmp filehash_exp filehash_out
2832
'
2933

30-
test_expect_success "adding a symlink adds the link itself" '
34+
test_expect_success "adding a symlink adds the file itself" '
3135
ipfs add -q files/bar/baz > goodlink_out
3236
'
3337

3438
test_expect_success "output looks good" '
35-
echo "QmdocmZeF7qwPT9Z8SiVhMSyKA2KKoA2J7jToW6z6WBmxR" > goodlink_exp &&
39+
echo QmcPNXE5zjkWkM24xQ7Bi3VAm8fRxiaNp88jFsij7kSQF1 > goodlink_exp &&
3640
test_cmp goodlink_exp goodlink_out
3741
'
3842

3943
test_expect_success "adding a broken symlink works" '
40-
ipfs add -q files/bad > badlink_out
44+
ipfs add -qr files/badin | head -1 > badlink_out
4145
'
4246

4347
test_expect_success "output looks good" '
4448
echo "QmWYN8SEXCgNT2PSjB6BnxAx6NJQtazWoBkTRH9GRfPFFQ" > badlink_exp &&
4549
test_cmp badlink_exp badlink_out
4650
'
51+
52+
test_expect_success "adding with symlink in middle of path is same as\
53+
adding with no symlink" '
54+
ipfs add -rq files2/a/b/c > no_sym &&
55+
ipfs add -rq files2/a/d/c > sym &&
56+
test_cmp no_sym sym
57+
'
4758
}
4859

4960
test_init_ipfs

0 commit comments

Comments
 (0)