Example #1
0
// 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
}
Example #2
0
// 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
	}
}
Example #3
0
func start() *libfs.Error {
	kbfsParams := libkbfs.AddFlags(flag.CommandLine)
	platformParams := libfuse.AddPlatformFlags(flag.CommandLine)

	flag.Parse()

	if *version {
		fmt.Printf("%s\n", libkbfs.VersionString())
		return nil
	}

	if len(flag.Args()) < 1 {
		fmt.Print(getUsageStr())
		return libfs.InitError("no mount specified")
	}

	if len(flag.Args()) > 1 {
		fmt.Print(getUsageStr())
		return libfs.InitError("extra arguments specified (flags go before the first argument)")
	}

	if kbfsParams.Debug {
		fuseLog := logger.NewWithCallDepth("FUSE", 1)
		fuseLog.Configure("", true, "")
		fuse.Debug = func(msg interface{}) {
			fuseLog.Debug("%s", msg)
		}
	}

	mountpoint := flag.Arg(0)
	var mounter libfuse.Mounter
	if *mountType == "force" {
		mounter = libfuse.NewForceMounter(mountpoint, *platformParams)
	} else {
		mounter = libfuse.NewDefaultMounter(mountpoint, *platformParams)
	}

	options := libfuse.StartOptions{
		KbfsParams: *kbfsParams,
		RuntimeDir: *runtimeDir,
		Label:      *label,
	}

	return libfuse.Start(mounter, options)
}