-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creation of car from file / directory (#246)
* creation of car from file / directory Co-authored-by: Daniel Martí <mvdan@mvdan.cc> Co-authored-by: Rod Vagg <rod@vagg.org>
- Loading branch information
1 parent
d5ab3c5
commit 6e1f4d7
Showing
8 changed files
with
438 additions
and
41 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,120 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"path" | ||
|
||
blocks "github.com/ipfs/go-block-format" | ||
"github.com/ipfs/go-cid" | ||
"github.com/ipfs/go-unixfsnode/data/builder" | ||
"github.com/ipld/go-car/v2" | ||
"github.com/ipld/go-car/v2/blockstore" | ||
dagpb "github.com/ipld/go-codec-dagpb" | ||
"github.com/ipld/go-ipld-prime" | ||
cidlink "github.com/ipld/go-ipld-prime/linking/cid" | ||
"github.com/multiformats/go-multicodec" | ||
"github.com/multiformats/go-multihash" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// CreateCar creates a car | ||
func CreateCar(c *cli.Context) error { | ||
var err error | ||
if c.Args().Len() == 0 { | ||
return fmt.Errorf("a source location to build the car from must be specified") | ||
} | ||
|
||
if !c.IsSet("file") { | ||
return fmt.Errorf("a file destination must be specified") | ||
} | ||
|
||
// 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 { | ||
return err | ||
} | ||
digest := hasher.Sum([]byte{}) | ||
hash, err := multihash.Encode(digest, multihash.SHA2_256) | ||
if err != nil { | ||
return err | ||
} | ||
proxyRoot := cid.NewCidV1(uint64(multicodec.DagPb), hash) | ||
|
||
cdest, err := blockstore.OpenReadWrite(c.String("file"), []cid.Cid{proxyRoot}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Write the unixfs blocks into the store. | ||
root, err := writeFiles(c.Context, cdest, c.Args().Slice()...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := cdest.Finalize(); err != nil { | ||
return err | ||
} | ||
// re-open/finalize with the final root. | ||
return car.ReplaceRootsInFile(c.String("file"), []cid.Cid{root}) | ||
} | ||
|
||
func writeFiles(ctx context.Context, 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) { | ||
cl, ok := l.(cidlink.Link) | ||
if !ok { | ||
return nil, fmt.Errorf("not a cidlink") | ||
} | ||
blk, err := bs.Get(cl.Cid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bytes.NewBuffer(blk.RawData()), nil | ||
} | ||
ls.StorageWriteOpener = func(_ ipld.LinkContext) (io.Writer, ipld.BlockWriteCommitter, error) { | ||
buf := bytes.NewBuffer(nil) | ||
return buf, func(l ipld.Link) error { | ||
cl, ok := l.(cidlink.Link) | ||
if !ok { | ||
return fmt.Errorf("not a cidlink") | ||
} | ||
blk, err := blocks.NewBlockWithCid(buf.Bytes(), cl.Cid) | ||
if err != nil { | ||
return err | ||
} | ||
bs.Put(blk) | ||
return nil | ||
}, nil | ||
} | ||
|
||
topLevel := make([]dagpb.PBLink, 0, len(paths)) | ||
for _, p := range paths { | ||
l, size, err := builder.BuildUnixFSRecursive(p, &ls) | ||
if err != nil { | ||
return cid.Undef, err | ||
} | ||
name := path.Base(p) | ||
entry, err := builder.BuildUnixFSDirectoryEntry(name, int64(size), l) | ||
if err != nil { | ||
return cid.Undef, err | ||
} | ||
topLevel = append(topLevel, entry) | ||
} | ||
|
||
// make a directory for the file(s). | ||
|
||
root, err := builder.BuildUnixFSDirectory(topLevel, &ls) | ||
if err != nil { | ||
return cid.Undef, nil | ||
} | ||
rcl, ok := root.(cidlink.Link) | ||
if !ok { | ||
return cid.Undef, fmt.Errorf("could not interpret %s", root) | ||
} | ||
|
||
return rcl.Cid, 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
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.