forked from odeke-em/drive
/
move.go
258 lines (210 loc) · 6.7 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// 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"
"os"
"path/filepath"
)
type moveOpt struct {
src string
dest string
byId bool
keepParent bool
}
type RenameMode uint
const (
RenameNone RenameMode = 1 << iota
RenameRemote
RenameLocal
)
func canRenameLocal(rmode RenameMode) bool {
return (rmode & RenameLocal) != 0
}
func canRenameRemote(rmode RenameMode) bool {
return (rmode & RenameRemote) != 0
}
func cannotRename(rmode RenameMode) bool {
return !(canRenameLocal(rmode) || canRenameRemote(rmode))
}
func (g *Commands) Move(byId, keepParent bool) error {
argc := len(g.opts.Sources)
if argc < 2 {
return invalidArgumentsErr(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]
var composedError error = nil
for _, src := range rest {
prefix := commonPrefix(src, dest)
// Trying to nest a parent into its child
if prefix == src {
return illogicalStateErr(fmt.Errorf("%s cannot be nested into %s", src, dest))
}
opt := moveOpt{
src: src,
dest: dest,
byId: byId,
keepParent: keepParent,
}
if err := g.move(&opt); err != nil {
message := fmt.Sprintf("move: %s: %v", src, err)
composedError = reComposeError(composedError, message)
}
}
return composedError
}
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 makeErrorWithStatus(fmt.Sprintf("src('%s')", opt.src), err, StatusRemoteLookupFailed)
}
if remSrc == nil {
return nonExistantRemoteErr(fmt.Errorf("src: '%s' could not be found", opt.src))
}
if newParent, err = g.rem.FindByPath(opt.dest); err != nil {
return remoteLookupErr(fmt.Errorf("dest: '%s' %v", opt.dest, err))
}
if newParent == nil || !newParent.IsDir {
return illogicalStateErr(fmt.Errorf("dest: '%s' must be an existent 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 illogicalStateErr(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 illogicalStateErr(fmt.Errorf("move: trying to move fileId:%s to self fileId:%s", customQuote(dupCheck.Id), customQuote(remSrc.Id)))
}
if !g.opts.Force {
return overwriteAttemptedErr(fmt.Errorf("%s already exists. Use `%s` flag to override this behaviour", newFullPath, ForceKey))
}
}
// Avoid self-nesting
if remSrc.Id == newParent.Id {
return illogicalStateErr(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
}
if !opt.keepParent {
err = g.removeParent(remSrc.Id, opt.src)
}
return err
}
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 illogicalStateErr(fmt.Errorf("non existent parent '%s' for src", parentPath))
}
return g.rem.removeParent(fileId, parent.Id)
}
func (g *Commands) renameLocal(oldRelToRootPath, newRelToRootPath string) error {
oldLocalPath := g.context.AbsPathOf(oldRelToRootPath)
newLocalPath := g.context.AbsPathOf(newRelToRootPath)
return os.Rename(oldLocalPath, newLocalPath)
}
func (g *Commands) renameRemote(remSrcId, parentPath, newName string) error {
urlBoundName := urlToPath(newName, true)
newFullPath := filepath.Join(parentPath, urlBoundName)
dupCheck, err := g.rem.FindByPath(newFullPath)
if err == nil && dupCheck != nil {
if dupCheck.Id == remSrcId { // Trying to rename self
return nil
}
if !g.opts.Force {
return overwriteAttemptedErr(fmt.Errorf("%s already exists. Use `%s` flag to override this behaviour", newFullPath, ForceKey))
}
}
_, err = g.rem.rename(remSrcId, newName)
return err
}
func (g *Commands) Rename(byId bool) error {
if len(g.opts.Sources) < 2 {
return invalidArgumentsErr(fmt.Errorf("rename: expecting <src> <newname>"))
}
if cannotRename(g.opts.RenameMode) {
return illogicalStateErr(fmt.Errorf("no rename mode set, set either `local` or `remote` mode"))
}
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 illogicalStateErr(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]
renamedRemote := false
if canRenameRemote(g.opts.RenameMode) {
if err = g.renameRemote(remSrc.Id, parentPath, newName); err != nil {
return err
}
renamedRemote = true
}
if canRenameLocal(g.opts.RenameMode) {
// Otherwise time to mirror the same changes locally
oldRelToRootPath := sepJoin(RemoteSeparator, parentPath, remSrc.Name)
newRelToRootPath := sepJoin(RemoteSeparator, parentPath, newName)
err = g.renameLocal(oldRelToRootPath, newRelToRootPath)
if err != nil {
switch {
// If a local path doesn't exist we shouldn't try to report an error if
// they've already just renamed the remote path.
// A common case is renaming a remote file that isn't on disk.
// See Issue https://github.com/odeke-em/drive/issues/826.
case NotExist(err) && renamedRemote:
err = nil
default:
return err
}
}
}
return err
}