cmArchiveWrite.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "cmLocale.h"
  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<std::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(
  73. std::ostream& os, Compress c, std::string const& format):
  74. Stream(os),
  75. Archive(archive_write_new()),
  76. Disk(archive_read_disk_new()),
  77. Verbose(false),
  78. Format(format)
  79. {
  80. switch (c)
  81. {
  82. case CompressNone:
  83. if(archive_write_add_filter_none(this->Archive) != ARCHIVE_OK)
  84. {
  85. this->Error = "archive_write_add_filter_none: ";
  86. this->Error += cm_archive_error_string(this->Archive);
  87. return;
  88. }
  89. break;
  90. case CompressCompress:
  91. if(archive_write_add_filter_compress(this->Archive) != ARCHIVE_OK)
  92. {
  93. this->Error = "archive_write_add_filter_compress: ";
  94. this->Error += cm_archive_error_string(this->Archive);
  95. return;
  96. }
  97. break;
  98. case CompressGZip:
  99. if(archive_write_add_filter_gzip(this->Archive) != ARCHIVE_OK)
  100. {
  101. this->Error = "archive_write_add_filter_gzip: ";
  102. this->Error += cm_archive_error_string(this->Archive);
  103. return;
  104. }
  105. break;
  106. case CompressBZip2:
  107. if(archive_write_add_filter_bzip2(this->Archive) != ARCHIVE_OK)
  108. {
  109. this->Error = "archive_write_add_filter_bzip2: ";
  110. this->Error += cm_archive_error_string(this->Archive);
  111. return;
  112. }
  113. break;
  114. case CompressLZMA:
  115. if(archive_write_add_filter_lzma(this->Archive) != ARCHIVE_OK)
  116. {
  117. this->Error = "archive_write_add_filter_lzma: ";
  118. this->Error += cm_archive_error_string(this->Archive);
  119. return;
  120. }
  121. break;
  122. case CompressXZ:
  123. if(archive_write_add_filter_xz(this->Archive) != ARCHIVE_OK)
  124. {
  125. this->Error = "archive_write_add_filter_xz: ";
  126. this->Error += cm_archive_error_string(this->Archive);
  127. return;
  128. }
  129. break;
  130. };
  131. #if !defined(_WIN32) || defined(__CYGWIN__)
  132. if (archive_read_disk_set_standard_lookup(this->Disk) != ARCHIVE_OK)
  133. {
  134. this->Error = "archive_read_disk_set_standard_lookup: ";
  135. this->Error += cm_archive_error_string(this->Archive);
  136. return;
  137. }
  138. #endif
  139. if(archive_write_set_format_by_name(this->Archive, format.c_str())
  140. != ARCHIVE_OK)
  141. {
  142. this->Error = "archive_write_set_format_by_name: ";
  143. this->Error += cm_archive_error_string(this->Archive);
  144. return;
  145. }
  146. // do not pad the last block!!
  147. if (archive_write_set_bytes_in_last_block(this->Archive, 1))
  148. {
  149. this->Error = "archive_write_set_bytes_in_last_block: ";
  150. this->Error += cm_archive_error_string(this->Archive);
  151. return;
  152. }
  153. if(archive_write_open(
  154. this->Archive, this, 0,
  155. reinterpret_cast<archive_write_callback*>(&Callback::Write),
  156. 0) != ARCHIVE_OK)
  157. {
  158. this->Error = "archive_write_open: ";
  159. this->Error += cm_archive_error_string(this->Archive);
  160. return;
  161. }
  162. }
  163. //----------------------------------------------------------------------------
  164. cmArchiveWrite::~cmArchiveWrite()
  165. {
  166. archive_read_free(this->Disk);
  167. archive_write_free(this->Archive);
  168. }
  169. //----------------------------------------------------------------------------
  170. bool cmArchiveWrite::Add(std::string path,
  171. size_t skip,
  172. const char* prefix,
  173. bool recursive)
  174. {
  175. if(this->Okay())
  176. {
  177. if(!path.empty() && path[path.size()-1] == '/')
  178. {
  179. path.erase(path.size()-1);
  180. }
  181. this->AddPath(path.c_str(), skip, prefix, recursive);
  182. }
  183. return this->Okay();
  184. }
  185. //----------------------------------------------------------------------------
  186. bool cmArchiveWrite::AddPath(const char* path,
  187. size_t skip, const char* prefix,
  188. bool recursive)
  189. {
  190. if(!this->AddFile(path, skip, prefix))
  191. {
  192. return false;
  193. }
  194. if((!cmSystemTools::FileIsDirectory(path) || !recursive) ||
  195. cmSystemTools::FileIsSymlink(path))
  196. {
  197. return true;
  198. }
  199. cmsys::Directory d;
  200. if(d.Load(path))
  201. {
  202. std::string next = path;
  203. next += "/";
  204. std::string::size_type end = next.size();
  205. unsigned long n = d.GetNumberOfFiles();
  206. for(unsigned long i = 0; i < n; ++i)
  207. {
  208. const char* file = d.GetFile(i);
  209. if(strcmp(file, ".") != 0 && strcmp(file, "..") != 0)
  210. {
  211. next.erase(end);
  212. next += file;
  213. if(!this->AddPath(next.c_str(), skip, prefix))
  214. {
  215. return false;
  216. }
  217. }
  218. }
  219. }
  220. return true;
  221. }
  222. //----------------------------------------------------------------------------
  223. bool cmArchiveWrite::AddFile(const char* file,
  224. size_t skip, const char* prefix)
  225. {
  226. // Skip the file if we have no name for it. This may happen on a
  227. // top-level directory, which does not need to be included anyway.
  228. if(skip >= strlen(file))
  229. {
  230. return true;
  231. }
  232. const char* out = file + skip;
  233. cmLocaleRAII localeRAII;
  234. static_cast<void>(localeRAII);
  235. // Meta-data.
  236. std::string dest = prefix? prefix : "";
  237. dest += out;
  238. if(this->Verbose)
  239. {
  240. std::cout << dest << "\n";
  241. }
  242. Entry e;
  243. cm_archive_entry_copy_sourcepath(e, file);
  244. cm_archive_entry_copy_pathname(e, dest);
  245. if(archive_read_disk_entry_from_file(this->Disk, e, -1, 0) != ARCHIVE_OK)
  246. {
  247. this->Error = "archive_read_disk_entry_from_file '";
  248. this->Error += file;
  249. this->Error += "': ";
  250. this->Error += cm_archive_error_string(this->Disk);
  251. return false;
  252. }
  253. if (!this->MTime.empty())
  254. {
  255. time_t now;
  256. time(&now);
  257. time_t t = cm_get_date(now, this->MTime.c_str());
  258. if (t == -1)
  259. {
  260. this->Error = "unable to parse mtime '";
  261. this->Error += this->MTime;
  262. this->Error += "'";
  263. return false;
  264. }
  265. archive_entry_set_mtime(e, t, 0);
  266. }
  267. // manages the uid/guid of the entry (if any)
  268. if (this->Uid.IsSet() && this->Gid.IsSet())
  269. {
  270. archive_entry_set_uid(e, this->Uid.Get());
  271. archive_entry_set_gid(e, this->Gid.Get());
  272. }
  273. if (this->Uname.size() && this->Gname.size())
  274. {
  275. archive_entry_set_uname(e, this->Uname.c_str());
  276. archive_entry_set_gname(e, this->Gname.c_str());
  277. }
  278. // manages the permissions
  279. if (this->Permissions.IsSet())
  280. {
  281. archive_entry_set_perm(e, this->Permissions.Get());
  282. }
  283. if (this->PermissionsMask.IsSet())
  284. {
  285. mode_t perm = archive_entry_perm(e);
  286. archive_entry_set_perm(e, perm & this->PermissionsMask.Get());
  287. }
  288. // Clear acl and xattr fields not useful for distribution.
  289. archive_entry_acl_clear(e);
  290. archive_entry_xattr_clear(e);
  291. archive_entry_set_fflags(e, 0, 0);
  292. if (this->Format == "pax" || this->Format == "paxr")
  293. {
  294. // Sparse files are a GNU tar extension.
  295. // Do not use them in standard tar files.
  296. archive_entry_sparse_clear(e);
  297. }
  298. if(archive_write_header(this->Archive, e) != ARCHIVE_OK)
  299. {
  300. this->Error = "archive_write_header: ";
  301. this->Error += cm_archive_error_string(this->Archive);
  302. return false;
  303. }
  304. // do not copy content of symlink
  305. if (!archive_entry_symlink(e))
  306. {
  307. // Content.
  308. if(size_t size = static_cast<size_t>(archive_entry_size(e)))
  309. {
  310. return this->AddData(file, size);
  311. }
  312. }
  313. return true;
  314. }
  315. //----------------------------------------------------------------------------
  316. bool cmArchiveWrite::AddData(const char* file, size_t size)
  317. {
  318. cmsys::ifstream fin(file, std::ios::in | std::ios::binary);
  319. if(!fin)
  320. {
  321. this->Error = "Error opening \"";
  322. this->Error += file;
  323. this->Error += "\": ";
  324. this->Error += cmSystemTools::GetLastSystemError();
  325. return false;
  326. }
  327. char buffer[16384];
  328. size_t nleft = size;
  329. while(nleft > 0)
  330. {
  331. typedef std::streamsize ssize_type;
  332. size_t const nnext = nleft > sizeof(buffer)? sizeof(buffer) : nleft;
  333. ssize_type const nnext_s = static_cast<ssize_type>(nnext);
  334. fin.read(buffer, nnext_s);
  335. // Some stream libraries (older HPUX) return failure at end of
  336. // file on the last read even if some data were read. Check
  337. // gcount instead of trusting the stream error status.
  338. if(static_cast<size_t>(fin.gcount()) != nnext)
  339. {
  340. break;
  341. }
  342. if(archive_write_data(this->Archive, buffer, nnext) != nnext_s)
  343. {
  344. this->Error = "archive_write_data: ";
  345. this->Error += cm_archive_error_string(this->Archive);
  346. return false;
  347. }
  348. nleft -= nnext;
  349. }
  350. if(nleft > 0)
  351. {
  352. this->Error = "Error reading \"";
  353. this->Error += file;
  354. this->Error += "\": ";
  355. this->Error += cmSystemTools::GetLastSystemError();
  356. return false;
  357. }
  358. return true;
  359. }