PERFORCE change 143136 for review

Anders Nore andenore at FreeBSD.org
Sun Jun 8 22:12:24 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=143136

Change 143136 by andenore at andenore_laptop on 2008/06/08 22:11:43

	Alot of syntax changing, mostly spaces replaced by tabs. I have added a pkg_convert program that
	converts some of the data to a bdb database, and the information is used in the pkg_info program
	by the functions isinstalledpkg() and matchinstalled(). A set of database helper functions is added
	to the library as well.

Affected files ...

.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/add/Makefile#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/Makefile#1 add
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/converter.c#1 add
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/convert/pkg_convert.1#1 add
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/create/Makefile#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/delete/Makefile#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/info/Makefile#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/info/main.c#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/info/perform.c#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/info/show.c#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/lib/database.c#1 add
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/lib/lib.h#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/lib/match.c#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/lib/plist.c#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/lib/str.c#2 edit
.. //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/version/Makefile#2 edit

Differences ...

==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/add/Makefile#2 (text+ko) ====


==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/create/Makefile#2 (text+ko) ====


==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/delete/Makefile#2 (text+ko) ====


==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/info/Makefile#2 (text+ko) ====


==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/info/main.c#2 (text+ko) ====

@@ -51,193 +51,197 @@
     whead = malloc(sizeof(struct which_head));
     if (whead == NULL)
 	err(2, NULL);
-    TAILQ_INIT(whead);
+	TAILQ_INIT(whead);
 
