Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/xinstall - improve error printing



details:   https://anonhg.NetBSD.org/src/rev/8a2e45d7f5a0
branches:  trunk
changeset: 809039:8a2e45d7f5a0
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Jun 15 16:33:38 2015 +0000

description:
- improve error printing
- deduplicate run functions and don't use the shell so that we handle
  filenames with spaces and metacharacters consistently.

diffstat:

 usr.bin/xinstall/Makefile   |    5 +-
 usr.bin/xinstall/xinstall.c |  294 +++++++++++++++++--------------------------
 2 files changed, 118 insertions(+), 181 deletions(-)

diffs (truncated from 604 to 300 lines):

diff -r 4989ab00db15 -r 8a2e45d7f5a0 usr.bin/xinstall/Makefile
--- a/usr.bin/xinstall/Makefile Mon Jun 15 15:38:52 2015 +0000
+++ b/usr.bin/xinstall/Makefile Mon Jun 15 16:33:38 2015 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.23 2015/06/15 07:05:09 martin Exp $
+#      $NetBSD: Makefile,v 1.24 2015/06/15 16:33:38 christos Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/6/93
 
 .include <bsd.own.mk>
@@ -8,7 +8,8 @@
 MAN=   install.1
 
 .PATH:         ${NETBSDSRCDIR}/usr.sbin/mtree
-CPPFLAGS+=     -I${NETBSDSRCDIR}/usr.sbin/mtree -DHAVE_POSIX_SPAWN
+CPPFLAGS+=     -I${NETBSDSRCDIR}/usr.sbin/mtree
+CPPFLAGS+=     -DHAVE_POSIX_SPAWN
 
 .if (${HOSTPROG:U} == "")
 DPADD+= ${LIBUTIL}
diff -r 4989ab00db15 -r 8a2e45d7f5a0 usr.bin/xinstall/xinstall.c
--- a/usr.bin/xinstall/xinstall.c       Mon Jun 15 15:38:52 2015 +0000
+++ b/usr.bin/xinstall/xinstall.c       Mon Jun 15 16:33:38 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: xinstall.c,v 1.118 2015/06/15 07:05:09 martin Exp $    */
+/*     $NetBSD: xinstall.c,v 1.119 2015/06/15 16:33:38 christos Exp $  */
 
 /*
  * Copyright (c) 1987, 1993
@@ -46,7 +46,7 @@
 #if 0
 static char sccsid[] = "@(#)xinstall.c 8.1 (Berkeley) 7/21/93";
 #else
-__RCSID("$NetBSD: xinstall.c,v 1.118 2015/06/15 07:05:09 martin Exp $");
+__RCSID("$NetBSD: xinstall.c,v 1.119 2015/06/15 16:33:38 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -84,7 +84,6 @@
 #include "pathnames.h"
 #include "mtree.h"
 
-#define STRIP_ARGS_MAX 32
 #define BACKUP_SUFFIX ".old"
 
 static int     dobackup, dodir, dostrip, dolink, dopreserve, dorename, dounpriv;
@@ -137,7 +136,8 @@
 static void    metadata_log(const char *, const char *, struct timeval *,
            const char *, const char *, off_t);
 static int     parseid(char *, id_t *);
-static void    strip(char *);
+static void    run(const char *, const char *, const char *, int);
+static void    strip(const char *);
 __dead static void     usage(void);
 static char   *xbasename(char *);
 static char   *xdirname(char *);
@@ -160,7 +160,8 @@
                case 'a':
                        afterinstallcmd = strdup(optarg);
                        if (afterinstallcmd == NULL)
-                               errx(1, "%s", strerror(ENOMEM));
+                               err(EXIT_FAILURE,
+                                   "Can't allocate after command");
                        break;
                case 'B':
                        suffix = optarg;
@@ -229,14 +230,14 @@
                                        dolink |= LN_RELATIVE;
                                        break;
                                default:
-                                       errx(1, "%c: invalid link type", *p);
+                                       errx(EXIT_FAILURE, "%c: invalid link type", *p);
                                        /* NOTREACHED */
                                }
                        break;
                case 'm':
                        haveopt_m = 1;
                        if (!(set = setmode(optarg)))
-                               err(1, "Cannot set file mode `%s'", optarg);
+                               err(EXIT_FAILURE, "Cannot set file mode `%s'", optarg);
                        mode = getmode(set, 0);
                        free(set);
                        break;
@@ -245,7 +246,7 @@
                        break;
                case 'N':
                        if (! setup_getid(optarg))
