-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsynthesize.go
93 lines (86 loc) · 3.16 KB
/
synthesize.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package linkwarp
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"facette.io/natsort"
)
type Application struct {
Name string // Name
Path string // Path is the whole path, relative to the searchroot.
Executables map[string]string // Executables is a map from short name to full path (again, relative to searchroot) of executables known in this application.
}
type BinSynthesisCfg struct {
AppRoot string
BinRoot string
}
func (synthCfg *BinSynthesisCfg) UpdateLinks(appInfo Application) error {
// This is not handling race conditions particularly calmly, but neither is it doing anything dangerously wrong in those cases.
// Rerunning the system should cause it to converge.
for name, pth := range appInfo.Executables {
if err := func() error {
lnkPath := filepath.Join(synthCfg.BinRoot, name)
targetPath := filepath.Join(synthCfg.AppRoot, pth)
fi, err := os.Lstat(lnkPath)
if err != nil {
if os.IsNotExist(err) {
// Great. We get to make it.
if err := os.Symlink(targetPath, lnkPath); err != nil {
return fmt.Errorf("creating link failed: %w", err)
}
return nil
} else {
return err
}
}
// If something exists and it's a symlink, read it, and see if it "wins"; leave it be if so; otherwise replace it.
if fi.Mode()&os.ModeSymlink != 0 {
existing, err := os.Readlink(lnkPath)
if err != nil {
return fmt.Errorf("can't read existing link, so not sure how to procede: %w", err)
}
if existing == targetPath {
return nil
}
// Compare.
// Re-infer the application name from the symlink's target string, then size it up with the new one.
existingChunks := strings.Split(existing, string(filepath.Separator))
existingAppName := ""
for i := len(existingChunks) - 1; i > 0; i-- {
if existingChunks[i] == "bin" { // note that this is the bit that makes a fool out of IsAppDirPredicate. hard to be that flexible here.
existingAppName = existingChunks[i-1]
break
}
}
// If the existing one looks bigger, skip out with no changes.
if !natsort.Compare(existingAppName, appInfo.Name) {
fmt.Printf(" leaving link for %q: %q looked older than %q\n", name, appInfo.Name, existingAppName)
return nil
}
// Overwrite.
// Creating a temp file and renaming it into place. This is borderline overkill, but it if you want a zero-downtime in-place-updates behavior, this is what you want.
fmt.Printf(" updating link for %q: %q looked newer than %q\n", name, appInfo.Name, existingAppName)
for {
tmpName := strconv.Itoa(int(time.Now().Unix()))
tmpName = filepath.Join(synthCfg.BinRoot, ".tmp."+tmpName)
if err := os.Symlink(targetPath, tmpName); err != nil {
if os.IsExist(err) {
continue
}
return fmt.Errorf("updating link failed: %w", err)
}
return os.Rename(tmpName, lnkPath)
}
return nil
}
// If something existed and it wasn't a symlink, we're... not gonna touch that.
return fmt.Errorf("something existed at %q and wasn't a symlink; exiting in fear", lnkPath)
}(); err != nil {
return err
}
}
return nil
}