@cryptotaxi247 / kubo / commits / 7a4491ada

Added --mount flag to mount when running 'ipfs daemon'

Matt Bell committed Nov 18, 2014 at 03:28 UTC 7a4491ada3ea4c4895a545ad603a7c3dfd393659
2 files changed +69 -25
cmd/ipfs2/daemon.go
+43 -9
@@ -18,6 +18,9 @@ import (
18
19 const (
20 initOptionKwd = "init"
21 + mountKwd = "mount"
22 + ipfsMountKwd = "mount-ipfs"
23 + ipnsMountKwd = "mount-ipns"
24 )
25
26 var daemonCmd = &cmds.Command{
@@ -34,12 +37,24 @@ the daemon.
37
38 Options: []cmds.Option{
39 cmds.BoolOption(initOptionKwd, "Initialize IPFS with default settings if not already initialized"),
40 + cmds.BoolOption(mountKwd, "Mounts IPFS to the filesystem"),
41 + cmds.StringOption(ipfsMountKwd, "Path to the mountpoint for IPFS (if using --mount)"),
42 + cmds.StringOption(ipnsMountKwd, "Path to the mountpoint for IPNS (if using --mount)"),
43 },
44 Subcommands: map[string]*cmds.Command{},
45 Run: daemonFunc,
46 }
47
48 func daemonFunc(req cmds.Request) (interface{}, error) {
49 + cfg, err := req.Context().GetConfig()
50 + if err != nil {
51 + return nil, err
52 + }
53 +
54 + node, err := core.NewIpfsNode(cfg, true)
55 + if err != nil {
56 + return nil, err
57 + }
58
59 initialize, _, err := req.Option(initOptionKwd).Bool()
60 if err != nil {
@@ -65,30 +80,49 @@ func daemonFunc(req cmds.Request) (interface{}, error) {
80 }
81 defer lock.Close()
82
68 - cfg, err := req.Context().GetConfig()
69 - if err != nil {
70 - return nil, err
71 - }
72 -
83 // setup function that constructs the context. we have to do it this way
84 // to play along with how the Context works and thus not expose its internals
85 req.Context().ConstructNode = func() (*core.IpfsNode, error) {
76 - return core.NewIpfsNode(cfg, true)
86 + return node, nil
87 }
78 - node, err := req.Context().GetNode()
88 +
89 + addr, err := ma.NewMultiaddr(cfg.Addresses.API)
90 if err != nil {
91 return nil, err
92 }
93
83 - addr, err := ma.NewMultiaddr(cfg.Addresses.API)
94 + _, host, err := manet.DialArgs(addr)
95 if err != nil {
96 return nil, err
97 }
98
88 - _, host, err := manet.DialArgs(addr)
99 + // mount if the user provided the --mount flag
100 + mount, _, err := req.Option(mountKwd).Bool()
101 if err != nil {
102 return nil, err
103 }
104 + if mount {
105 + fsdir, found, err := req.Option(ipfsMountKwd).String()
106 + if err != nil {
107 + return nil, err
108 + }
109 + if !found {
110 + fsdir = cfg.Mounts.IPFS
111 + }
112 +
113 + nsdir, found, err := req.Option(ipnsMountKwd).String()
114 + if err != nil {
115 + return nil, err
116 + }
117 + if !found {
118 + nsdir = cfg.Mounts.IPNS
119 + }
120 +
121 + err = commands.Mount(node, fsdir, nsdir)
122 + if err != nil {
123 + return nil, err
124 + }
125 + }
126
127 mux := http.NewServeMux()
128 cmdHandler := cmdsHttp.NewHandler(*req.Context(), commands.Root)
core/commands2/mount_unix.go
+26 -16
@@ -100,25 +100,11 @@ baz
100 return nil, err
101 }
102
103 - // check if we already have live mounts.
104 - // if the user said "Mount", then there must be something wrong.
105 - // so, close them and try again.
106 - if node.Mounts.Ipfs != nil {
107 - node.Mounts.Ipfs.Unmount()
108 - }
109 - if node.Mounts.Ipns != nil {
110 - node.Mounts.Ipns.Unmount()
111 - }
112 -
103 // error if we aren't running node in online mode
114 - if node.Network == nil {
104 + if !node.OnlineMode() {
105 return nil, errNotOnline
106 }
107
118 - if err := platformFuseChecks(); err != nil {
119 - return nil, err
120 - }
121 -
108 fsdir, found, err := req.Option("f").String()
109 if err != nil {
110 return nil, err
@@ -136,7 +122,8 @@ baz
122 nsdir = cfg.Mounts.IPNS // NB: be sure to not redeclare!
123 }
124
139 - if err := doMount(node, fsdir, nsdir); err != nil {
125 + err = Mount(node, fsdir, nsdir)
126 + if err != nil {
127 return nil, err
128 }
129
@@ -207,3 +194,26 @@ func doMount(node *core.IpfsNode, fsdir, nsdir string) error {
194 var platformFuseChecks = func() error {
195 return nil
196 }
197 +
198 +func Mount(node *core.IpfsNode, fsdir, nsdir string) error {
199 + // check if we already have live mounts.
200 + // if the user said "Mount", then there must be something wrong.
201 + // so, close them and try again.
202 + if node.Mounts.Ipfs != nil {
203 + node.Mounts.Ipfs.Unmount()
204 + }
205 + if node.Mounts.Ipns != nil {
206 + node.Mounts.Ipns.Unmount()
207 + }
208 +
209 + if err := platformFuseChecks(); err != nil {
210 + return err
211 + }
212 +
213 + var err error
214 + if err = doMount(node, fsdir, nsdir); err != nil {
215 + return err
216 + }
217 +
218 + return nil
219 +}