Skip to content

Commit

Permalink
Add extract helper
Browse files Browse the repository at this point in the history
  • Loading branch information
willscott committed Jun 5, 2024
1 parent 67a5a69 commit 3bdf616
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/car/lib/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ipfs/go-unixfsnode"
"github.com/ipfs/go-unixfsnode/data"
"github.com/ipfs/go-unixfsnode/file"
carstorage "github.com/ipld/go-car/v2/storage"
dagpb "github.com/ipld/go-codec-dagpb"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
Expand All @@ -21,6 +22,30 @@ import (

var ErrNotDir = fmt.Errorf("not a directory")

func ExtractFromFile(c context.Context, carPath string, outputDir string, logger io.Writer) error {
carFile, err := os.Open(carPath)
if err != nil {
return err
}
store, err := carstorage.OpenReadable(carFile)
if err != nil {
return err
}
roots := store.Roots()

ls := cidlink.DefaultLinkSystem()
ls.TrustedStorage = true
ls.SetReadStorage(store)

for _, root := range roots {
_, err = ExtractToDir(c, &ls, root, outputDir, []string{}, false, logger)
if err != nil {
return err
}

Check warning on line 44 in cmd/car/lib/extract.go

View check run for this annotation

Codecov / codecov/patch

cmd/car/lib/extract.go#L25-L44

Added lines #L25 - L44 were not covered by tests
}
return nil

Check warning on line 46 in cmd/car/lib/extract.go

View check run for this annotation

Codecov / codecov/patch

cmd/car/lib/extract.go#L46

Added line #L46 was not covered by tests
}

func ExtractToDir(c context.Context, ls *ipld.LinkSystem, root cid.Cid, outputDir string, path []string, verbose bool, logger io.Writer) (int, error) {
if root.Prefix().Codec == cid.Raw {
if verbose {
Expand Down

0 comments on commit 3bdf616

Please sign in to comment.