cmArchiveWrite.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmArchiveWrite.h"
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <ctime>
  7. #include <iostream>
  8. #include <limits>
  9. #include <sstream>
  10. #include <string>
  11. #include <thread>
  12. #include <cm/algorithm>
  13. #include <cm3p/archive.h>
  14. #include <cm3p/archive_entry.h>
  15. #include "cmsys/Directory.hxx"
  16. #include "cmsys/Encoding.hxx"
  17. #include "cmsys/FStream.hxx"
  18. #include "cm_parse_date.h"
  19. #include "cmLocale.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmSystemTools.h"
  22. #ifndef __LA_SSIZE_T
  23. # define __LA_SSIZE_T la_ssize_t
  24. #endif
  25. static std::string cm_archive_error_string(struct archive* a)
  26. {
  27. char const* e = archive_error_string(a);
  28. return e ? e : "unknown error";
  29. }
  30. static void cm_archive_entry_copy_pathname(struct archive_entry* e,
  31. std::string const& dest)
  32. {
  33. archive_entry_copy_pathname_w(e, cmsys::Encoding::ToWide(dest).c_str());
  34. }
  35. static void cm_archive_entry_copy_sourcepath(struct archive_entry* e,
  36. std::string const& file)
  37. {
  38. archive_entry_copy_sourcepath_w(e, cmsys::Encoding::ToWide(file).c_str());
  39. }
  40. class cmArchiveWrite::Entry
  41. {
  42. struct archive_entry* Object;
  43. public:
  44. Entry()
  45. : Object(archive_entry_new())
  46. {
  47. }
  48. ~Entry() { archive_entry_free(this->Object); }
  49. Entry(Entry const&) = delete;
  50. Entry& operator=(Entry const&) = delete;
  51. operator struct archive_entry *() { return this->Object; }
  52. };
  53. struct cmArchiveWrite::Callback
  54. {
  55. // archive_write_callback
  56. static __LA_SSIZE_T Write(struct archive* /*unused*/, void* cd,
  57. void const* b, size_t n)
  58. {
  59. cmArchiveWrite* self = static_cast<cmArchiveWrite*>(cd);
  60. if (self->Stream.write(static_cast<char const*>(b),
  61. static_cast<std::streamsize>(n))) {
  62. return static_cast<__LA_SSIZE_T>(n);
  63. }
  64. return static_cast<__LA_SSIZE_T>(-1);
  65. }
  66. };
  67. cmArchiveWrite::cmArchiveWrite(std::ostream& os, Compress c,
  68. std::string const& format, int compressionLevel,
  69. int numThreads)
  70. : Stream(os)
  71. , Archive(archive_write_new())
  72. , Disk(archive_read_disk_new())
  73. , Format(format)
  74. {
  75. // Upstream fixed an issue with their integer parsing in 3.4.0
  76. // which would cause spurious errors to be raised from `strtoull`.
  77. if (numThreads < 1) {
  78. int upperLimit = (numThreads == 0) ? std::numeric_limits<int>::max()
  79. : std::abs(numThreads);
  80. numThreads =
  81. cm::clamp<int>(std::thread::hardware_concurrency(), 1, upperLimit);
  82. }
  83. std::string sNumThreads = std::to_string(numThreads);
  84. switch (c) {
  85. case CompressNone:
  86. if (archive_write_add_filter_none(this->Archive) != ARCHIVE_OK) {
  87. this->Error = cmStrCat("archive_write_add_filter_none: ",
  88. cm_archive_error_string(this->Archive));
  89. return;
  90. }
  91. break;
  92. case CompressCompress:
  93. if (archive_write_add_filter_compress(this->Archive) != ARCHIVE_OK) {
  94. this->Error = cmStrCat("archive_write_add_filter_compress: ",
  95. cm_archive_error_string(this->Archive));
  96. return;
  97. }
  98. break;
  99. case CompressGZip: {
  100. if (archive_write_add_filter_gzip(this->Archive) != ARCHIVE_OK) {
  101. this->Error = cmStrCat("archive_write_add_filter_gzip: ",
  102. cm_archive_error_string(this->Archive));
  103. return;
  104. }
  105. std::string source_date_epoch;
  106. cmSystemTools::GetEnv("SOURCE_DATE_EPOCH", source_date_epoch);
  107. if (!source_date_epoch.empty()) {
  108. // We're not able to specify an arbitrary timestamp for gzip.
  109. // The next best thing is to omit the timestamp entirely.
  110. if (archive_write_set_filter_option(this->Archive, "gzip", "timestamp",
  111. nullptr) != ARCHIVE_OK) {
  112. this->Error = cmStrCat("archive_write_set_filter_option: ",
  113. cm_archive_error_string(this->Archive));
  114. return;
  115. }
  116. }
  117. } break;
  118. case CompressBZip2:
  119. if (archive_write_add_filter_bzip2(this->Archive) != ARCHIVE_OK) {
  120. this->Error = cmStrCat("archive_write_add_filter_bzip2: ",
  121. cm_archive_error_string(this->Archive));
  122. return;
  123. }
  124. break;
  125. case CompressLZMA:
  126. if (archive_write_add_filter_lzma(this->Archive) != ARCHIVE_OK) {
  127. this->Error = cmStrCat("archive_write_add_filter_lzma: ",
  128. cm_archive_error_string(this->Archive));
  129. return;
  130. }
  131. break;
  132. case CompressXZ:
  133. if (archive_write_add_filter_xz(this->Archive) != ARCHIVE_OK) {
  134. this->Error = cmStrCat("archive_write_add_filter_xz: ",
  135. cm_archive_error_string(this->Archive));
  136. return;
  137. }
  138. #if ARCHIVE_VERSION_NUMBER >= 3004000
  139. # ifdef _AIX
  140. // FIXME: Using more than 2 threads creates an empty archive.
  141. // Enforce this limit pending further investigation.
  142. if (numThreads > 2) {
  143. numThreads = 2;
  144. sNumThreads = std::to_string(numThreads);
  145. }
  146. # endif
  147. if (archive_write_set_filter_option(this->Archive, "xz", "threads",
  148. sNumThreads.c_str()) != ARCHIVE_OK) {
  149. this->Error = cmStrCat("archive_compressor_xz_options: ",
  150. cm_archive_error_string(this->Archive));
  151. return;
  152. }
  153. #endif
  154. break;
  155. case CompressZstd:
  156. if (archive_write_add_filter_zstd(this->Archive) != ARCHIVE_OK) {
  157. this->Error = cmStrCat("archive_write_add_filter_zstd: ",
  158. cm_archive_error_string(this->Archive));
  159. return;
  160. }
  161. #if ARCHIVE_VERSION_NUMBER >= 3006000
  162. if (archive_write_set_filter_option(this->Archive, "zstd", "threads",
  163. sNumThreads.c_str()) != ARCHIVE_OK) {
  164. this->Error = cmStrCat("archive_compressor_zstd_options: ",
  165. cm_archive_error_string(this->Archive));
  166. return;
  167. }
  168. #endif
  169. break;
  170. }
  171. if (compressionLevel != 0) {
  172. std::string compressionLevelStr = std::to_string(compressionLevel);
  173. std::string archiveFilterName;
  174. switch (c) {
  175. case CompressNone:
  176. case CompressCompress:
  177. break;
  178. case CompressGZip:
  179. archiveFilterName = "gzip";
  180. break;
  181. case CompressBZip2:
  182. archiveFilterName = "bzip2";
  183. break;
  184. case CompressLZMA:
  185. archiveFilterName = "lzma";
  186. break;
  187. case CompressXZ:
  188. archiveFilterName = "xz";
  189. break;
  190. case CompressZstd:
  191. archiveFilterName = "zstd";
  192. break;
  193. }
  194. if (!archiveFilterName.empty()) {
  195. if (archive_write_set_filter_option(
  196. this->Archive, archiveFilterName.c_str(), "compression-level",
  197. compressionLevelStr.c_str()) != ARCHIVE_OK) {
  198. this->Error = cmStrCat("archive_write_set_filter_option: ",
  199. cm_archive_error_string(this->Archive));
  200. return;
  201. }
  202. }
  203. }
  204. #if !defined(_WIN32) || defined(__CYGWIN__)
  205. if (archive_read_disk_set_standard_lookup(this->Disk) != ARCHIVE_OK) {
  206. this->Error = cmStrCat("archive_read_disk_set_standard_lookup: ",
  207. cm_archive_error_string(this->Archive));
  208. return;
  209. }
  210. #endif
  211. if (archive_write_set_format_by_name(this->Archive, format.c_str()) !=
  212. ARCHIVE_OK) {
  213. this->Error = cmStrCat("archive_write_set_format_by_name: ",
  214. cm_archive_error_string(this->Archive));
  215. return;
  216. }
  217. // do not pad the last block!!
  218. if (archive_write_set_bytes_in_last_block(this->Archive, 1)) {
  219. this->Error = cmStrCat("archive_write_set_bytes_in_last_block: ",
  220. cm_archive_error_string(this->Archive));
  221. return;
  222. }
  223. }
  224. bool cmArchiveWrite::Open()
  225. {
  226. if (!this->Error.empty()) {
  227. return false;
  228. }
  229. if (archive_write_open(
  230. this->Archive, this, nullptr,
  231. reinterpret_cast<archive_write_callback*>(&Callback::Write),
  232. nullptr) != ARCHIVE_OK) {
  233. this->Error =
  234. cmStrCat("archive_write_open: ", cm_archive_error_string(this->Archive));
  235. return false;
  236. }
  237. return true;
  238. }
  239. cmArchiveWrite::~cmArchiveWrite()
  240. {
  241. archive_read_free(this->Disk);
  242. archive_write_free(this->Archive);
  243. }
  244. bool cmArchiveWrite::Add(std::string path, size_t skip, char const* prefix,
  245. bool recursive)
  246. {
  247. if (!path.empty() && path.back() == '/') {
  248. path.erase(path.size() - 1);
  249. }
  250. this->AddPath(path.c_str(), skip, prefix, recursive);
  251. return this->Okay();
  252. }
  253. bool cmArchiveWrite::AddPath(char const* path, size_t skip, char const* prefix,
  254. bool recursive)
  255. {
  256. if (strcmp(path, ".") != 0 ||
  257. (this->Format != "zip" && this->Format != "7zip")) {
  258. if (!this->AddFile(path, skip, prefix)) {
  259. return false;
  260. }
  261. }
  262. if ((!cmSystemTools::FileIsDirectory(path) || !recursive) ||
  263. cmSystemTools::FileIsSymlink(path)) {
  264. return true;
  265. }
  266. cmsys::Directory d;
  267. if (d.Load(path)) {
  268. std::string next = cmStrCat(path, '/');
  269. if (next == "./" && (this->Format == "zip" || this->Format == "7zip")) {
  270. next.clear();
  271. }
  272. std::string::size_type end = next.size();
  273. unsigned long n = d.GetNumberOfFiles();
  274. for (unsigned long i = 0; i < n; ++i) {
  275. char const* file = d.GetFile(i);
  276. if (strcmp(file, ".") != 0 && strcmp(file, "..") != 0) {
  277. next.erase(end);
  278. next += file;
  279. if (!this->AddPath(next.c_str(), skip, prefix)) {
  280. return false;
  281. }
  282. }
  283. }
  284. }
  285. return true;
  286. }
  287. bool cmArchiveWrite::AddFile(char const* file, size_t skip, char const* prefix)
  288. {
  289. this->Error = "";
  290. // Skip the file if we have no name for it. This may happen on a
  291. // top-level directory, which does not need to be included anyway.
  292. if (skip >= strlen(file)) {
  293. return true;
  294. }
  295. char const* out = file + skip;
  296. cmLocaleRAII localeRAII;
  297. static_cast<void>(localeRAII);
  298. // Meta-data.
  299. std::string dest = cmStrCat(prefix ? prefix : "", out);
  300. if (this->Verbose) {
  301. std::cout << dest << "\n";
  302. }
  303. Entry e;
  304. cm_archive_entry_copy_sourcepath(e, file);
  305. cm_archive_entry_copy_pathname(e, dest);
  306. if (archive_read_disk_entry_from_file(this->Disk, e, -1, nullptr) !=
  307. ARCHIVE_OK) {
  308. this->Error = cmStrCat("Unable to read from file '", file,
  309. "': ", cm_archive_error_string(this->Disk));
  310. return false;
  311. }
  312. if (!this->MTime.empty()) {
  313. time_t now;
  314. time(&now);
  315. time_t t = cm_parse_date(now, this->MTime.c_str());
  316. if (t == -1) {
  317. this->Error = cmStrCat("unable to parse mtime '", this->MTime, '\'');
  318. return false;
  319. }
  320. archive_entry_set_mtime(e, t, 0);
  321. } else {
  322. std::string source_date_epoch;
  323. cmSystemTools::GetEnv("SOURCE_DATE_EPOCH", source_date_epoch);
  324. if (!source_date_epoch.empty()) {
  325. std::istringstream iss(source_date_epoch);
  326. time_t epochTime;
  327. iss >> epochTime;
  328. if (iss.eof() && !iss.fail()) {
  329. // Set all of the file times to the epoch time to handle archive
  330. // formats that include creation/access time.
  331. archive_entry_set_mtime(e, epochTime, 0);
  332. archive_entry_set_atime(e, epochTime, 0);
  333. archive_entry_set_ctime(e, epochTime, 0);
  334. archive_entry_set_birthtime(e, epochTime, 0);
  335. }
  336. }
  337. }
  338. // manages the uid/guid of the entry (if any)
  339. if (this->Uid.IsSet() && this->Gid.IsSet()) {
  340. archive_entry_set_uid(e, this->Uid.Get());
  341. archive_entry_set_gid(e, this->Gid.Get());
  342. }
  343. if (!this->Uname.empty() && !this->Gname.empty()) {
  344. archive_entry_set_uname(e, this->Uname.c_str());
  345. archive_entry_set_gname(e, this->Gname.c_str());
  346. }
  347. // manages the permissions
  348. if (this->Permissions.IsSet()) {
  349. archive_entry_set_perm(e, this->Permissions.Get());
  350. }
  351. if (this->PermissionsMask.IsSet()) {
  352. int perm = archive_entry_perm(e);
  353. archive_entry_set_perm(e, perm & this->PermissionsMask.Get());
  354. }
  355. // Clear acl and xattr fields not useful for distribution.
  356. archive_entry_acl_clear(e);
  357. archive_entry_xattr_clear(e);
  358. archive_entry_set_fflags(e, 0, 0);
  359. if (this->Format == "pax" || this->Format == "paxr") {
  360. // Sparse files are a GNU tar extension.
  361. // Do not use them in standard tar files.
  362. archive_entry_sparse_clear(e);
  363. }
  364. if (archive_write_header(this->Archive, e) != ARCHIVE_OK) {
  365. this->Error = cmStrCat("archive_write_header: ",
  366. cm_archive_error_string(this->Archive));
  367. return false;
  368. }
  369. // do not copy content of symlink
  370. if (!archive_entry_symlink(e)) {
  371. // Content.
  372. if (size_t size = static_cast<size_t>(archive_entry_size(e))) {
  373. return this->AddData(file, size);
  374. }
  375. }
  376. return true;
  377. }
  378. bool cmArchiveWrite::AddData(char const* file, size_t size)
  379. {
  380. cmsys::ifstream fin(file, std::ios::in | std::ios::binary);
  381. if (!fin) {
  382. this->Error = cmStrCat("Error opening \"", file,
  383. "\": ", cmSystemTools::GetLastSystemError());
  384. return false;
  385. }
  386. char buffer[16384];
  387. size_t nleft = size;
  388. while (nleft > 0) {
  389. using ssize_type = std::streamsize;
  390. size_t const nnext = nleft > sizeof(buffer) ? sizeof(buffer) : nleft;
  391. ssize_type const nnext_s = static_cast<ssize_type>(nnext);
  392. fin.read(buffer, nnext_s);
  393. // Some stream libraries (older HPUX) return failure at end of
  394. // file on the last read even if some data were read. Check
  395. // gcount instead of trusting the stream error status.
  396. if (static_cast<size_t>(fin.gcount()) != nnext) {
  397. break;
  398. }
  399. if (archive_write_data(this->Archive, buffer, nnext) != nnext_s) {
  400. this->Error = cmStrCat("archive_write_data: ",
  401. cm_archive_error_string(this->Archive));
  402. return false;
  403. }
  404. nleft -= nnext;
  405. }
  406. if (nleft > 0) {
  407. this->Error = cmStrCat("Error reading \"", file,
  408. "\": ", cmSystemTools::GetLastSystemError());
  409. return false;
  410. }
  411. return true;
  412. }