-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs-mover-tentative.go
51 lines (42 loc) · 1.66 KB
/
fs-mover-tentative.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
package nef
type tentativeMover struct {
baseMover
}
func (m *tentativeMover) create() mover {
m.actions = movers{
{true, false, false, false}: m.moveItemWithName, // from exists as file, to does not exist
{true, false, true, false}: m.moveDirectoryWithName, // from exists as dir, to does not exist
{true, true, false, true}: m.moveItemWithoutName, // from exists as file,to exists as dir
{true, true, true, true}: m.moveItemWithoutNameClash, // from exists as dir, to exists as dir
{true, true, false, false}: m.rejectOverwriteOrNoOp, // from and to may refer to the same existing file
}
return m
}
func (m *tentativeMover) moveDirectoryWithName(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 m.moveItemWithName(from, to)
}
func (m *tentativeMover) moveItemWithoutName(from, to string) error {
// 'to' does not include the file name, so it has to be appended, eg:
// from/file.txt => to/
//
if _, err := m.fS.Stat(m.calc.Join(to, m.calc.Base(from))); err == nil {
return NewInvalidBinaryFsOpError("Move", from, to)
}
return m.baseMover.moveItemWithoutName(from, to)
}
func (m *tentativeMover) rejectOverwriteOrNoOp(from, to string) error {
// both file names exists, but they may or may not be the same item. If
// they are not in the same location then we reject the overwrite attempt
// otherwise they are the same item and this should effectively be a no op.
//
if m.calc.Dir(from) != m.calc.Dir(to) {
return NewInvalidBinaryFsOpError(moveOpName, from, to)
}
return nil
}