-
Notifications
You must be signed in to change notification settings - Fork 2
/
file-tree.go
268 lines (240 loc) · 6.61 KB
/
file-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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package syncbox
import (
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
)
// constants for files
const (
indent = "\t"
DigestFileName = ".sb-digest.json"
TestDir = "/test-target"
DsStore = ".DS_Store"
GitFolder = ".git"
)
// variables default time
var (
DefaultTime = time.Date(0, time.January, 0, 0, 0, 0, 0, time.UTC)
IgnoreList = []string{DigestFileName, DsStore, GitFolder}
)
// Checksum alias [16]byte to Checksum
type Checksum [16]byte
// Files alias of map of files
type Files map[Checksum]*File
// Dirs alias of map of directories
type Dirs map[Checksum]*Dir
// Object base type of file system objects
type Object struct {
IsDir bool `json:"isDir"`
ModTime time.Time `json:"modTime"`
Mode os.FileMode `json:"mode"`
Name string `json:"name"`
Size int64 `json:"size"`
ContentChecksum Checksum `json:"contentChecksum"`
Path string `json:"path"`
walked bool
}
// Dir directory representation
type Dir struct {
*Object
Files Files `json:"files"`
Dirs Dirs `json:"dirs"`
}
// File file representation
type File struct {
*Object
}
// NewObject instantiate Object
func NewObject(info os.FileInfo, path string) *Object {
return &Object{
IsDir: info.IsDir(),
ModTime: info.ModTime(),
Mode: info.Mode(),
Name: info.Name(),
Size: info.Size(),
ContentChecksum: genEmptyChecksum(),
Path: path,
walked: false,
}
}
// NewDir instantiate Dir
func NewDir(info os.FileInfo, path string) *Dir {
return &Dir{
Object: NewObject(info, path),
Files: make(Files),
Dirs: make(Dirs),
}
}
// NewEmptyDir instantiates a Dir with empty ContentChecksum, to make Compare works with empty dir
func NewEmptyDir() *Dir {
return &Dir{
Object: &Object{
ContentChecksum: genEmptyChecksum(),
ModTime: DefaultTime,
},
}
}
// NewFile instantiate File
func NewFile(info os.FileInfo, path string) *File {
return &File{
Object: NewObject(info, path),
}
}
func (o *Object) String() string {
return o.toString(0)
}
func (dir *Dir) String() string {
return dir.toString(0)
}
func (f *File) String() string {
return f.Object.toString(0)
}
func (o *Object) toString(depth int) string {
prefix := strings.Repeat(indent, depth)
str := fmt.Sprintf("%vPath: %v\n", prefix, o.Path)
str += fmt.Sprintf("%vIsDir: %v\n", prefix, o.IsDir)
str += fmt.Sprintf("%vModTime: %v\n", prefix, o.ModTime)
str += fmt.Sprintf("%vMode: %v\n", prefix, o.Mode)
str += fmt.Sprintf("%vName: %v\n", prefix, o.Name)
str += fmt.Sprintf("%vSize: %v\n", prefix, o.Size)
str += fmt.Sprintf("%vContentChecksum: %v\n", prefix, o.ContentChecksum)
return str
}
func (dir *Dir) toString(depth int) string {
prefix := strings.Repeat(indent, depth)
str := fmt.Sprintf("%v", dir.Object.toString(depth))
str += fmt.Sprintf("%vfiles:\n", prefix)
for checksum, file := range dir.Files {
str += fmt.Sprintf("%v%v%v:\n%v\n", indent, prefix, checksum, file.toString(depth+1))
}
str += fmt.Sprintf("%vdirs:\n", prefix)
for checksum, dir := range dir.Dirs {
str += fmt.Sprintf("%v%v%v:\n%v\n", indent, prefix, checksum, dir.toString(depth+1))
}
return str
}
// MarshalJSON implements the json interface
// Files is map, no need to pass by pointer
func (fs Files) MarshalJSON() ([]byte, error) {
strMap := make(map[string]*File)
for checksum, file := range fs {
strMap[string(checksum[:])] = file
}
return json.MarshalIndent(strMap, "", "\t")
}
// UnmarshalJSON implements the json interface
// pass by reference because needs to change the target address of map reference
func (fs *Files) UnmarshalJSON(data []byte) error {
*fs = make(map[Checksum]*File)
strMap := make(map[string]*File)
if err := json.Unmarshal(data, &strMap); err != nil {
return err
}
for key, file := range strMap {
checksum := [16]byte{}
copy(checksum[:], []byte(key))
(*fs)[checksum] = file
}
return nil
}
// MarshalJSON json interface
// Dirs is map, no need to pass by pointer
func (ds Dirs) MarshalJSON() ([]byte, error) {
strMap := make(map[string]*Dir)
for checksum, dir := range ds {
strMap[string(checksum[:])] = dir
}
return json.MarshalIndent(strMap, "", "\t")
}
// UnmarshalJSON json interface
// pass by reference because needs to change the target address of map reference
func (ds *Dirs) UnmarshalJSON(data []byte) error {
*ds = map[Checksum]*Dir{}
strMap := make(map[string]*Dir)
if err := json.Unmarshal(data, &strMap); err != nil {
return err
}
for key, dir := range strMap {
var checksum Checksum
copy(checksum[:], []byte(key))
(*ds)[checksum] = dir
}
return nil
}
// ToJSON converts Dir to JSON string
func (dir *Dir) ToJSON() (string, error) {
jsonBytes, err := json.MarshalIndent(dir, "", "\t")
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
// RebornDir reborn directory from JSON string
func RebornDir(jsonStr string) (*Dir, error) {
jsonBytes := []byte(jsonStr)
restoredDir := Dir{}
if err := json.Unmarshal(jsonBytes, &restoredDir); err != nil {
return nil, err
}
return &restoredDir, nil
}
// Build walks through content of the path directory and builds a tree representation
func Build(path string) (*Dir, Checksum, error) {
var parentDir *Dir
var totalChecksum Checksum
parentDirFile, err := os.Open(path)
if err != nil {
return parentDir, totalChecksum, err
}
parentDirInfo, err := parentDirFile.Stat()
if err != nil {
return parentDir, totalChecksum, err
}
parentDir = NewDir(parentDirInfo, path)
digest := md5.New()
infos, err := ioutil.ReadDir(path)
if err != nil {
return parentDir, totalChecksum, err
}
for _, info := range infos {
if info.IsDir() {
dir, checksum, err := Build(path + "/" + info.Name())
if err != nil {
return parentDir, totalChecksum, err
}
parentDir.Dirs[checksum] = dir
digest.Write(checksum[:])
} else if shouldConsiderFile(info.Name()) {
content, err := ioutil.ReadFile(path + "/" + info.Name())
if err != nil {
return parentDir, totalChecksum, err
}
checksum := md5.Sum(content)
file := NewFile(info, path+"/"+info.Name())
file.ContentChecksum = checksum
parentDir.Files[checksum] = file
digest.Write(checksum[:])
}
copy(totalChecksum[:], digest.Sum(nil))
parentDir.ContentChecksum = totalChecksum
}
return parentDir, totalChecksum, nil
}
func shouldConsiderFile(name string) bool {
for _, item := range IgnoreList {
if name == item {
return false
}
}
return true
}
func genEmptyChecksum() Checksum {
checksumArr := md5.Sum(nil)
var checksum Checksum
copy(checksum[:], checksumArr[:])
return checksum
}