1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
11 #if defined(CMAKE_BOOTSTRAP)
12 # error "cmArchiveWrite not allowed during bootstrap build!"
16 class cmArchiveWriteOptional
19 cmArchiveWriteOptional() { this->Clear(); }
20 explicit cmArchiveWriteOptional(T val
) { this->Set(val
); }
24 this->IsValueSet
= true;
27 void Clear() { this->IsValueSet
= false; }
28 bool IsSet() const { return this->IsValueSet
; }
29 T
Get() const { return this->Value
; }
36 /** \class cmArchiveWrite
37 * \brief Wrapper around libarchive for writing.
43 /** Compression type. */
55 /** Construct with output stream to which to write archive. */
56 cmArchiveWrite(std::ostream
& os
, Compress c
= CompressNone
,
57 std::string
const& format
= "paxr", int compressionLevel
= 0,
62 cmArchiveWrite(const cmArchiveWrite
&) = delete;
63 cmArchiveWrite
& operator=(const cmArchiveWrite
&) = delete;
68 * Add a path (file or directory) to the archive. Directories are
69 * added recursively. The "path" must be readable on disk, either
70 * full path or relative to current working directory. The "skip"
71 * value indicates how many leading bytes from the input path to
72 * skip. The remaining part of the input path is appended to the
73 * "prefix" value to construct the final name in the archive.
75 bool Add(std::string path
, size_t skip
= 0, const char* prefix
= nullptr,
76 bool recursive
= true);
78 /** Returns true if there has been no error. */
79 explicit operator bool() const { return this->Okay(); }
81 /** Returns true if there has been an error. */
82 bool operator!() const { return !this->Okay(); }
84 /** Return the error string; empty if none. */
85 std::string
GetError() const { return this->Error
; }
87 // TODO: More general callback instead of hard-coding calls to
89 void SetVerbose(bool v
) { this->Verbose
= v
; }
91 void SetMTime(std::string
const& t
) { this->MTime
= t
; }
93 //! Sets the permissions of the added files/folders
94 void SetPermissions(int permissions_
)
96 this->Permissions
.Set(permissions_
);
99 //! Clears permissions - default is used instead
100 void ClearPermissions() { this->Permissions
.Clear(); }
102 //! Sets the permissions mask of files/folders
104 //! The permissions will be copied from the existing file
105 //! or folder. The mask will then be applied to unset
107 void SetPermissionsMask(int permissionsMask_
)
109 this->PermissionsMask
.Set(permissionsMask_
);
112 //! Clears permissions mask - default is used instead
113 void ClearPermissionsMask() { this->PermissionsMask
.Clear(); }
115 //! Sets UID and GID to be used in the tar file
116 void SetUIDAndGID(int uid_
, int gid_
)
122 //! Clears UID and GID to be used in the tar file - default is used instead
123 void ClearUIDAndGID()
129 //! Sets UNAME and GNAME to be used in the tar file
130 void SetUNAMEAndGNAME(const std::string
& uname_
, const std::string
& gname_
)
132 this->Uname
= uname_
;
133 this->Gname
= gname_
;
136 //! Clears UNAME and GNAME to be used in the tar file
137 //! default is used instead
138 void ClearUNAMEAndGNAME()
145 bool Okay() const { return this->Error
.empty(); }
146 bool AddPath(const char* path
, size_t skip
, const char* prefix
,
147 bool recursive
= true);
148 bool AddFile(const char* file
, size_t skip
, const char* prefix
);
149 bool AddData(const char* file
, size_t size
);
152 friend struct Callback
;
156 std::ostream
& Stream
;
157 struct archive
* Archive
;
158 struct archive
* Disk
;
159 bool Verbose
= false;
164 //! UID of the user in the tar file
165 cmArchiveWriteOptional
<int> Uid
;
167 //! GUID of the user in the tar file
168 cmArchiveWriteOptional
<int> Gid
;
170 //! UNAME/GNAME of the user (does not override UID/GID)
176 //! Permissions on files/folders
177 cmArchiveWriteOptional
<int> Permissions
;
178 cmArchiveWriteOptional
<int> PermissionsMask
;