From 98ee7cd35cf205c52b3698ee91cec76d704a3937 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Fri, 25 Jun 2010 08:01:48 +0200 Subject: * apt-pkg/contrib/error.{cc,h}: - complete rewrite but use the same API - add NOTICE and DEBUG as new types of a message --- apt-pkg/contrib/error.cc | 333 ++++++++++++++++++++++------------------------- apt-pkg/contrib/error.h | 230 +++++++++++++++++++++++++++----- 2 files changed, 356 insertions(+), 207 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/error.cc b/apt-pkg/contrib/error.cc index 927b7e05c..837d9e615 100644 --- a/apt-pkg/contrib/error.cc +++ b/apt-pkg/contrib/error.cc @@ -1,16 +1,15 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: error.cc,v 1.11 2002/03/26 07:38:58 jgg Exp $ /* ###################################################################### - - Global Erorr Class - Global error mechanism + + Global Error Class - Global error mechanism We use a simple STL vector to store each error record. A PendingFlag is kept which indicates when the vector contains a Sever error. - + This source is placed in the Public Domain, do with it what you will It was originally written by Jason Gunthorpe. - + ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ @@ -19,7 +18,6 @@ #include #include #include -#include #include #include @@ -28,209 +26,188 @@ #include "config.h" /*}}}*/ -using namespace std; - // Global Error Object /*{{{*/ /* If the implementation supports posix threads then the accessor function is compiled to be thread safe otherwise a non-safe version is used. A Per-Thread error object is maintained in much the same manner as libc manages errno */ #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD) - #include - - static pthread_key_t ErrorKey; - static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;}; - static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);}; - - GlobalError *_GetErrorObj() - { - static pthread_once_t Once = PTHREAD_ONCE_INIT; - pthread_once(&Once,KeyAlloc); - - void *Res = pthread_getspecific(ErrorKey); - if (Res == 0) - pthread_setspecific(ErrorKey,Res = new GlobalError); - return (GlobalError *)Res; - } + #include + + static pthread_key_t ErrorKey; + static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;}; + static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);}; + + GlobalError *_GetErrorObj() { + static pthread_once_t Once = PTHREAD_ONCE_INIT; + pthread_once(&Once,KeyAlloc); + + void *Res = pthread_getspecific(ErrorKey); + if (Res == 0) + pthread_setspecific(ErrorKey,Res = new GlobalError); + return (GlobalError *)Res; + } #else - GlobalError *_GetErrorObj() - { - static GlobalError *Obj = new GlobalError; - return Obj; - } + GlobalError *_GetErrorObj() { + static GlobalError *Obj = new GlobalError; + return Obj; + } #endif /*}}}*/ - // GlobalError::GlobalError - Constructor /*{{{*/ -// --------------------------------------------------------------------- -/* */ -GlobalError::GlobalError() : List(0), PendingFlag(false) -{ +GlobalError::GlobalError() : PendingFlag(false) {} + /*}}}*/ +// GlobalError::FatalE - Get part of the error string from errno /*{{{*/ +bool GlobalError::FatalE(const char *Function,const char *Description,...) { + va_list args; + va_start(args,Description); + return InsertErrno(FATAL, Function, Description, args); } /*}}}*/ // GlobalError::Errno - Get part of the error string from errno /*{{{*/ -// --------------------------------------------------------------------- -/* Function indicates the stdlib function that failed and Description is - a user string that leads the text. Form is: - Description - Function (errno: strerror) - Carefull of the buffer overrun, sprintf. - */ -bool GlobalError::Errno(const char *Function,const char *Description,...) -{ - va_list args; - va_start(args,Description); - - // sprintf the description - char S[400]; - vsnprintf(S,sizeof(S),Description,args); - snprintf(S + strlen(S),sizeof(S) - strlen(S), - " - %s (%i: %s)",Function,errno,strerror(errno)); - - // Put it on the list - Item *Itm = new Item; - Itm->Text = S; - Itm->Error = true; - Insert(Itm); - - PendingFlag = true; - - return false; +bool GlobalError::Errno(const char *Function,const char *Description,...) { + va_list args; + va_start(args,Description); + return InsertErrno(ERROR, Function, Description, args); } /*}}}*/ -// GlobalError::WarningE - Get part of the warn string from errno /*{{{*/ -// --------------------------------------------------------------------- -/* Function indicates the stdlib function that failed and Description is - a user string that leads the text. Form is: - Description - Function (errno: strerror) - Carefull of the buffer overrun, sprintf. - */ -bool GlobalError::WarningE(const char *Function,const char *Description,...) -{ - va_list args; - va_start(args,Description); - - // sprintf the description - char S[400]; - vsnprintf(S,sizeof(S),Description,args); - snprintf(S + strlen(S),sizeof(S) - strlen(S), - " - %s (%i: %s)",Function,errno,strerror(errno)); - - // Put it on the list - Item *Itm = new Item; - Itm->Text = S; - Itm->Error = false; - Insert(Itm); - - return false; +// GlobalError::WarningE - Get part of the warning string from errno /*{{{*/ +bool GlobalError::WarningE(const char *Function,const char *Description,...) { + va_list args; + va_start(args,Description); + return InsertErrno(WARNING, Function, Description, args); +} + /*}}}*/ +// GlobalError::NoticeE - Get part of the notice string from errno /*{{{*/ +bool GlobalError::NoticeE(const char *Function,const char *Description,...) { + va_list args; + va_start(args,Description); + return InsertErrno(NOTICE, Function, Description, args); +} + /*}}}*/ +// GlobalError::DebugE - Get part of the debug string from errno /*{{{*/ +bool GlobalError::DebugE(const char *Function,const char *Description,...) { + va_list args; + va_start(args,Description); + return InsertErrno(DEBUG, Function, Description, args); +} + /*}}}*/ +// GlobalError::InsertErrno - formats an error message with the errno /*{{{*/ +bool GlobalError::InsertErrno(MsgType type, const char* Function, + const char* Description, va_list const &args) { + char S[400]; + vsnprintf(S,sizeof(S),Description,args); + snprintf(S + strlen(S),sizeof(S) - strlen(S), + " - %s (%i: %s)", Function, errno, strerror(errno)); + return Insert(type, S, args); +} + /*}}}*/ +// GlobalError::Fatal - Add a fatal error to the list /*{{{*/ +bool GlobalError::Fatal(const char *Description,...) { + va_list args; + va_start(args,Description); + return Insert(FATAL, Description, args); } /*}}}*/ // GlobalError::Error - Add an error to the list /*{{{*/ -// --------------------------------------------------------------------- -/* Just vsprintfs and pushes */ -bool GlobalError::Error(const char *Description,...) -{ - va_list args; - va_start(args,Description); - - // sprintf the description - char S[400]; - vsnprintf(S,sizeof(S),Description,args); - - // Put it on the list - Item *Itm = new Item; - Itm->Text = S; - Itm->Error = true; - Insert(Itm); - - PendingFlag = true; - - return false; +bool GlobalError::Error(const char *Description,...) { + va_list args; + va_start(args,Description); + return Insert(ERROR, Description, args); } /*}}}*/ // GlobalError::Warning - Add a warning to the list /*{{{*/ -// --------------------------------------------------------------------- -/* This doesn't set the pending error flag */ -bool GlobalError::Warning(const char *Description,...) +bool GlobalError::Warning(const char *Description,...) { + va_list args; + va_start(args,Description); + return Insert(WARNING, Description, args); +} + /*}}}*/ +// GlobalError::Notice - Add a notice to the list /*{{{*/ +bool GlobalError::Notice(const char *Description,...) { - va_list args; - va_start(args,Description); - - // sprintf the description - char S[400]; - vsnprintf(S,sizeof(S),Description,args); - - // Put it on the list - Item *Itm = new Item; - Itm->Text = S; - Itm->Error = false; - Insert(Itm); - - return false; + va_list args; + va_start(args,Description); + return Insert(NOTICE, Description, args); } /*}}}*/ -// GlobalError::PopMessage - Pulls a single message out /*{{{*/ -// --------------------------------------------------------------------- -/* This should be used in a loop checking empty() each cycle. It returns - true if the message is an error. */ -bool GlobalError::PopMessage(string &Text) +// GlobalError::Debug - Add a debug to the list /*{{{*/ +bool GlobalError::Debug(const char *Description,...) { - if (List == 0) - return false; - - bool Ret = List->Error; - Text = List->Text; - Item *Old = List; - List = List->Next; - delete Old; - - // This really should check the list to see if only warnings are left.. - if (List == 0) - PendingFlag = false; - - return Ret; + va_list args; + va_start(args,Description); + return Insert(DEBUG, Description, args); +} + /*}}}*/ +// GlobalError::Insert - Insert a new item at the end /*{{{*/ +bool GlobalError::Insert(MsgType type, const char* Description, + va_list const &args) { + char S[400]; + vsnprintf(S,sizeof(S),Description,args); + + Item const m(S, type); + Messages.push_back(m); + + if (type == ERROR || type == FATAL) + PendingFlag = true; + + if (type == FATAL || type == DEBUG) + std::clog << m << std::endl; + + return false; +} + /*}}}*/ +// GlobalError::PopMessage - Pulls a single message out /*{{{*/ +bool GlobalError::PopMessage(std::string &Text) { + if (Messages.empty() == true) + return false; + + Item const msg = Messages.front(); + Messages.pop_front(); + + bool const Ret = (msg.Type == ERROR || msg.Type == FATAL); + Text = msg.Text; + if (PendingFlag == false || Ret == false) + return Ret; + + // check if another error message is pending + for (std::list::const_iterator m = Messages.begin(); + m != Messages.end(); m++) + if (m->Type == ERROR || m->Type == FATAL) + return Ret; + + PendingFlag = false; + return Ret; } /*}}}*/ // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/ -// --------------------------------------------------------------------- -/* */ -void GlobalError::DumpErrors() -{ - // Print any errors or warnings found - string Err; - while (empty() == false) - { - bool Type = PopMessage(Err); - if (Type == true) - cerr << "E: " << Err << endl; - else - cerr << "W: " << Err << endl; - } +void GlobalError::DumpErrors(std::ostream &out, MsgType const &trashhold) { + for (std::list::const_iterator m = Messages.begin(); + m != Messages.end(); m++) + if (m->Type >= trashhold) + out << (*m) << std::endl; + Discard(); } /*}}}*/ -// GlobalError::Discard - Discard /*{{{*/ -// --------------------------------------------------------------------- -/* */ -void GlobalError::Discard() -{ - while (List != 0) - { - Item *Old = List; - List = List->Next; - delete Old; - } - - PendingFlag = false; +// GlobalError::Discard - Discard /*{{{*/ +void GlobalError::Discard() { + Messages.clear(); + PendingFlag = false; }; /*}}}*/ -// GlobalError::Insert - Insert a new item at the end /*{{{*/ -// --------------------------------------------------------------------- -/* */ -void GlobalError::Insert(Item *Itm) -{ - Item **End = &List; - for (Item *I = List; I != 0; I = I->Next) - End = &I->Next; - Itm->Next = *End; - *End = Itm; +// GlobalError::empty - does our error list include anything? /*{{{*/ +bool GlobalError::empty(MsgType const &trashhold) const { + if (PendingFlag == true) + return false; + + if (Messages.empty() == true) + return true; + + for (std::list::const_iterator m = Messages.begin(); + m != Messages.end(); m++) + if (m->Type >= trashhold) + return false; + + return true; } /*}}}*/ diff --git a/apt-pkg/contrib/error.h b/apt-pkg/contrib/error.h index 8d5ec05ea..fc7b38f1b 100644 --- a/apt-pkg/contrib/error.h +++ b/apt-pkg/contrib/error.h @@ -42,43 +42,215 @@ #include +#include +#include #include -class GlobalError +#include + +class GlobalError /*{{{*/ { - struct Item - { - std::string Text; - bool Error; - Item *Next; - }; - - Item *List; - bool PendingFlag; - void Insert(Item *I); - - public: +public: /*{{{*/ + /** \brief a message can have one of following severity */ + enum MsgType { + /** \brief Message will be printed instantly as it is likely that + this error will lead to a complete crash */ + FATAL = 40, + /** \brief An error does hinder the correct execution and should be corrected */ + ERROR = 30, + /** \brief indicates problem that can lead to errors later on */ + WARNING = 20, + /** \brief deprecation warnings, old fallback behavior, … */ + NOTICE = 10, + /** \brief for developers only in areas it is hard to print something directly */ + DEBUG = 0 + }; - // Call to generate an error from a library call. - bool Errno(const char *Function,const char *Description,...) __like_printf(3) __cold; - bool WarningE(const char *Function,const char *Description,...) __like_printf(3) __cold; + /** \brief add a fatal error message with errno to the list + * + * \param Function name of the function generating the error + * \param Description format string for the error message + * + * \return \b false + */ + bool FatalE(const char *Function,const char *Description,...) __like_printf(3) __cold; - /* A warning should be considered less severe than an error, and may be - ignored by the client. */ - bool Error(const char *Description,...) __like_printf(2) __cold; - bool Warning(const char *Description,...) __like_printf(2) __cold; + /** \brief add an Error message with errno to the list + * + * \param Function name of the function generating the error + * \param Description format string for the error message + * + * \return \b false + */ + bool Errno(const char *Function,const char *Description,...) __like_printf(3) __cold; - // Simple accessors - inline bool PendingError() {return PendingFlag;}; - inline bool empty() {return List == 0;}; - bool PopMessage(std::string &Text); - void Discard(); + /** \brief add a warning message with errno to the list + * + * A warning should be considered less severe than an error and + * may be ignored by the client. + * + * \param Function Name of the function generates the warning. + * \param Description Format string for the warning message. + * + * \return \b false + */ + bool WarningE(const char *Function,const char *Description,...) __like_printf(3) __cold; - // Usefull routine to dump to cerr - void DumpErrors(); - - GlobalError(); + /** \brief add a notice message with errno to the list + * + * \param Function name of the function generating the error + * \param Description format string for the error message + * + * \return \b false + */ + bool NoticeE(const char *Function,const char *Description,...) __like_printf(3) __cold; + + /** \brief add a debug message with errno to the list + * + * \param Function name of the function generating the error + * \param Description format string for the error message + * + * \return \b false + */ + bool DebugE(const char *Function,const char *Description,...) __like_printf(3) __cold; + + /** \brief add an fatal error message to the list + * + * Most of the stuff we consider as "error" is also "fatal" for + * the user as the application will not have the expected result, + * but a fatal message here means that it gets printed directly + * to stderr in addiction to adding it to the list as the error + * leads sometimes to crashes and a maybe duplicated message + * is better than "Segfault" as the only displayed text + * + * \param Description Format string for the fatal error message. + * + * \return \b false + */ + bool Fatal(const char *Description,...) __like_printf(2) __cold; + + /** \brief add an Error message to the list + * + * \param Description Format string for the error message. + * + * \return \b false + */ + bool Error(const char *Description,...) __like_printf(2) __cold; + + /** \brief add a warning message to the list + * + * A warning should be considered less severe than an error and + * may be ignored by the client. + * + * \param Description Format string for the message + * + * \return \b false + */ + bool Warning(const char *Description,...) __like_printf(2) __cold; + + /** \brief add a notice message to the list + * + * A notice should be considered less severe than an error or a + * warning and can be ignored by the client without further problems + * for some times, but he should consider fixing the problem. + * This error type can be used for e.g. deprecation warnings of options. + * + * \param Description Format string for the message + * + * \return \b false + */ + bool Notice(const char *Description,...) __like_printf(2) __cold; + + /** \brief add a debug message to the list + * + * \param Description Format string for the message + * + * \return \b false + */ + bool Debug(const char *Description,...) __like_printf(2) __cold; + + /** \brief is an error in the list? + * + * \return \b true if an error is included in the list, \b false otherwise + */ + inline bool PendingError() const {return PendingFlag;}; + + /** \brief is the list empty? + * + * The default checks if the list is empty or contains only notices, + * if you want to check if also no notices happend set the parameter + * flag to \b false. + * + * \param WithoutNotice does notices count, default is \b true, so no + * + * \return \b true if an the list is empty, \b false otherwise + */ + bool empty(MsgType const &trashhold = WARNING) const; + + /** \brief returns and removes the first (or last) message in the list + * + * \param[out] Text message of the first/last item + * + * \return \b true if the message was an error, \b false otherwise + */ + bool PopMessage(std::string &Text); + + /** \brief clears the list of messages */ + void Discard(); + + /** \brief outputs the list of messages to the given stream + * + * Note that all messages are discarded, also the notices + * displayed or not. + * + * \param[out] out output stream to write the messages in + * \param WithoutNotice output notices or not + */ + void DumpErrors(std::ostream &out, MsgType const &trashhold = WARNING); + + /** \brief dumps the list of messages to std::cerr + * + * Note that all messages are discarded, also the notices + * displayed or not. + * + * \param WithoutNotice print notices or not + */ + void inline DumpErrors(MsgType const &trashhold = WARNING) { + DumpErrors(std::cerr, trashhold); + } + + GlobalError(); + /*}}}*/ +private: /*{{{*/ + struct Item { + std::string Text; + MsgType Type; + + Item(char const *Text, MsgType const &Type) : + Text(Text), Type(Type) {}; + + friend std::ostream& operator<< (std::ostream &out, Item i) { + switch(i.Type) { + case FATAL: + case ERROR: out << "E"; break; + case WARNING: out << "W"; break; + case NOTICE: out << "N"; break; + case DEBUG: out << "D"; break; + } + return out << ": " << i.Text; + } + }; + + std::list Messages; + bool PendingFlag; + + bool InsertErrno(MsgType type, const char* Function, + const char* Description, va_list const &args); + bool Insert(MsgType type, const char* Description, + va_list const &args); + /*}}}*/ }; + /*}}}*/ // The 'extra-ansi' syntax is used to help with collisions. GlobalError *_GetErrorObj(); -- cgit v1.2.3 From c4ba7c44ff03d67ff982bbab26dc48d796041e02 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Fri, 25 Jun 2010 19:16:12 +0200 Subject: add a simple stack handling to be able to delay error handling --- apt-pkg/contrib/error.cc | 32 +++++++++++++++++++++++++++++++- apt-pkg/contrib/error.h | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/error.cc b/apt-pkg/contrib/error.cc index 837d9e615..8cee21c9c 100644 --- a/apt-pkg/contrib/error.cc +++ b/apt-pkg/contrib/error.cc @@ -181,7 +181,13 @@ bool GlobalError::PopMessage(std::string &Text) { } /*}}}*/ // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/ -void GlobalError::DumpErrors(std::ostream &out, MsgType const &trashhold) { +void GlobalError::DumpErrors(std::ostream &out, MsgType const &trashhold, + bool const &mergeStack) { + if (mergeStack == true) + for (std::list::const_reverse_iterator s = Stacks.rbegin(); + s != Stacks.rend(); ++s) + Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end()); + for (std::list::const_iterator m = Messages.begin(); m != Messages.end(); m++) if (m->Type >= trashhold) @@ -211,3 +217,27 @@ bool GlobalError::empty(MsgType const &trashhold) const { return true; } /*}}}*/ +// GlobalError::PushToStack /*{{{*/ +void GlobalError::PushToStack() { + MsgStack pack(Messages, PendingFlag); + Stacks.push_back(pack); + Discard(); +} + /*}}}*/ +// GlobalError::RevertToStack /*{{{*/ +void GlobalError::RevertToStack() { + Discard(); + MsgStack pack = Stacks.back(); + Messages = pack.Messages; + PendingFlag = pack.PendingFlag; + Stacks.pop_back(); +} + /*}}}*/ +// GlobalError::MergeWithStack /*{{{*/ +void GlobalError::MergeWithStack() { + MsgStack pack = Stacks.back(); + Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end()); + PendingFlag = PendingFlag || pack.PendingFlag; + Stacks.pop_back(); +} + /*}}}*/ diff --git a/apt-pkg/contrib/error.h b/apt-pkg/contrib/error.h index fc7b38f1b..73735162d 100644 --- a/apt-pkg/contrib/error.h +++ b/apt-pkg/contrib/error.h @@ -206,7 +206,8 @@ public: /*{{{*/ * \param[out] out output stream to write the messages in * \param WithoutNotice output notices or not */ - void DumpErrors(std::ostream &out, MsgType const &trashhold = WARNING); + void DumpErrors(std::ostream &out, MsgType const &trashhold = WARNING, + bool const &mergeStack = true); /** \brief dumps the list of messages to std::cerr * @@ -219,6 +220,28 @@ public: /*{{{*/ DumpErrors(std::cerr, trashhold); } + /** \brief put the current Messages into the stack + * + * All "old" messages will be pushed into a stack to + * them later back, but for now the Message query will be + * empty and performs as no messages were present before. + * + * The stack can be as deep as you want - all stack operations + * will only operate on the last element in the stack. + */ + void PushToStack(); + + /** \brief throw away all current messages */ + void RevertToStack(); + + /** \brief merge current and stack together */ + void MergeWithStack(); + + /** \brief return the deep of the stack */ + size_t StackCount() const { + return Stacks.size(); + } + GlobalError(); /*}}}*/ private: /*{{{*/ @@ -244,6 +267,16 @@ private: /*{{{*/ std::list Messages; bool PendingFlag; + struct MsgStack { + std::list const Messages; + bool const PendingFlag; + + MsgStack(std::list const &Messages, bool const &Pending) : + Messages(Messages), PendingFlag(Pending) {}; + }; + + std::list Stacks; + bool InsertErrno(MsgType type, const char* Function, const char* Description, va_list const &args); bool Insert(MsgType type, const char* Description, -- cgit v1.2.3 From 1f2933a8de0f3a26f51b4a0a2112cbdfd4461e9b Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 26 Jun 2010 13:29:24 +0200 Subject: - use the new MatchAgainstConfig for the DefaultRootSetFunc * apt-pkg/contrib/configuration.{cc,h}: - add a wrapper to match strings against configurable regex patterns --- apt-pkg/contrib/configuration.cc | 43 ++++++++++++++++++++++++++++++++++++++++ apt-pkg/contrib/configuration.h | 19 +++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc index 9129d92f0..81cc87d15 100644 --- a/apt-pkg/contrib/configuration.cc +++ b/apt-pkg/contrib/configuration.cc @@ -843,3 +843,46 @@ bool ReadConfigDir(Configuration &Conf,const string &Dir, return true; } /*}}}*/ +// MatchAgainstConfig Constructor /*{{{*/ +Configuration::MatchAgainstConfig::MatchAgainstConfig(char const * Config) +{ + std::vector const strings = _config->FindVector(Config); + for (std::vector::const_iterator s = strings.begin(); + s != strings.end(); ++s) + { + regex_t *p = new regex_t; + if (regcomp(p, s->c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB) == 0) + patterns.push_back(p); + else + { + regfree(p); + delete p; + _error->Warning("Regex compilation error for '%s' in configuration option '%s'", + s->c_str(), Config); + } + } + +} + /*}}}*/ +// MatchAgainstConfig Destructor /*{{{*/ +Configuration::MatchAgainstConfig::~MatchAgainstConfig() +{ + for(std::vector::const_iterator p = patterns.begin(); + p != patterns.end(); ++p) + { + regfree(*p); + delete *p; + } +} + /*}}}*/ +// MatchAgainstConfig::Match - returns true if a pattern matches /*{{{*/ +bool Configuration::MatchAgainstConfig::Match(char const * str) const +{ + for(std::vector::const_iterator p = patterns.begin(); + p != patterns.end(); ++p) + if (regexec(*p, str, 0, 0, 0) == 0) + return true; + + return false; +} + /*}}}*/ diff --git a/apt-pkg/contrib/configuration.h b/apt-pkg/contrib/configuration.h index 2494c1d7c..cbe18e4e5 100644 --- a/apt-pkg/contrib/configuration.h +++ b/apt-pkg/contrib/configuration.h @@ -28,7 +28,7 @@ #ifndef PKGLIB_CONFIGURATION_H #define PKGLIB_CONFIGURATION_H - +#include #include #include @@ -104,6 +104,23 @@ class Configuration Configuration(const Item *Root); Configuration(); ~Configuration(); + + /** \brief match a string against a configurable list of patterns */ + class MatchAgainstConfig + { + std::vector patterns; + + public: + MatchAgainstConfig(char const * Config); + virtual ~MatchAgainstConfig(); + + /** \brief Returns \b true for a string matching one of the patterns */ + bool Match(char const * str) const; + bool Match(std::string const &str) const { return Match(str.c_str()); }; + + /** \brief returns if the matcher setup was successful */ + bool wasConstructedSuccessfully() const { return patterns.empty() == false; } + }; }; extern Configuration *_config; -- cgit v1.2.3 From 1408e219e76e577fb84ef1c48e58337b6569feec Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 26 Jun 2010 18:40:15 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - show notice about ignored file instead of being always silent - add a Dir::Ignore-Files-Silently list option to control the notice * --- apt-pkg/contrib/fileutl.cc | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 16f7ce929..e95ddf562 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -281,6 +281,7 @@ std::vector GetListOfFilesInDir(string const &Dir, std::vector c } std::vector List; + Configuration::MatchAgainstConfig SilentIgnore("Dir::Ignore-Files-Silently"); DIR *D = opendir(Dir.c_str()); if (D == 0) { @@ -306,6 +307,7 @@ std::vector GetListOfFilesInDir(string const &Dir, std::vector c { if (Debug == true) std::clog << "Bad file: " << Ent->d_name << " → no extension" << std::endl; + _error->Notice("Ignoring file '%s' in directory '%s' as it has no filename extension", Ent->d_name, Dir.c_str()); continue; } } @@ -313,6 +315,8 @@ std::vector GetListOfFilesInDir(string const &Dir, std::vector c { if (Debug == true) std::clog << "Bad file: " << Ent->d_name << " → bad extension »" << flExtension(Ent->d_name) << "«" << std::endl; + if (SilentIgnore.Match(Ent->d_name) == false) + _error->Notice("Ignoring file '%s' in directory '%s' as it has an invalid filename extension", Ent->d_name, Dir.c_str()); continue; } } -- cgit v1.2.3 From 5afa55c6703a71a198aac950db30ffc6ca49f269 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 26 Jun 2010 18:42:42 +0200 Subject: make the MMap Grow Error a fatal one as while in theory the code should never segfault it still tend to do it so better show it directly --- apt-pkg/contrib/mmap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index d233e51bc..d71066243 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -297,7 +297,7 @@ unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln) { if(!Grow()) { - _error->Error(_("Dynamic MMap ran out of room. Please increase the size " + _error->Fatal(_("Dynamic MMap ran out of room. Please increase the size " "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); return 0; } -- cgit v1.2.3 From a9fe592842bfa17d91f4904d7fb0e3af3adebb17 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 8 Jul 2010 15:28:53 +0200 Subject: * apt-pkg/pkgcachegen.{cc,h}: - make the used MMap moveable (and therefore dynamic resizeable) by applying (some) mad pointer magic (Closes: #195018) --- apt-pkg/contrib/mmap.cc | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index d71066243..aa184b130 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -225,22 +225,22 @@ DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace, // disable Moveable if we don't grow if (Grow == 0) - Flags &= ~Moveable; + this->Flags &= ~Moveable; #ifndef __linux__ // kfreebsd doesn't have mremap, so we use the fallback - if ((Flags & Moveable) == Moveable) - Flags |= Fallback; + if ((this->Flags & Moveable) == Moveable) + this->Flags |= Fallback; #endif #ifdef _POSIX_MAPPED_FILES - if ((Flags & Fallback) != Fallback) { + if ((this->Flags & Fallback) != Fallback) { // Set the permissions. int Prot = PROT_READ; int Map = MAP_PRIVATE | MAP_ANONYMOUS; - if ((Flags & ReadOnly) != ReadOnly) + if ((this->Flags & ReadOnly) != ReadOnly) Prot |= PROT_WRITE; - if ((Flags & Public) == Public) + if ((this->Flags & Public) == Public) Map = MAP_SHARED | MAP_ANONYMOUS; // use anonymous mmap() to get the memory @@ -314,7 +314,7 @@ unsigned long DynamicMMap::Allocate(unsigned long ItemSize) // Look for a matching pool entry Pool *I; Pool *Empty = 0; - for (I = Pools; I != Pools + PoolCount; I++) + for (I = Pools; I != Pools + PoolCount; ++I) { if (I->ItemSize == 0) Empty = I; @@ -342,7 +342,11 @@ unsigned long DynamicMMap::Allocate(unsigned long ItemSize) { const unsigned long size = 20*1024; I->Count = size/ItemSize; + Pool* oldPools = Pools; Result = RawAllocate(size,ItemSize); + if (Pools != oldPools) + I += Pools - oldPools; + // Does the allocation failed ? if (Result == 0 && _error->PendingError()) return 0; @@ -365,7 +369,7 @@ unsigned long DynamicMMap::WriteString(const char *String, if (Len == (unsigned long)-1) Len = strlen(String); - unsigned long Result = RawAllocate(Len+1,0); + unsigned long const Result = RawAllocate(Len+1,0); if (Result == 0 && _error->PendingError()) return 0; @@ -395,16 +399,20 @@ bool DynamicMMap::Grow() { return _error->Error(_("Unable to increase the size of the MMap as the " "limit of %lu bytes is already reached."), Limit); - unsigned long const newSize = WorkSpace + 1024*1024; + unsigned long const newSize = WorkSpace + GrowFactor; if(Fd != 0) { Fd->Seek(newSize - 1); char C = 0; Fd->Write(&C,sizeof(C)); } + + unsigned long const poolOffset = Pools - ((Pool*) Base); + if ((Flags & Fallback) != Fallback) { #if defined(_POSIX_MAPPED_FILES) && defined(__linux__) #ifdef MREMAP_MAYMOVE + if ((Flags & Moveable) == Moveable) Base = mremap(Base, WorkSpace, newSize, MREMAP_MAYMOVE); else @@ -425,6 +433,7 @@ bool DynamicMMap::Grow() { return false; } + Pools =(Pool*) Base + poolOffset; WorkSpace = newSize; return true; } -- cgit v1.2.3