Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ pub enum Command {
/// Construct a CAR file bytes from a list of blocks
#[cfg(feature = "ipld")]
CarFromBlocks(CarFromBlocksArgs),
/// Deconstruct a CAR into its constituent blocks
#[cfg(feature = "ipld")]
CarToBlocks(CarToBlocksArgs),

// ---------------- Libp2p Tools ----------------------------//
/// Ping a peer
Expand Down Expand Up @@ -285,6 +288,13 @@ pub struct CarFromBlocksArgs {
pub blocks: Vec<CarBlockValue>,
}

#[derive(Args, Debug, Clone)]
pub struct CarToBlocksArgs {
/// Path to save the extracted blocks (default to current directory)
#[arg(default_value = ".")]
pub blocks_dir: String,
}

#[derive(Debug, Clone)]
pub struct CarBlockValue {
pub root: bool,
Expand Down
13 changes: 11 additions & 2 deletions src/ipld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};

use crate::{
cli::{
CarExtractArgs, CarFromBlocksArgs, CarInspectArgs, CidFromDataArgs, CidInspectArgs,
Command, DagCborIndexArgs,
CarExtractArgs, CarFromBlocksArgs, CarInspectArgs, CarToBlocksArgs, CidFromDataArgs,
CidInspectArgs, Command, DagCborIndexArgs,
},
random_cid,
};
Expand All @@ -33,6 +33,7 @@ pub enum Operation {
CarInspect(CarInspectArgs),
CarExtract(CarExtractArgs),
CarFromBlocks(CarFromBlocksArgs),
CarToBlocks(CarToBlocksArgs),
}

impl TryFrom<Command> for Operation {
Expand All @@ -53,6 +54,7 @@ impl TryFrom<Command> for Operation {
Command::CarInspect(args) => Ok(Operation::CarInspect(args)),
Command::CarExtract(args) => Ok(Operation::CarExtract(args)),
Command::CarFromBlocks(args) => Ok(Operation::CarFromBlocks(args)),
Command::CarToBlocks(args) => Ok(Operation::CarToBlocks(args)),
_ => Err(value),
}
}
Expand Down Expand Up @@ -231,6 +233,13 @@ pub async fn run(

stdout.write_all(&car).await?;
}
Operation::CarToBlocks(args) => {
let mut reader = CarReader::new(stdin).await?;
while let Some((cid, data)) = reader.next_block().await? {
let path = format!("{}/{}", args.blocks_dir, cid);
tokio::fs::write(path, data).await?;
}
}
};
Ok(())
}
Expand Down