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