diff --git a/core/coreapi/interface/path.go b/core/coreapi/interface/path.go index 05ae8efa219c..38aafda3e8cb 100644 --- a/core/coreapi/interface/path.go +++ b/core/coreapi/interface/path.go @@ -6,13 +6,25 @@ import ( // Path is a generic wrapper for paths used in the API. A path can be resolved // to a CID using one of Resolve functions in the API. -// TODO: figure out/explain namespaces +// +// Paths must be prefixed with a valid prefix: +// +// * /ipfs - Immutable unixfs path (files) +// * /ipld - Immutable ipld path (data) +// * /ipns - Mutable names. Usually resolves to one of the immutable paths +//TODO: /local (MFS) type Path interface { // String returns the path as a string. String() string // Namespace returns the first component of the path Namespace() string + + // Mutable returns false when the content pointed to by this path can + // change. + // + // Note that resolved mutable path can be immutable. + Mutable() bool } // ResolvedPath is a resolved Path diff --git a/core/coreapi/path.go b/core/coreapi/path.go index fd4297e813f8..11aa140b0d83 100644 --- a/core/coreapi/path.go +++ b/core/coreapi/path.go @@ -97,7 +97,10 @@ func (api *CoreAPI) ParsePath(p string) (coreiface.Path, error) { return &path{path: pp}, nil } -func (p *path) String() string { return p.path.String() } +func (p *path) String() string { + return p.path.String() +} + func (p *path) Namespace() string { if len(p.path.Segments()) < 1 { return "" @@ -105,5 +108,15 @@ func (p *path) Namespace() string { return p.path.Segments()[0] } -func (p *resolvedPath) Cid() *cid.Cid { return p.cid } -func (p *resolvedPath) Root() *cid.Cid { return p.root } +func (p *path) Mutable() bool { + //TODO: MFS: check for /local + return p.Namespace() == "ipns" +} + +func (p *resolvedPath) Cid() *cid.Cid { + return p.cid +} + +func (p *resolvedPath) Root() *cid.Cid { + return p.root +} diff --git a/core/coreapi/path_test.go b/core/coreapi/path_test.go new file mode 100644 index 000000000000..7fc5ad5afaab --- /dev/null +++ b/core/coreapi/path_test.go @@ -0,0 +1,34 @@ +package coreapi_test + +import ( + "context" + "strings" + "testing" +) + +func TestMutablePath(t *testing.T) { + ctx := context.Background() + _, api, err := makeAPI(ctx) + if err != nil { + t.Fatal(err) + } + + // get self /ipns path + keys, err := api.Key().List(ctx) + if err != nil { + t.Fatal(err) + } + + if !keys[0].Path().Mutable() { + t.Error("expected self /ipns path to be mutable") + } + + blk, err := api.Block().Put(ctx, strings.NewReader(`foo`)) + if err != nil { + t.Error(err) + } + + if blk.Mutable() { + t.Error("expected /ipld path to be immutable") + } +}