diff options
author | Tim Schumacher <timschumi@gmx.de> | 2022-07-25 16:33:07 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-07-25 22:21:01 +0100 |
commit | 864221cb02a35c22b9d00375a863e9a7f7d27299 (patch) | |
tree | 376bc63ab658b1cd3e6e2b17c8c1a8f0c1ecb8c7 | |
parent | 26d4a44a0f47f95670576a0ef322ca4b4b936c3e (diff) | |
download | serenity-864221cb02a35c22b9d00375a863e9a7f7d27299.zip |
ln: Implement correct handling of directories as link targets
-rw-r--r-- | Userland/Utilities/ln.cpp | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/Userland/Utilities/ln.cpp b/Userland/Utilities/ln.cpp index dc935c99a9..fd48da50e8 100644 --- a/Userland/Utilities/ln.cpp +++ b/Userland/Utilities/ln.cpp @@ -30,15 +30,20 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) path = path_buffer.view(); } - if (force) { - auto stat = Core::System::lstat(path); + auto stat = Core::System::lstat(path); - if (stat.is_error() && stat.error().code() != ENOENT) - return stat.error(); + if (stat.is_error() && stat.error().code() != ENOENT) + return stat.error(); - if (!stat.is_error()) { - TRY(Core::System::unlink(path)); - } + if (!stat.is_error() && S_ISDIR(stat.value().st_mode)) { + // The target path is a directory, so we presumably want <path>/<filename> as the effective path. + path_buffer = LexicalPath::join(path, LexicalPath::basename(target)).string(); + path = path_buffer.view(); + stat = Core::System::lstat(path); + } + + if (force && !stat.is_error()) { + TRY(Core::System::unlink(path)); } if (symbolic) { |