forked from paketo-buildpacks/packit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_sorting.go
72 lines (63 loc) · 1.8 KB
/
link_sorting.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
package vacation
import (
"fmt"
"path/filepath"
"strings"
)
type link struct {
name string
path string
}
func sortLinks(symlinks []link) ([]link, error) {
// Create a map of all of the symlink names and where they are pointing to to
// act as a quasi-graph
index := map[string]string{}
for _, s := range symlinks {
index[filepath.Clean(s.path)] = s.name
}
// Check to see if the link name lies on the path of another symlink in
// the table or if it is another symlink in the table
//
// Example:
// path = dir/file
// a-symlink -> dir
// b-symlink -> a-symlink
// c-symlink -> a-symlink/file
shouldSkipLink := func(linkname, linkpath string) bool {
sln := strings.Split(linkname, "/")
for j := 0; j < len(sln); j++ {
if _, ok := index[linknameFullPath(linkpath, filepath.Join(sln[:j+1]...))]; ok {
return true
}
}
return false
}
// Iterate over the symlink map for every link that is found this ensures
// that all symlinks that can be created will be created and any that are
// left over are cyclically dependent
var links []link
maxIterations := len(index)
for i := 0; i < maxIterations; i++ {
for path, name := range index {
// If there is a match either of the symlink or it is on the path then
// skip the creation of this symlink for now
if shouldSkipLink(name, path) {
continue
}
links = append(links, link{
name: name,
path: path,
})
// Remove the created symlink from the symlink table so that its
// dependent symlinks can be created in the next iteration
delete(index, path)
break
}
}
// Check to see if there are any symlinks left in the map which would
// indicate a cyclical dependency
if len(index) > 0 {
return nil, fmt.Errorf("failed: max iterations reached: this link graph contains a cycle")
}
return links, nil
}