forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdates_file.go
81 lines (70 loc) · 2.04 KB
/
updates_file.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
// 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 (
"errors"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/keybase/kbfs/libkbfs"
"golang.org/x/net/context"
)
// UpdatesFile represents a write-only file where any write of at
// least one byte triggers either disabling remote folder updates and
// conflict resolution, or re-enabling both. It is mainly useful for
// testing.
type UpdatesFile struct {
folder *Folder
enable bool
}
var _ fs.Node = (*UpdatesFile)(nil)
// Attr implements the fs.Node interface for UpdatesFile.
func (f *UpdatesFile) Attr(ctx context.Context, a *fuse.Attr) error {
a.Size = 0
a.Mode = 0222
return nil
}
var _ fs.Handle = (*UpdatesFile)(nil)
var _ fs.HandleWriter = (*UpdatesFile)(nil)
// Write implements the fs.HandleWriter interface for UpdatesFile.
func (f *UpdatesFile) Write(ctx context.Context, req *fuse.WriteRequest,
resp *fuse.WriteResponse) (err error) {
f.folder.fs.log.CDebugf(ctx, "UpdatesFile (enable: %t) Write", f.enable)
defer func() { f.folder.reportErr(ctx, libkbfs.WriteMode, err) }()
if len(req.Data) == 0 {
return nil
}
f.folder.updateMu.Lock()
defer f.folder.updateMu.Unlock()
if f.enable {
if f.folder.updateChan == nil {
return errors.New("Updates are already enabled")
}
err = libkbfs.RestartCRForTesting(
libkbfs.BackgroundContextWithCancellationDelayer(),
f.folder.fs.config, f.folder.getFolderBranch())
if err != nil {
return err
}
f.folder.updateChan <- struct{}{}
close(f.folder.updateChan)
f.folder.updateChan = nil
} else {
if f.folder.updateChan != nil {
return errors.New("Updates are already disabled")
}
f.folder.updateChan, err =
libkbfs.DisableUpdatesForTesting(f.folder.fs.config,
f.folder.getFolderBranch())
if err != nil {
return err
}
err = libkbfs.DisableCRForTesting(f.folder.fs.config,
f.folder.getFolderBranch())
if err != nil {
return err
}
}
resp.Size = len(req.Data)
return nil
}