-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
33 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
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 |
---|---|---|
@@ -1,46 +1,67 @@ | ||
use std::path::PathBuf; | ||
use std::{fs, path::Path}; | ||
|
||
use anyhow::{Context, Result}; | ||
use anyhow::{Context, Ok, Result}; | ||
|
||
include!(concat!(env!("OUT_DIR"), "/methods.rs")); | ||
|
||
pub fn export_elf(elf_path: &PathBuf) -> Result<()> { | ||
// Create output directory if it doesn't exist | ||
fs::create_dir_all(elf_path).context("Failed to create ELF output directory")?; | ||
/// Exports ELF files to the specified directory. | ||
/// | ||
/// Creates the output directory if it doesn't exist and copies all `.elf` files | ||
/// from guest program directories into it. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `elf_path` - The path to the directory where ELF files will be exported. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns an error if directory creation or file operations fail. | ||
pub fn export_elf<P: AsRef<Path>>(elf_path: P) -> Result<()> { | ||
let elf_path = elf_path.as_ref(); | ||
fs::create_dir_all(elf_path) | ||
.with_context(|| format!("Failed to create ELF output directory {:?}", elf_path))?; | ||
|
||
// Get the current crate's directory | ||
let cargo_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
let sp1_builder_dir = Path::new(env!("CARGO_MANIFEST_DIR")); | ||
|
||
// Iterate over entries in cargo_dir | ||
for entry in fs::read_dir(&cargo_dir) | ||
.with_context(|| format!("Failed to read directory {:?}", cargo_dir))? | ||
for entry in fs::read_dir(sp1_builder_dir) | ||
.with_context(|| format!("Failed to read directory {:?}", sp1_builder_dir))? | ||
{ | ||
let entry = entry?; | ||
let entry = entry.with_context(|| "Failed to read directory entry")?; | ||
let path = entry.path(); | ||
if path.is_dir() { | ||
if let Some(folder_name) = path.file_name().and_then(|n| n.to_str()) { | ||
if folder_name.starts_with("guest-") { | ||
let cache_dir = path.join("cache"); | ||
if cache_dir.is_dir() { | ||
for file in fs::read_dir(&cache_dir).with_context(|| { | ||
format!("Failed to read cache directory {:?}", cache_dir) | ||
})? { | ||
let file = file?; | ||
let file_path = file.path(); | ||
if file_path.is_file() | ||
&& file_path.extension().and_then(|s| s.to_str()) == Some("elf") | ||
{ | ||
let file_name = file_path.file_name().unwrap(); | ||
let dest_file = elf_path.join(file_name); | ||
fs::copy(&file_path, &dest_file).with_context(|| { | ||
format!("Failed to copy {:?} to {:?}", file_path, dest_file) | ||
})?; | ||
} | ||
} | ||
migrate_guest_program(&path, elf_path)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn migrate_guest_program(path: &Path, elf_path: &Path) -> Result<()> { | ||
if path.is_dir() { | ||
if let Some(folder_name) = path.file_name().and_then(|n| n.to_str()) { | ||
if folder_name.starts_with("guest-") { | ||
let cache_dir = path.join("cache"); | ||
if cache_dir.is_dir() { | ||
for file in fs::read_dir(&cache_dir).with_context(|| { | ||
format!("Failed to read cache directory {:?}", cache_dir) | ||
})? { | ||
let file = file.with_context(|| "Failed to read cache file entry")?; | ||
let file_path = file.path(); | ||
migrate_elf(&file_path, elf_path)?; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn migrate_elf(file_path: &Path, elf_path: &Path) -> Result<()> { | ||
if file_path.is_file() && file_path.extension().and_then(|s| s.to_str()) == Some("elf") { | ||
let file_name = file_path | ||
.file_name() | ||
.with_context(|| format!("Failed to get file name from {:?}", file_path))?; | ||
let dest_file = elf_path.join(file_name); | ||
fs::copy(file_path, &dest_file) | ||
.with_context(|| format!("Failed to copy {:?} to {:?}", file_path, dest_file))?; | ||
} | ||
Ok(()) | ||
} |