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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmArchiveWrite.h"
#include <cstring>
#include <ctime>
#include <iostream>
#include <sstream>
#include <cm3p/archive.h>
#include <cm3p/archive_entry.h>
#include "cmsys/Directory.hxx"
#include "cmsys/Encoding.hxx"
#include "cmsys/FStream.hxx"
#include "cm_get_date.h"
#include "cmLocale.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#ifndef __LA_SSIZE_T
# define __LA_SSIZE_T la_ssize_t
#endif
static std::string cm_archive_error_string(struct archive* a)
{
const char* e = archive_error_string(a);
return e ? e : "unknown error";
}
static void cm_archive_entry_copy_pathname(struct archive_entry* e,
const std::string& dest)
{
#if cmsys_STL_HAS_WSTRING
archive_entry_copy_pathname_w(e, cmsys::Encoding::ToWide(dest).c_str());
#else
archive_entry_copy_pathname(e, dest.c_str());
#endif
}
static void cm_archive_entry_copy_sourcepath(struct archive_entry* e,
const std::string& file)
{
#if cmsys_STL_HAS_WSTRING
archive_entry_copy_sourcepath_w(e, cmsys::Encoding::ToWide(file).c_str());
#else
archive_entry_copy_sourcepath(e, file.c_str());
#endif
}
class cmArchiveWrite::Entry
{
struct archive_entry* Object;
public:
Entry()
: Object(archive_entry_new())
{
}
~Entry() { archive_entry_free(this->Object); }
Entry(const Entry&) = delete;
Entry& operator=(const Entry&) = delete;
operator struct archive_entry*() { return this->Object; }
};
struct cmArchiveWrite::Callback
{
// archive_write_callback
static __LA_SSIZE_T Write(struct archive* /*unused*/, void* cd,
const void* b, size_t n)
{
cmArchiveWrite* self = static_cast<cmArchiveWrite*>(cd);
if (self->Stream.write(static_cast<const char*>(b),
static_cast<std::streamsize>(n))) {
return static_cast<__LA_SSIZE_T>(n);
}
return static_cast<__LA_SSIZE_T>(-1);
}
};
cmArchiveWrite::cmArchiveWrite(std::ostream& os, Compress c,
std::string const& format)
: Stream(os)
, Archive(archive_write_new())
, Disk(archive_read_disk_new())
, Verbose(false)
, Format(format)
{
switch (c) {
case CompressNone:
if (archive_write_add_filter_none(this->Archive) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_add_filter_none: ",
cm_archive_error_string(this->Archive));
return;
}
break;
case CompressCompress:
if (archive_write_add_filter_compress(this->Archive) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_add_filter_compress: ",
cm_archive_error_string(this->Archive));
return;
}
break;
case CompressGZip: {
if (archive_write_add_filter_gzip(this->Archive) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_add_filter_gzip: ",
cm_archive_error_string(this->Archive));
return;
}
std::string source_date_epoch;
cmSystemTools::GetEnv("SOURCE_DATE_EPOCH", source_date_epoch);
if (!source_date_epoch.empty()) {
// We're not able to specify an arbitrary timestamp for gzip.
// The next best thing is to omit the timestamp entirely.
if (archive_write_set_filter_option(this->Archive, "gzip", "timestamp",
nullptr) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_set_filter_option: ",
cm_archive_error_string(this->Archive));
return;
}
}
} break;
case CompressBZip2:
if (archive_write_add_filter_bzip2(this->Archive) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_add_filter_bzip2: ",
cm_archive_error_string(this->Archive));
return;
}
break;
case CompressLZMA:
if (archive_write_add_filter_lzma(this->Archive) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_add_filter_lzma: ",
cm_archive_error_string(this->Archive));
return;
}
break;
case CompressXZ:
if (archive_write_add_filter_xz(this->Archive) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_add_filter_xz: ",
cm_archive_error_string(this->Archive));
return;
}
break;
case CompressZstd:
if (archive_write_add_filter_zstd(this->Archive) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_add_filter_zstd: ",
cm_archive_error_string(this->Archive));
return;
}
break;
}
#if !defined(_WIN32) || defined(__CYGWIN__)
if (archive_read_disk_set_standard_lookup(this->Disk) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_read_disk_set_standard_lookup: ",
cm_archive_error_string(this->Archive));
return;
}
#endif
if (archive_write_set_format_by_name(this->Archive, format.c_str()) !=
ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_set_format_by_name: ",
cm_archive_error_string(this->Archive));
return;
}
// do not pad the last block!!
if (archive_write_set_bytes_in_last_block(this->Archive, 1)) {
this->Error = cmStrCat("archive_write_set_bytes_in_last_block: ",
cm_archive_error_string(this->Archive));
return;
}
}
bool cmArchiveWrite::Open()
{
if (archive_write_open(
this->Archive, this, nullptr,
reinterpret_cast<archive_write_callback*>(&Callback::Write),
nullptr) != ARCHIVE_OK) {
this->Error =
cmStrCat("archive_write_open: ", cm_archive_error_string(this->Archive));
return false;
}
return true;
}
cmArchiveWrite::~cmArchiveWrite()
{
archive_read_free(this->Disk);
archive_write_free(this->Archive);
}
bool cmArchiveWrite::Add(std::string path, size_t skip, const char* prefix,
bool recursive)
{
if (!path.empty() && path.back() == '/') {
path.erase(path.size() - 1);
}
this->AddPath(path.c_str(), skip, prefix, recursive);
return this->Okay();
}
bool cmArchiveWrite::AddPath(const char* path, size_t skip, const char* prefix,
bool recursive)
{
if (strcmp(path, ".") != 0 ||
(this->Format != "zip" && this->Format != "7zip")) {
if (!this->AddFile(path, skip, prefix)) {
return false;
}
}
if ((!cmSystemTools::FileIsDirectory(path) || !recursive) ||
cmSystemTools::FileIsSymlink(path)) {
return true;
}
cmsys::Directory d;
if (d.Load(path)) {
std::string next = cmStrCat(path, '/');
if (next == "./" && (this->Format == "zip" || this->Format == "7zip")) {
next.clear();
}
std::string::size_type end = next.size();
unsigned long n = d.GetNumberOfFiles();
for (unsigned long i = 0; i < n; ++i) {
const char* file = d.GetFile(i);
if (strcmp(file, ".") != 0 && strcmp(file, "..") != 0) {
next.erase(end);
next += file;
if (!this->AddPath(next.c_str(), skip, prefix)) {
return false;
}
}
}
}
return true;
}
bool cmArchiveWrite::AddFile(const char* file, size_t skip, const char* prefix)
{
this->Error = "";
// Skip the file if we have no name for it. This may happen on a
// top-level directory, which does not need to be included anyway.
if (skip >= strlen(file)) {
return true;
}
const char* out = file + skip;
cmLocaleRAII localeRAII;
static_cast<void>(localeRAII);
// Meta-data.
std::string dest = cmStrCat(prefix ? prefix : "", out);
if (this->Verbose) {
std::cout << dest << "\n";
}
Entry e;
cm_archive_entry_copy_sourcepath(e, file);
cm_archive_entry_copy_pathname(e, dest);
if (archive_read_disk_entry_from_file(this->Disk, e, -1, nullptr) !=
ARCHIVE_OK) {
this->Error = cmStrCat("Unable to read from file '", file,
"': ", cm_archive_error_string(this->Disk));
return false;
}
if (!this->MTime.empty()) {
time_t now;
time(&now);
time_t t = cm_get_date(now, this->MTime.c_str());
if (t == -1) {
this->Error = cmStrCat("unable to parse mtime '", this->MTime, '\'');
return false;
}
archive_entry_set_mtime(e, t, 0);
} else {
std::string source_date_epoch;
cmSystemTools::GetEnv("SOURCE_DATE_EPOCH", source_date_epoch);
if (!source_date_epoch.empty()) {
std::istringstream iss(source_date_epoch);
time_t epochTime;
iss >> epochTime;
if (iss.eof() && !iss.fail()) {
// Set all of the file times to the epoch time to handle archive
// formats that include creation/access time.
archive_entry_set_mtime(e, epochTime, 0);
archive_entry_set_atime(e, epochTime, 0);
archive_entry_set_ctime(e, epochTime, 0);
archive_entry_set_birthtime(e, epochTime, 0);
}
}
}
// manages the uid/guid of the entry (if any)
if (this->Uid.IsSet() && this->Gid.IsSet()) {
archive_entry_set_uid(e, this->Uid.Get());
archive_entry_set_gid(e, this->Gid.Get());
}
if (!this->Uname.empty() && !this->Gname.empty()) {
archive_entry_set_uname(e, this->Uname.c_str());
archive_entry_set_gname(e, this->Gname.c_str());
}
// manages the permissions
if (this->Permissions.IsSet()) {
archive_entry_set_perm(e, this->Permissions.Get());
}
if (this->PermissionsMask.IsSet()) {
int perm = archive_entry_perm(e);
archive_entry_set_perm(e, perm & this->PermissionsMask.Get());
}
// Clear acl and xattr fields not useful for distribution.
archive_entry_acl_clear(e);
archive_entry_xattr_clear(e);
archive_entry_set_fflags(e, 0, 0);
if (this->Format == "pax" || this->Format == "paxr") {
// Sparse files are a GNU tar extension.
// Do not use them in standard tar files.
archive_entry_sparse_clear(e);
}
if (archive_write_header(this->Archive, e) != ARCHIVE_OK) {
this->Error = cmStrCat("archive_write_header: ",
cm_archive_error_string(this->Archive));
return false;
}
// do not copy content of symlink
if (!archive_entry_symlink(e)) {
// Content.
if (size_t size = static_cast<size_t>(archive_entry_size(e))) {
return this->AddData(file, size);
}
}
return true;
}
bool cmArchiveWrite::AddData(const char* file, size_t size)
{
cmsys::ifstream fin(file, std::ios::in | std::ios::binary);
if (!fin) {
this->Error = cmStrCat("Error opening \"", file,
"\": ", cmSystemTools::GetLastSystemError());
return false;
}
char buffer[16384];
size_t nleft = size;
while (nleft > 0) {
using ssize_type = std::streamsize;
size_t const nnext = nleft > sizeof(buffer) ? sizeof(buffer) : nleft;
ssize_type const nnext_s = static_cast<ssize_type>(nnext);
fin.read(buffer, nnext_s);
// Some stream libraries (older HPUX) return failure at end of
// file on the last read even if some data were read. Check
// gcount instead of trusting the stream error status.
if (static_cast<size_t>(fin.gcount()) != nnext) {
break;
}
if (archive_write_data(this->Archive, buffer, nnext) != nnext_s) {
this->Error = cmStrCat("archive_write_data: ",
cm_archive_error_string(this->Archive));
return false;
}
nleft -= nnext;
}
if (nleft > 0) {
this->Error = cmStrCat("Error reading \"", file,
"\": ", cmSystemTools::GetLastSystemError());
return false;
}
return true;
}
bool cmArchiveWrite::SetFilterOption(const char* module, const char* key,
const char* value)
{
if (archive_write_set_filter_option(this->Archive, module, key, value) !=
ARCHIVE_OK) {
this->Error = "archive_write_set_filter_option: ";
this->Error += cm_archive_error_string(this->Archive);
return false;
}
return true;
}
|