-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
261 lines (214 loc) · 5.39 KB
/
file.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
package main
import (
"os"
"io"
"fmt"
"sort"
"bufio"
"strings"
"path/filepath"
)
// check if path is directory
func checkDir(path string) (string, error) {
path = filepath.Clean(path)
fi, err := os.Stat(path)
if err != nil || !fi.IsDir() {
return path, fmt.Errorf("'%s' is not a directory", path)
}
return path, nil
}
// read text file by newline into string slice
func stringSliceFromFile(path string) (lines []string, err error) {
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
rd := bufio.NewReader(f)
for {
line, e := rd.ReadString('\n')
if e != nil {
if e == io.EOF {
break
}
return lines, e
}
line = strings.TrimSpace(line)
if len(line) > 0 {
lines = append(lines, line)
}
}
sort.Strings(lines)
return
}
// recursive walk directory path creating string slice of child paths
func stringSliceFromPathWalk(p string) (paths []string, err error) {
// closure to pass to filepath.Walk
walkFunc := func(path string, f os.FileInfo, err error) error {
// remove base path
path = strings.Replace(path, p, "", 1)
// if not blank (including separator)
if len(path) > 1 {
paths = append(paths, path[1:])
}
return err
}
err = filepath.Walk(p, walkFunc)
sort.Strings(paths)
return
}
// check if path exists & exec pathFunction for each iteration
type pathFunction func(fi os.FileInfo, path string)
func pathsThatExist(list []string, path string, f pathFunction) int {
count := 0
for i := range list {
fullpath := filepath.Join(path, list[i])
fi, err := os.Stat(fullpath)
if err != nil {
continue
}
count += 1
fmt.Printf("%s\n", list[i])
if f != nil {
f(fi, fullpath)
}
}
return count
}
// prompt confirmation before deleting files
func deleteConfirm(list []string, path string, in *os.File) bool {
fmt.Printf("\nSimulate delete from '%s'...\n", path)
result := false
count := pathsThatExist(list, path, nil)
if count > 0 {
fmt.Printf("\nConfirm delete files? (yes/no) ")
result = askConfirm(in)
}
return result
}
// remove all paths (dir & file)
func delete(list []string, path string) {
fmt.Printf("\nDelete from '%s'...\n", path)
_ = pathsThatExist(list, path, func(fi os.FileInfo, fullpath string) {
if fi.IsDir() {
os.RemoveAll(fullpath)
} else {
os.Remove(fullpath)
}
})
}
// copy new files & folders from srcPath to destPath
func copyAll(paths []string, srcPath, destPath string) (err error) {
for i := range paths {
// skip path if error while reading
fi, err := os.Stat(filepath.Join(srcPath, paths[i]))
if err != nil {
continue
}
// ensure dir created on dest
destDir := filepath.Dir(filepath.Join(destPath, paths[i]))
err = os.MkdirAll(destDir, 0777)
if err != nil {
return err
}
// continue of path is directory
if fi.IsDir() {
continue
}
src, _, found := mostRecentlyModified(paths[i], srcPath, destPath)
// only copy if dne or srcPath more recently modified
if !found || src == srcPath {
err = copyFile(paths[i], srcPath, destPath)
if err != nil {
return err
}
}
}
return
}
// return file path if one is more recently modified
func mostRecentlyModified(file, path1, path2 string) (string, string, bool) {
fi1, err := os.Stat(filepath.Join(path1, file))
if err != nil || fi1.IsDir() {
return "", "", false
}
fi2, err := os.Stat(filepath.Join(path2, file))
if err != nil || fi2.IsDir() {
return "", "", false
}
// modified timestamp not equal
if fi1.ModTime().Unix() != fi2.ModTime().Unix() {
// override flag option
switch flagForcePath {
case 1:
return path1, path2, true
case 2:
return path2, path1, true
}
// compared modified times
if fi1.ModTime().Unix() > fi2.ModTime().Unix() {
// update on path2
return path1, path2, true
} else if fi2.ModTime().Unix() > fi1.ModTime().Unix() {
// update on path1
return path2, path1, true
}
}
// modified timestamp equal
return "", "", true
}
// copy file srcPath to destPath
func copyFile(file, srcPath, destPath string) (err error) {
srcFile, err := os.Open(filepath.Join(srcPath, file))
if err != nil {
return
}
defer srcFile.Close()
srcInfo, err := srcFile.Stat()
if err != nil {
return
}
fmt.Printf("%s\n", file)
destFullpath := filepath.Join(destPath, file)
destFile, err := os.Create(destFullpath)
if err != nil {
return
}
defer destFile.Close()
_, err = io.Copy(destFile, srcFile)
if err != nil {
return
}
err = destFile.Sync()
if err != nil {
return
}
err = os.Chtimes(destFile.Name(), srcInfo.ModTime(), srcInfo.ModTime())
return
}
// rename folder src to dest. if dest already exists, append (x)
// to folder name, increment (x) until folder path not found
func RenameFolder(src, dest string) (string, error) {
// check if dest not found
_, err := os.Stat(dest)
if err == nil {
x := 0
for {
x += 1
newDir := fmt.Sprintf("%v (%v)", dest, x)
_, err := os.Stat(newDir)
if err != nil {
dest = newDir
break
}
}
}
// create all parent directories
err = os.MkdirAll(filepath.Dir(dest), 0777)
if err != nil {
return dest, err
}
// rename src to dest
err = os.Rename(src, dest)
return dest, err
}