forked from cs3org/reva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recycle.go
261 lines (228 loc) · 8.64 KB
/
recycle.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
// 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 ocis
import (
"context"
"os"
"path/filepath"
"strings"
"time"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/user"
"github.com/pkg/errors"
"github.com/pkg/xattr"
)
// Recycle items are stored inside the node folder and start with the uuid of the deleted node.
// The `.T.` indicates it is a trash item and what follows is the timestamp of the deletion.
// The deleted file is kept in the same location/dir as the original node. This prevents deletes
// from triggering cross storage moves when the trash is accidentally stored on another partition,
// because the admin mounted a different partition there.
// TODO For an efficient listing of deleted nodes the ocis storages trash folder should have
// contain a directory with symlinks to trash files for every userid/"root"
func (fs *ocisfs) ListRecycle(ctx context.Context) (items []*provider.RecycleItem, err error) {
log := appctx.GetLogger(ctx)
trashRoot := fs.getRecycleRoot(ctx)
items = make([]*provider.RecycleItem, 0)
// TODO how do we check if the storage allows listing the recycle for the current user? check owner of the root of the storage?
// use permissions ReadUserPermissions?
if fs.o.EnableHome {
if !ownerPermissions.ListContainer {
log.Debug().Msg("owner not allowed to list trash")
return items, errtypes.PermissionDenied("owner not allowed to list trash")
}
} else {
if !noPermissions.ListContainer {
log.Debug().Msg("default permissions prevent listing trash")
return items, errtypes.PermissionDenied("default permissions prevent listing trash")
}
}
f, err := os.Open(trashRoot)
if err != nil {
if os.IsNotExist(err) {
return items, nil
}
return nil, errors.Wrap(err, "tree: error listing "+trashRoot)
}
defer f.Close()
names, err := f.Readdirnames(0)
if err != nil {
return nil, err
}
for i := range names {
var trashnode string
trashnode, err = os.Readlink(filepath.Join(trashRoot, names[i]))
if err != nil {
log.Error().Err(err).Str("trashRoot", trashRoot).Str("name", names[i]).Msg("error reading trash link, skipping")
err = nil
continue
}
parts := strings.SplitN(filepath.Base(trashnode), ".T.", 2)
if len(parts) != 2 {
log.Error().Err(err).Str("trashRoot", trashRoot).Str("name", names[i]).Str("trashnode", trashnode).Interface("parts", parts).Msg("malformed trash link, skipping")
continue
}
nodePath := fs.lu.toInternalPath(filepath.Base(trashnode))
md, err := os.Stat(nodePath)
if err != nil {
log.Error().Err(err).Str("trashRoot", trashRoot).Str("name", names[i]).Str("trashnode", trashnode).Interface("parts", parts).Msg("could not stat trash item, skipping")
continue
}
item := &provider.RecycleItem{
Type: getResourceType(md.IsDir()),
Size: uint64(md.Size()),
Key: filepath.Base(trashRoot) + ":" + parts[0], // glue using :, a / is interpreted as a path and only the node id will reach the other methods
}
if deletionTime, err := time.Parse(time.RFC3339Nano, parts[1]); err == nil {
item.DeletionTime = &types.Timestamp{
Seconds: uint64(deletionTime.Unix()),
// TODO nanos
}
} else {
log.Error().Err(err).Str("trashRoot", trashRoot).Str("name", names[i]).Str("link", trashnode).Interface("parts", parts).Msg("could parse time format, ignoring")
}
// lookup origin path in extended attributes
var attrBytes []byte
if attrBytes, err = xattr.Get(nodePath, trashOriginAttr); err == nil {
item.Path = string(attrBytes)
} else {
log.Error().Err(err).Str("trashRoot", trashRoot).Str("name", names[i]).Str("link", trashnode).Msg("could not read origin path, skipping")
continue
}
// TODO filter results by permission ... on the original parent? or the trashed node?
// if it were on the original parent it would be possible to see files that were trashed before the current user got access
// so -> check the trash node itself
// hmm listing trash currently lists the current users trash or the 'root' trash. from ocs only the home storage is queried for trash items.
// for now we can only really check if the current user is the owner
if attrBytes, err = xattr.Get(nodePath, ownerIDAttr); err == nil {
if fs.o.EnableHome {
u := user.ContextMustGetUser(ctx)
if u.Id.OpaqueId != string(attrBytes) {
log.Warn().Str("trashRoot", trashRoot).Str("name", names[i]).Str("link", trashnode).Msg("trash item not owned by current user, skipping")
continue
}
}
} else {
log.Error().Err(err).Str("trashRoot", trashRoot).Str("name", names[i]).Str("link", trashnode).Msg("could not read owner, skipping")
continue
}
items = append(items, item)
}
return
}
func (fs *ocisfs) RestoreRecycleItem(ctx context.Context, key string) (err error) {
log := appctx.GetLogger(ctx)
var rn *Node
var trashItem string
var deletedNodePath string
var origin string
if rn, trashItem, deletedNodePath, origin, err = ReadRecycleItem(ctx, fs.lu, key); err != nil {
return
}
// check permissions of deleted node
ok, err := fs.p.HasPermission(ctx, rn, func(rp *provider.ResourcePermissions) bool {
return rp.RestoreRecycleItem
})
switch {
case err != nil:
return errtypes.InternalError(err.Error())
case !ok:
return errtypes.PermissionDenied(key)
}
// link to origin
var n *Node
n, err = fs.lu.NodeFromPath(ctx, origin)
if err != nil {
return
}
if n.Exists {
return errtypes.AlreadyExists("origin already exists")
}
// add the entry for the parent dir
err = os.Symlink("../"+rn.ID, filepath.Join(fs.lu.toInternalPath(n.ParentID), n.Name))
if err != nil {
return
}
// rename to node only name, so it is picked up by id
nodePath := fs.lu.toInternalPath(rn.ID)
err = os.Rename(deletedNodePath, nodePath)
if err != nil {
return
}
n.Exists = true
// delete item link in trash
if err = os.Remove(trashItem); err != nil {
log.Error().Err(err).Str("trashItem", trashItem).Msg("error deleting trashitem")
}
return fs.tp.Propagate(ctx, n)
}
func (fs *ocisfs) PurgeRecycleItem(ctx context.Context, key string) (err error) {
log := appctx.GetLogger(ctx)
var rn *Node
var trashItem string
var deletedNodePath string
if rn, trashItem, deletedNodePath, _, err = ReadRecycleItem(ctx, fs.lu, key); err != nil {
return
}
// check permissions of deleted node
ok, err := fs.p.HasPermission(ctx, rn, func(rp *provider.ResourcePermissions) bool {
return rp.PurgeRecycle
})
switch {
case err != nil:
return errtypes.InternalError(err.Error())
case !ok:
return errtypes.PermissionDenied(key)
}
if err = os.Remove(deletedNodePath); err != nil {
log.Error().Err(err).Str("deletedNodePath", deletedNodePath).Msg("error deleting trash node")
return
}
// delete item link in trash
if err = os.Remove(trashItem); err != nil {
log.Error().Err(err).Str("trashItem", trashItem).Msg("error deleting trash item")
}
// TODO recursively delete all children
return
}
func (fs *ocisfs) EmptyRecycle(ctx context.Context) error {
u, ok := user.ContextGetUser(ctx)
// TODO what permission should we check? we could check the root node of the user? or the owner permissions on his home root node?
// The current impl will wipe your own trash. or when no user provided the trash of 'root'
if !ok {
return os.RemoveAll(fs.getRecycleRoot(ctx))
}
// TODO use layout, see Tree.Delete() for problem
return os.RemoveAll(filepath.Join(fs.o.Root, "trash", u.Id.OpaqueId))
}
func getResourceType(isDir bool) provider.ResourceType {
if isDir {
return provider.ResourceType_RESOURCE_TYPE_CONTAINER
}
return provider.ResourceType_RESOURCE_TYPE_FILE
}
func (fs *ocisfs) getRecycleRoot(ctx context.Context) string {
if fs.o.EnableHome {
u := user.ContextMustGetUser(ctx)
// TODO use layout, see Tree.Delete() for problem
return filepath.Join(fs.o.Root, "trash", u.Id.OpaqueId)
}
return filepath.Join(fs.o.Root, "trash", "root")
}