Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd/car): add '--no-wrap' option to 'create' command #432

Merged
merged 1 commit into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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