Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 15 Jul 2007 20:19:39 GMT
From:      Garrett Cooper <gcooper@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 123552 for review
Message-ID:  <200707152019.l6FKJdm4029386@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=123552

Change 123552 by gcooper@optimus-revised_pkgtools on 2007/07/15 20:19:06

	Trying to optimize pkg_version. It's broken for now..
	
	perform.old.c is the backed up version of pkg_version's perform.c.

Affected files ...

.. //depot/projects/soc2007/revised_fbsd_pkgtools/usr/src/usr.sbin/pkg_install/version/perform.c#2 edit
.. //depot/projects/soc2007/revised_fbsd_pkgtools/usr/src/usr.sbin/pkg_install/version/perform.old.c#1 add
.. //depot/projects/soc2007/revised_fbsd_pkgtools/usr/src/usr.sbin/pkg_install/version/version.h#2 edit

Differences ...

==== //depot/projects/soc2007/revised_fbsd_pkgtools/usr/src/usr.sbin/pkg_install/version/perform.c#2 (text+ko) ====

@@ -194,9 +194,8 @@
 			ch = NULL;
 		if (ch == NULL)
 		    errx(2, "The INDEX does not appear to be valid!");
-		if ((ie = malloc(sizeof(struct index_entry))) == NULL)
+		if ( (ie = calloc(1, sizeof(struct index_entry))) == NULL )
 		    errx(2, "Unable to allocate memory in %s.", __func__);
-		bzero(ie, sizeof(struct index_entry));
 		ie->name = strdup(tmp);
 		ie->origin = strdup(&ch[1]);
 		/* Who really cares if we reverse the index... */
@@ -326,77 +325,122 @@
 }
 
 int
