-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathar.cpp
More file actions
111 lines (100 loc) · 3.62 KB
/
ar.cpp
File metadata and controls
111 lines (100 loc) · 3.62 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
#include <cstdio>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/io.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "ar",
.version = CFBOX_VERSION_STRING,
.one_line = "create, modify, and extract from archives",
.usage = "ar -[dmpqrtx] ARCHIVE [FILE]...",
.options = " -r insert or replace files\n"
" -t list contents\n"
" -x extract files",
.extra = "",
};
} // namespace
auto ar_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'r', false},
cfbox::args::OptSpec{'t', false},
cfbox::args::OptSpec{'x', 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 replace = parsed.has('r');
bool list = parsed.has('t');
bool extract = parsed.has('x');
const auto& pos = parsed.positional();
if (pos.empty()) {
CFBOX_ERR("ar", "missing archive name");
return 1;
}
std::string archive{pos[0]};
if (replace) {
std::string output;
output.append("!<arch>\n", 8);
for (std::size_t i = 1; i < pos.size(); ++i) {
std::string fname{pos[i]};
auto data = cfbox::io::read_all(fname);
if (!data) continue;
char hdr[60];
std::memset(hdr, ' ', 60);
std::memcpy(hdr, fname.c_str(), std::min(fname.size(), static_cast<std::size_t>(16)));
std::snprintf(hdr + 16, 12, "%-10lu", static_cast<unsigned long>(::time(nullptr)));
std::snprintf(hdr + 28, 12, "%-6u", 0u); // uid
std::snprintf(hdr + 34, 12, "%-6u", 0u); // gid
std::snprintf(hdr + 40, 12, "%-8o", 0100644u);
std::snprintf(hdr + 48, 12, "%-10zu", data->size());
hdr[58] = '`'; hdr[59] = '\n';
output.append(hdr, 60);
output.append(*data);
if (data->size() % 2) output += '\n';
}
auto wresult = cfbox::io::write_all(archive, output);
if (!wresult) {
CFBOX_ERR("ar", "%s", wresult.error().msg.c_str());
return 1;
}
return 0;
}
if (list || extract) {
auto input = cfbox::io::read_all(archive);
if (!input) {
CFBOX_ERR("ar", "%s", input.error().msg.c_str());
return 1;
}
const auto& data = *input;
if (data.size() < 8 || data.substr(0, 8) != "!<arch>\n") {
CFBOX_ERR("ar", "not a valid archive");
return 1;
}
std::size_t offset = 8;
while (offset + 60 <= data.size()) {
auto name = std::string{data.substr(offset, 16)};
auto space = name.find(' ');
if (space != std::string::npos) name = name.substr(0, space);
auto size_str = std::string{data.substr(offset + 48, 10)};
auto fsize = std::stoul(size_str);
if (list) {
std::puts(name.c_str());
} else if (extract) {
auto content = data.substr(offset + 60, fsize);
if (!cfbox::io::write_all(name, content)) {
CFBOX_ERR("ar", "write failed: %s", name.c_str());
return 1;
}
}
offset += 60 + fsize;
if (fsize % 2) ++offset;
}
return 0;
}
CFBOX_ERR("ar", "must specify -r, -t, or -x");
return 1;
}