-
-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathfsutil.go
297 lines (237 loc) · 6.54 KB
/
fsutil.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package fsutil
import (
"bufio"
"errors"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/gopasspw/gopass/pkg/appdir"
"github.com/gopasspw/gopass/pkg/debug"
)
var reCleanFilename = regexp.MustCompile(`[^\w\[email protected]]`)
// CleanFilename strips all possibly suspicious characters from a filename
// WARNING: NOT suiteable for pathnames as slashes will be stripped as well!
func CleanFilename(in string) string {
return strings.Trim(reCleanFilename.ReplaceAllString(in, "_"), "_ ")
}
// ExpandHomedir expands the tilde to the users home dir (if present).
func ExpandHomedir(path string) string {
if len(path) > 1 && path[:2] == "~/" {
dir := filepath.Clean(appdir.UserHome() + path[1:])
debug.V(1).Log("Expanding %s to %s", path, dir)
return dir
}
debug.V(2).Log("No tilde found in %s", path)
return path
}
// CleanPath resolves common aliases in a path and cleans it as much as possible.
func CleanPath(path string) string {
// Only replace ~ if GOPASS_HOMEDIR is set. In that case we do expect any reference
// to the users homedir to be replaced by the value of GOPASS_HOMEDIR. This is mainly
// for testing and experiments. In all other cases we do want to leave ~ as-is.
if len(path) > 1 && path[:2] == "~/" {
if hd := os.Getenv("GOPASS_HOMEDIR"); hd != "" {
return filepath.Clean(hd + path[2:])
}
}
if p, err := filepath.Abs(path); err == nil && !strings.HasPrefix(path, "~") {
return p
}
return filepath.Clean(path)
}
// IsDir checks if a certain path exists and is a directory.
// https://stackoverflow.com/questions/10510691/how-to-check-whether-a-file-or-directory-denoted-by-a-path-exists-in-golang
func IsDir(path string) bool {
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
// not found
return false
}
debug.Log("failed to check dir %s: %s\n", path, err)
return false
}
return fi.IsDir()
}
// IsFile checks if a certain path is actually a file.
func IsFile(path string) bool {
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
// not found
return false
}
debug.Log("failed to check file %s: %s\n", path, err)
return false
}
return fi.Mode().IsRegular()
}
// IsNonEmptyFile checks if a certain path is a regular file and
// non-zero in size.
func IsNonEmptyFile(path string) bool {
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
// not found
return false
}
debug.Log("failed to check file %s: %s\n", path, err)
return false
}
if !fi.Mode().IsRegular() {
return false
}
return fi.Size() > 0
}
// IsEmptyDir checks if a certain path is an empty directory.
func IsEmptyDir(path string) (bool, error) {
empty := true
if err := filepath.Walk(path, func(fp string, fi os.FileInfo, ferr error) error {
if ferr != nil {
return ferr
}
if fi.IsDir() && (fi.Name() == "." || fi.Name() == "..") {
return filepath.SkipDir
}
if !fi.IsDir() {
empty = false
}
return nil
}); err != nil {
return false, fmt.Errorf("failed to walk %s: %w", path, err)
}
return empty, nil
}
// Shred overwrite the given file any number of times.
func Shred(path string, runs int) error {
fh, err := os.OpenFile(path, os.O_WRONLY, 0o600)
if err != nil {
return fmt.Errorf("failed to open file %q: %w", path, err)
}
// ignore the error. this is only taking effect if we error out.
defer func() {
_ = fh.Close()
}()
fi, err := fh.Stat()
if err != nil {
return fmt.Errorf("failed to stat file %q: %w", path, err)
}
flen := fi.Size()
// overwrite using pseudo-random data n-1 times and
// use zeros in the last iteration
bufFn := func() []byte {
buf := make([]byte, 1024)
_, _ = rand.Read(buf)
return buf
}
for i := range runs {
if i >= runs-1 {
bufFn = func() []byte {
return make([]byte, 1024)
}
}
if _, err := fh.Seek(0, 0); err != nil {
return fmt.Errorf("failed to seek to 0,0: %w", err)
}
var written int64
for {
// end of file
if written >= flen {
break
}
buf := bufFn()
n, err := fh.Write(buf[0:min(flen-written, int64(len(buf)))])
if err != nil {
if !errors.Is(err, io.EOF) {
return fmt.Errorf("failed to write to file: %w", err)
}
// end of file, should not happen
break
}
written += int64(n)
}
// if we fail to sync the written blocks to disk it'd be pointless
// do any further loops
if err := fh.Sync(); err != nil {
return fmt.Errorf("failed to sync to disk: %w", err)
}
}
if err := fh.Close(); err != nil {
return fmt.Errorf("failed to close file after writing: %w", err)
}
if err := os.Remove(path); err != nil {
return fmt.Errorf("failed to remove %s: %w", path, err)
}
return nil
}
// FileContains searches the given file for the search string and returns true
// iff it's an exact (substring) match.
func FileContains(path, needle string) bool {
fh, err := os.Open(path)
if err != nil {
debug.Log("failed to open %q for reading: %s", path, err)
return false
}
defer func() {
_ = fh.Close()
}()
s := bufio.NewScanner(fh)
for s.Scan() {
if strings.Contains(s.Text(), needle) {
return true
}
}
return false
}
func min(a, b int64) int64 {
if a < b {
return a
}
return b
}
// CopyFile copies a file from src to dst. Permissions will be preserved. It is expected to
// fail if the destination does exist but is not writeable.
func CopyFile(from, to string) error {
rdr, err := os.Open(from)
if err != nil {
return fmt.Errorf("failed to open file %q for reading: %w", from, err)
}
defer func() {
_ = rdr.Close()
}()
rdrStat, err := rdr.Stat()
if err != nil {
return fmt.Errorf("failed to stat open file %q: %w", from, err)
}
wrt, err := os.OpenFile(to, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, rdrStat.Mode())
if err != nil {
return fmt.Errorf("failed to open file %q for writing: %w", to, err)
}
defer func() {
_ = wrt.Close()
}()
n, err := io.Copy(wrt, rdr)
if err != nil {
return fmt.Errorf("failed to copy content of %q to %q: %w", from, to, err)
}
debug.Log("copied %d bytes from %q to %q", n, from, to)
// sync permission, applies in case the destination did exist but had different perms
if err := os.Chmod(to, rdrStat.Mode()); err != nil {
return fmt.Errorf("failed to sync permissions to %q: %w", to, err)
}
return nil
}
// CopyFileForce copies a file from src to dst. Permissions will be preserved. The destination
// if removed before copying to avoid permission issues.
func CopyFileForce(from, to string) error {
if IsFile(to) {
if err := os.Remove(to); err != nil {
return fmt.Errorf("failed to remove %q: %w", to, err)
}
}
return CopyFile(from, to)
}