TarWriter: Set "00" to Ustar version field.

Most (maybe all?) tar commands can handle tar archives with blank
version fields, but POSIX requires "00" to be set to the field, so
doing it is good for compliance.

llvm-svn: 291479
This commit is contained in:
Rui Ueyama 2017-01-09 21:20:42 +00:00
parent 1559e8863e
commit a84ab073d9
1 changed files with 10 additions and 5 deletions

View File

@ -54,6 +54,13 @@ struct UstarHeader {
};
static_assert(sizeof(UstarHeader) == BlockSize, "invalid Ustar header");
static UstarHeader makeUstarHeader() {
UstarHeader Hdr = {};
memcpy(Hdr.Magic, "ustar", 5); // Ustar magic
memcpy(Hdr.Version, "00", 2); // Ustar version
return Hdr;
}
// A PAX attribute is in the form of "<length> <key>=<value>\n"
// where <length> is the length of the entire string including
// the length field itself. An example string is this.
@ -98,10 +105,9 @@ static void writePaxHeader(raw_fd_ostream &OS, StringRef Path) {
std::string PaxAttr = formatPax("path", Path);
// Create a 512-byte header.
UstarHeader Hdr = {};
UstarHeader Hdr = makeUstarHeader();
snprintf(Hdr.Size, sizeof(Hdr.Size), "%011zo", PaxAttr.size());
Hdr.TypeFlag = 'x'; // PAX magic
memcpy(Hdr.Magic, "ustar", 6); // Ustar magic
Hdr.TypeFlag = 'x'; // PAX magic
computeChecksum(Hdr);
// Write them down.
@ -138,11 +144,10 @@ static void writeUstarHeader(raw_fd_ostream &OS, StringRef Path, size_t Size) {
StringRef Name;
std::tie(Prefix, Name) = splitPath(Path);
UstarHeader Hdr = {};
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.Magic, "ustar", 6);
memcpy(Hdr.Prefix, Prefix.data(), Prefix.size());
computeChecksum(Hdr);
OS << StringRef(reinterpret_cast<char *>(&Hdr), sizeof(Hdr));