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