Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 17 May 2010 10:48:29 GMT
From:      Garrett Cooper <gcooper@FreeBSD.org>
To:        Perforce Change Reviews <perforce@FreeBSD.org>
Subject:   PERFORCE change 178386 for review
Message-ID:  <201005171048.o4HAmTvw000577@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://p4web.freebsd.org/@@178386?ac=10

Change 178386 by gcooper@gcooper-bayonetta on 2010/05/17 10:47:46

	
	1. Remove all remaining references to cleanup in libpkg and shove all
	   cleanup calls to individual pkg_install applications.
	2. Remove all unused functions.
	3. Better style(9)-ize the headers and prototypes.
	
	1. is required so I can test via ctypes and so others can load libpkg
	without defining cleanup as an external function.

Affected files ...

.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/file.c#14 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/pen.c#2 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/pkg.h#8 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/tools/regression/lib/libpkg/tests/test_unpack.py#2 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/add/extract.c#3 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/add/perform.c#11 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/create/perform.c#27 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/create/pl.c#3 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/delete/perform.c#5 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/info/perform.c#6 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/info/show.c#3 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/updating/main.c#8 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/version/perform.c#10 edit

Differences ...

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/file.c#14 (text+ko) ====

@@ -184,32 +184,41 @@
     return NULL;
 }
 
+/*
+ * Fetch the metadata for a package contained in +CONTENTS.
+ *
+ * Return NULL on failure, or a buffer corresponding to the +CONTENTS file on
+ * success.
+ *
+ * cleanup(<int>) should be called if this call fails.
+ */
 char *
 fileGetContents(const char *fname)
 {
-    char *contents;
-    struct stat sb;
-    int fd;
+	struct stat sb;
+	char *contents = NULL;
+	int fd = -1;
+
+	if (stat(fname, &sb) == FAIL) {
+		warn("%s: can't stat '%s'", __func__, fname);
+	} else {
+		contents = (char *)malloc(sb.st_size + 1);
+		fd = open(fname, O_RDONLY, 0);
+		if (fd == -1)
+			warn("%s: unable to open '%s' for reading",
+			    __func__, fname);
+		else if (read(fd, contents, sb.st_size) != sb.st_size)
+			warn("%s: short read on '%s' - did not get %lld bytes",
+			    __func__, fname, (long long) sb.st_size);
+	}
+
+	if (0 <= fd)
+		close(fd);
+	if (contents != NULL)
+		contents[sb.st_size] = '\0';
 
-    if (stat(fname, &sb) == FAIL) {
-	cleanup(0);
-	errx(2, "%s: can't stat '%s'", __func__, fname);
-    }
+	return contents;
 
-    contents = (char *)malloc(sb.st_size + 1);
-    fd = open(fname, O_RDONLY, 0);
-    if (fd == FAIL) {
-	cleanup(0);
-	errx(2, "%s: unable to open '%s' for reading", __func__, fname);
-    }
-    if (read(fd, contents, sb.st_size) != sb.st_size) {
-	cleanup(0);
-	errx(2, "%s: short read on '%s' - did not get %lld bytes", __func__,
-	     fname, (long long)sb.st_size);
-    }
-    close(fd);
-    contents[sb.st_size] = '\0';
-    return contents;
 }
 
 /*
@@ -247,96 +256,66 @@
     return TRUE;
 }
 
-/* Write the contents of "str" to a file */
-void
+/* 
+ * Write the contents of "str" to a file 
+ *
+ * Return the number of bytes successfully written out to str or -1 on
+ * failure.
+ */
+size_t
 write_file(const char *name, const char *str)
 {
-    FILE *fp;
-    size_t len;
+	FILE *fp = NULL;
+	off_t written_len = -1;
+	size_t len;
 
-    fp = fopen(name, "w");
-    if (!fp) {
-	cleanup(0);
-	errx(2, "%s: cannot fopen '%s' for writing", __func__, name);
-    }
-    len = strlen(str);
-    if (fwrite(str, 1, len, fp) != len) {
-	cleanup(0);
-	errx(2, "%s: short fwrite on '%s', tried to write %ld bytes",
-	    __func__, name, (long)len);
-    }
-    if (fclose(fp)) {
-	cleanup(0);
-	errx(2, "%s: failure to fclose '%s'", __func__, name);
-    }
-}
+	errno = 0;
 