-                               errx(1,
+                               errx(EXIT_FAILURE,
                            "Unable to use user and group databases in `%s'",
                                    optarg);
                        break;
@@ -262,7 +263,7 @@
                case 'S':
                        stripArgs = strdup(optarg);
                        if (stripArgs == NULL)
-                               errx(1, "%s", strerror(ENOMEM));
+                               err(EXIT_FAILURE, "Can't allocate options");
                        /* fall through; -S implies -s */
                        /*FALLTHROUGH*/
                case 's':
@@ -320,7 +321,7 @@
                if (gid_from_group(group, &gid) == -1) {
                        id_t id;
                        if (!parseid(group, &id))
-                               errx(1, "unknown group %s", group);
+                               errx(EXIT_FAILURE, "unknown group %s", group);
                        gid = id;
                }
                iflags |= HASGID;
@@ -329,7 +330,7 @@
                if (uid_from_user(owner, &uid) == -1) {
                        id_t id;
                        if (!parseid(owner, &id))
-                               errx(1, "unknown user %s", owner);
+                               errx(EXIT_FAILURE, "unknown user %s", owner);
                        uid = id;
                }
                iflags |= HASUID;
@@ -338,7 +339,7 @@
 #if ! HAVE_NBTOOL_CONFIG_H
        if (fflags && !dounpriv) {
                if (string_to_flags(&fflags, &fileflags, NULL))
-                       errx(1, "%s: invalid flag", fflags);
+                       errx(EXIT_FAILURE, "%s: invalid flag", fflags);
                /* restore fflags since string_to_flags() changed it */
                fflags = flags_to_string(fileflags, "-");
                iflags |= SETFLAGS;
@@ -375,12 +376,12 @@
                /* makelink() handles checks for links */
                if (!dolink) {
                        if (stat(*argv, &from_sb))
-                               err(1, "%s: stat", *argv);
+                               err(EXIT_FAILURE, "%s: stat", *argv);
                        if (!S_ISREG(to_sb.st_mode))
-                               errx(1, "%s: not a regular file", to_name);
+                               errx(EXIT_FAILURE, "%s: not a regular file", to_name);
                        if (to_sb.st_dev == from_sb.st_dev &&
                            to_sb.st_ino == from_sb.st_ino)
-                               errx(1, "%s and %s are the same file", *argv,
+                               errx(EXIT_FAILURE, "%s and %s are the same file", *argv,
                                    to_name);
                }
                /*
@@ -434,7 +435,7 @@
                (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
                /* This usage is safe. */
                if (mktemp(tmpl) == NULL)
-                       err(1, "%s: mktemp", tmpl);
+                       err(EXIT_FAILURE, "%s: mktemp", tmpl);
                ret = link(from_name, tmpl);
                if (ret == 0) {
                        ret = rename(tmpl, to_name);
@@ -463,18 +464,18 @@
                (void)snprintf(tmpl, sizeof(tmpl), "%s.inst.XXXXXX", to_name);
                /* This usage is safe. */
                if (mktemp(tmpl) == NULL)
-                       err(1, "%s: mktemp", tmpl);
+                       err(EXIT_FAILURE, "%s: mktemp", tmpl);
 
                if (symlink(from_name, tmpl) == -1)
-                       err(1, "symlink %s -> %s", from_name, tmpl);
+                       err(EXIT_FAILURE, "symlink %s -> %s", from_name, tmpl);
                if (rename(tmpl, to_name) == -1) {
                        /* remove temporary link before exiting */
                        (void)unlink(tmpl);
-                       err(1, "%s: rename", to_name);
+                       err(EXIT_FAILURE, "%s: rename", to_name);
                }
        } else {
                if (symlink(from_name, to_name) == -1)
-                       err(1, "symlink %s -> %s", from_name, to_name);
+                       err(EXIT_FAILURE, "symlink %s -> %s", from_name, to_name);
        }
 }
 
@@ -492,10 +493,10 @@
        if (dolink & (LN_HARD|LN_MIXED)) {
                if (do_link(from_name, to_name) == -1) {
                        if ((dolink & LN_HARD) || errno != EXDEV)
-                               err(1, "link %s -> %s", from_name, to_name);
+                               err(EXIT_FAILURE, "link %s -> %s", from_name, to_name);
                } else {
                        if (stat(to_name, &to_sb))
-                               err(1, "%s: stat", to_name);
+                               err(EXIT_FAILURE, "%s: stat", to_name);
                        if (S_ISREG(to_sb.st_mode)) {
                                        /* XXX: hard links to anything
                                         * other than plain files are not
@@ -558,7 +559,7 @@
        if (dolink & LN_ABSOLUTE) {
                /* Convert source path to absolute */
                if (realpath(from_name, src) == NULL)
-                       err(1, "%s: realpath", from_name);
+                       err(EXIT_FAILURE, "%s: realpath", from_name);
                do_symlink(src, to_name);
                        /* XXX: src may point outside of destdir */
                metadata_log(to_name, "link", NULL, src, NULL, 0);
@@ -570,7 +571,7 @@
 
                /* Resolve pathnames */
                if (realpath(from_name, src) == NULL)
-                       err(1, "%s: realpath", from_name);
+                       err(EXIT_FAILURE, "%s: realpath", from_name);
 
                /*
                 * The last component of to_name may be a symlink,
@@ -578,15 +579,15 @@
                 */
                cp = xdirname(to_name);
                if (realpath(cp, dst) == NULL)
-                       err(1, "%s: realpath", cp);
+                       err(EXIT_FAILURE, "%s: realpath", cp);
                /* .. and add the last component */
                if (strcmp(dst, "/") != 0) {
                        if (strlcat(dst, "/", sizeof(dst)) > sizeof(dst))
-                               errx(1, "resolved pathname too long");
+                               errx(EXIT_FAILURE, "resolved pathname too long");
                }
                cp = xbasename(to_name);
                if (strlcat(dst, cp, sizeof(dst)) > sizeof(dst))
-                       errx(1, "resolved pathname too long");
+                       errx(EXIT_FAILURE, "resolved pathname too long");
 
                /* trim common path components */
                for (s = src, d = dst; *s == *d; s++, d++)
@@ -634,7 +635,7 @@
        if (!dolink) {
                        /* ensure that from_sb & tv are sane if !dolink */
                if (stat(from_name, &from_sb))
-                       err(1, "%s: stat", from_name);
+                       err(EXIT_FAILURE, "%s: stat", from_name);
                size = from_sb.st_size;
 #if BSD4_4 && !HAVE_NBTOOL_CONFIG_H
                TIMESPEC_TO_TIMEVAL(&tv[0], &from_sb.st_atimespec);
@@ -651,7 +652,7 @@
                devnull = 0;
                if (!dolink) {
                        if (!S_ISREG(from_sb.st_mode))
-                               errx(1, "%s: not a regular file", from_name);
+                               errx(EXIT_FAILURE, "%s: not a regular file", from_name);
                }
                /* Build the target path. */
                if (flags & DIRECTORY) {
@@ -698,17 +699,17 @@
        /* Create target. */
        if (dorename) {
                if ((to_fd = mkstemp(to_name)) == -1)
-                       err(1, "%s: mkstemp", to_name);
+                       err(EXIT_FAILURE, "%s: mkstemp", to_name);
        } else {
                if ((to_fd = open(to_name,
                    O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) < 0)
-                       err(1, "%s: open", to_name);
+                       err(EXIT_FAILURE, "%s: open", to_name);
        }
        digestresult = NULL;
        if (!devnull) {
                if ((from_fd = open(from_name, O_RDONLY, 0)) < 0) {
                        (void)unlink(to_name);
-                       err(1, "%s: open", from_name);
+                       err(EXIT_FAILURE, "%s: open", from_name);
                }
                digestresult =
                    copy(from_fd, from_name, to_fd, to_name, from_sb.st_size);
@@ -724,13 +725,13 @@
                 */
                close(to_fd);
                if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
-                       err(1, "stripping %s", to_name);
+                       err(EXIT_FAILURE, "stripping %s", to_name);
 
                /*
                 * Recalculate size and digestresult after stripping.
                 */
                if (fstat(to_fd, &to_sb) != 0)
-                       err(1, "%s: fstat", to_name);
+                       err(EXIT_FAILURE, "%s: fstat", to_name);
                size = to_sb.st_size;
                digestresult =
                    copy(to_fd, to_name, -1, NULL, size);
@@ -746,7 +747,7 @@
                 */
                close(to_fd);
                if ((to_fd = open(to_name, O_RDONLY, S_IRUSR | S_IWUSR)) < 0)
-                       err(1, "running after install command on %s", to_name);
+                       err(EXIT_FAILURE, "running after install command on %s", to_name);
        }
 
        /*
@@ -757,7 +758,7 @@
            (flags & (HASUID | HASGID)) && fchown(to_fd, uid, gid) == -1) {
                serrno = errno;
                (void)unlink(to_name);
-               errx(1, "%s: chown/chgrp: %s", to_name, strerror(serrno));
+               errc(EXIT_FAILURE, serrno, "%s: chown/chgrp", to_name);
        }
        tmpmode = mode;
        if (dounpriv)
@@ -765,7 +766,7 @@
        if (fchmod(to_fd, tmpmode) == -1) {



Home | Main Index | Thread Index | Old Index