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