-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathln.cpp
More file actions
67 lines (57 loc) · 1.99 KB
/
ln.cpp
File metadata and controls
67 lines (57 loc) · 1.99 KB
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
57
58
59
60
61
62
63
64
65
66
67
#include <cstdio>
#include <cstring>
#include <string>
#include <cfbox/args.hpp>
#include <cfbox/fs_util.hpp>
#include <cfbox/help.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "ln",
.version = CFBOX_VERSION_STRING,
.one_line = "make links between files",
.usage = "ln [-s] [-f] [-n] TARGET... DIRECTORY",
.options = " -s make symbolic links instead of hard links\n"
" -f remove existing destination files\n"
" -n treat LINK_NAME as a normal file if it is a symlink to a dir",
.extra = "",
};
} // namespace
auto ln_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'s', false},
cfbox::args::OptSpec{'f', false},
cfbox::args::OptSpec{'n', false},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
bool symbolic = parsed.has('s');
bool force = parsed.has('f');
const auto& pos = parsed.positional();
if (pos.size() < 2) {
CFBOX_ERR("ln", "missing operand");
return 1;
}
std::string target{pos[0]};
std::string link_name{pos[1]};
// If link_name is an existing directory, put link inside it
if (pos.size() == 2 && cfbox::fs::is_directory(link_name)) {
auto filename = std::filesystem::path{target}.filename();
link_name = (std::filesystem::path{link_name} / filename).string();
}
if (force) {
std::error_code ec;
std::filesystem::remove(std::filesystem::path{link_name}, ec);
}
cfbox::base::Result<void> result;
if (symbolic) {
result = cfbox::fs::create_symlink(target, link_name);
} else {
result = cfbox::fs::create_hard_link(target, link_name);
}
if (!result) {
CFBOX_ERR("ln", "%s", result.error().msg.c_str());
return 1;
}
return 0;
}