-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs-changer.go
166 lines (135 loc) Β· 3.43 KB
/
fs-changer.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
package nef
import (
"os"
"strings"
"sync"
"github.com/snivilised/nefilim/internal/third/lo"
)
const (
changeOpName = "Change"
)
type (
changer interface {
create() changer
change(from, to string) error
}
changeFunc func(from, to string) error
changers map[bitmask]changeFunc
baseChanger struct {
baseOp[ChangerFS]
actions changers
}
)
func (m *baseChanger) guard(_, to string) error {
if strings.Contains(to, "/") {
return NewInvalidPathError(
"move rejected, change 'to' path can't contain separator", to,
)
}
return nil
}
func (m *baseChanger) change(from, to string) error {
if err := m.guard(from, to); err != nil {
return err
}
mask := m.query(from, to)
if action, exists := m.actions[mask]; exists {
return action(from, to)
}
return NewInvalidBinaryFsOpError(moveOpName, from, to)
}
func (m *baseChanger) query(from, to string) bitmask {
fromExists, fromIsDir := m.peek(from)
toExists, toIsDir := m.peek(m.fill(from, to))
return bitmask{
fromExists: fromExists,
toExists: toExists,
fromIsDir: fromIsDir,
toIsDir: toIsDir,
}
}
func (m *baseChanger) fill(from, to string) string {
// returns the parent from 'from' combined with 'to', ie
// given: from: 'foo/bar/baz.txt', to: 'pez.txt'
// returns 'foo/bar/pez.txt'
//
return Join(Parent(from), to)
}
func (m *baseChanger) rename(from, to string) error {
destination := m.fill(from, to)
if from == destination {
return nil
}
return os.Rename(
m.calc.Join(m.root, from),
m.calc.Join(m.root, destination),
)
}
type lazyChanger struct {
once sync.Once
changer changer
}
func (l *lazyChanger) instance(root string, overwrite bool, fS ChangerFS) changer {
l.once.Do(func() {
l.changer = l.create(root, overwrite, fS)
})
return l.changer
}
func (l *lazyChanger) create(root string, overwrite bool, fS ChangerFS) changer {
// create an interface for this function
//
calc := fS.Calc()
return lo.TernaryF(overwrite,
func() changer {
return &overwriteChanger{
baseChanger: baseChanger{
baseOp: baseOp[ChangerFS]{
fS: fS,
calc: calc,
root: root,
},
},
}
},
func() changer {
return &tentativeChanger{
baseChanger: baseChanger{
baseOp: baseOp[ChangerFS]{
fS: fS,
calc: calc,
root: root,
},
},
}
},
).create()
}
type overwriteChanger struct {
baseChanger
}
func (m *overwriteChanger) create() changer {
m.actions = changers{
{true, false, false, false}: m.rename, // from exists as file, to does not exist
{true, false, true, false}: m.rename, // from exists as dir, to does not exist
{true, true, true, true}: m.rename, // from exists as dir, to exists as dir
{true, true, false, false}: m.rename, // from and to refer to the same existing file
}
return m
}
type tentativeChanger struct {
baseChanger
}
func (m *tentativeChanger) create() changer {
m.actions = changers{
{true, false, false, false}: m.rename, // from exists as file, to does not exist
{true, false, true, false}: m.rename, // from exists as dir, to does not exist
{true, true, true, true}: m.rename, // from exists as dir, to exists as dir
{true, true, false, false}: m.rejectFileOverwrite, // from and to may refer to the same existing file
}
return m
}
func (m *tentativeChanger) rejectFileOverwrite(from, to string) error {
// to file already exists
//
return NewInvalidBinaryFsOpError("Change", from, to)
}