cmArchiveWrite.cxx 10 KB

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