#pragma once
#include "cmConfigure.h"
#include <cstddef>
#include <iosfwd>
#include <string>
#if defined(CMAKE_BOOTSTRAP)
# error "cmArchiveWrite not allowed during bootstrap build!"
#endif
template <typename T>
class cmArchiveWriteOptional
{
public:
cmArchiveWriteOptional() { this->Clear(); }
explicit cmArchiveWriteOptional(T val) { this->Set(val); }
void Set(T val)
{
this->IsValueSet = true;
this->Value = val;
}
void Clear() { this->IsValueSet = false; }
bool IsSet() const { return this->IsValueSet; }
T Get() const { return this->Value; }
private:
T Value;
bool IsValueSet;
};
class cmArchiveWrite
{
public:
enum Compress
{
CompressNone,
CompressCompress,
CompressGZip,
CompressBZip2,
CompressLZMA,
CompressXZ,
CompressZstd,
CompressPPMd,
};
cmArchiveWrite(std::ostream& os, Compress c = CompressNone,
std::string const& format = "paxr", int compressionLevel = 0,
int numThreads = 1);
~cmArchiveWrite();
cmArchiveWrite(cmArchiveWrite const&) = delete;
cmArchiveWrite& operator=(cmArchiveWrite const&) = delete;
bool Open();
bool Add(std::string path, size_t skip = 0, char const* prefix = nullptr,
bool recursive = true);
explicit operator bool() const { return this->Okay(); }
bool operator!() const { return !this->Okay(); }
std::string GetError() const { return this->Error; }
void SetVerbose(bool v) { this->Verbose = v; }
void SetMTime(std::string const& t) { this->MTime = t; }
void SetPermissions(int permissions_)
{
this->Permissions.Set(permissions_);
}
void ClearPermissions() { this->Permissions.Clear(); }
void SetPermissionsMask(int permissionsMask_)
{
this->PermissionsMask.Set(permissionsMask_);
}
void ClearPermissionsMask() { this->PermissionsMask.Clear(); }
void SetUIDAndGID(int uid_, int gid_)
{
this->Uid.Set(uid_);
this->Gid.Set(gid_);
}
void ClearUIDAndGID()
{
this->Uid.Clear();
this->Gid.Clear();
}
void SetUNAMEAndGNAME(std::string const& uname_, std::string const& gname_)
{
this->Uname = uname_;
this->Gname = gname_;
}
void ClearUNAMEAndGNAME()
{
this->Uname = "";
this->Gname = "";
}
private:
bool Okay() const { return this->Error.empty(); }
bool AddPath(char const* path, size_t skip, char const* prefix,
bool recursive = true);
bool AddFile(char const* file, size_t skip, char const* prefix);
bool AddData(char const* file, size_t size);
struct Callback;
friend struct Callback;
class Entry;
std::ostream& Stream;
struct archive* Archive;
struct archive* Disk;
bool Verbose = false;
std::string Format;
std::string Error;
std::string MTime;
cmArchiveWriteOptional<int> Uid;
cmArchiveWriteOptional<int> Gid;
std::string Uname;
std::string Gname;
cmArchiveWriteOptional<int> Permissions;
cmArchiveWriteOptional<int> PermissionsMask;
};