// Start the filesystem func Start(mounter Mounter, options StartOptions) *Error { c, err := mounter.Mount() if err != nil { return MountError(err.Error()) } defer c.Close() onInterruptFn := func() { select { case <-c.Ready: // Was mounted, so try to unmount if it was successful. if c.MountError == nil { err = mounter.Unmount() if err != nil { return } } default: // Was not mounted successfully yet, so do nothing. Note that the mount // could still happen, but that's a rare enough edge case. } } log := logger.NewWithCallDepth("", 1, os.Stderr) log.Info("KBFS version %s", libkbfs.VersionString()) config, err := libkbfs.Init(options.KbfsParams, onInterruptFn, log) if err != nil { return InitError(err.Error()) } defer libkbfs.Shutdown(options.KbfsParams.MemProfile) if options.RuntimeDir != "" { info := libkb.NewServiceInfo(libkbfs.Version, libkbfs.Build(), options.Label, os.Getpid()) err = info.WriteFile(path.Join(options.RuntimeDir, "kbfs.info")) if err != nil { return InitError(err.Error()) } } fs := NewFS(config, c, options.KbfsParams.Debug) ctx := context.WithValue(context.Background(), CtxAppIDKey, fs) logTags := make(logger.CtxLogTags) logTags[CtxIDKey] = CtxOpID ctx = logger.NewContextWithLogTags(ctx, logTags) fs.Serve(ctx) <-c.Ready err = c.MountError if err != nil { return MountError(err.Error()) } return nil }
// Define this so deferred functions get executed before exit. func realMain() (exitStatus int) { kbfsParams := libkbfs.AddFlags(flag.CommandLine) flag.Parse() if *version { fmt.Printf("%s\n", libkbfs.VersionString()) return 0 } if len(flag.Args()) < 1 { fmt.Print(getUsageStr()) return 1 } log := logger.NewWithCallDepth("", 1) config, err := libkbfs.Init(*kbfsParams, nil, log) if err != nil { printError("kbfs", err) return 1 } defer libkbfs.Shutdown() // TODO: Make the logging level WARNING instead of INFO, or // figure out some other way to log the full folder-branch // name for kbfsfuse but not for kbfs. cmd := flag.Arg(0) args := flag.Args()[1:] ctx := context.Background() switch cmd { case "stat": return stat(ctx, config, args) case "ls": return ls(ctx, config, args) case "mkdir": return mkdir(ctx, config, args) case "read": return read(ctx, config, args) case "write": return write(ctx, config, args) default: printError("kbfs", fmt.Errorf("unknown command '%s'", cmd)) return 1 } }
// Start the filesystem func Start(mounter Mounter, options StartOptions) *libfs.Error { // InitLog errors are non-fatal and are ignored. log, _ := libkbfs.InitLog(options.KbfsParams) if options.RuntimeDir != "" { info := libkb.NewServiceInfo(libkbfs.Version, libkbfs.PrereleaseBuild, options.Label, os.Getpid()) err := info.WriteFile(path.Join(options.RuntimeDir, "kbfs.info")) if err != nil { return libfs.InitError(err.Error()) } } log.Debug("Mounting: %s", mounter.Dir()) c, err := mounter.Mount() if err != nil { return libfs.MountError(err.Error()) } defer c.Close() onInterruptFn := func() { select { case <-c.Ready: // Was mounted, so try to unmount if it was successful. if c.MountError == nil { err = mounter.Unmount() if err != nil { return } } default: // Was not mounted successfully yet, so do nothing. Note that the mount // could still happen, but that's a rare enough edge case. } } log.Debug("Initializing") config, err := libkbfs.Init(options.KbfsParams, onInterruptFn, log) if err != nil { return libfs.InitError(err.Error()) } defer libkbfs.Shutdown() log.Debug("Creating filesystem") fs := NewFS(config, c, options.KbfsParams.Debug) ctx, cancel := context.WithCancel(context.Background()) defer cancel() ctx = context.WithValue(ctx, CtxAppIDKey, fs) log.Debug("Serving filesystem") fs.Serve(ctx) <-c.Ready err = c.MountError if err != nil { return libfs.MountError(err.Error()) } log.Debug("Ending") return nil }