This repository has been archived by the owner on Feb 12, 2019. It is now read-only.
/
start.go
149 lines (129 loc) · 4.2 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 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/go-framed-msgpack-rpc/rpc"
"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.
createSimpleFS := func(
libkbfsCtx libkbfs.Context, config libkbfs.Config) (rpc.Protocol, error) {
return keybase1.SimpleFSProtocol(
simplefs.NewSimpleFS(libkbfsCtx, config)), nil
}
// Hook git implementation in.
shutdownGit := func() {}
createGitHandler := func(
libkbfsCtx libkbfs.Context, config libkbfs.Config) (rpc.Protocol, error) {
var handler keybase1.KBFSGitInterface
handler, shutdownGit = libgit.NewRPCHandlerWithCtx(
libkbfsCtx, config, &options.KbfsParams)
return keybase1.KBFSGitProtocol(handler), nil
}
defer func() {
shutdownGit()
}()
// Patch the kbfsParams to inject two additional protocols.
options.KbfsParams.AdditionalProtocolCreators = []libkbfs.AdditionalProtocolCreator{
createSimpleFS, createGitHandler,
}
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 != "" {
err := os.MkdirAll(options.RuntimeDir, libkb.PermDir)
if err != nil {
return libfs.InitError(err.Error())
}
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)
st, err := os.Lstat(options.MountPoint)
log.CDebugf(ctx, "Before mount check (should fail) Lstat(%q): %v,%v", options.MountPoint, st, err)
err = startMounting(options, log, mi)
if err != nil {
logDokanInfo(ctx, log)
// 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
}