forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ln.cpp
56 lines (45 loc) · 1.63 KB
/
ln.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* 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);
DeprecatedString 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.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;
}