diff options
author | joerg <joerg@pkgsrc.org> | 2007-08-15 02:08:40 +0000 |
---|---|---|
committer | joerg <joerg@pkgsrc.org> | 2007-08-15 02:08:40 +0000 |
commit | 14b81a308968c2dad98d0b3837bc7e6da467a15a (patch) | |
tree | ef7119a3234d243b7b2acce54cee8e0f2b51ef64 | |
parent | 6b930e476d38f70d09002de3c41f3a4f714dc406 (diff) | |
download | pkgsrc-14b81a308968c2dad98d0b3837bc7e6da467a15a.tar.gz |
Start abstracting pkgdb access by introducing pkgdb_pkg_file.
The function returns the path name of the given file for the package.
The memory needs to be freed by the caller.
-rw-r--r-- | pkgtools/pkg_install/files/admin/main.c | 30 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/info/show.c | 17 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/lib/automatic.c | 24 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/lib/lib.h | 4 | ||||
-rw-r--r-- | pkgtools/pkg_install/files/lib/pkgdb.c | 15 |
5 files changed, 46 insertions, 44 deletions
diff --git a/pkgtools/pkg_install/files/admin/main.c b/pkgtools/pkg_install/files/admin/main.c index e744cf10c89..ffc5cb897ae 100644 --- a/pkgtools/pkg_install/files/admin/main.c +++ b/pkgtools/pkg_install/files/admin/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.30 2007/08/15 01:49:02 joerg Exp $ */ +/* $NetBSD: main.c,v 1.31 2007/08/15 02:08:40 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -8,7 +8,7 @@ #include <sys/cdefs.h> #endif #ifndef lint -__RCSID("$NetBSD: main.c,v 1.30 2007/08/15 01:49:02 joerg Exp $"); +__RCSID("$NetBSD: main.c,v 1.31 2007/08/15 02:08:40 joerg Exp $"); #endif /* @@ -229,7 +229,7 @@ add1pkg(const char *pkgdir) FILE *f; plist_t *p; package_t Plist; - char contents[MaxPathSize]; + char *contents; const char *PkgDBDir; char *PkgName, *dirp; char file[MaxPathSize]; @@ -240,14 +240,10 @@ add1pkg(const char *pkgdir) err(EXIT_FAILURE, "cannot open pkgdb"); PkgDBDir = _pkgdb_getPKGDB_DIR(); - (void) snprintf(contents, sizeof(contents), "%s/%s", PkgDBDir, pkgdir); - if (!(isdir(contents) || islinktodir(contents))) - errx(EXIT_FAILURE, "`%s' does not exist.", contents); - - (void) strlcat(contents, "/", sizeof(contents)); - (void) strlcat(contents, CONTENTS_FNAME, sizeof(contents)); + contents = pkgdb_pkg_file(pkgdir, CONTENTS_FNAME); if ((f = fopen(contents, "r")) == NULL) errx(EXIT_FAILURE, "%s: can't open `%s'", pkgdir, CONTENTS_FNAME); + free(contents); Plist.head = Plist.tail = NULL; read_plist(&Plist, f); @@ -456,9 +452,7 @@ remove_required_by(const char *pkgname, void *cookie) { char *path; - if (asprintf(&path, "%s/%s/%s", _pkgdb_getPKGDB_DIR(), pkgname, - REQUIRED_BY_FNAME) == -1) - errx(EXIT_FAILURE, "asprintf failed"); + path = pkgdb_pkg_file(pkgname, REQUIRED_BY_FNAME); if (unlink(path) == -1 && errno != ENOENT) err(EXIT_FAILURE, "Cannot remove %s", path); @@ -481,9 +475,7 @@ add_required_by(const char *pattern, const char *required_by) return; } - if (asprintf(&path, "%s/%s/%s", _pkgdb_getPKGDB_DIR(), best_installed, - REQUIRED_BY_FNAME) == -1) - errx(EXIT_FAILURE, "asprintf failed"); + path = pkgdb_pkg_file(best_installed, REQUIRED_BY_FNAME); free(best_installed); if ((fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0644)) == -1) @@ -506,9 +498,7 @@ add_depends_of(const char *pkgname, void *cookie) package_t plist; char *path; - if (asprintf(&path, "%s/%s/%s", _pkgdb_getPKGDB_DIR(), pkgname, - CONTENTS_FNAME) == -1) - errx(EXIT_FAILURE, "asprintf failed"); + path = pkgdb_pkg_file(pkgname, CONTENTS_FNAME); if ((fp = fopen(path, "r")) == NULL) errx(EXIT_FAILURE, "Cannot read %s of package %s", CONTENTS_FNAME, pkgname); @@ -848,9 +838,7 @@ set_installed_info_var(const char *name, void *cookie) char *filename; int retval; - if (asprintf(&filename, "%s/%s/%s", _pkgdb_getPKGDB_DIR(), name, - INSTALLED_INFO_FNAME) == -1) - errx(EXIT_FAILURE, "asprintf failed"); + filename = pkgdb_pkg_file(name, INSTALLED_INFO_FNAME); retval = var_set(filename, arg->variable, arg->value); diff --git a/pkgtools/pkg_install/files/info/show.c b/pkgtools/pkg_install/files/info/show.c index 6e73fcc633d..a1cc73a2b83 100644 --- a/pkgtools/pkg_install/files/info/show.c +++ b/pkgtools/pkg_install/files/info/show.c @@ -1,4 +1,4 @@ -/* $NetBSD: show.c,v 1.16 2007/08/09 23:54:17 joerg Exp $ */ +/* $NetBSD: show.c,v 1.17 2007/08/15 02:08:40 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -11,7 +11,7 @@ #if 0 static const char *rcsid = "from FreeBSD Id: show.c,v 1.11 1997/10/08 07:47:38 charnier Exp"; #else -__RCSID("$NetBSD: show.c,v 1.16 2007/08/09 23:54:17 joerg Exp $"); +__RCSID("$NetBSD: show.c,v 1.17 2007/08/15 02:08:40 joerg Exp $"); #endif #endif @@ -139,16 +139,15 @@ show_file(const char *pkg, const char *title, const char *fname, Boolean separat void show_var(const char *pkg, const char *fname, const char *variable) { - char *value; - char filename[BUFSIZ]; + char *filename, *value; - (void)snprintf(filename, sizeof(filename), "%s/%s/%s", - _pkgdb_getPKGDB_DIR(), pkg, fname); + filename = pkgdb_pkg_file(pkg, fname); - if ((value=var_get(fname, variable)) != NULL) { - (void) printf("%s\n", value); - free(value); + if ((value = var_get(filename, variable)) != NULL) { + (void) printf("%s\n", value); + free(value); } + free(filename); } void diff --git a/pkgtools/pkg_install/files/lib/automatic.c b/pkgtools/pkg_install/files/lib/automatic.c index c0ec8a21a2a..865f9ecdd43 100644 --- a/pkgtools/pkg_install/files/lib/automatic.c +++ b/pkgtools/pkg_install/files/lib/automatic.c @@ -1,4 +1,4 @@ -/* $NetBSD: automatic.c,v 1.3 2007/08/09 23:18:31 joerg Exp $ */ +/* $NetBSD: automatic.c,v 1.4 2007/08/15 02:08:40 joerg Exp $ */ /*- * Copyright (c) 2005 The NetBSD Foundation, Inc. @@ -40,7 +40,7 @@ #include <sys/cdefs.h> #endif #ifndef lint -__RCSID("$NetBSD: automatic.c,v 1.3 2007/08/09 23:18:31 joerg Exp $"); +__RCSID("$NetBSD: automatic.c,v 1.4 2007/08/15 02:08:40 joerg Exp $"); #endif #if HAVE_ASSERT_H @@ -69,14 +69,12 @@ __RCSID("$NetBSD: automatic.c,v 1.3 2007/08/09 23:18:31 joerg Exp $"); Boolean is_automatic_installed(const char *pkg) { - char filename[BUFSIZ]; - char *value; + char *filename, *value; Boolean ret; assert(pkg[0] != '/'); - (void)snprintf(filename, sizeof(filename), "%s/%s/%s", - _pkgdb_getPKGDB_DIR(), pkg, INSTALLED_INFO_FNAME); + filename = pkgdb_pkg_file(pkg, INSTALLED_INFO_FNAME); value = var_get(filename, AUTOMATIC_VARNAME); @@ -86,6 +84,7 @@ is_automatic_installed(const char *pkg) ret = FALSE; free(value); + free(filename); return ret; } @@ -93,13 +92,16 @@ is_automatic_installed(const char *pkg) int mark_as_automatic_installed(const char *pkg, int value) { - char filename[BUFSIZ]; + char *filename; + int retval; assert(pkg[0] != '/'); - (void)snprintf(filename, sizeof(filename), "%s/%s/%s", - _pkgdb_getPKGDB_DIR(), pkg, INSTALLED_INFO_FNAME); + filename = pkgdb_pkg_file(pkg, INSTALLED_INFO_FNAME); - return var_set(filename, AUTOMATIC_VARNAME, - value ? "yes" : NULL); + retval = var_set(filename, AUTOMATIC_VARNAME, value ? "yes" : NULL); + + free(filename); + + return retval; } diff --git a/pkgtools/pkg_install/files/lib/lib.h b/pkgtools/pkg_install/files/lib/lib.h index 8082253d3c6..c2f5c32319c 100644 --- a/pkgtools/pkg_install/files/lib/lib.h +++ b/pkgtools/pkg_install/files/lib/lib.h @@ -1,4 +1,4 @@ -/* $NetBSD: lib.h,v 1.35 2007/08/15 01:49:02 joerg Exp $ */ +/* $NetBSD: lib.h,v 1.36 2007/08/15 02:08:40 joerg Exp $ */ /* from FreeBSD Id: lib.h,v 1.25 1997/10/08 07:48:03 charnier Exp */ @@ -403,6 +403,8 @@ char *_pkgdb_getPKGDB_FILE(char *, unsigned); const char *_pkgdb_getPKGDB_DIR(void); void _pkgdb_setPKGDB_DIR(const char *); +char *pkgdb_pkg_file(const char *, const char *); + /* List of packages functions */ lpkg_t *alloc_lpkg(const char *); lpkg_t *find_on_queue(lpkg_head_t *, const char *); diff --git a/pkgtools/pkg_install/files/lib/pkgdb.c b/pkgtools/pkg_install/files/lib/pkgdb.c index 87bca3ee1d7..1b44e78a639 100644 --- a/pkgtools/pkg_install/files/lib/pkgdb.c +++ b/pkgtools/pkg_install/files/lib/pkgdb.c @@ -1,4 +1,4 @@ -/* $NetBSD: pkgdb.c,v 1.27 2007/08/15 01:49:02 joerg Exp $ */ +/* $NetBSD: pkgdb.c,v 1.28 2007/08/15 02:08:41 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -8,7 +8,7 @@ #include <sys/cdefs.h> #endif #ifndef lint -__RCSID("$NetBSD: pkgdb.c,v 1.27 2007/08/15 01:49:02 joerg Exp $"); +__RCSID("$NetBSD: pkgdb.c,v 1.28 2007/08/15 02:08:41 joerg Exp $"); #endif /* @@ -338,3 +338,14 @@ _pkgdb_setPKGDB_DIR(const char *dir) (void) snprintf(pkgdb_cache, sizeof(pkgdb_cache), "%s", dir); pkgdb_dir = pkgdb_cache; } + +char * +pkgdb_pkg_file(const char *pkg, const char *file) +{ + char *buf; + + if (asprintf(&buf, "%s/%s/%s", _pkgdb_getPKGDB_DIR(), pkg, file) == -1) + err(EXIT_FAILURE, "asprintf failed"); + + return buf; +} |