From 18fec6eb5f4ec73654baa26903c34e38c0866bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 3 Oct 2018 22:30:45 +0200 Subject: [PATCH] coreapi unixfs: wrap option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/coreapi/interface/options/unixfs.go | 13 ++++ core/coreapi/unixfs.go | 2 +- core/coreapi/unixfs_test.go | 79 +++++++++++++++++++----- 3 files changed, 77 insertions(+), 17 deletions(-) diff --git a/core/coreapi/interface/options/unixfs.go b/core/coreapi/interface/options/unixfs.go index df6f4fc71..abbea9681 100644 --- a/core/coreapi/interface/options/unixfs.go +++ b/core/coreapi/interface/options/unixfs.go @@ -31,6 +31,8 @@ type UnixfsAddSettings struct { Pin bool OnlyHash bool Local bool + + Wrap bool } type UnixfsAddOption func(*UnixfsAddSettings) error @@ -51,6 +53,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix, Pin: false, OnlyHash: false, Local: false, + + Wrap: false, } for _, opt := range opts { @@ -202,3 +206,12 @@ func (unixfsOpts) Local(local bool) UnixfsAddOption { return nil } } + +// Wrap tells the adder to wrap the added file structure with an additional +// directory. +func (unixfsOpts) Wrap(wrap bool) UnixfsAddOption { + return func(settings *UnixfsAddSettings) error { + settings.Wrap = wrap + return nil + } +} diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index 80f59d278..650ecda03 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -66,7 +66,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options fileAdder.Chunker = settings.Chunker //fileAdder.Progress = progress //fileAdder.Hidden = hidden - //fileAdder.Wrap = wrap + fileAdder.Wrap = settings.Wrap fileAdder.Pin = settings.Pin && !settings.OnlyHash fileAdder.Silent = true fileAdder.RawLeaves = settings.RawLeaves diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index c446032c0..30b1e571e 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -134,6 +134,18 @@ func strFile(data string) func() files.File { } } +func twoLevelDir() func() files.File { + return func() files.File { + return files.NewSliceFile("t", "t", []files.File{ + files.NewSliceFile("t/abc", "t/abc", []files.File{ + files.NewReaderFile("t/abc/def", "t/abc/def", ioutil.NopCloser(strings.NewReader("world")), nil), + }), + files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil), + files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil), + }) + } +} + func TestAdd(t *testing.T) { ctx := context.Background() _, api, err := makeAPI(ctx) @@ -142,12 +154,16 @@ func TestAdd(t *testing.T) { } cases := []struct { - name string - data func() files.File - path string - err string + name string + data func() files.File + + path string + err string + recursive bool - opts []options.UnixfsAddOption + wrapped bool + + opts []options.UnixfsAddOption }{ // Simple cases { @@ -232,6 +248,12 @@ func TestAdd(t *testing.T) { path: hello, opts: []options.UnixfsAddOption{options.Unixfs.Local(true)}, }, + { + name: "hashOnly", // test (non)fetchability + data: strFile(helloStr), + path: hello, + opts: []options.UnixfsAddOption{options.Unixfs.HashOnly(true)}, + }, // multi file { name: "simpleDir", @@ -245,19 +267,37 @@ func TestAdd(t *testing.T) { path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp", }, { - name: "twoLevelDir", - data: func() files.File { - return files.NewSliceFile("t", "t", []files.File{ - files.NewSliceFile("t/abc", "t/abc", []files.File{ - files.NewReaderFile("t/abc/def", "t/abc/def", ioutil.NopCloser(strings.NewReader("world")), nil), - }), - files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil), - files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil), - }) - }, + name: "twoLevelDir", + data: twoLevelDir(), recursive: true, path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr", }, + // wrapped + { + name: "addWrapped", + path: "/ipfs/QmVE9rNpj5doj7XHzp5zMUxD7BJgXEqx4pe3xZ3JBReWHE", + data: func() files.File { + return files.NewReaderFile("foo", "foo", ioutil.NopCloser(strings.NewReader(helloStr)), nil) + }, + wrapped: true, + opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)}, + }, + { + name: "twoLevelDirWrapped", + data: twoLevelDir(), + recursive: true, + wrapped: true, + path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg", + opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)}, + }, + { + name: "twoLevelInlineHash", + data: twoLevelDir(), + recursive: true, + wrapped: true, + path: "/ipfs/zBunoruKoyCHKkALNSWxDvj4L7yuQnMgQ4hUa9j1Z64tVcDEcu6Zdetyu7eeFCxMPfxb7YJvHeFHoFoHMkBUQf6vfdhmi", + opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true), options.Unixfs.Hash(mh.SHA3)}, + }, } for _, testCase := range cases { @@ -338,7 +378,14 @@ func TestAdd(t *testing.T) { t.Fatal(err) } - cmpFile(testCase.data(), f) + orig := testCase.data() + if testCase.wrapped { + orig = files.NewSliceFile("", "", []files.File{ + orig, + }) + } + + cmpFile(orig, f) }) } }