Skip to content

Commit 2ba2857

Browse files
committed
Fix follow symlinks (sharun)
Set APPDIR env var for sharun AppRun if env var not exists (sharun)
1 parent 8de1724 commit 2ba2857

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sharun"
3-
version = "0.5.9"
3+
version = "0.6.1"
44
readme = "README.md"
55
license = "MIT"
66
repository = "https://github.com/VHSgunzo/sharun"

src/main.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ fn main() {
381381
let mut exec_args: Vec<String> = env::args().collect();
382382

383383
let mut sharun_dir = realpath(&get_env_var("SHARUN_DIR"));
384-
if sharun_dir.is_empty() ||
384+
if sharun_dir.is_empty() ||
385385
!(is_dir(&sharun_dir) && {
386386
let sharun_dir_path = Path::new(&sharun_dir);
387387
let sharun_path = sharun_dir_path.join(SHARUN_NAME);
@@ -415,13 +415,20 @@ fn main() {
415415
exit(1)
416416
}
417417
});
418-
let arg0_path = arg0_dir.join(arg0_name);
419418

420-
let mut bin_name = if arg0_path.is_symlink() && arg0_path.canonicalize().unwrap() == sharun {
419+
let arg0_path = arg0_dir.join(arg0_name);
420+
let arg0_full_path = arg0_path.canonicalize().unwrap();
421+
let arg0_full_path_name = arg0_full_path.file_name().unwrap().to_string_lossy().to_string();
422+
let mut bin_name = if arg0_path.is_symlink() &&
423+
arg0_full_path == Path::new(&sharun_dir).join(SHARUN_NAME) {
421424
arg0_name.into()
425+
} else if arg0_path.is_symlink() && Path::new(&shared_bin).join(&arg0_full_path_name).exists() {
426+
arg0_full_path_name
422427
} else {
423-
basename(sharun.file_name().unwrap().to_str().unwrap())
428+
sharun.file_name().unwrap().to_string_lossy().to_string()
424429
};
430+
drop(arg0_dir);
431+
drop(arg0_full_path);
425432

426433
if bin_name == SHARUN_NAME {
427434
if !exec_args.is_empty() {
@@ -476,30 +483,36 @@ fn main() {
476483
_ => {
477484
bin_name = exec_args.remove(0);
478485
let bin_path = PathBuf::from(bin_dir).join(&bin_name);
479-
if is_exe(&bin_path) &&
480-
(is_hardlink(&sharun, &bin_path) ||
481-
!Path::new(&shared_bin).join(&bin_name).exists() ||
482-
bin_path.canonicalize().unwrap() != sharun.canonicalize().unwrap())
483-
{
484-
add_to_env("PATH", bin_dir);
485-
match is_script(&bin_path) {
486-
Ok(true) => {
487-
if let Err(err) = exec_script(&bin_path, &exec_args) {
488-
eprintln!("Error executing script: {err}");
489-
exit(1);
486+
if let Ok(bin_full_path) = bin_path.canonicalize() {
487+
let bin_full_path_name = bin_full_path.file_name().unwrap().to_string_lossy().to_string();
488+
if bin_path.is_symlink() && Path::new(&shared_bin).join(&bin_full_path_name).exists() {
489+
bin_name = bin_full_path_name
490+
}
491+
if is_exe(&bin_full_path) &&
492+
(is_hardlink(&sharun, &bin_full_path) ||
493+
!Path::new(&shared_bin).join(&bin_name).exists() ||
494+
bin_full_path != sharun)
495+
{
496+
add_to_env("PATH", bin_dir);
497+
match is_script(&bin_path) {
498+
Ok(true) => {
499+
if let Err(err) = exec_script(&bin_path, &exec_args) {
500+
eprintln!("Error executing script: {err}");
501+
exit(1);
502+
}
503+
}
504+
Ok(false) => {
505+
let err = Command::new(&bin_path)
506+
.envs(env::vars())
507+
.args(exec_args)
508+
.exec();
509+
eprintln!("Error executing file {:?}: {err}", &bin_path);
510+
exit(1)
511+
}
512+
Err(err) => {
513+
eprintln!("Error reading file {:?}: {err}", &bin_path);
514+
exit(1)
490515
}
491-
}
492-
Ok(false) => {
493-
let err = Command::new(&bin_path)
494-
.envs(env::vars())
495-
.args(exec_args)
496-
.exec();
497-
eprintln!("Error executing file {:?}: {err}", &bin_path);
498-
exit(1)
499-
}
500-
Err(err) => {
501-
eprintln!("Error reading file {:?}: {err}", &bin_path);
502-
exit(1)
503516
}
504517
}
505518
}
@@ -562,7 +575,9 @@ fn main() {
562575
if get_env_var("ARGV0").is_empty() {
563576
env::set_var("ARGV0", &arg0)
564577
}
565-
env::set_var("APPDIR", &sharun_dir);
578+
if get_env_var("APPDIR").is_empty() {
579+
env::set_var("APPDIR", &sharun_dir)
580+
}
566581

567582
let err = Command::new(app)
568583
.envs(env::vars())

0 commit comments

Comments
 (0)