cmArchiveWrite.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. 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. {
  139. #if ARCHIVE_VERSION_NUMBER >= 3004000
  140. // Upstream fixed an issue with their integer parsing in 3.4.0
  141. // which would cause spurious errors to be raised from `strtoull`.
  142. if (numThreads < 1) {
  143. int upperLimit = (numThreads == 0) ? std::numeric_limits<int>::max()
  144. : std::abs(numThreads);
  145. numThreads =
  146. cm::clamp<int>(std::thread::hardware_concurrency(), 1, upperLimit);
  147. }
  148. # ifdef _AIX
  149. // FIXME: Using more than 2 threads creates an empty archive.
  150. // Enforce this limit pending further investigation.
  151. numThreads = std::min(numThreads, 2);
  152. # endif
  153. std::string sNumThreads = std::to_string(numThreads);
  154. if (archive_write_set_filter_option(this->Archive, "xz", "threads",
  155. sNumThreads.c_str()) !=
  156. ARCHIVE_OK) {
  157. this->Error = cmStrCat("archive_compressor_xz_options: ",
  158. cm_archive_error_string(this->Archive));
  159. return;
  160. }
  161. #endif
  162. }
  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. break;
  171. }
  172. if (compressionLevel != 0) {
  173. std::string compressionLevelStr = std::to_string(compressionLevel);
  174. std::string archiveFilterName;
  175. switch (c) {
  176. case CompressNone:
  177. case CompressCompress:
  178. break;
  179. case CompressGZip:
  180. archiveFilterName = "gzip";
  181. break;
  182. case CompressBZip2:
  183. archiveFilterName = "bzip2";
  184. break;
  185. case CompressLZMA:
  186. archiveFilterName = "lzma";
  187. break;
  188. case CompressXZ:
  189. archiveFilterName = "xz";
  190. break;
  191. case CompressZstd:
  192. archiveFilterName = "zstd";
  193. break;
  194. }
  195. if (!archiveFilterName.empty()) {
  196. if (archive_write_set_filter_option(
  197. this->Archive, archiveFilterName.c_str(), "compression-level",
  198. compressionLevelStr.c_str()) != ARCHIVE_OK) {
  199. this->Error = cmStrCat("archive_write_set_filter_option: ",
  200. cm_archive_error_string(this->Archive));
  201. return;
  202. }
  203. }
  204. }
  205. #if !defined(_WIN32) || defined(__CYGWIN__)
  206. if (archive_read_disk_set_standard_lookup(this->Disk) != ARCHIVE_OK) {
  207. this->Error = cmStrCat("archive_read_disk_set_standard_lookup: ",
  208. cm_archive_error_string(this->Archive));
  209. return;
  210. }
  211. #endif
  212. if (archive_write_set_format_by_name(this->Archive, format.c_str()) !=
  213. ARCHIVE_OK) {
  214. this->Error = cmStrCat("archive_write_set_format_by_name: ",
  215. cm_archive_error_string(this->Archive));
  216. return;
  217. }
  218. // do not pad the last block!!
  219. if (archive_write_set_bytes_in_last_block(this->Archive, 1)) {
  220. this->Error = cmStrCat("archive_write_set_bytes_in_last_block: ",
  221. cm_archive_error_string(this->Archive));
  222. return;
  223. }
  224. }
  225. bool cmArchiveWrite::Open()
  226. {
  227. if (archive_write_open(
  228. this->Archive, this, nullptr,
  229. reinterpret_cast<archive_write_callback*>(&Callback::Write),
  230. nullptr) != ARCHIVE_OK) {
  231. this->Error =
  232. cmStrCat("archive_write_open: ", cm_archive_error_string(this->Archive));
  233. return false;
  234. }
  235. return true;
  236. }
  237. cmArchiveWrite::~cmArchiveWrite()
  238. {
  239. archive_read_free(this->Disk);
  240. archive_write_free(this->Archive);
  241. }
  242. bool cmArchiveWrite::Add(std::string path, size_t skip, const char* prefix,
  243. bool recursive)
  244. {
  245. if (!path.empty() && path.back() == '/') {
  246. path.erase(path.size() - 1);
  247. }
  248. this->AddPath(path.c_str(), skip, prefix, recursive);
  249. return this->Okay();
  250. }
  251. bool cmArchiveWrite::AddPath(const char* path, size_t skip, const char* prefix,
  252. bool recursive)
  253. {
  254. if (strcmp(path, ".") != 0 ||
  255. (this->Format != "zip" && this->Format != "7zip")) {
  256. if (!this->AddFile(path, skip, prefix)) {
  257. return false;
  258. }
  259. }
  260. if ((!cmSystemTools::FileIsDirectory(path) || !recursive) ||
  261. cmSystemTools::FileIsSymlink(path)) {
  262. return true;
  263. }
  264. cmsys::Directory d;
  265. if (d.Load(path)) {
  266. std::string next = cmStrCat(path, '/');
  267. if (next == "./" && (this->Format == "zip" || this->Format == "7zip")) {
  268. next.clear();
  269. }
  270. std::string::size_type end = next.size();
  271. unsigned long n = d.GetNumberOfFiles();
  272. for (unsigned long i = 0; i < n; ++i) {
  273. const char* file = d.GetFile(i);
  274. if (strcmp(file, ".") != 0 && strcmp(file, "..") != 0) {
  275. next.erase(end);
  276. next += file;
  277. if (!this->AddPath(next.c_str(), skip, prefix)) {
  278. return false;
  279. }
  280. }
  281. }
  282. }
  283. return true;
  284. }
  285. bool cmArchiveWrite::AddFile(const char* file, size_t skip, const char* prefix)
  286. {
  287. this->Error = "";
  288. // Skip the file if we have no name for it. This may happen on a
  289. // top-level directory, which does not need to be included anyway.
  290. if (skip >= strlen(file)) {
  291. return true;
  292. }
  293. const char* out = file + skip;
  294. cmLocaleRAII localeRAII;
  295. static_cast<void>(localeRAII);
  296. // Meta-data.
  297. std::string dest = cmStrCat(prefix ? prefix : "", out);
  298. if (this->Verbose) {
  299. std::cout << dest << "\n";
  300. }
  301. Entry e;
  302. cm_archive_entry_copy_sourcepath(e, file);
  303. cm_archive_entry_copy_pathname(e, dest);
  304. if (archive_read_disk_entry_from_file(this->Disk, e, -1, nullptr) !=
  305. ARCHIVE_OK) {
  306. this->Error = cmStrCat("Unable to read from file '", file,
  307. "': ", cm_archive_error_string(this->Disk));
  308. return false;
  309. }
  310. if (!this->MTime.empty()) {
  311. time_t now;
  312. time(&now);
  313. time_t t = cm_get_date(now, this->MTime.c_str());
  314. if (t == -1) {
  315. this->Error = cmStrCat("unable to parse mtime '", this->MTime, '\'');
  316. return false;
  317. }
  318. archive_entry_set_mtime(e, t, 0);
  319. } else {
  320. std::string source_date_epoch;
  321. cmSystemTools::GetEnv("SOURCE_DATE_EPOCH", source_date_epoch);
  322. if (!source_date_epoch.empty()) {
  323. std::istringstream iss(source_date_epoch);
  324. time_t epochTime;
  325. iss >> epochTime;
  326. if (iss.eof() && !iss.fail()) {
  327. // Set all of the file times to the epoch time to handle archive
  328. // formats that include creation/access time.
  329. archive_entry_set_mtime(e, epochTime, 0);
  330. archive_entry_set_atime(e, epochTime, 0);
  331. archive_entry_set_ctime(e, epochTime, 0);
  332. archive_entry_set_birthtime(e, epochTime, 0);
  333. }
  334. }
  335. }
  336. // manages the uid/guid of the entry (if any)
  337. if (this->Uid.IsSet() && this->Gid.IsSet()) {
  338. archive_entry_set_uid(e, this->Uid.Get());
  339. archive_entry_set_gid(e, this->Gid.Get());
  340. }
  341. if (!this->Uname.empty() && !this->Gname.empty()) {
  342. archive_entry_set_uname(e, this->Uname.c_str());
  343. archive_entry_set_gname(e, this->Gname.c_str());
  344. }
  345. // manages the permissions
  346. if (this->Permissions.IsSet()) {
  347. archive_entry_set_perm(e, this->Permissions.Get());
  348. }
  349. if (this->PermissionsMask.IsSet()) {
  350. int perm = archive_entry_perm(e);
  351. archive_entry_set_perm(e, perm & this->PermissionsMask.Get());
  352. }
  353. // Clear acl and xattr fields not useful for distribution.
  354. archive_entry_acl_clear(e);
  355. archive_entry_xattr_clear(e);
  356. archive_entry_set_fflags(e, 0, 0);
  357. if (this->Format == "pax" || this->Format == "paxr") {
  358. // Sparse files are a GNU tar extension.
  359. // Do not use them in standard tar files.
  360. archive_entry_sparse_clear(e);
  361. }
  362. if (archive_write_header(this->Archive, e) != ARCHIVE_OK) {
  363. this->Error = cmStrCat("archive_write_header: ",
  364. cm_archive_error_string(this->Archive));
  365. return false;
  366. }
  367. // do not copy content of symlink
  368. if (!archive_entry_symlink(e)) {
  369. // Content.
  370. if (size_t size = static_cast<size_t>(archive_entry_size(e))) {
  371. return this->AddData(file, size);
  372. }
  373. }
  374. return true;
  375. }
  376. bool cmArchiveWrite::AddData(const char* file, size_t size)
  377. {
  378. cmsys::ifstream fin(file, std::ios::in | std::ios::binary);
  379. if (!fin) {
  380. this->Error = cmStrCat("Error opening \"", file,
  381. "\": ", cmSystemTools::GetLastSystemError());
  382. return false;
  383. }
  384. char buffer[16384];
  385. size_t nleft = size;
  386. while (nleft > 0) {
  387. using ssize_type = std::streamsize;
  388. size_t const nnext = nleft > sizeof(buffer) ? sizeof(buffer) : nleft;
  389. ssize_type const nnext_s = static_cast<ssize_type>(nnext);
  390. fin.read(buffer, nnext_s);
  391. // Some stream libraries (older HPUX) return failure at end of
  392. // file on the last read even if some data were read. Check
  393. // gcount instead of trusting the stream error status.
  394. if (static_cast<size_t>(fin.gcount()) != nnext) {
  395. break;
  396. }
  397. if (archive_write_data(this->Archive, buffer, nnext) != nnext_s) {
  398. this->Error = cmStrCat("archive_write_data: ",
  399. cm_archive_error_string(this->Archive));
  400. return false;
  401. }
  402. nleft -= nnext;
  403. }
  404. if (nleft > 0) {
  405. this->Error = cmStrCat("Error reading \"", file,
  406. "\": ", cmSystemTools::GetLastSystemError());
  407. return false;
  408. }
  409. return true;
  410. }