From 3e754d64fbd9770a1e09af3dfb492f599b7fe21f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 3 Oct 2018 22:49:42 +0200 Subject: [PATCH] coreapi unixfs: hidden opiton 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 | 14 ++++- core/coreapi/unixfs.go | 2 +- core/coreapi/unixfs_test.go | 67 ++++++++++++++++++++---- 3 files changed, 69 insertions(+), 14 deletions(-) diff --git a/core/coreapi/interface/options/unixfs.go b/core/coreapi/interface/options/unixfs.go index abbea9681..7d7af5b81 100644 --- a/core/coreapi/interface/options/unixfs.go +++ b/core/coreapi/interface/options/unixfs.go @@ -32,7 +32,8 @@ type UnixfsAddSettings struct { OnlyHash bool Local bool - Wrap bool + Wrap bool + Hidden bool } type UnixfsAddOption func(*UnixfsAddSettings) error @@ -54,7 +55,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix, OnlyHash: false, Local: false, - Wrap: false, + Wrap: false, + Hidden: false, } for _, opt := range opts { @@ -215,3 +217,11 @@ func (unixfsOpts) Wrap(wrap bool) UnixfsAddOption { return nil } } + +// Hidden enables adding of hidden files (files prefixed with '.') +func (unixfsOpts) Hidden(hidden bool) UnixfsAddOption { + return func(settings *UnixfsAddSettings) error { + settings.Hidden = hidden + return nil + } +} diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index 650ecda03..7917d18f2 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -65,7 +65,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options fileAdder.Chunker = settings.Chunker //fileAdder.Progress = progress - //fileAdder.Hidden = hidden + fileAdder.Hidden = settings.Hidden fileAdder.Wrap = settings.Wrap fileAdder.Pin = settings.Pin && !settings.OnlyHash fileAdder.Silent = true diff --git a/core/coreapi/unixfs_test.go b/core/coreapi/unixfs_test.go index 30b1e571e..14577eae0 100644 --- a/core/coreapi/unixfs_test.go +++ b/core/coreapi/unixfs_test.go @@ -146,6 +146,12 @@ func twoLevelDir() func() files.File { } } +func wrapped(f files.File) files.File { + return files.NewSliceFile("", "", []files.File{ + f, + }) +} + func TestAdd(t *testing.T) { ctx := context.Background() _, api, err := makeAPI(ctx) @@ -154,14 +160,14 @@ func TestAdd(t *testing.T) { } cases := []struct { - name string - data func() files.File + name string + data func() files.File + expect func(files.File) files.File path string err string recursive bool - wrapped bool opts []options.UnixfsAddOption }{ @@ -279,14 +285,14 @@ func TestAdd(t *testing.T) { data: func() files.File { return files.NewReaderFile("foo", "foo", ioutil.NopCloser(strings.NewReader(helloStr)), nil) }, - wrapped: true, - opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)}, + expect: wrapped, + opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)}, }, { name: "twoLevelDirWrapped", data: twoLevelDir(), recursive: true, - wrapped: true, + expect: wrapped, path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg", opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)}, }, @@ -294,10 +300,51 @@ func TestAdd(t *testing.T) { name: "twoLevelInlineHash", data: twoLevelDir(), recursive: true, - wrapped: true, + expect: wrapped, path: "/ipfs/zBunoruKoyCHKkALNSWxDvj4L7yuQnMgQ4hUa9j1Z64tVcDEcu6Zdetyu7eeFCxMPfxb7YJvHeFHoFoHMkBUQf6vfdhmi", opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true), options.Unixfs.Hash(mh.SHA3)}, }, + // hidden + { + name: "hiddenFiles", + data: func() files.File { + return files.NewSliceFile("t", "t", []files.File{ + files.NewReaderFile("t/.bar", "t/.bar", ioutil.NopCloser(strings.NewReader("hello2")), 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), + }) + }, + recursive: true, + path: "/ipfs/QmehGvpf2hY196MzDFmjL8Wy27S4jbgGDUAhBJyvXAwr3g", + opts: []options.UnixfsAddOption{options.Unixfs.Hidden(true)}, + }, + { + name: "hiddenFileAlwaysAdded", + data: func() files.File { + return files.NewReaderFile(".foo", ".foo", ioutil.NopCloser(strings.NewReader(helloStr)), nil) + }, + recursive: true, + path: hello, + }, + { + name: "hiddenFilesNotAdded", + data: func() files.File { + return files.NewSliceFile("t", "t", []files.File{ + files.NewReaderFile("t/.bar", "t/.bar", ioutil.NopCloser(strings.NewReader("hello2")), 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), + }) + }, + expect: func(files.File) files.File { + return files.NewSliceFile("t", "t", []files.File{ + files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil), + files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil), + }) + }, + recursive: true, + path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp", + opts: []options.UnixfsAddOption{options.Unixfs.Hidden(false)}, + }, } for _, testCase := range cases { @@ -379,10 +426,8 @@ func TestAdd(t *testing.T) { } orig := testCase.data() - if testCase.wrapped { - orig = files.NewSliceFile("", "", []files.File{ - orig, - }) + if testCase.expect != nil { + orig = testCase.expect(orig) } cmpFile(orig, f)