github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/cmd/swarm/fs.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of go-dubxcoin.
     3  //
     4  // go-dubxcoin is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"path/filepath"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/alexdevranger/node-1.8.27/cmd/utils"
    27  	"github.com/alexdevranger/node-1.8.27/log"
    28  	"github.com/alexdevranger/node-1.8.27/rpc"
    29  	"github.com/alexdevranger/node-1.8.27/swarm/fuse"
    30  	"gopkg.in/urfave/cli.v1"
    31  )
    32  
    33  var fsCommand = cli.Command{
    34  	Name:               "fs",
    35  	CustomHelpTemplate: helpTemplate,
    36  	Usage:              "perform FUSE operations",
    37  	ArgsUsage:          "fs COMMAND",
    38  	Description:        "Performs FUSE operations by mounting/unmounting/listing mount points. This assumes you already have a Swarm node running locally. For all operation you must reference the correct path to bzzd.ipc in order to communicate with the node",
    39  	Subcommands: []cli.Command{
    40  		{
    41  			Action:             mount,
    42  			CustomHelpTemplate: helpTemplate,
    43  			Name:               "mount",
    44  			Usage:              "mount a swarm hash to a mount point",
    45  			ArgsUsage:          "swarm fs mount <manifest hash> <mount point>",
    46  			Description:        "Mounts a Swarm manifest hash to a given mount point. This assumes you already have a Swarm node running locally. You must reference the correct path to your bzzd.ipc file",
    47  		},
    48  		{
    49  			Action:             unmount,
    50  			CustomHelpTemplate: helpTemplate,
    51  			Name:               "unmount",
    52  			Usage:              "unmount a swarmfs mount",
    53  			ArgsUsage:          "swarm fs unmount <mount point>",
    54  			Description:        "Unmounts a swarmfs mount residing at <mount point>. This assumes you already have a Swarm node running locally. You must reference the correct path to your bzzd.ipc file",
    55  		},
    56  		{
    57  			Action:             listMounts,
    58  			CustomHelpTemplate: helpTemplate,
    59  			Name:               "list",
    60  			Usage:              "list swarmfs mounts",
    61  			ArgsUsage:          "swarm fs list",
    62  			Description:        "Lists all mounted swarmfs volumes. This assumes you already have a Swarm node running locally. You must reference the correct path to your bzzd.ipc file",
    63  		},
    64  	},
    65  }
    66  
    67  func mount(cliContext *cli.Context) {
    68  	args := cliContext.Args()
    69  	if len(args) < 2 {
    70  		utils.Fatalf("Usage: swarm fs mount <manifestHash> <file name>")
    71  	}
    72  
    73  	client, err := dialRPC(cliContext)
    74  	if err != nil {
    75  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
    76  	}
    77  	defer client.Close()
    78  
    79  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    80  	defer cancel()
    81  
    82  	mf := &fuse.MountInfo{}
    83  	mountPoint, err := filepath.Abs(filepath.Clean(args[1]))
    84  	if err != nil {
    85  		utils.Fatalf("error expanding path for mount point: %v", err)
    86  	}
    87  	err = client.CallContext(ctx, mf, "swarmfs_mount", args[0], mountPoint)
    88  	if err != nil {
    89  		utils.Fatalf("had an error calling the RPC endpoint while mounting: %v", err)
    90  	}
    91  }
    92  
    93  func unmount(cliContext *cli.Context) {
    94  	args := cliContext.Args()
    95  
    96  	if len(args) < 1 {
    97  		utils.Fatalf("Usage: swarm fs unmount <mount path>")
    98  	}
    99  	client, err := dialRPC(cliContext)
   100  	if err != nil {
   101  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
   102  	}
   103  	defer client.Close()
   104  
   105  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
   106  	defer cancel()
   107  
   108  	mf := fuse.MountInfo{}
   109  	err = client.CallContext(ctx, &mf, "swarmfs_unmount", args[0])
   110  	if err != nil {
   111  		utils.Fatalf("encountered an error calling the RPC endpoint while unmounting: %v", err)
   112  	}
   113  	fmt.Printf("%s\n", mf.LatestManifest) //print the latest manifest hash for user reference
   114  }
   115  
   116  func listMounts(cliContext *cli.Context) {
   117  	client, err := dialRPC(cliContext)
   118  	if err != nil {
   119  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
   120  	}
   121  	defer client.Close()
   122  
   123  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
   124  	defer cancel()
   125  
   126  	mf := []fuse.MountInfo{}
   127  	err = client.CallContext(ctx, &mf, "swarmfs_listmounts")
   128  	if err != nil {
   129  		utils.Fatalf("encountered an error calling the RPC endpoint while listing mounts: %v", err)
   130  	}
   131  	if len(mf) == 0 {
   132  		fmt.Print("Could not found any swarmfs mounts. Please make sure you've specified the correct RPC endpoint\n")
   133  	} else {
   134  		fmt.Printf("Found %d swarmfs mount(s):\n", len(mf))
   135  		for i, mountInfo := range mf {
   136  			fmt.Printf("%d:\n", i)
   137  			fmt.Printf("\tMount point: %s\n", mountInfo.MountPoint)
   138  			fmt.Printf("\tLatest Manifest: %s\n", mountInfo.LatestManifest)
   139  			fmt.Printf("\tStart Manifest: %s\n", mountInfo.StartManifest)
   140  		}
   141  	}
   142  }
   143  
   144  func dialRPC(ctx *cli.Context) (*rpc.Client, error) {
   145  	endpoint := getIPCEndpoint(ctx)
   146  	log.Info("IPC endpoint", "path", endpoint)
   147  	return rpc.Dial(endpoint)
   148  }
   149  
   150  func getIPCEndpoint(ctx *cli.Context) string {
   151  	cfg := defaultNodeConfig
   152  	utils.SetNodeConfig(ctx, &cfg)
   153  
   154  	endpoint := cfg.IPCEndpoint()
   155  
   156  	if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
   157  		// Backwards compatibility with gdubx < 1.5 which required
   158  		// these prefixes.
   159  		endpoint = endpoint[4:]
   160  	}
   161  	return endpoint
   162  }