コード例 #1
0
ファイル: main.go プロジェクト: freakmac/adbfs
func main() {
	cli.Initialize("adbfs", &config.BaseConfig)

	if config.DeviceSerial == "" {
		cli.Log.Fatalln("Device serial must be specified. Run with -h.")
	}

	if config.Mountpoint == "" {
		cli.Log.Fatalln("Mountpoint must be specified. Run with -h.")
	}
	absoluteMountpoint, err := filepath.Abs(config.Mountpoint)
	if err != nil {
		cli.Log.Fatal(err)
	}
	if err = checkValidMountpoint(absoluteMountpoint); err != nil {
		cli.Log.Fatal(err)
	}

	cache := initializeCache(config.CacheTtl)
	clientConfig := config.ClientConfig()

	fs := initializeFileSystem(clientConfig, absoluteMountpoint, cache)
	go watchForDeviceDisconnected(clientConfig, config.DeviceSerial)

	server, _, err = nodefs.MountRoot(absoluteMountpoint, fs.Root(), nil)
	if err != nil {
		cli.Log.Fatal(err)
	}

	serverDone, err := startServer(StartTimeout)
	if err != nil {
		cli.Log.Fatal(err)
	}
	cli.Log.Printf("mounted %s on %s", config.DeviceSerial, absoluteMountpoint)
	mounted.CompareAndSwap(false, true)
	defer unmountServer()

	signals := make(chan os.Signal, 1)
	signal.Notify(signals, os.Interrupt, os.Kill)

	for {
		select {
		case signal := <-signals:
			cli.Log.Println("got signal", signal)
			switch signal {
			case os.Kill, os.Interrupt:
				cli.Log.Println("exiting...")
				return
			}

		case <-serverDone:
			cli.Log.Debugln("server done channel closed.")
			return
		}
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: freakmac/adbfs
func main() {
	cli.Initialize(appName, &config.BaseConfig)
	eventLog := cli.NewEventLog(appName, "")
	defer eventLog.Finish()

	config.InitializePaths()
	eventLog.Infof("using mount root %s", config.MountRoot)

	deviceWatcher := goadb.NewDeviceWatcher(config.ClientConfig())
	defer deviceWatcher.Shutdown()

	signals := make(chan os.Signal)
	signal.Notify(signals, os.Kill, os.Interrupt)

	processes := cli.NewProcessTracker()
	defer processes.Shutdown()

	cli.Log.Info("automounter ready.")
	defer cli.Log.Info("exiting.")

	for {
		select {
		case event := <-deviceWatcher.C():
			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 {
				eventLog.Infof("shutting down all mount processes…")
				processes.Shutdown()
				eventLog.Infof("all processes shutdown.")
				return
			}
		}
	}
}