-
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.
* better name for detaching index command * add list and filter commands
- Loading branch information
Showing
4 changed files
with
175 additions
and
6 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
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,98 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/ipfs/go-cid" | ||
carv2 "github.com/ipld/go-car/v2" | ||
"github.com/ipld/go-car/v2/blockstore" | ||
icarv1 "github.com/ipld/go-car/v2/internal/carv1" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// FilterCar is a command to select a subset of a car by CID. | ||
func FilterCar(c *cli.Context) error { | ||
r, err := carv2.OpenReader(c.Args().Get(0)) | ||
if err != nil { | ||
return err | ||
} | ||
defer r.Close() | ||
|
||
if c.Args().Len() < 2 { | ||
return fmt.Errorf("an output filename must be provided") | ||
} | ||
roots, err := r.Roots() | ||
if err != nil { | ||
return err | ||
} | ||
bs, err := blockstore.OpenReadWrite(c.Args().Get(1), roots) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Get the set of CIDs from stdin. | ||
inStream := os.Stdin | ||
if c.IsSet("cidFile") { | ||
inStream, err = os.Open(c.String("cidFile")) | ||
if err != nil { | ||
return err | ||
} | ||
defer inStream.Close() | ||
} | ||
cidMap, err := parseCIDS(inStream) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("filtering to %d cids\n", len(cidMap)) | ||
|
||
rd, err := icarv1.NewCarReader(r.DataReader()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
blk, err := rd.Next() | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return err | ||
} | ||
if _, ok := cidMap[blk.Cid()]; ok { | ||
if err := bs.Put(blk); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return bs.Finalize() | ||
} | ||
|
||
func parseCIDS(r io.Reader) (map[cid.Cid]struct{}, error) { | ||
cids := make(map[cid.Cid]struct{}) | ||
br := bufio.NewReader(r) | ||
for { | ||
line, _, err := br.ReadLine() | ||
if err != nil { | ||
if err == io.EOF { | ||
return cids, nil | ||
} | ||
return nil, err | ||
} | ||
trimLine := strings.TrimSpace(string(line)) | ||
if len(trimLine) == 0 { | ||
continue | ||
} | ||
c, err := cid.Parse(trimLine) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if _, ok := cids[c]; ok { | ||
fmt.Fprintf(os.Stderr, "duplicate cid: %s\n", c) | ||
} | ||
cids[c] = struct{}{} | ||
} | ||
} |
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,53 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
carv2 "github.com/ipld/go-car/v2" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// ListCar is a command to output the cids in a car. | ||
func ListCar(c *cli.Context) error { | ||
inStream := os.Stdin | ||
var err error | ||
if c.Args().Len() >= 1 { | ||
inStream, err = os.Open(c.Args().First()) | ||
if err != nil { | ||
return err | ||
} | ||
defer inStream.Close() | ||
} | ||
rd, err := carv2.NewBlockReader(inStream) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outStream := os.Stdout | ||
if c.Args().Len() >= 2 { | ||
outStream, err = os.Create(c.Args().Get(1)) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
defer outStream.Close() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
blk, err := rd.Next() | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
return err | ||
} | ||
fmt.Fprintf(outStream, "%s\n", blk.Cid()) | ||
} | ||
|
||
return err | ||
} |