-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs-mover.go
153 lines (126 loc) Β· 2.77 KB
/
fs-mover.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
package nef
import (
"os"
"sync"
"github.com/snivilised/nefilim/internal/third/lo"
)
const (
moveOpName = "Move"
)
type (
bitmask struct {
fromExists bool
toExists bool
fromIsDir bool
toIsDir bool
}
mover interface {
create() mover
move(from, to string) error
}
moveFunc func(from, to string) error
movers map[bitmask]moveFunc
baseMover struct {
root string
fS MoverFS
calc PathCalc
actions movers
}
)
func noOp(_, _ string) error {
return nil
}
func (m *baseMover) move(from, to string) error {
mask := m.query(from, to)
if action, exists := m.actions[mask]; exists {
if err := action(from, to); err != nil {
return err
}
return nil
}
return NewInvalidBinaryFsOpError(moveOpName, from, to)
}
func (m *baseMover) query(from, to string) bitmask {
fromExists, fromIsDir := m.peek(from)
toExists, toIsDir := m.peek(to)
return bitmask{
fromExists: fromExists,
toExists: toExists,
fromIsDir: fromIsDir,
toIsDir: toIsDir,
}
}
func (m *baseMover) peek(name string) (exists, isDir bool) {
if m.fS.DirectoryExists(name) {
return true, true
}
if m.fS.FileExists(name) {
return true, false
}
return false, false
}
func (m *baseMover) moveItemWithName(from, to string) error {
// 'to' includes the file name eg:
// from/file.txt => to/file.txt
//
if m.calc.Dir(from) == m.calc.Dir(to) {
return NewRejectSameDirMoveError(moveOpName, from, to)
}
return os.Rename(
m.calc.Join(m.root, from),
m.calc.Join(m.root, to),
)
}
func (m *baseMover) moveItemWithoutName(from, to string) error {
// 'to' does not include the file name, so it has to be appended, eg:
// from/file.txt => to/
//
return os.Rename(
m.calc.Join(m.root, from),
m.calc.Join(m.root, to, m.calc.Base(from)),
)
}
func (m *baseMover) moveItemWithoutNameClash(from, to string) error {
fromBase := m.calc.Base(from)
toBase := m.calc.Base(to)
if fromBase == toBase {
// If there were a merge facility, this is where we would implement this,
// ie merge the from directory with to, instead of returning an error.
//
return NewRejectSameDirMoveError(moveOpName, from, to)
}
return m.moveItemWithoutName(from, to)
}
type lazyMover struct {
once sync.Once
mover mover
}
func (l *lazyMover) instance(root string, overwrite bool, fS MoverFS) mover {
l.once.Do(func() {
l.mover = l.create(root, overwrite, fS)
})
return l.mover
}
func (l *lazyMover) create(root string, overwrite bool, fS MoverFS) mover {
calc := fS.Calc()
return lo.TernaryF(overwrite,
func() mover {
return &overwriteMover{
baseMover: baseMover{
root: root,
fS: fS,
calc: calc,
},
}
},
func() mover {
return &tentativeMover{
baseMover: baseMover{
root: root,
fS: fS,
calc: calc,
},
}
},
).create()
}