Skip to content

Commit

Permalink
rework config, remove windows manet hacks, remove ipns
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Sep 21, 2019
1 parent 4717f73 commit 21b2d2c
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 484 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ require (
github.com/libp2p/go-maddr-filter v0.0.5
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.1.2
github.com/mr-tron/base58 v1.1.2
github.com/multiformats/go-multiaddr v0.0.4
github.com/multiformats/go-multiaddr-dns v0.0.3
Expand Down
39 changes: 22 additions & 17 deletions plugin/plugins/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package filesystem

import (
"context"
"encoding/json"
"os"
"path/filepath"
"runtime"

"github.com/djdv/p9/p9"
plugin "github.com/ipfs/go-ipfs/plugin"
fsnodes "github.com/ipfs/go-ipfs/plugin/plugins/filesystem/nodes"
logging "github.com/ipfs/go-log"
coreiface "github.com/ipfs/interface-go-ipfs-core"
"github.com/mitchellh/mapstructure"
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
)
Expand Down Expand Up @@ -50,6 +49,8 @@ func (*FileSystemPlugin) Version() string {

func (fs *FileSystemPlugin) Init(env *plugin.Environment) error {
logger.Info("Initializing 9P resource server...")

// stabilise repo path
if !filepath.IsAbs(env.Repo) {
absRepo, err := filepath.Abs(env.Repo)
if err != nil {
Expand All @@ -59,27 +60,30 @@ func (fs *FileSystemPlugin) Init(env *plugin.Environment) error {
}

cfg := &Config{}
// load config from file or initialize it
if env.Config != nil {
byteRep, err := json.Marshal(env.Config)
if err != nil {
return err
}
if err = json.Unmarshal(byteRep, cfg); err != nil {
if err := mapstructure.Decode(env.Config, cfg); err != nil {
return err
}
} else {
cfg = defaultConfig(env.Repo)
cfg = defaultConfig()
}

var err error
if envAddr := os.ExpandEnv(EnvAddr); envAddr == "" {
fs.addr, err = multiaddr.NewMultiaddr(cfg.Service[defaultService])
var addrString string
// allow environment variable to override config values
if envAddr := os.ExpandEnv(EnvAddr); envAddr != "" {
addrString = EnvAddr
} else {
fs.addr, err = multiaddr.NewMultiaddr(envAddr)
addrString = cfg.Service[defaultService]
}

// expand string templates and initialize listening addr
addrString = os.Expand(addrString, configVarMapper(env.Repo))
ma, err := multiaddr.NewMultiaddr(addrString)
if err != nil {
return err
}
fs.addr = ma

logger.Info("9P resource server okay for launch")
return nil
Expand All @@ -88,23 +92,24 @@ func (fs *FileSystemPlugin) Init(env *plugin.Environment) error {
func (fs *FileSystemPlugin) Start(core coreiface.CoreAPI) error {
logger.Info("Starting 9P resource server...")

//TODO [manet]: unix sockets are not removed on process death (on Windows)
// so for now we just try to remove it before listening on it
if runtime.GOOS == "windows" {
removeUnixSockets(fs.addr)
// make sure sockets are not in use already (if we're using them)
err := removeUnixSockets(fs.addr)
if err != nil {
return err
}

fs.ctx, fs.cancel = context.WithCancel(context.Background())
fs.errorChan = make(chan error, 1)

// launch the listener
listener, err := manet.Listen(fs.addr)
if err != nil {
logger.Errorf("9P listen error: %s\n", err)
return err
}
fs.listener = listener

// construct and run the 9P resource server
// construct and launch the 9P resource server
s := p9.NewServer(fsnodes.RootAttacher(fs.ctx, core))
go func() {
fs.errorChan <- s.Serve(manet.NetListener(fs.listener))
Expand Down
43 changes: 28 additions & 15 deletions plugin/plugins/filesystem/nodes/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ import (
)

const ( //device - attempts to comply with standard multicodec table
dMemory = 0x2f
dMemory = 0x2f // generic "path"
dIPFS = 0xe4
dIPNS = 0xe5
//TODO: decide what MFS/Files API should be; IPLD?
//dFiles = 0xe2
)

var _ p9.File = (*Base)(nil)
Expand Down Expand Up @@ -53,24 +50,40 @@ type IPFSBase struct {
// and cancel it in another operation (like Close)
// that pointer should be stored here between calls
operationsCancel context.CancelFunc

// operation handle storage
file files.File
directory *directoryStream
}

func (b Base) Close() error {
if b.child != nil {
return b.child.Close()
}

return nil
}

func (ib *IPFSBase) Close() error {
if ib.operationsCancel != nil {
ib.operationsCancel()
var lastErr error

if err := ib.Base.Close(); err != nil {
ib.Logger.Errorf("base close: %s", err)
lastErr = err
}
if ib.parent != nil {
ib.parent.Close()

if ib.file != nil {
if err := ib.file.Close(); err != nil {
ib.Logger.Errorf("files.File close: %s", err)
lastErr = err
}
}
if ib.child != nil {
ib.child.Close()

if ib.operationsCancel != nil {
ib.operationsCancel()
}
return nil
}

type ipfsHandle struct {
file files.File
directory *directoryStream
return lastErr
}

type directoryStream struct {
Expand Down
11 changes: 7 additions & 4 deletions plugin/plugins/filesystem/nodes/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
// e.g. `ipfs.Walk([]string("Qm...", "subdir")` not `ipfs.Walk([]string("ipfs", "Qm...", "subdir")`
type IPFS struct {
IPFSBase
ipfsHandle
}

func IPFSAttacher(ctx context.Context, core coreiface.CoreAPI) *IPFS {
Expand Down Expand Up @@ -66,19 +65,23 @@ func (id *IPFS) Walk(names []string) ([]p9.QID, p9.File, error) {
callCtx, cancel := context.WithTimeout(id.Ctx, 30*time.Second)
defer cancel()

if newFid.Path, err = id.core.ResolvePath(callCtx, corepath.Join(newFid.Path, name)); err != nil {
corePath, err := id.core.ResolvePath(callCtx, corepath.Join(newFid.Path, name))
if err != nil {
cancel()
//TODO: switch off error, return appropriate errno (likely ENOENT)
//TODO: switch off error, return appropriate errno (ENOENT is going to be common here)
// ref: https://github.com/hugelgupf/p9/pull/12#discussion_r324991695
return qids, nil, err
}

newFid.Path = corePath

ipldNode, err = id.core.Dag().Get(callCtx, newFid.Path.Cid())
if err != nil {
cancel()
return qids, nil, err
}

err, _ := ipldStat(callCtx, attr, ipldNode, requestType)
err, _ = ipldStat(callCtx, attr, ipldNode, requestType)
if err != nil {
cancel()
return qids, nil, err
Expand Down
Loading

0 comments on commit 21b2d2c

Please sign in to comment.