PERFORCE change 123519 for review

Garrett Cooper gcooper at FreeBSD.org
Sun Jul 15 06:03:02 UTC 2007


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

Change 123519 by gcooper at optimus-revised_pkgtools on 2007/07/15 06:02:26

	2 big changes:
	-Moved file read-in algoritm from add_plist(..) to lib/file.c (seems that there was already a subroutine that did what I was trying to accomplish sooner after I looked a bit closer.. heh).
	-Took the rest of kientzle's suggestion for converting if(str[0] == '{char}' methodology and stuffed it into switch/case statements. It does look nicer and the logic works. Added a nice
	comment to the head of the long statement block discussing choice-making in creating block and for ordering in block. 

Affected files ...

.. //depot/projects/soc2007/revised_fbsd_pkgtools/usr/src/usr.sbin/pkg_install/lib/plist.c#8 edit

Differences ...

==== //depot/projects/soc2007/revised_fbsd_pkgtools/usr/src/usr.sbin/pkg_install/lib/plist.c#8 (text+ko) ====

@@ -28,8 +28,6 @@
 #include <stdarg.h>
 #include <sys/time.h>
 
-#define BUFFER_TRY 1 
-
 int
 run_generic_plist_prof(char *fn_name, ...)
 {
@@ -109,7 +107,7 @@
 
     time_diff.tv_sec = after.tv_sec - before.tv_sec;
 
-    printf(  "(%s) Difference: %3.20lf secs\n", fn_name, (double) ( time_diff.tv_sec + time_diff.tv_nsec/1e9 )  );
+    fprintf(stderr,  "(%s) Difference: %3.20lf secs\n", fn_name, (double) ( time_diff.tv_sec + time_diff.tv_nsec/1e9 )  );
 
     return ret_code;
 
@@ -300,6 +298,7 @@
     str_lowercase(cmd);
     cp = cmd;
     sp = s;
+
     while (*cp) {
 	if (isspace(*cp)) {
 	    *cp = '\0';
@@ -309,98 +308,136 @@
 	}
 	++cp, ++sp;
     }
-    printf("COMMAND STRING: %s\nCMD"/*: %s\nCP: %s\nSP: %s\n"*/, s/*, cmd, cp, sp*/);
 
-    if(!*(cmd+1))
+    /** All commands should be greater than 1 char long **/
+    if(*cmd == '\0' || *(cmd+1) == '\0')
 	return FAIL;
 
     if (arg)
 	*arg = (char *)sp;
 
-    if(*cmd == 'c') {
+    /**
+     *  In order to avoid the a large sheer number of
+     *  loops produced by plist_cmd running strcmp,
+     *  let's prod the first char with a switch statement
+     *  to reduce the search radices later on down the line.
+     *
+     *  This is justified because this command is called
+     *  somewhere in the thousands range for iterations while
+     *  installing some packages.
+     *
+     *  First characters and subsequent command directives
+     *  are arranged in descending order of likelihood for
+     *  being present in +CONTENTS and related files.
+     */
+    switch(*cmd) {
+    case 'c':
+	/** COMMENT .. **/
+	if(!strcmp(cmd, "comment")) {
+	    /** .. with origin info. **/
+	    if (!strncmp(*arg, "ORIGIN:", 7)) {
+		*arg += 7;
+		return PLIST_ORIGIN;
+	    }
+	    /** .. with deporigin info. **/
+	    else if (!strncmp(*arg, "DEPORIGIN:", 10)) {
+	        *arg += 10;
+	        return PLIST_DEPORIGIN;
+	    }
+	    /** Standard comment :) **/
+	    return PLIST_COMMENT;
 
-	if(!strcmp(cmd+1, "wd") || *(cmd+1) == 'd') {
-	    printf("CWD IS: %s\n", sp);
+	}
+    	/** CWD / CD **/
+	else if(!strcmp(cmd, "cwd") || !strcmp(cmd, "cd")) {
 	    return PLIST_CWD;
 	}
+	/** CONFLICTS **/
+	else if(!strcmp(cmd, "conflicts"))
+	    return PLIST_CONFLICTS;
 
-	else if(*(cmd+1) == 'o') {
+	/** FALL-THROUGH **/
 
-	    if(!strcmp(cmd+2, "mment")) {
-	    	if (!strncmp(*arg, "ORIGIN:", 7)) {
-		    *arg += 7;
-		    return PLIST_ORIGIN;
-		} else if (!strncmp(*arg, "DEPORIGIN:", 10)) {
-		    *arg += 10;
-		    return PLIST_DEPORIGIN;
-	        }
-		return PLIST_COMMENT;
+    case 'e':
+	/** EXEC **/
+	if (!strcmp(cmd, "exec"))
+	    return PLIST_CMD;
+	/** FALL-THROUGH **/
 
-	    } else if(!strcmp(cmd+2, "nflicts")) {
-		return PLIST_CONFLICTS;
-	    }
+    case 'u':
+	/** UN(DO)EXEC **/
+	if (!strcmp(cmd, "unexec"))
+	    return PLIST_UNEXEC;
+	/** FALL-THROUGH **/
 
-	}
-
-    }
-
-    else if (!strcmp(cmd, "exec"))
-	return PLIST_CMD;
-    else if (!strcmp(cmd, "unexec"))
-	return PLIST_UNEXEC;
-    else if(*cmd == 'i') {
-
-	if (!strcmp(cmd+1, "gnore"))
+    case 'i':
+	/** IGNORE **/
+	if (!strcmp(cmd, "ignore"))
 	    return PLIST_IGNORE;
-	else if(!strcmp(cmd+1, "gnore_inst"))
+	/** IGNORE_INST **/
+	else if(!strcmp(cmd, "ignore_inst"))
 	    return PLIST_IGNORE_INST;
+	/** FALL-THROUGH **/
 
-    }
-
-    else if (*cmd == 'd') {
-
-	if (!strcmp(cmd+1, "irrm"))
+    case 'd':
+	/** DIRRM **/
+	if (!strcmp(cmd, "dirrm"))
 	    return PLIST_DIR_RM;
-	else if(!strcmp(cmd+1, "isplay"))
+	/** DISPLAY **/
+	else if(!strcmp(cmd, "display"))
 	    return PLIST_DISPLAY;
+	/** FALL-THROUGH **/
 
-    }
-
-    else if (*cmd == 'n') {
-
-	if (!strcmp(cmd+1, "oinst"))
+    case 'n':
+	/** NAME **/
+	if(!strcmp(cmd, "name"))
+	    return PLIST_NAME;
+	/** NOINST **/
+	else if (!strcmp(cmd, "noinst"))
 	    return PLIST_NOINST;
-	else if(!strcmp(cmd+1, "ame"))
-	    return PLIST_NAME;
+	/** FALL-THROUGH **/
 
-    }
-
-    else if (*cmd == 'm') {
-
-	if(!strcmp(cmd+1, "ode"))
+    case 'm':
+	/** MODE **/
+	if(!strcmp(cmd, "mode"))
 	    return PLIST_CHMOD;
-	else if(!strcmp(cmd+1, "tree"))
+	/** MTREE **/
+	else if(!strcmp(cmd, "mtree"))
 	    return PLIST_MTREE;
+	/** FALL-THROUGH **/
 
-    }
+    case 'o':
+	/** OWNER **/
+	if(!strcmp(cmd, "owner"))
+	    return PLIST_CHOWN;
+	/** OPTION **/
+	else if (!strcmp(cmd, "option"))
+	    return PLIST_OPTION;
+	/** FALL-THROUGH **/
 
-    else if (*cmd == 'o') {
+    case 's':
+	/** SRCDIR **/
+	if(!strcmp(cmd, "srcdir"))
+	    return PLIST_SRC;
+	/** FALL-THROUGH **/
 
-	if(!strcmp(cmd+1, "wner"))
-	    return PLIST_CHOWN;
-	else if (!strcmp(cmd+1, "ption"))
-	    return PLIST_OPTION;
+    case 'g':
+	/** GROUP **/
+    	if (!strcmp(cmd, "group"))
+	    return PLIST_CHGRP;
+	/** FALL-THROUGH **/
 
-    }
+    case 'p':
+	/** PKGDEP **/
+	if (!strcmp(cmd, "pkgdep"))
+	    return PLIST_PKGDEP;
+	/** FALL-THROUGH **/
 
-    else if (!strcmp(cmd, "srcdir"))
-	return PLIST_SRC;
-    else if (!strcmp(cmd, "group"))
-	return PLIST_CHGRP;
-    else if (!strcmp(cmd, "pkgdep"))
-	return PLIST_PKGDEP;
+    /** Apparently the command didn't match .. oops **/
+    default:
+	return FAIL;
 
-    return FAIL;
+    }
 
 }
 
@@ -414,8 +451,6 @@
 read_plist_np(Package *pkg, FILE *fp)
 {
 
-    struct stat contents_stat;
-
     char *plines;
     char *pline;
     char *cp;
@@ -426,19 +461,8 @@
     pkg->fmtver_mnr = 2;
     pkg->origin = NULL;
 
-    if(fstat( fileno(fp), &contents_stat ))
-	err(-1, "Could not fstat +CONTENTS file");
-
-    /* Empty file -- don't attempt to process */
-    assert(contents_stat.st_size != 0);
-
-    plines = (char*) malloc(contents_stat.st_size+1);
+    plines = fileGetContentsByDescriptor(fp, "+CONTENTS");
 
-    assert(contents_stat.st_size == read(fileno(fp), plines, contents_stat.st_size+1));
-
-    /* Just in case file doesn't have EOF */
-    *(plines+contents_stat.st_size) = '\0';
-
     pline = strtok(plines, "\n");
 
     if(0 == trim_end_whitespace(pline))
@@ -714,15 +738,20 @@
 		    if (cp != NULL) {
 			/* Mismatch? */
 			if (strcmp(cp, p->next->name + 4)) {
+
 			    warnx("'%s' fails original MD5 checksum - %s",
 				  tmp, Force ? "deleted anyway." : "not deleted.");
 			    if (!Force) {
 				fail = FAIL;
 				continue;
 			    }
+
 			}
+
 		    }
+
 		}
+
 		if (Verbose)
 		    printf("Delete file %s\n", tmp);
 		if (!Fake) {
@@ -732,15 +761,23 @@
 			char tmp2[FILENAME_MAX];
 			    
 			if (make_preserve_name(tmp2, FILENAME_MAX, name, tmp)) {
+
 			    if (fexists(tmp2)) {
+
 				if (rename(tmp2, tmp))
 				   warn("preserve: unable to restore %s as %s",
 					tmp2, tmp);
+
 			    }
+
 			}
+
 		    }
+
 		}
+
 	    }
+
 	    break;
 
 	case PLIST_DIR_RM:
@@ -822,9 +859,9 @@
 		return 1;
 	}
 	/* back up the pathname one component */
-	if (cp2) {
+	if (cp2)
 	    cp1 = strdup(dir);
-	}
+
     }
     return 0;
 }


More information about the p4-projects mailing list