Index: llvm/lib/Support/TarWriter.cpp =================================================================== --- llvm/lib/Support/TarWriter.cpp +++ llvm/lib/Support/TarWriter.cpp @@ -116,34 +116,36 @@ pad(OS); } -// In the Ustar header, a path can be split at any '/' to store -// a path into UstarHeader::Name and UstarHeader::Prefix. This -// function splits a given path for that purpose. -static std::pair splitPath(StringRef Path) { - if (Path.size() <= sizeof(UstarHeader::Name)) - return {"", Path}; +// Path fits in a Ustar header if +// +// - Path is is less than 100 characters long or, +// - Path is in the form of "/" where is less +// than or equal to 155 characters long and is less than 100 +// characters long. +// +// If Path fits in a Ustar header, updates Prefix and Name and returns true. +// Otherwise, returns false. +static bool splitUstar(StringRef Path, std::string &Prefix, std::string &Name) { + if (Path.size() < sizeof(UstarHeader::Name)) { + Name = Path; + return true; + } + size_t Sep = Path.rfind('/', sizeof(UstarHeader::Prefix) + 1); if (Sep == StringRef::npos) - return {"", Path}; - return {Path.substr(0, Sep), Path.substr(Sep + 1)}; -} + return false; + if (Path.size() - Sep - 1 >= sizeof(UstarHeader::Name)) + return false; -// Returns true if a given path can be stored to a Ustar header -// without the PAX extension. -static bool fitsInUstar(StringRef Path) { - StringRef Prefix; - StringRef Name; - std::tie(Prefix, Name) = splitPath(Path); - return Name.size() <= sizeof(UstarHeader::Name); + Prefix = Path.substr(0, Sep); + Name = Path.substr(Sep + 1); + return true; } // The PAX header is an extended format, so a PAX header needs // to be followed by a "real" header. -static void writeUstarHeader(raw_fd_ostream &OS, StringRef Path, size_t Size) { - StringRef Prefix; - StringRef Name; - std::tie(Prefix, Name) = splitPath(Path); - +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); @@ -168,12 +170,15 @@ // Append a given file to an archive. void TarWriter::append(StringRef Path, StringRef Data) { // Write Path and Data. - std::string S = BaseDir + "/" + sys::path::convert_to_slash(Path) + "\0"; - if (fitsInUstar(S)) { - writeUstarHeader(OS, S, Data.size()); + std::string Fullpath = BaseDir + "/" + sys::path::convert_to_slash(Path); + + std::string Prefix; + std::string Name; + if (splitUstar(Fullpath, Prefix, Name)) { + writeUstarHeader(OS, Prefix, Name, Data.size()); } else { - writePaxHeader(OS, S); - writeUstarHeader(OS, "", Data.size()); + writePaxHeader(OS, Fullpath); + writeUstarHeader(OS, "", "", Data.size()); } OS << Data; Index: llvm/unittests/Support/TarWriterTest.cpp =================================================================== --- llvm/unittests/Support/TarWriterTest.cpp +++ llvm/unittests/Support/TarWriterTest.cpp @@ -68,21 +68,19 @@ } TEST_F(TarWriterTest, LongFilename) { - UstarHeader Hdr1 = create( - "012345678", std::string(99, 'x') + "/" + std::string(44, 'x') + "/foo"); - EXPECT_EQ("foo", StringRef(Hdr1.Name)); - EXPECT_EQ("012345678/" + std::string(99, 'x') + "/" + std::string(44, 'x'), - StringRef(Hdr1.Prefix)); + UstarHeader Hdr1 = + create("", std::string(154, 'x') + "/" + std::string(99, 'y')); + EXPECT_EQ("/" + std::string(154, 'x'), StringRef(Hdr1.Prefix)); + EXPECT_EQ(std::string(99, 'y'), StringRef(Hdr1.Name)); - UstarHeader Hdr2 = create( - "012345678", std::string(99, 'x') + "/" + std::string(45, 'x') + "/foo"); - EXPECT_EQ("foo", StringRef(Hdr2.Name)); - EXPECT_EQ("012345678/" + std::string(99, 'x') + "/" + std::string(45, 'x'), - StringRef(Hdr2.Prefix)); + UstarHeader Hdr2 = + create("", std::string(155, 'x') + "/" + std::string(99, 'y')); + EXPECT_EQ("", StringRef(Hdr2.Prefix)); + EXPECT_EQ("", StringRef(Hdr2.Name)); - UstarHeader Hdr3 = create( - "012345678", std::string(99, 'x') + "/" + std::string(46, 'x') + "/foo"); - EXPECT_EQ(std::string(46, 'x') + "/foo", StringRef(Hdr3.Name)); - EXPECT_EQ("012345678/" + std::string(99, 'x'), StringRef(Hdr3.Prefix)); + UstarHeader Hdr3 = + create("", std::string(155, 'x') + "/" + std::string(100, 'y')); + EXPECT_EQ("", StringRef(Hdr3.Prefix)); + EXPECT_EQ("", StringRef(Hdr3.Name)); } }