-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.go
52 lines (45 loc) · 1.29 KB
/
dir.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
// Copyright 2021 Wayback Archiver. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package helper // import "github.com/wabarc/helper"
import (
"errors"
"os"
"path/filepath"
"time"
)
// Writable ensures the directory exists and is writable
func Writable(dir string) error {
// Construct the dir if missing
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
// Check the directory is writable
if f, err := os.Create(filepath.Join(dir, "._check_writable")); err == nil {
f.Close()
os.Remove(f.Name())
} else {
return errors.New("'" + dir + "' is not writable")
}
return nil
}
// IsDir ensures directory of given path
func IsDir(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return fileInfo.IsDir()
}
// RetryRemoveAll will attempt to remove an item or directory up to the given number of retries.
// NOTE: This function is necessary because of a Windows bug with removing files that have been recently used: https://github.com/golang/go/issues/51442
func RetryRemoveAll(path string, retries int) error {
for i := 0; i < retries; i++ {
err := os.RemoveAll(path)
if err == nil {
return nil
}
time.Sleep(500 * time.Millisecond)
}
return os.RemoveAll(path)
}