1
- package fs
1
+ package ipfs
2
2
3
3
import (
4
4
"context"
5
+ "encoding/binary"
5
6
"io"
6
7
"os"
7
8
"strings"
8
9
"sync"
9
10
11
+ "github.com/ipfs/go-cid"
10
12
"github.com/ipfs/go-unixfs"
13
+ fs "github.com/ipfs/interface-go-ipfs-core/filesystem/interface"
14
+ "github.com/multiformats/go-multihash"
11
15
)
12
16
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 )
52
19
}
53
20
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 ... ))
59
30
}
60
31
61
32
type PathParserRegistry struct {
62
33
sync.Mutex
63
- ctx context.Context
64
- nodeParsers map [string ]ParseFn
34
+ nodeParsers map [string ]fs.ParseFn
65
35
}
66
36
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 ) {
68
38
rr .Lock ()
69
39
val , ok := rr .nodeParsers [subrootPath ]
70
40
if ok || val != nil {
@@ -74,12 +44,14 @@ func (rr *PathParserRegistry) Register(subrootPath string, nodeParser ParseFn) (
74
44
rr .nodeParsers [subrootPath ] = nodeParsers
75
45
return func () {
76
46
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
77
49
delete (ri .subSystem , subrootPath )
78
50
ri .Unlock ()
79
51
}, nil
80
52
}
81
53
82
- func (PathParserRegistry ) Lookup (ctx context.Context , name string ) (FsPath , error ) {
54
+ func (PathParserRegistry ) Lookup (ctx context.Context , name string ) (FsNode , error ) {
83
55
//NOTE: we can use a pkg level cache here, and fallback to the parser only when necessary
84
56
85
57
/* very simple map lookup
@@ -119,13 +91,7 @@ func Unlock() {
119
91
pkgRoot .Unlock ()
120
92
}
121
93
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 ) {
129
95
fs .Lock ()
130
96
defer fs .Unlock ()
131
97
0 commit comments