github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/kbfsfuse/main.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build !windows
     6  // +build !windows
     7  
     8  // Keybase file system
     9  
    10  package main
    11  
    12  import (
    13  	"flag"
    14  	"fmt"
    15  	"os"
    16  
    17  	"bazil.org/fuse"
    18  
    19  	"github.com/keybase/client/go/kbfs/env"
    20  	"github.com/keybase/client/go/kbfs/libfs"
    21  	"github.com/keybase/client/go/kbfs/libfuse"
    22  	"github.com/keybase/client/go/kbfs/libkbfs"
    23  	"github.com/keybase/client/go/libkb"
    24  	"github.com/keybase/client/go/logger"
    25  )
    26  
    27  var runtimeDir = flag.String("runtime-dir", os.Getenv("KEYBASE_RUNTIME_DIR"), "runtime directory")
    28  var label = flag.String("label", os.Getenv("KEYBASE_LABEL"), "label to help identify if running as a service")
    29  var mountType = flag.String("mount-type", defaultMountType, "mount type: default, force, none")
    30  var version = flag.Bool("version", false, "Print version")
    31  
    32  const usageFormatStr = `Usage:
    33    kbfsfuse -version
    34  
    35  To run against remote KBFS servers:
    36    kbfsfuse
    37      [-runtime-dir=path/to/dir] [-label=label] [-mount-type=default|force|required|none]
    38  %s
    39      %s[/path/to/mountpoint]
    40  
    41  To run in a local testing environment:
    42    kbfsfuse
    43      [-runtime-dir=path/to/dir] [-label=label] [-mount-type=default|force|required|none]
    44  %s
    45      %s[/path/to/mountpoint]
    46  
    47  Defaults:
    48  %s `
    49  
    50  func getUsageString(ctx libkbfs.Context) string {
    51  	remoteUsageStr := libkbfs.GetRemoteUsageString()
    52  	localUsageStr := libkbfs.GetLocalUsageString()
    53  	platformUsageStr := libfuse.GetPlatformUsageString()
    54  	defaultUsageStr := libkbfs.GetDefaultsUsageString(ctx)
    55  	return fmt.Sprintf(usageFormatStr,
    56  		remoteUsageStr, platformUsageStr,
    57  		localUsageStr, platformUsageStr, defaultUsageStr)
    58  }
    59  
    60  func start() *libfs.Error {
    61  	ctx := env.NewContextWithPerfLog(libkb.KBFSPerfLogFileName)
    62  
    63  	kbfsParams := libkbfs.AddFlags(flag.CommandLine, ctx)
    64  	platformParams := libfuse.AddPlatformFlags(flag.CommandLine)
    65  
    66  	flag.Parse()
    67  
    68  	if *version {
    69  		fmt.Printf("%s\n", libkbfs.VersionString())
    70  		return nil
    71  	}
    72  
    73  	mountDir := ""
    74  	if len(flag.Args()) < 1 {
    75  		var err error
    76  		mountDir, err = ctx.GetMountDir()
    77  		if err != nil {
    78  			return libfs.InitError(err.Error())
    79  		}
    80  		// If a mountdir was not set by `keybase config set mountdir`, the
    81  		// service returns a default value which may or may not exist yet.
    82  		if len(mountDir) == 0 {
    83  			fmt.Print(getUsageString(ctx))
    84  			return libfs.InitError("no mount specified")
    85  		}
    86  	} else {
    87  		mountDir = flag.Arg(0)
    88  	}
    89  
    90  	if len(flag.Args()) > 1 {
    91  		fmt.Print(getUsageString(ctx))
    92  		return libfs.InitError("extra arguments specified (flags go before the first argument)")
    93  	}
    94  
    95  	if kbfsParams.Debug {
    96  		// Temporary, until we make a config and can make a vlogger.
    97  		fuseLog := logger.NewWithCallDepth("FUSE", 1)
    98  		fuseLog.Configure("", true, "")
    99  		fuse.Debug = libfuse.MakeFuseDebugFn(
   100  			fuseLog, false /* superVerbose */)
   101  	}
   102  
   103  	logger.EnableBufferedLogging()
   104  	defer logger.Shutdown()
   105  
   106  	options := libfuse.StartOptions{
   107  		KbfsParams:        *kbfsParams,
   108  		PlatformParams:    *platformParams,
   109  		RuntimeDir:        *runtimeDir,
   110  		Label:             *label,
   111  		ForceMount:        *mountType == "force" || *mountType == "required",
   112  		MountErrorIsFatal: *mountType == "required",
   113  		SkipMount:         *mountType == "none",
   114  		MountPoint:        mountDir,
   115  	}
   116  
   117  	return libfuse.Start(options, ctx)
   118  }
   119  
   120  func main() {
   121  	err := start()
   122  	if err != nil {
   123  		fmt.Fprintf(os.Stderr, "kbfsfuse error: (%d) %s\n", err.Code, err.Message)
   124  
   125  		os.Exit(err.Code)
   126  	}
   127  	os.Exit(0)
   128  }