441 441 static void link(
Path root,
String source,
String target) throws
Throwable {
442 - var source_path
= root.resolve(source).toAbsolutePath();
442 + var source_path
= root.resolve(source).toAbsolutePath().normalize();
443 443 if (
!Files.exists(source_path)) {
444 444 log(
"\n{0} does not exist anymore in my dorkfiles. Have I yeeted it?", emphasize_global(source_path.toString()));
447 + var expected
= source_path.toRealPath();
448 + require_inside_home(expected,
"Symlink source");
447 449 var target_path
= expand_target(target);
448 450 if (
Files.exists(target_path,
LinkOption.
NOFOLLOW_LINKS)) {
449 451 if (
Files.isSymbolicLink(target_path)) {
452 454 remove_owned_symlink(target_path);
454 456 var actual
= target_path.toRealPath();
455 - var expected
= source_path.toRealPath();
456 457 if (actual.equals(expected)) {
457 458 log(
"\nOK {0} -> {1}.", emphasize_global(target_path.toString()), emphasize_local(source_path.toString()));
460 - if (
!actual.startsWith(root.toRealPath()))
461 - throw fail(
"Target symlink points outside dorkfiles: {0} -> {1}. Move it manually.", target_path, actual);
462 - log(
"\nRELINK {0}: {1} -> {2}.", emphasize_global(target_path.toString()), emphasize_local(actual.toString()), emphasize_local(expected.toString()));
463 - remove_owned_symlink(target_path);
461 + if (
!inside_home(actual)) {
462 + log(
"\nRELINK {0}: outside HOME {1} -> {2}.", emphasize_global(target_path.toString()), emphasize_local(actual.toString()), emphasize_local(expected.toString()));
463 + remove_owned_symlink(target_path);
465 + if (
!actual.startsWith(root.toRealPath()))
466 + throw fail(
"Target symlink points outside dorkfiles: {0} -> {1}. Move it manually.", target_path, actual);
467 + log(
"\nRELINK {0}: {1} -> {2}.", emphasize_global(target_path.toString()), emphasize_local(actual.toString()), emphasize_local(expected.toString()));
468 + remove_owned_symlink(target_path);
465 471 }
else if (
Files.exists(target_path)) {
466 472 var actual
= target_path.toRealPath();
467 - var expected
= source_path.toRealPath();
468 473 if (actual.equals(expected)) {
469 474 log(
"\nOK {0} resolves to {1}.", emphasize_global(target_path.toString()), emphasize_local(source_path.toString()));
516 521 return candidate;
519 - static Path expand_target(
String target) {
520 - var home
= System.getProperty(
"user.home");
521 - if (
"~".equals(target))
522 - return Path.of(home).toAbsolutePath();
523 - if (target.startsWith(
"~/"))
524 - return Path.of(home).resolve(target.substring(
2)).toAbsolutePath();
525 - return Path.of(target).toAbsolutePath();
524 + static Path expand_target(
String target) throws
Throwable {
525 + var path
= switch (target) {
526 + case "~" -> target_home_path();
527 + default -> target.startsWith(
"~/")
? target_home_path().resolve(target.substring(
2))
: Path.of(target).toAbsolutePath();
529 + path
= path.normalize();
530 + require_inside_home(path,
"Symlink target");
534 + static Path target_home_path() {
535 + return Path.of(
System.getProperty(
"user.home")).toAbsolutePath().normalize();
538 + static Path home_path() {
539 + var home
= System.getenv(
"HOME");
540 + if (home
== null || home.isBlank())
541 + home
= System.getProperty(
"user.home");
542 + return Path.of(home).toAbsolutePath().normalize();
545 + static boolean inside_home(
Path path) {
546 + return path.toAbsolutePath().normalize().startsWith(home_path());
549 + static void require_inside_home(
Path path,
String label) throws
Throwable {
550 + if (
!inside_home(path))
551 + throw fail(
"{0} outside HOME: {1}", label, path.toAbsolutePath().normalize());
528 554 static String hostname() throws
Exception {