-void
-copy_file(const char *dir, const char *fname, const char *to)
-{
-    char cmd[FILENAME_MAX];
+	fp = fopen(name, "w");
+	if (fp == NULL)
+		warn("%s: cannot fopen '%s' for writing", __func__, name);
+	else {
+		len = strlen(str);
+		written_len = fwrite(str, 1, len, fp);
+		if ((len-written_len) != 0)
+			warn("%s: short fwrite on '%s', tried to write %ld "
+			    "bytes", __func__, name, (long) len);
 
-    if (fname[0] == '/')
-	snprintf(cmd, FILENAME_MAX, "/bin/cp -r %s %s", fname, to);
-    else
-	snprintf(cmd, FILENAME_MAX, "/bin/cp -r %s/%s %s", dir, fname, to);
-    if (vsystem(cmd)) {
-	cleanup(0);
-	errx(2, "%s: could not perform '%s'", __func__, cmd);
-    }
-}
+		if (fp != NULL)
+			if (fclose(fp) != 0)
+				warn("%s: failed to close file: '%s'",
+				    __func__, name);
+	}
 
-void
-move_file(const char *dir, const char *fname, const char *tdir)
-{
-    char from[FILENAME_MAX];
-    char to[FILENAME_MAX];
-
-    if (fname[0] == '/')
-	strncpy(from, fname, FILENAME_MAX);
-    else
-	snprintf(from, FILENAME_MAX, "%s/%s", dir, fname);
+	return (size_t) (errno == 0 && written_len > 0 ? written_len : -1);
 
-    snprintf(to, FILENAME_MAX, "%s/%s", tdir, fname);
-
-    if (rename(from, to) == -1) {
-        if (vsystem("/bin/mv %s %s", from, to)) {
-	    cleanup(0);
-	    errx(2, "%s: could not move '%s' to '%s'", __func__, from, to);
-	}
-    }
 }
 
 /*
- * Copy a hierarchy (possibly from dir) to the current directory, or
- * if "to" is TRUE, from the current directory to a location someplace
- * else.
+ * From a path specified by `from' to the path denoted as `to'.
  *
- * Though slower, using tar to copy preserves symlinks and everything
- * without me having to write some big hairy routine to do it.
+ * Returns 0 on success, -1 on failure.
  */
-void
-copy_hierarchy(const char *dir, const char *fname, Boolean to)
+int
+move_file(const char *dir, const char *fname, const char *tdir)
 {
-    char cmd[FILENAME_MAX * 3];
+	char from[FILENAME_MAX];
+	char to[FILENAME_MAX];
+	int rc;
+
+	if (fname[0] == '/')
+		strncpy(from, fname, sizeof(from));
+	else
+		snprintf(from, sizeof(from), "%s/%s", dir, fname);
+
+	snprintf(to, FILENAME_MAX, "%s/%s", tdir, fname);
+
+	if (rename(from, to) == 0 || vsystem("/bin/mv %s %s", from, to) == 0)
+		rc = 0;
+	else
+		rc = -1;
 
-    if (!to) {
-	/* If absolute path, use it */
-	if (*fname == '/')
-	    dir = "/";
-	snprintf(cmd, FILENAME_MAX * 3, "/usr/bin/tar cf - -C %s %s | /usr/bin/tar xpf -",
-		 dir, fname);
-    }
-    else
-	snprintf(cmd, FILENAME_MAX * 3, "/usr/bin/tar cf - %s | /usr/bin/tar xpf - -C %s",
-		 fname, dir);
-#ifdef DEBUG
-    printf("Using '%s' to copy trees.\n", cmd);
-#endif
-    if (system(cmd)) {
-	cleanup(0);
-	errx(2, "%s: could not perform '%s'", __func__, cmd);
-    }
+	return rc;
 }
 
 #define EXTRACT_ARCHIVE_FLAGS	(ARCHIVE_EXTRACT_OWNER |ARCHIVE_EXTRACT_PERM| \

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/pen.c#2 (text+ko) ====

@@ -21,13 +21,15 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD: src/lib/libpkg/pen.c,v 1.1 2010/04/23 11:07:43 flz Exp $");
 
-#include "pkg.h"
+#include <sys/signal.h>
+#include <sys/param.h>
+#include <sys/mount.h>
 #include <err.h>
+#include <errno.h>
 #include <libutil.h>
 #include <libgen.h>
-#include <sys/signal.h>
-#include <sys/param.h>
-#include <sys/mount.h>
+
+#include "pkg.h"
 
 /* For keeping track of where we are */
 static char PenLocation[FILENAME_MAX];
