]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Move function to cat file & dropins into basic/
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 26 Apr 2018 11:03:39 +0000 (13:03 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 26 Apr 2018 11:52:46 +0000 (13:52 +0200)
This fixes a buglet where the second and later drop-in would not be seperated
properly by a newline.

src/basic/terminal-util.c
src/basic/terminal-util.h
src/systemctl/systemctl.c
src/test/test-terminal-util.c

index 2bf0f4f86d8c60a04b53124823eb600caa3a405c..5624c03bced650d2364e832add043ebf2a0feaa9 100644 (file)
@@ -28,6 +28,7 @@
 #include <unistd.h>
 
 #include "alloc-util.h"
+#include "copy.h"
 #include "env-util.h"
 #include "fd-util.h"
 #include "fileio.h"
@@ -1362,3 +1363,39 @@ int terminal_urlify_path(const char *path, const char *text, char **ret) {
 
         return terminal_urlify(url, text, ret);
 }
+
+static int cat_file(const char *filename, bool newline) {
+        _cleanup_close_ int fd;
+
+        fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY);
+        if (fd < 0)
+                return -errno;
+
+        printf("%s%s# %s%s\n",
+               newline ? "\n" : "",
+               ansi_highlight_blue(),
+               filename,
+               ansi_normal());
+        fflush(stdout);
+
+        return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, 0);
+}
+
+int cat_files(const char *file, char **dropins) {
+        char **path;
+        int r;
+
+        if (file) {
+                r = cat_file(file, false);
+                if (r < 0)
+                        return log_warning_errno(r, "Failed to cat %s: %m", file);
+        }
+
+        STRV_FOREACH(path, dropins) {
+                r = cat_file(*path, file || path != dropins);
+                if (r < 0)
+                        return log_warning_errno(r, "Failed to cat %s: %m", *path);
+        }
+
+        return 0;
+}
index ad6ee338edff17886370288fb9495284e27e693e..d2d3ad5127ef4b81be4ccc7a28ce78125006a8e1 100644 (file)
@@ -159,3 +159,5 @@ int vt_reset_keyboard(int fd);
 
 int terminal_urlify(const char *url, const char *text, char **ret);
 int terminal_urlify_path(const char *path, const char *text, char **ret);
+
+int cat_files(const char *file, char **files);
index 69a6d940ef29e2fcd68ae0471fa3445d8868d121..23ce6acd6a0132155e3b033386138eff575efe6b 100644 (file)
@@ -5320,23 +5320,6 @@ static int show(int argc, char *argv[], void *userdata) {
         return ret;
 }
 
-static int cat_file(const char *filename, bool newline) {
-        _cleanup_close_ int fd;
-
-        fd = open(filename, O_RDONLY|O_CLOEXEC|O_NOCTTY);
-        if (fd < 0)
-                return -errno;
-
-        printf("%s%s# %s%s\n",
-               newline ? "\n" : "",
-               ansi_highlight_blue(),
-               filename,
-               ansi_normal());
-        fflush(stdout);
-
-        return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, 0);
-}
-
 static int cat(int argc, char *argv[], void *userdata) {
         _cleanup_(lookup_paths_free) LookupPaths lp = {};
         _cleanup_strv_free_ char **names = NULL;
@@ -5367,7 +5350,6 @@ static int cat(int argc, char *argv[], void *userdata) {
         STRV_FOREACH(name, names) {
                 _cleanup_free_ char *fragment_path = NULL;
                 _cleanup_strv_free_ char **dropin_paths = NULL;
-                char **path;
 
                 r = unit_find_paths(bus, *name, &lp, &fragment_path, &dropin_paths);
                 if (r < 0)
@@ -5394,17 +5376,9 @@ static int cat(int argc, char *argv[], void *userdata) {
                                 arg_scope == UNIT_FILE_SYSTEM ? "" : " --user",
                                 ansi_normal());
 
-                if (fragment_path) {
-                        r = cat_file(fragment_path, false);
-                        if (r < 0)
-                                return log_warning_errno(r, "Failed to cat %s: %m", fragment_path);
-                }
-
-                STRV_FOREACH(path, dropin_paths) {
-                        r = cat_file(*path, path == dropin_paths);
-                        if (r < 0)
-                                return log_warning_errno(r, "Failed to cat %s: %m", *path);
-                }
+                r = cat_files(fragment_path, dropin_paths);
+                if (r < 0)
+                        return r;
         }
 
         return 0;
index c83dfa9dcd54e637dc45d8ebb781fd0849b3a6ca..92add77531ac39f6f14c8d040608a67500d812ad 100644 (file)
@@ -14,6 +14,7 @@
 #include "fileio.h"
 #include "log.h"
 #include "macro.h"
+#include "strv.h"
 #include "terminal-util.h"
 #include "util.h"
 
@@ -76,6 +77,13 @@ static void test_terminal_urlify(void) {
         printf("Or click on %s to have a look at it!\n", formatted);
 }
 
+static void test_cat_files(void) {
+        assert_se(cat_files("/no/such/file", NULL) == -ENOENT);
+
+        if (access("/etc/fstab", R_OK) >= 0)
+                assert_se(cat_files("/etc/fstab", STRV_MAKE("/etc/fstab", "/etc/fstab")) == 0);
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -83,6 +91,7 @@ int main(int argc, char *argv[]) {
         test_default_term_for_tty();
         test_read_one_char();
         test_terminal_urlify();
+        test_cat_files();
 
         return 0;
 }