forked from odeke-em/drive
/
move.go
186 lines (154 loc) · 4.59 KB
/
move.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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package drive
import (
"fmt"
"path/filepath"
)
type moveOpt struct {
src string
dest string
byId bool
}
func (g *Commands) Move(byId bool) (err error) {
argc := len(g.opts.Sources)
if argc < 2 {
return fmt.Errorf("move: expected <src> [src...] <dest>, instead got: %v", g.opts.Sources)
}
rest, dest := g.opts.Sources[:argc-1], g.opts.Sources[argc-1]
for _, src := range rest {
prefix := commonPrefix(src, dest)
// Trying to nest a parent into its child
if prefix == src {
return fmt.Errorf("%s cannot be nested into %s", src, dest)
}
opt := moveOpt{
src: src,
dest: dest,
byId: byId,
}
err = g.move(&opt)
if err != nil {
// TODO: Actually throw the error? Impact on UX if thrown?
fmt.Printf("move: %s: %v\n", src, err)
}
}
return nil
}
func (g *Commands) move(opt *moveOpt) (err error) {
var newParent, remSrc *File
srcResolver := g.rem.FindByPath
if opt.byId {
srcResolver = g.rem.FindById
}
if remSrc, err = srcResolver(opt.src); err != nil {
return fmt.Errorf("src('%s') %v", opt.src, err)
}
if remSrc == nil {
return fmt.Errorf("src: '%s' could not be found", opt.src)
}
if newParent, err = g.rem.FindByPath(opt.dest); err != nil {
return fmt.Errorf("dest: '%s' %v", opt.dest, err)
}
if newParent == nil || !newParent.IsDir {
return fmt.Errorf("dest: '%s' must be an existant folder", opt.dest)
}
if !opt.byId {
parentPath := g.parentPather(opt.src)
oldParent, parErr := g.rem.FindByPath(parentPath)
if parErr != nil && parErr != ErrPathNotExists {
return parErr
}
// TODO: If oldParent is not found, retry since it may have been moved temporarily at least
if oldParent != nil && oldParent.Id == newParent.Id {
return fmt.Errorf("src and dest are the same srcParentId %s destParentId %s",
customQuote(oldParent.Id), customQuote(newParent.Id))
}
}
newFullPath := filepath.Join(opt.dest, remSrc.Name)
// Check for a duplicate
var dupCheck *File
dupCheck, err = g.rem.FindByPath(newFullPath)
if err != nil && err != ErrPathNotExists {
return err
}
if dupCheck != nil {
if dupCheck.Id == remSrc.Id { // Trying to move to self
return fmt.Errorf("move: trying to move fileId:%s to self fileId:%s", customQuote(dupCheck.Id), customQuote(remSrc.Id))
}
if !g.opts.Force {
return fmt.Errorf("%s already exists. Use `%s` flag to override this behaviour", newFullPath, ForceKey)
}
}
// Avoid self-nesting
if remSrc.Id == newParent.Id {
return fmt.Errorf("move: cannot move '%s' to itself", opt.src)
}
if err = g.rem.insertParent(remSrc.Id, newParent.Id); err != nil {
return err
}
if opt.byId { // TODO: Also take out this current parent
return nil
}
return g.removeParent(remSrc.Id, opt.src)
}
func (g *Commands) removeParent(fileId, relToRootPath string) error {
parentPath := g.parentPather(relToRootPath)
parent, pErr := g.rem.FindByPath(parentPath)
if pErr != nil {
return pErr
}
if parent == nil {
return fmt.Errorf("non existant parent '%s' for src", parentPath)
}
return g.rem.removeParent(fileId, parent.Id)
}
func (g *Commands) Rename(byId bool) error {
if len(g.opts.Sources) < 2 {
return fmt.Errorf("rename: expecting <src> <newname>")
}
src := g.opts.Sources[0]
resolver := g.rem.FindByPath
if byId {
resolver = g.rem.FindById
}
remSrc, err := resolver(src)
if err != nil {
return fmt.Errorf("%s: %v", src, err)
}
if remSrc == nil {
return fmt.Errorf("%s does not exist", src)
}
var parentPath string
if !byId {
parentPath = g.parentPather(src)
} else {
parentPath = g.opts.Path
}
newName := g.opts.Sources[1]
urlBoundName := urlToPath(newName, true)
newFullPath := filepath.Join(parentPath, urlBoundName)
var dupCheck *File
dupCheck, err = g.rem.FindByPath(newFullPath)
if err == nil && dupCheck != nil {
if dupCheck.Id == remSrc.Id { // Trying to rename self
return nil
}
if !g.opts.Force {
return fmt.Errorf("%s already exists. Use `%s` flag to override this behaviour", newFullPath, ForceKey)
}
}
_, err = g.rem.rename(remSrc.Id, newName)
return err
}