diff options
author | David Kalnischkies <kalnischkies@gmail.com> | 2011-12-17 23:53:31 +0100 |
---|---|---|
committer | David Kalnischkies <kalnischkies@gmail.com> | 2011-12-17 23:53:31 +0100 |
commit | 109eb1511d0cdfa4af3196105cada30bcbb77bc8 (patch) | |
tree | c07319a3ddc9a6484561d1cea918aaecade4a7e9 | |
parent | 1abbc47c045770476f5f9a57c58989d13290d51b (diff) |
try to avoid direct usage of .Fd() if possible and do read()s and co
on the FileFd instead
-rw-r--r-- | apt-pkg/acquire-item.cc | 4 | ||||
-rw-r--r-- | apt-pkg/contrib/hashes.cc | 40 | ||||
-rw-r--r-- | apt-pkg/contrib/hashes.h | 5 | ||||
-rw-r--r-- | apt-pkg/contrib/hashsum.cc | 22 | ||||
-rw-r--r-- | apt-pkg/contrib/hashsum_template.h | 3 | ||||
-rw-r--r-- | apt-pkg/deb/debindexfile.cc | 3 | ||||
-rw-r--r-- | ftparchive/cachedb.cc | 8 | ||||
-rw-r--r-- | ftparchive/writer.cc | 2 | ||||
-rw-r--r-- | methods/cdrom.cc | 2 | ||||
-rw-r--r-- | methods/copy.cc | 2 | ||||
-rw-r--r-- | methods/file.cc | 2 | ||||
-rw-r--r-- | methods/ftp.cc | 2 | ||||
-rw-r--r-- | methods/http.cc | 24 | ||||
-rw-r--r-- | methods/https.cc | 2 | ||||
-rw-r--r-- | methods/rsh.cc | 2 |
15 files changed, 85 insertions, 38 deletions
diff --git a/apt-pkg/acquire-item.cc b/apt-pkg/acquire-item.cc index 453fce109..f231c42b4 100644 --- a/apt-pkg/acquire-item.cc +++ b/apt-pkg/acquire-item.cc @@ -438,7 +438,7 @@ bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile) /*{{{*/ FileFd fd(CurrentPackagesFile, FileFd::ReadOnly); SHA1Summation SHA1; - SHA1.AddFD(fd.Fd(), fd.Size()); + SHA1.AddFD(fd); string const local_sha1 = SHA1.Result(); if(local_sha1 == ServerSha1) @@ -669,7 +669,7 @@ bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/ FileFd fd(FinalFile, FileFd::ReadOnly); SHA1Summation SHA1; - SHA1.AddFD(fd.Fd(), fd.Size()); + SHA1.AddFD(fd); string local_sha1 = string(SHA1.Result()); if(Debug) std::clog << "QueueNextDiff: " diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc index 05001f042..e1a431823 100644 --- a/apt-pkg/contrib/hashes.cc +++ b/apt-pkg/contrib/hashes.cc @@ -61,25 +61,25 @@ bool HashString::VerifyFile(std::string filename) const /*{{{*/ if(Type == "MD5Sum") { MD5Summation MD5; - MD5.AddFD(Fd.Fd(), Fd.Size()); + MD5.AddFD(Fd); fileHash = (std::string)MD5.Result(); } else if (Type == "SHA1") { SHA1Summation SHA1; - SHA1.AddFD(Fd.Fd(), Fd.Size()); + SHA1.AddFD(Fd); fileHash = (std::string)SHA1.Result(); } else if (Type == "SHA256") { SHA256Summation SHA256; - SHA256.AddFD(Fd.Fd(), Fd.Size()); + SHA256.AddFD(Fd); fileHash = (std::string)SHA256.Result(); } else if (Type == "SHA512") { SHA512Summation SHA512; - SHA512.AddFD(Fd.Fd(), Fd.Size()); + SHA512.AddFD(Fd); fileHash = (std::string)SHA512.Result(); } Fd.Close(); @@ -135,5 +135,35 @@ bool Hashes::AddFD(int const Fd,unsigned long long Size, bool const addMD5, } return true; } +bool Hashes::AddFD(FileFd &Fd,unsigned long long Size, bool const addMD5, + bool const addSHA1, bool const addSHA256, bool const addSHA512) +{ + unsigned char Buf[64*64]; + bool const ToEOF = (Size == 0); + while (Size != 0 || ToEOF) + { + unsigned long long n = sizeof(Buf); + if (!ToEOF) n = std::min(Size, n); + unsigned long long a = 0; + if (Fd.Read(Buf, n, &a) == false) // error + return false; + if (ToEOF == false) + { + if (a != n) // short read + return false; + } + else if (a == 0) // EOF + break; + Size -= a; + if (addMD5 == true) + MD5.Add(Buf, a); + if (addSHA1 == true) + SHA1.Add(Buf, a); + if (addSHA256 == true) + SHA256.Add(Buf, a); + if (addSHA512 == true) + SHA512.Add(Buf, a); + } + return true; +} /*}}}*/ - diff --git a/apt-pkg/contrib/hashes.h b/apt-pkg/contrib/hashes.h index b206eccb8..0c0b6c6a7 100644 --- a/apt-pkg/contrib/hashes.h +++ b/apt-pkg/contrib/hashes.h @@ -17,6 +17,7 @@ #include <apt-pkg/md5.h> #include <apt-pkg/sha1.h> #include <apt-pkg/sha2.h> +#include <apt-pkg/fileutl.h> #include <algorithm> #include <vector> @@ -74,6 +75,10 @@ class Hashes { return AddFD(Fd, Size, true, true, true, true); }; bool AddFD(int const Fd, unsigned long long Size, bool const addMD5, bool const addSHA1, bool const addSHA256, bool const addSHA512); + inline bool AddFD(FileFd &Fd,unsigned long long Size = 0) + { return AddFD(Fd, Size, true, true, true, true); }; + bool AddFD(FileFd &Fd, unsigned long long Size, bool const addMD5, + bool const addSHA1, bool const addSHA256, bool const addSHA512); inline bool Add(const unsigned char *Beg,const unsigned char *End) {return Add(Beg,End-Beg);}; }; diff --git a/apt-pkg/contrib/hashsum.cc b/apt-pkg/contrib/hashsum.cc index ff3b112bb..289e43aa4 100644 --- a/apt-pkg/contrib/hashsum.cc +++ b/apt-pkg/contrib/hashsum.cc @@ -25,4 +25,26 @@ bool SummationImplementation::AddFD(int const Fd, unsigned long long Size) { } return true; } +bool SummationImplementation::AddFD(FileFd &Fd, unsigned long long Size) { + unsigned char Buf[64 * 64]; + bool ToEOF = (Size == 0); + while (Size != 0 || ToEOF) + { + unsigned long long n = sizeof(Buf); + if (!ToEOF) n = std::min(Size, n); + unsigned long long a = 0; + if (Fd.Read(Buf, n, &a) == false) // error + return false; + if (ToEOF == false) + { + if (a != n) // short read + return false; + } + else if (a == 0) // EOF + break; + Size -= a; + Add(Buf, a); + } + return true; +} /*}}}*/ diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h index 6301ac9d0..51e3b0862 100644 --- a/apt-pkg/contrib/hashsum_template.h +++ b/apt-pkg/contrib/hashsum_template.h @@ -10,6 +10,8 @@ #ifndef APTPKG_HASHSUM_TEMPLATE_H #define APTPKG_HASHSUM_TEMPLATE_H +#include <apt-pkg/fileutl.h> + #include <string> #include <cstring> #include <algorithm> @@ -108,6 +110,7 @@ class SummationImplementation { return Add((const unsigned char *)Beg, End - Beg); }; bool AddFD(int Fd, unsigned long long Size = 0); + bool AddFD(FileFd &Fd, unsigned long long Size = 0); }; #endif diff --git a/apt-pkg/deb/debindexfile.cc b/apt-pkg/deb/debindexfile.cc index 84791a70a..5dc2a2ac2 100644 --- a/apt-pkg/deb/debindexfile.cc +++ b/apt-pkg/deb/debindexfile.cc @@ -600,9 +600,6 @@ bool debStatusIndex::Merge(pkgCacheGenerator &Gen,OpProgress *Prog) const // Store the IMS information pkgCache::PkgFileIterator CFile = Gen.GetCurFile(); - struct stat St; - if (fstat(Pkg.Fd(),&St) != 0) - return _error->Errno("fstat","Failed to stat"); CFile->Size = Pkg.FileSize(); CFile->mtime = Pkg.ModificationTime(); CFile->Archive = Gen.WriteUniqString("now"); 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/writer.cc b/ftparchive/writer.cc index 02777713c..159772991 100644 --- a/ftparchive/writer.cc +++ b/ftparchive/writer.cc @@ -1035,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) diff --git a/methods/cdrom.cc b/methods/cdrom.cc index e7114b168..22d4b9164 100644 --- a/methods/cdrom.cc +++ b/methods/cdrom.cc @@ -268,7 +268,7 @@ bool CDROMMethod::Fetch(FetchItem *Itm) Hashes Hash; FileFd Fd(Res.Filename, FileFd::ReadOnly); - Hash.AddFD(Fd.Fd(), Fd.Size()); + Hash.AddFD(Fd); Res.TakeHashes(Hash); URIDone(Res); diff --git a/methods/copy.cc b/methods/copy.cc index f8d58e479..e81d0022b 100644 --- a/methods/copy.cc +++ b/methods/copy.cc @@ -85,7 +85,7 @@ bool CopyMethod::Fetch(FetchItem *Itm) Hashes Hash; FileFd Fd(Res.Filename, FileFd::ReadOnly); - Hash.AddFD(Fd.Fd(), Fd.Size()); + Hash.AddFD(Fd); Res.TakeHashes(Hash); URIDone(Res); diff --git a/methods/file.cc b/methods/file.cc index 5025c996d..7ed4e6f60 100644 --- a/methods/file.cc +++ b/methods/file.cc @@ -83,7 +83,7 @@ bool FileMethod::Fetch(FetchItem *Itm) Hashes Hash; FileFd Fd(Res.Filename, FileFd::ReadOnly); - Hash.AddFD(Fd.Fd(), Fd.Size()); + Hash.AddFD(Fd); Res.TakeHashes(Hash); URIDone(Res); return true; diff --git a/methods/ftp.cc b/methods/ftp.cc index 2ca0ac6f7..ad8a7b828 100644 --- a/methods/ftp.cc +++ b/methods/ftp.cc @@ -868,7 +868,7 @@ bool FTPConn::Get(const char *Path,FileFd &To,unsigned long long Resume, if (Resume != 0) { - if (Hash.AddFD(To.Fd(),Resume) == false) + if (Hash.AddFD(To,Resume) == false) { _error->Errno("read",_("Problem hashing file")); return false; diff --git a/methods/http.cc b/methods/http.cc index 0d81c73ed..b8ed43cd2 100644 --- a/methods/http.cc +++ b/methods/http.cc @@ -1007,31 +1007,21 @@ HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv) FailFile.c_str(); // Make sure we dont do a malloc in the signal handler FailFd = File->Fd(); FailTime = Srv->Date; - - // Set the expected size - if (Srv->StartPos >= 0) - { - Res.ResumePoint = Srv->StartPos; - if (ftruncate(File->Fd(),Srv->StartPos) < 0) - _error->Errno("ftruncate", _("Failed to truncate file")); - } - - // Set the start point - lseek(File->Fd(),0,SEEK_END); delete Srv->In.Hash; Srv->In.Hash = new Hashes; - - // Fill the Hash if the file is non-empty (resume) - if (Srv->StartPos > 0) + + // Set the expected size and read file for the hashes + if (Srv->StartPos >= 0) { - lseek(File->Fd(),0,SEEK_SET); - if (Srv->In.Hash->AddFD(File->Fd(),Srv->StartPos) == false) + Res.ResumePoint = Srv->StartPos; + File->Truncate(Srv->StartPos); + + if (Srv->In.Hash->AddFD(*File,Srv->StartPos) == false) { _error->Errno("read",_("Problem hashing file")); return ERROR_NOT_FROM_SERVER; } - lseek(File->Fd(),0,SEEK_END); } SetNonBlock(File->Fd(),true); diff --git a/methods/https.cc b/methods/https.cc index 335699907..317c8a587 100644 --- a/methods/https.cc +++ b/methods/https.cc @@ -314,7 +314,7 @@ bool HttpsMethod::Fetch(FetchItem *Itm) // take hashes Hashes Hash; FileFd Fd(Res.Filename, FileFd::ReadOnly); - Hash.AddFD(Fd.Fd(), Fd.Size()); + Hash.AddFD(Fd); Res.TakeHashes(Hash); // keep apt updated diff --git a/methods/rsh.cc b/methods/rsh.cc index da9777fc4..d249ae961 100644 --- a/methods/rsh.cc +++ b/methods/rsh.cc @@ -305,7 +305,7 @@ bool RSHConn::Get(const char *Path,FileFd &To,unsigned long long Resume, return false; if (Resume != 0) { - if (Hash.AddFD(To.Fd(),Resume) == false) { + if (Hash.AddFD(To,Resume) == false) { _error->Errno("read",_("Problem hashing file")); return false; } |