-
-
Notifications
You must be signed in to change notification settings - Fork 480
/
link.go
73 lines (62 loc) · 1.82 KB
/
link.go
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
63
64
65
66
67
68
69
70
71
72
73
package fs
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/gopasspw/gopass/pkg/debug"
)
// addRel adds the required number of relative elements to go from dst back to
// src
func addRel(src, dst string) string {
for i := 0; i < strings.Count(dst, "/"); i++ {
src = "../" + src
}
return src
}
// longestCommonPrefix finds the longest common prefix directory
func longestCommonPrefix(l, r string) string {
var prefix string
for i := 0; i < len(l) && i < len(r); i++ {
if l[i] != r[i] {
prefix = l[:i]
break
}
}
if !strings.Contains(prefix, "/") {
return prefix
}
return prefix[:strings.LastIndex(prefix, "/")]
}
// Link creates a symlink
func (s *Store) Link(ctx context.Context, from, to string) error {
if runtime.GOOS == "windows" {
from = filepath.FromSlash(from)
to = filepath.FromSlash(to)
}
fromPath := filepath.Join(s.path, from)
toPath := filepath.Join(s.path, to)
prefix := longestCommonPrefix(fromPath, toPath)
fromRel := strings.TrimPrefix(fromPath, prefix+string(filepath.Separator))
toRel := strings.TrimPrefix(toPath, prefix+string(filepath.Separator))
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("can not get current working directory: %w", err)
}
defer func() {
os.Chdir(cwd)
}()
toDir := filepath.Dir(toPath)
if err := os.MkdirAll(toDir, 0700); err != nil {
return fmt.Errorf("failed to create destination dir %q: %w", toDir, err)
}
if err := os.Chdir(toDir); err != nil {
return fmt.Errorf("can no change to link dir %q: %w", toDir, err)
}
linkDst := addRel(fromRel, toRel)
debug.Log("path: %q\n\tfromPath:\t%q\n\ttoPath:\t\t%q\n\tprefix:\t\t%q\n\tfromRel:\t%q\n\ttoRel:\t\t%q\n\ttoDir:\t\t%q\n\tlinkDst:\t%q",
s.path, fromPath, toPath, prefix, fromRel, toRel, toDir, linkDst)
return os.Symlink(linkDst, filepath.Base(toRel))
}