-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathtree.go
390 lines (331 loc) · 10.9 KB
/
tree.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Copyright 2018-2020 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package ocis
import (
"context"
"os"
"path/filepath"
"time"
userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/user"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/pkg/xattr"
"github.com/rs/zerolog/log"
)
// Tree manages a hierarchical tree
type Tree struct {
lu *Lookup
}
// NewTree creates a new Tree instance
func NewTree(lu *Lookup) (TreePersistence, error) {
return &Tree{
lu: lu,
}, nil
}
// GetMD returns the metadata of a node in the tree
func (t *Tree) GetMD(ctx context.Context, node *Node) (os.FileInfo, error) {
md, err := os.Stat(t.lu.toInternalPath(node.ID))
if err != nil {
if os.IsNotExist(err) {
return nil, errtypes.NotFound(node.ID)
}
return nil, errors.Wrap(err, "tree: error stating "+node.ID)
}
return md, nil
}
// GetPathByID returns the fn pointed by the file id, without the internal namespace
func (t *Tree) GetPathByID(ctx context.Context, id *provider.ResourceId) (relativeExternalPath string, err error) {
var node *Node
node, err = t.lu.NodeFromID(ctx, id)
if err != nil {
return
}
relativeExternalPath, err = t.lu.Path(ctx, node)
return
}
// does not take care of linking back to parent
// TODO check if node exists?
func createNode(n *Node, owner *userpb.UserId) (err error) {
// create a directory node
nodePath := n.lu.toInternalPath(n.ID)
if err = os.MkdirAll(nodePath, 0700); err != nil {
return errors.Wrap(err, "ocisfs: error creating node")
}
return n.writeMetadata(owner)
}
// CreateDir creates a new directory entry in the tree
func (t *Tree) CreateDir(ctx context.Context, node *Node) (err error) {
if node.Exists || node.ID != "" {
return errtypes.AlreadyExists(node.ID) // path?
}
// create a directory node
node.ID = uuid.New().String()
// who will become the owner?
u, ok := user.ContextGetUser(ctx)
switch {
case ok:
// we have a user in context
err = createNode(node, u.Id)
case t.lu.Options.EnableHome:
// enable home requires a user
log := appctx.GetLogger(ctx)
log.Error().Msg("home support enabled but no user in context")
err = errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx")
case t.lu.Options.Owner != "":
// fallback to owner?
err = createNode(node, &userpb.UserId{
OpaqueId: t.lu.Options.Owner,
})
default:
// fallback to parent owner?
err = createNode(node, nil)
}
if err != nil {
return nil
}
// make child appear in listings
err = os.Symlink("../"+node.ID, filepath.Join(t.lu.toInternalPath(node.ParentID), node.Name))
if err != nil {
return
}
return t.Propagate(ctx, node)
}
// Move replaces the target with the source
func (t *Tree) Move(ctx context.Context, oldNode *Node, newNode *Node) (err error) {
// if target exists delete it without trashing it
if newNode.Exists {
// TODO make sure all children are deleted
if err := os.RemoveAll(t.lu.toInternalPath(newNode.ID)); err != nil {
return errors.Wrap(err, "ocisfs: Move: error deleting target node "+newNode.ID)
}
}
// are we just renaming (parent stays the same)?
if oldNode.ParentID == newNode.ParentID {
parentPath := t.lu.toInternalPath(oldNode.ParentID)
// rename child
err = os.Rename(
filepath.Join(parentPath, oldNode.Name),
filepath.Join(parentPath, newNode.Name),
)
if err != nil {
return errors.Wrap(err, "ocisfs: could not rename child")
}
// the new node id might be different, so we need to use the old nodes id
tgtPath := t.lu.toInternalPath(oldNode.ID)
// update name attribute
if err := xattr.Set(tgtPath, nameAttr, []byte(newNode.Name)); err != nil {
return errors.Wrap(err, "ocisfs: could not set name attribute")
}
return t.Propagate(ctx, newNode)
}
// we are moving the node to a new parent, any target has been removed
// bring old node to the new parent
// rename child
err = os.Rename(
filepath.Join(t.lu.toInternalPath(oldNode.ParentID), oldNode.Name),
filepath.Join(t.lu.toInternalPath(newNode.ParentID), newNode.Name),
)
if err != nil {
return errors.Wrap(err, "ocisfs: could not move child")
}
// update parentid and name
tgtPath := t.lu.toInternalPath(newNode.ID)
if err := xattr.Set(tgtPath, parentidAttr, []byte(newNode.ParentID)); err != nil {
return errors.Wrap(err, "ocisfs: could not set parentid attribute")
}
if err := xattr.Set(tgtPath, nameAttr, []byte(newNode.Name)); err != nil {
return errors.Wrap(err, "ocisfs: could not set name attribute")
}
// TODO inefficient because we might update several nodes twice, only propagate unchanged nodes?
// collect in a list, then only stat each node once
// also do this in a go routine ... webdav should check the etag async
err = t.Propagate(ctx, oldNode)
if err != nil {
return errors.Wrap(err, "ocisfs: Move: could not propagate old node")
}
err = t.Propagate(ctx, newNode)
if err != nil {
return errors.Wrap(err, "ocisfs: Move: could not propagate new node")
}
return nil
}
// ListFolder lists the content of a folder node
func (t *Tree) ListFolder(ctx context.Context, node *Node) ([]*Node, error) {
dir := t.lu.toInternalPath(node.ID)
f, err := os.Open(dir)
if err != nil {
if os.IsNotExist(err) {
return nil, errtypes.NotFound(dir)
}
return nil, errors.Wrap(err, "tree: error listing "+dir)
}
names, err := f.Readdirnames(0)
if err != nil {
return nil, err
}
nodes := []*Node{}
for i := range names {
link, err := os.Readlink(filepath.Join(dir, names[i]))
if err != nil {
// TODO log
continue
}
n := &Node{
lu: t.lu,
ParentID: node.ID,
ID: filepath.Base(link),
Name: names[i],
Exists: true, // TODO
}
nodes = append(nodes, n)
}
return nodes, nil
}
// Delete deletes a node in the tree
func (t *Tree) Delete(ctx context.Context, n *Node) (err error) {
// Prepare the trash
// TODO use layout?, but it requires resolving the owners user if the username is used instead of the id.
// the node knows the owner id so we use that for now
ownerid, _, err := n.Owner()
if err != nil {
return
}
if ownerid == "" {
// fall back to root trash
ownerid = "root"
}
err = os.MkdirAll(filepath.Join(t.lu.Options.Root, "trash", ownerid), 0700)
if err != nil {
return
}
// get the original path
origin, err := t.lu.Path(ctx, n)
if err != nil {
return
}
// set origin location in metadata
nodePath := t.lu.toInternalPath(n.ID)
if err := xattr.Set(nodePath, trashOriginAttr, []byte(origin)); err != nil {
return err
}
deletionTime := time.Now().UTC().Format(time.RFC3339Nano)
// first make node appear in the owners (or root) trash
// parent id and name are stored as extended attributes in the node itself
trashLink := filepath.Join(t.lu.Options.Root, "trash", ownerid, n.ID)
err = os.Symlink("../nodes/"+n.ID+".T."+deletionTime, trashLink)
if err != nil {
// To roll back changes
// TODO unset trashOriginAttr
return
}
// at this point we have a symlink pointing to a non existing destination, which is fine
// rename the trashed node so it is not picked up when traversing up the tree and matches the symlink
trashPath := nodePath + ".T." + deletionTime
err = os.Rename(nodePath, trashPath)
if err != nil {
// To roll back changes
// TODO remove symlink
// TODO unset trashOriginAttr
return
}
// finally remove the entry from the parent dir
src := filepath.Join(t.lu.toInternalPath(n.ParentID), n.Name)
err = os.Remove(src)
if err != nil {
// To roll back changes
// TODO revert the rename
// TODO remove symlink
// TODO unset trashOriginAttr
return
}
p, err := n.Parent()
if err != nil {
return errors.Wrap(err, "ocisfs: error getting parent "+n.ParentID)
}
return t.Propagate(ctx, p)
}
// Propagate propagates changes to the root of the tree
func (t *Tree) Propagate(ctx context.Context, n *Node) (err error) {
if !t.lu.Options.TreeTimeAccounting && !t.lu.Options.TreeSizeAccounting {
// no propagation enabled
log.Debug().Msg("propagation disabled")
return
}
log := appctx.GetLogger(ctx)
nodePath := t.lu.toInternalPath(n.ID)
// is propagation enabled for the parent node?
var root *Node
if root, err = t.lu.HomeOrRootNode(ctx); err != nil {
return
}
var fi os.FileInfo
if fi, err = os.Stat(nodePath); err != nil {
return err
}
var b []byte
for err == nil && n.ID != root.ID {
log.Debug().Interface("node", n).Msg("propagating")
if n, err = n.Parent(); err != nil {
break
}
// TODO none, sync and async?
if !n.HasPropagation() {
log.Debug().Interface("node", n).Str("attr", propagationAttr).Msg("propagation attribute not set or unreadable, not propagating")
// if the attribute is not set treat it as false / none / no propagation
return nil
}
if t.lu.Options.TreeTimeAccounting {
// update the parent tree time if it is older than the nodes mtime
updateSyncTime := false
var tmTime time.Time
tmTime, err = n.GetTMTime()
switch {
case err != nil:
// missing attribute, or invalid format, overwrite
log.Error().Err(err).Interface("node", n).Msg("could not read tmtime attribute, overwriting")
updateSyncTime = true
case tmTime.Before(fi.ModTime()):
log.Debug().Interface("node", n).Str("tmtime", string(b)).Str("mtime", fi.ModTime().UTC().Format(time.RFC3339Nano)).Msg("parent tmtime is older than node mtime, updating")
updateSyncTime = true
default:
log.Debug().Interface("node", n).Str("tmtime", string(b)).Str("mtime", fi.ModTime().UTC().Format(time.RFC3339Nano)).Msg("parent tmtime is younger than node mtime, not updating")
}
if updateSyncTime {
// update the tree time of the parent node
if err = n.SetTMTime(fi.ModTime()); err != nil {
log.Error().Err(err).Interface("node", n).Time("tmtime", fi.ModTime().UTC()).Msg("could not update tmtime of parent node")
return
}
log.Debug().Interface("node", n).Time("tmtime", fi.ModTime().UTC()).Msg("updated tmtime of parent node")
}
if err := n.UnsetTempEtag(); err != nil {
log.Error().Err(err).Interface("node", n).Msg("could not remove temporary etag attribute")
}
}
// TODO size accounting
}
if err != nil {
log.Error().Err(err).Interface("node", n).Msg("error propagating")
return
}
return
}