forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
216 lines (187 loc) · 5.7 KB
/
context.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
// 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 env
import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"sync"
"github.com/keybase/client/go/libkb"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
)
const (
kbfsSocketFile = "kbfsd.sock"
)
// Context defines the environment for this package
type Context interface {
GetRunMode() libkb.RunMode
GetLogDir() string
GetDataDir() string
GetMountDir() (string, error)
ConfigureSocketInfo() (err error)
GetGlobalContext() *libkb.GlobalContext
GetSocket(clearError bool) (net.Conn, rpc.Transporter, bool, error)
NewRPCLogFactory() rpc.LogFactory
GetKBFSSocket(clearError bool) (net.Conn, rpc.Transporter, bool, error)
BindToKBFSSocket() (net.Listener, error)
}
// KBFSContext is an implementation for libkbfs.Context
type KBFSContext struct {
g *libkb.GlobalContext
// protects the socket primitives below
kbfsSocketMtx sync.RWMutex
kbfsSocket libkb.Socket
kbfsSocketWrapper *libkb.SocketWrapper
}
var _ Context = (*KBFSContext)(nil)
func (c *KBFSContext) initKBFSSocket() {
c.kbfsSocketMtx.Lock()
defer c.kbfsSocketMtx.Unlock()
log := c.g.Log
bindFile := c.getKBFSSocketFile()
dialFiles := []string{bindFile}
c.kbfsSocket = libkb.NewSocketWithFiles(log, bindFile, dialFiles)
}
// NewContextFromGlobalContext constructs a context
func NewContextFromGlobalContext(g *libkb.GlobalContext) *KBFSContext {
c := &KBFSContext{g: g}
c.initKBFSSocket()
return c
}
// NewContext constructs a context. This should only be called once in
// main functions.
func NewContext() *KBFSContext {
g := libkb.NewGlobalContextInit()
g.ConfigureConfig()
g.ConfigureLogging()
g.ConfigureCaches()
g.ConfigureMerkleClient()
return NewContextFromGlobalContext(g)
}
// GetLogDir returns log dir
func (c *KBFSContext) GetLogDir() string {
return c.g.Env.GetLogDir()
}
// GetDataDir returns log dir
func (c *KBFSContext) GetDataDir() string {
return c.g.Env.GetDataDir()
}
// GetMountDir returns mount dir
func (c *KBFSContext) GetMountDir() (string, error) {
return c.g.Env.GetMountDir()
}
// GetRunMode returns run mode
func (c *KBFSContext) GetRunMode() libkb.RunMode {
return c.g.GetRunMode()
}
// GetGlobalContext returns the libkb global context.
func (c *KBFSContext) GetGlobalContext() *libkb.GlobalContext {
return c.g
}
// GetSocket returns a socket
func (c *KBFSContext) GetSocket(clearError bool) (
net.Conn, rpc.Transporter, bool, error) {
return c.g.GetSocket(clearError)
}
// ConfigureSocketInfo configures a socket
func (c *KBFSContext) ConfigureSocketInfo() error {
return c.g.ConfigureSocketInfo()
}
// NewRPCLogFactory constructs an RPC logger
func (c *KBFSContext) NewRPCLogFactory() rpc.LogFactory {
return &libkb.RPCLogFactory{Contextified: libkb.NewContextified(c.g)}
}
func (c *KBFSContext) getSandboxSocketFile() string {
sandboxDir := c.g.Env.HomeFinder.SandboxCacheDir()
if sandboxDir == "" {
return ""
}
return filepath.Join(sandboxDir, kbfsSocketFile)
}
func (c *KBFSContext) getKBFSSocketFile() string {
e := c.g.Env
return e.GetString(
func() string { return c.getSandboxSocketFile() },
// TODO: maybe add command-line option here
func() string { return os.Getenv("KBFS_SOCKET_FILE") },
func() string { return filepath.Join(e.GetRuntimeDir(), kbfsSocketFile) },
)
}
func (c *KBFSContext) newTransportFromSocket(s net.Conn) rpc.Transporter {
return rpc.NewTransport(s, c.NewRPCLogFactory(), libkb.WrapError)
}
// GetKBFSSocket dials the socket configured in `c.kbfsSocket`.
// Adapted from github.com/keybase/client/go/libkb.GlobalContext.GetSocket.
func (c *KBFSContext) GetKBFSSocket(clearError bool) (
net.Conn, rpc.Transporter, bool, error) {
var err error
c.g.Trace("GetSocket", func() error { return err })()
// Protect all global socket wrapper manipulation with a
// lock to prevent race conditions.
c.kbfsSocketMtx.Lock()
defer c.kbfsSocketMtx.Unlock()
needWrapper := false
if c.kbfsSocketWrapper == nil {
needWrapper = true
c.g.Log.Debug("empty socket wrapper; need a new one")
} else if c.kbfsSocketWrapper.Transporter != nil && !c.kbfsSocketWrapper.Transporter.IsConnected() {
// need reconnect
c.g.Log.Debug("rpc transport isn't connected, reconnecting...")
needWrapper = true
}
isNew := false
if needWrapper {
sw := libkb.SocketWrapper{}
if c.kbfsSocket == nil {
sw.Err = fmt.Errorf("Cannot get socket")
} else {
sw.Conn, sw.Err = c.kbfsSocket.DialSocket()
c.g.Log.Debug("DialSocket -> %s", libkb.ErrToOk(sw.Err))
isNew = true
}
if sw.Err == nil {
sw.Transporter = c.newTransportFromSocket(sw.Conn)
}
c.kbfsSocketWrapper = &sw
}
// Return the current error no matter what
sw := c.kbfsSocketWrapper
if sw.Err != nil && clearError {
c.kbfsSocketWrapper = nil
}
return sw.Conn, sw.Transporter, isNew, sw.Err
}
// cleanupSocketFile cleans up the socket file for binding.
func (c *KBFSContext) cleanupSocketFile() error {
switch sock := c.kbfsSocket.(type) {
case libkb.SocketInfo:
sf := sock.GetBindFile()
if exists, err := libkb.FileExists(sf); err != nil {
return err
} else if exists {
c.g.Log.Debug("removing stale socket file: %s", sf)
if err = os.Remove(sf); err != nil {
c.g.Log.Warning("error removing stale socket file: %s", err)
return err
}
}
case nil:
return errors.New("socket not initialized")
default:
return errors.New("invalid socket type")
}
return nil
}
// BindToKBFSSocket binds to the socket configured in `c.kbfsSocket`.
func (c *KBFSContext) BindToKBFSSocket() (net.Listener, error) {
c.kbfsSocketMtx.Lock()
defer c.kbfsSocketMtx.Unlock()
err := c.cleanupSocketFile()
if err != nil {
return nil, err
}
return c.kbfsSocket.BindToSocket()
}