Skip to content

Commit a1bc164

Browse files
committed
[Rust] Refactor FileMetadata file information
- Rename and retype `FileMetadata::filename` and make the assignment required to happen at time of construction. - Add `FileMetadata::display_name` which is only to be used for presentation purposes. - Add `FileMetadata::virtual_path` for containers. - Rename `FileMetadata::modified` to `FileMetadata::is_modified` to be more consistent across codebase. - Add some missing documentation. - Add `BinaryView::from_metadata` with accompanying documentation.
1 parent 25d78c1 commit a1bc164

File tree

11 files changed

+158
-40
lines changed

11 files changed

+158
-40
lines changed

plugins/dwarf/dwarf_import/src/helpers.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::ffi::OsStr;
16-
use std::path::{Path, PathBuf};
15+
use std::path::PathBuf;
1716
use std::{str::FromStr, sync::mpsc};
1817

1918
use crate::{DebugInfoBuilderContext, ReaderType};
@@ -590,10 +589,8 @@ pub(crate) fn find_sibling_debug_file(view: &BinaryView) -> Option<String> {
590589
return None;
591590
}
592591

593-
let full_file_path = view.file().filename().to_string();
594-
595-
let debug_file = PathBuf::from(format!("{}.debug", full_file_path));
596-
let dsym_folder = PathBuf::from(format!("{}.dSYM", full_file_path));
592+
let debug_file = view.file().file_path().with_extension("debug");
593+
let dsym_folder = view.file().file_path().with_extension("dSYM");
597594

