cmArchiveWrite.cxx 9.2 KB

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