/
mergepath.go
223 lines (180 loc) · 5.08 KB
/
mergepath.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
package main
import (
"crypto/sha256"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/golang/leveldb"
"github.com/golang/leveldb/db"
)
const (
ERR_INVALID_PARAMS = 1 << iota
ERR_CREATE_TMP_DIR_FAILED = 1 << iota
ERR_OPEN_DB_FAILED = 1 << iota
)
type fileEntry struct {
srcPath string
dstPath string
csum []byte
}
type PrintfFunc func(string, ...interface{}) (int, error)
var DEBUG PrintfFunc = func(fmt string, a ...interface{}) (int, error) { return 0, nil }
//var DEBUG PrintfFunc = fmt.Printf
func Copy(src, dst string) (err error) {
var srcFd, dstFd *os.File
if srcFd, err = os.Open(src); err != nil {
return
}
defer srcFd.Close()
if err = os.MkdirAll(filepath.Dir(dst), (os.ModeDir | 0750)); err != nil {
return
}
// create a new file and ensure it doesn't exist (O_EXCL)
if dstFd, err = os.OpenFile(dst, (os.O_WRONLY | os.O_CREATE | os.O_EXCL), 0640); err != nil {
return
}
_, err = io.Copy(dstFd, srcFd)
if errClose := dstFd.Close(); errClose != nil {
if err != nil {
return err
} else {
return errClose
}
}
if err != nil {
// If io.Copy() failed but one cannot remove newly created copy, what
// can be done anymore?
os.Remove(dst)
}
return
}
func processFile(dbh *leveldb.DB, processingCh <-chan *fileEntry, move bool) {
var fent *fileEntry
more := true
for more {
select {
case fent, more = <-processingCh:
if more {
if _, err := dbh.Get(fent.csum, nil); err == db.ErrNotFound {
// file not processed yet
if move {
DEBUG("moving file %s to %s\n", fent.srcPath, fent.dstPath)
if err = os.MkdirAll(filepath.Dir(fent.dstPath), (os.ModeDir | 0750)); err != nil {
fmt.Printf("%s\n", err)
continue
}
if err = os.Rename(fent.srcPath, fent.dstPath); err != nil {
fmt.Printf("error: renaming file %s to %s failed. trying to copy instead.\n", fent.srcPath, fent.dstPath)
if err = Copy(fent.srcPath, fent.dstPath); err != nil {
fmt.Printf("error: Couldn't copy file %s to %s: %s\n", fent.srcPath, fent.dstPath, err)
continue
}
}
} else {
DEBUG("copying file %s to %s\n", fent.srcPath, fent.dstPath)
if err = Copy(fent.srcPath, fent.dstPath); err != nil {
fmt.Printf("error: Couldn't copy file %s to %s: %s\n", fent.srcPath, fent.dstPath, err)
continue
}
}
dbh.Set(fent.csum, []byte(fent.srcPath), nil)
} else if err == nil {
DEBUG("skipping file %s since it already exists.\n", fent.srcPath)
} else {
fmt.Printf("error: Unable to check whether %s exists: %s. Skipping.\n", fent.srcPath, err)
continue
}
}
}
}
}
func hashFile(path string) (csum []byte, err error) {
var file *os.File
hash := sha256.New()
if file, err = os.Open(path); err != nil {
return
} else {
// ignoring possible error in Close() on purpose; failed close on
// O_RDONLY fd is not fatal (until file descriptors are consumed)
defer file.Close()
if _, err = io.Copy(hash, file); err != nil {
return
} else {
csum = hash.Sum([]byte{})
}
}
return
}
func hashFiles(hashingCh <-chan *fileEntry, processingCh chan<- *fileEntry) {
defer close(processingCh)
var fent *fileEntry
var err error
more := true
for more {
select {
case fent, more = <-hashingCh:
if more {
if fent.csum, err = hashFile(fent.srcPath); err != nil {
fmt.Printf("error: Couldn't calculate checksum for file %s: %s\n", fent.srcPath, err)
} else {
processingCh <- fent
}
}
}
}
}
func cleanupDbDir(dbDir string) {
if err := os.RemoveAll(dbDir); err != nil {
fmt.Printf("error: Unable to cleanup all temporary files from %s: %s", dbDir, err)
}
}
func getPathProcessor(hashingCh chan<- *fileEntry, rootDir, dstDir string) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
hashingCh <- &fileEntry{
srcPath: path,
dstPath: filepath.Join(dstDir, strings.SplitN(path, rootDir, 2)[1]),
}
}
return err
}
}
func main() {
PROG_NAME := filepath.Base(os.Args[0])
var moveFiles bool
flag.BoolVar(&moveFiles, "m", false, "move files to destination instead of copying")
flag.Parse()
if len(flag.Args()) < 2 {
fmt.Printf("error: Invalid parameters\n")
fmt.Printf("usage: %s [-m] dst src ...\n", PROG_NAME)
os.Exit(ERR_INVALID_PARAMS)
}
// Initialize LevelDB
var dbh *leveldb.DB
if dbDir, err := ioutil.TempDir("", PROG_NAME); err != nil {
fmt.Printf("error: Creation of tmp dir failed: %s\n", err)
os.Exit(ERR_CREATE_TMP_DIR_FAILED)
} else {
defer cleanupDbDir(dbDir)
dbh, err = leveldb.Open(dbDir, &db.Options{})
if err != nil {
fmt.Printf("error: Temporary working DB initialization failed: %s", err)
os.Exit(ERR_OPEN_DB_FAILED)
}
defer dbh.Close()
}
// Create channels
hashingCh := make(chan *fileEntry)
defer close(hashingCh)
processingCh := make(chan *fileEntry)
// Go for it
go hashFiles(hashingCh, processingCh)
go processFile(dbh, processingCh, moveFiles)
for _, d := range flag.Args()[1:] {
filepath.Walk(d, getPathProcessor(hashingCh, d, flag.Args()[0]))
}
}