-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from buffalojoec/dev
API Refactoring & More
- Loading branch information
Showing
13 changed files
with
1,010 additions
and
316 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
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,83 @@ | ||
//! Module for loading files from local filesystem. | ||
//! | ||
//! When compiling a Solana program wih `cargo build-sbf`, the environment | ||
//! variable `SBF_OUT_DIR` defaults to `target/deploy` and the compiled program | ||
//! ELF file is written to `target/deploy/program_name.so`. | ||
//! | ||
//! Note: The legacy/deprecated `cargo build-bpf` command used `BPF_OUT_DIR` | ||
//! instead of `SBF_OUT_DIR`. | ||
//! | ||
//! As a result, the default search paths for ELF files are: | ||
//! | ||
//! * `tests/fixtures` | ||
//! * `BPF_OUT_DIR` | ||
//! * `SBF_OUT_DIR` | ||
//! * The current working directory | ||
//! | ||
//! Since these functions are intended for the local filesystem and for testing | ||
//! purposes, most of them will panic if the file is not found or if there is an | ||
//! error reading the file. | ||
|
||
use std::{ | ||
fs::File, | ||
io::Read, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
fn default_shared_object_dirs() -> Vec<PathBuf> { | ||
let mut search_path = vec![PathBuf::from("tests/fixtures")]; | ||
|
||
if let Ok(bpf_out_dir) = std::env::var("BPF_OUT_DIR") { | ||
search_path.push(PathBuf::from(bpf_out_dir)); | ||
} | ||
|
||
if let Ok(bpf_out_dir) = std::env::var("SBF_OUT_DIR") { | ||
search_path.push(PathBuf::from(bpf_out_dir)); | ||
} | ||
|
||
if let Ok(dir) = std::env::current_dir() { | ||
search_path.push(dir); | ||
} | ||
|
||
search_path | ||
} | ||
|
||
fn find_file(filename: &str) -> Option<PathBuf> { | ||
for dir in default_shared_object_dirs() { | ||
let candidate = dir.join(filename); | ||
if candidate.exists() { | ||
return Some(candidate); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Read the contents of a file into a `Vec<u8>`. | ||
pub fn read_file<P: AsRef<Path>>(path: P) -> Vec<u8> { | ||
let path = path.as_ref(); | ||
let mut file = File::open(path) | ||
.unwrap_or_else(|err| panic!("Failed to open \"{}\": {}", path.display(), err)); | ||
|
||
let mut file_data = Vec::new(); | ||
file.read_to_end(&mut file_data) | ||
.unwrap_or_else(|err| panic!("Failed to read \"{}\": {}", path.display(), err)); | ||
file_data | ||
} | ||
|
||
/// Load a program ELF file from the local filesystem by program name. | ||
/// | ||
/// The program ELF file is expected to be located in one of the default search | ||
/// paths: | ||
/// | ||
/// * `tests/fixtures` | ||
/// * `BPF_OUT_DIR` | ||
/// * `SBF_OUT_DIR` | ||
/// * The current working directory | ||
/// | ||
/// The name of the program ELF file is expected to be `{program_name}.so`. | ||
pub fn load_program_elf(program_name: &str) -> Vec<u8> { | ||
let file_name = format!("{program_name}.so"); | ||
let program_file = find_file(&file_name) | ||
.unwrap_or_else(|| panic!("Program file data not available for \"{}\"", file_name,)); | ||
read_file(program_file) | ||
} |
Oops, something went wrong.