@cryptotaxi247 / kubo / commits / b85ecb848

files2.0: Use new type helpers

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

Łukasz Magiera committed Dec 3, 2018 at 21:24 UTC b85ecb848ae5ac07759d9ebc8745d68fd1ac8c4f
12 files changed +63 -69
cmd/ipfs/init.go
+3 -2
@@ -19,6 +19,7 @@ import (
19 "gx/ipfs/QmYyzmMnhNTtoXx5ttgUaRdHHckYnQWjPL98hgLAR2QLDD/go-ipfs-config"
20 "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
21 "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
22 + "gx/ipfs/QmeaQRmnRog7NxLEWHP9zSTkics4cbgwBVa7q49LmBowDr/go-ipfs-files"
23 )
24
25 const (
@@ -89,12 +90,12 @@ environment variable:
90 if !it.Next() && it.Err() != nil {
91 return it.Err()
92 }
92 - if it.File() == nil {
93 + if files.FileFrom(it) == nil {
94 return fmt.Errorf("expected a regular file")
95 }
96
97 conf = &config.Config{}
97 - if err := json.NewDecoder(it.File()).Decode(conf); err != nil {
98 + if err := json.NewDecoder(files.FileFrom(it)).Decode(conf); err != nil {
99 return err
100 }
101 }
core/commands/block.go
+3 -2
@@ -13,6 +13,7 @@ import (
13
14 cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
15 cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
16 + files "gx/ipfs/QmeaQRmnRog7NxLEWHP9zSTkics4cbgwBVa7q49LmBowDr/go-ipfs-files"
17 mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
18 )
19
@@ -157,7 +158,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
158 if !it.Next() && it.Err() != nil {
159 return it.Err()
160 }
160 - if it.File() == nil {
161 + if files.FileFrom(it) == nil {
162 return fmt.Errorf("expected a regular file")
163 }
164
@@ -181,7 +182,7 @@ than 'sha2-256' or format to anything other than 'v0' will result in CIDv1.
182 }
183 }
184
184 - p, err := api.Block().Put(req.Context, it.File(), options.Block.Hash(mhtval, mhlen), options.Block.Format(format))
185 + p, err := api.Block().Put(req.Context, files.FileFrom(it), options.Block.Hash(mhtval, mhlen), options.Block.Format(format))
186 if err != nil {
187 return err
188 }
core/commands/config.go
+3 -2
@@ -18,6 +18,7 @@ import (
18 config "gx/ipfs/QmYyzmMnhNTtoXx5ttgUaRdHHckYnQWjPL98hgLAR2QLDD/go-ipfs-config"
19 cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
20 cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
21 + files "gx/ipfs/QmeaQRmnRog7NxLEWHP9zSTkics4cbgwBVa7q49LmBowDr/go-ipfs-files"
22 )
23
24 // ConfigUpdateOutput is config profile apply command's output
@@ -284,10 +285,10 @@ can't be undone.
285 if !it.Next() && it.Err() != nil {
286 return it.Err()
287 }
287 - if it.File() == nil {
288 + if files.FileFrom(it) == nil {
289 return fmt.Errorf("expected a regular file")
290 }
290 - file := it.File()
291 + file := files.FileFrom(it)
292 defer file.Close()
293
294 return replaceConfig(r, file)
core/commands/dag/dag.go
+3 -2
@@ -14,6 +14,7 @@ import (
14 cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
15 ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
16 cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
17 + files "gx/ipfs/QmeaQRmnRog7NxLEWHP9zSTkics4cbgwBVa7q49LmBowDr/go-ipfs-files"
18 mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
19 )
20
@@ -94,10 +95,10 @@ into an object of the specified format.
95
96 it := req.Files.Entries()
97 for it.Next() {
97 - if it.File() == nil {
98 + if files.FileFrom(it) == nil {
99 return fmt.Errorf("expected a regular file")
100 }
100 - nds, err := coredag.ParseInputs(ienc, format, it.File(), mhType, -1)
101 + nds, err := coredag.ParseInputs(ienc, format, files.FileFrom(it), mhType, -1)
102 if err != nil {
103 return err
104 }
core/commands/files.go
+3 -2
@@ -25,6 +25,7 @@ import (
25 logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
26 dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
27 cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
28 + files "gx/ipfs/QmeaQRmnRog7NxLEWHP9zSTkics4cbgwBVa7q49LmBowDr/go-ipfs-files"
29 mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
30 )
31
@@ -773,11 +774,11 @@ stat' on the file or any of its ancestors.
774 if !it.Next() && it.Err() != nil {
775 return it.Err()
776 }
776 - if it.File() == nil {
777 + if files.FileFrom(it) == nil {
778 return fmt.Errorf("expected a regular file")
779 }
780
780 - var r io.Reader = it.File()
781 + var r io.Reader = files.FileFrom(it)
782 if countfound {
783 r = io.LimitReader(r, int64(count))
784 }
core/commands/object/object.go
+3 -2
@@ -17,6 +17,7 @@ import (
17 ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
18 dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
19 cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
20 + files "gx/ipfs/QmeaQRmnRog7NxLEWHP9zSTkics4cbgwBVa7q49LmBowDr/go-ipfs-files"
21 )
22
23 type Node struct {
@@ -395,7 +396,7 @@ And then run:
396 if !it.Next() && it.Err() != nil {
397 return it.Err()
398 }
398 - if it.File() == nil {
399 + if files.FileFrom(it) == nil {
400 return fmt.Errorf("expected a regular file")
401 }
402
@@ -414,7 +415,7 @@ And then run:
415 return err
416 }
417
417 - p, err := api.Object().Put(req.Context, it.File(),
418 + p, err := api.Object().Put(req.Context, files.FileFrom(it),
419 options.Object.DataType(datafieldenc),
420 options.Object.InputEnc(inputenc),
421 options.Object.Pin(dopin))
core/commands/object/patch.go
+5 -4
@@ -10,6 +10,7 @@ import (
10
11 "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
12 "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
13 + "gx/ipfs/QmeaQRmnRog7NxLEWHP9zSTkics4cbgwBVa7q49LmBowDr/go-ipfs-files"
14 )
15
16 var ObjectPatchCmd = &cmds.Command{
@@ -64,11 +65,11 @@ the limit will not be respected by the network.
65 if !it.Next() && it.Err() != nil {
66 return it.Err()
67 }
67 - if it.File() == nil {
68 + if files.FileFrom(it) == nil {
69 return fmt.Errorf("expected a regular file")
70 }
71
71 - p, err := api.Object().AppendData(req.Context, root, it.File())
72 + p, err := api.Object().AppendData(req.Context, root, files.FileFrom(it))
73 if err != nil {
74 return err
75 }
@@ -114,11 +115,11 @@ Example:
115 if !it.Next() && it.Err() != nil {
116 return it.Err()
117 }
117 - if it.File() == nil {
118 + if files.FileFrom(it) == nil {
119 return fmt.Errorf("expected a regular file")
120 }
121
121 - p, err := api.Object().SetData(req.Context, root, it.File())
122 + p, err := api.Object().SetData(req.Context, root, files.FileFrom(it))
123 if err != nil {
124 return err
125 }
core/commands/tar.go
+3 -2
@@ -13,6 +13,7 @@ import (
13 cmds "gx/ipfs/QmaAP56JAwdjwisPTu4yx17whcjTr6y5JCSCF77Y1rahWV/go-ipfs-cmds"
14 dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
15 cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
16 + files "gx/ipfs/QmeaQRmnRog7NxLEWHP9zSTkics4cbgwBVa7q49LmBowDr/go-ipfs-files"
17 )
18
19 var TarCmd = &cmds.Command{
@@ -48,11 +49,11 @@ represent it.
49 if !it.Next() && it.Err() != nil {
50 return it.Err()
51 }
51 - if it.File() == nil {
52 + if files.FileFrom(it) == nil {
53 return fmt.Errorf("expected a regular file")
54 }
55
55 - node, err := tar.ImportTar(req.Context, it.File(), nd.DAG)
56 + node, err := tar.ImportTar(req.Context, files.FileFrom(it), nd.DAG)
57 if err != nil {
58 return err
59 }
core/coreapi/name_test.go
+1 -2
@@ -4,7 +4,6 @@ import (
4 "context"
5 "github.com/ipfs/go-ipfs/core"
6 "io"
7 - "io/ioutil"
7 "math/rand"
8 "path"
9 "testing"
@@ -20,7 +19,7 @@ import (
19 var rnd = rand.New(rand.NewSource(0x62796532303137))
20
21 func addTestObject(ctx context.Context, api coreiface.CoreAPI) (coreiface.Path, error) {
23 - return api.Unixfs().Add(ctx, files.NewReaderFile(ioutil.NopCloser(&io.LimitedReader{R: rnd, N: 4092}), nil))
22 + return api.Unixfs().Add(ctx, files.FileFrom(&io.LimitedReader{R: rnd, N: 4092}))
23 }
24
25 func appendPath(p coreiface.Path, sub string) coreiface.Path {
core/coreapi/unixfile.go
-10
@@ -42,16 +42,6 @@ func (it *ufsIterator) Node() files.Node {
42 return it.curFile
43 }
44
45 -func (it *ufsIterator) File() files.File {
46 - f, _ := it.curFile.(files.File)
47 - return f
48 -}
49 -
50 -func (it *ufsIterator) Dir() files.Directory {
51 - d, _ := it.curFile.(files.Directory)
52 - return d
53 -}
54 -
45 func (it *ufsIterator) Next() bool {
46 l, ok := <-it.files
47 if !ok {
core/coreapi/unixfs_test.go
+29 -30
@@ -136,34 +136,34 @@ func makeAPI(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
136
137 func strFile(data string) func() files.Node {
138 return func() files.Node {
139 - return files.NewReaderFile(ioutil.NopCloser(strings.NewReader(data)), nil)
139 + return files.FileFrom([]byte(data))
140 }
141 }
142
143 func twoLevelDir() func() files.Node {
144 return func() files.Node {
145 - return files.NewSliceFile([]files.DirEntry{
146 - files.FileEntry("abc", files.NewSliceFile([]files.DirEntry{
147 - files.FileEntry("def", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("world")), nil)),
148 - })),
145 + return files.DirFrom(map[string]files.Node{
146 + "abc": files.DirFrom(map[string]files.Node{
147 + "def": files.FileFrom([]byte("world")),
148 + }),
149
150 - files.FileEntry("bar", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("hello2")), nil)),
151 - files.FileEntry("foo", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("hello1")), nil)),
150 + "bar": files.FileFrom([]byte("hello2")),
151 + "foo": files.FileFrom([]byte("hello1")),
152 })
153 }
154 }
155
156 func flatDir() files.Node {
157 - return files.NewSliceFile([]files.DirEntry{
158 - files.FileEntry("bar", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("hello2")), nil)),
159 - files.FileEntry("foo", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("hello1")), nil)),
157 + return files.DirFrom(map[string]files.Node{
158 + "bar": files.FileFrom([]byte("hello2")),
159 + "foo": files.FileFrom([]byte("hello1")),
160 })
161 }
162
163 func wrapped(name string) func(f files.Node) files.Node {
164 return func(f files.Node) files.Node {
165 - return files.NewSliceFile([]files.DirEntry{
166 - files.FileEntry(name, f),
165 + return files.DirFrom(map[string]files.Node{
166 + name: f,
167 })
168 }
169 }
@@ -296,7 +296,7 @@ func TestAdd(t *testing.T) {
296 name: "addWrapped",
297 path: "/ipfs/QmVE9rNpj5doj7XHzp5zMUxD7BJgXEqx4pe3xZ3JBReWHE",
298 data: func() files.Node {
299 - return files.NewReaderFile(ioutil.NopCloser(strings.NewReader(helloStr)), nil)
299 + return files.FileFrom([]byte(helloStr))
300 },
301 wrap: "foo",
302 expect: wrapped("foo"),
@@ -306,7 +306,7 @@ func TestAdd(t *testing.T) {
306 name: "addNotWrappedDirFile",
307 path: hello,
308 data: func() files.Node {
309 - return files.NewReaderFile(ioutil.NopCloser(strings.NewReader(helloStr)), nil)
309 + return files.FileFrom([]byte(helloStr))
310 },
311 wrap: "foo",
312 },
@@ -314,11 +314,11 @@ func TestAdd(t *testing.T) {
314 name: "stdinWrapped",
315 path: "/ipfs/QmU3r81oZycjHS9oaSHw37ootMFuFUw1DvMLKXPsezdtqU",
316 data: func() files.Node {
317 - return files.NewReaderFile(ioutil.NopCloser(strings.NewReader(helloStr)), nil)
317 + return files.FileFrom([]byte(helloStr))
318 },
319 expect: func(files.Node) files.Node {
320 - return files.NewSliceFile([]files.DirEntry{
321 - files.FileEntry("QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk", files.NewReaderFile(ioutil.NopCloser(strings.NewReader(helloStr)), nil)),
320 + return files.DirFrom(map[string]files.Node{
321 + "QmQy2Dw4Wk7rdJKjThjYXzfFJNaRKRHhHP5gHHXroJMYxk": files.FileFrom([]byte(helloStr)),
322 })
323 },
324 opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true)},
@@ -335,8 +335,8 @@ func TestAdd(t *testing.T) {
335 return rf
336 },
337 expect: func(files.Node) files.Node {
338 - return files.NewSliceFile([]files.DirEntry{
339 - files.FileEntry("test", files.NewReaderFile(ioutil.NopCloser(strings.NewReader(helloStr)), nil)),
338 + return files.DirFrom(map[string]files.Node{
339 + "test": files.FileFrom([]byte(helloStr)),
340 })
341 },
342 opts: []options.UnixfsAddOption{options.Unixfs.Wrap(true), options.Unixfs.StdinName("test")},
@@ -361,10 +361,10 @@ func TestAdd(t *testing.T) {
361 {
362 name: "hiddenFiles",
363 data: func() files.Node {
364 - return files.NewSliceFile([]files.DirEntry{
365 - files.FileEntry(".bar", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("hello2")), nil)),
366 - files.FileEntry("bar", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("hello2")), nil)),
367 - files.FileEntry("foo", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("hello1")), nil)),
364 + return files.DirFrom(map[string]files.Node{
365 + ".bar": files.FileFrom([]byte("hello2")),
366 + "bar": files.FileFrom([]byte("hello2")),
367 + "foo": files.FileFrom([]byte("hello1")),
368 })
369 },
370 wrap: "t",
@@ -374,7 +374,7 @@ func TestAdd(t *testing.T) {
374 {
375 name: "hiddenFileAlwaysAdded",
376 data: func() files.Node {
377 - return files.NewReaderFile(ioutil.NopCloser(strings.NewReader(helloStr)), nil)
377 + return files.FileFrom([]byte(helloStr))
378 },
379 wrap: ".foo",
380 path: hello,
@@ -382,10 +382,10 @@ func TestAdd(t *testing.T) {
382 {
383 name: "hiddenFilesNotAdded",
384 data: func() files.Node {
385 - return files.NewSliceFile([]files.DirEntry{
386 - files.FileEntry(".bar", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("hello2")), nil)),
387 - files.FileEntry("bar", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("hello2")), nil)),
388 - files.FileEntry("foo", files.NewReaderFile(ioutil.NopCloser(strings.NewReader("hello1")), nil)),
385 + return files.DirFrom(map[string]files.Node{
386 + ".bar": files.FileFrom([]byte("hello2")),
387 + "bar": files.FileFrom([]byte("hello2")),
388 + "foo": files.FileFrom([]byte("hello1")),
389 })
390 },
391 expect: func(files.Node) files.Node {
@@ -432,8 +432,7 @@ func TestAdd(t *testing.T) {
432 {
433 name: "progress1M",
434 data: func() files.Node {
435 - r := bytes.NewReader(bytes.Repeat([]byte{0}, 1000000))
436 - return files.NewReaderFile(ioutil.NopCloser(r), nil)
435 + return files.FileFrom(bytes.NewReader(bytes.Repeat([]byte{0}, 1000000)))
436 },
437 path: "/ipfs/QmXXNNbwe4zzpdMg62ZXvnX1oU7MwSrQ3vAEtuwFKCm1oD",
438 events: []coreiface.AddEvent{
core/coreunix/add_test.go
+7 -9
@@ -71,20 +71,18 @@ func TestAddGCLive(t *testing.T) {
71 }
72 adder.Out = out
73
74 - dataa := ioutil.NopCloser(bytes.NewBufferString("testfileA"))
75 - rfa := files.NewReaderFile(dataa, nil)
74 + rfa := files.FileFrom([]byte("testfileA"))
75
76 // make two files with pipes so we can 'pause' the add for timing of the test
77 piper, pipew := io.Pipe()
79 - hangfile := files.NewReaderFile(piper, nil)
78 + hangfile := files.FileFrom(piper)
79
81 - datad := ioutil.NopCloser(bytes.NewBufferString("testfileD"))
82 - rfd := files.NewReaderFile(datad, nil)
80 + rfd := files.FileFrom([]byte("testfileD"))
81
84 - slf := files.NewSliceFile([]files.DirEntry{
85 - files.FileEntry("a", rfa),
86 - files.FileEntry("b", hangfile),
87 - files.FileEntry("d", rfd),
82 + slf := files.DirFrom(map[string]files.Node{
83 + "a": rfa,
84 + "b": hangfile,
85 + "d": rfd,
86 })
87
88 addDone := make(chan struct{})