mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-05 00:08:06 +08:00
this is a major refactor of the entire codebase it changes the monolithic peer.Peer into using a peer.ID and a peer.Peerstore. Other changes: - removed handshake3. - testutil vastly simplified peer - secio bugfix + debugging logs - testutil: RandKeyPair - backpressure bugfix: w.o.w. - peer: added hex enc/dec - peer: added a PeerInfo struct PeerInfo is a small struct used to pass around a peer with a set of addresses and keys. This is not meant to be a complete view of the system, but rather to model updates to the peerstore. It is used by things like the routing system. - updated peer/queue + peerset - latency metrics - testutil: use crand for PeerID gen RandPeerID generates random "valid" peer IDs. it does not NEED to generate keys because it is as if we lost the key right away. fine to read some randomness and hash it. to generate proper keys and an ID, use: sk, pk, _ := testutil.RandKeyPair() id, _ := peer.IDFromPublicKey(pk) Also added RandPeerIDFatal helper - removed old spipe - updated seccat - core: cleanup initIdentity - removed old getFromPeerList
101 lines
2.3 KiB
Go
101 lines
2.3 KiB
Go
package ipns
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"runtime"
|
|
"time"
|
|
|
|
fuse "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse"
|
|
fs "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs"
|
|
|
|
core "github.com/jbenet/go-ipfs/core"
|
|
mount "github.com/jbenet/go-ipfs/fuse/mount"
|
|
)
|
|
|
|
// Mount mounts an IpfsNode instance at a particular path. It
|
|
// serves until the process receives exit signals (to Unmount).
|
|
func Mount(ipfs *core.IpfsNode, fpath string, ipfspath string) (mount.Mount, error) {
|
|
log.Infof("Mounting ipns at %s...", fpath)
|
|
|
|
// setup the Mount abstraction.
|
|
m := mount.New(ipfs.Context(), fpath)
|
|
|
|
// go serve the mount
|
|
m.Mount(func(m mount.Mount) error {
|
|
return internalMount(ipfs, fpath, ipfspath)
|
|
}, internalUnmount)
|
|
|
|
select {
|
|
case <-m.Closed():
|
|
return nil, fmt.Errorf("failed to mount")
|
|
case <-time.After(time.Second):
|
|
// assume it worked...
|
|
}
|
|
|
|
// bind the mount (ContextGroup) to the node, so that when the node exits
|
|
// the fsclosers are automatically closed.
|
|
ipfs.AddChildGroup(m)
|
|
return m, nil
|
|
}
|
|
|
|
// mount attempts to mount at the provided FUSE mount point
|
|
func internalMount(ipfs *core.IpfsNode, fpath string, ipfspath string) error {
|
|
|
|
c, err := fuse.Mount(fpath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer c.Close()
|
|
|
|
fsys, err := NewIpns(ipfs, ipfs.PrivateKey, ipfspath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Infof("Mounted ipns at %s.", fpath)
|
|
if err := fs.Serve(c, fsys); err != nil {
|
|
return err
|
|
}
|
|
|
|
// check if the mount process has an error to report
|
|
<-c.Ready
|
|
if err := c.MountError; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// unmount attempts to unmount the provided FUSE mount point, forcibly
|
|
// if necessary.
|
|
func internalUnmount(m mount.Mount) error {
|
|
point := m.MountPoint()
|
|
log.Infof("Unmounting ipns at %s...", point)
|
|
|
|
var cmd *exec.Cmd
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
cmd = exec.Command("diskutil", "umount", "force", point)
|
|
case "linux":
|
|
cmd = exec.Command("fusermount", "-u", point)
|
|
default:
|
|
return fmt.Errorf("unmount: unimplemented")
|
|
}
|
|
|
|
errc := make(chan error, 1)
|
|
go func() {
|
|
if err := exec.Command("umount", point).Run(); err == nil {
|
|
errc <- err
|
|
}
|
|
// retry to unmount with the fallback cmd
|
|
errc <- cmd.Run()
|
|
}()
|
|
|
|
select {
|
|
case <-time.After(1 * time.Second):
|
|
return fmt.Errorf("umount timeout")
|
|
case err := <-errc:
|
|
return err
|
|
}
|
|
}
|