Skip to content

Commit

Permalink
CR feedback + docs WIP, linting, fix freebsd builds
Browse files Browse the repository at this point in the history
  • Loading branch information
djdv committed Sep 7, 2019
1 parent a4fba6d commit d32927c
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 91 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd
github.com/blang/semver v3.5.1+incompatible
github.com/bren2010/proquint v0.0.0-20160323162903-38337c27106d
github.com/djdv/p9 v0.0.0-20190906032509-18ce8c2eba44
github.com/djdv/p9 v0.0.0-20190907022512-79c201cd4ecc
github.com/dustin/go-humanize v1.0.0
github.com/elgris/jsondiff v0.0.0-20160530203242-765b5c24c302
github.com/fatih/color v1.7.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/dgryski/go-farm v0.0.0-20190104051053-3adb47b1fb0f/go.mod h1:SqUrOPUn
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/djdv/p9 v0.0.0-20190906032509-18ce8c2eba44 h1:y9wbNe6laqcn1PgVJXKhQlJqMZ3M4kYb0MxPDbZnVmA=
github.com/djdv/p9 v0.0.0-20190906032509-18ce8c2eba44/go.mod h1:58+5oyIFRxi9jt7uQrdqpmUryJDTsxY8N8GxhnNocLQ=
github.com/djdv/p9 v0.0.0-20190907022512-79c201cd4ecc h1:v3PQvBZcOFgmwlNYeyFBlT23/x1ec9gZZTM6GQ/9Sbs=
github.com/djdv/p9 v0.0.0-20190907022512-79c201cd4ecc/go.mod h1:58+5oyIFRxi9jt7uQrdqpmUryJDTsxY8N8GxhnNocLQ=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/elgris/jsondiff v0.0.0-20160530203242-765b5c24c302 h1:QV0ZrfBLpFc2KDk+a4LJefDczXnonRwrYrQJY/9L4dA=
Expand Down
10 changes: 10 additions & 0 deletions plugin/plugins/filesystem/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*Package filesystem is an experimental package, that implements the go-ipfs daemon plugin interface
and defines the plugins config structure.
To set the multiaddr listen address, you may use the environment variable $IPFS_FS_ADDR, or set the option in a config file
via `ipfs config --json 'Plugins.Plugins.filesystem.Config "Config":{"Service":{"9P":"/ip4/127.0.0.1/tcp/567"}}'`
To disable this plugin entirely, use: `ipfs config --json Plugins.Plugins.filesystem.Disabled true`
By default, we try to expose the IPFS namespace using the 9P2000.L protocol, over a unix domain socket
(located at $IPFS_PATH/filesystem.9P.sock)*/
package filesystem
40 changes: 16 additions & 24 deletions plugin/plugins/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ import (
manet "github.com/multiformats/go-multiaddr-net"
)

// Plugins is an exported list of plugins that will be loaded by go-ipfs.
var Plugins = []plugin.Plugin{
&FileSystemPlugin{}, //TODO: individually name implementations: &P9{}
}
var (
_ plugin.PluginDaemon = (*FileSystemPlugin)(nil) // impl check

// Plugins is an exported list of plugins that will be loaded by go-ipfs.
Plugins = []plugin.Plugin{
&FileSystemPlugin{}, //TODO: individually name implementations: &P9{}
}

// impl check
var _ plugin.PluginDaemon = (*FileSystemPlugin)(nil)
logger logging.EventLogger
)

func init() {
logger = logging.Logger("plugin/filesystem")
}

