forked from cs3org/reva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.go
222 lines (198 loc) · 6.52 KB
/
lookup.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
// Copyright 2018-2021 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 decomposedfs
import (
"context"
"fmt"
"path/filepath"
"strings"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/storage/utils/decomposedfs/node"
"github.com/cs3org/reva/pkg/storage/utils/decomposedfs/options"
"github.com/cs3org/reva/pkg/storage/utils/decomposedfs/xattrs"
"github.com/cs3org/reva/pkg/storage/utils/templates"
"github.com/pkg/xattr"
)
// Lookup implements transformations from filepath to node and back
type Lookup struct {
Options *options.Options
}
// NodeFromResource takes in a request path or request id and converts it to a Node
func (lu *Lookup) NodeFromResource(ctx context.Context, ref *provider.Reference) (*node.Node, error) {
if ref.ResourceId != nil {
// check if a storage space reference is used
// currently, the decomposed fs uses the root node id as the space id
spaceRoot, err := lu.NodeFromID(ctx, ref.ResourceId)
if err != nil {
return nil, err
}
n := spaceRoot
// is this a relative reference?
if ref.Path != "" {
p := filepath.Clean(ref.Path)
if p != "." {
// walk the relative path
n, err = lu.WalkPath(ctx, n, p, false, func(ctx context.Context, n *node.Node) error {
return nil
})
if err != nil {
return nil, err
}
}
// use reference id as space root for relative references
n.SpaceRoot = spaceRoot
}
return n, nil
}
if ref.Path != "" {
return lu.NodeFromPath(ctx, ref.GetPath(), false)
}
// reference is invalid
return nil, fmt.Errorf("invalid reference %+v. at least resource_id or path must be set", ref)
}
// NodeFromPath converts a filename into a Node
func (lu *Lookup) NodeFromPath(ctx context.Context, fn string, followReferences bool) (*node.Node, error) {
log := appctx.GetLogger(ctx)
log.Debug().Interface("fn", fn).Msg("NodeFromPath()")
root, err := lu.HomeOrRootNode(ctx)
if err != nil {
return nil, err
}
n := root
// TODO collect permissions of the current user on every segment
fn = filepath.Clean(fn)
if fn != "/" && fn != "." {
n, err = lu.WalkPath(ctx, n, fn, followReferences, func(ctx context.Context, n *node.Node) error {
log.Debug().Interface("node", n).Msg("NodeFromPath() walk")
if n.SpaceRoot != nil && n.SpaceRoot != root {
root = n.SpaceRoot
}
return nil
})
if err != nil {
return nil, err
}
}
n.SpaceRoot = root
return n, nil
}
// NodeFromID returns the internal path for the id
func (lu *Lookup) NodeFromID(ctx context.Context, id *provider.ResourceId) (n *node.Node, err error) {
if id == nil || id.OpaqueId == "" {
return nil, fmt.Errorf("invalid resource id %+v", id)
}
return node.ReadNode(ctx, lu, id.OpaqueId)
}
// Path returns the path for node
func (lu *Lookup) Path(ctx context.Context, n *node.Node) (p string, err error) {
var root *node.Node
if root, err = lu.HomeOrRootNode(ctx); err != nil {
return
}
for n.ID != root.ID {
p = filepath.Join(n.Name, p)
if n, err = n.Parent(); err != nil {
appctx.GetLogger(ctx).
Error().Err(err).
Str("path", p).
Interface("node", n).
Msg("Path()")
return
}
}
p = filepath.Join("/", p)
return
}
// RootNode returns the root node of the storage
func (lu *Lookup) RootNode(ctx context.Context) (*node.Node, error) {
n := node.New("root", "", "", 0, "", nil, lu)
n.Exists = true
return n, nil
}
// HomeNode returns the home node of a user
func (lu *Lookup) HomeNode(ctx context.Context) (node *node.Node, err error) {
if !lu.Options.EnableHome {
return nil, errtypes.NotSupported("Decomposedfs: home supported disabled")
}
if node, err = lu.RootNode(ctx); err != nil {
return
}
node, err = lu.WalkPath(ctx, node, lu.mustGetUserLayout(ctx), false, nil)
return
}
// WalkPath calls n.Child(segment) on every path segment in p starting at the node r.
// If a function f is given it will be executed for every segment node, but not the root node r.
// If followReferences is given the current visited reference node is replaced by the referenced node.
func (lu *Lookup) WalkPath(ctx context.Context, r *node.Node, p string, followReferences bool, f func(ctx context.Context, n *node.Node) error) (*node.Node, error) {
segments := strings.Split(strings.Trim(p, "/"), "/")
var err error
for i := range segments {
if r, err = r.Child(ctx, segments[i]); err != nil {
return r, err
}
if followReferences {
if attrBytes, err := xattr.Get(r.InternalPath(), xattrs.ReferenceAttr); err == nil {
realNodeID := attrBytes
ref, err := xattrs.ReferenceFromAttr(realNodeID)
if err != nil {
return nil, err
}
r, err = lu.NodeFromID(ctx, ref.ResourceId)
if err != nil {
return nil, err
}
}
}
if !r.Exists && i < len(segments)-1 {
return r, errtypes.NotFound(segments[i])
}
if f != nil {
if err = f(ctx, r); err != nil {
return r, err
}
}
}
return r, nil
}
// HomeOrRootNode returns the users home node when home support is enabled.
// it returns the storages root node otherwise
func (lu *Lookup) HomeOrRootNode(ctx context.Context) (node *node.Node, err error) {
if lu.Options.EnableHome {
return lu.HomeNode(ctx)
}
return lu.RootNode(ctx)
}
// InternalRoot returns the internal storage root directory
func (lu *Lookup) InternalRoot() string {
return lu.Options.Root
}
// InternalPath returns the internal path for a given ID
func (lu *Lookup) InternalPath(id string) string {
return filepath.Join(lu.Options.Root, "nodes", id)
}
func (lu *Lookup) mustGetUserLayout(ctx context.Context) string {
u := ctxpkg.ContextMustGetUser(ctx)
return templates.WithUser(u, lu.Options.UserLayout)
}
// ShareFolder returns the internal storage root directory
func (lu *Lookup) ShareFolder() string {
return lu.Options.ShareFolder
}