-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
creation of car from file / directory #246
Merged
Merged
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f6ad38b
creation of car from file / directory
willscott 7ab4e12
add initial test script, stop vet errors temporarily
mvdan 971593c
better `car list -v` that describes codec types and contents of dag-p…
willscott 0d13a47
Merge branch 'master' of github.com:ipld/go-car into unixfscreate
willscott 834d9f0
use recursive creator
willscott dce708a
Merge branch 'unixfscreate' of github.com:ipld/go-car into unixfscreate
willscott cf07163
remove accidental test fixture
willscott 02a511d
add `car list --unixfs file.car` that outputs like `ls`
willscott 221c782
minor bug fixes
willscott b31ae01
working build
willscott 1b7db53
fix test
willscott 21211a0
alias output
willscott b7e7527
update to master unixfsnode
willscott 6efadbb
tidy
willscott 4cbd883
a bit more performance of not re-hashing on car operations
willscott 28f1c80
fix: make `get` match non-pointer cidlinks
rvagg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,119 @@ | ||
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.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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-o --output
would be mildly more famililar here and consistent withipfs-car
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i was trying to follow the flags from
tar
, i'll add both as equivalent aliases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, this is for creating a car not extracting. Ignore me. As you were.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tho, on second thoughts it does still feel more like an
--output
kind of a deal here. ipfs-car is using--output
for "where to write the car to" when creating and "where to unpack the files to" when extracting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Déjà vu! Oli and I had identical argument regarding ipfs-car, I said be like
tar
, Oli saystar
is terrible, don't copy it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uh huh. "if you know how
tar
works then you know howcar
works" sounds compelling, and given our erudite nieche may be the right choice for the immediate audience.My experience has been that
tar
is used as an example of a command that folks stuggle to remember what the flags do, so I aimed elsewhere. But we can have both.ipfs-car
is notcar
and won't try to be, and folks can use the flavour that works for them.