-
Notifications
You must be signed in to change notification settings - Fork 360
/
directory.go
110 lines (97 loc) · 2.66 KB
/
directory.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
package pyramid
import (
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"sync"
)
// directory synchronizes between file operations that might change (create/delete) directories
type directory struct {
// ceilingDir is the root directory of the FS - shouldn't never be deleted
ceilingDir string
mu sync.Mutex
}
// deleteDirRecIfEmpty deletes the given directory if it is empty.
// It will continue to delete all parents directory if they are empty, until the ceilingDir.
// Passed dir path isn't checked for malicious referencing (e.g. "../../../usr") and should never be
// controlled by any user input.
func (d *directory) deleteDirRecIfEmpty(dir string) error {
d.mu.Lock()
defer d.mu.Unlock()
for dir != d.ceilingDir {
empty, err := isDirEmpty(dir)
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return err
}
if !empty {
return nil
}
if err := os.Remove(dir); err != nil {
return err
}
// move up to the parent dir
dir = path.Dir(dir)
}
return nil
}
func isDirEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer func() {
_ = f.Close()
}()
_, err = f.Readdirnames(1)
if errors.Is(err, io.EOF) {
return true, nil
}
return false, err
}
// createFile creates the file under the path and creates all parent dirs if missing.
func (d *directory) createFile(path string) (*os.File, error) {
d.mu.Lock()
defer d.mu.Unlock()
if err := d.ensureParentDir(path); err != nil {
return nil, err
}
return os.Create(path)
}
// renameFile will move the src file to dst location and creates all parent dirs if missing.
// If we fail, we check if the destination exists, as it is content addressable, and remove the source.
func (d *directory) renameFile(src, dst string) error {
d.mu.Lock()
defer d.mu.Unlock()
err := d.ensureParentDir(dst)
if err != nil {
return err
}
err = os.Rename(src, dst)
if err != nil {
// in case of an error we like to check if destination already exists and just remove the source.
// this is usually relevant for cases where merge between two branches will have the same result as the source branch.
// on Windows the destination file will be locked by one of the iterators that reading the left side of the merge and the
// writer will try to store the result we are trying to rename.
if _, err := os.Stat(dst); err == nil {
return os.Remove(src)
}
}
return err
}
func (d *directory) ensureParentDir(path string) error {
parentDir := filepath.Dir(path)
if parentDir == d.ceilingDir {
return nil
}
err := os.MkdirAll(parentDir, os.ModePerm)
if err != nil {
return fmt.Errorf("creating dir: %w", err)
}
return nil
}