X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/b2e465d6d32d2dc884f58b94acb7e35f671a87fe..1701312682aad167b4ff3425a8e9b680c04b49a5:/ftparchive/multicompress.cc diff --git a/ftparchive/multicompress.cc b/ftparchive/multicompress.cc index 932ad6758..f35d5304a 100644 --- a/ftparchive/multicompress.cc +++ b/ftparchive/multicompress.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: multicompress.cc,v 1.2 2001/02/20 07:03:18 jgg Exp $ +// $Id: multicompress.cc,v 1.4 2003/02/10 07:34:41 doogie Exp $ /* ###################################################################### MultiCompressor @@ -14,61 +14,63 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ -#ifdef __GNUG__ -#pragma implementation "multicompress.h" -#endif +#include -#include "multicompress.h" - +#include #include #include #include - +#include +#include + +#include +#include #include #include -#include +#include #include + +#include "multicompress.h" +#include /*}}}*/ -const MultiCompress::CompType MultiCompress::Compressors[] = - {{".","",0,0,0,1}, - {"gzip",".gz","gzip","-9n","-d",2}, - {"bzip2",".bz2","bzip2","-9","-d",3}, - {}}; +using namespace std; + // MultiCompress::MultiCompress - Constructor /*{{{*/ // --------------------------------------------------------------------- /* Setup the file outputs, compression modes and fork the writer child */ -MultiCompress::MultiCompress(string Output,string Compress, - mode_t Permissions,bool Write) +MultiCompress::MultiCompress(string const &Output,string const &Compress, + mode_t const &Permissions,bool const &Write) : + Permissions(Permissions) { Outputs = 0; Outputter = -1; Input = 0; UpdateMTime = 0; - this->Permissions = Permissions; - + /* Parse the compression string, a space separated lists of compresison types */ string::const_iterator I = Compress.begin(); for (; I != Compress.end();) { - for (; I != Compress.end() && isspace(*I); I++); + for (; I != Compress.end() && isspace(*I); ++I); // Grab a word string::const_iterator Start = I; - for (; I != Compress.end() && !isspace(*I); I++); + for (; I != Compress.end() && !isspace(*I); ++I); // Find the matching compressor - const CompType *Comp = Compressors; - for (; Comp->Name != 0; Comp++) - if (stringcmp(Start,I,Comp->Name) == 0) + std::vector Compressors = APT::Configuration::getCompressors(); + std::vector::const_iterator Comp = Compressors.begin(); + for (; Comp != Compressors.end(); ++Comp) + if (stringcmp(Start,I,Comp->Name.c_str()) == 0) break; // Hmm.. unknown. - if (Comp->Name == 0) + if (Comp == Compressors.end()) { - _error->Warning("Unknown Compresison Algorithm '%s'",string(Start,I).c_str()); + _error->Warning(_("Unknown compression algorithm '%s'"),string(Start,I).c_str()); continue; } @@ -76,7 +78,7 @@ MultiCompress::MultiCompress(string Output,string Compress, Files *NewOut = new Files; NewOut->Next = Outputs; Outputs = NewOut; - NewOut->CompressProg = Comp; + NewOut->CompressProg = *Comp; NewOut->Output = Output+Comp->Extension; struct stat St; @@ -92,13 +94,13 @@ MultiCompress::MultiCompress(string Output,string Compress, /* Open all the temp files now so we can report any errors. File is made unreable to prevent people from touching it during creating. */ for (Files *I = Outputs; I != 0; I = I->Next) - I->TmpFile.Open(I->Output + ".new",FileFd::WriteEmpty,0600); + I->TmpFile.Open(I->Output + ".new", FileFd::WriteOnly | FileFd::Create | FileFd::Empty, FileFd::Extension, 0600); if (_error->PendingError() == true) return; if (Outputs == 0) { - _error->Error("Compressed output %s needs a compression set",Output.c_str()); + _error->Error(_("Compressed output %s needs a compression set"),Output.c_str()); return; } @@ -125,7 +127,7 @@ MultiCompress::~MultiCompress() /* This checks each compressed file to make sure it exists and returns stat information for a random file from the collection. False means one or more of the files is missing. */ -bool MultiCompress::GetStat(string Output,string Compress,struct stat &St) +bool MultiCompress::GetStat(string const &Output,string const &Compress,struct stat &St) { /* Parse the compression string, a space separated lists of compresison types */ @@ -133,20 +135,21 @@ bool MultiCompress::GetStat(string Output,string Compress,struct stat &St) bool DidStat = false; for (; I != Compress.end();) { - for (; I != Compress.end() && isspace(*I); I++); + for (; I != Compress.end() && isspace(*I); ++I); // Grab a word string::const_iterator Start = I; - for (; I != Compress.end() && !isspace(*I); I++); + for (; I != Compress.end() && !isspace(*I); ++I); // Find the matching compressor - const CompType *Comp = Compressors; - for (; Comp->Name != 0; Comp++) - if (stringcmp(Start,I,Comp->Name) == 0) + std::vector Compressors = APT::Configuration::getCompressors(); + std::vector::const_iterator Comp = Compressors.begin(); + for (; Comp != Compressors.end(); ++Comp) + if (stringcmp(Start,I,Comp->Name.c_str()) == 0) break; // Hmm.. unknown. - if (Comp->Name == 0) + if (Comp == Compressors.end()) continue; string Name = Output+Comp->Extension; @@ -165,7 +168,7 @@ bool MultiCompress::Start() // Create a data pipe int Pipe[2] = {-1,-1}; if (pipe(Pipe) != 0) - return _error->Errno("pipe","Failed to create IPC pipe to subprocess"); + return _error->Errno("pipe",_("Failed to create IPC pipe to subprocess")); for (int I = 0; I != 2; I++) SetCloseExec(Pipe[I],true); @@ -183,18 +186,13 @@ bool MultiCompress::Start() _exit(0); }; - /* Tidy up the temp files, we open them in the constructor so as to - get proper error reporting. Close them now. */ - for (Files *I = Outputs; I != 0; I = I->Next) - I->TmpFile.Close(); - close(Pipe[0]); Input = fdopen(Pipe[1],"w"); if (Input == 0) - return _error->Errno("fdopen","Failed to create FILE*"); + return _error->Errno("fdopen",_("Failed to create FILE*")); if (Outputter == -1) - return _error->Errno("fork","Failed to fork"); + return _error->Errno("fork",_("Failed to fork")); return true; } /*}}}*/ @@ -208,7 +206,7 @@ bool MultiCompress::Die() fclose(Input); Input = 0; - bool Res = ExecWait(Outputter,"Compress Child",false); + bool Res = ExecWait(Outputter,_("Compress child"),false); Outputter = -1; return Res; } @@ -216,7 +214,7 @@ bool MultiCompress::Die() // MultiCompress::Finalize - Finish up writing /*{{{*/ // --------------------------------------------------------------------- /* This is only necessary for statistics reporting. */ -bool MultiCompress::Finalize(unsigned long &OutSize) +bool MultiCompress::Finalize(unsigned long long &OutSize) { OutSize = 0; if (Input == 0 || Die() == false) @@ -231,7 +229,7 @@ bool MultiCompress::Finalize(unsigned long &OutSize) { struct stat St; if (stat(I->Output.c_str(),&St) != 0) - return _error->Error("Internal Error, Failed to create %s", + return _error->Error(_("Internal error, failed to create %s"), I->Output.c_str()); if (I->OldMTime != St.st_mtime) @@ -239,14 +237,12 @@ bool MultiCompress::Finalize(unsigned long &OutSize) else { // Update the mtime if necessary - if (UpdateMTime > 0 && + if (UpdateMTime > 0 && (Now - St.st_mtime > (signed)UpdateMTime || St.st_mtime > Now)) { - struct utimbuf Buf; - Buf.actime = Buf.modtime = Now; - utime(I->Output.c_str(),&Buf); + utimes(I->Output.c_str(), NULL); Changed = true; - } + } } // Force the file permissions @@ -262,127 +258,34 @@ bool MultiCompress::Finalize(unsigned long &OutSize) return true; } /*}}}*/ -// MultiCompress::OpenCompress - Open the compressor /*{{{*/ -// --------------------------------------------------------------------- -/* This opens the compressor, either in compress mode or decompress - mode. FileFd is always the compressor input/output file, - OutFd is the created pipe, Input for Compress, Output for Decompress. */ -bool MultiCompress::OpenCompress(const CompType *Prog,int &Pid,int FileFd, - int &OutFd,bool Comp) -{ - Pid = -1; - - // No compression - if (Prog->Binary == 0) - { - OutFd = dup(FileFd); - return true; - } - - // Create a data pipe - int Pipe[2] = {-1,-1}; - if (pipe(Pipe) != 0) - return _error->Errno("pipe","Failed to create subprocess IPC"); - for (int J = 0; J != 2; J++) - SetCloseExec(Pipe[J],true); - - if (Comp == true) - OutFd = Pipe[1]; - else - OutFd = Pipe[0]; - - // The child.. - Pid = ExecFork(); - if (Pid == 0) - { - if (Comp == true) - { - dup2(FileFd,STDOUT_FILENO); - dup2(Pipe[0],STDIN_FILENO); - } - else - { - dup2(FileFd,STDIN_FILENO); - dup2(Pipe[1],STDOUT_FILENO); - } - - SetCloseExec(STDOUT_FILENO,false); - SetCloseExec(STDIN_FILENO,false); - - const char *Args[3]; - Args[0] = Prog->Binary; - if (Comp == true) - Args[1] = Prog->CompArgs; - else - Args[1] = Prog->UnCompArgs; - Args[2] = 0; - execvp(Args[0],(char **)Args); - cerr << "Failed to exec compressor " << Args[0] << endl; - _exit(100); - }; - if (Comp == true) - close(Pipe[0]); - else - close(Pipe[1]); - return true; -} - /*}}}*/ // MultiCompress::OpenOld - Open an old file /*{{{*/ // --------------------------------------------------------------------- /* This opens one of the original output files, possibly decompressing it. */ -bool MultiCompress::OpenOld(int &Fd,int &Proc) +bool MultiCompress::OpenOld(FileFd &Fd) { Files *Best = Outputs; for (Files *I = Outputs; I != 0; I = I->Next) - if (Best->CompressProg->Cost > I->CompressProg->Cost) + if (Best->CompressProg.Cost > I->CompressProg.Cost) Best = I; // Open the file - FileFd F(Best->Output,FileFd::ReadOnly); - if (_error->PendingError() == true) - return false; - - // Decompress the file so we can read it - if (OpenCompress(Best->CompressProg,Proc,F.Fd(),Fd,false) == false) - return false; - - return true; + return Fd.Open(Best->Output, FileFd::ReadOnly, FileFd::Extension); } /*}}}*/ -// MultiCompress::CloseOld - Close the old file /*{{{*/ -// --------------------------------------------------------------------- -/* */ -bool MultiCompress::CloseOld(int Fd,int Proc) -{ - close(Fd); - if (Proc != -1) - if (ExecWait(Proc,"decompressor",false) == false) - return false; - return true; -} - /*}}}*/ // MultiCompress::Child - The writer child /*{{{*/ // --------------------------------------------------------------------- /* The child process forks a bunch of compression children and takes - input on FD and passes it to all the compressor childer. On the way it + input on FD and passes it to all the compressor child. On the way it computes the MD5 of the raw data. After this the raw data in the original files is compared to see if this data is new. If the data is new then the temp files are renamed, otherwise they are erased. */ -bool MultiCompress::Child(int FD) +bool MultiCompress::Child(int const &FD) { - // Start the compression children. - for (Files *I = Outputs; I != 0; I = I->Next) - { - if (OpenCompress(I->CompressProg,I->CompressProc,I->TmpFile.Fd(), - I->Fd,true) == false) - return false; - } - /* Okay, now we just feed data from FD to all the other FDs. Also stash a hash of the data to use later. */ SetNonBlock(FD,false); unsigned char Buffer[32*1024]; - unsigned long FileSize = 0; + unsigned long long FileSize = 0; MD5Summation MD5; while (1) { @@ -397,25 +300,14 @@ bool MultiCompress::Child(int FD) FileSize += Res; for (Files *I = Outputs; I != 0; I = I->Next) { - if (write(I->Fd,Buffer,Res) != Res) + if (I->TmpFile.Write(Buffer, Res) == false) { - _error->Errno("write","IO to subprocess/file failed"); + _error->Errno("write",_("IO to subprocess/file failed")); break; } } } - - // Close all the writers - for (Files *I = Outputs; I != 0; I = I->Next) - close(I->Fd); - - // Wait for the compressors to exit - for (Files *I = Outputs; I != 0; I = I->Next) - { - if (I->CompressProc != -1) - ExecWait(I->CompressProc,I->CompressProg->Binary,false); - } - + if (_error->PendingError() == true) return false; @@ -434,31 +326,27 @@ bool MultiCompress::Child(int FD) // Check the MD5 of the lowest cost entity. while (Missing == false) { - int CompFd = -1; - int Proc = -1; - if (OpenOld(CompFd,Proc) == false) + FileFd CompFd; + if (OpenOld(CompFd) == false) { _error->Discard(); break; } - + // Compute the hash MD5Summation OldMD5; - unsigned long NewFileSize = 0; + unsigned long long NewFileSize = 0; while (1) { - int Res = read(CompFd,Buffer,sizeof(Buffer)); + unsigned long long Res = 0; + if (CompFd.Read(Buffer,sizeof(Buffer), &Res) == false) + return _error->Errno("read",_("Failed to read while computing MD5")); if (Res == 0) break; - if (Res < 0) - return _error->Errno("read","Failed to read while computing MD5"); NewFileSize += Res; OldMD5.Add(Buffer,Res); } - - // Tidy the compressor - if (CloseOld(CompFd,Proc) == false) - return false; + CompFd.Close(); // Check the hash if (OldMD5.Result() == MD5.Result() && @@ -468,7 +356,7 @@ bool MultiCompress::Child(int FD) { I->TmpFile.Close(); if (unlink(I->TmpFile.Name().c_str()) != 0) - _error->Errno("unlink","Problem unlinking %s", + _error->Errno("unlink",_("Problem unlinking %s"), I->TmpFile.Name().c_str()); } return !_error->PendingError(); @@ -480,10 +368,10 @@ bool MultiCompress::Child(int FD) for (Files *I = Outputs; I != 0; I = I->Next) { // Set the correct file modes - fchmod(I->TmpFile.Fd(),Permissions); + chmod(I->TmpFile.Name().c_str(),Permissions); if (rename(I->TmpFile.Name().c_str(),I->Output.c_str()) != 0) - _error->Errno("rename","Failed to rename %s to %s", + _error->Errno("rename",_("Failed to rename %s to %s"), I->TmpFile.Name().c_str(),I->Output.c_str()); I->TmpFile.Close(); }