Skip to content

Commit

Permalink
Merge pull request #432 from hacdias/no-wrap
Browse files Browse the repository at this point in the history
feat(cmd/car): add '--no-wrap' option to 'create' command
  • Loading branch information
willscott authored Jun 3, 2023
2 parents 3f39582 + c9fd84c commit 67cf6ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmd/car/car.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 13 additions & 2 deletions cmd/car/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 67cf6ad

Please sign in to comment.