forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
folderlist.go
288 lines (251 loc) · 7.71 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
// 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 libdokan
import (
"sync"
"time"
"github.com/keybase/client/go/libkb"
"github.com/keybase/kbfs/dokan"
"github.com/keybase/kbfs/libfs"
"github.com/keybase/kbfs/libkbfs"
"github.com/keybase/kbfs/tlf"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
type fileOpener interface {
open(ctx context.Context, oc *openContext, path []string) (f dokan.File, isDir bool, err error)
dokan.File
}
// 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 {
emptyFile
fs *FS
tlfType tlf.Type
mu sync.Mutex
folders map[string]fileOpener
aliasCache map[string]string
}
// GetFileInformation for dokan.
func (*FolderList) GetFileInformation(context.Context, *dokan.FileInfo) (*dokan.Stat, error) {
return defaultDirectoryInformation()
}
func (fl *FolderList) reportErr(ctx context.Context,
mode libkbfs.ErrorModeType, tlfName tlf.CanonicalName, err error, cancelFn func()) {
if cancelFn != nil {
defer cancelFn()
}
if err == nil {
fl.fs.log.CDebugf(ctx, "Request complete")
return
}
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.log.CDebugf(ctx, err.Error())
}
func (fl *FolderList) addToFavorite(ctx context.Context, h *libkbfs.TlfHandle) (err error) {
cName := h.GetCanonicalName()
fl.fs.log.CDebugf(ctx, "adding %s to favorites", cName)
fl.fs.config.KBFSOps().AddFavorite(ctx, h.ToFavorite())
return nil
}
// open tries to open the correct thing. Following aliases and deferring to
// Dir.open as necessary.
func (fl *FolderList) open(ctx context.Context, oc *openContext, path []string) (f dokan.File, isDir bool, err error) {
fl.fs.log.CDebugf(ctx, "FL Lookup %#v type=%s upper=%v",
path, fl.tlfType, oc.isUppercasePath)
if len(path) == 0 {
return oc.returnDirNoCleanup(fl)
}
defer func() {
fl.reportErr(ctx, libkbfs.ReadMode, tlf.CanonicalName(path[0]), err, nil)
}()
// TODO: A simple lower-casing is not good enough - see CORE-2967
// However libkbfs does this too in tlf_handle.go...
// The case of possible redirections will be ok, so we only need
// to do this initially.
if c := lowerTranslateCandidate(oc, path[0]); c != "" {
path[0] = c
}
for oc.reduceRedirectionsLeft() {
name := path[0]
if name == "desktop.ini" || name == "DESKTOP.INI" {
fl.fs.log.CDebugf(ctx, "FL Lookup ignoring desktop.ini")
return nil, false, dokan.ErrObjectNameNotFound
}
var aliasTarget string
fl.mu.Lock()
child, ok := fl.folders[name]
if !ok {
aliasTarget = fl.aliasCache[name]
}
fl.mu.Unlock()
if ok {
fl.fs.log.CDebugf(ctx, "FL Lookup recursing to child %q", name)
return child.open(ctx, oc, path[1:])
}
if len(path) == 1 && isNewFolderName(name) {
if !oc.isCreateDirectory() {
return nil, false, dokan.ErrObjectNameNotFound
}
fl.fs.log.CDebugf(ctx, "FL Lookup creating EmptyFolder for Explorer")
e := &EmptyFolder{}
fl.lockedAddChild(name, e)
return e, true, nil
}
if aliasTarget != "" {
fl.fs.log.CDebugf(ctx, "FL Lookup aliasCache hit: %q -> %q", name, aliasTarget)
if len(path) == 1 && oc.isOpenReparsePoint() {
// TODO handle dir/non-dir here, semantics?
return &Alias{canon: aliasTarget}, true, nil
}
path[0] = aliasTarget
continue
}
h, err := libfs.ParseTlfHandlePreferredQuick(
ctx, fl.fs.config.KBPKI(), name, fl.tlfType)
fl.fs.log.CDebugf(ctx, "FL Lookup continuing -> %v,%v", h, err)
switch e := errors.Cause(err).(type) {
case nil:
// no error
case libkbfs.TlfNameNotCanonical:
// Only permit Aliases to targets that contain no errors.
aliasTarget = e.NameToTry
fl.fs.log.CDebugf(ctx, "FL Lookup set alias: %q -> %q", name, aliasTarget)
if !fl.isValidAliasTarget(ctx, aliasTarget) {
fl.fs.log.CDebugf(ctx, "FL Refusing alias to non-valid target %q", aliasTarget)
return nil, false, dokan.ErrObjectNameNotFound
}
fl.mu.Lock()
fl.aliasCache[name] = aliasTarget
fl.mu.Unlock()
if len(path) == 1 && oc.isOpenReparsePoint() {
// TODO handle dir/non-dir here, semantics?
fl.fs.log.CDebugf(ctx, "FL Lookup ret alias, oc: %#v", oc.CreateData)
return &Alias{canon: aliasTarget}, true, nil
}
path[0] = aliasTarget
continue
case libkbfs.NoSuchNameError, libkbfs.BadTLFNameError:
return nil, false, dokan.ErrObjectNameNotFound
default:
// Some other error.
return nil, false, err
}
fl.fs.log.CDebugf(ctx, "FL Lookup adding new child")
session, err := libkbfs.GetCurrentSessionIfPossible(ctx, fl.fs.config.KBPKI(), h.Type() == tlf.Public)
if err != nil {
return nil, false, err
}
child = newTLF(fl, h, h.GetPreferredFormat(session.Name))
fl.lockedAddChild(name, child)
return child.open(ctx, oc, path[1:])
}
return nil, false, dokan.ErrObjectNameNotFound
}
func (fl *FolderList) forgetFolder(folderName string) {
fl.mu.Lock()
defer fl.mu.Unlock()
delete(fl.folders, folderName)
}
// FindFiles for dokan.
func (fl *FolderList) FindFiles(ctx context.Context, fi *dokan.FileInfo, ignored string, callback func(*dokan.NamedStat) error) (err error) {
fl.fs.logEnter(ctx, "FL FindFiles")
defer func() { fl.fs.reportErr(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 err
}
}
var ns dokan.NamedStat
ns.FileAttributes = dokan.FileAttributeDirectory
empty := true
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.CErrorf(ctx, "CanonicalToPreferredName: %q %v", fav.Name, err)
continue
}
empty = false
ns.Name = string(pname)
err = callback(&ns)
if err != nil {
return err
}
}
if empty {
return dokan.ErrObjectNameNotFound
}
return nil
}
func (fl *FolderList) isValidAliasTarget(ctx context.Context, nameToTry string) bool {
return libkbfs.CheckTlfHandleOffline(ctx, nameToTry, fl.tlfType) == nil
}
func (fl *FolderList) lockedAddChild(name string, val fileOpener) {
fl.mu.Lock()
fl.folders[name] = val
fl.mu.Unlock()
}
func (fl *FolderList) updateTlfName(ctx context.Context, oldName string,
newName string) {
fl.mu.Lock()
defer fl.mu.Unlock()
tlf, ok := fl.folders[oldName]
if !ok {
return
}
fl.fs.log.CDebugf(ctx, "Folder name updated: %s -> %s", oldName, newName)
delete(fl.folders, oldName)
fl.folders[newName] = tlf
// TODO: invalidate kernel cache for this name? (Make sure to
// do so outside of the lock!)
}
func (fl *FolderList) clearAliasCache() {
fl.mu.Lock()
fl.aliasCache = map[string]string{}
fl.mu.Unlock()
}
func clearFolderListCacheLoop(ctx context.Context, r *Root) {
t := time.NewTicker(time.Hour)
for {
select {
case <-ctx.Done():
return
case <-t.C:
}
r.private.clearAliasCache()
r.public.clearAliasCache()
}
}
// 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 {
if tlf, ok := tlf.(*TLF); ok {
fs = append(fs, tlf.folder)
}
}
}()
for _, f := range fs {
f.TlfHandleChange(ctx, nil)
}
if newUser != libkb.NormalizedUsername("") {
fl.fs.config.KBFSOps().ForceFastForward(ctx)
}
}