cmArchiveWrite.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. };
  47. /** Construct with output stream to which to write archive. */
  48. cmArchiveWrite(std::ostream& os, Compress c = CompressNone,
  49. std::string const& format = "paxr");
  50. ~cmArchiveWrite();
  51. cmArchiveWrite(const cmArchiveWrite&) = delete;
  52. cmArchiveWrite& operator=(const cmArchiveWrite&) = delete;
  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. explicit operator bool() const { return this->Okay(); }
  65. /** Returns true if there has been an error. */
  66. bool operator!() const { return !this->Okay(); }
  67. /** Return the error string; empty if none. */
  68. std::string GetError() const { return this->Error; }
  69. // TODO: More general callback instead of hard-coding calls to
  70. // std::cout.
  71. void SetVerbose(bool v) { this->Verbose = v; }
  72. void SetMTime(std::string const& t) { this->MTime = t; }
  73. //! Sets the permissions of the added files/folders
  74. void SetPermissions(int permissions_)
  75. {
  76. this->Permissions.Set(permissions_);
  77. }
  78. //! Clears permissions - default is used instead
  79. void ClearPermissions() { this->Permissions.Clear(); }
  80. //! Sets the permissions mask of files/folders
  81. //!
  82. //! The permissions will be copied from the existing file
  83. //! or folder. The mask will then be applied to unset
  84. //! some of them
  85. void SetPermissionsMask(int permissionsMask_)
  86. {
  87. this->PermissionsMask.Set(permissionsMask_);
  88. }
  89. //! Clears permissions mask - default is used instead
  90. void ClearPermissionsMask() { this->PermissionsMask.Clear(); }
  91. //! Sets UID and GID to be used in the tar file
  92. void SetUIDAndGID(int uid_, int gid_)
  93. {
  94. this->Uid.Set(uid_);
  95. this->Gid.Set(gid_);
  96. }
  97. //! Clears UID and GID to be used in the tar file - default is used instead
  98. void ClearUIDAndGID()
  99. {
  100. this->Uid.Clear();
  101. this->Gid.Clear();
  102. }
  103. //! Sets UNAME and GNAME to be used in the tar file
  104. void SetUNAMEAndGNAME(const std::string& uname_, const std::string& gname_)
  105. {
  106. this->Uname = uname_;
  107. this->Gname = gname_;
  108. }
  109. //! Clears UNAME and GNAME to be used in the tar file
  110. //! default is used instead
  111. void ClearUNAMEAndGNAME()
  112. {
  113. this->Uname = "";
  114. this->Gname = "";
  115. }
  116. private:
  117. bool Okay() const { return this->Error.empty(); }
  118. bool AddPath(const char* path, size_t skip, const char* prefix,
  119. bool recursive = true);
  120. bool AddFile(const char* file, size_t skip, const char* prefix);
  121. bool AddData(const char* file, size_t size);
  122. struct Callback;
  123. friend struct Callback;
  124. class Entry;
  125. std::ostream& Stream;
  126. struct archive* Archive;
  127. struct archive* Disk;
  128. bool Verbose;
  129. std::string Format;
  130. std::string Error;
  131. std::string MTime;
  132. //! UID of the user in the tar file
  133. cmArchiveWriteOptional<int> Uid;
  134. //! GUID of the user in the tar file
  135. cmArchiveWriteOptional<int> Gid;
  136. //! UNAME/GNAME of the user (does not override UID/GID)
  137. //!@{
  138. std::string Uname;
  139. std::string Gname;
  140. //!@}
  141. //! Permissions on files/folders
  142. cmArchiveWriteOptional<int> Permissions;
  143. cmArchiveWriteOptional<int> PermissionsMask;
  144. };
  145. #endif