598595
// Find sibling debug file
599596
if debug_file.exists() && debug_file.is_file() {
@@ -624,13 +621,12 @@ pub(crate) fn find_sibling_debug_file(view: &BinaryView) -> Option<String> {
624621
// Look for dSYM
625622
// TODO: look for dSYM in project
626623
if dsym_folder.exists() && dsym_folder.is_dir() {
627-
let filename = Path::new(&full_file_path)
628-
.file_name()
629-
.unwrap_or(OsStr::new(""));
630-
631-
let dsym_file = dsym_folder.join("Contents/Resources/DWARF/").join(filename); // TODO: should this just pull any file out? Can there be multiple files?
632-
if dsym_file.exists() {
633-
return Some(dsym_file.to_string_lossy().to_string());
624+
if let Some(filename) = view.file().file_path().file_name() {
625+
// TODO: should this just pull any file out? Can there be multiple files?
626+
let dsym_file = dsym_folder.join("Contents/Resources/DWARF/").join(filename);
627+
if dsym_file.exists() {
628+
return Some(dsym_file.to_string_lossy().to_string());
629+
}
634630
}
635631
}
636632

plugins/idb_import/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ impl CustomDebugInfoParser for IDBDebugInfoParser {
2727
project_file.name().as_str().ends_with(".i64")
2828
|| project_file.name().as_str().ends_with(".idb")
2929
} else {
30-
view.file().filename().as_str().ends_with(".i64")
31-
|| view.file().filename().as_str().ends_with(".idb")
30+
view.file()
31+
.file_path()
32+
.extension()
33+
.map_or(false, |ext| ext == "i64" || ext == "idb")
3234
}
3335
}
3436

@@ -55,7 +57,10 @@ impl CustomDebugInfoParser for TILDebugInfoParser {
5557
if let Some(project_file) = view.file().project_file() {
5658
project_file.name().as_str().ends_with(".til")
5759
} else {
58-
view.file().filename().as_str().ends_with(".til")
60+
view.file()
61+
.file_path()
62+
.extension()
63+
.map_or(false, |ext| ext == "til")
5964
}
6065
}
6166

plugins/pdb-ng/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ impl CustomDebugInfoParser for PDBParser {
617617
}
618618

619619
// Try in the same directory as the file
620-
let mut potential_path = PathBuf::from(view.file().filename().to_string());
620+
let mut potential_path = view.file().file_path();
621621
potential_path.pop();
622622
potential_path.push(&info.file_name);
623623
if potential_path.exists() {

plugins/warp/src/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ pub struct CacheDestructor;
7979
impl ObjectDestructor for CacheDestructor {
8080
fn destruct_view(&self, view: &BinaryView) {
8181
clear_type_ref_cache(view);
82-
tracing::debug!("Removed WARP caches for {:?}", view.file().filename());
82+
tracing::debug!("Removed WARP caches for {}", view.file());
8383
}
8484
}

plugins/warp/src/plugin/create.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ impl SaveFileField {
2323
let default_name = match file.project_file() {
2424
None => {
2525
// Not in a project, use the file name directly.
26-
file.filename()
27-
.split('/')
28-
.last()
29-
.unwrap_or("file")
30-
.to_string()
26+
file.display_name()
3127
}
3228
Some(project_file) => project_file.name(),
3329
};

rust/examples/decompile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub fn main() {
4242
.load(&filename)
4343
.expect("Couldn't open file!");
4444

45-
tracing::info!("Filename: `{}`", bv.file().filename());
45+
tracing::info!("File: `{}`", bv.file());
4646
tracing::info!("File size: `{:#x}`", bv.len());
4747
tracing::info!("Function count: {}", bv.functions().len());
4848

rust/examples/disassemble.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn main() {
3939
.load(&filename)
4040
.expect("Couldn't open file!");
4141

42-
tracing::info!("Filename: `{}`", bv.file().filename());
42+
tracing::info!("File: `{}`", bv.file());
4343
tracing::info!("File size: `{:#x}`", bv.len());
4444
tracing::info!("Function count: {}", bv.functions().len());
4545

rust/examples/high_level_il.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
.load("/bin/cat")
1515
.expect("Couldn't open `/bin/cat`");
1616

17-
tracing::info!("Filename: `{}`", bv.file().filename());
17+
tracing::info!("File: `{}`", bv.file());
1818
tracing::info!("File size: `{:#x}`", bv.len());
1919
tracing::info!("Function count: {}", bv.functions().len());
2020

rust/examples/medium_level_il.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
.load("/bin/cat")
1515
.expect("Couldn't open `/bin/cat`");
1616

17-
tracing::info!("Filename: `{}`", bv.file().filename());
17+
tracing::info!("File: `{}`", bv.file());
1818
tracing::info!("File size: `{:#x}`", bv.len());
1919
tracing::info!("Function count: {}", bv.functions().len());
2020

rust/src/binary_view.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,8 +2476,14 @@ impl BinaryView {
24762476
Ref::new(Self { handle })
24772477
}
24782478

2479-
pub fn from_path(meta: &mut FileMetadata, file_path: impl AsRef<Path>) -> Result<Ref<Self>> {
2480-
let file = file_path.as_ref().to_cstr();
2479+
/// Construct the raw binary view from the given metadata. Before calling this make sure you have
2480+
/// a valid file path set for the [`FileMetadata`]. It is required that the [`FileMetadata::file_path`]
2481+
/// exist on the local filesystem.
2482+
pub fn from_metadata(meta: &FileMetadata) -> Result<Ref<Self>> {
2483+
if !meta.file_path().exists() {
2484+
return Err(());
2485+
}
2486+
let file = meta.file_path().to_cstr();
24812487
let handle =
24822488
unsafe { BNCreateBinaryDataViewFromFilename(meta.handle, file.as_ptr() as *mut _) };
24832489

@@ -2488,6 +2494,15 @@ impl BinaryView {
24882494
unsafe { Ok(Ref::new(Self { handle })) }
24892495
}
24902496

2497+
/// Construct the raw binary view from the given `file_path` and metadata.
2498+
///
2499+
/// This will implicitly set the metadata file path and then construct the view. If the metadata
2500+
/// already has the desired file path, use [`BinaryView::from_metadata`] instead.
2501+
pub fn from_path(meta: &FileMetadata, file_path: impl AsRef<Path>) -> Result<Ref<Self>> {
2502+
meta.set_file_path(file_path.as_ref());
2503+
Self::from_metadata(meta)
2504+
}
2505+
24912506
pub fn from_accessor<A: Accessor>(
24922507
meta: &FileMetadata,
24932508
file: &mut FileAccessor<A>,

0 commit comments

Comments
 (0)