cmArchiveWrite.cxx 12 KB

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