aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/swarm/fs.go
blob: 0124586cfe19b5340d276ef1bb39bc85bdbff44f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2018 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
    "context"
    "fmt"
    "path/filepath"
    "strings"
    "time"

    "github.com/ethereum/go-ethereum/cmd/utils"
    "github.com/ethereum/go-ethereum/node"
    "github.com/ethereum/go-ethereum/rpc"
    "github.com/ethereum/go-ethereum/swarm/fuse"
    "gopkg.in/urfave/cli.v1"
)

func mount(cliContext *cli.Context) {
    args := cliContext.Args()
    if len(args) < 2 {
        utils.Fatalf("Usage: swarm fs mount --ipcpath <path to bzzd.ipc> <manifestHash> <file name>")
    }

    client, err := dialRPC(cliContext)
    if err != nil {
        utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
    }
    defer client.Close()

    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    mf := &fuse.MountInfo{}
    mountPoint, err := filepath.Abs(filepath.Clean(args[1]))
    if err != nil {
        utils.Fatalf("error expanding path for mount point: %v", err)
    }
    err = client.CallContext(ctx, mf, "swarmfs_mount", args[0], mountPoint)
    if err != nil {
        utils.Fatalf("had an error calling the RPC endpoint while mounting: %v", err)
    }
}

func unmount(cliContext *cli.Context) {
    args := cliContext.Args()

    if len(args) < 1 {
        utils.Fatalf("Usage: swarm fs unmount --ipcpath <path to bzzd.ipc> <mount path>")
    }
    client, err := dialRPC(cliContext)
    if err != nil {
        utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
    }
    defer client.Close()

    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    mf := fuse.MountInfo{}
    err = client.CallContext(ctx, &mf, "swarmfs_unmount", args[0])
    if err != nil {
        utils.Fatalf("encountered an error calling the RPC endpoint while unmounting: %v", err)
    }
    fmt.Printf("%s\n", mf.LatestManifest) //print the latest manifest hash for user reference
}

func listMounts(cliContext *cli.Context) {
    client, err := dialRPC(cliContext)
    if err != nil {
        utils.Fatalf("had an error dailing to RPC endpoint: %v", err)
    }
    defer client.Close()

    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    mf := []fuse.MountInfo{}
    err = client.CallContext(ctx, &mf, "swarmfs_listmounts")
    if err != nil {
        utils.Fatalf("encountered an error calling the RPC endpoint while unmounting: %v", err)
    }
    if len(mf) == 0 {
        fmt.Print("Could not found any swarmfs mounts. Please make sure you've specified the correct RPC endpoint\n")
    } else {
        fmt.Printf("Found %d swarmfs mount(s):\n", len(mf))
        for i, mountInfo := range mf {
            fmt.Printf("%d:\n", i)
            fmt.Printf("\tMount point: %s\n", mountInfo.MountPoint)
            fmt.Printf("\tLatest Manifest: %s\n", mountInfo.LatestManifest)
            fmt.Printf("\tStart Manifest: %s\n", mountInfo.StartManifest)
        }
    }
}

func dialRPC(ctx *cli.Context) (*rpc.Client, error) {
    var endpoint string

    if ctx.IsSet(utils.IPCPathFlag.Name) {
        endpoint = ctx.String(utils.IPCPathFlag.Name)
    } else {
        utils.Fatalf("swarm ipc endpoint not specified")
    }

    if endpoint == "" {
        endpoint = node.DefaultIPCEndpoint(clientIdentifier)
    } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
        // Backwards compatibility with geth < 1.5 which required
        // these prefixes.
        endpoint = endpoint[4:]
    }
    return rpc.Dial(endpoint)
}