-
Notifications
You must be signed in to change notification settings - Fork 3
/
dirtree.go
159 lines (143 loc) · 3.58 KB
/
dirtree.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
// This file is part of bitrot, a bitrot scrubber in Go
// http://github.com/marcopaganini/bitrot
// (C) 2014 by Marco Paganini <[email protected]>
package main
import (
"compress/gzip"
"crypto/md5"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"time"
)
// FileInfo holds file metadata about each file in the filesystem.
type FileInfo struct {
Size int64
Mode os.FileMode
Mtime time.Time
Md5sum []byte
}
// DirTree contains the metadata information about all files in a given
// mountpoint.
type DirTree struct {
Root string
ExcludeRe []*string
Files map[string]*FileInfo
}
// NewDirTree creates a new DirTree struct.
func NewDirTree(root string, excludeRe []*string) *DirTree {
dt := &DirTree{
Root: root,
ExcludeRe: excludeRe,
}
dt.Files = make(map[string]*FileInfo)
return dt
}
func (d *DirTree) compareFile(fname string) (string, error) {
var (
memfi *FileInfo
exists bool
)
osinfo, err := os.Stat(fname)
if err != nil {
return "", err
}
md5sum, err := md5sum(fname)
if err != nil {
return "", err
}
if memfi, exists = d.Files[fname]; !exists {
// If medatada changed, replace entry silently
memfi = &FileInfo{
Size: osinfo.Size(),
Mode: osinfo.Mode(),
Mtime: osinfo.ModTime(),
Md5sum: md5sum,
}
d.Files[fname] = memfi
Log.Verbosef(1, "[New] %s (%x)\n", fname, md5sum)
} else {
if osinfo.ModTime() != memfi.Mtime || osinfo.Mode() != memfi.Mode || osinfo.Size() != memfi.Size {
// Exists: If medatada changed, replace entry silently
Log.Verbosef(1, "[Metadata changes] %s (%x)\n", fname, md5sum)
memfi.Size = osinfo.Size()
memfi.Mode = osinfo.Mode()
memfi.Mtime = osinfo.ModTime()
memfi.Md5sum = md5sum
d.Files[fname] = memfi
} else {
Log.Verbosef(1, "[No metadata changes] %s (%x)\n", fname, md5sum)
// Exists: No metadata changes. Report md5 differences
for k := range md5sum {
if memfi.Md5sum[k] != md5sum[k] {
return fmt.Sprintf("[MD5 Mismatch] %s (%x -> %x)", fname, memfi.Md5sum, md5sum), nil
}
}
}
}
return "", nil
}
// Compare compares all files in the DirTree to the files on disk. Adds new
// files found on disk to DirTree and replace Files with changed attributes.
// Report in case a file exists with the exact same attributes but different
// checksum.
func (d *DirTree) Compare() error {
filepath.Walk(d.Root, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return nil
}
if fi.Mode().IsRegular() {
msg, err := d.compareFile(path)
if err != nil {
return (fmt.Errorf("Error comparing file: %s: %q", path, err))
}
if msg != "" {
fmt.Println(msg)
}
}
return nil
})
return nil
}
// Save writes a JSON representation of the current DirTree struct to the
// specified io.Writer.
func (d *DirTree) Save(writer io.Writer) error {
zwriter := gzip.NewWriter(writer)
defer zwriter.Close()
enc := json.NewEncoder(zwriter)
err := enc.Encode(d)
if err != nil {
return err
}
return nil
}
// Load a JSON representation of DirTree from the specified io.Reader.
func (d *DirTree) Load(reader io.Reader) error {
zreader, err := gzip.NewReader(reader)
if err != nil {
return (fmt.Errorf("Error uncompressing state DB: %q", err))
}
defer zreader.Close()
dec := json.NewDecoder(zreader)
err = dec.Decode(d)
if err != nil {
return err
}
return nil
}
// Calculate the md5sum of a file
func md5sum(fname string) ([]byte, error) {
fd, err := os.Open(fname)
if err != nil {
return []byte{}, err
}
defer fd.Close()
hd := md5.New()
_, err = io.Copy(hd, fd)
if err != nil {
return []byte{}, err
}
return hd.Sum(nil), nil
}