Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit 95220b3

Browse files
committed
WIP
1 parent d1e2792 commit 95220b3

File tree

15 files changed

+1204
-240
lines changed

15 files changed

+1204
-240
lines changed

filesystem/client.go

Lines changed: 0 additions & 70 deletions
This file was deleted.

filesystem/default/default.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package ipfs
2+
3+
import (
4+
"context"
5+
"time"
6+
coreiface "github.com/ipfs/interface-go-ipfs-core"
7+
8+
9+
"github.com/ipfs/interface-go-ipfs-core/filesystem/node/soft"
10+
"github.com/ipfs/go-ipfs-http-client"
11+
)
12+
13+
type defaultFs struct {
14+
// index
15+
PathParserRegistry
16+
17+
//defaultFS specific
18+
ctx context.Context
19+
epoch time.Time
20+
21+
//IPFS parser specifics
22+
//ipfsNode core.IpfsNode
23+
core coreiface.CoreAPI
24+
}
25+
/* From DRAFT
26+
func NewDefaultFileSystem(parentCtx context.Context) (FileSystem, error) {
27+
// something like this
28+
fsCtx := deriveFrom(parentCtx)
29+
// go onCancel(fsCtx) { callClosers() } ()
30+
return &PathParserRegistry{fsCtx}, nil
31+
}
32+
*/
33+
34+
func newDefaultFs() (FileSystem, error) {
35+
root := defaultFs {
36+
ctx:context.TODO(), // cancelable something or other
37+
epoch:time.Now(),
38+
}
39+
40+
// we depend on data from the coreapi to initalize our API nodes
41+
// so fetch it or something and store it on the FS
42+
//daemon := fallbackApi()
43+
//ctx := deriveCtx(daemon.ctx) // if the daemon cancels, so should we
44+
45+
//root.ipfsNode = daemon
46+
47+
// mount base subsystems
48+
epoch := time.Now()
49+
50+
// TODO: connect to daemon or fallback [new constructor]
51+
core, err := httpapi.NewLocalApi()
52+
if err != nil {
53+
return nil, err
54+
}
55+
for _, pair := range [...]struct {
56+
string
57+
ParseFn
58+
}{
59+
{"/", fsnode.RootParser(epoch)},
60+
{"/ipfs", inode.PinParser(core.Pin(), epoch)}, // requests for "/ipfs" are directed at pinRootParser(ctx, requestString)
61+
{"/ipfs/", coreAPIParser}, // all requests beneath "/ipfs/" are directed at coreAPIParser(ctx, requestString)
62+
{"/ipns", keyRootParser},
63+
{"/ipns/", nameAPIParser},
64+
{filesRootPrefix, filesAPIParser},
65+
} {
66+
closer, err := root.Register(pair.string, pair.ParseFn)
67+
if err != nil {
68+
if err == errRegistered {
69+
//TODO: [discuss] consider having Plan9 style unions; Mount() would require flags (union contents go to: front, back, replace)
70+
// doing this complicates our io.Closer consturction, but may be worth having
71+
return nil, fmt.Errorf("%q is already registered", pair.string)
72+
}
73+
return nil, fmt.Errorf("cannot register %q: %s", pair.string, err)
74+
}
75+
76+
// store these somewhere important and call them before you exit
77+
// this will release the namespace at the FS level
78+
root.closers = append(root.closers, closer) // in our example we do nothing with them :^)
79+
}
80+
}
81+
82+
type DescriptorTable interface {
83+
}
84+
85+
func newDescriptorTable() {
86+
}
87+

filesystem/index.go renamed to filesystem/default/index.go

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,40 @@
1-
package fs
1+
package ipfs
22

33
import (
44
"context"
5+
"encoding/binary"
56
"io"
67
"os"
78
"strings"
89
"sync"
910

11+
"github.com/ipfs/go-cid"
1012
"github.com/ipfs/go-unixfs"
13+
fs "github.com/ipfs/interface-go-ipfs-core/filesystem/interface"
14+
"github.com/multiformats/go-multihash"
1115
)
1216

