github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/swarm/fs.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2018 Go Ethereum作者
    10  //此文件是Go以太坊的一部分。
    11  //
    12  //Go以太坊是免费软件:您可以重新发布和/或修改它
    13  //根据GNU通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊的分布希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU通用公共许可证了解更多详细信息。
    21  //
    22  //你应该已经收到一份GNU通用公共许可证的副本
    23  //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package main
    26  
    27  import (
    28  	"context"
    29  	"fmt"
    30  	"path/filepath"
    31  	"strings"
    32  	"time"
    33  
    34  	"github.com/ethereum/go-ethereum/cmd/utils"
    35  	"github.com/ethereum/go-ethereum/node"
    36  	"github.com/ethereum/go-ethereum/rpc"
    37  	"github.com/ethereum/go-ethereum/swarm/fuse"
    38  	"gopkg.in/urfave/cli.v1"
    39  )
    40  
    41  func mount(cliContext *cli.Context) {
    42  	args := cliContext.Args()
    43  	if len(args) < 2 {
    44  		utils.Fatalf("Usage: swarm fs mount --ipcpath <path to bzzd.ipc> <manifestHash> <file name>")
    45  	}
    46  
    47  	client, err := dialRPC(cliContext)
    48  	if err != nil {
    49  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
    50  	}
    51  	defer client.Close()
    52  
    53  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    54  	defer cancel()
    55  
    56  	mf := &fuse.MountInfo{}
    57  	mountPoint, err := filepath.Abs(filepath.Clean(args[1]))
    58  	if err != nil {
    59  		utils.Fatalf("error expanding path for mount point: %v", err)
    60  	}
    61  	err = client.CallContext(ctx, mf, "swarmfs_mount", args[0], mountPoint)
    62  	if err != nil {
    63  		utils.Fatalf("had an error calling the RPC endpoint while mounting: %v", err)
    64  	}
    65  }
    66  
    67  func unmount(cliContext *cli.Context) {
    68  	args := cliContext.Args()
    69  
    70  	if len(args) < 1 {
    71  		utils.Fatalf("Usage: swarm fs unmount --ipcpath <path to bzzd.ipc> <mount path>")
    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  	err = client.CallContext(ctx, &mf, "swarmfs_unmount", args[0])
    84  	if err != nil {
    85  		utils.Fatalf("encountered an error calling the RPC endpoint while unmounting: %v", err)
    86  	}
    87  fmt.Printf("%s\n", mf.LatestManifest) //
    88  }
    89  
    90  func listMounts(cliContext *cli.Context) {
    91  	client, err := dialRPC(cliContext)
    92  	if err != nil {
    93  		utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
    94  	}
    95  	defer client.Close()
    96  
    97  	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    98  	defer cancel()
    99  
   100  	mf := []fuse.MountInfo{}
   101  	err = client.CallContext(ctx, &mf, "swarmfs_listmounts")
   102  	if err != nil {
   103  		utils.Fatalf("encountered an error calling the RPC endpoint while listing mounts: %v", err)
   104  	}
   105  	if len(mf) == 0 {
   106  		fmt.Print("Could not found any swarmfs mounts. Please make sure you've specified the correct RPC endpoint\n")
   107  	} else {
   108  		fmt.Printf("Found %d swarmfs mount(s):\n", len(mf))
   109  		for i, mountInfo := range mf {
   110  			fmt.Printf("%d:\n", i)
   111  			fmt.Printf("\tMount point: %s\n", mountInfo.MountPoint)
   112  			fmt.Printf("\tLatest Manifest: %s\n", mountInfo.LatestManifest)
   113  			fmt.Printf("\tStart Manifest: %s\n", mountInfo.StartManifest)
   114  		}
   115  	}
   116  }
   117  
   118  func dialRPC(ctx *cli.Context) (*rpc.Client, error) {
   119  	var endpoint string
   120  
   121  	if ctx.IsSet(utils.IPCPathFlag.Name) {
   122  		endpoint = ctx.String(utils.IPCPathFlag.Name)
   123  	} else {
   124  		utils.Fatalf("swarm ipc endpoint not specified")
   125  	}
   126  
   127  	if endpoint == "" {
   128  		endpoint = node.DefaultIPCEndpoint(clientIdentifier)
   129  	} else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
   130  //与geth的向后兼容性<1.5,这需要
   131  //这些前缀。
   132  		endpoint = endpoint[4:]
   133  	}
   134  	return rpc.Dial(endpoint)
   135  }