Skip to content

Commit

Permalink
Fix AppRun ARGV0
Browse files Browse the repository at this point in the history
Update lib4bin
  • Loading branch information
VHSgunzo committed Nov 12, 2024
1 parent f353205 commit a05c1d4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sharun"
version = "0.1.4"
version = "0.1.5"
readme = "README.md"
license = "MIT"
repository = "https://github.com/VHSgunzo/sharun"
Expand All @@ -20,6 +20,7 @@ panic = "abort"
opt-level = 0

[dependencies]
which = "7.0.0"
walkdir = "2.5.0"
userland-execve = "0.2.0"
dotenv = { git = "https://github.com/VHSgunzo/dotenv.git" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ cp ./target/$(uname -m)-unknown-linux-musl/release/sharun .
./test/sharun bash --version
```

* You can create a symlink from `sharun` to `AppRun` and write the name of the executable file from the `bin` directory to the `.app` file for compatibility with [AppImage](https://appimage.org) `AppDir`. If the `.app` file does not exist, the `*.desktop` file will be used.
* You can create a hard link from `sharun` to `AppRun` and write the name of the executable file from the `bin` directory to the `.app` file for compatibility with [AppImage](https://appimage.org) `AppDir`. If the `.app` file does not exist, the `*.desktop` file will be used.
* Additional env var can be specified in the `.env` file (see [dotenv](https://crates.io/crates/dotenv)). Env var can also be deleted using `unset ENV_VAR` in the end of the `.env` file.

# Screenshots:
Expand Down
2 changes: 1 addition & 1 deletion lib4bin
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ get_libs() {
sleep $STRACE_TIME
local pids="$(cut -d ' ' -f1<"$libs_file"|sort -u)"
kill $pids 2>/dev/null
local libs="$(echo -e "$(sed '/nvidia/d;/libcuda/d;/ENOENT/d'<"$libs_file"|\
local libs="$(echo -e "$(sed '/nvidia/d;/libcuda/d;/ENOENT/d;/glibc-hwcaps/d'<"$libs_file"|\
grep -oP '".*lib.*\.so.*"'|sed -u 's|"||g')")\n"
rm -f "$libs_file"
fi
Expand Down
50 changes: 43 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
os::unix::{fs::MetadataExt, process::CommandExt}
};

use which::which;
use walkdir::WalkDir;


Expand Down Expand Up @@ -53,6 +54,21 @@ fn basename(path: &str) -> String {
pieces.get(0).unwrap().to_string()
}

fn dirname(path: &str) -> String {
let mut pieces: Vec<&str> = path.split('/').collect();
if pieces.len() == 1 || path.is_empty() {
// return ".".to_string();
} else if !path.starts_with('/') &&
!path.starts_with('.') &&
!path.starts_with('~') {
pieces.insert(0, ".");
} else if pieces.len() == 2 && path.starts_with('/') {
pieces.insert(0, "");
};
pieces.pop();
pieces.join(&'/'.to_string())
}

fn is_file(path: &str) -> bool {
let path = Path::new(path);
path.is_file()
Expand Down Expand Up @@ -163,8 +179,9 @@ fn print_usage() {
}

fn main() {
let sharun = env::current_exe().unwrap();
let lib4bin = include_bytes!("../lib4bin");

let sharun: PathBuf = env::current_exe().unwrap();
let mut exec_args: Vec<String> = env::args().collect();

let mut sharun_dir = sharun.parent().unwrap().to_str().unwrap().to_string();
Expand All @@ -182,8 +199,26 @@ fn main() {
let shared_lib = format!("{shared_dir}/lib");
let shared_lib32 = format!("{shared_dir}/lib32");

let arg0 = &exec_args.remove(0);
let mut bin_name = basename(arg0);
let arg0 = PathBuf::from(exec_args.remove(0));
let arg0_name = arg0.file_name().unwrap();
let arg0_dir = PathBuf::from(dirname(arg0.to_str().unwrap())).canonicalize()
.unwrap_or_else(|_|{
if let Ok(which_arg0) = which(arg0_name) {
which_arg0.parent().unwrap().to_path_buf()
} else {
eprintln!("Failed to find ARG0 dir!");
exit(1)
}
});
let arg0_path = arg0_dir.join(arg0_name);

let mut bin_name: String;
if arg0_path.is_symlink() && arg0_path.canonicalize().unwrap() == sharun {
bin_name = arg0_name.to_str().unwrap().into();
} else {
bin_name = basename(sharun.file_name().unwrap().to_str().unwrap());
}

if bin_name == SHARUN_NAME {
if exec_args.len() > 0 {
match exec_args[0].as_str() {
Expand Down Expand Up @@ -269,7 +304,7 @@ fn main() {
});
appname = data.split("\n").filter_map(|string| {
if string.starts_with("Exec=") {
Some(string.replace("Exec=", "").split_whitespace().next().unwrap_or("").to_string())
Some(string.replace("Exec=", "").split_whitespace().next().unwrap_or("").into())
} else {None}
}).next().unwrap_or_else(||"".into())
}
Expand All @@ -296,7 +331,7 @@ fn main() {
let app = &format!("{bin_dir}/{appname}");

if get_env_var("ARGV0").is_empty() {
env::set_var("ARGV0", arg0)
env::set_var("ARGV0", &arg0)
}
env::set_var("APPDIR", &sharun_dir);

Expand All @@ -308,6 +343,7 @@ fn main() {
exit(1)
}
let bin = format!("{shared_bin}/{bin_name}");
println!("bin_name: {bin_name}");

let is_elf32_bin = is_elf32(&bin).unwrap_or_else(|err|{
eprintln!("Failed to check ELF class: {bin}: {err}");
Expand All @@ -322,7 +358,7 @@ fn main() {
}

read_dotenv(&sharun_dir);

let interpreter = get_interpreter(&library_path).unwrap_or_else(|_|{
eprintln!("Interpreter not found!");
exit(1)
Expand Down Expand Up @@ -354,7 +390,7 @@ fn main() {
for dir in dirs {
let dir_path = &format!("{library_path}/{dir}");
if dir.starts_with("python") {
add_to_env("PYTHONHOME", shared_dir);
add_to_env("PYTHONHOME", &sharun_dir);
env::set_var("PYTHONDONTWRITEBYTECODE", "1")
}
if dir.starts_with("perl") {
Expand Down

0 comments on commit a05c1d4

Please sign in to comment.