forked from zach-klippenstein/adbfs
/
main.go
182 lines (152 loc) · 4.41 KB
/
main.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
/*
Connects to the adb server to listen for new devices, and mounts devices under a certain directory
when connected.
*/
package main
import (
"fmt"
"os"
"os/exec"
"os/signal"
"strings"
"github.com/Henri-J-Norden/adbfs/internal/cli"
"github.com/zach-klippenstein/goadb"
"golang.org/x/net/context"
)
const appName = "adbfs-automount"
var (
config cli.AutomountConfig
server adb.Server
)
func init() {
cli.RegisterAutomountFlags(&config)
}
func main() {
exitCode := mainWithExitCode()
os.Exit(exitCode)
}
// Allows us to avoid calling os.Exit so we can run deferred functions as normal.
func mainWithExitCode() int {
cli.Initialize(appName, &config.BaseConfig)
eventLog := cli.NewEventLog(appName, "")
defer eventLog.Finish()
config.InitializePaths()
eventLog.Infof("using mount root %s", config.MountRoot)
if config.ReadOnly {
eventLog.Infof("mounting as read-only filesystem")
} else {
eventLog.Infof("mounting as writable filesystem")
}
var err error
server, err = adb.NewServer(config.ServerConfig())
if err != nil {
eventLog.Errorf("error initializing adb server: %s", err)
return 1
}
deviceWatcher := adb.NewDeviceWatcher(server)
defer deviceWatcher.Shutdown()
signals := make(chan os.Signal)
signal.Notify(signals, os.Kill, os.Interrupt)
processes := cli.NewProcessTracker()
defer func() {
eventLog.Infof("shutting down all mount processes…")
processes.Shutdown()
eventLog.Infof("all processes shutdown.")
}()
cli.Log.Info("automounter ready.")
defer cli.Log.Info("exiting.")
for {
select {
case event, ok := <-deviceWatcher.C():
if !ok {
// DeviceWatcher gave up.
eventLog.Errorf("device watcher quit unexpectedly: %s", deviceWatcher.Err())
return 1
}
if event.CameOnline() {
eventLog.Debugf("device connected: %s", event.Serial)
processes.Go(event.Serial, mountDevice)
} else if event.WentOffline() {
eventLog.Debugf("device disconnected: %s", event.Serial)
} else {
eventLog.Debugf("unknown device event: %+v", event)
}
case signal := <-signals:
eventLog.Debugf("got signal %v", signal)
if signal == os.Kill || signal == os.Interrupt {
return 0
}
}
}
}
func mountDevice(serial string, context context.Context) {
eventLog := cli.NewEventLog(appName, "device:"+serial)
defer func() {
eventLog.Debugf("device mount process finished: %s", serial)
eventLog.Finish()
}()
adbClient := adb.NewDeviceClient(server, adb.DeviceWithSerial(serial))
deviceInfo, err := adbClient.GetDeviceInfo()
if err != nil {
eventLog.Errorf("error getting device info for %s: %s", serial, err)
return
}
mountpoint, err := cli.NewMountpointForDevice(deviceInfo, config.MountRoot, serial)
if err != nil {
eventLog.Errorf("error creating mountpoint for %s: %s", serial, err)
return
}
defer RemoveLoggingError(mountpoint)
eventLog.Infof("mounting %s on %s", serial, mountpoint)
cmd := NewMountProcess(config.PathToAdbfs, cli.AdbfsConfig{
BaseConfig: config.BaseConfig,
DeviceSerial: serial,
Mountpoint: mountpoint,
})
eventLog.Debugf("launching adbfs: %s", CommandLine(cmd))
if err := cmd.Start(); err != nil {
eventLog.Errorf("error starting adbfs process: %s", err)
return
}
eventLog.Infof("device %s mounted with PID %d", serial, cmd.Process.Pid)
// If we're told to stop, kill the mount process.
go func() {
<-context.Done()
cmd.Process.Kill()
}()
handlerBinding := map[string]string{
cli.PathHandlerVar: mountpoint,
cli.SerialHandlerVar: serial,
cli.ModelHandlerVar: deviceInfo.Model,
}
cli.FireHandlers(config.OnMountHandlers, handlerBinding)
defer cli.FireHandlers(config.OnUnmountHandlers, handlerBinding)
if err := cmd.Wait(); err != nil {
if err, ok := err.(*exec.ExitError); ok {
eventLog.Errorf("adbfs exited with %+v", err)
} else {
eventLog.Errorf("lost connection with adbfs process: %+v", err)
}
return
}
eventLog.Infof("mount process for device %s stopped", serial)
}
func RemoveLoggingError(path string) {
cli.Log.Debugln("removing", path)
if err := os.Remove(path); err != nil {
cli.Log.Errorf("error removing %s: %s", path, err)
} else {
cli.Log.Debug("removed successfully.")
}
}
func NewMountProcess(pathToAdbfs string, config cli.AdbfsConfig) *exec.Cmd {
return &exec.Cmd{
Path: pathToAdbfs,
Args: config.AsArgs(),
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}
func CommandLine(cmd *exec.Cmd) string {
return fmt.Sprintf("%s %s", cmd.Path, strings.Join(cmd.Args, " "))
}