ladybird/Userland/Utilities/ln.cpp
Ali Mohammad Pur 5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30

57 lines
1.6 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/LexicalPath.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio cpath rpath"));
bool force = false;
bool symbolic = false;
StringView target;
StringView path;
Core::ArgsParser args_parser;
args_parser.add_option(force, "Force the creation", "force", 'f');
args_parser.add_option(symbolic, "Create a symlink", "symbolic", 's');
args_parser.add_positional_argument(target, "Link target", "target");
args_parser.add_positional_argument(path, "Link path", "path", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
ByteString path_buffer;
if (path.is_empty()) {
path_buffer = LexicalPath::basename(target);
path = path_buffer.view();
}
auto stat = Core::System::lstat(path);
if (stat.is_error() && stat.error().code() != ENOENT)
return stat.release_error();
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) {
TRY(Core::System::symlink(target, path));
} else {
TRY(Core::System::link(target, path));
}
return 0;
}