cmArchiveWrite.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. {
  174. return true;
  175. }
  176. cmsys::Directory d;
  177. if(d.Load(path))
  178. {
  179. std::string next = path;
  180. next += "/";
  181. std::string::size_type end = next.size();
  182. unsigned long n = d.GetNumberOfFiles();
  183. for(unsigned long i = 0; i < n; ++i)
  184. {
  185. const char* file = d.GetFile(i);
  186. if(strcmp(file, ".") != 0 && strcmp(file, "..") != 0)
  187. {
  188. next.erase(end);
  189. next += file;
  190. if(!this->AddPath(next.c_str(), skip, prefix))
  191. {
  192. return false;
  193. }
  194. }
  195. }
  196. }
  197. return true;
  198. }
  199. //----------------------------------------------------------------------------
  200. bool cmArchiveWrite::AddFile(const char* file,
  201. size_t skip, const char* prefix)
  202. {
  203. // Skip the file if we have no name for it. This may happen on a
  204. // top-level directory, which does not need to be included anyway.
  205. if(skip >= strlen(file))
  206. {
  207. return true;
  208. }
  209. const char* out = file + skip;
  210. // Meta-data.
  211. std::string dest = prefix? prefix : "";
  212. dest += out;
  213. if(this->Verbose)
  214. {
  215. std::cout << dest << "\n";
  216. }
  217. Entry e;
  218. archive_entry_copy_sourcepath(e, file);
  219. archive_entry_set_pathname(e, dest.c_str());
  220. if(archive_read_disk_entry_from_file(this->Disk, e, -1, 0) != ARCHIVE_OK)
  221. {
  222. this->Error = "archive_read_disk_entry_from_file: ";
  223. this->Error += archive_error_string(this->Disk);
  224. return false;
  225. }
  226. // Clear acl and xattr fields not useful for distribution.
  227. archive_entry_acl_clear(e);
  228. archive_entry_xattr_clear(e);
  229. if(archive_write_header(this->Archive, e) != ARCHIVE_OK)
  230. {
  231. this->Error = "archive_write_header: ";
  232. this->Error += archive_error_string(this->Archive);
  233. return false;
  234. }
  235. // Content.
  236. if(size_t size = static_cast<size_t>(archive_entry_size(e)))
  237. {
  238. return this->AddData(file, size);
  239. }
  240. return true;
  241. }
  242. //----------------------------------------------------------------------------
  243. bool cmArchiveWrite::AddData(const char* file, size_t size)
  244. {
  245. std::ifstream fin(file, std::ios::in | cmsys_ios_binary);
  246. if(!fin)
  247. {
  248. this->Error = "Error opening \"";
  249. this->Error += file;
  250. this->Error += "\": ";
  251. this->Error += cmSystemTools::GetLastSystemError();
  252. return false;
  253. }
  254. char buffer[16384];
  255. size_t nleft = size;
  256. while(nleft > 0)
  257. {
  258. typedef cmsys_ios::streamsize ssize_type;
  259. size_t const nnext = nleft > sizeof(buffer)? sizeof(buffer) : nleft;
  260. ssize_type const nnext_s = static_cast<ssize_type>(nnext);
  261. fin.read(buffer, nnext_s);
  262. // Some stream libraries (older HPUX) return failure at end of
  263. // file on the last read even if some data were read. Check
  264. // gcount instead of trusting the stream error status.
  265. if(static_cast<size_t>(fin.gcount()) != nnext)
  266. {
  267. break;
  268. }
  269. if(archive_write_data(this->Archive, buffer, nnext) != nnext_s)
  270. {
  271. this->Error = "archive_write_data: ";
  272. this->Error += archive_error_string(this->Archive);
  273. return false;
  274. }
  275. nleft -= nnext;
  276. }
  277. if(nleft > 0)
  278. {
  279. this->Error = "Error reading \"";
  280. this->Error += file;
  281. this->Error += "\": ";
  282. this->Error += cmSystemTools::GetLastSystemError();
  283. return false;
  284. }
  285. return true;
  286. }