cmArchiveWrite.cxx 14 KB

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