chiark / gitweb /
unit: print the color status marks on the left
[elogind.git] / src / shared / util.c
index 5f06c4b7d8b9c408ce519514a15f8d439958f489..d6af92745341a4f34a1dabe870ba4022070b5ee2 100644 (file)
@@ -3139,7 +3139,8 @@ static int rm_rf_children(int fd, bool only_dirs, bool honour_sticky) {
         /* This returns the first error we run into, but nevertheless
          * tries to go on */
 
         /* This returns the first error we run into, but nevertheless
          * tries to go on */
 
-        if (!(d = fdopendir(fd))) {
+        d = fdopendir(fd);
+        if (!d) {
                 close_nointr_nofail(fd);
 
                 return errno == ENOENT ? 0 : -errno;
                 close_nointr_nofail(fd);
 
                 return errno == ENOENT ? 0 : -errno;
@@ -3150,9 +3151,9 @@ static int rm_rf_children(int fd, bool only_dirs, bool honour_sticky) {
                 bool is_dir, keep_around = false;
                 int r;
 
                 bool is_dir, keep_around = false;
                 int r;
 
-                if ((r = readdir_r(d, &buf, &de)) != 0) {
-                        if (ret == 0)
-                                ret = -r;
+                r = readdir_r(d, &buf, &de);
+                if (r != 0 && ret == 0) {
+                        ret = -r;
                         break;
                 }
 
                         break;
                 }
 
@@ -3199,17 +3200,16 @@ static int rm_rf_children(int fd, bool only_dirs, bool honour_sticky) {
                 if (is_dir) {
                         int subdir_fd;
 
                 if (is_dir) {
                         int subdir_fd;
 
-                        subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
+                        subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
                         if (subdir_fd < 0) {
                                 if (ret == 0 && errno != ENOENT)
                                         ret = -errno;
                                 continue;
                         }
 
                         if (subdir_fd < 0) {
                                 if (ret == 0 && errno != ENOENT)
                                         ret = -errno;
                                 continue;
                         }
 
-                        if ((r = rm_rf_children(subdir_fd, only_dirs, honour_sticky)) < 0) {
-                                if (ret == 0)
-                                        ret = r;
-                        }
+                        r = rm_rf_children(subdir_fd, only_dirs, honour_sticky);
+                        if (r < 0 && ret == 0)
+                                ret = r;
 
                         if (!keep_around)
                                 if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
 
                         if (!keep_around)
                                 if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
@@ -3237,13 +3237,14 @@ int rm_rf(const char *path, bool only_dirs, bool delete_root, bool honour_sticky
 
         assert(path);
 
 
         assert(path);
 
-        if ((fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
+        fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
+        if (fd < 0) {
 
                 if (errno != ENOTDIR)
                         return -errno;
 
                 if (delete_root && !only_dirs)
 
                 if (errno != ENOTDIR)
                         return -errno;
 
                 if (delete_root && !only_dirs)
-                        if (unlink(path) < 0)
+                        if (unlink(path) < 0 && errno != ENOENT)
                                 return -errno;
 
                 return 0;
                                 return -errno;
 
                 return 0;
@@ -3328,15 +3329,15 @@ cpu_set_t* cpu_set_malloc(unsigned *ncpus) {
 }
 
 void status_vprintf(const char *status, bool ellipse, const char *format, va_list ap) {
 }
 
 void status_vprintf(const char *status, bool ellipse, const char *format, va_list ap) {
-        char *s = NULL, *spaces = NULL, *e;
-        int fd = -1, c;
-        size_t emax, sl, left;
+        char *s = NULL;
+        static const char status_indent[] = "         "; /* "[" STATUS "] " */
+        int fd = -1;
         struct iovec iovec[5];
         int n = 0;
 
         assert(format);
 
         struct iovec iovec[5];
         int n = 0;
 
         assert(format);
 
-        /* This independent of logging, as status messages are
+        /* This is independent of logging, as status messages are
          * optional and go exclusively to the console. */
 
         if (vasprintf(&s, format, ap) < 0)
          * optional and go exclusively to the console. */
 
         if (vasprintf(&s, format, ap) < 0)
@@ -3347,15 +3348,19 @@ void status_vprintf(const char *status, bool ellipse, const char *format, va_lis
                 goto finish;
 
         if (ellipse) {
                 goto finish;
 
         if (ellipse) {
+                char *e;
+                size_t emax, sl;
+                int c;
+
                 c = fd_columns(fd);
                 if (c <= 0)
                         c = 80;
 
                 c = fd_columns(fd);
                 if (c <= 0)
                         c = 80;
 
-                if (status) {
-                        sl = 2 + 6 + 1; /* " [" status "]" */
-                        emax = (size_t) c > sl ? c - sl - 1 : 0;
-                } else
-                        emax = c - 1;
+                sl = status ? strlen(status_indent) : 0;
+
+                emax = c - sl - 1;
+                if (emax < 3)
+                        emax = 3;
 
                 e = ellipsize(s, emax, 75);
                 if (e) {
 
                 e = ellipsize(s, emax, 75);
                 if (e) {
@@ -3365,34 +3370,23 @@ void status_vprintf(const char *status, bool ellipse, const char *format, va_lis
         }
 
         zero(iovec);
         }
 
         zero(iovec);
-        IOVEC_SET_STRING(iovec[n++], s);
 
 
-        if (ellipse) {
-                sl = strlen(s);
-                left = emax > sl ? emax - sl : 0;
-                if (left > 0) {
-                        spaces = malloc(left);
-                        if (spaces) {
-                                memset(spaces, ' ', left);
-                                iovec[n].iov_base = spaces;
-                                iovec[n].iov_len = left;
-                                n++;
-                        }
-                }
+        if (status) {
+                if (!isempty(status)) {
+                        IOVEC_SET_STRING(iovec[n++], "[");
+                        IOVEC_SET_STRING(iovec[n++], status);
+                        IOVEC_SET_STRING(iovec[n++], "] ");
+                } else
+                        IOVEC_SET_STRING(iovec[n++], status_indent);
         }
 
         }
 
-        if (status) {
-                IOVEC_SET_STRING(iovec[n++], " [");
-                IOVEC_SET_STRING(iovec[n++], status);
-                IOVEC_SET_STRING(iovec[n++], "]\n");
-        } else
-                IOVEC_SET_STRING(iovec[n++], "\n");
+        IOVEC_SET_STRING(iovec[n++], s);
+        IOVEC_SET_STRING(iovec[n++], "\n");
 
         writev(fd, iovec, n);
 
 finish:
         free(s);
 
         writev(fd, iovec, n);
 
 finish:
         free(s);
-        free(spaces);
 
         if (fd >= 0)
                 close_nointr_nofail(fd);
 
         if (fd >= 0)
                 close_nointr_nofail(fd);