cmake/Source/cmArchiveWrite.h

180 lines
4.6 KiB
C
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2021-09-14 00:13:48 +02:00
#pragma once
2010-11-13 01:00:53 +02:00
2017-07-20 19:35:53 +02:00
#include "cmConfigure.h" // IWYU pragma: keep
2016-10-30 18:24:19 +01:00
2020-02-01 23:06:01 +01:00
#include <cstddef>
2016-10-30 18:24:19 +01:00
#include <iosfwd>
#include <string>
2010-11-13 01:00:53 +02:00
2020-02-01 23:06:01 +01:00
#if defined(CMAKE_BOOTSTRAP)
2018-08-09 18:06:22 +02:00
# error "cmArchiveWrite not allowed during bootstrap build!"
2010-11-13 01:00:53 +02:00
#endif
2016-07-09 11:21:54 +02:00
template <typename T>
2015-11-17 17:22:37 +01:00
class cmArchiveWriteOptional
{
public:
2016-07-09 11:21:54 +02:00
cmArchiveWriteOptional() { this->Clear(); }
explicit cmArchiveWriteOptional(T val) { this->Set(val); }
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
void Set(T val)
{
this->IsValueSet = true;
this->Value = val;
}
void Clear() { this->IsValueSet = false; }
bool IsSet() const { return this->IsValueSet; }
2021-09-14 00:13:48 +02:00
T Get() const { return this->Value; }
2018-08-09 18:06:22 +02:00
2015-11-17 17:22:37 +01:00
private:
T Value;
bool IsValueSet;
};
2010-11-13 01:00:53 +02:00
/** \class cmArchiveWrite
* \brief Wrapper around libarchive for writing.
*
*/
class cmArchiveWrite
{
public:
/** Compression type. */
enum Compress
{
CompressNone,
CompressCompress,
CompressGZip,
CompressBZip2,
CompressLZMA,
2019-11-11 23:01:05 +01:00
CompressXZ,
CompressZstd
2010-11-13 01:00:53 +02:00
};
/** Construct with output stream to which to write archive. */
2015-08-17 11:37:30 +02:00
cmArchiveWrite(std::ostream& os, Compress c = CompressNone,
2021-09-14 00:13:48 +02:00
std::string const& format = "paxr", int compressionLevel = 0,
int numThreads = 1);
2015-08-17 11:37:30 +02:00
2010-11-13 01:00:53 +02:00
~cmArchiveWrite();
2019-11-11 23:01:05 +01:00
cmArchiveWrite(const cmArchiveWrite&) = delete;
cmArchiveWrite& operator=(const cmArchiveWrite&) = delete;
2020-08-30 11:54:41 +02:00
bool Open();
2010-11-13 01:00:53 +02:00
/**
* Add a path (file or directory) to the archive. Directories are
* added recursively. The "path" must be readable on disk, either
* full path or relative to current working directory. The "skip"
* value indicates how many leading bytes from the input path to
* skip. The remaining part of the input path is appended to the
* "prefix" value to construct the final name in the archive.
*/
2018-01-26 17:06:56 +01:00
bool Add(std::string path, size_t skip = 0, const char* prefix = nullptr,
2016-07-09 11:21:54 +02:00
bool recursive = true);
2010-11-13 01:00:53 +02:00
/** Returns true if there has been no error. */
2019-11-11 23:01:05 +01:00
explicit operator bool() const { return this->Okay(); }
2010-11-13 01:00:53 +02:00
/** Returns true if there has been an error. */
bool operator!() const { return !this->Okay(); }
/** Return the error string; empty if none. */
std::string GetError() const { return this->Error; }
// TODO: More general callback instead of hard-coding calls to
// std::cout.
void SetVerbose(bool v) { this->Verbose = v; }
2015-04-27 22:25:09 +02:00
void SetMTime(std::string const& t) { this->MTime = t; }
2015-11-17 17:22:37 +01:00
//! Sets the permissions of the added files/folders
2016-10-30 18:24:19 +01:00
void SetPermissions(int permissions_)
2016-07-09 11:21:54 +02:00
{
2015-11-17 17:22:37 +01:00
this->Permissions.Set(permissions_);
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
//! Clears permissions - default is used instead
void ClearPermissions() { this->Permissions.Clear(); }
//! Sets the permissions mask of files/folders
//!
//! The permissions will be copied from the existing file
//! or folder. The mask will then be applied to unset
//! some of them
2016-10-30 18:24:19 +01:00
void SetPermissionsMask(int permissionsMask_)
2016-07-09 11:21:54 +02:00
{
2015-11-17 17:22:37 +01:00
this->PermissionsMask.Set(permissionsMask_);
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
//! Clears permissions mask - default is used instead
2016-07-09 11:21:54 +02:00
void ClearPermissionsMask() { this->PermissionsMask.Clear(); }
2015-11-17 17:22:37 +01:00
//! Sets UID and GID to be used in the tar file
void SetUIDAndGID(int uid_, int gid_)
2016-07-09 11:21:54 +02:00
{
2015-11-17 17:22:37 +01:00
this->Uid.Set(uid_);
this->Gid.Set(gid_);
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
//! Clears UID and GID to be used in the tar file - default is used instead
void ClearUIDAndGID()
2016-07-09 11:21:54 +02:00
{
2015-11-17 17:22:37 +01:00
this->Uid.Clear();
this->Gid.Clear();
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
//! Sets UNAME and GNAME to be used in the tar file
void SetUNAMEAndGNAME(const std::string& uname_, const std::string& gname_)
2016-07-09 11:21:54 +02:00
{
2015-11-17 17:22:37 +01:00
this->Uname = uname_;
this->Gname = gname_;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
//! Clears UNAME and GNAME to be used in the tar file
//! default is used instead
void ClearUNAMEAndGNAME()
2016-07-09 11:21:54 +02:00
{
2015-11-17 17:22:37 +01:00
this->Uname = "";
this->Gname = "";
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2010-11-13 01:00:53 +02:00
private:
bool Okay() const { return this->Error.empty(); }
2015-11-17 17:22:37 +01:00
bool AddPath(const char* path, size_t skip, const char* prefix,
bool recursive = true);
2010-11-13 01:00:53 +02:00
bool AddFile(const char* file, size_t skip, const char* prefix);
bool AddData(const char* file, size_t size);
struct Callback;
friend struct Callback;
class Entry;
std::ostream& Stream;
struct archive* Archive;
struct archive* Disk;
bool Verbose;
2015-11-17 17:22:37 +01:00
std::string Format;
2010-11-13 01:00:53 +02:00
std::string Error;
2015-04-27 22:25:09 +02:00
std::string MTime;
2015-11-17 17:22:37 +01:00
//! UID of the user in the tar file
cmArchiveWriteOptional<int> Uid;
//! GUID of the user in the tar file
cmArchiveWriteOptional<int> Gid;
//! UNAME/GNAME of the user (does not override UID/GID)
//!@{
std::string Uname;
std::string Gname;
//!@}
//! Permissions on files/folders
2016-10-30 18:24:19 +01:00
cmArchiveWriteOptional<int> Permissions;
cmArchiveWriteOptional<int> PermissionsMask;
2010-11-13 01:00:53 +02:00
};