-    pkgs = start = argv;
-    if (argc == 1) {
-	MatchType = MATCH_ALL;
-	Flags = SHOW_INDEX;
-    }
-    else while ((ch = getopt(argc, argv, Options)) != -1) {
-	switch(ch) {
-	case 'a':
-	    MatchType = MATCH_ALL;
-	    break;
+	if(cacheExists() == TRUE) {
+		openDatabase(PKG_DBCACHE_FILE);
+	}
 
-	case 'b':
-	    UseBlkSz = TRUE;
-	    break;
-
-	case 'v':
-	    Verbose++;
-	    /* Reasonable definition of 'everything' */
-	    Flags = SHOW_COMMENT | SHOW_DESC | SHOW_PLIST | SHOW_INSTALL |
-		SHOW_DEINSTALL | SHOW_REQUIRE | SHOW_DISPLAY | SHOW_MTREE;
-	    break;
-
-	case 'E':
-	    Flags |= SHOW_PKGNAME;
-	    break;
-
-	case 'I':
-	    Flags |= SHOW_INDEX;
-	    break;
-
-	case 'p':
-	    Flags |= SHOW_PREFIX;
-	    break;
-
-	case 'c':
-	    Flags |= SHOW_COMMENT;
-	    break;
-
-	case 'd':
-	    Flags |= SHOW_DESC;
-	    break;
-
-	case 'D':
-	    Flags |= SHOW_DISPLAY;
-	    break;
-
-	case 'f':
-	    Flags |= SHOW_PLIST;
-	    break;
-
-	case 'g':
-	    Flags |= SHOW_CKSUM;
-	    break;
-
-	case 'G':
-	    MatchType = MATCH_EXACT;
-	    break;
-
-	case 'i':
-	    Flags |= SHOW_INSTALL;
-	    break;
-
-	case 'j':
-	    Flags |= SHOW_REQUIRE;
-	    break;
-
-	case 'k':
-	    Flags |= SHOW_DEINSTALL;
-	    break;
-
-	case 'K':
-	    KeepPackage = TRUE;
-	    break;
-
-	case 'r':
-	    Flags |= SHOW_DEPEND;
-	    break;
-
-	case 'R':
-	    Flags |= SHOW_REQBY;
-	    break;
-
-	case 'L':
-	    Flags |= SHOW_FILES;
-	    break;
-
-	case 'm':
-	    Flags |= SHOW_MTREE;
-	    break;
-
-	case 's':
-	    Flags |= SHOW_SIZE;
-	    break;
-
-	case 'o':
-	    Flags |= SHOW_ORIGIN;
-	    break;
-
-	case 'O':
-	    LookUpOrigin = strdup(optarg);
-	    if (LookUpOrigin == NULL)
-		err(2, NULL);
-	    break;
-
-	case 'V':
-	    Flags |= SHOW_FMTREV;
-	    break;
-
-	case 'l':
-	    InfoPrefix = optarg;
-	    break;
-
-	case 'q':
-	    Quiet = TRUE;
-	    break;
-
-	case 'Q':
-	    Quiet = TRUE;
-	    QUIET = TRUE;
-	    break;
-
-	case 't':
-	    strlcpy(PlayPen, optarg, sizeof(PlayPen));
-	    break;
-
-	case 'x':
-	    MatchType = MATCH_REGEX;
-	    break;
-
-	case 'X':
-	    MatchType = MATCH_EREGEX;
-	    break;
-
-	case 'e':
-	    CheckPkg = optarg;
-	    break;
-
-	case 'W':
-	    {
-		struct which_entry *entp;
-
-		entp = calloc(1, sizeof(struct which_entry));
-		if (entp == NULL)
-		    err(2, NULL);
-		
-		strlcpy(entp->file, optarg, PATH_MAX);
-		entp->skip = FALSE;
-		TAILQ_INSERT_TAIL(whead, entp, next);
-		break;
-	    }
-
-	case 'P':
-	    Flags = SHOW_PTREV;
-	    break;
-
-	case 'h':
-	case '?':
-	default:
-	    usage();
-	    break;
-	}
+	pkgs = start = argv;
+	if (argc == 1) {
+		MatchType = MATCH_ALL;
+		Flags = SHOW_INDEX;
+	} else
+		while ((ch = getopt(argc, argv, Options)) != -1) {
+		switch(ch) {
+		case 'a':
+			MatchType = MATCH_ALL;
+			break;
+	
+		case 'b':
+			UseBlkSz = TRUE;
+			break;
+	
+		case 'v':
+			Verbose++;
+			/* Reasonable definition of 'everything' */
+			Flags = SHOW_COMMENT | SHOW_DESC | SHOW_PLIST | SHOW_INSTALL |
+			SHOW_DEINSTALL | SHOW_REQUIRE | SHOW_DISPLAY | SHOW_MTREE;
+			break;
+	
+		case 'E':
+			Flags |= SHOW_PKGNAME;
+			break;
+	
+		case 'I':
+			Flags |= SHOW_INDEX;
+			break;
+	
+		case 'p':
+			Flags |= SHOW_PREFIX;
+			break;
+	
+		case 'c':
+			Flags |= SHOW_COMMENT;
+			break;
+	
+		case 'd':
+			Flags |= SHOW_DESC;
+			break;
+	
+		case 'D':
+			Flags |= SHOW_DISPLAY;
+			break;
+	
+		case 'f':
+			Flags |= SHOW_PLIST;
+			break;
+	
+		case 'g':
+			Flags |= SHOW_CKSUM;
+			break;
+	
+		case 'G':
+			MatchType = MATCH_EXACT;
+			break;
+	
+		case 'i':
+			Flags |= SHOW_INSTALL;
+			break;
+	
+		case 'j':
+			Flags |= SHOW_REQUIRE;
+			break;
+	
+		case 'k':
+			Flags |= SHOW_DEINSTALL;
+			break;
+	
+		case 'K':
+			KeepPackage = TRUE;
+			break;
+	
+		case 'r':
+			Flags |= SHOW_DEPEND;
+			break;
+	
+		case 'R':
+			Flags |= SHOW_REQBY;
+			break;
+	
+		case 'L':
+			Flags |= SHOW_FILES;
+			break;
+	
+		case 'm':
+			Flags |= SHOW_MTREE;
+			break;
+	
+		case 's':
+			Flags |= SHOW_SIZE;
+			break;
+	
+		case 'o':
+			Flags |= SHOW_ORIGIN;
+			break;
+	
+		case 'O':
+			LookUpOrigin = strdup(optarg);
+			if (LookUpOrigin == NULL)
+			err(2, NULL);
+			break;
+	
+		case 'V':
+			Flags |= SHOW_FMTREV;
+			break;
+	
+		case 'l':
+			InfoPrefix = optarg;
+			break;
+	
+		case 'q':
+			Quiet = TRUE;
+			break;
+	
+		case 'Q':
+			Quiet = TRUE;
+			QUIET = TRUE;
+			break;
+	
+		case 't':
+			strlcpy(PlayPen, optarg, sizeof(PlayPen));
+			break;
+	
+		case 'x':
+			MatchType = MATCH_REGEX;
+			break;
+	
+		case 'X':
+			MatchType = MATCH_EREGEX;
+			break;
+	
+		case 'e':
+			CheckPkg = optarg;
+			break;
+	
+		case 'W':
+			{
+			struct which_entry *entp;
+	
+			entp = calloc(1, sizeof(struct which_entry));
+			if (entp == NULL)
+				err(2, NULL);
+			
+			strlcpy(entp->file, optarg, PATH_MAX);
+			entp->skip = FALSE;
+			TAILQ_INSERT_TAIL(whead, entp, next);
+			break;
+			}
+	
+		case 'P':
+			Flags = SHOW_PTREV;
+			break;
+	
+		case 'h':
+		case '?':
+		default:
+			usage();
+			break;
+		}
     }
 
     argc -= optind;
     argv += optind;
 
-    if (Flags & SHOW_PTREV) {
-	if (!Quiet)
-	    printf("Package tools revision: ");
-	printf("%d\n", PKG_INSTALL_VERSION);
-	exit(0);
-    }
+	if (Flags & SHOW_PTREV) {
+		if (!Quiet)
+			printf("Package tools revision: ");
+		printf("%d\n", PKG_INSTALL_VERSION);
+		exit(0);
+	}
 
     /* Set some reasonable defaults */
-    if (!Flags)
-	Flags = SHOW_COMMENT | SHOW_DESC | SHOW_REQBY;
+	if (!Flags)
+		Flags = SHOW_COMMENT | SHOW_DESC | SHOW_REQBY;
 
     /* Get all the remaining package names, if any */
-    while (*argv) {
+	while (*argv) {
 	/* 
 	 * Don't try to apply heuristics if arguments are regexs or if
 	 * the argument refers to an existing file.
 	 */
 	if (MatchType != MATCH_REGEX && MatchType != MATCH_EREGEX && !isfile(*argv) && !isURL(*argv))
-	    while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) {
+		while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) {
 		*pkgs_split++ = '\0';
 		/*
 		 * If character after the '/' is alphanumeric or shell
@@ -246,18 +250,19 @@
 		 * quest.
 		 */
 		if (isalnum(*pkgs_split) || ((MatchType == MATCH_GLOB) && \
-		    strpbrk(pkgs_split, "*?[]") != NULL)) {
-		    *argv = pkgs_split;
-		    break;
+			strpbrk(pkgs_split, "*?[]") != NULL)) {
+			*argv = pkgs_split;
+			break;
 		}
 	    }
 	*pkgs++ = *argv++;
-    }
+	}
 
     /* If no packages, yelp */
     if (pkgs == start && MatchType != MATCH_ALL && !CheckPkg && 
 	TAILQ_EMPTY(whead) && LookUpOrigin == NULL)
