github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/cmd/swarm/fs.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum 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/ethereum/go-ethereum/cmd/utils"
    27  	"github.com/ethereum/go-ethereum/node"
    28  	"github.com/ethereum/go-ethereum/rpc"
    29  	"github.com/ethereum/go-ethereum/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  			Flags:              []cli.Flag{utils.IPCPathFlag},
    45  			Usage:              "mount a swarm hash to a mount point",
    46  			ArgsUsage:          "swarm fs mount --ipcpath <path to bzzd.ipc> <manifest hash> <mount point>",
    47  			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",
    48  		},
    49  		{
    50  			Action:             unmount,
    51  			CustomHelpTemplate: helpTemplate,
    52  			Name:               "unmount",
    53  			Flags:              []cli.Flag{utils.IPCPathFlag},
    54  			Usage:              "unmount a swarmfs mount",
    55  			ArgsUsage:          "swarm fs unmount --ipcpath <path to bzzd.ipc> <mount point>",
    56  			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",
    57  		},
    58  		{
    59  			Action:             listMounts,
    60  			CustomHelpTemplate: helpTemplate,
    61  			Name:               "list",
    62  			Flags:              []cli.Flag{utils.IPCPathFlag},
    63  			Usage:              "list swarmfs mounts",
    64  			ArgsUsage:          "swarm fs list --ipcpath <path to bzzd.ipc>",
    65  			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",
    66  		},
    67  	},
    68  }
    69  
    70  func mount(cliContext *cli.Context) {
    71  	args := cliContext.Args()
    72  	if len(args) < 2 {
    73  		utils.Fatalf("Usage: swarm fs mount --ipcpath <path to bzzd.ipc> <manifestHash> <file name>")
    74  	}
    75  
    76  	client, err := dialRPC(cliContext)
    77  	if err != nil {
    78  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
    79  	}
    80  	defer client.Close()
    81  
    82  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    83  	defer cancel()
    84  
    85  	mf := &fuse.MountInfo{}
    86  	mountPoint, err := filepath.Abs(filepath.Clean(args[1]))
    87  	if err != nil {
    88  		utils.Fatalf("error expanding path for mount point: %v", err)
    89  	}
    90  	err = client.CallContext(ctx, mf, "swarmfs_mount", args[0], mountPoint)
    91  	if err != nil {
    92  		utils.Fatalf("had an error calling the RPC endpoint while mounting: %v", err)
    93  	}
    94  }
    95  
    96  func unmount(cliContext *cli.Context) {
    97  	args := cliContext.Args()
    98  
    99  	if len(args) < 1 {
   100  		utils.Fatalf("Usage: swarm fs unmount --ipcpath <path to bzzd.ipc> <mount path>")
   101  	}
   102  	client, err := dialRPC(cliContext)
   103  	if err != nil {
   104  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
   105  	}
   106  	defer client.Close()
   107  
   108  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
   109  	defer cancel()
   110  
   111  	mf := fuse.MountInfo{}
   112  	err = client.CallContext(ctx, &mf, "swarmfs_unmount", args[0])
   113  	if err != nil {
   114  		utils.Fatalf("encountered an error calling the RPC endpoint while unmounting: %v", err)
   115  	}
   116  	fmt.Printf("%s\n", mf.LatestManifest) //print the latest manifest hash for user reference
   117  }
   118  
   119  func listMounts(cliContext *cli.Context) {
   120  	client, err := dialRPC(cliContext)
   121  	if err != nil {
   122  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
   123  	}
   124  	defer client.Close()
   125  
   126  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
   127  	defer cancel()
   128  
   129  	mf := []fuse.MountInfo{}
   130  	err = client.CallContext(ctx, &mf, "swarmfs_listmounts")
   131  	if err != nil {
   132  		utils.Fatalf("encountered an error calling the RPC endpoint while listing mounts: %v", err)
   133  	}
   134  	if len(mf) == 0 {
   135  		fmt.Print("Could not found any swarmfs mounts. Please make sure you've specified the correct RPC endpoint\n")
   136  	} else {
   137  		fmt.Printf("Found %d swarmfs mount(s):\n", len(mf))
   138  		for i, mountInfo := range mf {
   139  			fmt.Printf("%d:\n", i)
   140  			fmt.Printf("\tMount point: %s\n", mountInfo.MountPoint)
   141  			fmt.Printf("\tLatest Manifest: %s\n", mountInfo.LatestManifest)
   142  			fmt.Printf("\tStart Manifest: %s\n", mountInfo.StartManifest)
   143  		}
   144  	}
   145  }
   146  
   147  func dialRPC(ctx *cli.Context) (*rpc.Client, error) {
   148  	var endpoint string
   149  
   150  	if ctx.IsSet(utils.IPCPathFlag.Name) {
   151  		endpoint = ctx.String(utils.IPCPathFlag.Name)
   152  	} else {
   153  		utils.Fatalf("swarm ipc endpoint not specified")
   154  	}
   155  
   156  	if endpoint == "" {
   157  		endpoint = node.DefaultIPCEndpoint(clientIdentifier)
   158  	} else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
   159  		// Backwards compatibility with geth < 1.5 which required
   160  		// these prefixes.
   161  		endpoint = endpoint[4:]
   162  	}
   163  	return rpc.Dial(endpoint)
   164  }