@cryptotaxi247 / kubo / commits / 18fec6eb5

coreapi unixfs: wrap option

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Oct 3, 2018 at 22:30 UTC 18fec6eb5f4ec73654baa26903c34e38c0866bcc
3 files changed +76 -16
core/coreapi/interface/options/unixfs.go
+13
@@ -31,6 +31,8 @@ type UnixfsAddSettings struct {
31 Pin bool
32 OnlyHash bool
33 Local bool
34 +
35 + Wrap bool
36 }
37
38 type UnixfsAddOption func(*UnixfsAddSettings) error
@@ -51,6 +53,8 @@ func UnixfsAddOptions(opts ...UnixfsAddOption) (*UnixfsAddSettings, cid.Prefix,
53 Pin: false,
54 OnlyHash: false,
55 Local: false,
56 +
57 + Wrap: false,
58 }
59
60 for _, opt := range opts {
@@ -202,3 +206,12 @@ func (unixfsOpts) Local(local bool) UnixfsAddOption {
206 return nil
207 }
208 }
209 +
210 +// Wrap tells the adder to wrap the added file structure with an additional
211 +// directory.
212 +func (unixfsOpts) Wrap(wrap bool) UnixfsAddOption {
213 + return func(settings *UnixfsAddSettings) error {
214 + settings.Wrap = wrap
215 + return nil
216 + }
217 +}
core/coreapi/unixfs.go
+1 -1
@@ -66,7 +66,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options
66 fileAdder.Chunker = settings.Chunker
67 //fileAdder.Progress = progress
68 //fileAdder.Hidden = hidden
69 - //fileAdder.Wrap = wrap
69 + fileAdder.Wrap = settings.Wrap
70 fileAdder.Pin = settings.Pin && !settings.OnlyHash
71 fileAdder.Silent = true
72 fileAdder.RawLeaves = settings.RawLeaves
core/coreapi/unixfs_test.go
+62 -15
@@ -134,6 +134,18 @@ func strFile(data string) func() files.File {
134 }
135 }
136
137 +func twoLevelDir() func() files.File {
138 + return func() files.File {
139 + return files.NewSliceFile("t", "t", []files.File{
140 + files.NewSliceFile("t/abc", "t/abc", []files.File{
141 + files.NewReaderFile("t/abc/def", "t/abc/def", ioutil.NopCloser(strings.NewReader("world")), nil),
142 + }),
143 + files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
144 + files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil),
145 + })
146 + }
147 +}
148 +
149 func TestAdd(t *testing.T) {
150 ctx := context.Background()
151 _, api, err := makeAPI(ctx)
@@ -142,12 +154,16 @@ func TestAdd(t *testing.T) {
154 }
155
156 cases := []struct {
145 - name string
146 - data func() files.File
147 - path string
148 - err string
157 + name string
158 + data func() files.File
159 +
160 + path string
161 + err string
162 +
163 recursive bool
150 - opts []options.UnixfsAddOption
164 + wrapped bool
165 +
166 + opts []options.UnixfsAddOption
167 }{
168 // Simple cases
169 {
@@ -232,6 +248,12 @@ func TestAdd(t *testing.T) {
248 path: hello,
249 opts: []options.UnixfsAddOption{options.Unixfs.Local(true)},
250 },
251 + {
252 + name: "hashOnly", // test (non)fetchability
253 + data: strFile(helloStr),
254 + path: hello,
255 + opts: []options.UnixfsAddOption{options.Unixfs.HashOnly(true)},
256 + },
257 // multi file
258 {
259 name: "simpleDir",
@@ -245,18 +267,36 @@ func TestAdd(t *testing.T) {
267 path: "/ipfs/QmRKGpFfR32FVXdvJiHfo4WJ5TDYBsM1P9raAp1p6APWSp",
268 },
269 {
248 - name: "twoLevelDir",
270 + name: "twoLevelDir",
271 + data: twoLevelDir(),
272 + recursive: true,
273 + path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
274 + },
275 + // wrapped
276 + {
277 + name: "addWrapped",
278 + path: "/ipfs/QmVE9rNpj5doj7XHzp5zMUxD7BJgXEqx4pe3xZ3JBReWHE",
279 data: func() files.File {
250 - return files.NewSliceFile("t", "t", []files.File{
251 - files.NewSliceFile("t/abc", "t/abc", []files.File{
252 - files.NewReaderFile("t/abc/def", "t/abc/def", ioutil.NopCloser(strings.NewReader("world")), nil),
253 - }),
254 - files.NewReaderFile("t/bar", "t/bar", ioutil.NopCloser(strings.NewReader("hello2")), nil),
255 - files.NewReaderFile("t/foo", "t/foo", ioutil.NopCloser(strings.NewReader("hello1")), nil),
256 - })
280 + return files.NewReaderFile("foo", "foo", ioutil.NopCloser(strings.NewReader(helloStr)), nil)
281 },
282 + wrapped: true,
283 + opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
284 + },
285 + {
286 + name: "twoLevelDirWrapped",
287 + data: twoLevelDir(),
288 recursive: true,
259 - path: "/ipfs/QmVG2ZYCkV1S4TK8URA3a4RupBF17A8yAr4FqsRDXVJASr",
289 + wrapped: true,
290 + path: "/ipfs/QmPwsL3T5sWhDmmAWZHAzyjKtMVDS9a11aHNRqb3xoVnmg",
291 + opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
292 + },
293 + {
294 + name: "twoLevelInlineHash",
295 + data: twoLevelDir(),
296 + recursive: true,
297 + wrapped: true,
298 + path: "/ipfs/zBunoruKoyCHKkALNSWxDvj4L7yuQnMgQ4hUa9j1Z64tVcDEcu6Zdetyu7eeFCxMPfxb7YJvHeFHoFoHMkBUQf6vfdhmi",
299 + opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.Inline(true), options.Unixfs.RawLeaves(true), options.Unixfs.Hash(mh.SHA3)},
300 },
301 }
302
@@ -338,7 +378,14 @@ func TestAdd(t *testing.T) {
378 t.Fatal(err)
379 }
380
341 - cmpFile(testCase.data(), f)
381 + orig := testCase.data()
382 + if testCase.wrapped {
383 + orig = files.NewSliceFile("", "", []files.File{
384 + orig,
385 + })
386 + }
387 +
388 + cmpFile(orig, f)
389 })
390 }
391 }