break ipns into separate files
Jeromy committed
Oct 2, 2014 at 17:25 UTC
80c64ffa6d1e03ebcabed8a116d46daf4cbf6061
4 files changed
+153
-132
fuse/ipns/ipns_unix.go
-132
@@ -1,14 +1,9 @@
1
package ipns
2
3
import (
4
- "fmt"
4
"io/ioutil"
5
"os"
7
- "os/exec"
8
- "os/signal"
6
"path/filepath"
10
- "runtime"
11
- "syscall"
7
"time"
8
9
"bytes"
@@ -469,130 +464,3 @@ func (n *Node) Rename(req *fuse.RenameRequest, newDir fs.Node, intr fs.Intr) fus
464
}
465
return nil
466
}
472
-
473
-// Mount mounts an IpfsNode instance at a particular path. It
474
-// serves until the process receives exit signals (to Unmount).
475
-func Mount(ipfs *core.IpfsNode, fpath string, ipfspath string) error {
476
-
477
- sigc := make(chan os.Signal, 1)
478
- signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT,
479
- syscall.SIGTERM, syscall.SIGQUIT)
480
-
481
- go func() {
482
- <-sigc
483
- for {
484
- err := Unmount(fpath)
485
- if err == nil {
486
- return
487
- }
488
- time.Sleep(time.Millisecond * 100)
489
- }
490
- ipfs.Network.Close()
491
- }()
492
-
493
- c, err := fuse.Mount(fpath)
494
- if err != nil {
495
- return err
496
- }
497
- defer c.Close()
498
-
499
- fsys, err := NewIpns(ipfs, ipfspath)
500
- if err != nil {
501
- return err
502
- }
503
-
504
- err = fs.Serve(c, fsys)
505
- if err != nil {
506
- return err
507
- }
508
-
509
- // check if the mount process has an error to report
510
- <-c.Ready
511
- if err := c.MountError; err != nil {
512
- return err
513
- }
514
- return nil
515
-}
516
-
517
-// Unmount attempts to unmount the provided FUSE mount point, forcibly
518
-// if necessary.
519
-func Unmount(point string) error {
520
- fmt.Printf("Unmounting %s...\n", point)
521
-
522
- var cmd *exec.Cmd
523
- switch runtime.GOOS {
524
- case "darwin":
525
- cmd = exec.Command("diskutil", "umount", "force", point)
526
- case "linux":
527
- cmd = exec.Command("fusermount", "-u", point)
528
- default:
529
- return fmt.Errorf("unmount: unimplemented")
530
- }
531
-
532
- errc := make(chan error, 1)
533
- go func() {
534
- if err := exec.Command("umount", point).Run(); err == nil {
535
- errc <- err
536
- }
537
- // retry to unmount with the fallback cmd
538
- errc <- cmd.Run()
539
- }()
540
-
541
- select {
542
- case <-time.After(1 * time.Second):
543
- return fmt.Errorf("umount timeout")
544
- case err := <-errc:
545
- return err
546
- }
547
-}
548
-
549
-type Link struct {
550
- Target string
551
-}
552
-
553
-func (l *Link) Attr() fuse.Attr {
554
- log.Debug("Link attr.")
555
- return fuse.Attr{
556
- Mode: os.ModeSymlink | 0555,
557
- }
558
-}
559
-
560
-func (l *Link) Readlink(req *fuse.ReadlinkRequest, intr fs.Intr) (string, fuse.Error) {
561
- log.Debug("ReadLink: %s", l.Target)
562
- return l.Target, nil
563
-}
564
-
565
-type Republisher struct {
566
- Timeout time.Duration
567
- Publish chan struct{}
568
- node *Node
569
-}
570
-
571
-func NewRepublisher(n *Node, tout time.Duration) *Republisher {
572
- return &Republisher{
573
- Timeout: tout,
574
- Publish: make(chan struct{}),
575
- node: n,
576
- }
577
-}
578
-
579
-func (np *Republisher) Run() {
580
- for _ = range np.Publish {
581
- timer := time.After(np.Timeout)
582
- for {
583
- select {
584
- case <-timer:
585
- //Do the publish!
586
- log.Info("Publishing Changes!")
587
- err := np.node.updateTree()
588
- if err != nil {
589
- log.Critical("updateTree error: %s", err)
590
- }
591
- goto done
592
- case <-np.Publish:
593
- timer = time.After(np.Timeout)
594
- }
595
- }
596
- done:
597
- }
598
-}
fuse/ipns/link_unix.go
new
+24
@@ -0,0 +1,24 @@
1
+package ipns
2
+
3
+import (
4
+ "os"
5
+
6
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse"
7
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs"
8
+)
9
+
10
+type Link struct {
11
+ Target string
12
+}
13
+
14
+func (l *Link) Attr() fuse.Attr {
15
+ log.Debug("Link attr.")
16
+ return fuse.Attr{
17
+ Mode: os.ModeSymlink | 0555,
18
+ }
19
+}
20
+
21
+func (l *Link) Readlink(req *fuse.ReadlinkRequest, intr fs.Intr) (string, fuse.Error) {
22
+ log.Debug("ReadLink: %s", l.Target)
23
+ return l.Target, nil
24
+}
fuse/ipns/mount_unix.go
new
+91
@@ -0,0 +1,91 @@
1
+package ipns
2
+
3
+import (
4
+ "fmt"
5
+ "os"
6
+ "os/exec"
7
+ "os/signal"
8
+ "runtime"
9
+ "syscall"
10
+ "time"
11
+
12
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse"
13
+ "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs"
14
+ "github.com/jbenet/go-ipfs/core"
15
+)
16
+
17
+// Mount mounts an IpfsNode instance at a particular path. It
18
+// serves until the process receives exit signals (to Unmount).
19
+func Mount(ipfs *core.IpfsNode, fpath string, ipfspath string) error {
20
+
21
+ sigc := make(chan os.Signal, 1)
22
+ signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT,
23
+ syscall.SIGTERM, syscall.SIGQUIT)
24
+
25
+ go func() {
26
+ <-sigc
27
+ for {
28
+ err := Unmount(fpath)
29
+ if err == nil {
30
+ return
31
+ }
32
+ time.Sleep(time.Millisecond * 100)
33
+ }
34
+ ipfs.Network.Close()
35
+ }()
36
+
37
+ c, err := fuse.Mount(fpath)
38
+ if err != nil {
39
+ return err
40
+ }
41
+ defer c.Close()
42
+
43
+ fsys, err := NewIpns(ipfs, ipfspath)
44
+ if err != nil {
45
+ return err
46
+ }
47
+
48
+ err = fs.Serve(c, fsys)
49
+ if err != nil {
50
+ return err
51
+ }
52
+
53
+ // check if the mount process has an error to report
54
+ <-c.Ready
55
+ if err := c.MountError; err != nil {
56
+ return err
57
+ }
58
+ return nil
59
+}
60
+
61
+// Unmount attempts to unmount the provided FUSE mount point, forcibly
62
+// if necessary.
63
+func Unmount(point string) error {
64
+ fmt.Printf("Unmounting %s...\n", point)
65
+
66
+ var cmd *exec.Cmd
67
+ switch runtime.GOOS {
68
+ case "darwin":
69
+ cmd = exec.Command("diskutil", "umount", "force", point)
70
+ case "linux":
71
+ cmd = exec.Command("fusermount", "-u", point)
72
+ default:
73
+ return fmt.Errorf("unmount: unimplemented")
74
+ }
75
+
76
+ errc := make(chan error, 1)
77
+ go func() {
78
+ if err := exec.Command("umount", point).Run(); err == nil {
79
+ errc <- err
80
+ }
81
+ // retry to unmount with the fallback cmd
82
+ errc <- cmd.Run()
83
+ }()
84
+
85
+ select {
86
+ case <-time.After(1 * time.Second):
87
+ return fmt.Errorf("umount timeout")
88
+ case err := <-errc:
89
+ return err
90
+ }
91
+}
fuse/ipns/repub_unix.go
new
+38
@@ -0,0 +1,38 @@
1
+package ipns
2
+
3
+import "time"
4
+
5
+type Republisher struct {
6
+ Timeout time.Duration
7
+ Publish chan struct{}
8
+ node *Node
9
+}
10
+
11
+func NewRepublisher(n *Node, tout time.Duration) *Republisher {
12
+ return &Republisher{
13
+ Timeout: tout,
14
+ Publish: make(chan struct{}),
15
+ node: n,
16
+ }
17
+}
18
+
19
+func (np *Republisher) Run() {
20
+ for _ = range np.Publish {
21
+ timer := time.After(np.Timeout)
22
+ for {
23
+ select {
24
+ case <-timer:
25
+ //Do the publish!
26
+ log.Info("Publishing Changes!")
27
+ err := np.node.updateTree()
28
+ if err != nil {
29
+ log.Critical("updateTree error: %s", err)
30
+ }
31
+ goto done
32
+ case <-np.Publish:
33
+ timer = time.After(np.Timeout)
34
+ }
35
+ }
36
+ done:
37
+ }
38
+}