diff options
author | Michael Vogt <michael.vogt@ubuntu.com> | 2011-12-19 14:22:05 +0100 |
---|---|---|
committer | Michael Vogt <michael.vogt@ubuntu.com> | 2011-12-19 14:22:05 +0100 |
commit | 2f67d29b0c3de8618983055e811e70ee36662f4a (patch) | |
tree | 7ab0646d51474b4f3c16c8a984f2f1edd43528d8 /apt-pkg/contrib | |
parent | 27da8141d21cfbfc29675510737ee05bdfd4a2b1 (diff) | |
parent | 29966fd1cc2bab68777e85b9070dba7821a58d36 (diff) |
merged from lp:~donkult/apt/experimental
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r-- | apt-pkg/contrib/fileutl.cc | 73 | ||||
-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 |
5 files changed, 121 insertions, 22 deletions
diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index a98c2cb85..b350973af 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -75,7 +75,9 @@ class FileFdPrivate { bool pipe; APT::Configuration::Compressor compressor; unsigned int openmode; - FileFdPrivate() : gz(NULL), compressed_fd(-1), compressor_pid(-1), pipe(false) {}; + unsigned long long seekpos; + FileFdPrivate() : gz(NULL), compressed_fd(-1), compressor_pid(-1), pipe(false), + openmode(0), seekpos(0) {}; }; // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/ @@ -1065,6 +1067,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) To = (char *)To + Res; Size -= Res; + d->seekpos += Res; if (Actual != 0) *Actual += Res; } @@ -1097,15 +1100,19 @@ char* FileFd::ReadLine(char *To, unsigned long long const Size) #endif unsigned long long read = 0; - if (Read(To, Size, &read) == false) + while ((Size - 1) != read) + { + unsigned long long done = 0; + if (Read(To + read, 1, &done) == false) + return NULL; + if (done == 0) + break; + if (To[read++] == '\n') + break; + } + if (read == 0) return NULL; - char* c = To; - for (; *c != '\n' && *c != '\0' && read != 0; --read, ++c) - ; // find the end of the line - if (*c != '\0') - *c = '\0'; - if (read != 0) - Seek(Tell() - read); + To[read] = '\0'; return To; } /*}}}*/ @@ -1134,6 +1141,7 @@ bool FileFd::Write(const void *From,unsigned long long Size) From = (char *)From + Res; Size -= Res; + d->seekpos += Res; } while (Res > 0 && Size > 0); @@ -1151,6 +1159,13 @@ bool FileFd::Seek(unsigned long long To) { if (d->pipe == true) { + // Our poor man seeking in pipes is costly, so try to avoid it + unsigned long long seekpos = Tell(); + if (seekpos == To) + return true; + else if (seekpos < To) + return Skip(To - seekpos); + if ((d->openmode & ReadOnly) != ReadOnly) return _error->Error("Reopen is only implemented for read-only files!"); close(iFd); @@ -1173,6 +1188,8 @@ bool FileFd::Seek(unsigned long long To) if (To != 0) return Skip(To); + + d->seekpos = To; return true; } int res; @@ -1187,7 +1204,8 @@ bool FileFd::Seek(unsigned long long To) Flags |= Fail; return _error->Error("Unable to seek to %llu", To); } - + + d->seekpos = To; return true; } /*}}}*/ @@ -1196,6 +1214,20 @@ bool FileFd::Seek(unsigned long long To) /* */ bool FileFd::Skip(unsigned long long Over) { + if (d->pipe == true) + { + d->seekpos += Over; + char buffer[1024]; + while (Over != 0) + { + unsigned long long toread = std::min((unsigned long long) sizeof(buffer), Over); + if (Read(buffer, toread) == false) + return _error->Error("Unable to seek ahead %llu",Over); + Over -= toread; + } + return true; + } + int res; #if APT_USE_ZLIB if (d->gz != NULL) @@ -1208,7 +1240,8 @@ bool FileFd::Skip(unsigned long long Over) Flags |= Fail; return _error->Error("Unable to seek ahead %llu",Over); } - + d->seekpos = res; + return true; } /*}}}*/ @@ -1236,6 +1269,13 @@ bool FileFd::Truncate(unsigned long long To) /* */ unsigned long long FileFd::Tell() { + // In theory, we could just return seekpos here always instead of + // seeking around, but not all users of FileFd use always Seek() and co + // so d->seekpos isn't always true and we can just use it as a hint if + // we have nothing else, but not always as an authority… + if (d->pipe == true) + return d->seekpos; + off_t Res; #if APT_USE_ZLIB if (d->gz != NULL) @@ -1245,6 +1285,7 @@ unsigned long long FileFd::Tell() Res = lseek(iFd,0,SEEK_CUR); if (Res == (off_t)-1) _error->Errno("lseek","Failed to determine the current file position"); + d->seekpos = Res; return Res; } /*}}}*/ @@ -1281,15 +1322,14 @@ unsigned long long FileFd::Size() // so we 'read' the content and 'seek' back - see there if (d->pipe == true) { - // FIXME: If we have read first and then FileSize() the report is wrong - size = 0; + unsigned long long const oldSeek = Tell(); char ignore[1000]; unsigned long long read = 0; do { Read(ignore, sizeof(ignore), &read); - size += read; } while(read != 0); - Seek(0); + size = Tell(); + Seek(oldSeek); } #if APT_USE_ZLIB // only check gzsize if we are actually a gzip file, just checking for @@ -1301,7 +1341,6 @@ unsigned long long FileFd::Size() * this ourselves; the original (uncompressed) file size is the last 32 * bits of the file */ // FIXME: Size for gz-files is limited by 32bit… no largefile support - off_t orig_pos = lseek(iFd, 0, SEEK_CUR); if (lseek(iFd, -4, SEEK_END) < 0) return _error->Errno("lseek","Unable to seek to end of gzipped file"); size = 0L; @@ -1315,7 +1354,7 @@ unsigned long long FileFd::Size() size = tmp_size; #endif - if (lseek(iFd, orig_pos, SEEK_SET) < 0) + if (lseek(iFd, d->seekpos, SEEK_SET) < 0) return _error->Errno("lseek","Unable to seek in gzipped file"); return size; } 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 |