-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Note: depends on ipld/go-ipld-prime#376
- Loading branch information
Showing
8 changed files
with
272 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/ipld/go-car/v2" | ||
"github.com/ipld/go-car/v2/blockstore" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// CatCar will concatinate the blocks from a set of source car files together into a | ||
// combined destination car file. | ||
// The root of the destination car will be the roots of the last specified source car. | ||
func CatCar(c *cli.Context) error { | ||
var err error | ||
if c.Args().Len() == 0 { | ||
return fmt.Errorf("a least one source from must be specified") | ||
} | ||
|
||
if !c.IsSet("file") { | ||
return fmt.Errorf("a file destination must be specified") | ||
} | ||
|
||
options := []car.Option{} | ||
switch c.Int("version") { | ||
case 1: | ||
options = []car.Option{blockstore.WriteAsCarV1(true)} | ||
case 2: | ||
// already the default | ||
default: | ||
return fmt.Errorf("invalid CAR version %d", c.Int("version")) | ||
} | ||
|
||
// peak at final root | ||
lst := c.Args().Get(c.Args().Len() - 1) | ||
lstStore, err := blockstore.OpenReadOnly(lst) | ||
if err != nil { | ||
return err | ||
} | ||
roots, err := lstStore.Roots() | ||
if err != nil { | ||
return err | ||
} | ||
_ = lstStore.Close() | ||
|
||
cdest, err := blockstore.OpenReadWrite(c.String("file"), roots, options...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, src := range c.Args().Slice() { | ||
f, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
blkRdr, err := car.NewBlockReader(f) | ||
if err != nil { | ||
return err | ||
} | ||
blk, err := blkRdr.Next() | ||
for err != io.EOF { | ||
if err := cdest.Put(c.Context, blk); err != nil { | ||
return err | ||
} | ||
blk, err = blkRdr.Next() | ||
if err != nil && err != io.EOF { | ||
return err | ||
} | ||
} | ||
|
||
if err := f.Close(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return cdest.Finalize() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/ipld/go-car/v2" | ||
cidlink "github.com/ipld/go-ipld-prime/linking/cid" | ||
"github.com/ipld/go-ipld-prime/storage/memstore" | ||
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse" | ||
"github.com/multiformats/go-multicodec" | ||
"github.com/multiformats/go-multihash" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// ImportCar will take a file or stream representing a block of data | ||
// and create a car in the specified codec such that the data is packaged | ||
// into a single-block car file. | ||
func ImportCar(c *cli.Context) error { | ||
var err error | ||
inStream := os.Stdin | ||
if c.Args().Len() >= 1 && c.Args().First() != "-" { | ||
inStream, err = os.Open(c.Args().First()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
data, err := io.ReadAll(inStream) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
convertTo := multicodec.Raw | ||
for _, candidate := range multicodec.KnownCodes() { | ||
if candidate.String() == c.String("codec") { | ||
convertTo = candidate | ||
} | ||
} | ||
|
||
proto := cid.Prefix{ | ||
Version: 1, | ||
Codec: uint64(convertTo), | ||
MhType: multihash.SHA2_256, | ||
MhLength: -1, | ||
} | ||
root, err := proto.Sum(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ls := cidlink.DefaultLinkSystem() | ||
store := memstore.Store{} | ||
store.Put(c.Context, string(root.KeyString()), data) | ||
ls.SetReadStorage(&store) | ||
|
||
outStream := os.Stdout | ||
if c.Args().Len() >= 2 { | ||
outStream, err = os.Create(c.Args().Get(1)) | ||
if err != nil { | ||
return err | ||
} | ||
defer outStream.Close() | ||
} | ||
_, err = car.TraverseV1(c.Context, &ls, root, selectorparse.CommonSelector_MatchPoint, outStream) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
stdin filteredroot.txt | ||
car filter ${INPUTS}/sample-wrapped-v2.car out.car | ||
! stderr . | ||
car list out.car | ||
! stderr . | ||
cmp stdout filteredroot.txt | ||
|
||
stdin filteredcids.txt | ||
car filter ${INPUTS}/sample-wrapped-v2.car out-2.car | ||
car list out-2.car | ||
! stderr . | ||
cmp stdout filteredcids.txt | ||
|
||
car concatinate -f combined.car out.car out-2.car | ||
! stderr . | ||
car list combined.car | ||
stdout -count=3 '^bafy' | ||
|
||
-- filteredroot.txt -- | ||
bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy | ||
-- filteredcids.txt -- | ||
bafy2bzacebohz654namrgmwjjx4qmtwgxixsd7pn4tlanyrc3g3hwj75hlxrw | ||
bafy2bzaceaqtiesyfqd2jibmofz22oolguzf5wscwh73rmeypglfu2xhkptri |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
car convert ${INPUTS}/sample-wrapped-v2.car out.car dagjson | ||
! stderr . | ||
car list out.car | ||
stdout -count=1049 '^baguq' |
Oops, something went wrong.