forked from Cloud-Foundations/Dominator
/
computedFiles.go
119 lines (113 loc) · 3.33 KB
/
computedFiles.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
package util
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"os"
"path"
"sort"
"strings"
"github.com/Cloud-Foundations/Dominator/lib/filesystem"
"github.com/Cloud-Foundations/Dominator/lib/fsutil"
)
func spliceComputedFiles(fs *filesystem.FileSystem,
computedFileList []ComputedFile) error {
if len(computedFileList) < 1 {
return nil
}
filenameToInodeTable := fs.FilenameToInodeTable()
inodeToFilenamesTable := fs.InodeToFilenamesTable()
for _, computedFile := range computedFileList {
inum, ok := filenameToInodeTable[computedFile.Filename]
if !ok {
return errors.New(computedFile.Filename + ": missing from image")
}
if filenames, ok := inodeToFilenamesTable[inum]; !ok {
panic(computedFile.Filename + ": no corresponding list of files")
} else if len(filenames) != 1 {
return fmt.Errorf("%s: multiple inodes: %d", computedFile.Filename,
len(filenames))
}
if inode, ok :=
fs.InodeTable[inum].(*filesystem.ComputedRegularInode); ok {
inode.Source = computedFile.Source
continue
}
if oldInode, ok := fs.InodeTable[inum].(*filesystem.RegularInode); !ok {
return fmt.Errorf("%s: type: %T is not a regular inode",
computedFile.Filename, fs.InodeTable[inum])
} else {
newInode := new(filesystem.ComputedRegularInode)
newInode.Mode = oldInode.Mode
newInode.Uid = oldInode.Uid
newInode.Gid = oldInode.Gid
newInode.Source = computedFile.Source
fs.InodeTable[inum] = newInode
}
}
fs.ComputeTotalDataBytes()
clearInodePointers(&fs.DirectoryInode, "")
return fs.RebuildInodePointers()
}
func loadComputedFiles(filename string) ([]ComputedFile, error) {
var computedFileList []ComputedFile
if strings.HasSuffix(filename, ".json") {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
reader := bufio.NewReader(file)
decoder := json.NewDecoder(reader)
if err := decoder.Decode(&computedFileList); err != nil {
return nil, errors.New("error decoding computed files list " +
err.Error())
}
} else {
lines, err := fsutil.LoadLines(filename)
if err != nil {
return nil, err
}
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) != 2 {
return nil, fmt.Errorf("bad line: %s", line)
}
computedFileList = append(computedFileList,
ComputedFile{fields[0], fields[1]})
}
}
return computedFileList, nil
}
func clearInodePointers(directoryInode *filesystem.DirectoryInode,
name string) {
for _, dirent := range directoryInode.EntryList {
if inode, ok := dirent.Inode().(*filesystem.DirectoryInode); ok {
clearInodePointers(inode, path.Join(name, dirent.Name))
}
dirent.SetInode(nil)
}
}
func mergeComputedFiles(base, overlay []ComputedFile) []ComputedFile {
computedFilesMap := make(map[string]string)
for _, computedFile := range base {
computedFilesMap[computedFile.Filename] = computedFile.Source
}
for _, computedFile := range overlay {
computedFilesMap[computedFile.Filename] = computedFile.Source
}
filenames := make([]string, 0, len(computedFilesMap))
for filename := range computedFilesMap {
filenames = append(filenames, filename)
}
sort.Strings(filenames)
newList := make([]ComputedFile, 0, len(computedFilesMap))
for _, filename := range filenames {
newList = append(newList, ComputedFile{
Filename: filename,
Source: computedFilesMap[filename],
})
}
return newList
}