summaryrefslogtreecommitdiff
path: root/ftparchive
diff options
context:
space:
mode:
authorMichael Vogt <michael.vogt@ubuntu.com>2012-01-16 22:17:23 +0100
committerMichael Vogt <michael.vogt@ubuntu.com>2012-01-16 22:17:23 +0100
commite70c1d067335bfa686fa0c6e75e32afb095e1aaa (patch)
treee985e74ed4d613126b24839d710e4b49db753824 /ftparchive
parente75aa33384d52635fba502bed628bc68f9cb5066 (diff)
parent85e1885525977809ff6a3b70abb84d3a63e41817 (diff)
merged from debian-experimental2
Diffstat (limited to 'ftparchive')
-rw-r--r--ftparchive/cachedb.cc8
-rw-r--r--ftparchive/multicompress.cc140
-rw-r--r--ftparchive/multicompress.h6
-rw-r--r--ftparchive/writer.cc25
4 files changed, 27 insertions, 152 deletions
diff --git a/ftparchive/cachedb.cc b/ftparchive/cachedb.cc
index f0bfa2a6d..a8b637a80 100644
--- a/ftparchive/cachedb.cc
+++ b/ftparchive/cachedb.cc
@@ -351,7 +351,7 @@ bool CacheDB::GetMD5(bool const &GenOnly)
return false;
}
MD5Summation MD5;
- if (Fd->Seek(0) == false || MD5.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+ if (Fd->Seek(0) == false || MD5.AddFD(*Fd, CurStat.FileSize) == false)
return false;
MD5Res = MD5.Result();
@@ -382,7 +382,7 @@ bool CacheDB::GetSHA1(bool const &GenOnly)
return false;
}
SHA1Summation SHA1;
- if (Fd->Seek(0) == false || SHA1.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+ if (Fd->Seek(0) == false || SHA1.AddFD(*Fd, CurStat.FileSize) == false)
return false;
SHA1Res = SHA1.Result();
@@ -413,7 +413,7 @@ bool CacheDB::GetSHA256(bool const &GenOnly)
return false;
}
SHA256Summation SHA256;
- if (Fd->Seek(0) == false || SHA256.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+ if (Fd->Seek(0) == false || SHA256.AddFD(*Fd, CurStat.FileSize) == false)
return false;
SHA256Res = SHA256.Result();
@@ -444,7 +444,7 @@ bool CacheDB::GetSHA512(bool const &GenOnly)
return false;
}
SHA512Summation SHA512;
- if (Fd->Seek(0) == false || SHA512.AddFD(Fd->Fd(),CurStat.FileSize) == false)
+ if (Fd->Seek(0) == false || SHA512.AddFD(*Fd, CurStat.FileSize) == false)
return false;
SHA512Res = SHA512.Result();
diff --git a/ftparchive/multicompress.cc b/ftparchive/multicompress.cc
index bf0f858d9..1fea589e2 100644
--- a/ftparchive/multicompress.cc
+++ b/ftparchive/multicompress.cc
@@ -16,6 +16,7 @@
// Include Files /*{{{*/
#include <config.h>
+#include <apt-pkg/fileutl.h>
#include <apt-pkg/strutl.h>
#include <apt-pkg/error.h>
#include <apt-pkg/md5.h>
@@ -90,7 +91,7 @@ MultiCompress::MultiCompress(string const &Output,string const &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;
@@ -182,11 +183,6 @@ 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)
@@ -261,77 +257,10 @@ bool MultiCompress::Finalize(unsigned long 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(APT::Configuration::Compressor const &Prog,
- pid_t &Pid,int const &FileFd,int &OutFd,bool const &Comp)
-{
- Pid = -1;
-
- // No compression
- if (Prog.Binary.empty() == true)
- {
- 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);
-
- std::vector<char const*> Args;
- Args.push_back(Prog.Binary.c_str());
- std::vector<std::string> const * const addArgs =
- (Comp == true) ? &(Prog.CompressArgs) : &(Prog.UncompressArgs);
- for (std::vector<std::string>::const_iterator a = addArgs->begin();
- a != addArgs->end(); ++a)
- Args.push_back(a->c_str());
- Args.push_back(NULL);
-
- execvp(Args[0],(char **)&Args[0]);
- 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,pid_t &Proc)
+bool MultiCompress::OpenOld(FileFd &Fd)
{
Files *Best = Outputs;
for (Files *I = Outputs; I != 0; I = I->Next)
@@ -339,29 +268,9 @@ bool MultiCompress::OpenOld(int &Fd,pid_t &Proc)
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,pid_t 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
@@ -371,14 +280,6 @@ bool MultiCompress::CloseOld(int Fd,pid_t Proc)
is new then the temp files are renamed, otherwise they are erased. */
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);
@@ -398,25 +299,14 @@ bool MultiCompress::Child(int const &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"));
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.c_str(), false);
- }
-
+
if (_error->PendingError() == true)
return false;
@@ -435,31 +325,27 @@ bool MultiCompress::Child(int const &FD)
// Check the MD5 of the lowest cost entity.
while (Missing == false)
{
- int CompFd = -1;
- pid_t Proc = -1;
- if (OpenOld(CompFd,Proc) == false)
+ FileFd CompFd;
+ if (OpenOld(CompFd) == false)
{
_error->Discard();
break;
}
-
+
// Compute the hash
MD5Summation OldMD5;
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() &&
diff --git a/ftparchive/multicompress.h b/ftparchive/multicompress.h
index 5496b06d0..388fad22e 100644
--- a/ftparchive/multicompress.h
+++ b/ftparchive/multicompress.h
@@ -34,15 +34,12 @@ class MultiCompress
FileFd TmpFile;
pid_t CompressProc;
time_t OldMTime;
- int Fd;
};
Files *Outputs;
pid_t Outputter;
mode_t Permissions;
- bool OpenCompress(APT::Configuration::Compressor const &Prog,
- pid_t &Pid,int const &FileFd, int &OutFd,bool const &Comp);
bool Child(int const &Fd);
bool Start();
bool Die();
@@ -54,8 +51,7 @@ class MultiCompress
unsigned long UpdateMTime;
bool Finalize(unsigned long long &OutSize);
- bool OpenOld(int &Fd,pid_t &Proc);
- bool CloseOld(int Fd,pid_t Proc);
+ bool OpenOld(FileFd &Fd);
static bool GetStat(std::string const &Output,std::string const &Compress,struct stat &St);
MultiCompress(std::string const &Output,std::string const &Compress,
diff --git a/ftparchive/writer.cc b/ftparchive/writer.cc
index 27e53faf8..159772991 100644
--- a/ftparchive/writer.cc
+++ b/ftparchive/writer.cc
@@ -889,22 +889,16 @@ bool ContentsWriter::ReadFromPkgs(string const &PkgFile,string const &PkgCompres
MultiCompress Pkgs(PkgFile,PkgCompress,0,false);
if (_error->PendingError() == true)
return false;
-
+
// Open the package file
- int CompFd = -1;
- pid_t Proc = -1;
- if (Pkgs.OpenOld(CompFd,Proc) == false)
+ FileFd Fd;
+ if (Pkgs.OpenOld(Fd) == false)
return false;
-
- // No auto-close FD
- FileFd Fd(CompFd,false);
+
pkgTagFile Tags(&Fd);
if (_error->PendingError() == true)
- {
- Pkgs.CloseOld(CompFd,Proc);
return false;
- }
-
+
// Parse.
pkgTagSection Section;
while (Tags.Step(Section) == true)
@@ -926,11 +920,10 @@ bool ContentsWriter::ReadFromPkgs(string const &PkgFile,string const &PkgCompres
_error->DumpErrors();
}
}
-
+
// Tidy the compressor
- if (Pkgs.CloseOld(CompFd,Proc) == false)
- return false;
-
+ Fd.Close();
+
return true;
}
@@ -1042,7 +1035,7 @@ bool ReleaseWriter::DoPackage(string FileName)
CheckSums[NewFileName].size = fd.Size();
Hashes hs;
- hs.AddFD(fd.Fd(), 0, DoMD5, DoSHA1, DoSHA256, DoSHA512);
+ hs.AddFD(fd, 0, DoMD5, DoSHA1, DoSHA256, DoSHA512);
if (DoMD5 == true)
CheckSums[NewFileName].MD5 = hs.MD5.Result();
if (DoSHA1 == true)