-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
car inspect
command to cmd pkg (#320)
- Loading branch information
Showing
8 changed files
with
301 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"sort" | ||
"strings" | ||
|
||
carv2 "github.com/ipld/go-car/v2" | ||
"github.com/multiformats/go-multicodec" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// InspectCar verifies a CAR and prints a basic report about its contents | ||
func InspectCar(c *cli.Context) (err error) { | ||
inStream := os.Stdin | ||
if c.Args().Len() >= 1 { | ||
inStream, err = os.Open(c.Args().First()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
rd, err := carv2.NewReader(inStream) | ||
if err != nil { | ||
return err | ||
} | ||
stats, err := rd.Inspect(c.IsSet("full")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var v2s string | ||
if stats.Version == 2 { | ||
idx := "(none)" | ||
if stats.IndexCodec != 0 { | ||
idx = stats.IndexCodec.String() | ||
} | ||
var buf bytes.Buffer | ||
stats.Header.Characteristics.WriteTo(&buf) | ||
v2s = fmt.Sprintf(`Characteristics: %x | ||
Data offset: %d | ||
Data (payload) length: %d | ||
Index offset: %d | ||
Index type: %s | ||
`, buf.Bytes(), stats.Header.DataOffset, stats.Header.DataSize, stats.Header.IndexOffset, idx) | ||
} | ||
|
||
var roots strings.Builder | ||
switch len(stats.Roots) { | ||
case 0: | ||
roots.WriteString(" (none)") | ||
case 1: | ||
roots.WriteString(" ") | ||
roots.WriteString(stats.Roots[0].String()) | ||
default: | ||
for _, r := range stats.Roots { | ||
roots.WriteString("\n\t") | ||
roots.WriteString(r.String()) | ||
} | ||
} | ||
|
||
var codecs strings.Builder | ||
{ | ||
keys := make([]int, len(stats.CodecCounts)) | ||
i := 0 | ||
for codec := range stats.CodecCounts { | ||
keys[i] = int(codec) | ||
i++ | ||
} | ||
sort.Ints(keys) | ||
for _, code := range keys { | ||
codec := multicodec.Code(code) | ||
codecs.WriteString(fmt.Sprintf("\n\t%s: %d", codec, stats.CodecCounts[codec])) | ||
} | ||
} | ||
|
||
var hashers strings.Builder | ||
{ | ||
keys := make([]int, len(stats.MhTypeCounts)) | ||
i := 0 | ||
for codec := range stats.MhTypeCounts { | ||
keys[i] = int(codec) | ||
i++ | ||
} | ||
sort.Ints(keys) | ||
for _, code := range keys { | ||
codec := multicodec.Code(code) | ||
hashers.WriteString(fmt.Sprintf("\n\t%s: %d", codec, stats.MhTypeCounts[codec])) | ||
} | ||
} | ||
|
||
rp := "No" | ||
if stats.RootsPresent { | ||
rp = "Yes" | ||
} | ||
|
||
pfmt := `Version: %d | ||
%sRoots:%s | ||
Root blocks present in data: %s | ||
Block count: %d | ||
Min / average / max block length (bytes): %d / %d / %d | ||
Min / average / max CID length (bytes): %d / %d / %d | ||
Block count per codec:%s | ||
CID count per multihash:%s | ||
` | ||
|
||
fmt.Printf( | ||
pfmt, | ||
stats.Version, | ||
v2s, | ||
roots.String(), | ||
rp, | ||
stats.BlockCount, | ||
stats.MinBlockLength, | ||
stats.AvgBlockLength, | ||
stats.MaxBlockLength, | ||
stats.MinCidLength, | ||
stats.AvgCidLength, | ||
stats.MaxCidLength, | ||
codecs.String(), | ||
hashers.String(), | ||
) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
����olLʔ<#oKg#H���*���gversion |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
car inspect ${INPUTS}/sample-v1.car | ||
cmp stdout v1inspect.txt | ||
|
||
car inspect ${INPUTS}/sample-wrapped-v2.car | ||
cmp stdout v2inspect.txt | ||
|
||
! car inspect ${INPUTS}/badheaderlength.car | ||
stderr 'invalid header data' | ||
|
||
! car inspect ${INPUTS}/badsectionlength.car | ||
stderr 'invalid section data' | ||
|
||
-- v1inspect.txt -- | ||
Version: 1 | ||
Roots: bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy | ||
Root blocks present in data: Yes | ||
Block count: 1049 | ||
Min / average / max block length (bytes): 1 / 417 / 1342 | ||
Min / average / max CID length (bytes): 14 / 37 / 38 | ||
Block count per codec: | ||
raw: 6 | ||
dag-cbor: 1043 | ||
CID count per multihash: | ||
identity: 6 | ||
blake2b-256: 1043 | ||
-- v2inspect.txt -- | ||
Version: 2 | ||
Characteristics: 00000000000000000000000000000000 | ||
Data offset: 51 | ||
Data (payload) length: 479907 | ||
Index offset: 479958 | ||
Index type: car-multihash-index-sorted | ||
Roots: bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy | ||
Root blocks present in data: Yes | ||
Block count: 1049 | ||
Min / average / max block length (bytes): 1 / 417 / 1342 | ||
Min / average / max CID length (bytes): 14 / 37 / 38 | ||
Block count per codec: | ||
raw: 6 | ||
dag-cbor: 1043 | ||
CID count per multihash: | ||
identity: 6 | ||
blake2b-256: 1043 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.