chiark / gitweb /
systemctl: handle correctly template units for edit verb
[elogind.git] / src / shared / util.c
index 273552f62222ea579244ae1244e08b1a62d993f3..ee95a4b6f71c67bb7ff3f5c3e8ce736bfaa98113 100644 (file)
@@ -2268,21 +2268,25 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
                 ssize_t k;
 
                 k = read(fd, p, nbytes);
-                if (k < 0 && errno == EINTR)
-                        continue;
+                if (k < 0) {
+                        if (errno == EINTR)
+                                continue;
 
-                if (k < 0 && errno == EAGAIN && do_poll) {
+                        if (errno == EAGAIN && do_poll) {
 
-                        /* We knowingly ignore any return value here,
-                         * and expect that any error/EOF is reported
-                         * via read() */
+                                /* We knowingly ignore any return value here,
+                                 * and expect that any error/EOF is reported
+                                 * via read() */
 
-                        fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
-                        continue;
+                                fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
+                                continue;
+                        }
+
+                        return n > 0 ? n : -errno;
                 }
 
-                if (k <= 0)
-                        return n > 0 ? n : (k < 0 ? -errno : 0);
+                if (k == 0)
+                        return n;
 
                 p += k;
                 nbytes -= k;
@@ -2294,7 +2298,6 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
 
 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
         const uint8_t *p = buf;
-        ssize_t n = 0;
 
         assert(fd >= 0);
         assert(buf);
@@ -2305,26 +2308,27 @@ int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
                 ssize_t k;
 
                 k = write(fd, p, nbytes);
-                if (k < 0 && errno == EINTR)
-                        continue;
+                if (k < 0) {
+                        if (errno == EINTR)
+                                continue;
 
-                if (k < 0 && errno == EAGAIN && do_poll) {
+                        if (errno == EAGAIN && do_poll) {
+                                /* We knowingly ignore any return value here,
+                                 * and expect that any error/EOF is reported
+                                 * via write() */
 
-                        /* We knowingly ignore any return value here,
-                         * and expect that any error/EOF is reported
-                         * via write() */
+                                fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
+                                continue;
+                        }
 
-                        fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
-                        continue;
+                        return -errno;
                 }
 
-                if (k <= 0)
-                        /* We were not done yet, and a write error occured. */
-                        return errno ? -errno : -EIO;
+                if (k == 0) /* Can't really happen */
+                        return -EIO;
 
                 p += k;
                 nbytes -= k;
-                n += k;
         }
 
         return 0;
@@ -6973,6 +6977,14 @@ int tempfn_xxxxxx(const char *p, char **ret) {
         assert(p);
         assert(ret);
 
+        /*
+         * Turns this:
+         *         /foo/bar/waldo
+         *
+         * Into this:
+         *         /foo/bar/.waldoXXXXXX
+         */
+
         fn = basename(p);
         if (!filename_is_valid(fn))
                 return -EINVAL;
@@ -6983,7 +6995,7 @@ int tempfn_xxxxxx(const char *p, char **ret) {
 
         strcpy(stpcpy(stpcpy(mempcpy(t, p, fn - p), "."), fn), "XXXXXX");
 
-        *ret = t;
+        *ret = path_kill_slashes(t);
         return 0;
 }
 
@@ -6996,6 +7008,14 @@ int tempfn_random(const char *p, char **ret) {
         assert(p);
         assert(ret);
 
+        /*
+         * Turns this:
+         *         /foo/bar/waldo
+         *
+         * Into this:
+         *         /foo/bar/.waldobaa2a261115984a9
+         */
+
         fn = basename(p);
         if (!filename_is_valid(fn))
                 return -EINVAL;
@@ -7014,7 +7034,39 @@ int tempfn_random(const char *p, char **ret) {
 
         *x = 0;
 
-        *ret = t;
+        *ret = path_kill_slashes(t);
+        return 0;
+}
+
+int tempfn_random_child(const char *p, char **ret) {
+        char *t, *x;
+        uint64_t u;
+        unsigned i;
+
+        assert(p);
+        assert(ret);
+
+        /* Turns this:
+         *         /foo/bar/waldo
+         * Into this:
+         *         /foo/bar/waldo/.3c2b6219aa75d7d0
+         */
+
+        t = new(char, strlen(p) + 2 + 16 + 1);
+        if (!t)
+                return -ENOMEM;
+
+        x = stpcpy(stpcpy(t, p), "/.");
+
+        u = random_u64();
+        for (i = 0; i < 16; i++) {
+                *(x++) = hexchar(u & 0xF);
+                u >>= 4;
+        }
+
+        *x = 0;
+
+        *ret = path_kill_slashes(t);
         return 0;
 }