Skip to content

Commit

Permalink
Move create_apk_elf_path() helper into new apk module
Browse files Browse the repository at this point in the history
Move the create_apk_elf_path() helper function into the newly created
apk module, to make it accessible from outside the symbolization logic.

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o committed Feb 26, 2025
1 parent aa0b610 commit 8391c2d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 35 deletions.
49 changes: 49 additions & 0 deletions src/apk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf;

use crate::Error;
use crate::Result;


pub(crate) fn create_apk_elf_path(apk: &Path, elf: &Path) -> Result<PathBuf> {
let mut extension = apk
.extension()
.unwrap_or_else(|| OsStr::new("apk"))
.to_os_string();
// Append '!' to indicate separation from archive internal contents
// that follow. This is an Android convention.
let () = extension.push("!");

let mut apk = apk.to_path_buf();
if !apk.set_extension(extension) {
return Err(Error::with_invalid_data(format!(
"path {} is not valid",
apk.display()
)))
}

let path = apk.join(elf);
Ok(path)
}


#[cfg(test)]
mod tests {
use super::*;

use crate::ErrorKind;


/// Check that we can create a path to an ELF inside an APK as expected.
#[test]
fn elf_apk_path_creation() {
let apk = Path::new("/root/test.apk");
let elf = Path::new("subdir/libc.so");
let path = create_apk_elf_path(apk, elf).unwrap();
assert_eq!(path, Path::new("/root/test.apk!/subdir/libc.so"));

let err = create_apk_elf_path(Path::new(""), elf).unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidData);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ extern crate test;

#[macro_use]
mod cfg;
#[cfg(feature = "apk")]
mod apk;
#[cfg(feature = "breakpad")]
mod breakpad;
#[cfg(feature = "dwarf")]
Expand Down
37 changes: 2 additions & 35 deletions src/symbolize/symbolizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;

#[cfg(feature = "apk")]
use crate::apk::create_apk_elf_path;
#[cfg(feature = "breakpad")]
use crate::breakpad::BreakpadResolver;
use crate::elf::ElfParser;
Expand Down Expand Up @@ -114,29 +116,6 @@ impl Debug for DebugMapsEntry<'_> {
}


#[cfg(feature = "apk")]
fn create_apk_elf_path(apk: &Path, elf: &Path) -> Result<PathBuf> {
let mut extension = apk
.extension()
.unwrap_or_else(|| OsStr::new("apk"))
.to_os_string();
// Append '!' to indicate separation from archive internal contents
// that follow. This is an Android convention.
let () = extension.push("!");

let mut apk = apk.to_path_buf();
if !apk.set_extension(extension) {
return Err(Error::with_invalid_data(format!(
"path {} is not valid",
apk.display()
)))
}

let path = apk.join(elf);
Ok(path)
}


/// Demangle a symbol name using the demangling scheme for the given language.
#[cfg(feature = "demangle")]
fn maybe_demangle(name: Cow<'_, str>, language: SrcLang) -> Cow<'_, str> {
Expand Down Expand Up @@ -1527,18 +1506,6 @@ mod tests {
});
}

/// Check that we can create a path to an ELF inside an APK as expected.
#[test]
fn elf_apk_path_creation() {
let apk = Path::new("/root/test.apk");
let elf = Path::new("subdir/libc.so");
let path = create_apk_elf_path(apk, elf).unwrap();
assert_eq!(path, Path::new("/root/test.apk!/subdir/libc.so"));

let err = create_apk_elf_path(Path::new(""), elf).unwrap_err();
assert_eq!(err.kind(), ErrorKind::InvalidData);
}

/// Check that we can correctly construct the source code path to a symbol.
#[test]
fn symbol_source_code_path() {
Expand Down

0 comments on commit 8391c2d

Please sign in to comment.