cmArchiveWrite.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 CompressGZip:
  61. if(archive_write_set_compression_gzip(this->Archive) != ARCHIVE_OK)
  62. {
  63. this->Error = "archive_write_set_compression_gzip: ";
  64. this->Error += archive_error_string(this->Archive);
  65. return;
  66. }
  67. break;
  68. case CompressBZip2:
  69. if(archive_write_set_compression_bzip2(this->Archive) != ARCHIVE_OK)
  70. {
  71. this->Error = "archive_write_set_compression_bzip2: ";
  72. this->Error += archive_error_string(this->Archive);
  73. return;
  74. }
  75. break;
  76. case CompressLZMA:
  77. if(archive_write_set_compression_lzma(this->Archive) != ARCHIVE_OK)
  78. {
  79. this->Error = "archive_write_set_compression_lzma: ";
  80. this->Error += archive_error_string(this->Archive);
  81. return;
  82. }
  83. break;
  84. case CompressXZ:
  85. if(archive_write_set_compression_xz(this->Archive) != ARCHIVE_OK)
  86. {
  87. this->Error = "archive_write_set_compression_xz: ";
  88. this->Error += archive_error_string(this->Archive);
  89. return;
  90. }
  91. break;
  92. };
  93. #if !defined(_WIN32) || defined(__CYGWIN__)
  94. if (archive_read_disk_set_standard_lookup(this->Disk) != ARCHIVE_OK)
  95. {
  96. this->Error = "archive_read_disk_set_standard_lookup: ";
  97. this->Error += archive_error_string(this->Archive);
  98. return;;
  99. }
  100. #endif
  101. switch (t)
  102. {
  103. case TypeZIP:
  104. if(archive_write_set_format_zip(this->Archive) != ARCHIVE_OK)
  105. {
  106. this->Error = "archive_write_set_format_zip: ";
  107. this->Error += archive_error_string(this->Archive);
  108. return;
  109. }
  110. break;
  111. case TypeTAR:
  112. if(archive_write_set_format_pax_restricted(this->Archive) != ARCHIVE_OK)
  113. {
  114. this->Error = "archive_write_set_format_pax_restricted: ";
  115. this->Error += archive_error_string(this->Archive);
  116. return;
  117. }
  118. break;
  119. }
  120. // do not pad the last block!!
  121. if (archive_write_set_bytes_in_last_block(this->Archive, 1))
  122. {
  123. this->Error = "archive_write_set_bytes_in_last_block: ";
  124. this->Error += archive_error_string(this->Archive);
  125. return;
  126. }
  127. if(archive_write_open(
  128. this->Archive, this, 0,
  129. reinterpret_cast<archive_write_callback*>(&Callback::Write),
  130. 0) != ARCHIVE_OK)
  131. {
  132. this->Error = "archive_write_open: ";
  133. this->Error += archive_error_string(this->Archive);
  134. return;
  135. }
  136. }
  137. //----------------------------------------------------------------------------
  138. cmArchiveWrite::~cmArchiveWrite()
  139. {
  140. archive_read_finish(this->Disk);
  141. archive_write_finish(this->Archive);
  142. }
  143. //----------------------------------------------------------------------------
  144. bool cmArchiveWrite::Add(std::string path, size_t skip, const char* prefix)
  145. {
  146. if(this->Okay())
  147. {
  148. if(!path.empty() && path[path.size()-1] == '/')
  149. {
  150. path.erase(path.size()-1);
  151. }
  152. this->AddPath(path.c_str(), skip, prefix);
  153. }
  154. return this->Okay();
  155. }
  156. //----------------------------------------------------------------------------
  157. bool cmArchiveWrite::AddPath(const char* path,
  158. size_t skip, const char* prefix)
  159. {
  160. if(!this->AddFile(path, skip, prefix))
  161. {
  162. return false;
  163. }
  164. if(!cmSystemTools::FileIsDirectory(path))
  165. {
  166. return true;
  167. }
  168. cmsys::Directory d;
  169. if(d.Load(path))
  170. {
  171. std::string next = path;
  172. next += "/";
  173. std::string::size_type end = next.size();
  174. unsigned long n = d.GetNumberOfFiles();
  175. for(unsigned long i = 0; i < n; ++i)
  176. {
  177. const char* file = d.GetFile(i);
  178. if(strcmp(file, ".") != 0 && strcmp(file, "..") != 0)
  179. {
  180. next.erase(end);
  181. next += file;
  182. if(!this->AddPath(next.c_str(), skip, prefix))
  183. {
  184. return false;
  185. }
  186. }
  187. }
  188. }
  189. return true;
  190. }
  191. //----------------------------------------------------------------------------
  192. bool cmArchiveWrite::AddFile(const char* file,
  193. size_t skip, const char* prefix)
  194. {
  195. // Skip the file if we have no name for it. This may happen on a
  196. // top-level directory, which does not need to be included anyway.
  197. if(skip >= strlen(file))
  198. {
  199. return true;
  200. }
  201. const char* out = file + skip;
  202. // Meta-data.
  203. std::string dest = prefix? prefix : "";
  204. dest += out;
  205. if(this->Verbose)
  206. {
  207. std::cout << dest << "\n";
  208. }
  209. Entry e;
  210. archive_entry_copy_sourcepath(e, file);
  211. archive_entry_set_pathname(e, dest.c_str());
  212. if(archive_read_disk_entry_from_file(this->Disk, e, -1, 0) != ARCHIVE_OK)
  213. {
  214. this->Error = "archive_read_disk_entry_from_file: ";
  215. this->Error += archive_error_string(this->Disk);
  216. return false;
  217. }
  218. if(archive_write_header(this->Archive, e) != ARCHIVE_OK)
  219. {
  220. this->Error = "archive_write_header: ";
  221. this->Error += archive_error_string(this->Archive);
  222. return false;
  223. }
  224. // Content.
  225. if(size_t size = static_cast<size_t>(archive_entry_size(e)))
  226. {
  227. return this->AddData(file, size);
  228. }
  229. return true;
  230. }
  231. //----------------------------------------------------------------------------
  232. bool cmArchiveWrite::AddData(const char* file, size_t size)
  233. {
  234. std::ifstream fin(file, std::ios::in | cmsys_ios_binary);
  235. if(!fin)
  236. {
  237. this->Error = "Error opening \"";
  238. this->Error += file;
  239. this->Error += "\": ";
  240. this->Error += cmSystemTools::GetLastSystemError();
  241. return false;
  242. }
  243. char buffer[16384];
  244. size_t nleft = size;
  245. while(nleft > 0)
  246. {
  247. typedef cmsys_ios::streamsize ssize_type;
  248. size_t const nnext = nleft > sizeof(buffer)? sizeof(buffer) : nleft;
  249. ssize_type const nnext_s = static_cast<ssize_type>(nnext);
  250. fin.read(buffer, nnext_s);
  251. // Some stream libraries (older HPUX) return failure at end of
  252. // file on the last read even if some data were read. Check
  253. // gcount instead of trusting the stream error status.
  254. if(static_cast<size_t>(fin.gcount()) != nnext)
  255. {
  256. break;
  257. }
  258. if(archive_write_data(this->Archive, buffer, nnext) != nnext_s)
  259. {
  260. this->Error = "archive_write_data: ";
  261. this->Error += archive_error_string(this->Archive);
  262. return false;
  263. }
  264. nleft -= nnext;
  265. }
  266. if(nleft > 0)
  267. {
  268. this->Error = "Error reading \"";
  269. this->Error += file;
  270. this->Error += "\": ";
  271. this->Error += cmSystemTools::GetLastSystemError();
  272. return false;
  273. }
  274. return true;
  275. }