forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolderlist.go
367 lines (317 loc) · 10.3 KB
/
folderlist.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
// Copyright 2016 Keybase Inc. All rights reserved.
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file.
package libfuse
import (
"fmt"
"os"
"strings"
"sync"
"syscall"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/keybase/client/go/libkb"
"github.com/keybase/kbfs/libfs"
"github.com/keybase/kbfs/libkbfs"
"github.com/keybase/kbfs/tlf"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
// FolderList is a node that can list all of the logged-in user's
// favorite top-level folders, on either a public or private basis.
type FolderList struct {
fs *FS
// only accept public folders
tlfType tlf.Type
inode uint64
mu sync.Mutex
folders map[string]*TLF
muRecentlyRemoved sync.RWMutex
recentlyRemoved map[tlf.CanonicalName]bool
}
var _ fs.NodeAccesser = (*FolderList)(nil)
// Access implements fs.NodeAccesser interface for *FolderList.
func (*FolderList) Access(ctx context.Context, r *fuse.AccessRequest) error {
if int(r.Uid) != os.Getuid() &&
// Finder likes to use UID 0 for some operations. osxfuse already allows
// ACCESS and GETXATTR requests from root to go through. This allows root
// in ACCESS handler. See KBFS-1733 for more details.
int(r.Uid) != 0 {
// short path: not accessible by anybody other than root or the user who
// executed the kbfsfuse process.
return fuse.EPERM
}
if r.Mask&02 != 0 {
return fuse.EPERM
}
return nil
}
var _ fs.Node = (*FolderList)(nil)
// Attr implements the fs.Node interface.
func (fl *FolderList) Attr(ctx context.Context, a *fuse.Attr) error {
a.Mode = os.ModeDir | 0500
a.Uid = uint32(os.Getuid())
a.Inode = fl.inode
return nil
}
var _ fs.NodeRequestLookuper = (*FolderList)(nil)
func (fl *FolderList) processError(ctx context.Context,
mode libkbfs.ErrorModeType, tlfName tlf.CanonicalName, err error) error {
if err == nil {
fl.fs.errLog.CDebugf(ctx, "Request complete")
return nil
}
fl.fs.config.Reporter().ReportErr(ctx, tlfName, fl.tlfType, mode, err)
// We just log the error as debug, rather than error, because it
// might just indicate an expected error such as an ENOENT.
//
// TODO: Classify errors and escalate the logging level of the
// important ones.
fl.fs.errLog.CDebugf(ctx, err.Error())
return filterError(err)
}
func (fl *FolderList) addToRecentlyRemoved(name tlf.CanonicalName) {
func() {
fl.muRecentlyRemoved.Lock()
defer fl.muRecentlyRemoved.Unlock()
if fl.recentlyRemoved == nil {
fl.recentlyRemoved = make(map[tlf.CanonicalName]bool)
}
fl.recentlyRemoved[name] = true
}()
fl.fs.execAfterDelay(time.Second, func() {
fl.muRecentlyRemoved.Lock()
defer fl.muRecentlyRemoved.Unlock()
delete(fl.recentlyRemoved, name)
})
}
func (fl *FolderList) isRecentlyRemoved(name tlf.CanonicalName) bool {
fl.muRecentlyRemoved.RLock()
defer fl.muRecentlyRemoved.RUnlock()
return fl.recentlyRemoved != nil && fl.recentlyRemoved[name]
}
func (fl *FolderList) addToFavorite(ctx context.Context, h *libkbfs.TlfHandle) (err error) {
cName := h.GetCanonicalName()
// `rmdir` command on macOS does a lookup after removing the dir. if the
// TLF is recently removed, it's likely that this lookup is issued by the
// `rmdir` command, and the lookup should not result in adding the dir to
// favorites.
if !fl.isRecentlyRemoved(cName) {
fl.fs.log.CDebugf(ctx, "adding %s to favorites", cName)
fl.fs.config.KBFSOps().AddFavorite(ctx, h.ToFavorite())
} else {
fl.fs.log.CDebugf(ctx, "recently removed; will skip adding %s to favorites and return ENOENT", cName)
return fuse.ENOENT
}
return nil
}
// PathType returns PathType for this folder
func (fl *FolderList) PathType() libkbfs.PathType {
switch fl.tlfType {
case tlf.Private:
return libkbfs.PrivatePathType
case tlf.Public:
return libkbfs.PublicPathType
case tlf.SingleTeam:
return libkbfs.SingleTeamPathType
default:
panic(fmt.Sprintf("Unsupported tlf type: %s", fl.tlfType))
}
}
// Create implements the fs.NodeCreater interface for FolderList.
func (fl *FolderList) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (_ fs.Node, _ fs.Handle, err error) {
fl.fs.log.CDebugf(ctx, "FL Create")
tlfName := tlf.CanonicalName(req.Name)
defer func() { err = fl.processError(ctx, libkbfs.WriteMode, tlfName, err) }()
if strings.HasPrefix(req.Name, "._") {
// Quietly ignore writes to special macOS files, without
// triggering a notification.
return nil, nil, syscall.ENOENT
}
return nil, nil, libkbfs.NewWriteUnsupportedError(libkbfs.BuildCanonicalPath(fl.PathType(), string(tlfName)))
}
// Mkdir implements the fs.NodeMkdirer interface for FolderList.
func (fl *FolderList) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (_ fs.Node, err error) {
fl.fs.log.CDebugf(ctx, "FL Mkdir")
tlfName := tlf.CanonicalName(req.Name)
defer func() { err = fl.processError(ctx, libkbfs.WriteMode, tlfName, err) }()
return nil, libkbfs.NewWriteUnsupportedError(libkbfs.BuildCanonicalPath(fl.PathType(), string(tlfName)))
}
// Lookup implements the fs.NodeRequestLookuper interface.
func (fl *FolderList) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (node fs.Node, err error) {
fl.fs.log.CDebugf(ctx, "FL Lookup %s", req.Name)
defer func() {
err = fl.processError(ctx, libkbfs.ReadMode,
tlf.CanonicalName(req.Name), err)
}()
fl.mu.Lock()
defer fl.mu.Unlock()
specialNode := handleNonTLFSpecialFile(
req.Name, fl.fs, &resp.EntryValid)
if specialNode != nil {
return specialNode, nil
}
if child, ok := fl.folders[req.Name]; ok {
return child, nil
}
// Shortcut for dreaded extraneous OSX finder lookups
if strings.HasPrefix(req.Name, "._") {
return nil, fuse.ENOENT
}
h, err := libfs.ParseTlfHandlePreferredQuick(
ctx, fl.fs.config.KBPKI(), req.Name, fl.tlfType)
switch e := errors.Cause(err).(type) {
case nil:
// no error
case libkbfs.TlfNameNotCanonical:
// Only permit Aliases to targets that contain no errors.
if !fl.isValidAliasTarget(ctx, e.NameToTry) {
fl.fs.log.CDebugf(ctx, "FL Refusing alias to non-valid target %q", e.NameToTry)
return nil, fuse.ENOENT
}
// Non-canonical name.
n := &Alias{
realPath: e.NameToTry,
inode: 0,
}
return n, nil
case libkbfs.NoSuchNameError, libkbfs.BadTLFNameError:
// Invalid public TLF.
return nil, fuse.ENOENT
default:
// Some other error.
return nil, err
}
session, err := libkbfs.GetCurrentSessionIfPossible(
ctx, fl.fs.config.KBPKI(), h.Type() == tlf.Public)
if err != nil {
return nil, err
}
child := newTLF(fl, h, h.GetPreferredFormat(session.Name))
fl.folders[req.Name] = child
return child, nil
}
func (fl *FolderList) isValidAliasTarget(ctx context.Context, nameToTry string) bool {
return libkbfs.CheckTlfHandleOffline(ctx, nameToTry, fl.tlfType) == nil
}
func (fl *FolderList) forgetFolder(folderName string) {
fl.mu.Lock()
defer fl.mu.Unlock()
delete(fl.folders, folderName)
}
var _ fs.Handle = (*FolderList)(nil)
var _ fs.HandleReadDirAller = (*FolderList)(nil)
// ReadDirAll implements the ReadDirAll interface.
func (fl *FolderList) ReadDirAll(ctx context.Context) (res []fuse.Dirent, err error) {
fl.fs.log.CDebugf(ctx, "FL ReadDirAll")
defer func() {
err = fl.fs.processError(ctx, libkbfs.ReadMode, err)
}()
session, err := fl.fs.config.KBPKI().GetCurrentSession(ctx)
isLoggedIn := err == nil
var favs []libkbfs.Favorite
if isLoggedIn {
favs, err = fl.fs.config.KBFSOps().GetFavorites(ctx)
if err != nil {
return nil, err
}
}
res = make([]fuse.Dirent, 0, len(favs))
for _, fav := range favs {
if fav.Type != fl.tlfType {
continue
}
pname, err := tlf.CanonicalToPreferredName(
session.Name, tlf.CanonicalName(fav.Name))
if err != nil {
fl.fs.log.Errorf("CanonicalToPreferredName: %q %v", fav.Name, err)
continue
}
res = append(res, fuse.Dirent{
Type: fuse.DT_Dir,
Name: string(pname),
})
}
return res, nil
}
var _ fs.NodeRemover = (*FolderList)(nil)
// Remove implements the fs.NodeRemover interface for FolderList.
func (fl *FolderList) Remove(ctx context.Context, req *fuse.RemoveRequest) (err error) {
fl.fs.log.CDebugf(ctx, "FolderList Remove %s", req.Name)
defer func() { err = fl.fs.processError(ctx, libkbfs.WriteMode, err) }()
h, err := libkbfs.ParseTlfHandlePreferred(
ctx, fl.fs.config.KBPKI(), fl.fs.config.MDOps(), req.Name, fl.tlfType)
switch err := errors.Cause(err).(type) {
case nil:
func() {
fl.mu.Lock()
defer fl.mu.Unlock()
if tlf, ok := fl.folders[req.Name]; ok {
// Fake future attr calls for this TLF until the user
// actually opens the TLF again, because some OSes (*cough
// OS X cough*) like to do immediate lookup/attr calls
// right after doing a remove, which would otherwise end
// up re-adding the favorite.
tlf.clearStoredDir()
}
}()
// TODO how to handle closing down the folderbranchops
// object? Open files may still exist long after removing
// the favorite.
return fl.fs.config.KBFSOps().DeleteFavorite(ctx, h.ToFavorite())
case libkbfs.TlfNameNotCanonical:
return nil
default:
return err
}
}
func isTlfNameNotCanonical(err error) bool {
_, ok := errors.Cause(err).(libkbfs.TlfNameNotCanonical)
return ok
}
func (fl *FolderList) updateTlfName(ctx context.Context, oldName string,
newName string) {
ok := func() bool {
fl.mu.Lock()
defer fl.mu.Unlock()
tlf, ok := fl.folders[oldName]
if !ok {
return false
}
fl.fs.log.CDebugf(ctx, "Folder name updated: %s -> %s", oldName, newName)
delete(fl.folders, oldName)
fl.folders[newName] = tlf
return true
}()
if !ok {
return
}
if err := fl.fs.fuse.InvalidateEntry(fl, oldName); err != nil {
// TODO we have no mechanism to do anything about this
fl.fs.log.CErrorf(ctx, "FUSE invalidate error for oldName=%s: %v",
oldName, err)
}
if err := fl.fs.fuse.InvalidateEntry(fl, newName); err != nil {
// TODO we have no mechanism to do anything about this
fl.fs.log.CErrorf(ctx, "FUSE invalidate error for newName=%s: %v",
newName, err)
}
}
// update things after user changed.
func (fl *FolderList) userChanged(ctx context.Context, _, newUser libkb.NormalizedUsername) {
var fs []*Folder
func() {
fl.mu.Lock()
defer fl.mu.Unlock()
for _, tlf := range fl.folders {
fs = append(fs, tlf.folder)
}
}()
for _, f := range fs {
f.TlfHandleChange(ctx, nil)
}
if newUser != libkb.NormalizedUsername("") {
fl.fs.config.KBFSOps().ForceFastForward(ctx)
}
}