@@ -42,33 +44,30 @@
 static char *
 find_play_pen(char *pen, off_t sz)
 {
-    char *cp;
-    struct stat sb;
-    char humbuf[6];
+	char *cp;
+	struct stat sb;
 
-    if (pen[0] && isdir(dirname(pen)) == TRUE && (min_free(dirname(pen)) >= sz))
+	if (pen[0] != '\0' && isdir(dirname(pen)) == TRUE &&
+	    (min_free(dirname(pen)) >= sz))
+		return pen;
+	else if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) == 0 &&
+	    (min_free(cp) >= sz))
+		sprintf(pen, "%s/instmp.XXXXXX", cp);
+	else if ((cp = getenv("TMPDIR")) != NULL && stat(cp, &sb) == 0 &&
+	    (min_free(cp) >= sz))
+		sprintf(pen, "%s/instmp.XXXXXX", cp);
+	else if (stat("/var/tmp", &sb) == 0 && min_free("/var/tmp") >= sz)
+		strcpy(pen, "/var/tmp/instmp.XXXXXX");
+	else if (stat("/tmp", &sb) == 0 && min_free("/tmp") >= sz)
+		strcpy(pen, "/tmp/instmp.XXXXXX");
+	else if ((stat("/usr/tmp", &sb) == 0 ||
+	    mkdir("/usr/tmp", 01777) == SUCCESS) && min_free("/usr/tmp") >= sz)
+		strcpy(pen, "/usr/tmp/instmp.XXXXXX");
+	else {
+		errno = ENOSPC;
+		return NULL;
+	}
 	return pen;
