cmArchiveWrite.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #ifndef cmArchiveWrite_h
  11. #define cmArchiveWrite_h
  12. #include "cmStandardIncludes.h"
  13. #if !defined(CMAKE_BUILD_WITH_CMAKE)
  14. # error "cmArchiveWrite not allowed during bootstrap build!"
  15. #endif
  16. template<typename T>
  17. class cmArchiveWriteOptional
  18. {
  19. public:
  20. cmArchiveWriteOptional() {this->Clear();}
  21. explicit cmArchiveWriteOptional(T val) {this->Set(val);}
  22. void Set(T val) {this->IsValueSet = true; this->Value=val;}
  23. void Clear() {this->IsValueSet = false;}
  24. bool IsSet() const {return this->IsValueSet;}
  25. T Get() const {return Value;}
  26. private:
  27. T Value;
  28. bool IsValueSet;
  29. };
  30. /** \class cmArchiveWrite
  31. * \brief Wrapper around libarchive for writing.
  32. *
  33. */
  34. class cmArchiveWrite
  35. {
  36. typedef void (cmArchiveWrite::* safe_bool)();
  37. void safe_bool_true() {}
  38. public:
  39. /** Compression type. */
  40. enum Compress
  41. {
  42. CompressNone,
  43. CompressCompress,
  44. CompressGZip,
  45. CompressBZip2,
  46. CompressLZMA,
  47. CompressXZ
  48. };
  49. /** Construct with output stream to which to write archive. */
  50. cmArchiveWrite(std::ostream& os, Compress c = CompressNone,
  51. std::string const& format = "paxr");
  52. ~cmArchiveWrite();
  53. /**
  54. * Add a path (file or directory) to the archive. Directories are
  55. * added recursively. The "path" must be readable on disk, either
  56. * full path or relative to current working directory. The "skip"
  57. * value indicates how many leading bytes from the input path to
  58. * skip. The remaining part of the input path is appended to the
  59. * "prefix" value to construct the final name in the archive.
  60. */
  61. bool Add(std::string path,
  62. size_t skip = 0,
  63. const char* prefix = 0,
  64. bool recursive = true);
  65. /** Returns true if there has been no error. */
  66. operator safe_bool() const
  67. { return this->Okay()? &cmArchiveWrite::safe_bool_true : 0; }
  68. /** Returns true if there has been an error. */
  69. bool operator!() const { return !this->Okay(); }
  70. /** Return the error string; empty if none. */
  71. std::string GetError() const { return this->Error; }
  72. // TODO: More general callback instead of hard-coding calls to
  73. // std::cout.
  74. void SetVerbose(bool v) { this->Verbose = v; }
  75. void SetMTime(std::string const& t) { this->MTime = t; }
  76. //! Sets the permissions of the added files/folders
  77. void SetPermissions(mode_t permissions_)
  78. {
  79. this->Permissions.Set(permissions_);
  80. }
  81. //! Clears permissions - default is used instead
  82. void ClearPermissions() { this->Permissions.Clear(); }
  83. //! Sets the permissions mask of files/folders
  84. //!
  85. //! The permissions will be copied from the existing file
  86. //! or folder. The mask will then be applied to unset
  87. //! some of them
  88. void SetPermissionsMask(mode_t permissionsMask_)
  89. {
  90. this->PermissionsMask.Set(permissionsMask_);
  91. }
  92. //! Clears permissions mask - default is used instead
  93. void ClearPermissionsMask()
  94. {
  95. this->PermissionsMask.Clear();
  96. }
  97. //! Sets UID and GID to be used in the tar file
  98. void SetUIDAndGID(int uid_, int gid_)
  99. {
  100. this->Uid.Set(uid_);
  101. this->Gid.Set(gid_);
  102. }
  103. //! Clears UID and GID to be used in the tar file - default is used instead
  104. void ClearUIDAndGID()
  105. {
  106. this->Uid.Clear();
  107. this->Gid.Clear();
  108. }
  109. //! Sets UNAME and GNAME to be used in the tar file
  110. void SetUNAMEAndGNAME(const std::string& uname_, const std::string& gname_)
  111. {
  112. this->Uname = uname_;
  113. this->Gname = gname_;
  114. }
  115. //! Clears UNAME and GNAME to be used in the tar file
  116. //! default is used instead
  117. void ClearUNAMEAndGNAME()
  118. {
  119. this->Uname = "";
  120. this->Gname = "";
  121. }
  122. private:
  123. bool Okay() const { return this->Error.empty(); }
  124. bool AddPath(const char* path, size_t skip, const char* prefix,
  125. bool recursive = true);
  126. bool AddFile(const char* file, size_t skip, const char* prefix);
  127. bool AddData(const char* file, size_t size);
  128. struct Callback;
  129. friend struct Callback;
  130. class Entry;
  131. std::ostream& Stream;
  132. struct archive* Archive;
  133. struct archive* Disk;
  134. bool Verbose;
  135. std::string Format;
  136. std::string Error;
  137. std::string MTime;
  138. //! UID of the user in the tar file
  139. cmArchiveWriteOptional<int> Uid;
  140. //! GUID of the user in the tar file
  141. cmArchiveWriteOptional<int> Gid;
  142. //! UNAME/GNAME of the user (does not override UID/GID)
  143. //!@{
  144. std::string Uname;
  145. std::string Gname;
  146. //!@}
  147. //! Permissions on files/folders
  148. cmArchiveWriteOptional<mode_t> Permissions;
  149. cmArchiveWriteOptional<mode_t> PermissionsMask;
  150. };
  151. #endif