-
Notifications
You must be signed in to change notification settings - Fork 1
/
files.go
275 lines (239 loc) · 5.22 KB
/
files.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
269
270
271
272
273
274
275
package util
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"github.com/pkg/errors"
)
const (
DefaultWritePermissions = 0760
MaximumNewDirectoryAttempts = 1000
)
func FileExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func IsEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1) // Or f.Readdir(1)
if err == io.EOF {
return true, nil
}
return false, err // Either not empty or error, suits both cases
}
// CreateUniqueDirectory creates a new directory but if the combination of dir and name exists
// then append a number until a unique name is found
func CreateUniqueDirectory(dir string, name string, maximumAttempts int) (string, error) {
for i := 0; i < maximumAttempts; i++ {
n := name
if i > 0 {
n += strconv.Itoa(i)
}
p := filepath.Join(dir, n)
exists, err := FileExists(p)
if err != nil {
return p, err
}
if !exists {
err := os.MkdirAll(p, DefaultWritePermissions)
if err != nil {
return "", fmt.Errorf("Failed to create directory %s due to %s", p, err)
}
return p, nil
}
}
return "", fmt.Errorf("Could not create a unique file in %s starting with %s after %d attempts", dir, name, maximumAttempts)
}
func RenameDir(src string, dst string, force bool) (err error) {
err = CopyDir(src, dst, force)
if err != nil {
return fmt.Errorf("failed to copy source dir %s to %s: %s", src, dst, err)
}
err = os.RemoveAll(src)
if err != nil {
return fmt.Errorf("failed to cleanup source dir %s: %s", src, err)
}
return nil
}
func RenameFile(src string, dst string) (err error) {
if src == dst {
return nil
}
err = CopyFile(src, dst)
if err != nil {
return fmt.Errorf("failed to copy source file %s to %s: %s", src, dst, err)
}
err = os.RemoveAll(src)
if err != nil {
return fmt.Errorf("failed to cleanup source file %s: %s", src, err)
}
return nil
}
// credit https://gist.github.com/r0l1/92462b38df26839a3ca324697c8cba04
func CopyDir(src string, dst string, force bool) (err error) {
src = filepath.Clean(src)
dst = filepath.Clean(dst)
si, err := os.Stat(src)
if err != nil {
return err
}
if !si.IsDir() {
return fmt.Errorf("source is not a directory")
}
_, err = os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return
}
if err == nil {
if force {
os.RemoveAll(dst)
} else {
return fmt.Errorf("destination already exists")
}
}
err = os.MkdirAll(dst, si.Mode())
if err != nil {
return
}
entries, err := ioutil.ReadDir(src)
if err != nil {
return
}
for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())
if entry.IsDir() {
err = CopyDir(srcPath, dstPath, force)
if err != nil {
return
}
} else {
// Skip symlinks.
if entry.Mode()&os.ModeSymlink != 0 {
continue
}
err = CopyFile(srcPath, dstPath)
if err != nil {
return
}
}
}
return
}
// credit https://gist.github.com/r0l1/92462b38df26839a3ca324697c8cba04
func CopyFile(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
if e := out.Close(); e != nil {
err = e
}
}()
_, err = io.Copy(out, in)
if err != nil {
return
}
err = out.Sync()
if err != nil {
return
}
si, err := os.Stat(src)
if err != nil {
return
}
err = os.Chmod(dst, si.Mode())
if err != nil {
return
}
return
}
// CopyDirOverwrite copies from the source dir to the destination dir overwriting files along the way
func CopyDirOverwrite(src string, dst string) (err error) {
src = filepath.Clean(src)
dst = filepath.Clean(dst)
si, err := os.Stat(src)
if err != nil {
return err
}
if !si.IsDir() {
return fmt.Errorf("source is not a directory")
}
_, err = os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return
}
err = os.MkdirAll(dst, si.Mode())
if err != nil {
return
}
entries, err := ioutil.ReadDir(src)
if err != nil {
return
}
for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())
if entry.IsDir() {
err = CopyDirOverwrite(srcPath, dstPath)
if err != nil {
return
}
} else {
// Skip symlinks.
if entry.Mode()&os.ModeSymlink != 0 {
continue
}
err = CopyFile(srcPath, dstPath)
if err != nil {
return
}
}
}
return
}
// loads a file
func LoadBytes(dir, name string) ([]byte, error) {
path := filepath.Join(dir, name) // relative path
bytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error loading file %s in directory %s, %v", name, dir, err)
}
return bytes, nil
}
func DeleteFile(fileName string) (err error) {
if fileName != "" {
exists, err := FileExists(fileName)
if err != nil {
return fmt.Errorf("Could not check if file exists %s due to %s", fileName, err)
}
if exists {
err = os.Remove(fileName)
if err != nil {
return errors.Wrapf(err, "Could not remove file due to %s", fileName)
}
}
} else {
return fmt.Errorf("Filename is not valid")
}
return nil
}