commit ecebb8635637a9fb9cb7d8e8533312e42b821049
parent 83de33262adf03d1c5057bd84f8d9d7b9fc47b09
Author: sin <sin@2f30.org>
Date: Tue, 24 Jun 2014 15:07:43 +0100
Re-organize code
Diffstat:
12 files changed, 115 insertions(+), 230 deletions(-)
diff --git a/Makefile b/Makefile
@@ -7,6 +7,7 @@ LIB = \
db.o \
ealloc.o \
eprintf.o \
+ reject.o \
strlcat.o \
strlcpy.o
@@ -29,7 +30,7 @@ binlib: util.a
bin: $(BIN)
-$(OBJ): util.h config.mk
+$(OBJ): pkg.h config.mk
.o:
@echo LD $@
diff --git a/db.c b/db.c
@@ -1,32 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#define _XOPEN_SOURCE 500
-#include <archive.h>
-#include <archive_entry.h>
-#include <dirent.h>
-#include <ftw.h>
-#include <limits.h>
-#include <regex.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/file.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include "db.h"
-#include "util.h"
-
-#define DBPATH "/var/pkg"
-#define DBPATHREJECT "/etc/pkgtools/reject.conf"
-#define ARCHIVEBUFSIZ BUFSIZ
-
-struct db {
- DIR *pkgdir;
- char prefix[PATH_MAX];
- char path[PATH_MAX];
- TAILQ_HEAD(rejrule_head, rejrule) rejrule_head;
- TAILQ_HEAD(pkg_head, pkg) pkg_head;
-};
+#include "pkg.h"
int fflag = 0;
int vflag = 0;
@@ -584,85 +557,3 @@ err:
eprintf("%s: invalid package filename\n",
path);
}
-
-/* Release the pre-loaded regexes */
-void
-rej_free(struct db *db)
-{
- struct rejrule *rule, *tmp;
-
- for (rule = TAILQ_FIRST(&db->rejrule_head); rule; rule = tmp) {
- tmp = TAILQ_NEXT(rule, entry);
- regfree(&rule->preg);
- free(rule);
- }
-}
-
-/* Parse reject.conf and pre-compute regexes */
-int
-rej_load(struct db *db)
-{
- struct rejrule *rule;
- char rejpath[PATH_MAX];
- FILE *fp;
- char *buf = NULL;
- size_t sz = 0;
- ssize_t len;
- int r;
-
- estrlcpy(rejpath, db->prefix, sizeof(rejpath));
- estrlcat(rejpath, DBPATHREJECT, sizeof(rejpath));
-
- if (!(fp = fopen(rejpath, "r")))
- return -1;
-
- while ((len = getline(&buf, &sz, fp)) != -1) {
- if (!len || buf[0] == '#' || buf[0] == '\n')
- continue; /* skip empty lines and comments. */
- if (len > 0 && buf[len - 1] == '\n')
- buf[len - 1] = '\0';
-
- /* copy and add regex */
- rule = emalloc(sizeof(*rule));
-
- r = regcomp(&(rule->preg), buf, REG_NOSUB | REG_EXTENDED);
- if (r != 0) {
- regerror(r, &(rule->preg), buf, len);
- weprintf("invalid pattern: %s\n", buf);
- free(buf);
- fclose(fp);
- rej_free(db);
- return -1;
- }
-
- TAILQ_INSERT_TAIL(&db->rejrule_head, rule, entry);
- }
- free(buf);
- if (ferror(fp)) {
- weprintf("%s: read error:", rejpath);
- fclose(fp);
- rej_free(db);
- return -1;
- }
- fclose(fp);
-
- return 0;
-}
-
-/* Match pre-computed regexes against the given filename */
-int
-rej_match(struct db *db, const char *file)
-{
- struct rejrule *rule;
-
- /* Skip initial '.' of "./" */
- if (strncmp(file, "./", 2) == 0)
- file++;
-
- TAILQ_FOREACH(rule, &db->rejrule_head, entry) {
- if (regexec(&rule->preg, file, 0, NULL, 0) != REG_NOMATCH)
- return 1;
- }
-
- return 0;
-}
diff --git a/db.h b/db.h
@@ -1,52 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-
-#include <regex.h>
-#include <sys/types.h>
-#include "queue.h"
-
-struct pkgentry {
- /* full path */
- char path[PATH_MAX];
- /* relative path */
- char rpath[PATH_MAX];
- TAILQ_ENTRY(pkgentry) entry;
-};
-
-struct rejrule {
- regex_t preg;
- TAILQ_ENTRY(rejrule) entry;
-};
-
-struct pkg {
- char *name;
- char *version;
- char path[PATH_MAX];
- TAILQ_HEAD(pe_head, pkgentry) pe_head;
- TAILQ_ENTRY(pkg) entry;
-};
-
-extern int fflag;
-extern int vflag;
-
-struct db *db_new(const char *);
-int db_free(struct db *);
-int db_add(struct db *, struct pkg *);
-int db_rm(struct pkg *);
-int db_load(struct db *);
-struct pkg *pkg_load_file(struct db *, const char *);
-int db_walk(struct db *, int (*)(struct db *, struct pkg *, void *), void *);
-int db_links(struct db *, const char *);
-
-struct pkg *pkg_load(struct db *, const char *);
-int pkg_install(struct db *, struct pkg *);
-int pkg_remove(struct db *, struct pkg *);
-int pkg_collisions(struct pkg *);
-struct pkg *pkg_new(const char *, const char *, const char *);
-void pkg_free(struct pkg *);
-
-void parse_version(const char *, char **);
-void parse_name(const char *, char **);
-
-void rej_free(struct db *);
-int rej_load(struct db *);
-int rej_match(struct db *, const char *);
diff --git a/ealloc.c b/ealloc.c
@@ -1,7 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include <stdlib.h>
-#include <string.h>
-#include "util.h"
+#include "pkg.h"
void *
ecalloc(size_t nmemb, size_t size)
diff --git a/eprintf.c b/eprintf.c
@@ -1,9 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include "util.h"
+#include "pkg.h"
char *argv0;
diff --git a/infopkg.c b/infopkg.c
@@ -1,14 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include "db.h"
-#include "util.h"
+#include "pkg.h"
static int own_pkg_cb(struct db *, struct pkg *, void *);
diff --git a/installpkg.c b/installpkg.c
@@ -1,9 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "db.h"
-#include "util.h"
+#include "pkg.h"
static void
usage(void)
diff --git a/pkg.h b/pkg.h
@@ -0,0 +1,105 @@
+/* See LICENSE file for copyright and license details. */
+#include <archive.h>
+#include <archive_entry.h>
+#include <dirent.h>
+#include <ftw.h>
+#include <limits.h>
+#include <regex.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/file.h>
+#include <unistd.h>
+#include "queue.h"
+#include "arg.h"
+
+#define LEN(x) (sizeof (x) / sizeof *(x))
+
+#define DBPATH "/var/pkg"
+#define DBPATHREJECT "/etc/pkgtools/reject.conf"
+#define ARCHIVEBUFSIZ BUFSIZ
+
+struct pkgentry {
+ /* full path */
+ char path[PATH_MAX];
+ /* relative path */
+ char rpath[PATH_MAX];
+ TAILQ_ENTRY(pkgentry) entry;
+};
+
+struct pkg {
+ char *name;
+ char *version;
+ char path[PATH_MAX];
+ TAILQ_HEAD(pe_head, pkgentry) pe_head;
+ TAILQ_ENTRY(pkg) entry;
+};
+
+struct rejrule {
+ regex_t preg;
+ TAILQ_ENTRY(rejrule) entry;
+};
+
+struct db {
+ DIR *pkgdir;
+ char prefix[PATH_MAX];
+ char path[PATH_MAX];
+ TAILQ_HEAD(rejrule_head, rejrule) rejrule_head;
+ TAILQ_HEAD(pkg_head, pkg) pkg_head;
+};
+
+/* db.c */
+extern int fflag;
+extern int vflag;
+
+/* eprintf.c */
+extern char *argv0;
+
+/* db.c */
+struct db *db_new(const char *);
+int db_free(struct db *);
+int db_add(struct db *, struct pkg *);
+int db_rm(struct pkg *);
+int db_load(struct db *);
+struct pkg *pkg_load_file(struct db *, const char *);
+int db_walk(struct db *, int (*)(struct db *, struct pkg *, void *), void *);
+int db_links(struct db *, const char *);
+
+struct pkg *pkg_load(struct db *, const char *);
+int pkg_install(struct db *, struct pkg *);
+int pkg_remove(struct db *, struct pkg *);
+int pkg_collisions(struct pkg *);
+struct pkg *pkg_new(const char *, const char *, const char *);
+void pkg_free(struct pkg *);
+
+void parse_version(const char *, char **);
+void parse_name(const char *, char **);
+
+/* reject.c */
+void rej_free(struct db *);
+int rej_load(struct db *);
+int rej_match(struct db *, const char *);
+
+/* ealloc.c */
+void *ecalloc(size_t, size_t);
+void *emalloc(size_t size);
+void *erealloc(void *, size_t);
+char *estrdup(const char *);
+
+/* eprintf.c */
+void enprintf(int, const char *, ...);
+void eprintf(const char *, ...);
+void weprintf(const char *, ...);
+
+/* strlcat.c */
+#undef strlcat
+size_t strlcat(char *, const char *, size_t);
+size_t estrlcat(char *, const char *, size_t);
+
+/* strlcpy.c */
+#undef strlcpy
+size_t strlcpy(char *, const char *, size_t);
+size_t estrlcpy(char *, const char *, size_t);
diff --git a/removepkg.c b/removepkg.c
@@ -1,10 +1,5 @@
/* See LICENSE file for copyright and license details. */
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include "db.h"
-#include "util.h"
+#include "pkg.h"
static int pkg_remove_cb(struct db *, struct pkg *, void *);
diff --git a/strlcat.c b/strlcat.c
@@ -14,11 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include "util.h"
+#include "pkg.h"
/*
* Appends src to string dst of size siz (unlike strncat, siz is the
diff --git a/strlcpy.c b/strlcpy.c
@@ -14,11 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include "util.h"
+#include "pkg.h"
/*
* Copy src to string dst of size siz. At most siz-1 characters
diff --git a/util.h b/util.h
@@ -1,28 +0,0 @@
-/* See LICENSE file for copyright and license details. */
-#include <stddef.h>
-#include "arg.h"
-
-#define LEN(x) (sizeof (x) / sizeof *(x))
-
-extern char *argv0;
-
-/* ealloc.c */
-void *ecalloc(size_t, size_t);
-void *emalloc(size_t size);
-void *erealloc(void *, size_t);
-char *estrdup(const char *);
-
-/* eprintf.c */
-void enprintf(int, const char *, ...);
-void eprintf(const char *, ...);
-void weprintf(const char *, ...);
-
-/* strlcat.c */
-#undef strlcat
-size_t strlcat(char *, const char *, size_t);
-size_t estrlcat(char *, const char *, size_t);
-
-/* strlcpy.c */
-#undef strlcpy
-size_t strlcpy(char *, const char *, size_t);
-size_t estrlcpy(char *, const char *, size_t);