forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
130 lines (112 loc) · 3.49 KB
/
start.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
// 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 (
"os"
"path"
"strings"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/logger"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/kbfs/dokan"
"github.com/keybase/kbfs/libfs"
"github.com/keybase/kbfs/libgit"
"github.com/keybase/kbfs/libkbfs"
"github.com/keybase/kbfs/simplefs"
"golang.org/x/net/context"
)
// StartOptions are options for starting up
type StartOptions struct {
KbfsParams libkbfs.InitParams
RuntimeDir string
Label string
DokanConfig dokan.Config
ForceMount bool
SkipMount bool
MountPoint string
}
func startMounting(options StartOptions,
log logger.Logger, mi *libfs.MountInterrupter) error {
var mounter = &mounter{options: options, log: log}
err := mi.MountAndSetUnmount(mounter)
if err != nil {
return err
}
log.Info("Mounting the filesystem was a success!")
return mounter.c.BlockTillDone()
}
// Start the filesystem
func Start(options StartOptions, kbCtx libkbfs.Context) *libfs.Error {
// Hook simplefs implementation in.
options.KbfsParams.CreateSimpleFSInstance = simplefs.NewSimpleFS
// Hook git implementation in.
shutdownGit := func() {}
options.KbfsParams.CreateGitHandlerInstance =
func(config libkbfs.Config) (i keybase1.KBFSGitInterface) {
i, shutdownGit = libgit.NewRPCHandlerWithCtx(
kbCtx, config, &options.KbfsParams)
return i
}
defer func() {
shutdownGit()
}()
log, err := libkbfs.InitLog(options.KbfsParams, kbCtx)
if err != nil {
return libfs.InitError(err.Error())
}
mi := libfs.NewMountInterrupter(log)
ctx := context.Background()
config, err := libkbfs.Init(
ctx, kbCtx, options.KbfsParams, nil, mi.Done, log)
if err != nil {
return libfs.InitError(err.Error())
}
defer libkbfs.Shutdown()
if options.RuntimeDir != "" {
info := libkb.NewServiceInfo(libkbfs.Version, libkbfs.PrereleaseBuild, options.Label, os.Getpid())
err = info.WriteFile(path.Join(options.RuntimeDir, "kbfs.info"), log)
if err != nil {
return libfs.InitError(err.Error())
}
}
if options.KbfsParams.Debug {
// Turn on debugging. TODO: allow a proper log file and
// style to be specified.
log.Configure("", true, "")
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if options.MountPoint == "" {
// The mounter will detect this case and pick up the path from DokanConfig
options.MountPoint, err = config.KeybaseService().EstablishMountDir(ctx)
if err != nil {
return libfs.InitError(err.Error())
}
log.CInfof(ctx, "Got mount dir from service: -%s-", options.MountPoint)
}
options.DokanConfig.Path = options.MountPoint
if !options.SkipMount && !strings.EqualFold(options.MountPoint, "none") {
fs, err := NewFS(ctx, config, log)
if err != nil {
return libfs.InitError(err.Error())
}
options.DokanConfig.FileSystem = fs
if newFolderNameErr != nil {
log.CWarningf(ctx, "Error guessing new folder name: %v", newFolderNameErr)
}
log.CDebugf(ctx, "New folder name guess: %q %q", newFolderName, newFolderAltName)
err = startMounting(options, log, mi)
if err != nil {
// Abort on error if we were force mounting, otherwise continue.
if options.ForceMount {
// Cleanup when exiting in case the mount got dirty.
mi.Done()
return libfs.MountError(err.Error())
}
log.CErrorf(ctx, "Running KBFS without a filesystem mount due to: %v", err)
}
}
mi.Wait()
return nil
}