-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcp.cpp
More file actions
142 lines (125 loc) · 4.66 KB
/
cp.cpp
File metadata and controls
142 lines (125 loc) · 4.66 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <cstdio>
#include <string>
#include <string_view>
#include <cfbox/args.hpp>
#include <cfbox/fs_util.hpp>
#include <cfbox/help.hpp>
#include <cfbox/io.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "cp",
.version = CFBOX_VERSION_STRING,
.one_line = "copy files and directories",
.usage = "cp [OPTIONS] SOURCE... DEST",
.options = " -r copy directories recursively\n"
" -p preserve mode, ownership, and timestamps",
.extra = "",
};
auto copy_preserve(const std::string& src, const std::string& dst) -> int {
// Copy the file first
auto copy_result = cfbox::fs::copy_file(src, dst);
if (!copy_result) {
CFBOX_ERR("cp", "%s", copy_result.error().msg.c_str());
return 1;
}
// Preserve permissions
auto status_result = cfbox::fs::status(src);
if (status_result) {
auto perm_result = cfbox::fs::permissions(dst, status_result->permissions());
if (!perm_result) {
// non-fatal
}
}
return 0;
}
} // namespace
auto cp_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'r', false, "recursive"},
cfbox::args::OptSpec{'p', false, "preserve"},
});
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 recursive = parsed.has('r');
bool preserve = parsed.has('p');
const auto& pos = parsed.positional();
if (pos.size() < 2) {
CFBOX_ERR("cp", "missing file operand");
return 1;
}
// Last argument is the destination
std::string dst{pos.back()};
int rc = 0;
if (pos.size() == 2) {
// Single source
std::string src{pos[0]};
if (cfbox::fs::is_directory(src)) {
if (!recursive) {
CFBOX_ERR("cp", "-r not specified; omitting directory '%s'", src.c_str());
return 1;
}
// Determine destination path
std::string dest = dst;
if (cfbox::fs::is_directory(dst)) {
// Copy into directory
std::filesystem::path src_path{src};
dest = (std::filesystem::path{dst} / src_path.filename()).string();
}
auto result = cfbox::fs::copy_recursive(src, dest);
if (!result) {
CFBOX_ERR("cp", "cannot copy '%s' to '%s': %s", src.c_str(), dest.c_str(), result.error().msg.c_str());
rc = 1;
}
} else {
std::string dest = dst;
if (cfbox::fs::is_directory(dst)) {
std::filesystem::path src_path{src};
dest = (std::filesystem::path{dst} / src_path.filename()).string();
}
if (preserve) {
rc = copy_preserve(src, dest);
} else {
auto result = cfbox::fs::copy_file(src, dest);
if (!result) {
CFBOX_ERR("cp", "cannot copy '%s' to '%s': %s", src.c_str(), dest.c_str(), result.error().msg.c_str());
rc = 1;
}
}
}
} else {
// Multiple sources — destination must be a directory
if (!cfbox::fs::is_directory(dst)) {
CFBOX_ERR("cp", "target '%s' is not a directory", dst.c_str());
return 1;
}
for (std::size_t i = 0; i < pos.size() - 1; ++i) {
std::string src{pos[i]};
std::filesystem::path src_path{src};
std::string dest = (std::filesystem::path{dst} / src_path.filename()).string();
if (cfbox::fs::is_directory(src)) {
if (!recursive) {
CFBOX_ERR("cp", "-r not specified; omitting directory '%s'", src.c_str());
rc = 1;
continue;
}
auto result = cfbox::fs::copy_recursive(src, dest);
if (!result) {
CFBOX_ERR("cp", "cannot copy '%s' to '%s': %s", src.c_str(), dest.c_str(), result.error().msg.c_str());
rc = 1;
}
} else {
if (preserve) {
if (copy_preserve(src, dest) != 0) rc = 1;
} else {
auto result = cfbox::fs::copy_file(src, dest);
if (!result) {
CFBOX_ERR("cp", "cannot copy '%s' to '%s': %s", src.c_str(), dest.c_str(), result.error().msg.c_str());
rc = 1;
}
}
}
}
}
return rc;
}