-    else if ((cp = getenv("PKG_TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
-	sprintf(pen, "%s/instmp.XXXXXX", cp);
-    else if ((cp = getenv("TMPDIR")) != NULL && stat(cp, &sb) != FAIL && (min_free(cp) >= sz))
-	sprintf(pen, "%s/instmp.XXXXXX", cp);
-    else if (stat("/var/tmp", &sb) != FAIL && min_free("/var/tmp") >= sz)
-	strcpy(pen, "/var/tmp/instmp.XXXXXX");
-    else if (stat("/tmp", &sb) != FAIL && min_free("/tmp") >= sz)
-	strcpy(pen, "/tmp/instmp.XXXXXX");
-    else if ((stat("/usr/tmp", &sb) == SUCCESS || mkdir("/usr/tmp", 01777) == SUCCESS) && min_free("/usr/tmp") >= sz)
-	strcpy(pen, "/usr/tmp/instmp.XXXXXX");
-    else {
-	cleanup(0);
-	humanize_number(humbuf, sizeof humbuf, sz, "", HN_AUTOSCALE,
-	    HN_NOSPACE);
-	errx(2,
-"%s: can't find enough temporary space to extract the files, please set your\n"
-"PKG_TMPDIR environment variable to a location with at least %s bytes\n"
-"free", __func__, humbuf);
-	return NULL;
-    }
-    return pen;
 }
 
 #define MAX_STACK	20
@@ -103,67 +102,61 @@
 const char *
 make_playpen(char *pen, off_t sz)
 {
-    char humbuf[6];
-    char cwd[FILENAME_MAX];
+	char cwd[FILENAME_MAX];
+	const char *pen_location = NULL;
 
-    if (!find_play_pen(pen, sz))
-	return NULL;
+	if (find_play_pen(pen, sz) != NULL && mkdtemp(pen) == 0) {
 
-    if (!mkdtemp(pen)) {
-	cleanup(0);
-	errx(2, "%s: can't mktemp '%s'", __func__, pen);
-    }
+		if (min_free(pen) < sz) {
+			rmdir(pen);
+			errno = ENOSPC;
+		}
+		else if (getcwd(cwd, FILENAME_MAX) == 0 && chdir(pen) == 0) {
+			strcpy(PenLocation, pen);
+			pen_location = pushPen(cwd);
+		}
 
-    humanize_number(humbuf, sizeof humbuf, sz, "", HN_AUTOSCALE, HN_NOSPACE);
+	}
 
-    if (min_free(pen) < sz) {
-	rmdir(pen);
-	cleanup(0);
-	errx(2, "%s: not enough free space to create '%s'.\n"
-	     "Please set your PKG_TMPDIR environment variable to a location\n"
-	     "with at least %s and try the command again",
-	     __func__, humbuf, pen);
-    }
+	return pen_location;
 
-    if (!getcwd(cwd, FILENAME_MAX)) {
-	upchuck("getcwd");
-	return NULL;
-    }
-
-    if (chdir(pen) == FAIL) {
-	cleanup(0);
-	errx(2, "%s: can't chdir to '%s'", __func__, pen);
-    }
-
-    strcpy(PenLocation, pen);
-    return pushPen(cwd);
 }
 
 /* Convenience routine for getting out of playpen */
 int
 leave_playpen()
 {
-    static char left[FILENAME_MAX];
-    void (*oldsig)(int);
+	void (*oldsig)(int);
+	static char left[FILENAME_MAX];
+	int rc = -1;
+
+	if (PenLocation[0] == '\0')
+		rc = 0;
+	else {
+
+		/* Don't interrupt while we're cleaning up */
+		oldsig = signal(SIGINT, SIG_IGN);
+		strcpy(left, PenLocation);
+		popPen(PenLocation);
+
+		if (chdir(PenLocation) == 0) {
+
+			if (left[0] == '/' &&
+			    vsystem("/bin/rm -rf %s", left)) {
+				warnx("couldn't remove temporary dir '%s'",
+				    left);
+			}
+
+			rc = 1;
 
-    if (!PenLocation[0])
-	return 0;
+		}
 
-    /* Don't interrupt while we're cleaning up */
-    oldsig = signal(SIGINT, SIG_IGN);
-    strcpy(left, PenLocation);
-    popPen(PenLocation);
+		signal(SIGINT, oldsig);
 
-    if (chdir(PenLocation) == FAIL) {
-	cleanup(0);
-	errx(2, "%s: can't chdir back to '%s'", __func__, PenLocation);
-    }
+	}
 
-    if (left[0] == '/' && vsystem("/bin/rm -rf %s", left))
-	warnx("couldn't remove temporary dir '%s'", left);
-    signal(SIGINT, oldsig);
+	return rc;
 
-    return 1;
 }
 
 off_t

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/lib/libpkg/pkg.h#8 (text+ko) ====

@@ -155,7 +155,6 @@
 /* Misc */
 int		vsystem(const char *, ...);
 char		*vpipe(const char *, ...);
-void		cleanup(int);
 const char	*make_playpen(char *, off_t);
 char		*where_playpen(void);
 int		leave_playpen(void);
@@ -183,22 +182,15 @@
 const char	*fileGetURL(const char *, const char *, int);
 char		*fileFindByPath(const char *, const char *);
 char		*fileGetContents(const char *);
-void		write_file(const char *, const char *);
-void		copy_file(const char *, const char *, const char *);
-void		move_file(const char *, const char *, const char *);
-void		copy_hierarchy(const char *, const char *, Boolean);
+size_t		write_file(const char *, const char *);
+int		move_file(const char *, const char *, const char *);
 int		delete_hierarchy(const char *, Boolean, Boolean);
 char*		unpack_to_buffer(const char *, const char *);
 int		unpack_to_disk(const char *, const char *);
 int		unpack_to_fd(const char *, const char *);
-void		format_cmd(char *, int, const char *, const char *, const char *);
+void		format_cmd(char *, int, const char *, const char *,
+		    const char *);
 
-/* Msg */
-void		upchuck(const char *);
-void		barf(const char *, ...);
-void		whinge(const char *, ...);
-Boolean		y_or_n(Boolean, const char *, ...);
-
 /* Packing list */
 PackingList	new_plist_entry(void);
 PackingList	last_plist(Package *);
@@ -211,8 +203,8 @@
 void		add_plist(Package *, plist_t, const char *);
 void		add_plist_top(Package *, plist_t, const char *);
 void		delete_plist(Package *pkg, Boolean all, plist_t type, const char *name);
-void		write_plist(Package *, FILE *);
-void		read_plist(Package *, int);
+int		write_plist(Package *, FILE *);
+int		read_plist(Package *, int);
 int		plist_cmd(const char *, char **);
 int		delete_package(Boolean, Boolean, Package *);
 Boolean 	make_preserve_name(char *, int, const char *, const char *);

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/tools/regression/lib/libpkg/tests/test_unpack.py#2 (text+ko) ====

@@ -1,7 +1,6 @@
 #!/usr/bin/env python
 
 import ctypes
-#from nose import
 import tarfile
 import tempfile
 import tests

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/add/extract.c#3 (text+ko) ====

@@ -26,6 +26,7 @@
 #include <pkg.h>
 #include "add.h"
 
+extern void	cleanup(int);
 
 #define STARTSTRING "/usr/bin/tar cf -"
 #define TOOBIG(str) \

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/add/perform.c#11 (text+ko) ====

@@ -30,10 +30,12 @@
 #include <signal.h>
 #include <sys/wait.h>
 
-static int pkg_do(char *);
-static int sanity_check(char *);
-static char LogDir[FILENAME_MAX];
-static int zapLogDir;		/* Should we delete LogDir? */
+void		cleanup(int);
+static int	pkg_do(char *);
+static int	sanity_check(char *);
+
+static char	LogDir[FILENAME_MAX];
+static int	zapLogDir;		/* Should we delete LogDir? */
 
 int
 pkg_perform(char **pkgs)
@@ -74,6 +76,7 @@
     int cfile;
     int code;
     int inPlace, conflictsfound, errcode;
+    int rc;
     /* support for separate pre/post install scripts */
     int new_m = 0;
     int fd;
@@ -95,7 +98,8 @@
 	    warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
 	    return 1;
 	}
-	read_plist(&Plist, fileno(stdin));
+	if (read_plist(&Plist, fileno(stdin)) != 0)
+	    return 1;
 	where_to = playpen;
     }
     /* Nope - do it now */
