X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/7be8c02360bdb9bd7f59b087da874f88af2a7206..2658e1c5487d0bb9c6223cf149a48f47e8d10b4d:/apt-inst/contrib/extracttar.cc?ds=sidebyside diff --git a/apt-inst/contrib/extracttar.cc b/apt-inst/contrib/extracttar.cc index 487027c3d..60360053e 100644 --- a/apt-inst/contrib/extracttar.cc +++ b/apt-inst/contrib/extracttar.cc @@ -6,7 +6,7 @@ Extract a Tar - Tar Extractor Some performance measurements showed that zlib performed quite poorly - in comparision to a forked gzip process. This tar extractor makes use + in comparison to a forked gzip process. This tar extractor makes use of the fact that dup'd file descriptors have the same seek pointer and that gzip will not read past the end of a compressed stream, even if there is more data. We use the dup property to track extraction @@ -18,13 +18,16 @@ // Include Files /*{{{*/ #include +#include #include #include #include #include -#include +#include -#include +#include +#include +#include #include #include #include @@ -57,12 +60,10 @@ struct ExtractTar::TarHeader // ExtractTar::ExtractTar - Constructor /*{{{*/ // --------------------------------------------------------------------- /* */ -ExtractTar::ExtractTar(FileFd &Fd,unsigned long Max,string DecompressionProgram) : File(Fd), - MaxInSize(Max), DecompressProg(DecompressionProgram) - +ExtractTar::ExtractTar(FileFd &Fd,unsigned long long Max,string DecompressionProgram) + : File(Fd), MaxInSize(Max), DecompressProg(DecompressionProgram) { GZPid = -1; - InFd = -1; Eof = false; } /*}}}*/ @@ -72,36 +73,17 @@ ExtractTar::ExtractTar(FileFd &Fd,unsigned long Max,string DecompressionProgram) ExtractTar::~ExtractTar() { // Error close - Done(true); + Done(); } /*}}}*/ // ExtractTar::Done - Reap the gzip sub process /*{{{*/ -// --------------------------------------------------------------------- -/* If the force flag is given then error messages are suppressed - this - means we hit the end of the tar file but there was still gzip data. */ -bool ExtractTar::Done(bool Force) +bool ExtractTar::Done(bool) { - InFd.Close(); - if (GZPid <= 0) - return true; - - /* If there is a pending error then we are cleaning up gzip and are - not interested in it's failures */ - if (_error->PendingError() == true) - Force = true; - - // Make sure we clean it up! - kill(GZPid,SIGINT); - string confvar = string("dir::bin::") + DecompressProg; - if (ExecWait(GZPid,_config->Find(confvar.c_str(),DecompressProg.c_str()).c_str(), - Force) == false) - { - GZPid = -1; - return Force; - } - - GZPid = -1; - return true; + return Done(); +} +bool ExtractTar::Done() +{ + return InFd.Close(); } /*}}}*/ // ExtractTar::StartGzip - Startup gzip /*{{{*/ @@ -111,43 +93,23 @@ bool ExtractTar::Done(bool Force) gzip will efficiently ignore the extra bits. */ bool ExtractTar::StartGzip() { - int Pipes[2]; - if (pipe(Pipes) != 0) - return _error->Errno("pipe",_("Failed to create pipes")); - - // Fork off the process - GZPid = ExecFork(); - - // Spawn the subprocess - if (GZPid == 0) + if (DecompressProg.empty()) { - // Setup the FDs - dup2(Pipes[1],STDOUT_FILENO); - dup2(File.Fd(),STDIN_FILENO); - int Fd = open("/dev/null",O_RDWR); - if (Fd == -1) - _exit(101); - dup2(Fd,STDERR_FILENO); - close(Fd); - SetCloseExec(STDOUT_FILENO,false); - SetCloseExec(STDIN_FILENO,false); - SetCloseExec(STDERR_FILENO,false); - - const char *Args[3]; - string confvar = string("dir::bin::") + DecompressProg; - string argv0 = _config->Find(confvar.c_str(),DecompressProg.c_str()); - Args[0] = argv0.c_str(); - Args[1] = "-d"; - Args[2] = 0; - execvp(Args[0],(char **)Args); - cerr << _("Failed to exec gzip ") << Args[0] << endl; - _exit(100); + InFd.OpenDescriptor(File.Fd(), FileFd::ReadOnly, FileFd::None, false); + return true; } - // Fix up our FDs - InFd.Fd(Pipes[0]); - close(Pipes[1]); - return true; + std::vector const compressors = APT::Configuration::getCompressors(); + std::vector::const_iterator compressor = compressors.begin(); + for (; compressor != compressors.end(); ++compressor) { + if (compressor->Name == DecompressProg) { + return InFd.OpenDescriptor(File.Fd(), FileFd::ReadOnly, *compressor, false); + } + } + + return _error->Error(_("Cannot find a configured compressor for '%s'"), + DecompressProg.c_str()); + } /*}}}*/ // ExtractTar::Go - Perform extraction /*{{{*/ @@ -161,8 +123,8 @@ bool ExtractTar::Go(pkgDirStream &Stream) return false; // Loop over all blocks - string LastLongLink; - string LastLongName; + string LastLongLink, ItemLink; + string LastLongName, ItemName; while (1) { bool BadRecord = false; @@ -189,7 +151,7 @@ bool ExtractTar::Go(pkgDirStream &Stream) /* Check for a block of nulls - in this case we kill gzip, GNU tar does this.. */ if (NewSum == ' '*sizeof(Tar->Checksum)) - return Done(true); + return Done(); if (NewSum != CheckSum) return _error->Error(_("Tar checksum failed, archive corrupted")); @@ -208,25 +170,23 @@ bool ExtractTar::Go(pkgDirStream &Stream) StrToNum(Tar->Major,Itm.Major,sizeof(Tar->Major),8) == false || StrToNum(Tar->Minor,Itm.Minor,sizeof(Tar->Minor),8) == false) return _error->Error(_("Corrupted archive")); - - // Grab the filename + + // Grab the filename and link target: use last long name if one was + // set, otherwise use the header value as-is, but remember that it may + // fill the entire 100-byte block and needs to be zero-terminated. + // See Debian Bug #689582. if (LastLongName.empty() == false) Itm.Name = (char *)LastLongName.c_str(); else - { - Tar->Name[sizeof(Tar->Name)-1] = 0; - Itm.Name = Tar->Name; - } + Itm.Name = (char *)ItemName.assign(Tar->Name, sizeof(Tar->Name)).c_str(); if (Itm.Name[0] == '.' && Itm.Name[1] == '/' && Itm.Name[2] != 0) Itm.Name += 2; - - // Grab the link target - Tar->Name[sizeof(Tar->LinkName)-1] = 0; - Itm.LinkTarget = Tar->LinkName; if (LastLongLink.empty() == false) Itm.LinkTarget = (char *)LastLongLink.c_str(); - + else + Itm.LinkTarget = (char *)ItemLink.assign(Tar->LinkName, sizeof(Tar->LinkName)).c_str(); + // Convert the type over switch (Tar->LinkFlag) { @@ -261,7 +221,7 @@ bool ExtractTar::Go(pkgDirStream &Stream) case GNU_LongLink: { - unsigned long Length = Itm.Size; + unsigned long long Length = Itm.Size; unsigned char Block[512]; while (Length > 0) { @@ -280,7 +240,7 @@ bool ExtractTar::Go(pkgDirStream &Stream) case GNU_LongName: { - unsigned long Length = Itm.Size; + unsigned long long Length = Itm.Size; unsigned char Block[512]; while (Length > 0) { @@ -309,11 +269,11 @@ bool ExtractTar::Go(pkgDirStream &Stream) return false; // Copy the file over the FD - unsigned long Size = Itm.Size; + unsigned long long Size = Itm.Size; while (Size != 0) { unsigned char Junk[32*1024]; - unsigned long Read = min(Size,(unsigned long)sizeof(Junk)); + unsigned long Read = min(Size, (unsigned long long)sizeof(Junk)); if (InFd.Read(Junk,((Read+511)/512)*512) == false) return false; @@ -346,6 +306,6 @@ bool ExtractTar::Go(pkgDirStream &Stream) LastLongLink.erase(); } - return Done(false); + return Done(); } /*}}}*/