cmArchiveWrite.cxx 8.9 KB

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