github.com/pslzym/go-ethereum@v1.8.17-0.20180926104442-4b6824e07b1b/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  func mount(cliContext *cli.Context) {
    34  	args := cliContext.Args()
    35  	if len(args) < 2 {
    36  		utils.Fatalf("Usage: swarm fs mount --ipcpath <path to bzzd.ipc> <manifestHash> <file name>")
    37  	}
    38  
    39  	client, err := dialRPC(cliContext)
    40  	if err != nil {
    41  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
    42  	}
    43  	defer client.Close()
    44  
    45  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    46  	defer cancel()
    47  
    48  	mf := &fuse.MountInfo{}
    49  	mountPoint, err := filepath.Abs(filepath.Clean(args[1]))
    50  	if err != nil {
    51  		utils.Fatalf("error expanding path for mount point: %v", err)
    52  	}
    53  	err = client.CallContext(ctx, mf, "swarmfs_mount", args[0], mountPoint)
    54  	if err != nil {
    55  		utils.Fatalf("had an error calling the RPC endpoint while mounting: %v", err)
    56  	}
    57  }
    58  
    59  func unmount(cliContext *cli.Context) {
    60  	args := cliContext.Args()
    61  
    62  	if len(args) < 1 {
    63  		utils.Fatalf("Usage: swarm fs unmount --ipcpath <path to bzzd.ipc> <mount path>")
    64  	}
    65  	client, err := dialRPC(cliContext)
    66  	if err != nil {
    67  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
    68  	}
    69  	defer client.Close()
    70  
    71  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    72  	defer cancel()
    73  
    74  	mf := fuse.MountInfo{}
    75  	err = client.CallContext(ctx, &mf, "swarmfs_unmount", args[0])
    76  	if err != nil {
    77  		utils.Fatalf("encountered an error calling the RPC endpoint while unmounting: %v", err)
    78  	}
    79  	fmt.Printf("%s\n", mf.LatestManifest) //print the latest manifest hash for user reference
    80  }
    81  
    82  func listMounts(cliContext *cli.Context) {
    83  	client, err := dialRPC(cliContext)
    84  	if err != nil {
    85  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
    86  	}
    87  	defer client.Close()
    88  
    89  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    90  	defer cancel()
    91  
    92  	mf := []fuse.MountInfo{}
    93  	err = client.CallContext(ctx, &mf, "swarmfs_listmounts")
    94  	if err != nil {
    95  		utils.Fatalf("encountered an error calling the RPC endpoint while listing mounts: %v", err)
    96  	}
    97  	if len(mf) == 0 {
    98  		fmt.Print("Could not found any swarmfs mounts. Please make sure you've specified the correct RPC endpoint\n")
    99  	} else {
   100  		fmt.Printf("Found %d swarmfs mount(s):\n", len(mf))
   101  		for i, mountInfo := range mf {
   102  			fmt.Printf("%d:\n", i)
   103  			fmt.Printf("\tMount point: %s\n", mountInfo.MountPoint)
   104  			fmt.Printf("\tLatest Manifest: %s\n", mountInfo.LatestManifest)
   105  			fmt.Printf("\tStart Manifest: %s\n", mountInfo.StartManifest)
   106  		}
   107  	}
   108  }
   109  
   110  func dialRPC(ctx *cli.Context) (*rpc.Client, error) {
   111  	var endpoint string
   112  
   113  	if ctx.IsSet(utils.IPCPathFlag.Name) {
   114  		endpoint = ctx.String(utils.IPCPathFlag.Name)
   115  	} else {
   116  		utils.Fatalf("swarm ipc endpoint not specified")
   117  	}
   118  
   119  	if endpoint == "" {
   120  		endpoint = node.DefaultIPCEndpoint(clientIdentifier)
   121  	} else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
   122  		// Backwards compatibility with geth < 1.5 which required
   123  		// these prefixes.
   124  		endpoint = endpoint[4:]
   125  	}
   126  	return rpc.Dial(endpoint)
   127  }