From a5414e56403537678d5be87acf59c37a05f55719 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 24 Feb 2014 23:10:25 +0100 Subject: add default and override handling for Cnf::FindVector Automatically handle the override of list options via its parent value which can even be a comma-separated list of values. It also adds an easy way of providing a default for the list. --- apt-pkg/contrib/configuration.cc | 14 ++++++++++++-- apt-pkg/contrib/configuration.h | 16 +++++++++++++++- apt-pkg/contrib/strutl.cc | 4 +++- 3 files changed, 30 insertions(+), 4 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc index 4ef4663c0..8eddd56d4 100644 --- a/apt-pkg/contrib/configuration.cc +++ b/apt-pkg/contrib/configuration.cc @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -246,12 +247,18 @@ string Configuration::FindDir(const char *Name,const char *Default) const // Configuration::FindVector - Find a vector of values /*{{{*/ // --------------------------------------------------------------------- /* Returns a vector of config values under the given item */ -vector Configuration::FindVector(const char *Name) const +#if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR < 13) +vector Configuration::FindVector(const char *Name) const { return FindVector(Name, ""); } +#endif +vector Configuration::FindVector(const char *Name, std::string const &Default) const { vector Vec; const Item *Top = Lookup(Name); if (Top == NULL) - return Vec; + return VectorizeString(Default, ','); + + if (Top->Value.empty() == false) + return VectorizeString(Top->Value, ','); Item *I = Top->Child; while(I != NULL) @@ -259,6 +266,9 @@ vector Configuration::FindVector(const char *Name) const Vec.push_back(I->Value); I = I->Next; } + if (Vec.empty() == true) + return VectorizeString(Default, ','); + return Vec; } /*}}}*/ diff --git a/apt-pkg/contrib/configuration.h b/apt-pkg/contrib/configuration.h index 8e09ea0a6..c256139f4 100644 --- a/apt-pkg/contrib/configuration.h +++ b/apt-pkg/contrib/configuration.h @@ -74,8 +74,22 @@ class Configuration std::string Find(std::string const &Name, std::string const &Default) const {return Find(Name.c_str(),Default.c_str());}; std::string FindFile(const char *Name,const char *Default = 0) const; std::string FindDir(const char *Name,const char *Default = 0) const; + /** return a list of child options + * + * Options like Acquire::Languages are handled as lists which + * can be overridden and have a default. For the later two a comma + * separated list of values is supported. + * + * \param Name of the parent node + * \param Default list of values separated by commas */ + std::vector FindVector(const char *Name, std::string const &Default) const; + std::vector FindVector(std::string const &Name, std::string const &Default) const { return FindVector(Name.c_str(), Default); }; +#if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13) + std::vector FindVector(const char *Name) const { return FindVector(Name, ""); }; +#else std::vector FindVector(const char *Name) const; - std::vector FindVector(std::string const &Name) const { return FindVector(Name.c_str()); }; +#endif + std::vector FindVector(std::string const &Name) const { return FindVector(Name.c_str(), ""); }; int FindI(const char *Name,int const &Default = 0) const; int FindI(std::string const &Name,int const &Default = 0) const {return FindI(Name.c_str(),Default);}; bool FindB(const char *Name,bool const &Default = false) const; diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index d4f53ea3a..f8bb3890d 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1130,9 +1130,11 @@ bool TokSplitString(char Tok,char *Input,char **List, also, but the advantage is that we have an iteratable vector */ vector VectorizeString(string const &haystack, char const &split) { + vector exploded; + if (haystack.empty() == true) + return exploded; string::const_iterator start = haystack.begin(); string::const_iterator end = start; - vector exploded; do { for (; end != haystack.end() && *end != split; ++end); exploded.push_back(string(start, end)); -- cgit v1.2.3 From 255c9e4b74fe677d723c51d3450869ad45ca5463 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 25 Feb 2014 22:10:40 +0100 Subject: make doxygen more quiet, fix issues and disable latex Git-Dch: Ignore --- apt-pkg/contrib/error.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/error.h b/apt-pkg/contrib/error.h index bcee70b1a..c06e45ee4 100644 --- a/apt-pkg/contrib/error.h +++ b/apt-pkg/contrib/error.h @@ -232,11 +232,11 @@ public: /*{{{*/ * if you want to check if also no notices happened set the parameter * flag to \b false. * - * \param WithoutNotice does notices count, default is \b true, so no + * \param threshold minimim level considered * * \return \b true if an the list is empty, \b false otherwise */ - bool empty(MsgType const &trashhold = WARNING) const; + bool empty(MsgType const &threshold = WARNING) const; /** \brief returns and removes the first (or last) message in the list * -- cgit v1.2.3 From d3e8fbb395f57954acd7a2095f02ce530a05ec6a Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 27 Feb 2014 01:20:53 +0100 Subject: =?UTF-8?q?warning:=20extra=20=E2=80=98;=E2=80=99=20[-Wpedantic]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Git-Dch: Ignore Reported-By: gcc -Wpedantic --- apt-pkg/contrib/configuration.cc | 3 +-- apt-pkg/contrib/error.cc | 2 +- apt-pkg/contrib/gpgv.h | 2 +- apt-pkg/contrib/progress.cc | 12 ++++++------ apt-pkg/contrib/strutl.cc | 2 +- apt-pkg/contrib/strutl.h | 38 +++++++++++++++++++------------------- 6 files changed, 29 insertions(+), 30 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc index 8eddd56d4..003fd01d8 100644 --- a/apt-pkg/contrib/configuration.cc +++ b/apt-pkg/contrib/configuration.cc @@ -43,8 +43,7 @@ Configuration::Configuration() : ToFree(true) } Configuration::Configuration(const Item *Root) : Root((Item *)Root), ToFree(false) { -}; - +} /*}}}*/ // Configuration::~Configuration - Destructor /*{{{*/ // --------------------------------------------------------------------- diff --git a/apt-pkg/contrib/error.cc b/apt-pkg/contrib/error.cc index d457781c3..2d25f5ed9 100644 --- a/apt-pkg/contrib/error.cc +++ b/apt-pkg/contrib/error.cc @@ -223,7 +223,7 @@ void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold, void GlobalError::Discard() { Messages.clear(); PendingFlag = false; -}; +} /*}}}*/ // GlobalError::empty - does our error list include anything? /*{{{*/ bool GlobalError::empty(MsgType const &trashhold) const { diff --git a/apt-pkg/contrib/gpgv.h b/apt-pkg/contrib/gpgv.h index 1d79a52ac..d9712d6a8 100644 --- a/apt-pkg/contrib/gpgv.h +++ b/apt-pkg/contrib/gpgv.h @@ -45,7 +45,7 @@ inline void ExecGPGV(std::string const &File, std::string const &FileSig, int const &statusfd = -1) { int fd[2]; ExecGPGV(File, FileSig, statusfd, fd); -}; +} #undef APT_noreturn diff --git a/apt-pkg/contrib/progress.cc b/apt-pkg/contrib/progress.cc index 916e1d730..9d74ed495 100644 --- a/apt-pkg/contrib/progress.cc +++ b/apt-pkg/contrib/progress.cc @@ -125,14 +125,14 @@ bool OpProgress::CheckChange(float Interval) // OpTextProgress::OpTextProgress - Constructor /*{{{*/ // --------------------------------------------------------------------- /* */ -OpTextProgress::OpTextProgress(Configuration &Config) : - NoUpdate(false), NoDisplay(false), LastLen(0) +OpTextProgress::OpTextProgress(Configuration &Config) : + NoUpdate(false), NoDisplay(false), LastLen(0) { if (Config.FindI("quiet",0) >= 1 || Config.FindB("quiet::NoUpdate", false) == true) NoUpdate = true; if (Config.FindI("quiet",0) >= 2) NoDisplay = true; -}; +} /*}}}*/ // OpTextProgress::Done - Clean up the display /*{{{*/ // --------------------------------------------------------------------- @@ -150,12 +150,12 @@ void OpTextProgress::Done() cout << endl; OldOp = string(); } - + if (NoUpdate == true && NoDisplay == false && OldOp.empty() == false) { OldOp = string(); - cout << endl; - } + cout << endl; + } } /*}}}*/ // OpTextProgress::Update - Simple text spinner /*{{{*/ diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index f8bb3890d..0d85b4fd3 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -153,7 +153,7 @@ char *_strrstrip(char *String) End++; *End = 0; return String; -}; +} /*}}}*/ // strtabexpand - Converts tabs into 8 spaces /*{{{*/ // --------------------------------------------------------------------- diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 8d746f10e..3a6d38b75 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -37,8 +37,8 @@ namespace APT { namespace String { std::string Strip(const std::string &s); bool Endswith(const std::string &s, const std::string &ending); - }; -}; + } +} bool UTF8ToCodeset(const char *codeset, const std::string &orig, std::string *dest); @@ -104,17 +104,17 @@ int tolower_ascii(int const c) __attrib_const __hot; std::string StripEpoch(const std::string &VerStr); #define APT_MKSTRCMP(name,func) \ -inline int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));}; \ -inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \ -inline int name(const std::string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));}; \ -inline int name(const std::string& A,const std::string& B) {return func(A.c_str(),A.c_str()+A.length(),B.c_str(),B.c_str()+B.length());}; \ -inline int name(const std::string& A,const char *B,const char *BEnd) {return func(A.c_str(),A.c_str()+A.length(),B,BEnd);}; +inline int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \ +inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \ +inline int name(const std::string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));} \ +inline int name(const std::string& A,const std::string& B) {return func(A.c_str(),A.c_str()+A.length(),B.c_str(),B.c_str()+B.length());} \ +inline int name(const std::string& A,const char *B,const char *BEnd) {return func(A.c_str(),A.c_str()+A.length(),B,BEnd);} #define APT_MKSTRCMP2(name,func) \ -inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));}; \ -inline int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));}; \ -inline int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());}; \ -inline int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);}; +inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \ +inline int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \ +inline int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \ +inline int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);} int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd); int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd); @@ -132,18 +132,18 @@ int stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd int stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd, std::string::const_iterator B,std::string::const_iterator BEnd); -inline int stringcmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcmp(A,Aend,B,B+strlen(B));}; -inline int stringcasecmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcasecmp(A,Aend,B,B+strlen(B));}; +inline int stringcmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcmp(A,Aend,B,B+strlen(B));} +inline int stringcasecmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcasecmp(A,Aend,B,B+strlen(B));} #endif -APT_MKSTRCMP2(stringcmp,stringcmp); -APT_MKSTRCMP2(stringcasecmp,stringcasecmp); +APT_MKSTRCMP2(stringcmp,stringcmp) +APT_MKSTRCMP2(stringcasecmp,stringcasecmp) // Return the length of a NULL-terminated string array size_t strv_length(const char **str_array); -inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);}; +inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);} class URI { @@ -159,13 +159,13 @@ class URI unsigned int Port; operator std::string(); - inline void operator =(const std::string &From) {CopyFrom(From);}; + inline void operator =(const std::string &From) {CopyFrom(From);} inline bool empty() {return Access.empty();}; static std::string SiteOnly(const std::string &URI); static std::string NoUserPassword(const std::string &URI); - URI(std::string Path) {CopyFrom(Path);}; - URI() : Port(0) {}; + URI(std::string Path) {CopyFrom(Path);} + URI() : Port(0) {} }; struct SubstVar -- cgit v1.2.3 From cf4ff3b78dc347188949370db914fe6329be6c99 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 27 Feb 2014 01:54:10 +0100 Subject: warning: cast from type A to type B casts away qualifiers [-Wcast-qual] Git-Dch: Ignore Reported-By: gcc -Wcast-qual --- apt-pkg/contrib/fileutl.cc | 4 ++-- apt-pkg/contrib/hashes.h | 2 +- apt-pkg/contrib/hashsum_template.h | 42 +++++++++++++++++++------------------- 3 files changed, 24 insertions(+), 24 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 52411a762..17833f090 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1400,7 +1400,7 @@ bool FileFd::Write(const void *From,unsigned long long Size) return FileFdErrno("write",_("Write error")); } - From = (char *)From + Res; + From = (char const *)From + Res; Size -= Res; if (d != NULL) d->seekpos += Res; @@ -1424,7 +1424,7 @@ bool FileFd::Write(int Fd, const void *From, unsigned long long Size) if (Res < 0) return _error->Errno("write",_("Write error")); - From = (char *)From + Res; + From = (char const *)From + Res; Size -= Res; } while (Res > 0 && Size > 0); diff --git a/apt-pkg/contrib/hashes.h b/apt-pkg/contrib/hashes.h index 0a8bcd259..636cad257 100644 --- a/apt-pkg/contrib/hashes.h +++ b/apt-pkg/contrib/hashes.h @@ -77,7 +77,7 @@ class Hashes { return MD5.Add(Data,Size) && SHA1.Add(Data,Size) && SHA256.Add(Data,Size) && SHA512.Add(Data,Size); }; - inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));}; + inline bool Add(const char *Data) {return Add((unsigned char const *)Data,strlen(Data));}; inline bool AddFD(int const Fd,unsigned long long Size = 0) { return AddFD(Fd, Size, true, true, true, true); }; bool AddFD(int const Fd, unsigned long long Size, bool const addMD5, diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h index 9bf160b2b..97b6a4ad9 100644 --- a/apt-pkg/contrib/hashsum_template.h +++ b/apt-pkg/contrib/hashsum_template.h @@ -28,18 +28,18 @@ template class HashSumValue { unsigned char Sum[N/8]; - + public: // Accessors bool operator ==(const HashSumValue &rhs) const { return memcmp(Sum,rhs.Sum,sizeof(Sum)) == 0; - }; + } bool operator !=(const HashSumValue &rhs) const { return memcmp(Sum,rhs.Sum,sizeof(Sum)) != 0; - }; + } std::string Value() const { @@ -49,7 +49,7 @@ class HashSumValue }; char Result[((N/8)*2)+1]; Result[(N/8)*2] = 0; - + // Convert each char into two letters int J = 0; int I = 0; @@ -59,31 +59,31 @@ class HashSumValue Result[I + 1] = Conv[Sum[J] & 0xF]; } return std::string(Result); - }; - + } + inline void Value(unsigned char S[N/8]) { - for (int I = 0; I != sizeof(Sum); I++) + for (int I = 0; I != sizeof(Sum); ++I) S[I] = Sum[I]; - }; + } - inline operator std::string() const + inline operator std::string() const { return Value(); - }; + } - bool Set(std::string Str) + bool Set(std::string Str) { return Hex2Num(Str,Sum,sizeof(Sum)); - }; + } - inline void Set(unsigned char S[N/8]) + inline void Set(unsigned char S[N/8]) { - for (int I = 0; I != sizeof(Sum); I++) + for (int I = 0; I != sizeof(Sum); ++I) Sum[I] = S[I]; - }; + } - HashSumValue(std::string Str) + HashSumValue(std::string Str) { memset(Sum,0,sizeof(Sum)); Set(Str); @@ -99,17 +99,17 @@ class SummationImplementation public: virtual bool Add(const unsigned char *inbuf, unsigned long long inlen) = 0; inline bool Add(const char *inbuf, unsigned long long const inlen) - { return Add((unsigned char *)inbuf, inlen); }; + { return Add((const unsigned char *)inbuf, inlen); } inline bool Add(const unsigned char *Data) - { return Add(Data, strlen((const char *)Data)); }; + { return Add(Data, strlen((const char *)Data)); } inline bool Add(const char *Data) - { return Add((const unsigned char *)Data, strlen((const char *)Data)); }; + { return Add((const unsigned char *)Data, strlen((const char *)Data)); } inline bool Add(const unsigned char *Beg, const unsigned char *End) - { return Add(Beg, End - Beg); }; + { return Add(Beg, End - Beg); } inline bool Add(const char *Beg, const char *End) - { return Add((const unsigned char *)Beg, End - Beg); }; + { 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); -- cgit v1.2.3 From e788a834ce421042e727b72e43177edaa47e53a7 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 27 Feb 2014 02:18:11 +0100 Subject: warning: useless cast to type A [-Wuseless-cast] Git-Dch: Ignore Reported-By: gcc -Wuseless-cast --- apt-pkg/contrib/fileutl.cc | 4 ++-- apt-pkg/contrib/hashsum_template.h | 2 +- apt-pkg/contrib/mmap.cc | 2 +- apt-pkg/contrib/netrc.cc | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 17833f090..464950abd 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -891,7 +891,7 @@ bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress, { for (; compressor != compressors.end(); ++compressor) { - std::string file = std::string(FileName).append(compressor->Extension); + std::string file = FileName + compressor->Extension; if (FileExists(file) == false) continue; FileName = file; @@ -1793,7 +1793,7 @@ bool FileFd::FileFdError(const char *Description,...) { } /*}}}*/ -gzFile FileFd::gzFd() { return (gzFile) d->gz; } +gzFile FileFd::gzFd() { return d->gz; } // Glob - wrapper around "glob()" /*{{{*/ diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h index 97b6a4ad9..f96188eb8 100644 --- a/apt-pkg/contrib/hashsum_template.h +++ b/apt-pkg/contrib/hashsum_template.h @@ -104,7 +104,7 @@ class SummationImplementation inline bool Add(const unsigned char *Data) { return Add(Data, strlen((const char *)Data)); } inline bool Add(const char *Data) - { return Add((const unsigned char *)Data, strlen((const char *)Data)); } + { return Add((const unsigned char *)Data, strlen(Data)); } inline bool Add(const unsigned char *Beg, const unsigned char *End) { return Add(Beg, End - Beg); } diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 51e8eb30f..37acba340 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -198,7 +198,7 @@ bool MMap::Sync(unsigned long Start,unsigned long Stop) { #ifdef _POSIX_SYNCHRONIZED_IO unsigned long long const PSize = sysconf(_SC_PAGESIZE); - if (msync((char *)Base+(unsigned long long)(Start/PSize)*PSize,Stop - Start,MS_SYNC) < 0) + if (msync((char *)Base+(Start/PSize)*PSize, Stop - Start, MS_SYNC) < 0) return _error->Errno("msync", _("Unable to synchronize mmap")); #endif } diff --git a/apt-pkg/contrib/netrc.cc b/apt-pkg/contrib/netrc.cc index de95aa4ab..32b146581 100644 --- a/apt-pkg/contrib/netrc.cc +++ b/apt-pkg/contrib/netrc.cc @@ -198,7 +198,7 @@ void maybe_add_auth (URI &Uri, string NetRCFile) // if host did not work, try Host+Path next, this will trigger // a lookup uri.startswith(host) in the netrc file parser (because // of the "/" - char *hostpath = strdup(string(Uri.Host+Uri.Path).c_str()); + char *hostpath = strdup((Uri.Host + Uri.Path).c_str()); if (hostpath && parsenetrc_string(hostpath, login, password, netrcfile) == 0) { if (_config->FindB("Debug::Acquire::netrc", false) == true) -- cgit v1.2.3 From ef74268b47fae1afd302a4a524b53143cbfd6ce1 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 27 Feb 2014 03:32:13 +0100 Subject: warning: cannot optimize loop, the loop counter may overflow [-Wunsafe-loop-optimizations] Git-Dch: Ignore Reported-By: gcc -Wunsafe-loop-optimizations --- apt-pkg/contrib/strutl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 0d85b4fd3..61fcc6a7d 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1184,7 +1184,7 @@ unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, R->Hit = false; unsigned long Hits = 0; - for (; ListBegin != ListEnd; ListBegin++) + for (; ListBegin < ListEnd; ++ListBegin) { // Check if the name is a regex const char *I; -- cgit v1.2.3 From 54298f49d71347616df19b8d2f59c907374e07b3 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 1 Mar 2014 22:27:11 +0100 Subject: move defines for version to macros.h also adds namespaced attributes for good usage Git-Dch: Ignore --- apt-pkg/contrib/macros.h | 84 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 17 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/macros.h b/apt-pkg/contrib/macros.h index e53d01b8f..294242e68 100644 --- a/apt-pkg/contrib/macros.h +++ b/apt-pkg/contrib/macros.h @@ -54,37 +54,87 @@ #define CLRFLAG(v,f) ((v) &=~FLAG(f)) #define CHKFLAG(v,f) ((v) & FLAG(f) ? true : false) -// some nice optional GNUC features -#if __GNUC__ >= 3 - #define __must_check __attribute__ ((warn_unused_result)) - #define __deprecated __attribute__ ((deprecated)) - #define __attrib_const __attribute__ ((__const__)) - /* likely() and unlikely() can be used to mark boolean expressions - as (not) likely true which will help the compiler to optimise */ +#ifdef __GNUC__ +#define APT_GCC_VERSION (__GNUC__ << 8 | __GNUC_MINOR__) +#else +#define APT_GCC_VERSION 0 +#endif + +/* likely() and unlikely() can be used to mark boolean expressions + as (not) likely true which will help the compiler to optimise */ +#if APT_GCC_VERSION >= 0x0300 #define likely(x) __builtin_expect (!!(x), 1) #define unlikely(x) __builtin_expect (!!(x), 0) #else - #define __must_check /* no warn_unused_result */ - #define __deprecated /* no deprecated */ - #define __attrib_const /* no const attribute */ #define likely(x) (x) #define unlikely(x) (x) #endif +#if APT_GCC_VERSION >= 0x0300 + #define APT_UNUSED __attribute__((unused)) + #define APT_CONST __attribute__((const)) + #define APT_PURE __attribute__((pure)) + #define APT_NORETURN __attribute__((noreturn)) + #define APT_PRINTF(n) __attribute__((format(printf, n, n + 1))) +#else + #define APT_UNUSED + #define APT_CONST + #define APT_PURE + #define APT_NORETURN + #define APT_PRINTF(n) +#endif + +#if APT_GCC_VERSION > 0x0302 + #define APT_NONNULL(...) __attribute__((nonnull(__VA_ARGS__))) + #define APT_MUSTCHECK __attribute__((warn_unused_result)) +#else + #define APT_NONNULL(...) + #define APT_REQRET +#endif + +#if APT_GCC_VERSION >= 0x0400 + #define APT_SENTINEL __attribute__((sentinel)) +#else + #define APT_SENTINEL +#endif + // cold functions are unlikely() to be called -#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4 - #define __cold __attribute__ ((__cold__)) - #define __hot __attribute__ ((__hot__)) +#if APT_GCC_VERSION >= 0x0403 + #define APT_COLD __attribute__ ((__cold__)) + #define APT_HOT __attribute__ ((__hot__)) #else - #define __cold /* no cold marker */ - #define __hot /* no hot marker */ + #define __cold + #define __hot #endif -#ifdef __GNUG__ -// Methods have a hidden this parameter that is visible to this attribute +#ifndef APT_10_CLEANER_HEADERS +#if APT_GCC_VERSION >= 0x0300 + #define __must_check __attribute__ ((warn_unused_result)) + #define __deprecated __attribute__ ((deprecated)) + #define __attrib_const __attribute__ ((__const__)) #define __like_printf(n) __attribute__((format(printf, n, n + 1))) #else + #define __must_check /* no warn_unused_result */ + #define __deprecated /* no deprecated */ + #define __attrib_const /* no const attribute */ #define __like_printf(n) /* no like-printf */ #endif +#if APT_GCC_VERSION >= 0x0403 + #define __cold __attribute__ ((__cold__)) + #define __hot __attribute__ ((__hot__)) +#else + #define __cold /* no cold marker */ + #define __hot /* no hot marker */ +#endif +#endif + +// These lines are extracted by the makefiles and the buildsystem +// Increasing MAJOR or MINOR results in the need of recompiling all +// reverse-dependencies of libapt-pkg against the new SONAME. +// Non-ABI-Breaks should only increase RELEASE number. +// See also buildlib/libversion.mak +#define APT_PKG_MAJOR 4 +#define APT_PKG_MINOR 12 +#define APT_PKG_RELEASE 0 #endif -- cgit v1.2.3 From 453b82a388013e522b3a1b9fcd6ed0810dab1f4f Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 5 Mar 2014 22:11:25 +0100 Subject: cleanup headers and especially #includes everywhere Beside being a bit cleaner it hopefully also resolves oddball problems I have with high levels of parallel jobs. Git-Dch: Ignore Reported-By: iwyu (include-what-you-use) --- apt-pkg/contrib/cdromutl.cc | 5 ++++- apt-pkg/contrib/cmndline.cc | 5 +++++ apt-pkg/contrib/configuration.cc | 13 ++++++++++--- apt-pkg/contrib/error.cc | 4 +++- apt-pkg/contrib/error.h | 27 ++++++++++++++------------- apt-pkg/contrib/fileutl.cc | 12 +++++++++--- apt-pkg/contrib/fileutl.h | 11 ++++++----- apt-pkg/contrib/gpgv.cc | 18 ++++++++++-------- apt-pkg/contrib/gpgv.h | 14 ++++++-------- apt-pkg/contrib/hashes.cc | 6 +++++- apt-pkg/contrib/hashes.h | 13 +++++++++---- apt-pkg/contrib/hashsum.cc | 3 +++ apt-pkg/contrib/hashsum_template.h | 10 +++++++--- apt-pkg/contrib/macros.h | 8 ++++---- apt-pkg/contrib/md5.cc | 6 +----- apt-pkg/contrib/md5.h | 9 +++++---- apt-pkg/contrib/mmap.cc | 4 ++-- apt-pkg/contrib/netrc.cc | 3 +-- apt-pkg/contrib/netrc.h | 6 ++++-- apt-pkg/contrib/progress.cc | 2 ++ apt-pkg/contrib/sha1.cc | 5 +---- apt-pkg/contrib/sha1.h | 7 ++++--- apt-pkg/contrib/sha2.h | 10 +++++++--- apt-pkg/contrib/sha2_internal.cc | 1 + apt-pkg/contrib/sha2_internal.h | 1 + apt-pkg/contrib/strutl.cc | 9 +++++++-- apt-pkg/contrib/strutl.h | 23 +++++++++++++---------- 27 files changed, 144 insertions(+), 91 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index 20210ec0a..096d3bcf5 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -19,7 +19,10 @@ #include #include -#include +#include +#include +#include +#include #include #include #include diff --git a/apt-pkg/contrib/cmndline.cc b/apt-pkg/contrib/cmndline.cc index ed5800007..3799c822d 100644 --- a/apt-pkg/contrib/cmndline.cc +++ b/apt-pkg/contrib/cmndline.cc @@ -18,6 +18,11 @@ #include #include +#include +#include +#include +#include + #include /*}}}*/ using namespace std; diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc index 003fd01d8..00f6ad0f9 100644 --- a/apt-pkg/contrib/configuration.cc +++ b/apt-pkg/contrib/configuration.cc @@ -21,11 +21,18 @@ #include #include #include -#include - +#include + +#include +#include +#include +#include +#include +#include +#include +#include #include #include -#include #include diff --git a/apt-pkg/contrib/error.cc b/apt-pkg/contrib/error.cc index 2d25f5ed9..892cd4874 100644 --- a/apt-pkg/contrib/error.cc +++ b/apt-pkg/contrib/error.cc @@ -17,12 +17,14 @@ #include +#include +#include +#include #include #include #include #include #include - #include #include diff --git a/apt-pkg/contrib/error.h b/apt-pkg/contrib/error.h index c06e45ee4..919b1e6d4 100644 --- a/apt-pkg/contrib/error.h +++ b/apt-pkg/contrib/error.h @@ -46,6 +46,7 @@ #include #include +#include #include class GlobalError /*{{{*/ @@ -73,7 +74,7 @@ public: /*{{{*/ * * \return \b false */ - bool FatalE(const char *Function,const char *Description,...) __like_printf(3) __cold; + bool FatalE(const char *Function,const char *Description,...) APT_PRINTF(3) APT_COLD; /** \brief add an Error message with errno to the list * @@ -82,7 +83,7 @@ public: /*{{{*/ * * \return \b false */ - bool Errno(const char *Function,const char *Description,...) __like_printf(3) __cold; + bool Errno(const char *Function,const char *Description,...) APT_PRINTF(3) APT_COLD; /** \brief add a warning message with errno to the list * @@ -94,7 +95,7 @@ public: /*{{{*/ * * \return \b false */ - bool WarningE(const char *Function,const char *Description,...) __like_printf(3) __cold; + bool WarningE(const char *Function,const char *Description,...) APT_PRINTF(3) APT_COLD; /** \brief add a notice message with errno to the list * @@ -103,7 +104,7 @@ public: /*{{{*/ * * \return \b false */ - bool NoticeE(const char *Function,const char *Description,...) __like_printf(3) __cold; + bool NoticeE(const char *Function,const char *Description,...) APT_PRINTF(3) APT_COLD; /** \brief add a debug message with errno to the list * @@ -112,7 +113,7 @@ public: /*{{{*/ * * \return \b false */ - bool DebugE(const char *Function,const char *Description,...) __like_printf(3) __cold; + bool DebugE(const char *Function,const char *Description,...) APT_PRINTF(3) APT_COLD; /** \brief adds an errno message with the given type * @@ -121,7 +122,7 @@ public: /*{{{*/ * \param Description of the error */ bool InsertErrno(MsgType const &type, const char* Function, - const char* Description,...) __like_printf(4) __cold; + const char* Description,...) APT_PRINTF(4) APT_COLD; /** \brief adds an errno message with the given type * @@ -155,7 +156,7 @@ public: /*{{{*/ * * \return \b false */ - bool Fatal(const char *Description,...) __like_printf(2) __cold; + bool Fatal(const char *Description,...) APT_PRINTF(2) APT_COLD; /** \brief add an Error message to the list * @@ -163,7 +164,7 @@ public: /*{{{*/ * * \return \b false */ - bool Error(const char *Description,...) __like_printf(2) __cold; + bool Error(const char *Description,...) APT_PRINTF(2) APT_COLD; /** \brief add a warning message to the list * @@ -174,7 +175,7 @@ public: /*{{{*/ * * \return \b false */ - bool Warning(const char *Description,...) __like_printf(2) __cold; + bool Warning(const char *Description,...) APT_PRINTF(2) APT_COLD; /** \brief add a notice message to the list * @@ -187,7 +188,7 @@ public: /*{{{*/ * * \return \b false */ - bool Notice(const char *Description,...) __like_printf(2) __cold; + bool Notice(const char *Description,...) APT_PRINTF(2) APT_COLD; /** \brief add a debug message to the list * @@ -195,14 +196,14 @@ public: /*{{{*/ * * \return \b false */ - bool Debug(const char *Description,...) __like_printf(2) __cold; + bool Debug(const char *Description,...) APT_PRINTF(2) APT_COLD; /** \brief adds an error message with the given type * * \param type of the error message * \param Description of the error */ - bool Insert(MsgType const &type, const char* Description,...) __like_printf(3) __cold; + bool Insert(MsgType const &type, const char* Description,...) APT_PRINTF(3) APT_COLD; /** \brief adds an error message with the given type * @@ -218,7 +219,7 @@ public: /*{{{*/ * should call this method again in that case */ bool Insert(MsgType type, const char* Description, - va_list &args, size_t &msgSize) __cold; + va_list &args, size_t &msgSize) APT_COLD; /** \brief is an error in the list? * diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 464950abd..55ba41128 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -26,16 +26,22 @@ #include #include #include - +#include + +#include +#include +#include +#include +#include +#include +#include #include #include #include - #include #include #include #include -#include #include #include #include diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h index e752e9621..6fdea1294 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -94,7 +95,7 @@ class FileFd And as the auto-conversation converts a 'unsigned long *' to a 'bool' instead of 'unsigned long long *' we need to provide this explicitely - otherwise applications magically start to fail… */ - __deprecated bool Read(void *To,unsigned long long Size,unsigned long *Actual) + bool Read(void *To,unsigned long long Size,unsigned long *Actual) APT_DEPRECATED { unsigned long long R; bool const T = Read(To, Size, &R); @@ -118,7 +119,7 @@ class FileFd // Simple manipulators inline int Fd() {return iFd;}; inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);}; - __deprecated gzFile gzFd(); + gzFile gzFd() APT_DEPRECATED; inline bool IsOpen() {return iFd >= 0;}; inline bool Failed() {return (Flags & Fail) == Fail;}; @@ -152,8 +153,8 @@ class FileFd bool OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor); // private helpers to set Fail flag and call _error->Error - bool FileFdErrno(const char* Function, const char* Description,...) __like_printf(3) __cold; - bool FileFdError(const char* Description,...) __like_printf(2) __cold; + bool FileFdErrno(const char* Function, const char* Description,...) APT_PRINTF(3) APT_COLD; + bool FileFdError(const char* Description,...) APT_PRINTF(2) APT_COLD; }; bool RunScripts(const char *Cnf); @@ -161,7 +162,7 @@ bool CopyFile(FileFd &From,FileFd &To); int GetLock(std::string File,bool Errors = true); bool FileExists(std::string File); bool RealFileExists(std::string File); -bool DirectoryExists(std::string const &Path) __attrib_const; +bool DirectoryExists(std::string const &Path) APT_CONST; bool CreateDirectory(std::string const &Parent, std::string const &Path); time_t GetModificationTime(std::string const &Path); diff --git a/apt-pkg/contrib/gpgv.cc b/apt-pkg/contrib/gpgv.cc index 9de227062..f24dd9640 100644 --- a/apt-pkg/contrib/gpgv.cc +++ b/apt-pkg/contrib/gpgv.cc @@ -2,21 +2,23 @@ // Include Files /*{{{*/ #include +#include +#include +#include +#include +#include + #include #include #include #include #include -#include -#include #include #include - -#include -#include -#include -#include -#include +#include +#include +#include +#include #include /*}}}*/ diff --git a/apt-pkg/contrib/gpgv.h b/apt-pkg/contrib/gpgv.h index d9712d6a8..ab3c68de5 100644 --- a/apt-pkg/contrib/gpgv.h +++ b/apt-pkg/contrib/gpgv.h @@ -9,17 +9,17 @@ #ifndef CONTRIB_GPGV_H #define CONTRIB_GPGV_H +#include + #include #include +#ifndef APT_10_CLEANER_HEADERS #include - -#if __GNUC__ >= 4 - #define APT_noreturn __attribute__ ((noreturn)) -#else - #define APT_noreturn /* no support */ #endif +class FileFd; + /** \brief generates and run the command to verify a file with gpgv * * If File and FileSig specify the same file it is assumed that we @@ -40,15 +40,13 @@ * @param FileSig is the signature (detached or clear-signed) */ void ExecGPGV(std::string const &File, std::string const &FileSig, - int const &statusfd, int fd[2]) APT_noreturn; + int const &statusfd, int fd[2]) APT_NORETURN; inline void ExecGPGV(std::string const &File, std::string const &FileSig, int const &statusfd = -1) { int fd[2]; ExecGPGV(File, FileSig, statusfd, fd); } -#undef APT_noreturn - /** \brief Split an inline signature into message and signature * * Takes a clear-signed message and puts the first signed message diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc index 890573d9c..5efafa511 100644 --- a/apt-pkg/contrib/hashes.cc +++ b/apt-pkg/contrib/hashes.cc @@ -16,8 +16,12 @@ #include #include #include -#include +#include +#include +#include +#include +#include #include #include #include diff --git a/apt-pkg/contrib/hashes.h b/apt-pkg/contrib/hashes.h index 636cad257..979ee1eb8 100644 --- a/apt-pkg/contrib/hashes.h +++ b/apt-pkg/contrib/hashes.h @@ -17,17 +17,22 @@ #include #include #include -#include -#include -#include #include - +#include #ifndef APT_8_CLEANER_HEADERS using std::min; using std::vector; #endif +#ifndef APT_10_CLEANER_HEADERS +#include +#include +#include +#endif + + +class FileFd; // helper class that contains hash function name // and hash diff --git a/apt-pkg/contrib/hashsum.cc b/apt-pkg/contrib/hashsum.cc index d02177724..25ccc187d 100644 --- a/apt-pkg/contrib/hashsum.cc +++ b/apt-pkg/contrib/hashsum.cc @@ -1,6 +1,9 @@ // Cryptographic API Base #include +#include + +#include #include #include "hashsum_template.h" diff --git a/apt-pkg/contrib/hashsum_template.h b/apt-pkg/contrib/hashsum_template.h index f96188eb8..869dc5cb7 100644 --- a/apt-pkg/contrib/hashsum_template.h +++ b/apt-pkg/contrib/hashsum_template.h @@ -10,20 +10,24 @@ #ifndef APTPKG_HASHSUM_TEMPLATE_H #define APTPKG_HASHSUM_TEMPLATE_H -#include #include #include -#include -#include #include +#ifndef APT_10_CLEANER_HEADERS +#include +#include +#include +#endif #ifndef APT_8_CLEANER_HEADERS using std::string; using std::min; #endif +class FileFd; + template class HashSumValue { diff --git a/apt-pkg/contrib/macros.h b/apt-pkg/contrib/macros.h index 294242e68..d97053553 100644 --- a/apt-pkg/contrib/macros.h +++ b/apt-pkg/contrib/macros.h @@ -71,13 +71,13 @@ #endif #if APT_GCC_VERSION >= 0x0300 - #define APT_UNUSED __attribute__((unused)) + #define APT_DEPRECATED __attribute__ ((deprecated)) #define APT_CONST __attribute__((const)) #define APT_PURE __attribute__((pure)) #define APT_NORETURN __attribute__((noreturn)) #define APT_PRINTF(n) __attribute__((format(printf, n, n + 1))) #else - #define APT_UNUSED + #define APT_DEPRECATED #define APT_CONST #define APT_PURE #define APT_NORETURN @@ -103,8 +103,8 @@ #define APT_COLD __attribute__ ((__cold__)) #define APT_HOT __attribute__ ((__hot__)) #else - #define __cold - #define __hot + #define APT_COLD + #define APT_HOT #endif #ifndef APT_10_CLEANER_HEADERS diff --git a/apt-pkg/contrib/md5.cc b/apt-pkg/contrib/md5.cc index 4351aeb22..b487a96f9 100644 --- a/apt-pkg/contrib/md5.cc +++ b/apt-pkg/contrib/md5.cc @@ -38,13 +38,9 @@ #include #include -#include -#include +#include #include -#include -#include // For htonl -#include /*}}}*/ // byteSwap - Swap bytes in a buffer /*{{{*/ diff --git a/apt-pkg/contrib/md5.h b/apt-pkg/contrib/md5.h index 195455645..f4992c223 100644 --- a/apt-pkg/contrib/md5.h +++ b/apt-pkg/contrib/md5.h @@ -23,14 +23,15 @@ #ifndef APTPKG_MD5_H #define APTPKG_MD5_H - -#include -#include -#include #include #include "hashsum_template.h" +#ifndef APT_10_CLEANER_HEADERS +#include +#include +#include +#endif #ifndef APT_8_CLEANER_HEADERS using std::string; using std::min; diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 37acba340..b2a53a6cb 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -22,11 +22,11 @@ #include #include #include +#include +#include #include -#include #include -#include #include #include #include diff --git a/apt-pkg/contrib/netrc.cc b/apt-pkg/contrib/netrc.cc index 32b146581..feaed67c8 100644 --- a/apt-pkg/contrib/netrc.cc +++ b/apt-pkg/contrib/netrc.cc @@ -15,14 +15,13 @@ #include #include -#include -#include #include #include #include #include #include +#include #include #include "netrc.h" diff --git a/apt-pkg/contrib/netrc.h b/apt-pkg/contrib/netrc.h index 6feb5b726..dbeb45386 100644 --- a/apt-pkg/contrib/netrc.h +++ b/apt-pkg/contrib/netrc.h @@ -16,6 +16,8 @@ #include +#include + #ifndef APT_8_CLEANER_HEADERS #include #endif @@ -25,9 +27,9 @@ class URI; -// kill this export on the next ABI break - strongly doubt its in use anyway +// FIXME: kill this export on the next ABI break - strongly doubt its in use anyway // outside of the apt itself, its really a internal interface -__deprecated int parsenetrc (char *host, char *login, char *password, char *filename); +APT_DEPRECATED int parsenetrc (char *host, char *login, char *password, char *filename); void maybe_add_auth (URI &Uri, std::string NetRCFile); #endif diff --git a/apt-pkg/contrib/progress.cc b/apt-pkg/contrib/progress.cc index 9d74ed495..4ff4f181d 100644 --- a/apt-pkg/contrib/progress.cc +++ b/apt-pkg/contrib/progress.cc @@ -14,6 +14,8 @@ #include #include +#include +#include #include #include #include diff --git a/apt-pkg/contrib/sha1.cc b/apt-pkg/contrib/sha1.cc index b5a6a2440..bf6bc6cb6 100644 --- a/apt-pkg/contrib/sha1.cc +++ b/apt-pkg/contrib/sha1.cc @@ -32,12 +32,9 @@ #include #include -#include -#include +#include #include -#include -#include /*}}}*/ // SHA1Transform - Alters an existing SHA-1 hash /*{{{*/ diff --git a/apt-pkg/contrib/sha1.h b/apt-pkg/contrib/sha1.h index a8d55eb13..5770c315a 100644 --- a/apt-pkg/contrib/sha1.h +++ b/apt-pkg/contrib/sha1.h @@ -14,12 +14,13 @@ #ifndef APTPKG_SHA1_H #define APTPKG_SHA1_H +#include "hashsum_template.h" + +#ifndef APT_10_CLEANER_HEADERS #include #include #include - -#include "hashsum_template.h" - +#endif #ifndef APT_8_CLEANER_HEADERS using std::string; using std::min; diff --git a/apt-pkg/contrib/sha2.h b/apt-pkg/contrib/sha2.h index 8e0c99a1b..a25ad4d32 100644 --- a/apt-pkg/contrib/sha2.h +++ b/apt-pkg/contrib/sha2.h @@ -14,14 +14,18 @@ #ifndef APTPKG_SHA2_H #define APTPKG_SHA2_H -#include #include -#include -#include #include "sha2_internal.h" #include "hashsum_template.h" +#ifndef APT_10_CLEANER_HEADERS +#include +#include +#include +#endif + + typedef HashSumValue<512> SHA512SumValue; typedef HashSumValue<256> SHA256SumValue; diff --git a/apt-pkg/contrib/sha2_internal.cc b/apt-pkg/contrib/sha2_internal.cc index bb2560252..131ff5beb 100644 --- a/apt-pkg/contrib/sha2_internal.cc +++ b/apt-pkg/contrib/sha2_internal.cc @@ -33,6 +33,7 @@ */ #include +#include #include /* memcpy()/memset() or bcopy()/bzero() */ #include /* assert() */ #include "sha2_internal.h" diff --git a/apt-pkg/contrib/sha2_internal.h b/apt-pkg/contrib/sha2_internal.h index d9d429c92..1b82d965d 100644 --- a/apt-pkg/contrib/sha2_internal.h +++ b/apt-pkg/contrib/sha2_internal.h @@ -44,6 +44,7 @@ #ifdef SHA2_USE_INTTYPES_H +#include #include #endif /* SHA2_USE_INTTYPES_H */ diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 61fcc6a7d..2100ee47b 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -21,6 +21,11 @@ #include #include +#include +#include +#include +#include +#include #include #include #include @@ -33,9 +38,9 @@ #include #include - -using namespace std; /*}}}*/ +using namespace std; + // Strip - Remove white space from the front and back of a string /*{{{*/ // --------------------------------------------------------------------- namespace APT { diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 3a6d38b75..79479957d 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -18,15 +18,18 @@ #include -#include #include #include #include #include #include +#include #include "macros.h" +#ifndef APT_10_CLEANER_HEADERS +#include +#endif #ifndef APT_8_CLEANER_HEADERS using std::string; using std::vector; @@ -60,9 +63,9 @@ std::string Base64Encode(const std::string &Str); std::string OutputInDepth(const unsigned long Depth, const char* Separator=" "); std::string URItoFileName(const std::string &URI); std::string TimeRFC1123(time_t Date); -bool RFC1123StrToTime(const char* const str,time_t &time) __must_check; -bool FTPMDTMStrToTime(const char* const str,time_t &time) __must_check; -__deprecated bool StrToTime(const std::string &Val,time_t &Result); +bool RFC1123StrToTime(const char* const str,time_t &time) APT_MUSTCHECK; +bool FTPMDTMStrToTime(const char* const str,time_t &time) APT_MUSTCHECK; +APT_DEPRECATED bool StrToTime(const std::string &Val,time_t &Result); std::string LookupTag(const std::string &Message,const char *Tag,const char *Default = 0); int StringToBool(const std::string &Text,int Default = -1); bool ReadMessages(int Fd, std::vector &List); @@ -76,7 +79,7 @@ bool TokSplitString(char Tok,char *Input,char **List, unsigned long ListMax); // split a given string by a char -std::vector VectorizeString(std::string const &haystack, char const &split) __attrib_const; +std::vector VectorizeString(std::string const &haystack, char const &split) APT_CONST; /* \brief Return a vector of strings from string "input" where "sep" * is used as the delimiter string. @@ -94,13 +97,13 @@ std::vector VectorizeString(std::string const &haystack, char const */ std::vector StringSplit(std::string const &input, std::string const &sep, - unsigned int maxsplit=std::numeric_limits::max()) __attrib_const; + unsigned int maxsplit=std::numeric_limits::max()) APT_CONST; -void ioprintf(std::ostream &out,const char *format,...) __like_printf(2); -void strprintf(std::string &out,const char *format,...) __like_printf(2); -char *safe_snprintf(char *Buffer,char *End,const char *Format,...) __like_printf(3); +void ioprintf(std::ostream &out,const char *format,...) APT_PRINTF(2); +void strprintf(std::string &out,const char *format,...) APT_PRINTF(2); +char *safe_snprintf(char *Buffer,char *End,const char *Format,...) APT_PRINTF(3); bool CheckDomainList(const std::string &Host, const std::string &List); -int tolower_ascii(int const c) __attrib_const __hot; +int tolower_ascii(int const c) APT_CONST APT_HOT; std::string StripEpoch(const std::string &VerStr); #define APT_MKSTRCMP(name,func) \ -- cgit v1.2.3 From a02db58fd50ef7fc2f0284852c6b3f98e458a232 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 6 Mar 2014 00:33:10 +0100 Subject: follow method attribute suggestions by gcc Git-Dch: Ignore Reported-By: gcc -Wsuggest-attribute={pure,const,noreturn} --- apt-pkg/contrib/cmndline.h | 8 +++++--- apt-pkg/contrib/crc-16.h | 4 +++- apt-pkg/contrib/error.h | 8 ++++---- apt-pkg/contrib/fileutl.h | 2 +- apt-pkg/contrib/gpgv.h | 2 +- apt-pkg/contrib/hashes.cc | 2 +- apt-pkg/contrib/hashes.h | 2 +- apt-pkg/contrib/strutl.h | 36 ++++++++++++++++++------------------ 8 files changed, 34 insertions(+), 30 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/cmndline.h b/apt-pkg/contrib/cmndline.h index 180276633..143df58b2 100644 --- a/apt-pkg/contrib/cmndline.h +++ b/apt-pkg/contrib/cmndline.h @@ -44,6 +44,8 @@ #ifndef PKGLIB_CMNDLINE_H #define PKGLIB_CMNDLINE_H +#include + #ifndef APT_8_CLEANER_HEADERS #include #endif @@ -80,14 +82,14 @@ class CommandLine bool Parse(int argc,const char **argv); void ShowHelp(); - unsigned int FileSize() const; + unsigned int FileSize() const APT_PURE; bool DispatchArg(Dispatch *List,bool NoMatch = true); static char const * GetCommand(Dispatch const * const Map, - unsigned int const argc, char const * const * const argv); + unsigned int const argc, char const * const * const argv) APT_PURE; static CommandLine::Args MakeArgs(char ShortOpt, char const *LongOpt, - char const *ConfName, unsigned long Flags); + char const *ConfName, unsigned long Flags) APT_CONST; CommandLine(Args *AList,Configuration *Conf); ~CommandLine(); diff --git a/apt-pkg/contrib/crc-16.h b/apt-pkg/contrib/crc-16.h index 702de40b2..08acdafb7 100644 --- a/apt-pkg/contrib/crc-16.h +++ b/apt-pkg/contrib/crc-16.h @@ -10,8 +10,10 @@ #ifndef APTPKG_CRC16_H #define APTPKG_CRC16_H +#include + #define INIT_FCS 0xffff unsigned short AddCRC16(unsigned short fcs, void const *buf, - unsigned long long len); + unsigned long long len) APT_PURE; #endif diff --git a/apt-pkg/contrib/error.h b/apt-pkg/contrib/error.h index 919b1e6d4..ed8c19153 100644 --- a/apt-pkg/contrib/error.h +++ b/apt-pkg/contrib/error.h @@ -141,7 +141,7 @@ public: /*{{{*/ */ bool InsertErrno(MsgType type, const char* Function, const char* Description, va_list &args, - int const errsv, size_t &msgSize); + int const errsv, size_t &msgSize) APT_COLD; /** \brief add an fatal error message to the list * @@ -225,7 +225,7 @@ public: /*{{{*/ * * \return \b true if an error is included in the list, \b false otherwise */ - inline bool PendingError() const {return PendingFlag;}; + inline bool PendingError() const APT_PURE {return PendingFlag;}; /** \brief is the list empty? * @@ -237,7 +237,7 @@ public: /*{{{*/ * * \return \b true if an the list is empty, \b false otherwise */ - bool empty(MsgType const &threshold = WARNING) const; + bool empty(MsgType const &threshold = WARNING) const APT_PURE; /** \brief returns and removes the first (or last) message in the list * @@ -303,7 +303,7 @@ public: /*{{{*/ void MergeWithStack(); /** \brief return the deep of the stack */ - size_t StackCount() const { + size_t StackCount() const APT_PURE { return Stacks.size(); } diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h index 6fdea1294..35f3ab0f4 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -119,7 +119,7 @@ class FileFd // Simple manipulators inline int Fd() {return iFd;}; inline void Fd(int fd) { OpenDescriptor(fd, ReadWrite);}; - gzFile gzFd() APT_DEPRECATED; + gzFile gzFd() APT_DEPRECATED APT_PURE; inline bool IsOpen() {return iFd >= 0;}; inline bool Failed() {return (Flags & Fail) == Fail;}; diff --git a/apt-pkg/contrib/gpgv.h b/apt-pkg/contrib/gpgv.h index ab3c68de5..f018893fd 100644 --- a/apt-pkg/contrib/gpgv.h +++ b/apt-pkg/contrib/gpgv.h @@ -41,7 +41,7 @@ class FileFd; */ void ExecGPGV(std::string const &File, std::string const &FileSig, int const &statusfd, int fd[2]) APT_NORETURN; -inline void ExecGPGV(std::string const &File, std::string const &FileSig, +inline APT_NORETURN void ExecGPGV(std::string const &File, std::string const &FileSig, int const &statusfd = -1) { int fd[2]; ExecGPGV(File, FileSig, statusfd, fd); diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc index 5efafa511..1fce0d75f 100644 --- a/apt-pkg/contrib/hashes.cc +++ b/apt-pkg/contrib/hashes.cc @@ -116,7 +116,7 @@ const char** HashString::SupportedHashes() return _SupportedHashes; } -bool HashString::empty() const +APT_PURE bool HashString::empty() const { return (Type.empty() || Hash.empty()); } diff --git a/apt-pkg/contrib/hashes.h b/apt-pkg/contrib/hashes.h index 979ee1eb8..5cd1af03b 100644 --- a/apt-pkg/contrib/hashes.h +++ b/apt-pkg/contrib/hashes.h @@ -66,7 +66,7 @@ class HashString bool empty() const; // return the list of hashes we support - static const char** SupportedHashes(); + static APT_CONST const char** SupportedHashes(); }; class Hashes diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 79479957d..185cdc3fc 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -107,43 +107,43 @@ int tolower_ascii(int const c) APT_CONST APT_HOT; std::string StripEpoch(const std::string &VerStr); #define APT_MKSTRCMP(name,func) \ -inline int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \ -inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \ -inline int name(const std::string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));} \ -inline int name(const std::string& A,const std::string& B) {return func(A.c_str(),A.c_str()+A.length(),B.c_str(),B.c_str()+B.length());} \ -inline int name(const std::string& A,const char *B,const char *BEnd) {return func(A.c_str(),A.c_str()+A.length(),B,BEnd);} +inline APT_PURE int name(const char *A,const char *B) {return func(A,A+strlen(A),B,B+strlen(B));} \ +inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \ +inline APT_PURE int name(const std::string& A,const char *B) {return func(A.c_str(),A.c_str()+A.length(),B,B+strlen(B));} \ +inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.c_str(),A.c_str()+A.length(),B.c_str(),B.c_str()+B.length());} \ +inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.c_str(),A.c_str()+A.length(),B,BEnd);} #define APT_MKSTRCMP2(name,func) \ -inline int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \ -inline int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \ -inline int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \ -inline int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);} +inline APT_PURE int name(const char *A,const char *AEnd,const char *B) {return func(A,AEnd,B,B+strlen(B));} \ +inline APT_PURE int name(const std::string& A,const char *B) {return func(A.begin(),A.end(),B,B+strlen(B));} \ +inline APT_PURE int name(const std::string& A,const std::string& B) {return func(A.begin(),A.end(),B.begin(),B.end());} \ +inline APT_PURE int name(const std::string& A,const char *B,const char *BEnd) {return func(A.begin(),A.end(),B,BEnd);} -int stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd); -int stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd); +int APT_PURE stringcmp(const char *A,const char *AEnd,const char *B,const char *BEnd); +int APT_PURE stringcasecmp(const char *A,const char *AEnd,const char *B,const char *BEnd); /* We assume that GCC 3 indicates that libstdc++3 is in use too. In that case the definition of string::const_iterator is not the same as const char * and we need these extra functions */ #if __GNUC__ >= 3 -int stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd, +int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd, const char *B,const char *BEnd); -int stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd, +int APT_PURE stringcmp(std::string::const_iterator A,std::string::const_iterator AEnd, std::string::const_iterator B,std::string::const_iterator BEnd); -int stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd, +int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd, const char *B,const char *BEnd); -int stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd, +int APT_PURE stringcasecmp(std::string::const_iterator A,std::string::const_iterator AEnd, std::string::const_iterator B,std::string::const_iterator BEnd); -inline int stringcmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcmp(A,Aend,B,B+strlen(B));} -inline int stringcasecmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcasecmp(A,Aend,B,B+strlen(B));} +inline APT_PURE int stringcmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcmp(A,Aend,B,B+strlen(B));} +inline APT_PURE int stringcasecmp(std::string::const_iterator A,std::string::const_iterator Aend,const char *B) {return stringcasecmp(A,Aend,B,B+strlen(B));} #endif APT_MKSTRCMP2(stringcmp,stringcmp) APT_MKSTRCMP2(stringcasecmp,stringcasecmp) // Return the length of a NULL-terminated string array -size_t strv_length(const char **str_array); +size_t APT_PURE strv_length(const char **str_array); inline const char *DeNull(const char *s) {return (s == 0?"(null)":s);} -- cgit v1.2.3 From 79bde56ea93b396e509fff0ad7a490f949aa5aa4 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 9 Mar 2014 13:32:07 +0100 Subject: if mountpoint has a ".disk" directory it is mounted Checking that parent-directory of mountpoint and mountpoint are on different devices is fine most of the time, but is too restrictive for our testcases and there shouldn't be anything wrong with 'normal' users copying disk-contents around either if they want to. We check for the existance of the ".disk/" directory now as this will not be present if the disk isn't 'mounted'. Disks doesn't need to have such a directory through, so for those we fall back to the old way of detecting mounted or not mounted. --- apt-pkg/contrib/cdromutl.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index 096d3bcf5..176cc2024 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -45,11 +45,16 @@ bool IsMounted(string &Path) { if (Path.empty() == true) return false; - + // Need that trailing slash for directories if (Path[Path.length() - 1] != '/') Path += '/'; - + + // if the path has a ".disk" directory we treat it as mounted + // this way even extracted copies of disks are recognized + if (DirectoryExists(Path + ".disk/") == true) + return true; + /* First we check if the path is actually mounted, we do this by stating the path and the previous directory (careful of links!) and comparing their device fields. */ -- cgit v1.2.3 From 454b97a57ff3b7ced87ac359658adecaae7af7ee Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 9 Mar 2014 14:38:07 +0100 Subject: no error for non-existing mountpoints in MountCdrom The mountpoint might be auto-generated by the mount command so pushing an error on the stack will confuse the following code and let it believe an unrecoverable error occured while potentially everything is okay. Same goes for umount as a non-existing mountpoint is by definition not mounted. --- apt-pkg/contrib/cdromutl.cc | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index 176cc2024..f850e08a5 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -74,7 +74,13 @@ bool IsMounted(string &Path) leave /etc/mtab inconsitant. We drop all messages this produces. */ bool UnmountCdrom(string Path) { - if (IsMounted(Path) == false) + // do not generate errors, even if the mountpoint does not exist + // the mountpoint might be auto-created by the mount command + // and a non-existing mountpoint is surely not mounted + _error->PushToStack(); + bool const mounted = IsMounted(Path); + _error->RevertToStack(); + if (mounted == false) return true; for (int i=0;i<3;i++) @@ -86,8 +92,9 @@ bool UnmountCdrom(string Path) if (Child == 0) { // Make all the fds /dev/null - for (int I = 0; I != 3; I++) - dup2(open("/dev/null",O_RDWR),I); + int const null_fd = open("/dev/null",O_RDWR); + for (int I = 0; I != 3; ++I) + dup2(null_fd, I); if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true) { @@ -121,19 +128,24 @@ bool UnmountCdrom(string Path) /* We fork mount and drop all messages */ bool MountCdrom(string Path, string DeviceName) { - if (IsMounted(Path) == true) + // do not generate errors, even if the mountpoint does not exist + // the mountpoint might be auto-created by the mount command + _error->PushToStack(); + bool const mounted = IsMounted(Path); + _error->RevertToStack(); + if (mounted == true) return true; - + int Child = ExecFork(); // The child if (Child == 0) { // Make all the fds /dev/null - int null_fd = open("/dev/null",O_RDWR); - for (int I = 0; I != 3; I++) + int const null_fd = open("/dev/null",O_RDWR); + for (int I = 0; I != 3; ++I) dup2(null_fd, I); - + if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true) { if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0) -- cgit v1.2.3 From 12844170ad33d96cb0c5fa411f4eb62fea6e3d5d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 10 Mar 2014 01:49:37 +0100 Subject: support very long mtab entries in mountpoint discovery MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Old code limited lines to 250 characters which is probably enough for everybody, but who knows… It also takes care of device nodes which start with the same prefix. --- apt-pkg/contrib/cdromutl.cc | 48 ++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 25 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index f850e08a5..936e377fb 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -262,37 +263,34 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version) return true; } /*}}}*/ - // FindMountPointForDevice - Find mountpoint for the given device /*{{{*/ string FindMountPointForDevice(const char *devnode) { - char buf[255]; - char *out[10]; - int i=0; - // this is the order that mount uses as well - const char *mount[] = { "/etc/mtab", - "/proc/mount", - NULL }; + std::vector const mounts = _config->FindVector("Dir::state::MountPoints", "/etc/mtab,/proc/mount"); - for (i=0; mount[i] != NULL; i++) { - if (FileExists(mount[i])) { - FILE *f=fopen(mount[i], "r"); - while ( fgets(buf, sizeof(buf), f) != NULL) { - if (strncmp(buf, devnode, strlen(devnode)) == 0) { - if(TokSplitString(' ', buf, out, 10)) - { - fclose(f); - // unescape the \0XXX chars in the path - string mount_point = out[1]; - return DeEscapeString(mount_point); - } - } - } - fclose(f); + for (std::vector::const_iterator m = mounts.begin(); m != mounts.end(); ++m) + if (FileExists(*m) == true) + { + char * line = NULL; + size_t line_len = 0; + FILE * f = fopen(m->c_str(), "r"); + while(getline(&line, &line_len, f) != -1) + { + char * out[] = { NULL, NULL, NULL }; + TokSplitString(' ', line, out, 3); + if (out[2] != NULL || out[1] == NULL || out[0] == NULL) + continue; + if (strcmp(out[0], devnode) != 0) + continue; + fclose(f); + // unescape the \0XXX chars in the path + string mount_point = out[1]; + return DeEscapeString(mount_point); + } + fclose(f); } - } - + return string(); } /*}}}*/ -- cgit v1.2.3 From 84baaae93badc2da7c1f4f356456762895cef278 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Fri, 31 May 2013 19:27:57 +0200 Subject: move fd duplication closer to the gz/bz2 open calls Git-Dch: Ignore --- apt-pkg/contrib/fileutl.cc | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 55ba41128..2188f616a 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1067,30 +1067,12 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration: { Close(); Flags = (AutoClose) ? FileFd::AutoClose : 0; - if (AutoClose == false && ( -#ifdef HAVE_ZLIB - compressor.Name == "gzip" || -#endif -#ifdef HAVE_BZ2 - compressor.Name == "bzip2" || -#endif - false)) - { - // Need to duplicate fd here or gzclose for cleanup will close the fd as well - iFd = dup(Fd); - } - else - iFd = Fd; + iFd = Fd; this->FileName = ""; - if (Fd == -1 || OpenInternDescriptor(Mode, compressor) == false) + if (OpenInternDescriptor(Mode, compressor) == false) { if (iFd != -1 && ( -#ifdef HAVE_ZLIB - compressor.Name == "gzip" || -#endif -#ifdef HAVE_BZ2 - compressor.Name == "bzip2" || -#endif + (Flags & Compressed) == Compressed || AutoClose == true)) { close (iFd); @@ -1102,6 +1084,8 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration: } bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor) { + if (iFd == -1) + return false; if (compressor.Name == "." || compressor.Binary.empty() == true) return true; @@ -1110,6 +1094,21 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C d = new FileFdPrivate(); d->openmode = Mode; d->compressor = compressor; + if (AutoClose == false && ( +#ifdef HAVE_ZLIB + compressor.Name == "gzip" || +#endif +#ifdef HAVE_BZ2 + compressor.Name == "bzip2" || +#endif + false)) + { + // Need to duplicate fd here or gz/bz2 close for cleanup will close the fd as well + int const internFd = dup(iFd); + if (internFd == -1) + return FileFdErrno("OpenInternDescriptor", _("Could not open file descriptor %d"), iFd); + iFd = internFd; + } } #ifdef HAVE_ZLIB -- cgit v1.2.3 From 69d6988a1914881952c013cb63b4d835c3db0db7 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 9 Jun 2013 14:32:48 +0200 Subject: refactor setup of file opening via zlib/bz2 lib Git-Dch: Ignore --- apt-pkg/contrib/fileutl.cc | 73 +++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 37 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 2188f616a..057cb6a94 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1089,19 +1089,32 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C if (compressor.Name == "." || compressor.Binary.empty() == true) return true; +#if defined HAVE_ZLIB || defined HAVE_BZ2 + // the API to open files is similar, so setup to avoid code duplicates later + // and while at it ensure that we close before opening (if its a reopen) + void* (*compress_open)(int, const char *) = NULL; +#define APT_COMPRESS_INIT(NAME,OPEN,CLOSE,STRUCT) \ + if (compressor.Name == NAME) \ + { \ + compress_open = (void*(*)(int, const char *)) OPEN; \ + if (d != NULL && STRUCT != NULL) { CLOSE(STRUCT); STRUCT = NULL; } \ + } +#ifdef HAVE_ZLIB + APT_COMPRESS_INIT("gzip", gzdopen, gzclose, d->gz) +#endif +#ifdef HAVE_BZ2 + APT_COMPRESS_INIT("bzip2", BZ2_bzdopen, BZ2_bzclose, d->bz2) +#endif +#undef APT_COMPRESS_INIT +#endif + if (d == NULL) { d = new FileFdPrivate(); d->openmode = Mode; d->compressor = compressor; - if (AutoClose == false && ( -#ifdef HAVE_ZLIB - compressor.Name == "gzip" || -#endif -#ifdef HAVE_BZ2 - compressor.Name == "bzip2" || -#endif - false)) +#if defined HAVE_ZLIB || defined HAVE_BZ2 + if (AutoClose == false && compress_open != NULL) { // Need to duplicate fd here or gz/bz2 close for cleanup will close the fd as well int const internFd = dup(iFd); @@ -1109,44 +1122,30 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C return FileFdErrno("OpenInternDescriptor", _("Could not open file descriptor %d"), iFd); iFd = internFd; } +#endif } -#ifdef HAVE_ZLIB - if (compressor.Name == "gzip") +#if defined HAVE_ZLIB || defined HAVE_BZ2 + if (compress_open != NULL) { - if (d->gz != NULL) - { - gzclose(d->gz); - d->gz = NULL; - } + void* compress_struct = NULL; if ((Mode & ReadWrite) == ReadWrite) - d->gz = gzdopen(iFd, "r+"); + compress_struct = compress_open(iFd, "r+"); else if ((Mode & WriteOnly) == WriteOnly) - d->gz = gzdopen(iFd, "w"); + compress_struct = compress_open(iFd, "w"); else - d->gz = gzdopen(iFd, "r"); - if (d->gz == NULL) + compress_struct = compress_open(iFd, "r"); + if (compress_struct == NULL) return false; - Flags |= Compressed; - return true; - } + +#ifdef HAVE_ZLIB + if (compressor.Name == "gzip") + d->gz = (gzFile) compress_struct; #endif #ifdef HAVE_BZ2 - if (compressor.Name == "bzip2") - { - if (d->bz2 != NULL) - { - BZ2_bzclose(d->bz2); - d->bz2 = NULL; - } - if ((Mode & ReadWrite) == ReadWrite) - d->bz2 = BZ2_bzdopen(iFd, "r+"); - else if ((Mode & WriteOnly) == WriteOnly) - d->bz2 = BZ2_bzdopen(iFd, "w"); - else - d->bz2 = BZ2_bzdopen(iFd, "r"); - if (d->bz2 == NULL) - return false; + if (compressor.Name == "bzip2") + d->bz2 = (BZFILE*) compress_struct; +#endif Flags |= Compressed; return true; } -- cgit v1.2.3 From 7f350a377e0c65a656b9b5437e27d037fd742901 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 12 Mar 2014 14:39:18 +0100 Subject: use liblzma-dev to provide xz/lzma support We have xz/lzma support for a while, but only via an external binary provided by xz-utils. Now that the Debian archive provides xz by default and dpkg pre-depends on the library provided by liblzma-dev we can switch now to use this library as well to avoid requiring an external binary. For now the binary is in a prio:required package, but this might change in the future. API wise it is quiet similar to bz2 code expect that it doesn't provide file I/O methods, so we piece this together on our own. --- apt-pkg/contrib/fileutl.cc | 280 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 245 insertions(+), 35 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 057cb6a94..22730ec04 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -58,6 +58,9 @@ #ifdef HAVE_BZ2 #include #endif +#ifdef HAVE_LZMA + #include +#endif #ifdef WORDS_BIGENDIAN #include @@ -72,13 +75,47 @@ class FileFdPrivate { public: #ifdef HAVE_ZLIB gzFile gz; -#else - void* gz; #endif #ifdef HAVE_BZ2 BZFILE* bz2; -#else - void* bz2; +#endif +#ifdef HAVE_LZMA + struct LZMAFILE { + FILE* file; + uint8_t buffer[4096]; + lzma_stream stream; + lzma_ret err; + bool eof; + bool compressing; + + LZMAFILE() : file(NULL), eof(false), compressing(false) {} + ~LZMAFILE() { + if (compressing == true) + { + for (;;) { + stream.avail_out = sizeof(buffer)/sizeof(buffer[0]); + stream.next_out = buffer; + err = lzma_code(&stream, LZMA_FINISH); + if (err != LZMA_OK && err != LZMA_STREAM_END) + { + _error->Error("~LZMAFILE: Compress finalisation failed"); + break; + } + size_t const n = sizeof(buffer)/sizeof(buffer[0]) - stream.avail_out; + if (n && fwrite(buffer, 1, n, file) != n) + { + _error->Errno("~LZMAFILE",_("Write error")); + break; + } + if (err == LZMA_STREAM_END) + break; + } + } + lzma_end(&stream); + fclose(file); + } + }; + LZMAFILE* lzma; #endif int compressed_fd; pid_t compressor_pid; @@ -86,7 +123,16 @@ class FileFdPrivate { APT::Configuration::Compressor compressor; unsigned int openmode; unsigned long long seekpos; - FileFdPrivate() : gz(NULL), bz2(NULL), + FileFdPrivate() : +#ifdef HAVE_ZLIB + gz(NULL), +#endif +#ifdef HAVE_BZ2 + bz2(NULL), +#endif +#ifdef HAVE_LZMA + lzma(NULL), +#endif compressed_fd(-1), compressor_pid(-1), pipe(false), openmode(0), seekpos(0) {}; bool CloseDown(std::string const &FileName) @@ -106,6 +152,12 @@ class FileFdPrivate { BZ2_bzclose(bz2); bz2 = NULL; } +#endif +#ifdef HAVE_LZMA + if (lzma != NULL) { + delete lzma; + lzma = NULL; + } #endif if (compressor_pid > 0) ExecWait(compressor_pid, "FileFdCompressor", true); @@ -1089,12 +1141,14 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C if (compressor.Name == "." || compressor.Binary.empty() == true) return true; -#if defined HAVE_ZLIB || defined HAVE_BZ2 +#if defined HAVE_ZLIB || defined HAVE_BZ2 || defined HAVE_LZMA // the API to open files is similar, so setup to avoid code duplicates later // and while at it ensure that we close before opening (if its a reopen) void* (*compress_open)(int, const char *) = NULL; + if (false) + /* dummy so that the rest can be 'else if's */; #define APT_COMPRESS_INIT(NAME,OPEN,CLOSE,STRUCT) \ - if (compressor.Name == NAME) \ + else if (compressor.Name == NAME) \ { \ compress_open = (void*(*)(int, const char *)) OPEN; \ if (d != NULL && STRUCT != NULL) { CLOSE(STRUCT); STRUCT = NULL; } \ @@ -1105,6 +1159,17 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C #ifdef HAVE_BZ2 APT_COMPRESS_INIT("bzip2", BZ2_bzdopen, BZ2_bzclose, d->bz2) #endif +#ifdef HAVE_LZMA + else if (compressor.Name == "xz" || compressor.Name == "lzma") + { + compress_open = (void*(*)(int, const char*)) fdopen; + if (d != NULL && d->lzma != NULL) + { + delete d->lzma; + d->lzma = NULL; + } + } +#endif #undef APT_COMPRESS_INIT #endif @@ -1113,7 +1178,7 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C d = new FileFdPrivate(); d->openmode = Mode; d->compressor = compressor; -#if defined HAVE_ZLIB || defined HAVE_BZ2 +#if defined HAVE_ZLIB || defined HAVE_BZ2 || defined HAVE_LZMA if (AutoClose == false && compress_open != NULL) { // Need to duplicate fd here or gz/bz2 close for cleanup will close the fd as well @@ -1125,7 +1190,7 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C #endif } -#if defined HAVE_ZLIB || defined HAVE_BZ2 +#if defined HAVE_ZLIB || defined HAVE_BZ2 || defined HAVE_LZMA if (compress_open != NULL) { void* compress_struct = NULL; @@ -1138,13 +1203,60 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C if (compress_struct == NULL) return false; + if (false) + /* dummy so that the rest can be 'else if's */; #ifdef HAVE_ZLIB - if (compressor.Name == "gzip") + else if (compressor.Name == "gzip") d->gz = (gzFile) compress_struct; #endif #ifdef HAVE_BZ2 - if (compressor.Name == "bzip2") + else if (compressor.Name == "bzip2") d->bz2 = (BZFILE*) compress_struct; +#endif +#ifdef HAVE_LZMA + else if (compressor.Name == "xz" || compressor.Name == "lzma") + { + uint32_t const xzlevel = 6; + uint64_t const memlimit = UINT64_MAX; + if (d->lzma == NULL) + d->lzma = new FileFdPrivate::LZMAFILE; + d->lzma->file = (FILE*) compress_struct; + d->lzma->stream = LZMA_STREAM_INIT; + + if ((Mode & ReadWrite) == ReadWrite) + return FileFdError("ReadWrite mode is not supported for file %s", FileName.c_str()); + + if ((Mode & WriteOnly) == WriteOnly) + { + if (compressor.Name == "xz") + { + if (lzma_easy_encoder(&d->lzma->stream, xzlevel, LZMA_CHECK_CRC32) != LZMA_OK) + return false; + } + else + { + lzma_options_lzma options; + lzma_lzma_preset(&options, xzlevel); + if (lzma_alone_encoder(&d->lzma->stream, &options) != LZMA_OK) + return false; + } + d->lzma->compressing = true; + } + else + { + if (compressor.Name == "xz") + { + if (lzma_auto_decoder(&d->lzma->stream, memlimit, 0) != LZMA_OK) + return false; + } + else + { + if (lzma_alone_decoder(&d->lzma->stream, memlimit) != LZMA_OK) + return false; + } + d->lzma->compressing = false; + } + } #endif Flags |= Compressed; return true; @@ -1202,7 +1314,7 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C } else { - if (FileName.empty() == true) + if (d->compressed_fd != -1) dup2(d->compressed_fd,STDIN_FILENO); dup2(Pipe[1],STDOUT_FILENO); } @@ -1271,24 +1383,55 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) *((char *)To) = '\0'; do { + if (false) + /* dummy so that the rest can be 'else if's */; #ifdef HAVE_ZLIB - if (d != NULL && d->gz != NULL) + else if (d != NULL && d->gz != NULL) Res = gzread(d->gz,To,Size); - else #endif #ifdef HAVE_BZ2 - if (d != NULL && d->bz2 != NULL) + else if (d != NULL && d->bz2 != NULL) Res = BZ2_bzread(d->bz2,To,Size); - else #endif +#ifdef HAVE_LZMA + else if (d != NULL && d->lzma != NULL) + { + if (d->lzma->eof == true) + break; + + d->lzma->stream.next_out = (uint8_t *) To; + d->lzma->stream.avail_out = Size; + if (d->lzma->stream.avail_in == 0) + { + d->lzma->stream.next_in = d->lzma->buffer; + d->lzma->stream.avail_in = fread(d->lzma->buffer, 1, sizeof(d->lzma->buffer)/sizeof(d->lzma->buffer[0]), d->lzma->file); + } + d->lzma->err = lzma_code(&d->lzma->stream, LZMA_RUN); + if (d->lzma->err == LZMA_STREAM_END) + { + d->lzma->eof = true; + Res = Size - d->lzma->stream.avail_out; + } + else if (d->lzma->err != LZMA_OK) + { + Res = -1; + errno = 0; + } + else + Res = Size - d->lzma->stream.avail_out; + } +#endif + else Res = read(iFd,To,Size); if (Res < 0) { if (errno == EINTR) continue; + if (false) + /* dummy so that the rest can be 'else if's */; #ifdef HAVE_ZLIB - if (d != NULL && d->gz != NULL) + else if (d != NULL && d->gz != NULL) { int err; char const * const errmsg = gzerror(d->gz, &err); @@ -1297,13 +1440,17 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) } #endif #ifdef HAVE_BZ2 - if (d != NULL && d->bz2 != NULL) + else if (d != NULL && d->bz2 != NULL) { int err; char const * const errmsg = BZ2_bzerror(d->bz2, &err); if (err != BZ_IO_ERROR) return FileFdError("BZ2_bzread: %s (%d: %s)", _("Read error"), err, errmsg); } +#endif +#ifdef HAVE_LZMA + else if (d != NULL && d->lzma != NULL) + return FileFdError("lzma_read: %s (%d)", _("Read error"), d->lzma->err); #endif return FileFdErrno("read",_("Read error")); } @@ -1368,23 +1515,45 @@ bool FileFd::Write(const void *From,unsigned long long Size) errno = 0; do { + if (false) + /* dummy so that the rest can be 'else if's */; #ifdef HAVE_ZLIB - if (d != NULL && d->gz != NULL) - Res = gzwrite(d->gz,From,Size); - else + else if (d != NULL && d->gz != NULL) + Res = gzwrite(d->gz,From,Size); #endif #ifdef HAVE_BZ2 - if (d != NULL && d->bz2 != NULL) - Res = BZ2_bzwrite(d->bz2,(void*)From,Size); - else + else if (d != NULL && d->bz2 != NULL) + Res = BZ2_bzwrite(d->bz2,(void*)From,Size); #endif - Res = write(iFd,From,Size); +#ifdef HAVE_LZMA + else if (d != NULL && d->lzma != NULL) + { + d->lzma->stream.next_in = (uint8_t *)From; + d->lzma->stream.avail_in = Size; + d->lzma->stream.next_out = d->lzma->buffer; + d->lzma->stream.avail_out = sizeof(d->lzma->buffer)/sizeof(d->lzma->buffer[0]); + d->lzma->err = lzma_code(&d->lzma->stream, LZMA_RUN); + if (d->lzma->err != LZMA_OK) + return false; + size_t const n = sizeof(d->lzma->buffer)/sizeof(d->lzma->buffer[0]) - d->lzma->stream.avail_out; + size_t const m = (n == 0) ? 0 : fwrite(d->lzma->buffer, 1, n, d->lzma->file); + if (m != n) + Res = -1; + else + Res = Size - d->lzma->stream.avail_in; + } +#endif + else + Res = write(iFd,From,Size); + if (Res < 0 && errno == EINTR) continue; if (Res < 0) { + if (false) + /* dummy so that the rest can be 'else if's */; #ifdef HAVE_ZLIB - if (d != NULL && d->gz != NULL) + else if (d != NULL && d->gz != NULL) { int err; char const * const errmsg = gzerror(d->gz, &err); @@ -1393,13 +1562,17 @@ bool FileFd::Write(const void *From,unsigned long long Size) } #endif #ifdef HAVE_BZ2 - if (d != NULL && d->bz2 != NULL) + else if (d != NULL && d->bz2 != NULL) { int err; char const * const errmsg = BZ2_bzerror(d->bz2, &err); if (err != BZ_IO_ERROR) return FileFdError("BZ2_bzwrite: %s (%d: %s)", _("Write error"), err, errmsg); } +#endif +#ifdef HAVE_LZMA + else if (d != NULL && d->lzma != NULL) + return FileFdErrno("lzma_fwrite", _("Write error")); #endif return FileFdErrno("write",_("Write error")); } @@ -1447,6 +1620,9 @@ bool FileFd::Seek(unsigned long long To) if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 || d->bz2 != NULL +#endif +#ifdef HAVE_LZMA + || d->lzma != NULL #endif )) { @@ -1459,12 +1635,21 @@ bool FileFd::Seek(unsigned long long To) if ((d->openmode & ReadOnly) != ReadOnly) return FileFdError("Reopen is only implemented for read-only files!"); + if (false) + /* dummy so that the rest can be 'else if's */; #ifdef HAVE_BZ2 - if (d->bz2 != NULL) - { - BZ2_bzclose(d->bz2); - d->bz2 = NULL; - } + else if (d->bz2 != NULL) + { + BZ2_bzclose(d->bz2); + d->bz2 = NULL; + } +#endif +#ifdef HAVE_LZMA + else if (d->lzma != NULL) + { + delete d->lzma; + d->lzma = NULL; + } #endif if (iFd != -1) close(iFd); @@ -1514,6 +1699,9 @@ bool FileFd::Skip(unsigned long long Over) if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 || d->bz2 != NULL +#endif +#ifdef HAVE_LZMA + || d->lzma != NULL #endif )) { @@ -1552,8 +1740,18 @@ bool FileFd::Truncate(unsigned long long To) // truncating /dev/null is always successful - as we get an error otherwise if (To == 0 && FileName == "/dev/null") return true; -#if defined HAVE_ZLIB || defined HAVE_BZ2 - if (d != NULL && (d->gz != NULL || d->bz2 != NULL)) +#if defined HAVE_ZLIB || defined HAVE_BZ2 || defined HAVE_LZMA + if (d != NULL && ( +#ifdef HAVE_ZLIB + d->gz != NULL || +#endif +#ifdef HAVE_BZ2 + d->bz2 != NULL || +#endif +#ifdef HAVE_LZMA + d->lzma != NULL || +#endif + false)) return FileFdError("Truncating compressed files is not implemented (%s)", FileName.c_str()); #endif if (ftruncate(iFd,To) != 0) @@ -1574,6 +1772,9 @@ unsigned long long FileFd::Tell() if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 || d->bz2 != NULL +#endif +#ifdef HAVE_LZMA + || d->lzma != NULL #endif )) return d->seekpos; @@ -1653,6 +1854,9 @@ unsigned long long FileFd::Size() if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 || (d->bz2 && size > 0) +#endif +#ifdef HAVE_LZMA + || (d->lzma && size > 0) #endif )) { @@ -1797,7 +2001,13 @@ bool FileFd::FileFdError(const char *Description,...) { } /*}}}*/ -gzFile FileFd::gzFd() { return d->gz; } +APT_DEPRECATED gzFile FileFd::gzFd() { +#ifdef HAVE_ZLIB + return d->gz; +#else + return NULL; +#endif +} // Glob - wrapper around "glob()" /*{{{*/ -- cgit v1.2.3 From 4239dbca053512b6f8357ed143c24c50685b890f Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 12 Mar 2014 16:16:14 +0100 Subject: refactor FileFd to hide some #ifdefs They tend to be ugly to look at, so hide them. Git-Dch: Ignore --- apt-pkg/contrib/fileutl.cc | 299 ++++++++++++++++++++------------------------- 1 file changed, 131 insertions(+), 168 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 22730ec04..26945c183 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1,6 +1,5 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: fileutl.cc,v 1.42 2002/09/14 05:29:22 jgg Exp $ /* ###################################################################### File Utilities @@ -59,6 +58,7 @@ #include #endif #ifdef HAVE_LZMA + #include #include #endif @@ -71,103 +71,6 @@ using namespace std; -class FileFdPrivate { - public: -#ifdef HAVE_ZLIB - gzFile gz; -#endif -#ifdef HAVE_BZ2 - BZFILE* bz2; -#endif -#ifdef HAVE_LZMA - struct LZMAFILE { - FILE* file; - uint8_t buffer[4096]; - lzma_stream stream; - lzma_ret err; - bool eof; - bool compressing; - - LZMAFILE() : file(NULL), eof(false), compressing(false) {} - ~LZMAFILE() { - if (compressing == true) - { - for (;;) { - stream.avail_out = sizeof(buffer)/sizeof(buffer[0]); - stream.next_out = buffer; - err = lzma_code(&stream, LZMA_FINISH); - if (err != LZMA_OK && err != LZMA_STREAM_END) - { - _error->Error("~LZMAFILE: Compress finalisation failed"); - break; - } - size_t const n = sizeof(buffer)/sizeof(buffer[0]) - stream.avail_out; - if (n && fwrite(buffer, 1, n, file) != n) - { - _error->Errno("~LZMAFILE",_("Write error")); - break; - } - if (err == LZMA_STREAM_END) - break; - } - } - lzma_end(&stream); - fclose(file); - } - }; - LZMAFILE* lzma; -#endif - int compressed_fd; - pid_t compressor_pid; - bool pipe; - APT::Configuration::Compressor compressor; - unsigned int openmode; - unsigned long long seekpos; - FileFdPrivate() : -#ifdef HAVE_ZLIB - gz(NULL), -#endif -#ifdef HAVE_BZ2 - bz2(NULL), -#endif -#ifdef HAVE_LZMA - lzma(NULL), -#endif - compressed_fd(-1), compressor_pid(-1), pipe(false), - openmode(0), seekpos(0) {}; - bool CloseDown(std::string const &FileName) - { - bool Res = true; -#ifdef HAVE_ZLIB - if (gz != NULL) { - int const e = gzclose(gz); - gz = NULL; - // gzdclose() on empty files always fails with "buffer error" here, ignore that - if (e != 0 && e != Z_BUF_ERROR) - Res &= _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str()); - } -#endif -#ifdef HAVE_BZ2 - if (bz2 != NULL) { - BZ2_bzclose(bz2); - bz2 = NULL; - } -#endif -#ifdef HAVE_LZMA - if (lzma != NULL) { - delete lzma; - lzma = NULL; - } -#endif - if (compressor_pid > 0) - ExecWait(compressor_pid, "FileFdCompressor", true); - compressor_pid = -1; - - return Res; - } - ~FileFdPrivate() { CloseDown(""); } -}; - // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -932,6 +835,122 @@ bool ExecWait(pid_t Pid,const char *Name,bool Reap) } /*}}}*/ +class FileFdPrivate { /*{{{*/ + public: +#ifdef HAVE_ZLIB + gzFile gz; +#endif +#ifdef HAVE_BZ2 + BZFILE* bz2; +#endif +#ifdef HAVE_LZMA + struct LZMAFILE { + FILE* file; + uint8_t buffer[4096]; + lzma_stream stream; + lzma_ret err; + bool eof; + bool compressing; + + LZMAFILE() : file(NULL), eof(false), compressing(false) {} + ~LZMAFILE() { + if (compressing == true) + { + for (;;) { + stream.avail_out = sizeof(buffer)/sizeof(buffer[0]); + stream.next_out = buffer; + err = lzma_code(&stream, LZMA_FINISH); + if (err != LZMA_OK && err != LZMA_STREAM_END) + { + _error->Error("~LZMAFILE: Compress finalisation failed"); + break; + } + size_t const n = sizeof(buffer)/sizeof(buffer[0]) - stream.avail_out; + if (n && fwrite(buffer, 1, n, file) != n) + { + _error->Errno("~LZMAFILE",_("Write error")); + break; + } + if (err == LZMA_STREAM_END) + break; + } + } + lzma_end(&stream); + fclose(file); + } + }; + LZMAFILE* lzma; +#endif + int compressed_fd; + pid_t compressor_pid; + bool pipe; + APT::Configuration::Compressor compressor; + unsigned int openmode; + unsigned long long seekpos; + FileFdPrivate() : +#ifdef HAVE_ZLIB + gz(NULL), +#endif +#ifdef HAVE_BZ2 + bz2(NULL), +#endif +#ifdef HAVE_LZMA + lzma(NULL), +#endif + compressed_fd(-1), compressor_pid(-1), pipe(false), + openmode(0), seekpos(0) {}; + bool InternalClose(std::string const &FileName) + { + if (false) + /* dummy so that the rest can be 'else if's */; +#ifdef HAVE_ZLIB + else if (gz != NULL) { + int const e = gzclose(gz); + gz = NULL; + // gzdclose() on empty files always fails with "buffer error" here, ignore that + if (e != 0 && e != Z_BUF_ERROR) + return _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str()); + } +#endif +#ifdef HAVE_BZ2 + else if (bz2 != NULL) { + BZ2_bzclose(bz2); + bz2 = NULL; + } +#endif +#ifdef HAVE_LZMA + else if (lzma != NULL) { + delete lzma; + lzma = NULL; + } +#endif + return true; + } + bool CloseDown(std::string const &FileName) + { + bool const Res = InternalClose(FileName); + + if (compressor_pid > 0) + ExecWait(compressor_pid, "FileFdCompressor", true); + compressor_pid = -1; + + return Res; + } + bool InternalStream() const { + return false +#ifdef HAVE_BZ2 + || bz2 != NULL +#endif +#ifdef HAVE_LZMA + || lzma != NULL +#endif + ; + } + + + ~FileFdPrivate() { CloseDown(""); } +}; + /*}}}*/ // FileFd::Open - Open a file /*{{{*/ // --------------------------------------------------------------------- /* The most commonly used open mode combinations are given with Mode */ @@ -1147,28 +1166,21 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C void* (*compress_open)(int, const char *) = NULL; if (false) /* dummy so that the rest can be 'else if's */; -#define APT_COMPRESS_INIT(NAME,OPEN,CLOSE,STRUCT) \ +#define APT_COMPRESS_INIT(NAME,OPEN) \ else if (compressor.Name == NAME) \ { \ compress_open = (void*(*)(int, const char *)) OPEN; \ - if (d != NULL && STRUCT != NULL) { CLOSE(STRUCT); STRUCT = NULL; } \ + if (d != NULL) d->InternalClose(FileName); \ } #ifdef HAVE_ZLIB - APT_COMPRESS_INIT("gzip", gzdopen, gzclose, d->gz) + APT_COMPRESS_INIT("gzip", gzdopen) #endif #ifdef HAVE_BZ2 - APT_COMPRESS_INIT("bzip2", BZ2_bzdopen, BZ2_bzclose, d->bz2) + APT_COMPRESS_INIT("bzip2", BZ2_bzdopen) #endif #ifdef HAVE_LZMA - else if (compressor.Name == "xz" || compressor.Name == "lzma") - { - compress_open = (void*(*)(int, const char*)) fdopen; - if (d != NULL && d->lzma != NULL) - { - delete d->lzma; - d->lzma = NULL; - } - } + APT_COMPRESS_INIT("xz", fdopen) + APT_COMPRESS_INIT("lzma", fdopen) #endif #undef APT_COMPRESS_INIT #endif @@ -1617,14 +1629,7 @@ bool FileFd::Write(int Fd, const void *From, unsigned long long Size) /* */ bool FileFd::Seek(unsigned long long To) { - if (d != NULL && (d->pipe == true -#ifdef HAVE_BZ2 - || d->bz2 != NULL -#endif -#ifdef HAVE_LZMA - || d->lzma != NULL -#endif - )) + if (d != NULL && (d->pipe == true || d->InternalStream() == true)) { // Our poor man seeking in pipes is costly, so try to avoid it unsigned long long seekpos = Tell(); @@ -1635,22 +1640,7 @@ bool FileFd::Seek(unsigned long long To) if ((d->openmode & ReadOnly) != ReadOnly) return FileFdError("Reopen is only implemented for read-only files!"); - if (false) - /* dummy so that the rest can be 'else if's */; -#ifdef HAVE_BZ2 - else if (d->bz2 != NULL) - { - BZ2_bzclose(d->bz2); - d->bz2 = NULL; - } -#endif -#ifdef HAVE_LZMA - else if (d->lzma != NULL) - { - delete d->lzma; - d->lzma = NULL; - } -#endif + d->InternalClose(FileName); if (iFd != -1) close(iFd); iFd = -1; @@ -1696,14 +1686,7 @@ bool FileFd::Seek(unsigned long long To) /* */ bool FileFd::Skip(unsigned long long Over) { - if (d != NULL && (d->pipe == true -#ifdef HAVE_BZ2 - || d->bz2 != NULL -#endif -#ifdef HAVE_LZMA - || d->lzma != NULL -#endif - )) + if (d != NULL && (d->pipe == true || d->InternalStream() == true)) { d->seekpos += Over; char buffer[1024]; @@ -1741,17 +1724,11 @@ bool FileFd::Truncate(unsigned long long To) if (To == 0 && FileName == "/dev/null") return true; #if defined HAVE_ZLIB || defined HAVE_BZ2 || defined HAVE_LZMA - if (d != NULL && ( + if (d != NULL && (d->InternalStream() == true #ifdef HAVE_ZLIB - d->gz != NULL || -#endif -#ifdef HAVE_BZ2 - d->bz2 != NULL || + || d->gz != NULL #endif -#ifdef HAVE_LZMA - d->lzma != NULL || -#endif - false)) + )) return FileFdError("Truncating compressed files is not implemented (%s)", FileName.c_str()); #endif if (ftruncate(iFd,To) != 0) @@ -1769,14 +1746,7 @@ unsigned long long FileFd::Tell() // 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 != NULL && (d->pipe == true -#ifdef HAVE_BZ2 - || d->bz2 != NULL -#endif -#ifdef HAVE_LZMA - || d->lzma != NULL -#endif - )) + if (d != NULL && (d->pipe == true || d->InternalStream() == true)) return d->seekpos; off_t Res; @@ -1851,14 +1821,7 @@ unsigned long long FileFd::Size() // for compressor pipes st_size is undefined and at 'best' zero, // so we 'read' the content and 'seek' back - see there - if (d != NULL && (d->pipe == true -#ifdef HAVE_BZ2 - || (d->bz2 && size > 0) -#endif -#ifdef HAVE_LZMA - || (d->lzma && size > 0) -#endif - )) + if (d != NULL && (d->pipe == true || (d->InternalStream() == true && size > 0))) { unsigned long long const oldSeek = Tell(); char ignore[1000]; -- cgit v1.2.3