-version_match(char *pattern, const char *pkgname)
+version_match(const char *pattern, const char *pkgname)
 {
+
+    typedef enum { EXACT_PACKAGENAME=0, PATTERN, UNDEF } search_type_e;
+    typedef enum { UNDETECTED_STREAM=-1, URL, LOCALFILE, STDIN } inputstream_type_e;
+
+    search_type_e trial_index;
+    inputstream_type_e matchstream = UNDETECTED_STREAM;
+
     int ret = 0;
-    int matchstream = 0;
+
     FILE *fp = NULL;
-    Boolean isTMP = FALSE;
+
+    const char *trial_strings[] = { pkgname, pattern, NULL };
+    search_type_e trial_categories[] = { EXACT_PACKAGENAME, PATTERN, UNDEF };
+
+    for(trial_index = EXACT_PACKAGENAME; trial_categories[trial_index] != UNDEF; trial_index++) {
+
+	/** URL provided file? **/
+	if (isURL(trial_strings[trial_index])) {
+	    fp = fetchGetURL(trial_strings[trial_index], "");
+	    if (fp == NULL)
+		errx(2, "Can't open %s.", trial_strings[trial_index]);
+	    matchstream = URL;
+	}
+	/** Open file? **/
+	else if (*trial_strings[trial_index] == '/') {
+	    fp = fopen(trial_strings[trial_index], "r");
+	    if (fp == NULL)
+		errx(2, "Unable to open %s.", trial_strings[trial_index]);
+	    matchstream = LOCALFILE;
+	}
+	/** Check for STDIN provided input **/
+	else if (strcmp(trial_strings[trial_index], "-") == 0) {
+	    fp = stdin;
+	    matchstream = STDIN;
+	}
+
+	if(matchstream != UNDETECTED_STREAM)
+	    break;
+
+    }
 
-    if (isURL(pkgname)) {
-	fp = fetchGetURL(pkgname, "");
-	isTMP = TRUE;
-	matchstream = 1;
-	if (fp == NULL)
-	    errx(2, "Unable to open %s.", pkgname);
-    } else if (pkgname[0] == '/') {
-	fp = fopen(pkgname, "r");
-	isTMP = TRUE;
-	matchstream = 1;
-	if (fp == NULL)
-	    errx(2, "Unable to open %s.", pkgname);
-    } else if (strcmp(pkgname, "-") == 0) {
-	fp = stdin;
-	matchstream = 1;
-    } else if (isURL(pattern)) {
-	fp = fetchGetURL(pattern, "");
-	isTMP = TRUE;
-	matchstream = -1;
-	if (fp == NULL)
-	    errx(2, "Unable to open %s.", pattern);
-    } else if (pattern[0] == '/') {
-	fp = fopen(pattern, "r");
-	isTMP = TRUE;
-	matchstream = -1;
-	if (fp == NULL)
-	    errx(2, "Unable to open %s.", pattern);
-    } else if (strcmp(pattern, "-") == 0) {
-	fp = stdin;
-	matchstream = -1;
-    } else {
+    if(trial_index == UNDEF)
 	ret = pattern_match(MATCH_GLOB, pattern, pkgname);
-    }
+    else {
 
-    if (fp != NULL) {
 	size_t len;
+
+	char ln[4*FILENAME_MAX+1];
 	char *line;
-	while ((line = fgetln(fp, &len)) != NULL) {
-	    int match;
-	    char *ch, ln[2048];
-	    size_t lnlen;
-	    if (len > 0 && line[len-1] == '\n')
-		len --;
-	    lnlen = len;
-	    if (lnlen > sizeof(ln)-1)
-		lnlen = sizeof(ln)-1;
-	    memcpy(ln, line, lnlen);
-	    ln[lnlen] = '\0';
-	    if ((ch = strchr(ln, '|')) != NULL)
-    		ch[0] = '\0';
-	    if (matchstream > 0)
-	    	match = pattern_match(MATCH_GLOB, pattern, ln);
-	    else
-	    	match = pattern_match(MATCH_GLOB, ln, pkgname);
-	    if (match == 1) {
-		ret = 1;
-		printf("%.*s\n", (int)len, line);
+
+	/** matchstream's not a real file; have to read line by line **/
+        if(matchstream != LOCALFILE) {
+
+	    while ( (line = fgetln(fp, &len)) != NULL )
+		ret = version_match_service(ln, line, len, trial_strings[trial_index], (Boolean) matchstream);
+
+	    if (matchstream != STDIN)
+		fclose(fp);
+
+	}
+	/** matchstream's a real file; can completely buffer the file into memory **/
+	else {
+
+	    char *lines = fileGetContentsByDescriptor(fp, pkgname);
+
+	    fclose(fp);
+
+	    for( line = strtok(lines, "\n"); line != NULL; line = strtok(NULL, "\n") ) {
+		len = sizeof(line);
+		ret = version_match_service(ln, line, len, trial_strings[trial_index], (Boolean) matchstream);
 	    }
+
 	}
-	if (isTMP)
-	    fclose(fp);
+
     }
 
     return ret;
+
+}
+
+int
+version_match_service(char *dest, const char *src, size_t src_len, const char *trial_string, Boolean is_package)
+{
+
+    int match;
+    char *ch;
+
+    size_t dest_len = src_len;
+
+    if(dest_len != 0 && src[dest_len-1] == '\n')
+	dest_len--;
+
+    if(dest_len > sizeof(src)-1)
+	dest_len = sizeof(src)-1;
+
+    memcpy(dest, src, dest_len);
+
+    *(dest+dest_len) = '\0';
+
+    if((ch = strchr(dest, '|')) != NULL)
+	ch[0] = '\0';
+
+    if(is_package)
+	match = pattern_match(MATCH_GLOB, (const char*) trial_string, dest);
+    else
+	match = pattern_match(MATCH_GLOB, dest, trial_string);
+
+    if(match == 1) {
+	printf("%.*s\n", (int)src_len, src);
+	return 1;
+    }
+
+    return 0;
+
 }
 
 void

==== //depot/projects/soc2007/revised_fbsd_pkgtools/usr/src/usr.sbin/pkg_install/version/version.h#2 (text+ko) ====

@@ -45,6 +45,7 @@
 extern Boolean	UseINDEXOnly;
 extern Boolean	ShowOrigin;
 
-extern int	version_match(char *, const char *);
+extern int	version_match(const char *, const char *);
+extern int	version_match_service(char *, const char *, size_t, const char *, Boolean);
 
 #endif	/* _INST_VERSION_H_INCLUDE */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200707152019.l6FKJdm4029386>