github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/kbfsdokan/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  // Keybase file system
     6  
     7  package main
     8  
     9  import (
    10  	"flag"
    11  	"fmt"
    12  	"github.com/keybase/client/go/kbfs/dokan"
    13  	"github.com/keybase/client/go/kbfs/env"
    14  	"github.com/keybase/client/go/kbfs/libdokan"
    15  	"github.com/keybase/client/go/kbfs/libfs"
    16  	"github.com/keybase/client/go/kbfs/libkbfs"
    17  	"github.com/keybase/client/go/libkb"
    18  	"github.com/keybase/client/go/logger"
    19  	"os"
    20  	"strconv"
    21  )
    22  
    23  func getDefaultMountFlags() int64 {
    24  	mf := os.Getenv("KEYBASE_MOUNT_FLAGS")
    25  	if mf == "" {
    26  		return int64(libdokan.DefaultMountFlags)
    27  	}
    28  	iv, _ := strconv.Atoi(mf)
    29  	return int64(iv)
    30  }
    31  
    32  var runtimeDir = flag.String("runtime-dir", os.Getenv("KEYBASE_RUNTIME_DIR"), "runtime directory")
    33  var label = flag.String("label", os.Getenv("KEYBASE_LABEL"), "label to help identify if running as a service")
    34  var mountType = flag.String("mount-type", defaultMountType, "mount type: default, force, none")
    35  var version = flag.Bool("version", false, "Print version")
    36  var mountFlags = flag.Int64("mount-flags", getDefaultMountFlags(), "Dokan mount flags")
    37  var dokandll = flag.String("dokan-dll", "", "Absolute path of dokan dll to load")
    38  var servicemount = flag.Bool("mount-from-service", false, "get mount path from service")
    39  
    40  const usageFormatStr = `Usage:
    41    kbfsdokan -version
    42  
    43  To run against remote KBFS servers:
    44    kbfsdokan
    45      [-runtime-dir=path/to/dir] [-label=label] [-mount-type=force]
    46      [-mount-flags=n] [-dokan-dll=path/to/dokan.dll]
    47  %s
    48      -mount-from-service | /path/to/mountpoint
    49  
    50  To run in a local testing environment:
    51    kbfsdokan
    52      [-runtime-dir=path/to/dir] [-label=label] [-mount-type=force]
    53      [-mount-flags=n] [-dokan-dll=path/to/dokan.dll]
    54  %s
    55      -mount-from-service | /path/to/mountpoint
    56  
    57  Defaults:
    58  %s
    59  `
    60  
    61  func getUsageString(ctx libkbfs.Context) string {
    62  	remoteUsageStr := libkbfs.GetRemoteUsageString()
    63  	localUsageStr := libkbfs.GetLocalUsageString()
    64  	defaultUsageStr := libkbfs.GetDefaultsUsageString(ctx)
    65  	return fmt.Sprintf(usageFormatStr, remoteUsageStr,
    66  		localUsageStr, defaultUsageStr)
    67  }
    68  
    69  func start() *libfs.Error {
    70  	err := libkb.SaferDLLLoading()
    71  	if err != nil {
    72  		fmt.Printf("SaferDLLLoading failed: %v\n", err)
    73  	}
    74  
    75  	ctx := env.NewContextWithPerfLog(libkb.KBFSPerfLogFileName)
    76  	kbfsParams := libkbfs.AddFlags(flag.CommandLine, ctx)
    77  
    78  	flag.Parse()
    79  
    80  	if *version {
    81  		fmt.Printf("%s\n", libkbfs.VersionString())
    82  		return nil
    83  	}
    84  
    85  	var mountpoint string
    86  	if len(flag.Args()) < 1 {
    87  		if !*servicemount {
    88  			fmt.Print(getUsageString(ctx))
    89  			return libfs.InitError("no mount specified")
    90  		}
    91  	} else {
    92  		mountpoint = flag.Arg(0)
    93  	}
    94  
    95  	if len(flag.Args()) > 1 {
    96  		fmt.Print(getUsageString(ctx))
    97  		return libfs.InitError("extra arguments specified (flags go before the first argument)")
    98  	}
    99  
   100  	logger.EnableBufferedLogging()
   101  	defer logger.Shutdown()
   102  
   103  	options := libdokan.StartOptions{
   104  		KbfsParams: *kbfsParams,
   105  		RuntimeDir: *runtimeDir,
   106  		Label:      *label,
   107  		DokanConfig: dokan.Config{
   108  			MountFlags: dokan.MountFlag(*mountFlags),
   109  			DllPath:    *dokandll,
   110  		},
   111  		ForceMount: *mountType == "force",
   112  		SkipMount:  *mountType == "none",
   113  		MountPoint: mountpoint,
   114  	}
   115  
   116  	return libdokan.Start(options, ctx)
   117  }
   118  
   119  func main() {
   120  	err := start()
   121  	if err != nil {
   122  		fmt.Fprintf(os.Stderr, "kbfsdokan error: (%d) %s\n", err.Code, err.Message)
   123  
   124  		os.Exit(err.Code)
   125  	}
   126  	fmt.Fprintf(os.Stderr, "kbfsdokan normal shutdown")
   127  	os.Exit(0)
   128  }