cmArchiveWrite.cxx 9.6 KB

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