From eddc34703223bc35ee40ea29c6d7461732740b26 Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Sun, 20 Jan 2008 10:36:25 +0100 Subject: [PATCH] Refactor, move filtering functions to their own file. * Move the filtering functions from lib/myopt.[ch] to src/filters.[ch]. * Add filters.[ch] to src/Makefile.am * Add #include "filters.h" to main.c and archives.c so they still get at the filterlist definition. --- lib/myopt.c | 88 ---------------------------------- lib/myopt.h | 8 ---- src/Makefile.am | 4 +- src/archives.c | 1 + src/filters.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ src/filters.h | 30 ++++++++++++ src/main.c | 1 + 7 files changed, 157 insertions(+), 97 deletions(-) create mode 100644 src/filters.c create mode 100644 src/filters.h diff --git a/lib/myopt.c b/lib/myopt.c index 42586d8d..30e4fa32 100644 --- a/lib/myopt.c +++ b/lib/myopt.c @@ -169,91 +169,3 @@ void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos) { } } } - -struct filterlist *filters = NULL; - -void loadfilter(char *fn) { - FILE *file; - char linebuf[1024]; - struct filterlist *filtertail; - - file = fopen(fn, "r"); - if (!file) { - warningf(_("failed to open filter file `%.255s' for reading"), fn); - return; - } - - while (fgets(linebuf, sizeof(linebuf), file)) { - struct filterlist *filter; - - filter = m_malloc(sizeof(struct filterlist)); - if (!filter) { - ohshite(_("Error allocating memory for filter entry")); - } - memset(filter, 0, sizeof(struct filterlist)); - - if (linebuf[0] == '#' || linebuf[0] == '\n') { - continue; - } - - if (linebuf[strlen(linebuf) - 1] == '\n') - linebuf[strlen(linebuf) - 1] = '\0'; - - if (linebuf[0] == '+') { - filter->positive = 1; - } else if (linebuf[0] == '-') { - filter->positive = 0; - } else { - warningf(_("Invalid filter line: `%.255s'"), linebuf); - free(filter); - continue; - } - - filter->filterstring = strdup(&linebuf[1]); - if (!filter->filterstring) { - ohshite(_("Error allocating memory for filter entry")); - } - - if (! filters) { - filters = filter; - filtertail = filter; - } else { - filtertail->next = filter; - filtertail = filtertail->next; - } - } - - if (ferror(file)) - ohshite(_("read error in configuration file `%.255s'"), fn); - - if (fclose(file)) - ohshite(_("error closing configuration file `%.255s'"), fn); -} - -void loadfilters(void) { - struct dirent *dent; - char *dirname = CONFIGDIR "/filters.d"; - DIR *dir = opendir(dirname); - if (!dir) { - if (errno == ENOENT) - return NULL; - else - ohshite(_("Error opening filters.d")); - } - - while ((dent = readdir(dir)) != NULL) { - struct stat statbuf; - char *file = m_malloc(strlen(dirname) + 1 + strlen(dent->d_name) + 1); - if (!file) - ohshite(_("Error allocating memory for file")); - sprintf(file, "%s/%s", dirname, dent->d_name); - if (stat(file, &statbuf) != 0) { - ohshite(_("Error stating file")); - } - if (S_ISREG(statbuf.st_mode)) { - loadfilter(file); - } - free(file); - } - closedir(dir); -} diff --git a/lib/myopt.h b/lib/myopt.h index 290ad602..c96541fa 100644 --- a/lib/myopt.h +++ b/lib/myopt.h @@ -39,13 +39,5 @@ struct cmdinfo { void myfileopt(const char* fn, const struct cmdinfo* cmdinfos); void myopt(const char *const **argvp, const struct cmdinfo *cmdinfos); void loadcfgfile(const char *prog, const struct cmdinfo *cmdinfos); -void loadfilter(char *fn); -void loadfilters(void); - -struct filterlist { - int positive; - char *filterstring; - struct filterlist *next; -}; #endif /* MYOPT_H */ diff --git a/src/Makefile.am b/src/Makefile.am index cbc06282..2092e961 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,6 +4,7 @@ localedir = $(datadir)/locale INCLUDES = \ -DLOCALEDIR=\"$(localedir)\" -I$(top_srcdir)/intl \ -DADMINDIR=\"$(admindir)\" \ + -DCONFIGDIR=\"$(pkgconfdir)\" \ -I$(top_srcdir)/lib @@ -23,7 +24,8 @@ dpkg_SOURCES = \ processarc.c \ remove.c \ select.c \ - update.c + update.c \ + filters.c filters.h dpkg_LDADD = \ $(LIBINTL) ../lib/libdpkg.a $(ZLIB_LIBS) $(BZ2_LIBS) $(SELINUX_LIBS) diff --git a/src/archives.c b/src/archives.c index a1239402..90a74e4a 100644 --- a/src/archives.c +++ b/src/archives.c @@ -53,6 +53,7 @@ static security_context_t scontext = NULL; #include "filesdb.h" #include "main.h" #include "archives.h" +#include "filters.h" #define MAXCONFLICTORS 20 diff --git a/src/filters.c b/src/filters.c new file mode 100644 index 00000000..702a70e9 --- /dev/null +++ b/src/filters.c @@ -0,0 +1,122 @@ +/* + * dpkg - main program for package management + * filters.c - filtering routines for excluding bits of packages + * + * Copyright (C) 2007,2008 Tollef Fog Heen + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2, + * or (at your option) any later version. + * + * This is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "filters.h" + +struct filterlist *filters = NULL; + +void loadfilter(char *fn) { + FILE *file; + char linebuf[1024]; + struct filterlist *filtertail; + + file = fopen(fn, "r"); + if (!file) { + warningf(_("failed to open filter file `%.255s' for reading"), fn); + return; + } + + while (fgets(linebuf, sizeof(linebuf), file)) { + struct filterlist *filter; + + filter = m_malloc(sizeof(struct filterlist)); + if (!filter) { + ohshite(_("Error allocating memory for filter entry")); + } + memset(filter, 0, sizeof(struct filterlist)); + + if (linebuf[0] == '#' || linebuf[0] == '\n') { + continue; + } + + if (linebuf[strlen(linebuf) - 1] == '\n') + linebuf[strlen(linebuf) - 1] = '\0'; + + if (linebuf[0] == '+') { + filter->positive = 1; + } else if (linebuf[0] == '-') { + filter->positive = 0; + } else { + warningf(_("Invalid filter line: `%.255s'"), linebuf); + free(filter); + continue; + } + + filter->filterstring = strdup(&linebuf[1]); + if (!filter->filterstring) { + ohshite(_("Error allocating memory for filter entry")); + } + + if (! filters) { + filters = filter; + filtertail = filter; + } else { + filtertail->next = filter; + filtertail = filtertail->next; + } + } + + if (ferror(file)) + ohshite(_("read error in configuration file `%.255s'"), fn); + + if (fclose(file)) + ohshite(_("error closing configuration file `%.255s'"), fn); +} + +void loadfilters(void) { + struct dirent *dent; + char *dirname = CONFIGDIR "/filters.d"; + DIR *dir = opendir(dirname); + if (!dir) { + if (errno == ENOENT) + return NULL; + else + ohshite(_("Error opening filters.d")); + } + + while ((dent = readdir(dir)) != NULL) { + struct stat statbuf; + char *file = m_malloc(strlen(dirname) + 1 + strlen(dent->d_name) + 1); + if (!file) + ohshite(_("Error allocating memory for file")); + sprintf(file, "%s/%s", dirname, dent->d_name); + if (stat(file, &statbuf) != 0) { + ohshite(_("Error stating file")); + } + if (S_ISREG(statbuf.st_mode)) { + loadfilter(file); + } + free(file); + } + closedir(dir); +} diff --git a/src/filters.h b/src/filters.h new file mode 100644 index 00000000..d4dba5c2 --- /dev/null +++ b/src/filters.h @@ -0,0 +1,30 @@ +/* + * dpkg - main program for package management + * filters.h - external definitions for filter handling + * + * Copyright (C) 2007,2008 Tollef Fog Heen + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2, + * or (at your option) any later version. + * + * This is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +void loadfilter(char *fn); +void loadfilters(void); + +struct filterlist { + int positive; + char *filterstring; + struct filterlist *next; +}; + diff --git a/src/main.c b/src/main.c index b2fbc4de..cd7dd606 100644 --- a/src/main.c +++ b/src/main.c @@ -39,6 +39,7 @@ #include #include "main.h" +#include "filters.h" static void printversion(void) { if (printf(_("Debian `%s' package management program version %s.\n"), -- 2.39.5