From c9fd84cbfd545772129fd6c9a97efe87fc5cc278 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 3 Jun 2023 07:58:43 +0200 Subject: [PATCH] feat(cmd/car): add '--no-wrap' option to 'create' command --- cmd/car/car.go | 4 ++++ cmd/car/create.go | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/car/car.go b/cmd/car/car.go index d2356484..d8ee0ce8 100644 --- a/cmd/car/car.go +++ b/cmd/car/car.go @@ -40,6 +40,10 @@ func main1() int { Usage: "The car file to write to", TakesFile: true, }, + &cli.BoolFlag{ + Name: "no-wrap", + Usage: "Do not wrap the files in a directory", + }, &cli.IntFlag{ Name: "version", Value: 2, diff --git a/cmd/car/create.go b/cmd/car/create.go index 3a76c913..596c3f81 100644 --- a/cmd/car/create.go +++ b/cmd/car/create.go @@ -31,6 +31,10 @@ func CreateCar(c *cli.Context) error { return fmt.Errorf("a file destination must be specified") } + if c.Bool("no-wrap") && c.Args().Len() > 1 { + return fmt.Errorf("no-wrap cannot be set with multiple source locations") + } + // make a cid with the right length that we eventually will patch with the root. hasher, err := multihash.GetHasher(multihash.SHA2_256) if err != nil { @@ -59,7 +63,7 @@ func CreateCar(c *cli.Context) error { } // Write the unixfs blocks into the store. - root, err := writeFiles(c.Context, cdest, c.Args().Slice()...) + root, err := writeFiles(c.Context, c.Bool("no-wrap"), cdest, c.Args().Slice()...) if err != nil { return err } @@ -71,7 +75,7 @@ func CreateCar(c *cli.Context) error { return car.ReplaceRootsInFile(c.String("file"), []cid.Cid{root}) } -func writeFiles(ctx context.Context, bs *blockstore.ReadWrite, paths ...string) (cid.Cid, error) { +func writeFiles(ctx context.Context, noWrap bool, bs *blockstore.ReadWrite, paths ...string) (cid.Cid, error) { ls := cidlink.DefaultLinkSystem() ls.TrustedStorage = true ls.StorageReadOpener = func(_ ipld.LinkContext, l ipld.Link) (io.Reader, error) { @@ -107,6 +111,13 @@ func writeFiles(ctx context.Context, bs *blockstore.ReadWrite, paths ...string) if err != nil { return cid.Undef, err } + if noWrap { + rcl, ok := l.(cidlink.Link) + if !ok { + return cid.Undef, fmt.Errorf("could not interpret %s", l) + } + return rcl.Cid, nil + } name := path.Base(p) entry, err := builder.BuildUnixFSDirectoryEntry(name, int64(size), l) if err != nil {