forked from keybase/client
/
tlf.go
361 lines (314 loc) · 9.61 KB
/
tlf.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
// 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.
//
// +build !windows
package libfuse
import (
"os"
"sync"
"time"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/keybase/client/go/kbfs/data"
"github.com/keybase/client/go/kbfs/libfs"
"github.com/keybase/client/go/kbfs/libkbfs"
"github.com/keybase/client/go/kbfs/tlf"
"github.com/keybase/client/go/kbfs/tlfhandle"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
"golang.org/x/net/context"
)
// TLF represents the root directory of a TLF. It wraps a lazy-loaded
// Dir.
type TLF struct {
folder *Folder
inode uint64
dirLock sync.RWMutex
dir *Dir
// We never set quarantine on TLF roots, so don't bother with getting a dir
// and calling the method on it. Instead, just use this dumb handler to
// always return fuse.ENOTSUP.
NoXattrHandler
}
func newTLF(ctx context.Context, fl *FolderList, h *tlfhandle.Handle,
name tlf.PreferredName) *TLF {
folder := newFolder(ctx, fl, h, name)
tlf := &TLF{
folder: folder,
inode: fl.fs.assignInode(),
}
return tlf
}
var _ DirInterface = (*TLF)(nil)
func (tlf *TLF) getStoredDir() *Dir {
tlf.dirLock.RLock()
defer tlf.dirLock.RUnlock()
return tlf.dir
}
func (tlf *TLF) clearStoredDir() {
tlf.dirLock.Lock()
defer tlf.dirLock.Unlock()
tlf.dir = nil
}
func (tlf *TLF) log() logger.Logger {
return tlf.folder.fs.log
}
func (tlf *TLF) vlog() *libkb.VDebugLog {
return tlf.folder.fs.vlog
}
func (tlf *TLF) loadDirHelper(
ctx context.Context, mode libkbfs.ErrorModeType, branch data.BranchName,
filterErr bool) (dir *Dir, exitEarly bool, err error) {
dir = tlf.getStoredDir()
if dir != nil {
return dir, false, nil
}
tlf.dirLock.Lock()
defer tlf.dirLock.Unlock()
// Need to check for nilness again to avoid racing with other
// calls to loadDir().
if tlf.dir != nil {
return tlf.dir, false, nil
}
tlf.log().CDebugf(ctx, "Loading root directory for folder %s "+
"(type: %s, filter error: %t)", tlf.folder.name(),
tlf.folder.list.tlfType, filterErr)
defer func() {
if filterErr {
exitEarly, err = libfs.FilterTLFEarlyExitError(ctx, err, tlf.log(), tlf.folder.name())
}
err = tlf.folder.processError(ctx, mode, err)
}()
handle, err := tlf.folder.resolve(ctx)
if err != nil {
return nil, false, err
}
if branch == data.MasterBranch {
conflictBranch, isLocalConflictBranch :=
data.MakeConflictBranchName(handle)
if isLocalConflictBranch {
branch = conflictBranch
}
}
var rootNode libkbfs.Node
if filterErr {
rootNode, _, err = tlf.folder.fs.config.KBFSOps().GetRootNode(
ctx, handle, branch)
if err != nil {
return nil, false, err
}
// If not fake an empty directory.
if rootNode == nil {
return nil, false, libfs.TlfDoesNotExist{}
}
} else {
rootNode, _, err = tlf.folder.fs.config.KBFSOps().GetOrCreateRootNode(
ctx, handle, branch)
if err != nil {
return nil, false, err
}
}
err = tlf.folder.setFolderBranch(rootNode.GetFolderBranch())
if err != nil {
return nil, false, err
}
tlf.folder.nodes[rootNode.GetID()] = tlf
tlf.dir = newDirWithInode(tlf.folder, rootNode, tlf.inode)
return tlf.dir, false, nil
}
func (tlf *TLF) loadDir(ctx context.Context) (*Dir, error) {
dir, _, err := tlf.loadDirHelper(
ctx, libkbfs.WriteMode, data.MasterBranch, false)
return dir, err
}
// loadDirAllowNonexistent loads a TLF if it's not already loaded. If
// the TLF doesn't yet exist, it still returns a nil error and
// indicates that the calling function should pretend it's an empty
// folder.
func (tlf *TLF) loadDirAllowNonexistent(ctx context.Context) (
*Dir, bool, error) {
return tlf.loadDirHelper(ctx, libkbfs.ReadMode, data.MasterBranch, true)
}
func (tlf *TLF) loadArchivedDir(
ctx context.Context, branch data.BranchName) (*Dir, bool, error) {
// Always filter errors for archive TLF directories, so that we
// don't try to initialize them.
return tlf.loadDirHelper(ctx, libkbfs.ReadMode, branch, true)
}
// Access implements the fs.NodeAccesser interface for *TLF.
func (tlf *TLF) Access(ctx context.Context, r *fuse.AccessRequest) error {
return tlf.folder.access(ctx, r)
}
// Attr implements the fs.Node interface for TLF.
func (tlf *TLF) Attr(ctx context.Context, a *fuse.Attr) error {
dir := tlf.getStoredDir()
a.Inode = tlf.inode
if dir == nil {
tlf.vlog().CLogf(
ctx, libkb.VLog1, "Faking Attr for TLF %s", tlf.folder.name())
// Have a low non-zero value for Valid to avoid being
// swamped with requests, while still not showing
// stale data for too long if we end up loading the
// dir.
a.Valid = 1 * time.Second
a.Mode = os.ModeDir | 0500
a.Uid = uint32(os.Getuid())
return nil
}
return dir.Attr(ctx, a)
}
// tlfLoadAvoidingLookupNames specifies a set of directory entry names
// that should NOT cause a TLF to be fully loaded and identified on a
// lookup. If the directory is not yet loaded and one of these names
// are looked-up, then ENOENT will be returned automatically. This is
// to avoid unnecessary loading and tracker popups when listing the
// folder list directories. For example, when macOS finder opens
// `/keybase/private`, it looks up `.localized` in every TLF
// subdirectory to see if it should translate the TLF folder name or
// not, which can cause a tracker popup storm (see KBFS-2649).
var tlfLoadAvoidingLookupNames = map[string]bool{
".localized": true,
}
// Lookup implements the fs.NodeRequestLookuper interface for TLF.
func (tlf *TLF) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fs.Node, error) {
if tlfLoadAvoidingLookupNames[req.Name] {
dir := tlf.getStoredDir()
if dir == nil {
tlf.vlog().CLogf(
ctx, libkb.VLog1, "Avoiding TLF loading for name %s", req.Name)
return nil, fuse.ENOENT
}
return dir.Lookup(ctx, req, resp)
}
dir, exitEarly, err := tlf.loadDirAllowNonexistent(ctx)
if err != nil {
return nil, err
}
if exitEarly {
if node := handleTLFSpecialFile(
req.Name, tlf.folder, &resp.EntryValid); node != nil {
return node, nil
}
return nil, fuse.ENOENT
}
branch, isArchivedBranch := libfs.BranchNameFromArchiveRefDir(req.Name)
if isArchivedBranch {
archivedTLF := newTLF(ctx,
tlf.folder.list, tlf.folder.h, tlf.folder.hPreferredName)
_, _, err := archivedTLF.loadArchivedDir(ctx, branch)
if err != nil {
return nil, err
}
return archivedTLF, nil
}
linkTarget, isArchivedTimeLink, err := libfs.LinkTargetFromTimeString(
ctx, tlf.folder.fs.config, tlf.folder.h, req.Name)
if err != nil {
return nil, err
}
if isArchivedTimeLink {
return &Alias{
realPath: linkTarget,
inode: 0,
}, nil
}
_, isRelTimeLink, err := libfs.FileDataFromRelativeTimeString(
ctx, tlf.folder.fs.config, tlf.folder.h, req.Name)
if err != nil {
return nil, err
}
if isRelTimeLink {
return NewArchiveRelTimeFile(
tlf.folder.fs, tlf.folder.h, req.Name, &resp.EntryValid), nil
}
return dir.Lookup(ctx, req, resp)
}
// Create implements the fs.NodeCreater interface for TLF.
func (tlf *TLF) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fs.Node, fs.Handle, error) {
dir, err := tlf.loadDir(ctx)
if err != nil {
return nil, nil, err
}
return dir.Create(ctx, req, resp)
}
// Mkdir implements the fs.NodeMkdirer interface for TLF.
func (tlf *TLF) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (_ fs.Node, err error) {
dir, err := tlf.loadDir(ctx)
if err != nil {
return nil, err
}
return dir.Mkdir(ctx, req)
}
// Symlink implements the fs.NodeSymlinker interface for TLF.
func (tlf *TLF) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (
fs.Node, error) {
dir, err := tlf.loadDir(ctx)
if err != nil {
return nil, err
}
return dir.Symlink(ctx, req)
}
// Rename implements the fs.NodeRenamer interface for TLF.
func (tlf *TLF) Rename(ctx context.Context, req *fuse.RenameRequest,
newDir fs.Node) error {
dir, err := tlf.loadDir(ctx)
if err != nil {
return err
}
return dir.Rename(ctx, req, newDir)
}
// Remove implements the fs.NodeRemover interface for TLF.
func (tlf *TLF) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
dir, err := tlf.loadDir(ctx)
if err != nil {
return err
}
return dir.Remove(ctx, req)
}
// ReadDirAll implements the fs.NodeReadDirAller interface for TLF.
func (tlf *TLF) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
dir, exitEarly, err := tlf.loadDirAllowNonexistent(ctx)
if err != nil || exitEarly {
return nil, err
}
return dir.ReadDirAll(ctx)
}
// Forget kernel reference to this node.
func (tlf *TLF) Forget() {
dir := tlf.getStoredDir()
if dir != nil {
dir.Forget()
}
}
// Setattr implements the fs.NodeSetattrer interface for TLF.
func (tlf *TLF) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
dir, err := tlf.loadDir(ctx)
if err != nil {
return err
}
return dir.Setattr(ctx, req, resp)
}
// Fsync implements the fs.NodeFsyncer interface for TLF.
func (tlf *TLF) Fsync(ctx context.Context, req *fuse.FsyncRequest) (err error) {
dir := tlf.getStoredDir()
if dir == nil {
// The directory hasn't been loaded yet, so there's nothing to do.
return nil
}
return dir.Fsync(ctx, req)
}
var _ fs.Handle = (*TLF)(nil)
var _ fs.NodeOpener = (*TLF)(nil)
// Open implements the fs.NodeOpener interface for TLF.
func (tlf *TLF) Open(ctx context.Context, req *fuse.OpenRequest,
resp *fuse.OpenResponse) (fs.Handle, error) {
// Explicitly load the directory when a TLF is opened, because
// some OSX programs like ls have a bug that doesn't report errors
// on a ReadDirAll.
_, _, err := tlf.loadDirAllowNonexistent(ctx)
if err != nil {
return nil, err
}
return tlf, nil
}