cmArchiveWrite.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmArchiveWrite_h
  4. #define cmArchiveWrite_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <iosfwd>
  7. #include <stddef.h>
  8. #include <string>
  9. #if !defined(CMAKE_BUILD_WITH_CMAKE)
  10. #error "cmArchiveWrite not allowed during bootstrap build!"
  11. #endif
  12. template <typename T>
  13. class cmArchiveWriteOptional
  14. {
  15. public:
  16. cmArchiveWriteOptional() { this->Clear(); }
  17. explicit cmArchiveWriteOptional(T val) { this->Set(val); }
  18. void Set(T val)
  19. {
  20. this->IsValueSet = true;
  21. this->Value = val;
  22. }
  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, size_t skip = 0, const char* prefix = nullptr,
  62. bool recursive = true);
  63. /** Returns true if there has been no error. */
  64. operator safe_bool() const
  65. {
  66. return this->Okay() ? &cmArchiveWrite::safe_bool_true : nullptr;
  67. }
  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(int 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(int permissionsMask_)
  89. {
  90. this->PermissionsMask.Set(permissionsMask_);
  91. }
  92. //! Clears permissions mask - default is used instead
  93. void ClearPermissionsMask() { this->PermissionsMask.Clear(); }
  94. //! Sets UID and GID to be used in the tar file
  95. void SetUIDAndGID(int uid_, int gid_)
  96. {
  97. this->Uid.Set(uid_);
  98. this->Gid.Set(gid_);
  99. }
  100. //! Clears UID and GID to be used in the tar file - default is used instead
  101. void ClearUIDAndGID()
  102. {
  103. this->Uid.Clear();
  104. this->Gid.Clear();
  105. }
  106. //! Sets UNAME and GNAME to be used in the tar file
  107. void SetUNAMEAndGNAME(const std::string& uname_, const std::string& gname_)
  108. {
  109. this->Uname = uname_;
  110. this->Gname = gname_;
  111. }
  112. //! Clears UNAME and GNAME to be used in the tar file
  113. //! default is used instead
  114. void ClearUNAMEAndGNAME()
  115. {
  116. this->Uname = "";
  117. this->Gname = "";
  118. }
  119. private:
  120. bool Okay() const { return this->Error.empty(); }
  121. bool AddPath(const char* path, size_t skip, const char* prefix,
  122. bool recursive = true);
  123. bool AddFile(const char* file, size_t skip, const char* prefix);
  124. bool AddData(const char* file, size_t size);
  125. struct Callback;
  126. friend struct Callback;
  127. class Entry;
  128. std::ostream& Stream;
  129. struct archive* Archive;
  130. struct archive* Disk;
  131. bool Verbose;
  132. std::string Format;
  133. std::string Error;
  134. std::string MTime;
  135. //! UID of the user in the tar file
  136. cmArchiveWriteOptional<int> Uid;
  137. //! GUID of the user in the tar file
  138. cmArchiveWriteOptional<int> Gid;
  139. //! UNAME/GNAME of the user (does not override UID/GID)
  140. //!@{
  141. std::string Uname;
  142. std::string Gname;
  143. //!@}
  144. //! Permissions on files/folders
  145. cmArchiveWriteOptional<int> Permissions;
  146. cmArchiveWriteOptional<int> PermissionsMask;
  147. };
  148. #endif