github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/cmd/swarm/fs.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:31</date> 10 //</624342605596987392> 11 12 13 package main 14 15 import ( 16 "context" 17 "fmt" 18 "path/filepath" 19 "strings" 20 "time" 21 22 "github.com/ethereum/go-ethereum/cmd/utils" 23 "github.com/ethereum/go-ethereum/node" 24 "github.com/ethereum/go-ethereum/rpc" 25 "github.com/ethereum/go-ethereum/swarm/fuse" 26 "gopkg.in/urfave/cli.v1" 27 ) 28 29 func mount(cliContext *cli.Context) { 30 args := cliContext.Args() 31 if len(args) < 2 { 32 utils.Fatalf("Usage: swarm fs mount --ipcpath <path to bzzd.ipc> <manifestHash> <file name>") 33 } 34 35 client, err := dialRPC(cliContext) 36 if err != nil { 37 utils.Fatalf("had an error dailing to RPC endpoint: %v", err) 38 } 39 defer client.Close() 40 41 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 42 defer cancel() 43 44 mf := &fuse.MountInfo{} 45 mountPoint, err := filepath.Abs(filepath.Clean(args[1])) 46 if err != nil { 47 utils.Fatalf("error expanding path for mount point: %v", err) 48 } 49 err = client.CallContext(ctx, mf, "swarmfs_mount", args[0], mountPoint) 50 if err != nil { 51 utils.Fatalf("had an error calling the RPC endpoint while mounting: %v", err) 52 } 53 } 54 55 func unmount(cliContext *cli.Context) { 56 args := cliContext.Args() 57 58 if len(args) < 1 { 59 utils.Fatalf("Usage: swarm fs unmount --ipcpath <path to bzzd.ipc> <mount path>") 60 } 61 client, err := dialRPC(cliContext) 62 if err != nil { 63 utils.Fatalf("had an error dailing to RPC endpoint: %v", err) 64 } 65 defer client.Close() 66 67 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 68 defer cancel() 69 70 mf := fuse.MountInfo{} 71 err = client.CallContext(ctx, &mf, "swarmfs_unmount", args[0]) 72 if err != nil { 73 utils.Fatalf("encountered an error calling the RPC endpoint while unmounting: %v", err) 74 } 75 fmt.Printf("%s\n", mf.LatestManifest) // 76 } 77 78 func listMounts(cliContext *cli.Context) { 79 client, err := dialRPC(cliContext) 80 if err != nil { 81 utils.Fatalf("had an error dailing to RPC endpoint: %v", err) 82 } 83 defer client.Close() 84 85 ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) 86 defer cancel() 87 88 mf := []fuse.MountInfo{} 89 err = client.CallContext(ctx, &mf, "swarmfs_listmounts") 90 if err != nil { 91 utils.Fatalf("encountered an error calling the RPC endpoint while listing mounts: %v", err) 92 } 93 if len(mf) == 0 { 94 fmt.Print("Could not found any swarmfs mounts. Please make sure you've specified the correct RPC endpoint\n") 95 } else { 96 fmt.Printf("Found %d swarmfs mount(s):\n", len(mf)) 97 for i, mountInfo := range mf { 98 fmt.Printf("%d:\n", i) 99 fmt.Printf("\tMount point: %s\n", mountInfo.MountPoint) 100 fmt.Printf("\tLatest Manifest: %s\n", mountInfo.LatestManifest) 101 fmt.Printf("\tStart Manifest: %s\n", mountInfo.StartManifest) 102 } 103 } 104 } 105 106 func dialRPC(ctx *cli.Context) (*rpc.Client, error) { 107 var endpoint string 108 109 if ctx.IsSet(utils.IPCPathFlag.Name) { 110 endpoint = ctx.String(utils.IPCPathFlag.Name) 111 } else { 112 utils.Fatalf("swarm ipc endpoint not specified") 113 } 114 115 if endpoint == "" { 116 endpoint = node.DefaultIPCEndpoint(clientIdentifier) 117 } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") { 118 //与geth的向后兼容性<1.5,这需要 119 //这些前缀。 120 endpoint = endpoint[4:] 121 } 122 return rpc.Dial(endpoint) 123 } 124