type FileSystemPlugin struct {
ctx context.Context
Expand Down Expand Up @@ -62,7 +69,7 @@ func (fs *FileSystemPlugin) Init(env *plugin.Environment) error {
}

var err error
fs.addr, err = multiaddr.NewMultiaddr(cfg.Service[DefaultService])
fs.addr, err = multiaddr.NewMultiaddr(cfg.Service[defaultService])
if err != nil {
return err
}
Expand All @@ -72,14 +79,6 @@ func (fs *FileSystemPlugin) Init(env *plugin.Environment) error {
return nil
}

var (
logger logging.EventLogger
)

func init() {
logger = logging.Logger("plugin/filesystem")
}

func (fs *FileSystemPlugin) Start(core coreiface.CoreAPI) error {
logger.Info("Starting 9P resource server...")

Expand All @@ -89,15 +88,8 @@ func (fs *FileSystemPlugin) Start(core coreiface.CoreAPI) error {
return err
}

// construct 9P resource server
p9pFSS, err := fsnodes.NewRoot(fs.ctx, core, logger)
if err != nil {
logger.Errorf("9P root construction error: %s\n", err)
return err
}

// Run the server.
s := p9.NewServer(p9pFSS)
// construct and run the 9P resource server
s := p9.NewServer(fsnodes.RootAttacher(fs.ctx, core))
go func() {
if err := s.Serve(manet.NetListener(fs.listener)); err != nil {
logger.Errorf("9P server error: %s\n", err)
Expand Down
56 changes: 23 additions & 33 deletions plugin/plugins/filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
fsnodes "github.com/ipfs/go-ipfs/plugin/plugins/filesystem/nodes"
logging "github.com/ipfs/go-log"
coreiface "github.com/ipfs/interface-go-ipfs-core"
coreoptions "github.com/ipfs/interface-go-ipfs-core/options"
corepath "github.com/ipfs/interface-go-ipfs-core/path"
Expand All @@ -37,20 +36,17 @@ func TestAll(t *testing.T) {
t.Fatalf("Failed to construct IPFS node: %s\n", err)
}

logger := logging.Logger("plugin/filesystem")

t.Run("RootFS", func(t *testing.T) { testRootFS(t, ctx, core, logger) })
t.Run("PinFS", func(t *testing.T) { testPinFS(t, ctx, core, logger) })
t.Run("IPFS", func(t *testing.T) { testIPFS(t, ctx, core, logger) })
t.Run("RootFS", func(t *testing.T) { testRootFS(ctx, t, core) })
t.Run("PinFS", func(t *testing.T) { testPinFS(ctx, t, core) })
t.Run("IPFS", func(t *testing.T) { testIPFS(ctx, t, core) })
}

func testRootFS(t *testing.T, ctx context.Context, core coreiface.CoreAPI, logger logging.EventLogger) {
ri, err := fsnodes.NewRoot(ctx, core, logger)
func testRootFS(ctx context.Context, t *testing.T, core coreiface.CoreAPI) {
nineRoot, err := fsnodes.RootAttacher(ctx, core).Attach()
if err != nil {
t.Fatalf("Failed to attach to 9P root resource: %s\n", err)
}

nineRoot, err := ri.Attach()
_, nineRef, err := nineRoot.Walk(nil)
if err != nil {
t.Fatalf("Failed to walk root: %s\n", err)
Expand All @@ -64,17 +60,16 @@ func testRootFS(t *testing.T, ctx context.Context, core coreiface.CoreAPI, logge
t.Fatalf("Failed to read root: %s\n", err)
}

//TODO: currently magic, as subsystems are implemented, rework this part of the test + lib
//TODO: currently magic. As subsystems are implemented, rework this part of the test + lib to contain some list
if len(ents) != 1 || ents[0].Name != "ipfs" {
t.Fatalf("Failed, root has bad entries:: %v\n", ents)
}

//TODO: type checking
}

func testPinFS(t *testing.T, ctx context.Context, core coreiface.CoreAPI, logger logging.EventLogger) {
//init
pinRoot, err := fsnodes.InitPinFS(ctx, core, logger).Attach()
func testPinFS(ctx context.Context, t *testing.T, core coreiface.CoreAPI) {
pinRoot, err := fsnodes.PinFSAttacher(ctx, core).Attach()
if err != nil {
t.Fatalf("Failed to attach to 9P Pin resource: %s\n", err)
}
Expand Down Expand Up @@ -112,7 +107,7 @@ func testPinFS(t *testing.T, ctx context.Context, core coreiface.CoreAPI, logger
//test default (likely empty) test repo pins
shallowCompare()

// test modifying pinset +1; initEnv pins its IPFS envrionment
// test modifying pinset +1; initEnv pins its IPFS environment
env, _, err := initEnv(ctx, core)
if err != nil {
t.Fatalf("Failed to construct IPFS test environment: %s\n", err)
Expand All @@ -128,10 +123,9 @@ func testPinFS(t *testing.T, ctx context.Context, core coreiface.CoreAPI, logger
t.Fatalf("Failed to add directory to IPFS: %s\n", err)
}
shallowCompare()

//TODO: type checking
}
func testIPFS(t *testing.T, ctx context.Context, core coreiface.CoreAPI, logger logging.EventLogger) {

func testIPFS(ctx context.Context, t *testing.T, core coreiface.CoreAPI) {
env, iEnv, err := initEnv(ctx, core)
if err != nil {
t.Fatalf("Failed to construct IPFS test environment: %s\n", err)
Expand All @@ -143,31 +137,26 @@ func testIPFS(t *testing.T, ctx context.Context, core coreiface.CoreAPI, logger
t.Fatalf("Failed to attach to local resource %q: %s\n", env, err)
}

ipfsRoot, err := fsnodes.InitIPFS(ctx, core, logger).Attach()
ipfsRoot, err := fsnodes.IPFSAttacher(ctx, core).Attach()
if err != nil {
t.Fatalf("Failed to attach to IPFS resource: %s\n", err)
}
_, ipfsEnv, err := ipfsRoot.Walk([]string{gopath.Base(iEnv.String())})
if err != nil {
t.Fatalf("Failed to walk to IPFS test envrionment: %s\n", err)
t.Fatalf("Failed to walk to IPFS test environment: %s\n", err)
}

recursiveCompare(t, localEnv, ipfsEnv)
testCompareTreeModes(t, localEnv, ipfsEnv)
}

//TODO: rename
func recursiveCompare(t *testing.T, f1, f2 p9.File) {
func testCompareTreeModes(t *testing.T, f1, f2 p9.File) {
var expand func(p9.File) (map[string]p9.Attr, error)
expand = func(nineRef p9.File) (map[string]p9.Attr, error) {
ents, err := p9Readdir(nineRef)
if err != nil {
return nil, err
}

//TODO: current
// map[string]Stat; ["/sub/incantation"]{...}
///from root, walk(name); stat; if dir; recurse

res := make(map[string]p9.Attr)
for _, ent := range ents {
_, child, err := nineRef.Walk([]string{ent.Name})
Expand Down Expand Up @@ -210,10 +199,10 @@ func recursiveCompare(t *testing.T, f1, f2 p9.File) {

var baseNames []string
var targetNames []string
for name, _ := range base {
for name := range base {
baseNames = append(baseNames, name)
}
for name, _ := range target {
for name := range target {
targetNames = append(targetNames, name)
}

Expand Down Expand Up @@ -368,23 +357,24 @@ func p9PinNames(root p9.File) ([]string, error) {
names = append(names, ent.Name)
}

return names, root.Close()
return names, nil
}

func p9Readdir(dir p9.File) ([]p9.Dirent, error) {
_, dir, err := dir.Walk(nil)
_, dirClone, err := dir.Walk(nil)
if err != nil {
return nil, err
}

_, _, err = dir.Open(p9.ReadOnly)
_, _, err = dirClone.Open(p9.ReadOnly)
if err != nil {
return nil, err
}
defer dir.Close()
return dir.Readdir(0, ^uint32(0))
defer dirClone.Close()
return dirClone.Readdir(0, ^uint32(0))
}

//TODO:
// NOTE: compares a subset of attributes, matching those of IPFS
func testIPFSCompare(t *testing.T, f1, f2 p9.File) {
_, _, f1Attr, err := f1.GetAttr(attrMaskIPFSTest)
Expand Down
7 changes: 7 additions & 0 deletions plugin/plugins/filesystem/nodes/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*Package fsnodes provides constructors and interfaces, for composing various 9P
file systems implementations and wrappers.
The default RootIndex provided by RootAttacher, is a file system of itself
which relays request to various IPFS subsystems.
Utilizing the subsystem implementations itself, in the same way a client program would.*/
package fsnodes
31 changes: 31 additions & 0 deletions plugin/plugins/filesystem/nodes/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fsnodes

import (
"bytes"
"strings"

"github.com/djdv/p9/p9"
)

func ExampleRootIndex() {
root, err := fsnodes.RootAttacher(ctx, coreAPI).Attach()
_, file, err := root.Walk(strings.Split("ipfs/Qm.../subdir/file", "/"))
_, _, err := file.Open(p9.ReadOnly)
defer file.Close()
_, err := file.ReadAt(byteBuffer, offset)
}

func ExampleIPFS() {
ipfs, err := fsnodes.IPFSAttacher(ctx, coreAPI).Attach()
_, file, err := ipfs.Walk(strings.Split("Qm.../subdir/file", "/"))
_, _, err := file.Open(p9.ReadOnly)
defer file.Close()
_, err := file.ReadAt(byteBuffer, offset)
}

func ExamplePinFS() {
ipfs, err := fsnodes.PinFSAttacher(ctx, coreAPI).Attach()
_, dir, err := ipfs.Walk(nil)
_, _, err := dir.Open(p9.ReadOnly)
entries, err := dirClone.Readdir(offset, entryReturnCount)
}
9 changes: 6 additions & 3 deletions plugin/plugins/filesystem/nodes/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import (
corepath "github.com/ipfs/interface-go-ipfs-core/path"
)

// IPFS exposes the IPFS API over a p9.File interface
// Walk does not expect a namespace, only its path argument
// e.g. `ipfs.Walk([]string("Qm...", "subdir")` not `ipfs.Walk([]string("ipfs", "Qm...", "subdir")`
type IPFS struct {
IPFSBase
}

//TODO: [review] check fields; better wrappers around inheritance init, etc.
func InitIPFS(ctx context.Context, core coreiface.CoreAPI, logger logging.EventLogger) p9.Attacher {
id := &IPFS{IPFSBase: newIPFSBase(ctx, newRootPath("/ipfs"), p9.TypeDir, core, logger)}
func IPFSAttacher(ctx context.Context, core coreiface.CoreAPI) *IPFS {
id := &IPFS{IPFSBase: newIPFSBase(ctx, newRootPath("/ipfs"), p9.TypeDir,
core, logging.Logger("IPFS"))}
id.meta, id.metaMask = defaultRootAttr()
return id
}
Expand Down
16 changes: 4 additions & 12 deletions plugin/plugins/filesystem/nodes/pinfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@ type PinFS struct {
}

//TODO: [review] check fields
func InitPinFS(ctx context.Context, core coreiface.CoreAPI, logger logging.EventLogger) p9.Attacher {
pd := &PinFS{
IPFSBase: IPFSBase{
Path: newRootPath("/ipfs"),
core: core,
Base: Base{
Logger: logger,
Ctx: ctx,
Qid: p9.QID{Type: p9.TypeDir}}}}

pd.Qid.Path = cidToQPath(pd.Path.Cid())
func PinFSAttacher(ctx context.Context, core coreiface.CoreAPI) *PinFS {
pd := &PinFS{IPFSBase: newIPFSBase(ctx, newRootPath("/ipfs"), p9.TypeDir,
core, logging.Logger("PinFS"))}
pd.meta, pd.metaMask = defaultRootAttr()
return pd
}
Expand All @@ -51,7 +43,7 @@ func (pd *PinFS) Walk(names []string) ([]p9.QID, p9.File, error) {
return []p9.QID{pd.Qid}, pd, nil
}

ipfsDir, err := InitIPFS(pd.Ctx, pd.core, pd.Logger).Attach()
ipfsDir, err := IPFSAttacher(pd.Ctx, pd.core).Attach()
if err != nil {
return nil, nil, err
}
Expand Down
Loading

0 comments on commit d32927c

Please sign in to comment.