-	warnx("missing package name(s)"), usage();
+		warnx("missing package name(s)"), usage();
+
     *pkgs = NULL;
     return pkg_perform(start);
 }
@@ -265,7 +270,7 @@
 static void
 usage()
 {
-    fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
+	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
 	"usage: pkg_info [-bcdDEfgGiIjkKLmopPqQrRsvVxX] [-e package] [-l prefix]",
 	"                [-t template] -a | pkg-name ...",
 	"       pkg_info [-qQ] -W filename",

==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/info/perform.c#2 (text+ko) ====

@@ -43,44 +43,44 @@
     signal(SIGINT, cleanup);
 
     /* Overriding action? */
-    if (Flags & SHOW_PKGNAME) {
-	return matched_packages(pkgs);
-    } else if (CheckPkg) {
-	return isinstalledpkg(CheckPkg) > 0 ? 0 : 1;
+	if (Flags & SHOW_PKGNAME) {
+		return matched_packages(pkgs);
+	} else if (CheckPkg) {
+		return isinstalledpkg(CheckPkg) > 0 ? 0 : 1;
 	/* Not reached */
-    } else if (!TAILQ_EMPTY(whead)) {
-	return find_pkg(whead);
-    } else if (LookUpOrigin != NULL) {
-	return find_pkgs_by_origin(LookUpOrigin);
-    }
+	} else if (!TAILQ_EMPTY(whead)) {
+		return find_pkg(whead);
+	} else if (LookUpOrigin != NULL) {
+		return find_pkgs_by_origin(LookUpOrigin);
+	}
 
-    if (MatchType != MATCH_EXACT) {
-	matched = matchinstalled(MatchType, pkgs, &errcode);
-	if (errcode != 0)
-	    return 1;
-	    /* Not reached */
-
-	if (matched != NULL)
-	    pkgs = matched;
-	else switch (MatchType) {
-	    case MATCH_GLOB:
-		break;
-	    case MATCH_ALL:
-		warnx("no packages installed");
-		return 0;
-		/* Not reached */
-	    case MATCH_REGEX:
-	    case MATCH_EREGEX:
-		warnx("no packages match pattern(s)");
-		return 1;
-		/* Not reached */
-	    default:
-		break;
-	}
+	if (MatchType != MATCH_EXACT) {
+		matched = matchinstalled(MatchType, pkgs, &errcode);
+		if (errcode != 0)
+			return 1;
+			/* Not reached */
+	
+		if (matched != NULL)
+			pkgs = matched;
+		else switch (MatchType) {
+			case MATCH_GLOB:
+			break;
+			case MATCH_ALL:
+				warnx("no packages installed");
+			return 0;
+			/* Not reached */
+			case MATCH_REGEX:
+			case MATCH_EREGEX:
+				warnx("no packages match pattern(s)");
+				return 1;
+			/* Not reached */
+			default:
+			break;
+		}
     }
 
     for (i = 0; pkgs[i]; i++)
-	err_cnt += pkg_do(pkgs[i]);
+		err_cnt += pkg_do(pkgs[i]);
 
     return err_cnt;
 }
@@ -100,93 +100,92 @@
     int code = 0;
 
     if (isURL(pkg)) {
-	if ((cp = fileGetURL(NULL, pkg, KeepPackage)) != NULL) {
-	    if (!getcwd(fname, FILENAME_MAX))
-		upchuck("getcwd");
-	    isTMP = TRUE;
+		if ((cp = fileGetURL(NULL, pkg, KeepPackage)) != NULL) {
+			if (!getcwd(fname, FILENAME_MAX))
+			upchuck("getcwd");
+			isTMP = TRUE;
+		} else {
+			goto bail;
+		}
+    } else if (fexists(pkg) && isfile(pkg)) {
+		int len;
+	
+		if (*pkg != '/') {
+			if (!getcwd(fname, FILENAME_MAX))
+			upchuck("getcwd");
+			len = strlen(fname);
+			snprintf(&fname[len], FILENAME_MAX - len, "/%s", pkg);
+		}
+		else
+			strcpy(fname, pkg);
+		cp = fname;
 	} else {
-	    goto bail;
+		if ((cp = fileFindByPath(NULL, pkg)) != NULL)
+			strncpy(fname, cp, FILENAME_MAX);
 	}
-    }
-    else if (fexists(pkg) && isfile(pkg)) {
-	int len;
 
-	if (*pkg != '/') {
-	    if (!getcwd(fname, FILENAME_MAX))
-		upchuck("getcwd");
-	    len = strlen(fname);
-	    snprintf(&fname[len], FILENAME_MAX - len, "/%s", pkg);
+	if (cp) {
+		if (!isURL(pkg)) {
+			/*
+			* Apply a crude heuristic to see how much space the package will
+			* take up once it's unpacked.  I've noticed that most packages
+			* compress an average of 75%, but we're only unpacking the + files so
+			* be very optimistic.
+			*/
+			if (stat(fname, &sb) == FAIL) {
+				warnx("can't stat package file '%s'", fname);
+				code = 1;
+				goto bail;
+			}
+			Home = make_playpen(PlayPen, sb.st_size / 2);
+			if (unpack(fname, "'+*'")) {
+			warnx("error during unpacking, no info for '%s' available", pkg);
+			code = 1;
+			goto bail;
+			}
+		}
+	}
+    /* It's not an uninstalled package, try and find it among the installed */
+	else {
+		int isinstalled = isinstalledpkg(pkg);
+		if (isinstalled < 0) {
+			warnx("the package info for package '%s' is corrupt", pkg);
+			return 1;
+		} else if (isinstalled == 0) {
+			warnx("can't find package '%s' installed or in a file!", pkg);
+			return 1;
+		}
+		sprintf(log_dir, "%s/%s", LOG_DIR, pkg);
+		if (chdir(log_dir) == FAIL) {
+			warnx("can't change directory to '%s'!", log_dir);
+			return 1;
+		}
+		installed = TRUE;
 	}
-	else
-	    strcpy(fname, pkg);
-	cp = fname;
-    }
-    else {
-	if ((cp = fileFindByPath(NULL, pkg)) != NULL)
-	    strncpy(fname, cp, FILENAME_MAX);
-    }
-    if (cp) {
-	if (!isURL(pkg)) {
-	    /*
-	     * Apply a crude heuristic to see how much space the package will
-	     * take up once it's unpacked.  I've noticed that most packages
-	     * compress an average of 75%, but we're only unpacking the + files so
-	     * be very optimistic.
-	     */
-	    if (stat(fname, &sb) == FAIL) {
-	        warnx("can't stat package file '%s'", fname);
-	        code = 1;
-	        goto bail;
-	    }
-	    Home = make_playpen(PlayPen, sb.st_size / 2);
-	    if (unpack(fname, "'+*'")) {
-		warnx("error during unpacking, no info for '%s' available", pkg);
+
+	/* Suck in the contents list */
+	plist.head = plist.tail = NULL;
+	fp = fopen(CONTENTS_FNAME, "r");
+	if (!fp) {
+		warnx("unable to open %s file", CONTENTS_FNAME);
 		code = 1;
 		goto bail;
-	    }
 	}
-    }
-    /* It's not an uninstalled package, try and find it among the installed */
-    else {
-	int isinstalled = isinstalledpkg(pkg);
-	if (isinstalled < 0) {
-	    warnx("the package info for package '%s' is corrupt", pkg);
-	    return 1;
-	} else if (isinstalled == 0) {
-	    warnx("can't find package '%s' installed or in a file!", pkg);
-	    return 1;
-	}
-	sprintf(log_dir, "%s/%s", LOG_DIR, pkg);
-	if (chdir(log_dir) == FAIL) {
-	    warnx("can't change directory to '%s'!", log_dir);
-	    return 1;
-	}
-	installed = TRUE;
-    }
+	/* If we have a prefix, add it now */
+	read_plist(&plist, fp);
+	fclose(fp);
 
-    /* Suck in the contents list */
-    plist.head = plist.tail = NULL;
-    fp = fopen(CONTENTS_FNAME, "r");
-    if (!fp) {
-	warnx("unable to open %s file", CONTENTS_FNAME);
-	code = 1;
-	goto bail;
-    }
-    /* If we have a prefix, add it now */
-    read_plist(&plist, fp);
-    fclose(fp);
-
-    /*
+	/*
      * Index is special info type that has to override all others to make
      * any sense.
      */
-    if (Flags & SHOW_INDEX) {
-	char tmp[FILENAME_MAX];
+	if (Flags & SHOW_INDEX) {
+		char tmp[FILENAME_MAX];
 
-	snprintf(tmp, FILENAME_MAX, "%-19s ", pkg);
-	show_index(tmp, COMMENT_FNAME);
-    }
-    else {
+		snprintf(tmp, FILENAME_MAX, "%-19s ", pkg);
+		show_index(tmp, COMMENT_FNAME);
+	}
+	else {
 	/* Start showing the package contents */
 	if (!Quiet)
 	    printf("%sInformation for %s:\n\n", InfoPrefix, pkg);
@@ -329,6 +328,7 @@
 /* 
  * Look through package dbs in LOG_DIR and find which
  * packages installed the files in which_list.
+ * TODO: Speedup with bdb cache
  */
 static int 
 find_pkg(struct which_head *which_list)

==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/info/show.c#2 (text+ko) ====

@@ -248,7 +248,12 @@
     }
 }
 
-/* Calculate and show size of all installed package files (except ignored ones) */
+/* Calculate and show size of all installed package files (except ignored ones) 
+ * TODO: Make size easier to read (It should be unnecessary to set BLOCKSIZE in
+ *       environment). If the files are static in size, maybe it could be cached
+ *       instead of calculated every time. Could also be made default to show.
+ * 	
+ */
 void
 show_size(const char *title, Package *plist)
 {

==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/lib/lib.h#2 (text+ko) ====

@@ -35,6 +35,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <db.h>
 
 /* Macros */
 #define SUCCESS	(0)
@@ -70,6 +71,8 @@
 #define PKG_DBDIR	"PKG_DBDIR"
 /* macro to get name of directory where we put logging information */
 #define LOG_DIR		(getenv(PKG_DBDIR) ? getenv(PKG_DBDIR) : DEF_LOG_DIR)
+/* Path to the BDB cache file */
+#define PKG_DBCACHE_FILE	"/usr/home/duckjen/projects/soc2008/src/usr.sbin/pkg_install/pkgcache.db"
 
 /* The names of our "special" files */
 #define CONTENTS_FNAME		"+CONTENTS"
@@ -114,6 +117,8 @@
 #define PLIST_FMT_VER_MAJOR	1
 #define PLIST_FMT_VER_MINOR	1
 
+#define DEBUG(s) printf(s)
+
 enum _plist_t {
     PLIST_FILE, PLIST_CWD, PLIST_CMD, PLIST_CHMOD,
     PLIST_CHOWN, PLIST_CHGRP, PLIST_COMMENT, PLIST_IGNORE,
@@ -173,6 +178,8 @@
 void		str_lowercase(char *);
 char		*strconcat(const char *, const char *);
 char		*get_string(char *, int, FILE *);
+char		**get_string_array(char *str, char delim);
+void		free_string_array(char **str_arr);
 
 /* File */
 Boolean		fexists(const char *);
@@ -238,11 +245,24 @@
 int		verscmp(Package *, int, int);
 int		version_cmp(const char *, const char *);
 
+/* Database */
+Boolean cacheExists();	
+DB		*opendb(const char *filename);
+void	openDatabase(const char *filename);
+int		dbput(const DB *db, DBT *key, DBT *data);
+int		dbget(const DB *db, DBT *key, DBT *data);
+char	*dbgetdata(const char *aKey);
+Boolean	dbKeyExists(const char *aKey);
+int		closedb(DB *db);
+int		dbscan(const DB *db, DBT *key, DBT *data);
+
 /* Externs */
 extern Boolean	Quiet;
 extern Boolean	Fake;
-extern Boolean  Force;
+extern Boolean	Force;
+extern Boolean	CacheExists;
 extern int	AutoAnswer;
 extern int	Verbose;
+extern DB	*database;
 
 #endif /* _INST_LIB_LIB_H_ */

==== //depot/projects/soc2008/andenore_pkginstall/src/usr.sbin/pkg_install/lib/match.c#2 (text+ko) ====

@@ -53,6 +53,8 @@
  * Returns NULL-terminated list with matching names.
  * Names in list returned are dynamically allocated and should
  * not be altered by the caller.
+ *
+ * TODO: Make use of bdb cache to speed things up
  */
 char **
 matchinstalled(match_t MatchType, char **patterns, int *retval)
@@ -67,80 +69,95 @@
 
     store = storecreate(store);
     if (store == NULL) {
-	if (retval != NULL)
-	    *retval = 1;
-	return NULL;
+		if (retval != NULL)
+			*retval = 1;
+
+		return NULL;
     }
 
     if (retval != NULL)
-	*retval = 0;
+		*retval = 0;
 
     if (!isdir(paths[0])) {
-	if (retval != NULL)
-	    *retval = 1;
-	return NULL;
-	/* Not reached */
+		if (retval != NULL)
+			*retval = 1;
+		return NULL;
+		/* Not reached */
     }
 
     /* Count number of patterns */
     if (patterns != NULL) {
-	for (len = 0; patterns[len]; len++) {}
-	lmatched = alloca(sizeof(*lmatched) * len);
-	if (lmatched == NULL) {
-	    warnx("%s(): alloca() failed", __func__);
-	    if (retval != NULL)
-		*retval = 1;
-	    return NULL;
-    	} 
+		for (len = 0; patterns[len]; len++) {}
+		lmatched = alloca(sizeof(*lmatched) * len);
+		if (lmatched == NULL) {
+			warnx("%s(): alloca() failed", __func__);
+			if (retval != NULL)
+			*retval = 1;
+			return NULL;
+		} 
     } else
-	len = 0;
-    
-    for (i = 0; i < len; i++)
+		len = 0;
+
+	for (i = 0; i < len; i++)
 	lmatched[i] = FALSE;
 
-    ftsp = fts_open((char * const *)(uintptr_t)paths, FTS_LOGICAL | FTS_NOCHDIR | FTS_NOSTAT, fname_cmp);
-    if (ftsp != NULL) {
-	while ((f = fts_read(ftsp)) != NULL) {
-	    if (f->fts_info == FTS_D && f->fts_level == 1) {
-		fts_set(ftsp, f, FTS_SKIP);
-		matched = NULL;
-		errcode = 0;
-		if (MatchType == MATCH_ALL)
-		    matched = f->fts_name;
-		else 
-		    for (i = 0; patterns[i]; i++) {
-			errcode = pattern_match(MatchType, patterns[i], f->fts_name);
-			if (errcode == 1) {
-			    matched = f->fts_name;
-			    lmatched[i] = TRUE;
-			    errcode = 0;
+	/* TODO: Make use of cache to get directory names */
+	if (CacheExists == TRUE) {
+		DEBUG("USING CACHE\n");
+		DBT key, data;
+		while(dbscan(database, &key, &data) == 0) {
+			matched = key.data;
+			DEBUG(matched);
+			storeappend(store, matched);
+		}
+		return store->store;
+	} else {
+		DEBUG("Does not use cache!\n");
+	}
+
+	ftsp = fts_open((char * const *)(uintptr_t)paths, FTS_LOGICAL | FTS_NOCHDIR | FTS_NOSTAT, fname_cmp);
+	if (ftsp != NULL) {
+		while ((f = fts_read(ftsp)) != NULL) {
+			if (f->fts_info == FTS_D && f->fts_level == 1) {
+				fts_set(ftsp, f, FTS_SKIP);
+				matched = NULL;
+				errcode = 0;
+				if (MatchType == MATCH_ALL)
+					matched = f->fts_name;
+				else 
+					for (i = 0; patterns[i]; i++) {
+						errcode = pattern_match(MatchType, patterns[i], f->fts_name);
+						if (errcode == 1) {
+							matched = f->fts_name;
+							lmatched[i] = TRUE;
+							errcode = 0;
+						}
+						if (matched != NULL || errcode != 0)
+							break;
+					}
+				if (errcode == 0 && matched != NULL)
+					errcode = storeappend(store, matched);
+				if (errcode != 0) {
+					if (retval != NULL)
+						*retval = 1;
+					return NULL;
+					/* Not reached */
+				}
 			}
-			if (matched != NULL || errcode != 0)
-			    break;
-		    }
-		if (errcode == 0 && matched != NULL)
-		    errcode = storeappend(store, matched);
-		if (errcode != 0) {
-		    if (retval != NULL)
-			*retval = 1;
-		    return NULL;
-		    /* Not reached */
 		}
-	    }
+		fts_close(ftsp);
 	}
-	fts_close(ftsp);
-    }
 
-    if (MatchType == MATCH_GLOB) {
-	for (i = 0; i < len; i++)
-	    if (lmatched[i] == FALSE)
-		storeappend(store, patterns[i]);
-    }
+	if (MatchType == MATCH_GLOB) {
+		for (i = 0; i < len; i++)
+			if (lmatched[i] == FALSE)
+				storeappend(store, patterns[i]);
+	}
 
-    if (store->used == 0)
-	return NULL;
-    else
-	return store->store;
+	if (store->used == 0)
+		return NULL;
+	else
+		return store->store;
 }
 
 int
@@ -236,6 +253,7 @@
 /*
  * Synopsis is similar to matchinstalled(), but use origin
  * as a key for matching packages.
+ * TODO: Use bdb cache
  */
 char **
 matchallbyorigin(const char **origins, int *retval)
@@ -359,35 +377,43 @@
  * 
  * Return 1 if the specified package is installed,
  * 0 if not, and -1 if an error occured.
+ * TODO: Use bdb cache lookup
  */
 int
 isinstalledpkg(const char *name)
 {
-    int result;
-    char *buf, *buf2;
-    struct iip_memo *memo;
+	int result;
+	char *buf, *buf2;
+	struct iip_memo *memo;
 
-    LIST_FOREACH(memo, &iip_memo, iip_link) {
+	LIST_FOREACH(memo, &iip_memo, iip_link) {
 	if (strcmp(memo->iip_name, name) == 0)
-	    return memo->iip_result;
-    }
-    
-    buf2 = NULL;
-    asprintf(&buf, "%s/%s", LOG_DIR, name);
-    if (buf == NULL)
-	goto errout;
-    if (!isdir(buf) || access(buf, R_OK) == FAIL) {
-	result = 0;
-    } else {
-	asprintf(&buf2, "%s/%s", buf, CONTENTS_FNAME);
-	if (buf2 == NULL)
-	    goto errout;
+		return memo->iip_result;
+	}
+
+	/* If we have cache available use it to perform fast lookup */
+	if(CacheExists) {
+		return dbKeyExists(name);
+	} else 
+		DEBUG("Not using cache!\n");
+
+	buf2 = NULL;
+	asprintf(&buf, "%s/%s", LOG_DIR, name);
+	if (buf == NULL)

>>> TRUNCATED FOR MAIL (1000 lines) <<<


More information about the p4-projects mailing list