Skip to content

Commit

Permalink
fix(appimage): use path check for ownership
Browse files Browse the repository at this point in the history
Should allow editing desktop files without breaking ownership
  • Loading branch information
QaidVoid committed Oct 13, 2024
1 parent f697a5f commit 7181629
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[package]
name = "soar"
description = "A modern package manager for Linux"
license = "MIT"
version = "0.2.0"
edition = "2021"

Expand Down
2 changes: 1 addition & 1 deletion src/core/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub async fn cleanup() -> Result<()> {

while let Some(entry) = tree.next_entry().await? {
let path = entry.path();
if xattr::get(&path, "user.ManagedBy")?.as_deref() != Some(b"soar") {
if xattr::get(&path, "user.managed_by")?.as_deref() != Some(b"soar") {
continue;
};

Expand Down
25 changes: 13 additions & 12 deletions src/registry/package/appimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use backhand::{kind::Kind, FilesystemReader, InnerNode, Node, SquashfsFileReader
use image::{imageops::FilterType, DynamicImage, GenericImageView};
use tokio::fs;

use crate::core::{constant::BIN_PATH, util::home_data_path};
use crate::core::{
constant::{BIN_PATH, PACKAGES_PATH},
util::home_data_path,
};

const SUPPORTED_DIMENSIONS: &[(u32, u32)] = &[
(16, 16),
Expand Down Expand Up @@ -83,25 +86,20 @@ fn is_appimage(file: &mut BufReader<File>) -> bool {

async fn create_symlink(from: &Path, to: &Path) -> Result<()> {
if to.exists() {
if to.read_link().is_ok()
&& xattr::get_deref(from, "user.ManagedBy")?.as_deref() != Some(b"soar")
{
if to.read_link().is_ok() && !to.read_link()?.starts_with(&*PACKAGES_PATH) {
eprintln!("{} is not managed by soar", to.to_string_lossy());
return Ok(());
}
fs::remove_file(to).await?;
}
xattr::set(from, "user.ManagedBy", b"soar")?;
fs::symlink(from, to).await?;

Ok(())
}

async fn remove_link(path: &Path) -> Result<()> {
if path.exists() {
if path.read_link().is_ok()
&& xattr::get_deref(path, "user.ManagedBy")?.as_deref() != Some(b"soar")
{
if path.read_link().is_ok() && !path.read_link()?.starts_with(&*PACKAGES_PATH) {
eprintln!("{} is not managed by soar", path.to_string_lossy());
return Ok(());
}
Expand All @@ -125,7 +123,7 @@ pub async fn remove_applinks(name: &str, file_path: &Path) -> Result<()> {
.with_extension("png");
let desktop_path = data_path
.join("applications")
.join(name)
.join(format!("soar-{name}"))
.with_extension("desktop");

remove_link(&desktop_path).await?;
Expand All @@ -149,8 +147,8 @@ pub async fn extract_appimage(name: &str, file_path: &Path) -> Result<()> {

for node in squashfs.files() {
let node_path = node.fullpath.to_string_lossy();
if !node_path.trim_start_matches("/").contains("/") && node_path.ends_with(".DirIcon")
|| node_path.ends_with(".desktop")
if !node_path.trim_start_matches("/").contains("/")
&& (node_path.ends_with(".DirIcon") || node_path.ends_with(".desktop"))
{
let extension = if node_path.ends_with(".DirIcon") {
"png"
Expand Down Expand Up @@ -256,6 +254,8 @@ async fn process_desktop(output_path: &Path, name: &str, data_path: &Path) -> Re
format!("Icon={}", name)
} else if line.starts_with("Exec=") {
format!("Exec={}/{}", &*BIN_PATH.to_string_lossy(), name)
} else if line.starts_with("TryExec=") {
format!("TryExec={}/{}", &*BIN_PATH.to_string_lossy(), name)
} else {
line.to_string()
}
Expand All @@ -268,7 +268,7 @@ async fn process_desktop(output_path: &Path, name: &str, data_path: &Path) -> Re

let final_path = data_path
.join("applications")
.join(name)
.join(format!("soar-{name}"))
.with_extension("desktop");

if let Some(parent) = final_path.parent() {
Expand All @@ -277,6 +277,7 @@ async fn process_desktop(output_path: &Path, name: &str, data_path: &Path) -> Re
parent.to_string_lossy()
))?;
}

create_symlink(output_path, &final_path).await?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/registry/package/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl Installer {
}
tokio::fs::rename(&temp_path, &install_path).await?;
tokio::fs::set_permissions(&install_path, Permissions::from_mode(0o755)).await?;
xattr::set(install_path, "user.ManagedBy", b"soar")?;
xattr::set(install_path, "user.managed_by", b"soar")?;

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/registry/package/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Runner {
let package_name = &package.full_name('/');

if self.install_path.exists() {
if xattr::get(&self.install_path, "user.ManagedBy")?.as_deref() != Some(b"soar") {
if xattr::get(&self.install_path, "user.managed_by")?.as_deref() != Some(b"soar") {
return Err(anyhow::anyhow!(
"Path {} is not managed by soar. Exiting.",
self.install_path.to_string_lossy()
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Runner {
}
tokio::fs::rename(&temp_path, &install_path).await?;
tokio::fs::set_permissions(&install_path, Permissions::from_mode(0o755)).await?;
xattr::set(install_path, "user.ManagedBy", b"soar")?;
xattr::set(install_path, "user.managed_by", b"soar")?;

Ok(())
}
Expand Down

0 comments on commit 7181629

Please sign in to comment.