@@ -104,6 +108,7 @@
 	if (isURL(pkg)) {
 	    if (!(where_to = fileGetURL(NULL, pkg, KeepPackage))) {
 		warnx("unable to fetch '%s' by URL", pkg);
+		cleanup(0);
 		return 1;
 	    }
 	    cfile = open(CONTENTS_FNAME, O_RDONLY);
@@ -113,8 +118,10 @@
 		CONTENTS_FNAME);
 		goto bomb;
 	    }
-	    read_plist(&Plist, cfile);
+	    rc = read_plist(&Plist, cfile);
 	    close(cfile);
+	    if (rc != 0)
+		goto bomb;
 	}
 	else {
 
@@ -133,7 +140,7 @@
 		    goto bomb;
 		}
 	    }
-	    if (!(where_to = make_playpen(playpen, sb.st_size * 4)))
+	    if ((where_to = make_playpen(playpen, sb.st_size * 4)) == NULL)
 		errx(1, "unable to make playpen for %lld bytes", (long long)sb.st_size * 4);
 	    /* Since we can call ourselves recursively, keep notes on where we came from */
 	    if (!getenv("_TOP"))
@@ -155,8 +162,10 @@
 		CONTENTS_FNAME);
 		goto bomb;
 	    }
-	    read_plist(&Plist, cfile);
+	    rc = read_plist(&Plist, cfile);
 	    close(cfile);
+	    if (rc != 0)
+		goto bomb;
 
 	    /* Extract directly rather than moving?  Oh goodie! */
 	    if (find_plist_option(&Plist, "extract-in-place")) {
@@ -217,7 +226,8 @@
 	/* If we're running in MASTER mode, just output the plist and return */
 	if (AddMode == MASTER) {
 	    printf("%s\n", where_playpen());
-	    write_plist(&Plist, stdout);
+	    if (write_plist(&Plist, stdout) != 0)
+		return 1;
 	    return 0;
 	}
     }
@@ -369,26 +379,37 @@
 			    ++code;
 		    }
 		}
