Skip to content

Commit

Permalink
Allow extracton of a raw unixfs file (#284)
Browse files Browse the repository at this point in the history
* Allow extracton of a raw unixfs file

Co-authored-by: Daniel Martí <[email protected]>
  • Loading branch information
willscott and mvdan authored Feb 15, 2022
1 parent d1093d9 commit 4cb3d4f
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions cmd/car/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"errors"
"fmt"
"io"
"os"
Expand All @@ -20,6 +21,8 @@ import (
"github.com/urfave/cli/v2"
)

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

// ExtractCar pulls files and directories out of a car
func ExtractCar(c *cli.Context) error {
outputDir, err := os.Getwd()
Expand Down Expand Up @@ -92,7 +95,27 @@ func extractRoot(c *cli.Context, ls *ipld.LinkSystem, root cid.Cid, outputDir st
}
}
if err := extractDir(c, ls, ufn, outputResolvedDir, "/"); err != nil {
return fmt.Errorf("%s: %w", root, err)
if !errors.Is(err, ErrNotDir) {
return fmt.Errorf("%s: %w", root, err)
}
ufsData, err := pbnode.LookupByString("Data")
if err != nil {
return err
}
ufsBytes, err := ufsData.AsBytes()
if err != nil {
return err
}
ufsNode, err := data.DecodeUnixFSData(ufsBytes)
if err != nil {
return err
}
if ufsNode.DataType.Int() == data.Data_File || ufsNode.DataType.Int() == data.Data_Raw {
if err := extractFile(c, ls, pbnode, filepath.Join(outputResolvedDir, "unknown")); err != nil {
return err
}
}
return nil
}

return nil
Expand Down Expand Up @@ -208,7 +231,7 @@ func extractDir(c *cli.Context, ls *ipld.LinkSystem, n ipld.Node, outputRoot, ou
}
return nil
}
return fmt.Errorf("not a directory")
return ErrNotDir
}

func extractFile(c *cli.Context, ls *ipld.LinkSystem, n ipld.Node, outputName string) error {
Expand Down

0 comments on commit 4cb3d4f

Please sign in to comment.