cmArchiveWrite.cxx 11 KB

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