-		else if ((cp = fileGetURL(pkg, p->name, KeepPackage)) != NULL) {
-		    if (Verbose)
-			printf("Finished loading %s via a URL\n", p->name);
-		    if (!fexists(CONTENTS_FNAME)) {
-			warnx("autoloaded package %s has no %s file?",
+		else {
+
+		    if ((cp = fileGetURL(pkg, p->name, KeepPackage)) == NULL) {
+			cleanup(0);
+			warnx("Getting pkg '%s' by URL failed", pkg);
+			goto bomb;
+		    } else {
+
+			if (Verbose)
+			    printf("Finished loading %s via a URL\n", p->name);
+			if (!fexists(CONTENTS_FNAME)) {
+			    warnx("autoloaded package %s has no %s file?",
 				p->name, CONTENTS_FNAME);
-			if (!Force)
-			    ++code;
-		    }
-		    else if (vsystem("(pwd; /bin/cat " CONTENTS_FNAME ") | %s %s %s %s -S", PkgAddCmd, Verbose ? "-v" : "", PrefixRecursive ? prefixArg : "", KeepPackage ? "-K" : "")) {
-			warnx("pkg_add of dependency '%s' failed%s",
+			    if (!Force)
+				++code;
+		        }
+			else if (vsystem("(pwd; /bin/cat " CONTENTS_FNAME ") | %s %s %s %s -S", PkgAddCmd, Verbose ? "-v" : "", PrefixRecursive ? prefixArg : "", KeepPackage ? "-K" : "")) {
+			    warnx("pkg_add of dependency '%s' failed%s",
 				p->name, Force ? " (proceeding anyway)" : "!");
-			if (!Force)
-			    ++code;
+			    if (!Force)
+				++code;
+			}
+			else if (Verbose)
+			    printf("\t'%s' loaded successfully.\n", p->name);
+			/* Nuke the temporary playpen */
+			leave_playpen();
+
 		    }
-		    else if (Verbose)
-			printf("\t'%s' loaded successfully.\n", p->name);
-		    /* Nuke the temporary playpen */
-		    leave_playpen();
+
 		}
+
 	    }
 	    else {
 		if (Verbose)
@@ -491,10 +512,10 @@
 
     /* Time to record the deed? */
     if (!NoRecord && !Fake) {
+	FILE *contfile;
 	char contents[FILENAME_MAX];
 	char **depnames = NULL, **deporigins = NULL, ***depmatches;
-	int i, dep_count = 0;
-	FILE *contfile;
+	int i, dep_count = 0, rc;
 
 	if (getuid() != 0)
 	    warnx("not running as root - trying to record install anyway");
@@ -510,26 +531,38 @@
 	    goto success;	/* close enough for government work */
 	}
 	/* Make sure pkg_info can read the entry */
+
+	/* XXX (gcooper) fix return code checks. */
 	fd = open(LogDir, O_RDWR);
 	fstat(fd, &sb);
 	fchmod(fd, sb.st_mode | S_IRALL | S_IXALL);	/* be sure, chmod a+rx */
 	close(fd);
-	move_file(".", DESC_FNAME, LogDir);
+	if (move_file(".", DESC_FNAME, LogDir) == -1) {
+	    cleanup(0);
+	    errx(EXIT_FAILURE, "failed to move %s to %s", DESC_FNAME, LogDir);
+	}
 	move_file(".", COMMENT_FNAME, LogDir);
-	if (fexists(INSTALL_FNAME))
-	    move_file(".", INSTALL_FNAME, LogDir);
-	if (fexists(POST_INSTALL_FNAME))
-	    move_file(".", POST_INSTALL_FNAME, LogDir);
-	if (fexists(DEINSTALL_FNAME))
-	    move_file(".", DEINSTALL_FNAME, LogDir);
-	if (fexists(POST_DEINSTALL_FNAME))
-	    move_file(".", POST_DEINSTALL_FNAME, LogDir);
-	if (fexists(REQUIRE_FNAME))
-	    move_file(".", REQUIRE_FNAME, LogDir);
-	if (fexists(DISPLAY_FNAME))
-	    move_file(".", DISPLAY_FNAME, LogDir);
-	if (fexists(MTREE_FNAME))
-	    move_file(".", MTREE_FNAME, LogDir);
+
+#define MOVE_IF_PRESENT(file)						\
+	do {								\
+		if (fexists(file)) {					\
+			if (move_file(".", file, LogDir) == -1) {	\
+				cleanup(0);				\
+				errx(EXIT_FAILURE,			\
+				    "failed to move '%s' to '%s'",	\
+				    file, LogDir);			\
+			}						\
+		}							\
+	} while (0)
+
+	MOVE_IF_PRESENT(INSTALL_FNAME);
+	MOVE_IF_PRESENT(POST_INSTALL_FNAME);
+	MOVE_IF_PRESENT(POST_INSTALL_FNAME);
+	MOVE_IF_PRESENT(DEINSTALL_FNAME);
+	MOVE_IF_PRESENT(POST_DEINSTALL_FNAME);
+	MOVE_IF_PRESENT(REQUIRE_FNAME);
+	MOVE_IF_PRESENT(DISPLAY_FNAME);
+	MOVE_IF_PRESENT(MTREE_FNAME);
 	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
 	contfile = fopen(contents, "w");
 	if (!contfile) {
@@ -537,8 +570,10 @@
 		contents);
 	    goto success; /* can't log, but still keep pkg */
 	}
-	write_plist(&Plist, contfile);
+	rc = write_plist(&Plist, contfile);
 	fclose(contfile);
+	if (rc != 0)
+	    goto fail;
 	for (p = Plist.head; p ; p = p->next) {
 	    char *deporigin;
 

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/create/perform.c#27 (text+ko) ====

@@ -46,12 +46,14 @@
 #include <pkg.h>
 #include "create.h"
 
-static void sanity_check(void);
+void		cleanup(int);
+
+static void	sanity_check(void);
 static const char* add_file(struct archive *, const char *, const char *,
 		const int);
-static void make_dist(const char *, const char *, const char *, Package *);
-static int create_from_installed_recursive(const char *, const char *);
-static int create_from_installed(const char *, const char *, const char *);
+static void	make_dist(const char *, const char *, const char *, Package *);
+static int	create_from_installed_recursive(const char *, const char *);
+static int	create_from_installed(const char *, const char *, const char *);
 
 int
 pkg_perform(char **pkgs)
@@ -63,6 +65,7 @@
     char *cp;
     int len;
     int pkg_in;
+    int rc;
     const char *suf;
 
     /* Preliminary setup */
@@ -219,7 +222,10 @@
     }
 
     /* Slurp in the packing list */
-    read_plist(&plist, pkg_in);
+    if (read_plist(&plist, pkg_in) != 0) {
+	close(pkg_in);
+	return FALSE;
+    }
 
     /* Prefix should add an @cwd to the packing list */
     if (Prefix)
@@ -250,12 +256,14 @@
      */
     if (PlistOnly) {
 	check_list(home, &plist);
-	write_plist(&plist, stdout);
-	exit(0);
+	exit(write_plist(&plist, stdout) == 0 ? 0 : 1);
     }
 
     /* Make a directory to stomp around in */
-    home = make_playpen(PlayPen, 0);
+    if ((home = make_playpen(PlayPen, 0)) == NULL) {
+	cleanup(0);
+	errx(EXIT_FAILURE, "failed to create a playpen");
+    }
     signal(SIGINT, cleanup);
     signal(SIGHUP, cleanup);
 
@@ -272,14 +280,24 @@
     if (Prefix == NULL)
 	add_plist(&plist, PLIST_CWD, ".");
 
-    write_file(COMMENT_FNAME, Comment);
-    add_plist(&plist, PLIST_IGNORE, NULL);
-    add_plist(&plist, PLIST_FILE, COMMENT_FNAME);
-    add_cksum(&plist, plist.tail, COMMENT_FNAME);
-    write_file(DESC_FNAME, Desc);
-    add_plist(&plist, PLIST_IGNORE, NULL);
-    add_plist(&plist, PLIST_FILE, DESC_FNAME);
-    add_cksum(&plist, plist.tail, DESC_FNAME);
+    if (write_file(COMMENT_FNAME, Comment) == 0) {
+	add_plist(&plist, PLIST_IGNORE, NULL);
+	add_plist(&plist, PLIST_FILE, COMMENT_FNAME);
+	add_cksum(&plist, plist.tail, COMMENT_FNAME);
+    }
+    else {
+	cleanup(0);
+	err(EXIT_FAILURE, "failed to write comment file");
+    }
+    if (write_file(DESC_FNAME, Desc) == 0) {
+	add_plist(&plist, PLIST_IGNORE, NULL);
+	add_plist(&plist, PLIST_FILE, DESC_FNAME);
+	add_cksum(&plist, plist.tail, DESC_FNAME);
+    }
+    else {
+	cleanup(0);
+	err(EXIT_FAILURE, "failed to write description file");
+    }
 
     if (Install != NULL) {
 	add_plist(&plist, PLIST_IGNORE, NULL);
@@ -323,12 +341,15 @@
     fp = fopen(CONTENTS_FNAME, "w");
     if (!fp) {
 	cleanup(0);
-	err(2, "%s: can't open file %s for writing", __func__, CONTENTS_FNAME);
+	errx(2, "%s: can't open file %s for writing", __func__, CONTENTS_FNAME);
     }
-    write_plist(&plist, fp);
+    rc = write_plist(&plist, fp);
     if (fclose(fp)) {
 	cleanup(0);
-	err(2, "%s: error occurred when closing %s", __func__, CONTENTS_FNAME);
+	errx(2, "%s: error occurred when closing %s", __func__, CONTENTS_FNAME);
+    } else if (rc != 0) {
+	cleanup(0);
+	errx(2, "%s: couldn't write to %s", __func__, CONTENTS_FNAME);
     }
 
     /* And stick it into a tarball */
@@ -775,82 +796,96 @@
 	Package plist;
 	PackingList p;
 	char tmp[PATH_MAX];
-	int fd;
-	int rval;
+	int fd, rc;
+	int rval = FALSE;
 
 	if (!create_from_installed(InstalledPkg, pkg, suf))
-		return FALSE;
+		return rval;
 	snprintf(tmp, sizeof(tmp), "%s/%s/%s",
 	    LOG_DIR, InstalledPkg, CONTENTS_FNAME);
 	if (!fexists(tmp)) {
 		warnx("can't find package '%s' installed!", InstalledPkg);
-		return FALSE;
+		return rval;
 	}
 	/* Suck in the contents list */
 	plist.head = plist.tail = NULL;
 	fd = open(tmp, O_RDONLY);
 	if (fd == -1) {
 		warn("unable to open %s file", tmp);
-		return FALSE;
+		return rval;
 	}
-	read_plist(&plist, fd);
+	rc = read_plist(&plist, fd);
 	close(fd);
-	rval = TRUE;
-	for (p = plist.head; p != NULL && rval == TRUE; p = p->next) {
-		if (p->type == PLIST_PKGDEP) {
-			if (Verbose)
-				printf("Creating package %s\n", p->name);
-			if (!create_from_installed(p->name, p->name, suf))
-				rval = FALSE;
+	if (rc == 0) {
+
+		rval = TRUE;
+
+		for (p = plist.head; p != NULL && rval == TRUE; p = p->next) {
+
+			if (p->type == PLIST_PKGDEP) {
+				if (Verbose)
+					printf("Creating package %s\n",
+					    p->name);
+				if (!create_from_installed(p->name, p->name,
+				    suf))
+					rval = FALSE;
+			}
+
 		}
+
+		free_plist(&plist);
+
 	}
-	free_plist(&plist);
+
 	return rval;
 }
 
 static int
 create_from_installed(const char *ipkg, const char *pkg, const char *suf)
 {
-    int fd;
-    Package plist;
-    char homedir[MAXPATHLEN], log_dir[FILENAME_MAX];
+	Package plist;
+	int fd, rc;
+	char homedir[MAXPATHLEN], log_dir[FILENAME_MAX];
+
+	snprintf(log_dir, sizeof(log_dir), "%s/%s", LOG_DIR, ipkg);
+	if (!fexists(log_dir)) {
+		warnx("can't find package '%s' installed!", ipkg);
+		return FALSE;
+	}
+	getcwd(homedir, sizeof(homedir));
+	if (chdir(log_dir) == FAIL) {
+		warnx("can't change directory to '%s'!", log_dir);
+		return FALSE;
+	}
+	/* Suck in the contents list */
+	plist.head = plist.tail = NULL;
+	fd = open(CONTENTS_FNAME, O_RDONLY);
+	if (fd == -1) {
+		warnx("unable to open %s file", CONTENTS_FNAME);
+		return FALSE;
+	}
+	rc = read_plist(&plist, fd);
+	(void) close(fd);
 
-    snprintf(log_dir, sizeof(log_dir), "%s/%s", LOG_DIR, ipkg);
-    if (!fexists(log_dir)) {
-	warnx("can't find package '%s' installed!", ipkg);
-	return FALSE;
-    }
-    getcwd(homedir, sizeof(homedir));
-    if (chdir(log_dir) == FAIL) {
-	warnx("can't change directory to '%s'!", log_dir);
-	return FALSE;
-    }
-    /* Suck in the contents list */
-    plist.head = plist.tail = NULL;
-    fd = open(CONTENTS_FNAME, O_RDONLY);
-    if (fd == -1) {
-	warnx("unable to open %s file", CONTENTS_FNAME);
-	return FALSE;
-    }
-    read_plist(&plist, fd);
-    (void) close(fd);
+	if (rc != 0)
+		return FALSE;
 
-    Install = isfile(INSTALL_FNAME) ? (char *)INSTALL_FNAME : NULL;
-    PostInstall = isfile(POST_INSTALL_FNAME) ?
-	(char *)POST_INSTALL_FNAME : NULL;
-    DeInstall = isfile(DEINSTALL_FNAME) ? (char *)DEINSTALL_FNAME : NULL;
-    PostDeInstall = isfile(POST_DEINSTALL_FNAME) ?
-	(char *)POST_DEINSTALL_FNAME : NULL;
-    Require = isfile(REQUIRE_FNAME) ? (char *)REQUIRE_FNAME : NULL;
-    Display = isfile(DISPLAY_FNAME) ? (char *)DISPLAY_FNAME : NULL;
-    Mtree = isfile(MTREE_FNAME) ?  (char *)MTREE_FNAME : NULL;
+	Install = isfile(INSTALL_FNAME) ? (char *)INSTALL_FNAME : NULL;
+	PostInstall = isfile(POST_INSTALL_FNAME) ?
+	    (char *)POST_INSTALL_FNAME : NULL;
+	DeInstall = isfile(DEINSTALL_FNAME) ? (char *)DEINSTALL_FNAME : NULL;
+	PostDeInstall = isfile(POST_DEINSTALL_FNAME) ?
+	    (char *)POST_DEINSTALL_FNAME : NULL;
+	Require = isfile(REQUIRE_FNAME) ? (char *)REQUIRE_FNAME : NULL;
+	Display = isfile(DISPLAY_FNAME) ? (char *)DISPLAY_FNAME : NULL;
+	Mtree = isfile(MTREE_FNAME) ?  (char *)MTREE_FNAME : NULL;
 
-    make_dist(homedir, pkg, suf, &plist);
+	make_dist(homedir, pkg, suf, &plist);
 
-    free_plist(&plist);
-    if (chdir(homedir) == FAIL) {
-	warnx("can't change directory to '%s'!", homedir);
-	return FALSE;
-    }
-    return TRUE;
+	free_plist(&plist);
+	if (chdir(homedir) == FAIL) {
+		warnx("can't change directory to '%s'!", homedir);
+		return FALSE;
+	}
+	return TRUE;
 }

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/create/pl.c#3 (text+ko) ====

@@ -21,11 +21,15 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD: src/usr.sbin/pkg_install/create/pl.c,v 1.29 2010/04/23 11:07:43 flz Exp $");
 
-#include <pkg.h>
-#include "create.h"
+#include <sys/types.h>
 #include <errno.h>
 #include <err.h>
 #include <md5.h>
+#include <pkg.h>
+
+#include "create.h"
+
+extern void	cleanup(int);
 
 /* Add an MD5 checksum entry for a file or link */
 void

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/delete/perform.c#5 (text+ko) ====

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



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