-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
tree.go
175 lines (157 loc) · 3.88 KB
/
tree.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
"zenhack.net/go/sandstorm/capnp/spk"
)
// A set of files inside of a directory.
type Tree map[string]*File
// A node in a file system. We store the docker file system in this format
// between reading the image and converting it to an spk.Archive.
type File struct {
// If this is a directory, the contents of the directory by relative
// path. Otherwise, this will be nil.
kids Tree
// If this is a regular file, the bytes of the file (otherwise nil)
data []byte
// Whether this is an executable. Only meaningful for regular files.
isExe bool
// If this is a symlink, the target of the symlink. Otherwise "".
target string
}
// Return whether the file is a directory.
func (f *File) isDir() bool {
return f.kids != nil
}
// Merge the argument into this tree. Directories are merged recursively.
// Otherwise, files in the argument take precedence. The argument should
// not be used afterwards.
func (t Tree) Merge(other Tree) {
for k, vOther := range other {
vThis, ok := t[k]
if ok && vThis.isDir() && vOther.isDir() {
vThis.kids.Merge(vOther.kids)
} else {
t[k] = vOther
}
}
}
// Convert the tree into an sandstorm pacakge archive.
func (t Tree) ToArchive(dest spk.Archive) error {
files, err := dest.NewFiles(int32(len(t)))
if err != nil {
return err
}
return insertDir(files, t)
}
func getKeys(t Tree) []string {
keys := make([]string, 0, len(t))
for k, _ := range t {
keys = append(keys, k)
}
return keys
}
// Marshal the contents of a directory into an archive. `dest` must
// already have the correct length.
func insertDir(dest spk.Archive_File_List, t Tree) error {
// For the sake of reproducable builds, we sort the keys.
keys := getKeys(t)
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
for i, k := range keys {
if err := insertFile(dest.At(i), k, t[k]); err != nil {
return err
}
}
return nil
}
// Marshal a single file into an archive.
func insertFile(dest spk.Archive_File, name string, file *File) error {
err := dest.SetName(name)
if err != nil {
return err
}
switch {
case file.isDir():
kids, err := dest.NewDirectory(int32(len(file.kids)))
if err == nil {
err = insertDir(kids, file.kids)
}
case file.data != nil && file.isExe:
err = dest.SetExecutable(file.data)
case file.data != nil && !file.isExe:
err = dest.SetRegular(file.data)
default:
err = dest.SetSymlink(file.target)
}
return err
}
// Look for whiteout files in the tree, and remove both them and the files
// to which they point. See:
//
// https://github.com/moby/moby/blob/master/image/spec/v1.md
func removeWhiteout(t Tree) {
keys := getKeys(t)
for _, name := range keys {
if strings.HasPrefix(name, ".wh.") {
delete(t, name)
delete(t, name[len(".wh."):])
}
}
for _, file := range t {
if file.isDir() {
removeWhiteout(file.kids)
}
}
}
// Read a File from the local directory at `root`.
func readLocalFS(root string) (*File, error) {
fi, err := os.Lstat(root)
if err != nil {
return nil, err
}
mode := fi.Mode()
typ := mode & os.ModeType
switch typ {
case os.ModeDir:
t, err := readLocalFSTree(root)
return &File{kids: t}, err
case os.ModeSymlink:
target, err := os.Readlink(root)
return &File{target: target}, err
case 0:
// regular file
data, err := ioutil.ReadFile(root)
return &File{
data: data,
isExe: mode&0111 != 0,
}, err
default:
return nil, fmt.Errorf("%q: unsupported file type: '%v'", root, typ)
}
}
// Read the local directory at `root` into a tree.
func readLocalFSTree(root string) (Tree, error) {
f, err := os.Open(root)
if err != nil {
return nil, err
}
fis, err := f.Readdir(0)
f.Close()
if err != nil {
return nil, err
}
ret := make(Tree, len(fis))
for _, fi := range fis {
node, err := readLocalFS(root + "/" + fi.Name())
if err != nil {
return nil, err
}
ret[fi.Name()] = node
}
return ret, nil
}