-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathfile_list.go
More file actions
151 lines (132 loc) · 3.1 KB
/
file_list.go
File metadata and controls
151 lines (132 loc) · 3.1 KB
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
package embed_util
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/fs"
"net/http"
"os"
"path/filepath"
"sort"
)
type fileList struct {
ContentHash string `json:"contentHash"`
Files []fileListEntry `json:"files"`
}
type fileListEntry struct {
Name string `json:"name"`
Size int64 `json:"size"`
Mode fs.FileMode `json:"perm"`
Symlink string `json:"symlink,omitempty"`
Compressed bool `json:"compressed,omitempty"`
}
func readFileList(fileListStr string) (*fileList, error) {
var fl fileList
err := json.Unmarshal([]byte(fileListStr), &fl)
if err != nil {
return nil, err
}
return &fl, err
}
func buildFileListFromDir(dir string) (*fileList, error) {
var fl fileList
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
relPath, err := filepath.Rel(dir, path)
if err != nil {
return err
}
if relPath == "." || relPath == "files.json" {
return nil
}
fle := fileListEntry{
Name: filepath.ToSlash(relPath), // Fix for Windows, for an fs.FS, you should open files with forward slashes
Size: info.Size(),
Mode: info.Mode(),
}
if info.Mode().Type() == fs.ModeSymlink {
sl, err := os.Readlink(path)
if err != nil {
return err
}
fle.Symlink = sl
fle.Mode &= ^fs.ModePerm
} else if info.Mode().IsDir() {
fle.Size = 0
} else if info.Mode().IsRegular() {
fle.Compressed = shouldCompress(path)
}
fl.Files = append(fl.Files, fle)
return nil
})
if err != nil {
return nil, err
}
sort.Slice(fl.Files, func(i, j int) bool {
return fl.Files[i].Name < fl.Files[j].Name
})
return &fl, nil
}
func buildFileListFromFs(embedFs fs.FS) (*fileList, error) {
var fl fileList
err := fs.WalkDir(embedFs, ".", func(path string, d fs.DirEntry, err error) error {
if path == "." || path == "files.json" {
return nil
}
info, err := d.Info()
if err != nil {
return err
}
fle := fileListEntry{
Name: filepath.ToSlash(path), // Fix for Windows, for an fs.FS, you should open files with forward slashes
Size: info.Size(),
Mode: info.Mode() | 0o600,
}
if info.Mode().Type() == fs.ModeSymlink {
return fmt.Errorf("symlink not supported in buildFileListFromFs")
} else if info.Mode().IsDir() {
fle.Size = 0
}
fl.Files = append(fl.Files, fle)
return nil
})
if err != nil {
return nil, err
}
sort.Slice(fl.Files, func(i, j int) bool {
return fl.Files[i].Name < fl.Files[j].Name
})
return &fl, nil
}
func shouldCompress(path string) bool {
f, err := os.Open(path)
if err != nil {
return false
}
defer f.Close()
data := make([]byte, 512)
n, err := f.Read(data)
if err != nil {
return false
}
if http.DetectContentType(data[:n]) == "application/octet-stream" {
return true
}
return false
}
func (fl *fileList) toMap() map[string]fileListEntry {
m := make(map[string]fileListEntry)
for _, e := range fl.Files {
m[e.Name] = e
}
return m
}
func (fl *fileList) Hash() string {
h := sha256.New()
e := json.NewEncoder(h)
err := e.Encode(fl)
if err != nil {
panic(err)
}
return hex.EncodeToString(h.Sum(nil))
}