Skip to content

Commit

Permalink
change app image impl + add helper methods on Error struct
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Jan 20, 2024
1 parent f18b819 commit 7dd4958
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/resource-resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ features = [ "auto-detect-format" ]
[dependencies]
thiserror.workspace = true
cargo-packager-utils.workspace = true
log = "0.4.20"

[features]
process-relaunch-dangerous-allow-symlink-macos = [ ]
auto-detect-format = [ ]
auto-detect-format = [ ]
12 changes: 12 additions & 0 deletions crates/resource-resolver/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ pub enum Error {
#[error("{0}: {1}")]
Var(String, std::env::VarError),
}

impl Error {
/// Construct an [`Error::Io`] error variant
pub fn io(desc: &str, err: std::io::Error) -> Self {
Error::Io(desc.to_owned(), err)
}

/// Construct an [`Error::Env`] error variant
pub fn env(desc: &str) -> Self {
Error::Env(desc.to_owned())
}
}
45 changes: 39 additions & 6 deletions crates/resource-resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,65 @@ pub fn current_format() -> PackageFormat {
pub fn resources_dir(package_format: PackageFormat) -> Result<PathBuf> {
match package_format {
PackageFormat::None => {
env::current_dir().map_err(|e| Error::Io("Can't access current dir".to_string(), e))
env::current_dir().map_err(|e| Error::io("Can't access current dir", e))
}
PackageFormat::App | PackageFormat::Dmg => {
let exe = current_exe()?;
let exe_dir = exe.parent().unwrap();
exe_dir
.join("../Resources")
.canonicalize()
.map_err(|e| Error::Io("".to_string(), e))
.map_err(|e| Error::io("Can't canonicalize path", e))
}
PackageFormat::Wix | PackageFormat::Nsis => {
let exe = current_exe()?;
let exe_dir = exe.parent().unwrap();
Ok(exe_dir.to_path_buf())
}
PackageFormat::Deb | PackageFormat::AppImage => {
PackageFormat::Deb => {
let exe = current_exe()?;
let binary_name = exe.file_name().unwrap().to_string_lossy();
let exe_name = exe.file_name().unwrap().to_string_lossy();

let path = format!("/usr/lib/{}/", binary_name);
let path = format!("/usr/lib/{}/", exe_name);
Ok(PathBuf::from(path))
}

PackageFormat::AppImage => {
let Some(appdir) = std::env::var_os("APPDIR") else {
return Err(Error::env("Can't find APPDIR env var"));
};

// validate that we're actually running on an AppImage
// an AppImage is mounted to `/$TEMPDIR/.mount_${appPrefix}${hash}`
// see https://github.com/AppImage/AppImageKit/blob/1681fd84dbe09c7d9b22e13cdb16ea601aa0ec47/src/runtime.c#L501
// note that it is safe to use `std::env::current_exe` here since we just loaded an AppImage.
let is_temp = std::env::current_exe()
.map(|p| {
p.display()
.to_string()
.starts_with(&format!("{}/.mount_", std::env::temp_dir().display()))
})
.unwrap_or(true);

if !is_temp {
log::warn!("`APPDIR` or `APPIMAGE` environment variable found but this application was not detected as an AppImage; this might be a security issue.");
}

let appdir: &std::path::Path = appdir.as_ref();

let exe = current_exe()?;
let exe_name = exe.file_name().unwrap().to_string_lossy();

Ok(PathBuf::from(format!(
"{}/usr/lib/{}",
appdir.display(),
exe_name
)))
}
}
}

fn current_exe() -> Result<PathBuf> {
cargo_packager_utils::current_exe::current_exe()
.map_err(|e| Error::Io("Can't detect the path of the current exe".to_string(), e))
.map_err(|e| Error::io("Can't detect the path of the current exe", e))
}

0 comments on commit 7dd4958

Please sign in to comment.