-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.go
187 lines (157 loc) · 3.97 KB
/
fs.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
package xfs
import (
"errors"
"fmt"
"github.com/hephbuild/heph/log/log"
"github.com/hephbuild/heph/utils/flock"
"github.com/hephbuild/heph/utils/xrand"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"syscall"
)
func RandPath(base, prefix, suffix string) string {
return filepath.Join(base, prefix+xrand.RandStr(16)+suffix)
}
func IsDirEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return true, nil
}
return false, err
}
defer f.Close()
// read in ONLY one file
_, err = f.Readdir(1)
// and if the file is EOF... well, the dir is empty.
if err == io.EOF {
return true, nil
}
return false, err
}
func PathExists(filename string) bool {
_, err := os.Lstat(filename)
return err == nil
}
func Touch(filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
return f.Close()
}
func CreateParentDir(path string) error {
if dir := filepath.Dir(path); dir != "." {
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
}
return nil
}
func WriteFileSync(name string, data []byte, perm os.FileMode) error {
f, err := os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}
_, err = f.Write(data)
if err1 := f.Sync(); err1 != nil && err == nil {
err = err1
}
if err1 := f.Close(); err1 != nil && err == nil {
err = err1
}
return err
}
// CloseEnsureROFD ensures the fd is ro; fixing the issue described in https://github.com/rust-lang/rust/issues/114554
func CloseEnsureROFD(f *os.File) error {
err := flock.Flock(f, false, true)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
f, err = os.OpenFile(f.Name(), os.O_RDONLY, 0)
if err != nil {
return err
}
defer f.Close()
err = flock.Flock(f, false, true)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
return nil
}
func DeleteDir(dir string, async bool) error {
rm, err := exec.LookPath("rm")
if err != nil {
return err
} else if !PathExists(dir) {
return nil // not an error, just don't need to do anything.
}
log.Tracef("Deleting %v", dir)
if async {
newDir := RandPath(os.TempDir(), filepath.Base(dir), "")
err = os.Rename(dir, newDir)
if err != nil {
// May be because os.TempDir() and the current dir aren't on the same device, try a sibling folder
newDir = RandPath(filepath.Dir(dir), filepath.Base(dir), "")
err1 := os.Rename(dir, newDir)
if err1 != nil {
log.Warnf("rename failed %v, deleting synchronously", err)
return DeleteDir(dir, false)
}
}
// Note that we can't fork() directly and continue running Go code, but ForkExec() works okay.
// Hence why we're using rm rather than fork() + os.RemoveAll.
_, err = syscall.ForkExec(rm, []string{rm, "-rf", newDir}, nil)
return err
}
out, err := exec.Command(rm, "-rf", dir).CombinedOutput()
if err != nil {
return fmt.Errorf("failed to remove directory: %s", string(out))
}
return nil
}
// Inspired from https://github.com/golang/go/blob/3c72dd513c30df60c0624360e98a77c4ae7ca7c8/src/cmd/go/internal/modfetch/fetch.go
func MakeDirsReadOnly(dir string) {
type pathMode struct {
path string
mode fs.FileMode
}
var dirs []pathMode // in lexical order
filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err == nil && d.IsDir() {
info, err := d.Info()
if err == nil && info.Mode()&0222 != 0 {
dirs = append(dirs, pathMode{path, info.Mode()})
}
}
return nil
})
// Run over list backward to chmod children before parents.
for i := len(dirs) - 1; i >= 0; i-- {
os.Chmod(dirs[i].path, dirs[i].mode&^0222)
}
}
func MakeDirsReadWrite(dir string) {
// Module cache has 0555 directories; make them writable in order to remove content.
filepath.WalkDir(dir, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return nil // ignore errors walking in file system
}
if info.IsDir() {
os.Chmod(path, 0777)
}
return nil
})
}