fs.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. package main
  17. import (
  18. "context"
  19. "fmt"
  20. "path/filepath"
  21. "strings"
  22. "time"
  23. "github.com/ethereum/go-ethereum/cmd/utils"
  24. "github.com/ethereum/go-ethereum/log"
  25. "github.com/ethereum/go-ethereum/rpc"
  26. "github.com/ethereum/go-ethereum/swarm/fuse"
  27. "gopkg.in/urfave/cli.v1"
  28. )
  29. var fsCommand = cli.Command{
  30. Name: "fs",
  31. CustomHelpTemplate: helpTemplate,
  32. Usage: "perform FUSE operations",
  33. ArgsUsage: "fs COMMAND",
  34. 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",
  35. Subcommands: []cli.Command{
  36. {
  37. Action: mount,
  38. CustomHelpTemplate: helpTemplate,
  39. Name: "mount",
  40. Usage: "mount a swarm hash to a mount point",
  41. ArgsUsage: "swarm fs mount <manifest hash> <mount point>",
  42. 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",
  43. },
  44. {
  45. Action: unmount,
  46. CustomHelpTemplate: helpTemplate,
  47. Name: "unmount",
  48. Usage: "unmount a swarmfs mount",
  49. ArgsUsage: "swarm fs unmount <mount point>",
  50. 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",
  51. },
  52. {
  53. Action: listMounts,
  54. CustomHelpTemplate: helpTemplate,
  55. Name: "list",
  56. Usage: "list swarmfs mounts",
  57. ArgsUsage: "swarm fs list",
  58. 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",
  59. },
  60. },
  61. }
  62. func mount(cliContext *cli.Context) {
  63. args := cliContext.Args()
  64. if len(args) < 2 {
  65. utils.Fatalf("Usage: swarm fs mount <manifestHash> <file name>")
  66. }
  67. client, err := dialRPC(cliContext)
  68. if err != nil {
  69. utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
  70. }
  71. defer client.Close()
  72. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  73. defer cancel()
  74. mf := &fuse.MountInfo{}
  75. mountPoint, err := filepath.Abs(filepath.Clean(args[1]))
  76. if err != nil {
  77. utils.Fatalf("error expanding path for mount point: %v", err)
  78. }
  79. err = client.CallContext(ctx, mf, "swarmfs_mount", args[0], mountPoint)
  80. if err != nil {
  81. utils.Fatalf("had an error calling the RPC endpoint while mounting: %v", err)
  82. }
  83. }
  84. func unmount(cliContext *cli.Context) {
  85. args := cliContext.Args()
  86. if len(args) < 1 {
  87. utils.Fatalf("Usage: swarm fs unmount <mount path>")
  88. }
  89. client, err := dialRPC(cliContext)
  90. if err != nil {
  91. utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
  92. }
  93. defer client.Close()
  94. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  95. defer cancel()
  96. mf := fuse.MountInfo{}
  97. err = client.CallContext(ctx, &mf, "swarmfs_unmount", args[0])
  98. if err != nil {
  99. utils.Fatalf("encountered an error calling the RPC endpoint while unmounting: %v", err)
  100. }
  101. fmt.Printf("%s\n", mf.LatestManifest) //print the latest manifest hash for user reference
  102. }
  103. func listMounts(cliContext *cli.Context) {
  104. client, err := dialRPC(cliContext)
  105. if err != nil {
  106. utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
  107. }
  108. defer client.Close()
  109. ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
  110. defer cancel()
  111. var mf []fuse.MountInfo
  112. err = client.CallContext(ctx, &mf, "swarmfs_listmounts")
  113. if err != nil {
  114. utils.Fatalf("encountered an error calling the RPC endpoint while listing mounts: %v", err)
  115. }
  116. if len(mf) == 0 {
  117. fmt.Print("Could not found any swarmfs mounts. Please make sure you've specified the correct RPC endpoint\n")
  118. } else {
  119. fmt.Printf("Found %d swarmfs mount(s):\n", len(mf))
  120. for i, mountInfo := range mf {
  121. fmt.Printf("%d:\n", i)
  122. fmt.Printf("\tMount point: %s\n", mountInfo.MountPoint)
  123. fmt.Printf("\tLatest Manifest: %s\n", mountInfo.LatestManifest)
  124. fmt.Printf("\tStart Manifest: %s\n", mountInfo.StartManifest)
  125. }
  126. }
  127. }
  128. func dialRPC(ctx *cli.Context) (*rpc.Client, error) {
  129. endpoint := getIPCEndpoint(ctx)
  130. log.Info("IPC endpoint", "path", endpoint)
  131. return rpc.Dial(endpoint)
  132. }
  133. func getIPCEndpoint(ctx *cli.Context) string {
  134. cfg := defaultNodeConfig
  135. utils.SetNodeConfig(ctx, &cfg)
  136. endpoint := cfg.IPCEndpoint()
  137. if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
  138. // Backwards compatibility with geth < 1.5 which required
  139. // these prefixes.
  140. endpoint = endpoint[4:]
  141. }
  142. return endpoint
  143. }