Skip to content

Commit

Permalink
Avoid shell out tar for genesis archive creation (#3079)
Browse files Browse the repository at this point in the history
Use the tar crate instead; doing so avoids spawning a subprocess and
also removes the variability of relying on whatever tar the caller has
in their path
  • Loading branch information
steviez authored Oct 11, 2024
1 parent 7cf3d1d commit 8f0465f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ assert_matches = { workspace = true }
bincode = { workspace = true }
bitflags = { workspace = true, features = ["serde"] }
byteorder = { workspace = true }
bzip2 = { workspace = true }
chrono = { workspace = true, features = ["default", "serde"] }
chrono-humanize = { workspace = true }
crossbeam-channel = { workspace = true }
Expand Down Expand Up @@ -73,6 +74,7 @@ spl-token-2022 = { workspace = true, features = ["no-entrypoint"] }
static_assertions = { workspace = true }
strum = { workspace = true, features = ["derive"] }
strum_macros = { workspace = true }
tar = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"] }
Expand Down
49 changes: 22 additions & 27 deletions ledger/src/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use {
},
convert::TryInto,
fmt::Write,
fs,
fs::{self, File},
io::{Error as IoError, ErrorKind},
ops::Bound,
path::{Path, PathBuf},
Expand All @@ -81,6 +81,7 @@ use {
Arc, Mutex, RwLock,
},
},
tar,
tempfile::{Builder, TempDir},
thiserror::Error,
trees::{Tree, TreeWalk},
Expand Down Expand Up @@ -4835,32 +4836,12 @@ pub fn create_new_ledger(
drop(blockstore);

let archive_path = ledger_path.join(DEFAULT_GENESIS_ARCHIVE);
let args = vec![
"jcfhS",
archive_path.to_str().unwrap(),
"-C",
ledger_path.to_str().unwrap(),
DEFAULT_GENESIS_FILE,
blockstore_dir,
];
let output = std::process::Command::new("tar")
.env("COPYFILE_DISABLE", "1")
.args(args)
.output()
.unwrap();
if !output.status.success() {
use std::str::from_utf8;
error!("tar stdout: {}", from_utf8(&output.stdout).unwrap_or("?"));
error!("tar stderr: {}", from_utf8(&output.stderr).unwrap_or("?"));

return Err(BlockstoreError::Io(IoError::new(
ErrorKind::Other,
format!(
"Error trying to generate snapshot archive: {}",
output.status
),
)));
}
let archive_file = File::create(&archive_path)?;
let encoder = bzip2::write::BzEncoder::new(archive_file, bzip2::Compression::best());
let mut archive = tar::Builder::new(encoder);
archive.append_path_with_name(ledger_path.join(DEFAULT_GENESIS_FILE), DEFAULT_GENESIS_FILE)?;
archive.append_dir_all(blockstore_dir, ledger_path.join(blockstore_dir))?;
archive.into_inner()?;

// ensure the genesis archive can be unpacked and it is under
// max_genesis_archive_unpacked_size, immediately after creating it above.
Expand Down Expand Up @@ -5352,6 +5333,9 @@ pub mod tests {
crossbeam_channel::unbounded,
rand::{seq::SliceRandom, thread_rng},
solana_account_decoder::parse_token::UiTokenAmount,
solana_accounts_db::hardened_unpack::{
open_genesis_config, MAX_GENESIS_ARCHIVE_UNPACKED_SIZE,
},
solana_entry::entry::{next_entry, next_entry_mut},
solana_runtime::bank::{Bank, RewardType},
solana_sdk::{
Expand Down Expand Up @@ -5422,6 +5406,17 @@ pub mod tests {
assert!(Path::new(ledger_path.path())
.join(BLOCKSTORE_DIRECTORY_ROCKS_LEVEL)
.exists());

assert_eq!(
genesis_config,
open_genesis_config(ledger_path.path(), MAX_GENESIS_ARCHIVE_UNPACKED_SIZE).unwrap()
);
// Remove DEFAULT_GENESIS_FILE to force extraction of DEFAULT_GENESIS_ARCHIVE
std::fs::remove_file(ledger_path.path().join(DEFAULT_GENESIS_FILE)).unwrap();
assert_eq!(
genesis_config,
open_genesis_config(ledger_path.path(), MAX_GENESIS_ARCHIVE_UNPACKED_SIZE).unwrap()
);
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8f0465f

Please sign in to comment.