cmArchiveWrite.cxx 11 KB

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