1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:48:11 +00:00

ln: Rework to use LibCore syscall wrappers

This commit is contained in:
Tim Schumacher 2022-07-25 16:05:20 +02:00 committed by Linus Groh
parent c44b9acac3
commit 26d4a44a0f

View file

@ -7,17 +7,15 @@
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/System.h> #include <LibCore/System.h>
#include <stdio.h>
#include <unistd.h>
ErrorOr<int> serenity_main(Main::Arguments arguments) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
TRY(Core::System::pledge("stdio cpath")); TRY(Core::System::pledge("stdio cpath rpath"));
bool force = false; bool force = false;
bool symbolic = false; bool symbolic = false;
char const* target = nullptr; StringView target;
char const* path = nullptr; StringView path;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.add_option(force, "Force the creation", "force", 'f'); args_parser.add_option(force, "Force the creation", "force", 'f');
@ -27,37 +25,27 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.parse(arguments); args_parser.parse(arguments);
String path_buffer; String path_buffer;
if (!path) { if (path.is_empty()) {
path_buffer = LexicalPath::basename(target); path_buffer = LexicalPath::basename(target);
path = path_buffer.characters(); path = path_buffer.view();
} }
do { if (force) {
if (symbolic) { auto stat = Core::System::lstat(path);
int rc = symlink(target, path);
if (rc < 0 && !force) {
perror("symlink");
return 1;
} else if (rc == 0) {
return 0;
}
} else {
int rc = link(target, path);
if (rc < 0 && !force) {
perror("link");
return 1;
} else if (rc == 0) {
return 0;
}
}
int rc = unlink(path); if (stat.is_error() && stat.error().code() != ENOENT)
if (rc < 0) { return stat.error();
perror("unlink");
return 1; if (!stat.is_error()) {
TRY(Core::System::unlink(path));
} }
force = false; }
} while (true);
if (symbolic) {
TRY(Core::System::symlink(target, path));
} else {
TRY(Core::System::link(target, path));
}
return 0; return 0;
} }