Deleted Added
full compact
TarWriter.cpp (311833) TarWriter.cpp (312197)
1//===-- TarWriter.cpp - Tar archive file creator --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//

--- 40 unchanged lines hidden (view full) ---

49 char Gname[32];
50 char DevMajor[8];
51 char DevMinor[8];
52 char Prefix[155];
53 char Pad[12];
54};
55static_assert(sizeof(UstarHeader) == BlockSize, "invalid Ustar header");
56
1//===-- TarWriter.cpp - Tar archive file creator --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//

--- 40 unchanged lines hidden (view full) ---

49 char Gname[32];
50 char DevMajor[8];
51 char DevMinor[8];
52 char Prefix[155];
53 char Pad[12];
54};
55static_assert(sizeof(UstarHeader) == BlockSize, "invalid Ustar header");
56
57static UstarHeader makeUstarHeader() {
58 UstarHeader Hdr = {};
59 memcpy(Hdr.Magic, "ustar", 5); // Ustar magic
60 memcpy(Hdr.Version, "00", 2); // Ustar version
61 return Hdr;
62}
63
57// A PAX attribute is in the form of "<length> <key>=<value>\n"
58// where <length> is the length of the entire string including
59// the length field itself. An example string is this.
60//
61// 25 ctime=1084839148.1212\n
62//
63// This function create such string.
64static std::string formatPax(StringRef Key, StringRef Val) {

--- 28 unchanged lines hidden (view full) ---

93
94// Create a tar header and write it to a given output stream.
95static void writePaxHeader(raw_fd_ostream &OS, StringRef Path) {
96 // A PAX header consists of a 512-byte header followed
97 // by key-value strings. First, create key-value strings.
98 std::string PaxAttr = formatPax("path", Path);
99
100 // Create a 512-byte header.
64// A PAX attribute is in the form of "<length> <key>=<value>\n"
65// where <length> is the length of the entire string including
66// the length field itself. An example string is this.
67//
68// 25 ctime=1084839148.1212\n
69//
70// This function create such string.
71static std::string formatPax(StringRef Key, StringRef Val) {

--- 28 unchanged lines hidden (view full) ---

100
101// Create a tar header and write it to a given output stream.
102static void writePaxHeader(raw_fd_ostream &OS, StringRef Path) {
103 // A PAX header consists of a 512-byte header followed
104 // by key-value strings. First, create key-value strings.
105 std::string PaxAttr = formatPax("path", Path);
106
107 // Create a 512-byte header.
101 UstarHeader Hdr = {};
108 UstarHeader Hdr = makeUstarHeader();
102 snprintf(Hdr.Size, sizeof(Hdr.Size), "%011zo", PaxAttr.size());
109 snprintf(Hdr.Size, sizeof(Hdr.Size), "%011zo", PaxAttr.size());
103 Hdr.TypeFlag = 'x'; // PAX magic
104 memcpy(Hdr.Magic, "ustar", 6); // Ustar magic
110 Hdr.TypeFlag = 'x'; // PAX magic
105 computeChecksum(Hdr);
106
107 // Write them down.
108 OS << StringRef(reinterpret_cast<char *>(&Hdr), sizeof(Hdr));
109 OS << PaxAttr;
110 pad(OS);
111}
112
113// In the Ustar header, a path can be split at any '/' to store
114// a path into UstarHeader::Name and UstarHeader::Prefix. This
115// function splits a given path for that purpose.
116static std::pair<StringRef, StringRef> splitPath(StringRef Path) {
117 if (Path.size() <= sizeof(UstarHeader::Name))
118 return {"", Path};
111 computeChecksum(Hdr);
112
113 // Write them down.
114 OS << StringRef(reinterpret_cast<char *>(&Hdr), sizeof(Hdr));
115 OS << PaxAttr;
116 pad(OS);
117}
118
119// In the Ustar header, a path can be split at any '/' to store
120// a path into UstarHeader::Name and UstarHeader::Prefix. This
121// function splits a given path for that purpose.
122static std::pair<StringRef, StringRef> splitPath(StringRef Path) {
123 if (Path.size() <= sizeof(UstarHeader::Name))
124 return {"", Path};
119 size_t Sep = Path.rfind('/', sizeof(UstarHeader::Name) + 1);
125 size_t Sep = Path.rfind('/', sizeof(UstarHeader::Prefix) + 1);
120 if (Sep == StringRef::npos)
121 return {"", Path};
122 return {Path.substr(0, Sep), Path.substr(Sep + 1)};
123}
124
125// Returns true if a given path can be stored to a Ustar header
126// without the PAX extension.
127static bool fitsInUstar(StringRef Path) {

--- 5 unchanged lines hidden (view full) ---

133
134// The PAX header is an extended format, so a PAX header needs
135// to be followed by a "real" header.
136static void writeUstarHeader(raw_fd_ostream &OS, StringRef Path, size_t Size) {
137 StringRef Prefix;
138 StringRef Name;
139 std::tie(Prefix, Name) = splitPath(Path);
140
126 if (Sep == StringRef::npos)
127 return {"", Path};
128 return {Path.substr(0, Sep), Path.substr(Sep + 1)};
129}
130
131// Returns true if a given path can be stored to a Ustar header
132// without the PAX extension.
133static bool fitsInUstar(StringRef Path) {

--- 5 unchanged lines hidden (view full) ---

139
140// The PAX header is an extended format, so a PAX header needs
141// to be followed by a "real" header.
142static void writeUstarHeader(raw_fd_ostream &OS, StringRef Path, size_t Size) {
143 StringRef Prefix;
144 StringRef Name;
145 std::tie(Prefix, Name) = splitPath(Path);
146
141 UstarHeader Hdr = {};
147 UstarHeader Hdr = makeUstarHeader();
142 memcpy(Hdr.Name, Name.data(), Name.size());
143 memcpy(Hdr.Mode, "0000664", 8);
144 snprintf(Hdr.Size, sizeof(Hdr.Size), "%011zo", Size);
148 memcpy(Hdr.Name, Name.data(), Name.size());
149 memcpy(Hdr.Mode, "0000664", 8);
150 snprintf(Hdr.Size, sizeof(Hdr.Size), "%011zo", Size);
145 memcpy(Hdr.Magic, "ustar", 6);
146 memcpy(Hdr.Prefix, Prefix.data(), Prefix.size());
147 computeChecksum(Hdr);
148 OS << StringRef(reinterpret_cast<char *>(&Hdr), sizeof(Hdr));
149}
150
151// Creates a TarWriter instance and returns it.
152Expected<std::unique_ptr<TarWriter>> TarWriter::create(StringRef OutputPath,
153 StringRef BaseDir) {

--- 31 unchanged lines hidden ---
151 memcpy(Hdr.Prefix, Prefix.data(), Prefix.size());
152 computeChecksum(Hdr);
153 OS << StringRef(reinterpret_cast<char *>(&Hdr), sizeof(Hdr));
154}
155
156// Creates a TarWriter instance and returns it.
157Expected<std::unique_ptr<TarWriter>> TarWriter::create(StringRef OutputPath,
158 StringRef BaseDir) {

--- 31 unchanged lines hidden ---