cmArchiveWrite.cxx 10.0 KB

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