commit:     e52886411dab194ed0951f7282f967c8d4626bcd
Author:     Mike Gilbert <floppym <AT> gentoo <DOT> org>
AuthorDate: Sun Mar  1 21:47:36 2026 +0000
Commit:     Mike Gilbert <floppym <AT> gentoo <DOT> org>
CommitDate: Sun Mar  1 21:47:36 2026 +0000
URL:        https://gitweb.gentoo.org/proj/install-xattr.git/commit/?id=e5288641

Exit early if the real install fails

Signed-off-by: Mike Gilbert <floppym <AT> gentoo.org>

 install-xattr.c | 66 ++++++++++++++++++++++++++++-----------------------------
 1 file changed, 32 insertions(+), 34 deletions(-)

diff --git a/install-xattr.c b/install-xattr.c
index 75fa70f..a9d95d1 100644
--- a/install-xattr.c
+++ b/install-xattr.c
@@ -47,7 +47,7 @@ xmalloc(size_t size)
 {
        void *ret = malloc(size);
        if (ret == NULL)
-               err(1, "malloc() failed");
+               err(EXIT_FAILURE, "malloc() failed");
        return ret;
 }
 
@@ -56,7 +56,7 @@ xrealloc(void *p, size_t size)
 {
        void *ret = realloc(p, size);
        if (ret == NULL)
-               err(1, "realloc() failed");
+               err(EXIT_FAILURE, "realloc() failed");
        return ret;
 }
 
@@ -80,7 +80,7 @@ xlistxattr(const char *path, char *list, size_t size)
 {
        ssize_t ret = listxattr(path, list, size);
        if (ret < 0)
-               err(1, "listxattr() failed on %s", path);
+               err(EXIT_FAILURE, "listxattr() failed on %s", path);
        return ret;
 }
 
@@ -89,7 +89,7 @@ xgetxattr(const char *path, char *list, char *value, size_t 
size)
 {
        ssize_t ret = getxattr(path, list, value, size);
        if (ret < 0)
-               err(1, "getxattr() failed on %s", path);
+               err(EXIT_FAILURE, "getxattr() failed on %s", path);
        return ret;
 }
 
@@ -98,7 +98,7 @@ xsetxattr(const char *path, char *list, char *value, size_t 
size)
 {
        ssize_t ret = setxattr(path, list, value, size, 0);
        if (ret < 0)
-               err(1, "setxattr() failed setting %s=%s on %s", list, value, 
path);
+               err(EXIT_FAILURE, "setxattr() failed setting %s=%s on %s", 
list, value, path);
        return ret;
 }
 
@@ -119,7 +119,7 @@ copyxattr(const char *source, const char *target)
                warn("listxattr() failed on %s", source);
                if (errno == ENOTSUP)
                        return;
-               exit(1);
+               exit(EXIT_FAILURE);
        }
 
        /* There's no xattrs at all. */
@@ -172,7 +172,7 @@ main(int argc, char* argv[])
         * and expects us to switch back. */
        char *portage_helper_cwd = getenv("__PORTAGE_HELPER_CWD");
        if (portage_helper_cwd && chdir(portage_helper_cwd))
-               err(1, "failed to chdir %s", portage_helper_cwd);
+               err(EXIT_FAILURE, "failed to chdir %s", portage_helper_cwd);
 
        argv[0] = getenv("REAL_INSTALL") ?: REAL_INSTALL;
 
@@ -180,6 +180,23 @@ main(int argc, char* argv[])
        if (r)
                errx(r == ENOENT ? 127 : 126, "failed to spawn %s: %s", 
argv[0], strerror(r));
 
+       int wstatus;
+       if (wait(&wstatus) < 0)
+               err(EXIT_FAILURE, "failed to wait for child process");
+
+       if (WIFSIGNALED(wstatus)) {
+               /* Do the right thing and pass the signal back up.  See:
+                * http://www.cons.org/cracauer/sigint.html */
+               int signum = WTERMSIG(wstatus);
+               kill(getpid(), signum);
+               return 128 + signum;
+       }
+       else {
+               int estatus = WEXITSTATUS(wstatus);
+               if (estatus != EXIT_SUCCESS)
+                       return estatus;
+       }
+
        char *portage_xattr_exclude = getenv("PORTAGE_XATTR_EXCLUDE");
        if (portage_xattr_exclude) {
                exclude = portage_xattr_exclude;
@@ -245,13 +262,10 @@ main(int argc, char* argv[])
                                break;
 
                        default:
-                               err(1, "getopt_long() failed");
+                               err(EXIT_FAILURE, "getopt_long() failed");
                }
        }
 
-       int status;
-       wait(&status);
-
        /* Are there enough files/directories on the cmd line to
         * proceed?  This can happen if install is called with no
         * arguments or with just --help.  In which case there is
@@ -260,19 +274,17 @@ main(int argc, char* argv[])
        int first = optind;
        int last = argc - 1;
        if (first >= last)
-               goto done;
+               return EXIT_SUCCESS;
 
        /* If all the targets are directories, do nothing. */
        if (opts_directory)
-               goto done;
+               return EXIT_SUCCESS;
 
        if (!opts_target_directory) {
                struct stat s;
                target = argv[last];
-               if (stat(target, &s) != 0) {
-                       err(1, "failed to stat %s", target);
-                       return EXIT_FAILURE;
-               }
+               if (stat(target, &s) != 0)
+                       err(EXIT_FAILURE, "failed to stat %s", target);
                target_is_directory = S_ISDIR(s.st_mode);
        } else {
                /* target was set above with the -t option */
@@ -288,10 +300,8 @@ main(int argc, char* argv[])
 
                for (int i = first; i < last; i++) {
                        struct stat s;
-                       if (stat(argv[i], &s) != 0) {
-                               err(1, "failed to stat %s", argv[i]);
-                               return EXIT_FAILURE;
-                       }
+                       if (stat(argv[i], &s) != 0)
+                               err(EXIT_FAILURE, "failed to stat %s", argv[i]);
                        /* We reproduce install's behavior and skip
                         * all extra directories on the command line
                         * that are not the final target directory.
@@ -306,17 +316,5 @@ main(int argc, char* argv[])
        } else
                copyxattr(argv[first], target);
 
-
- done:
-       /* Do the right thing and pass the signal back up.  See:
-        * http://www.cons.org/cracauer/sigint.html
-        */
-       if (WIFSIGNALED(status)) {
-               int signum = WTERMSIG(status);
-               kill(getpid(), signum);
-               return 128 + signum;
-       } else if (WIFEXITED(status))
-               return WEXITSTATUS(status);
-       else
-               return EXIT_FAILURE; /* We should never get here. */
+       return EXIT_SUCCESS;
 }

Reply via email to