#include "llvm/Support/TarWriter.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Path.h"
using namespace llvm;
static const int BlockSize = 512;
struct UstarHeader {
char Name[100];
char Mode[8];
char Uid[8];
char Gid[8];
char Size[12];
char Mtime[12];
char Checksum[8];
char TypeFlag;
char Linkname[100];
char Magic[6];
char Version[2];
char Uname[32];
char Gname[32];
char DevMajor[8];
char DevMinor[8];
char Prefix[155];
char Pad[12];
};
static_assert(sizeof(UstarHeader) == BlockSize, "invalid Ustar header");
static UstarHeader makeUstarHeader() {
UstarHeader Hdr = {};
memcpy(Hdr.Magic, "ustar", 5); memcpy(Hdr.Version, "00", 2); return Hdr;
}
static std::string formatPax(StringRef Key, StringRef Val) {
int Len = Key.size() + Val.size() + 3;
int Total = Len + Twine(Len).str().size();
Total = Len + Twine(Total).str().size();
return (Twine(Total) + " " + Key + "=" + Val + "\n").str();
}
static void pad(raw_fd_ostream &OS) {
uint64_t Pos = OS.tell();
OS.seek(alignTo(Pos, BlockSize));
}
static void computeChecksum(UstarHeader &Hdr) {
memset(Hdr.Checksum, ' ', sizeof(Hdr.Checksum));
unsigned Chksum = 0;
for (size_t I = 0; I < sizeof(Hdr); ++I)
Chksum += reinterpret_cast<uint8_t *>(&Hdr)[I];
snprintf(Hdr.Checksum, sizeof(Hdr.Checksum), "%06o", Chksum);
}
static void writePaxHeader(raw_fd_ostream &OS, StringRef Path) {
std::string PaxAttr = formatPax("path", Path);
UstarHeader Hdr = makeUstarHeader();
snprintf(Hdr.Size, sizeof(Hdr.Size), "%011zo", PaxAttr.size());
Hdr.TypeFlag = 'x'; computeChecksum(Hdr);
OS << StringRef(reinterpret_cast<char *>(&Hdr), sizeof(Hdr));
OS << PaxAttr;
pad(OS);
}
static bool splitUstar(StringRef Path, StringRef &Prefix, StringRef &Name) {
if (Path.size() < sizeof(UstarHeader::Name)) {
Prefix = "";
Name = Path;
return true;
}
const int MaxPrefix = 137;
size_t Sep = Path.rfind('/', MaxPrefix + 1);
if (Sep == StringRef::npos)
return false;
if (Path.size() - Sep - 1 >= sizeof(UstarHeader::Name))
return false;
Prefix = Path.substr(0, Sep);
Name = Path.substr(Sep + 1);
return true;
}
static void writeUstarHeader(raw_fd_ostream &OS, StringRef Prefix,
StringRef Name, size_t Size) {
UstarHeader Hdr = makeUstarHeader();
memcpy(Hdr.Name, Name.data(), Name.size());
memcpy(Hdr.Mode, "0000664", 8);
snprintf(Hdr.Size, sizeof(Hdr.Size), "%011zo", Size);
memcpy(Hdr.Prefix, Prefix.data(), Prefix.size());
computeChecksum(Hdr);
OS << StringRef(reinterpret_cast<char *>(&Hdr), sizeof(Hdr));
}
Expected<std::unique_ptr<TarWriter>> TarWriter::create(StringRef OutputPath,
StringRef BaseDir) {
using namespace sys::fs;
int FD;
if (std::error_code EC =
openFileForWrite(OutputPath, FD, CD_CreateAlways, OF_None))
return make_error<StringError>("cannot open " + OutputPath, EC);
return std::unique_ptr<TarWriter>(new TarWriter(FD, BaseDir));
}
TarWriter::TarWriter(int FD, StringRef BaseDir)
: OS(FD, true, false),
BaseDir(std::string(BaseDir)) {}
void TarWriter::append(StringRef Path, StringRef Data) {
std::string Fullpath = BaseDir + "/" + sys::path::convert_to_slash(Path);
if (!Files.insert(Fullpath).second)
return;
StringRef Prefix;
StringRef Name;
if (splitUstar(Fullpath, Prefix, Name)) {
writeUstarHeader(OS, Prefix, Name, Data.size());
} else {
writePaxHeader(OS, Fullpath);
writeUstarHeader(OS, "", "", Data.size());
}
OS << Data;
pad(OS);
uint64_t Pos = OS.tell();
OS << std::string(BlockSize * 2, '\0');
OS.seek(Pos);
OS.flush();
}