13-
// This file represents a partial and theoretical
14-
// pkg-level implementation of a filesystem{}
15-
16-
var (
17-
pkgRoot FileSystem
18-
closers []io.Closer
19-
)
20-
21-
func init() {
22-
23-
// we depend on data from the coreapi to initalize our API nodes
24-
// so fetch it or something and store it on the FS
25-
daemon := fallbackApi()
26-
ctx := deriveCtx(daemon.ctx) // if the daemon cancels, so should we
27-
28-
pkgRoot = NewFileSystem(ctx)
29-
for _, pair := range [...]struct {
30-
string
31-
ParseFn
32-
}{
33-
{"/", rootParser},
34-
{"/ipfs", pinRootParser}, // requests for "/ipfs" are directed at pinRootParser(ctx, requestString)
35-
{"/ipfs/", coreAPIParser}, // all requests beneath "/ipfs/" are directed at coreAPIParser(ctx, requestString)
36-
{"/ipns", keyRootParser},
37-
{"/ipns/", nameAPIParser},
38-
{filesRootPrefix, filesAPIParser},
39-
} {
40-
closer, err := pkgRoot.Register(pair.string, pair.ParseFn)
41-
if err != nil {
42-
if err == errRegistered {
43-
panic(fmt.Sprtinf("%q is already registered", pair.string))
44-
}
45-
panic(fmt.Sprtinf("cannot register %q: %s", pair.string, err))
46-
}
47-
48-
// store these somewhere important and call them before you exit
49-
// this will release the namespace at the FS level
50-
closers = append(closers, closer) // in our example we do nothing with them :^)
51-
}
17+
func Lookup(ctx context.Context, name string) (fs.FsNode, error) {
18+
return pkgRoot.Lookup(ctx, name)
5219
}
5320

54-
func NewDefaultFileSystem(parentCtx context.Context) (FileSystem, error) {
55-
// something like this
56-
fsCtx := deriveFrom(parentCtx)
57-
// go onCancel(fsCtx) { callClosers() } ()
58-
return &PathParserRegistry{fsCtx}, nil
21+
// api's may define Metadata.Cid() however they like
22+
// for the default, we use this QID-like generator
23+
func GenQueryID(path string, md fs.Metadata) (cid.Cid, error) {
24+
mdBuf := make([]byte, 16)
25+
binary.LittleEndian.PutUint64(mdBuf, uint64(md.Size()))
26+
binary.LittleEndian.PutUint64(mdBuf[8:], uint64(md.Type()))
27+
28+
prefix := cid.V1Builder{Codec: cid.DagCBOR, MhType: multihash.BLAKE2B_MIN}
29+
return prefix.Sum(append([]byte(path), mdBuf...))
5930
}
6031

6132
type PathParserRegistry struct {
6233
sync.Mutex
63-
ctx context.Context
64-
nodeParsers map[string]ParseFn
34+
nodeParsers map[string]fs.ParseFn
6535
}
6636

67-
func (rr *PathParserRegistry) Register(subrootPath string, nodeParser ParseFn) (io.Closer, error) {
37+
func (rr *PathParserRegistry) Mount(subrootPath string, nodeParser fs.ParseFn) (io.Closer, error) {
6838
rr.Lock()
6939
val, ok := rr.nodeParsers[subrootPath]
7040
if ok || val != nil {
@@ -74,12 +44,14 @@ func (rr *PathParserRegistry) Register(subrootPath string, nodeParser ParseFn) (
7444
rr.nodeParsers[subrootPath] = nodeParsers
7545
return func() {
7646
ri.Lock()
47+
//TODO: somehow trigger cascading close for open subsystem handles
48+
// or note that open handles are still valid, but new handles will not be made
7749
delete(ri.subSystem, subrootPath)
7850
ri.Unlock()
7951
}, nil
8052
}
8153

82-
func (PathParserRegistry) Lookup(ctx context.Context, name string) (FsPath, error) {
54+
func (PathParserRegistry) Lookup(ctx context.Context, name string) (FsNode, error) {
8355
//NOTE: we can use a pkg level cache here, and fallback to the parser only when necessary
8456

8557
/* very simple map lookup
@@ -119,13 +91,7 @@ func Unlock() {
11991
pkgRoot.Unlock()
12092
}
12193

122-
func Lookup(ctx context.Context, name string) (FsPath, error) {
123-
return pkgRoot.Lookup(ctx, name)
124-
}
125-
126-
// ...
127-
128-
func OpenFile(name string, flags flags, perm os.FileMode) (FsFile, error) {
94+
func OpenFile(name string, flags OFlags, perm os.FileMode) (FsFile, error) {
12995
fs.Lock()
13096
defer fs.Unlock()
13197

filesystem/default/init.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ipfs
2+
3+
import "github.com/ipfs/interface-go-ipfs-core/filesystem/interface"
4+
5+
//TODO: store these in the daemon/ipfs-node scope, or elsewhere
6+
// have something extract the FS from the daemon (`core.fsFrom(IpfsNode)``)
7+
var pkgRoot fs.FileSystem
8+
9+
func init() {
10+
pkgRoot, err = newDefaultFs()
11+
if err != nil {
12+
panic(err)
13+
}
14+
}

0 commit comments

Comments
 (0)