cmArchiveWrite.cxx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2010 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmArchiveWrite.h"
  11. #include "cmLocale.h"
  12. #include "cmSystemTools.h"
  13. #include "cm_get_date.h"
  14. #include <cm_libarchive.h>
  15. #include <cmsys/Directory.hxx>
  16. #include <cmsys/FStream.hxx>
  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. 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*, void* cd, const void* b, size_t n)
  58. {
  59. cmArchiveWrite* self = static_cast<cmArchiveWrite*>(cd);
  60. if (self->Stream.write(static_cast<const char*>(b),
  61. static_cast<std::streamsize>(n))) {
  62. return static_cast<__LA_SSIZE_T>(n);
  63. } else {
  64. return static_cast<__LA_SSIZE_T>(-1);
  65. }
  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. break;
  98. case CompressBZip2:
  99. if (archive_write_add_filter_bzip2(this->Archive) != ARCHIVE_OK) {
  100. this->Error = "archive_write_add_filter_bzip2: ";
  101. this->Error += cm_archive_error_string(this->Archive);
  102. return;
  103. }
  104. break;
  105. case CompressLZMA:
  106. if (archive_write_add_filter_lzma(this->Archive) != ARCHIVE_OK) {
  107. this->Error = "archive_write_add_filter_lzma: ";
  108. this->Error += cm_archive_error_string(this->Archive);
  109. return;
  110. }
  111. break;
  112. case CompressXZ:
  113. if (archive_write_add_filter_xz(this->Archive) != ARCHIVE_OK) {
  114. this->Error = "archive_write_add_filter_xz: ";
  115. this->Error += cm_archive_error_string(this->Archive);
  116. return;
  117. }
  118. break;
  119. };
  120. #if !defined(_WIN32) || defined(__CYGWIN__)
  121. if (archive_read_disk_set_standard_lookup(this->Disk) != ARCHIVE_OK) {
  122. this->Error = "archive_read_disk_set_standard_lookup: ";
  123. this->Error += cm_archive_error_string(this->Archive);
  124. return;
  125. }
  126. #endif
  127. if (archive_write_set_format_by_name(this->Archive, format.c_str()) !=
  128. ARCHIVE_OK) {
  129. this->Error = "archive_write_set_format_by_name: ";
  130. this->Error += cm_archive_error_string(this->Archive);
  131. return;
  132. }
  133. // do not pad the last block!!
  134. if (archive_write_set_bytes_in_last_block(this->Archive, 1)) {
  135. this->Error = "archive_write_set_bytes_in_last_block: ";
  136. this->Error += cm_archive_error_string(this->Archive);
  137. return;
  138. }
  139. if (archive_write_open(
  140. this->Archive, this, CM_NULLPTR,
  141. reinterpret_cast<archive_write_callback*>(&Callback::Write),
  142. CM_NULLPTR) != ARCHIVE_OK) {
  143. this->Error = "archive_write_open: ";
  144. this->Error += cm_archive_error_string(this->Archive);
  145. return;
  146. }
  147. }
  148. cmArchiveWrite::~cmArchiveWrite()
  149. {
  150. archive_read_free(this->Disk);
  151. archive_write_free(this->Archive);
  152. }
  153. bool cmArchiveWrite::Add(std::string path, size_t skip, const char* prefix,
  154. bool recursive)
  155. {
  156. if (this->Okay()) {
  157. if (!path.empty() && path[path.size() - 1] == '/') {
  158. path.erase(path.size() - 1);
  159. }
  160. this->AddPath(path.c_str(), skip, prefix, recursive);
  161. }
  162. return this->Okay();
  163. }
  164. bool cmArchiveWrite::AddPath(const char* path, size_t skip, const char* prefix,
  165. bool recursive)
  166. {
  167. if (!this->AddFile(path, skip, prefix)) {
  168. return false;
  169. }
  170. if ((!cmSystemTools::FileIsDirectory(path) || !recursive) ||
  171. cmSystemTools::FileIsSymlink(path)) {
  172. return true;
  173. }
  174. cmsys::Directory d;
  175. if (d.Load(path)) {
  176. std::string next = path;
  177. next += "/";
  178. std::string::size_type end = next.size();
  179. unsigned long n = d.GetNumberOfFiles();
  180. for (unsigned long i = 0; i < n; ++i) {
  181. const char* file = d.GetFile(i);
  182. if (strcmp(file, ".") != 0 && strcmp(file, "..") != 0) {
  183. next.erase(end);
  184. next += file;
  185. if (!this->AddPath(next.c_str(), skip, prefix)) {
  186. return false;
  187. }
  188. }
  189. }
  190. }
  191. return true;
  192. }
  193. bool cmArchiveWrite::AddFile(const char* file, size_t skip, const char* prefix)
  194. {
  195. // Skip the file if we have no name for it. This may happen on a
  196. // top-level directory, which does not need to be included anyway.
  197. if (skip >= strlen(file)) {
  198. return true;
  199. }
  200. const char* out = file + skip;
  201. cmLocaleRAII localeRAII;
  202. static_cast<void>(localeRAII);
  203. // Meta-data.
  204. std::string dest = prefix ? prefix : "";
  205. dest += out;
  206. if (this->Verbose) {
  207. std::cout << dest << "\n";
  208. }
  209. Entry e;
  210. cm_archive_entry_copy_sourcepath(e, file);
  211. cm_archive_entry_copy_pathname(e, dest);
  212. if (archive_read_disk_entry_from_file(this->Disk, e, -1, CM_NULLPTR) !=
  213. ARCHIVE_OK) {
  214. this->Error = "archive_read_disk_entry_from_file '";
  215. this->Error += file;
  216. this->Error += "': ";
  217. this->Error += cm_archive_error_string(this->Disk);
  218. return false;
  219. }
  220. if (!this->MTime.empty()) {
  221. time_t now;
  222. time(&now);
  223. time_t t = cm_get_date(now, this->MTime.c_str());
  224. if (t == -1) {
  225. this->Error = "unable to parse mtime '";
  226. this->Error += this->MTime;
  227. this->Error += "'";
  228. return false;
  229. }
  230. archive_entry_set_mtime(e, t, 0);
  231. }
  232. // manages the uid/guid of the entry (if any)
  233. if (this->Uid.IsSet() && this->Gid.IsSet()) {
  234. archive_entry_set_uid(e, this->Uid.Get());
  235. archive_entry_set_gid(e, this->Gid.Get());
  236. }
  237. if (!this->Uname.empty() && !this->Gname.empty()) {
  238. archive_entry_set_uname(e, this->Uname.c_str());
  239. archive_entry_set_gname(e, this->Gname.c_str());
  240. }
  241. // manages the permissions
  242. if (this->Permissions.IsSet()) {
  243. archive_entry_set_perm(e, this->Permissions.Get());
  244. }
  245. if (this->PermissionsMask.IsSet()) {
  246. mode_t perm = archive_entry_perm(e);
  247. archive_entry_set_perm(e, perm & this->PermissionsMask.Get());
  248. }
  249. // Clear acl and xattr fields not useful for distribution.
  250. archive_entry_acl_clear(e);
  251. archive_entry_xattr_clear(e);
  252. archive_entry_set_fflags(e, 0, 0);
  253. if (this->Format == "pax" || this->Format == "paxr") {
  254. // Sparse files are a GNU tar extension.
  255. // Do not use them in standard tar files.
  256. archive_entry_sparse_clear(e);
  257. }
  258. if (archive_write_header(this->Archive, e) != ARCHIVE_OK) {
  259. this->Error = "archive_write_header: ";
  260. this->Error += cm_archive_error_string(this->Archive);
  261. return false;
  262. }
  263. // do not copy content of symlink
  264. if (!archive_entry_symlink(e)) {
  265. // Content.
  266. if (size_t size = static_cast<size_t>(archive_entry_size(e))) {
  267. return this->AddData(file, size);
  268. }
  269. }
  270. return true;
  271. }
  272. bool cmArchiveWrite::AddData(const char* file, size_t size)
  273. {
  274. cmsys::ifstream fin(file, std::ios::in | std::ios::binary);
  275. if (!fin) {
  276. this->Error = "Error opening \"";
  277. this->Error += file;
  278. this->Error += "\": ";
  279. this->Error += cmSystemTools::GetLastSystemError();
  280. return false;
  281. }
  282. char buffer[16384];
  283. size_t nleft = size;
  284. while (nleft > 0) {
  285. typedef std::streamsize ssize_type;
  286. size_t const nnext = nleft > sizeof(buffer) ? sizeof(buffer) : nleft;
  287. ssize_type const nnext_s = static_cast<ssize_type>(nnext);
  288. fin.read(buffer, nnext_s);
  289. // Some stream libraries (older HPUX) return failure at end of
  290. // file on the last read even if some data were read. Check
  291. // gcount instead of trusting the stream error status.
  292. if (static_cast<size_t>(fin.gcount()) != nnext) {
  293. break;
  294. }
  295. if (archive_write_data(this->Archive, buffer, nnext) != nnext_s) {
  296. this->Error = "archive_write_data: ";
  297. this->Error += cm_archive_error_string(this->Archive);
  298. return false;
  299. }
  300. nleft -= nnext;
  301. }
  302. if (nleft > 0) {
  303. this->Error = "Error reading \"";
  304. this->Error += file;
  305. this->Error += "\": ";
  306. this->Error += cmSystemTools::GetLastSystemError();
  307. return false;
  308. }
  309. return true;
  310. }