cmArchiveWrite.cxx 10 KB

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