-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtar.cpp
More file actions
199 lines (176 loc) · 6.76 KB
/
tar.cpp
File metadata and controls
199 lines (176 loc) · 6.76 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <cstdio>
#include <ctime>
#include <cstring>
#include <filesystem>
#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 = "tar",
.version = CFBOX_VERSION_STRING,
.one_line = "create, extract, or list tar archives",
.usage = "tar -[cxt] [-f ARCHIVE] [-C DIR] [FILE]...",
.options = " -c create a new archive\n"
" -x extract from archive\n"
" -t list archive contents\n"
" -f FILE use FILE as the archive\n"
" -C DIR change to DIR before operating",
.extra = "",
};
struct TarHeader {
char name[100];
char mode[8];
char uid[8];
char gid[8];
char size[12];
char mtime[12];
char chksum[8];
char typeflag;
char linkname[100];
char magic[6];
char version[2];
char uname[32];
char gname[32];
char devmajor[8];
char devminor[8];
char prefix[155];
char padding[12];
};
static auto write_octal(char* buf, int len, unsigned long val) -> void {
std::snprintf(buf, static_cast<std::size_t>(len), "%0*lo", len - 1, val);
}
static auto compute_checksum(TarHeader& hdr) -> unsigned {
std::memset(hdr.chksum, ' ', 8);
auto* p = reinterpret_cast<unsigned char*>(&hdr);
unsigned sum = 0;
for (std::size_t i = 0; i < sizeof(hdr); ++i) sum += p[i];
return sum;
}
static auto create_header(const std::string& name, unsigned long size, char type) -> TarHeader {
TarHeader hdr{};
std::strncpy(hdr.name, name.c_str(), 99);
write_octal(hdr.mode, 8, 0644);
write_octal(hdr.uid, 8, 0);
write_octal(hdr.gid, 8, 0);
write_octal(hdr.size, 12, size);
write_octal(hdr.mtime, 12, static_cast<unsigned long>(::time(nullptr)));
hdr.typeflag = type;
std::memcpy(hdr.magic, "ustar", 5);
hdr.version[0] = '0'; hdr.version[1] = '0';
auto chk = compute_checksum(hdr);
std::snprintf(hdr.chksum, 7, "%06o", chk);
hdr.chksum[6] = '\0';
hdr.chksum[7] = ' ';
return hdr;
}
static auto collect_files(const std::filesystem::path& base,
std::vector<std::pair<std::string, std::filesystem::path>>& out) -> void {
if (!std::filesystem::exists(base)) return;
auto cwd = std::filesystem::current_path();
if (std::filesystem::is_directory(base)) {
out.emplace_back(std::filesystem::relative(base, cwd).string() + "/", base);
for (const auto& entry : std::filesystem::recursive_directory_iterator(base)) {
auto rel = std::filesystem::relative(entry.path(), cwd);
out.emplace_back(rel.string(), entry.path());
}
} else {
out.emplace_back(std::filesystem::relative(base, cwd).string(), base);
}
}
} // namespace
auto tar_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'c', false},
cfbox::args::OptSpec{'x', false},
cfbox::args::OptSpec{'t', false},
cfbox::args::OptSpec{'f', true, "file"},
cfbox::args::OptSpec{'C', true, "directory"},
});
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 create = parsed.has('c');
bool extract = parsed.has('x');
bool list = parsed.has('t');
std::string archive = "-";
if (auto f = parsed.get_any('f', "file")) archive = std::string{*f};
std::string dir;
if (auto d = parsed.get_any('C', "directory")) dir = std::string{*d};
const auto& pos = parsed.positional();
if (create) {
if (!dir.empty()) std::filesystem::current_path(dir);
std::string archive_data;
auto targets = pos.empty() ? std::vector<std::string_view>{"."} : pos;
std::vector<std::pair<std::string, std::filesystem::path>> files;
for (auto t : targets) {
collect_files(std::filesystem::path{t}, files);
}
for (auto& [relpath, fullpath] : files) {
if (std::filesystem::is_directory(fullpath)) {
auto hdr = create_header(relpath, 0, '5');
archive_data.append(reinterpret_cast<const char*>(&hdr), 512);
continue;
}
auto data = cfbox::io::read_all(fullpath.string());
if (!data) {
CFBOX_ERR("tar", "%s: %s", relpath.c_str(), data.error().msg.c_str());
continue;
}
auto hdr = create_header(relpath, data->size(), '0');
archive_data.append(reinterpret_cast<const char*>(&hdr), 512);
archive_data.append(*data);
auto rem = data->size() % 512;
if (rem > 0) archive_data.append(512 - rem, '\0');
}
archive_data.append(1024, '\0');
if (archive == "-") {
std::fwrite(archive_data.data(), 1, archive_data.size(), stdout);
} else {
auto wresult = cfbox::io::write_all(archive, archive_data);
if (!wresult) {
CFBOX_ERR("tar", "%s", wresult.error().msg.c_str());
return 1;
}
}
return 0;
}
if (list || extract) {
if (!dir.empty()) std::filesystem::current_path(dir);
auto input = (archive == "-") ? cfbox::io::read_all_stdin() : cfbox::io::read_all(archive);
if (!input) {
CFBOX_ERR("tar", "%s", input.error().msg.c_str());
return 1;
}
const auto& data = *input;
std::size_t offset = 0;
while (offset + 512 <= data.size()) {
auto* hdr = reinterpret_cast<const TarHeader*>(data.data() + offset);
if (hdr->name[0] == '\0') break;
unsigned long fsize = std::strtoul(hdr->size, nullptr, 8);
std::string name{hdr->name, static_cast<std::size_t>(strnlen(hdr->name, 100))};
if (list) {
std::puts(name.c_str());
} else if (extract) {
if (hdr->typeflag == '5') {
std::filesystem::create_directories(name);
} else {
auto parent = std::filesystem::path{name}.parent_path();
if (!parent.empty()) std::filesystem::create_directories(parent);
auto content = data.substr(offset + 512, fsize);
auto wresult = cfbox::io::write_all(name, content);
if (!wresult) {
CFBOX_ERR("tar", "%s", wresult.error().msg.c_str());
}
}
}
auto blocks = (fsize + 511) / 512;
offset += 512 + blocks * 512;
}
return 0;
}
CFBOX_ERR("tar", "must specify -c, -x, or -t");
return 1;
}