-
Notifications
You must be signed in to change notification settings - Fork 328
/
filemap.go
57 lines (45 loc) · 1.2 KB
/
filemap.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
package image
import (
"io/fs"
"os"
"github.com/dghubble/trie"
)
type fileType int
const (
RegularFile fileType = iota
Dir
)
// fileNode represents a file on a specific layer, mapping the contents to an extracted file on disk
type fileNode struct {
fileType fileType
isWhiteout bool
absoluteDiskPath string
virtualPath string
permission fs.FileMode
}
// fileMap represents all the files on a layer
type fileMap struct {
fileNodeTrie *trie.PathTrie
// TODO: Use hashset to speed up path lookups
}
func (filemap fileMap) OpenFile(path string) (fs.File, error) {
node, ok := filemap.fileNodeTrie.Get(path).(fileNode)
if !ok {
return nil, fs.ErrNotExist
}
return os.Open(node.absoluteDiskPath)
}
// AllFiles return all files that exist on the layer the FileMap is representing
func (filemap fileMap) AllFiles() []fileNode {
allFiles := []fileNode{}
// No need to check error since we are not returning any errors
_ = filemap.fileNodeTrie.Walk(func(key string, value interface{}) error {
node := value.(fileNode)
if node.fileType != RegularFile { // Only add regular files
return nil
}
allFiles = append(allFiles, value.(fileNode))
return nil
})
return allFiles
}