chiark / gitweb /
tmpfiles: copy/link /usr/share/factory/ files when the source argument is omitted
[elogind.git] / src / tmpfiles / tmpfiles.c
index 25c2a8c413439ee3c1a4a9f17c2d401a81bc5ebe..68cfa55ce9cee09e96985c76ac2390cfbe5140f7 100644 (file)
@@ -667,10 +667,29 @@ static int create_item(Item *i) {
                 break;
 
         case COPY_FILES:
-                r = copy_tree(i->argument, i->path);
+                r = copy_tree(i->argument, i->path, false);
                 if (r < 0) {
-                        log_error("Failed to copy files: %s", strerror(-r));
-                        return r;
+                        struct stat a, b;
+
+                        if (r != -EEXIST) {
+                                log_error("Failed to copy files to %s: %s", i->path, strerror(-r));
+                                return -r;
+                        }
+
+                        if (stat(i->argument, &a) < 0) {
+                                log_error("stat(%s) failed: %m", i->argument);
+                                return -errno;
+                        }
+
+                        if (stat(i->path, &b) < 0) {
+                                log_error("stat(%s) failed: %m", i->path);
+                                return -errno;
+                        }
+
+                        if ((a.st_mode ^ b.st_mode) & S_IFMT) {
+                                log_debug("Can't copy to %s, file exists already and is of different type", i->path);
+                                return 0;
+                        }
                 }
 
                 r = item_set_perms(i, i->path);
@@ -691,22 +710,24 @@ static int create_item(Item *i) {
 
                 RUN_WITH_UMASK(0000) {
                         mkdir_parents_label(i->path, 0755);
-                        r = mkdir(i->path, i->mode);
+                        r = mkdir_label(i->path, i->mode);
                 }
 
-                if (r < 0 && errno != EEXIST) {
-                        log_error("Failed to create directory %s: %m", i->path);
-                        return -errno;
-                }
+                if (r < 0) {
+                        if (r != -EEXIST) {
+                                log_error("Failed to create directory %s: %s", i->path, strerror(-r));
+                                return r;
+                        }
 
-                if (stat(i->path, &st) < 0) {
-                        log_error("stat(%s) failed: %m", i->path);
-                        return -errno;
-                }
+                        if (stat(i->path, &st) < 0) {
+                                log_error("stat(%s) failed: %m", i->path);
+                                return -errno;
+                        }
 
-                if (!S_ISDIR(st.st_mode)) {
-                        log_error("%s is not a directory.", i->path);
-                        return -EEXIST;
+                        if (!S_ISDIR(st.st_mode)) {
+                                log_debug("%s already exists and is not a directory.", i->path);
+                                return 0;
+                        }
                 }
 
                 r = item_set_perms(i, i->path);
@@ -1228,10 +1249,10 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
 
         case CREATE_SYMLINK:
                 if (!i->argument) {
-                        log_error("[%s:%u] Symlink file requires argument.", fname, line);
-                        return -EBADMSG;
+                        i->argument = strappend("/usr/share/factory", i->path);
+                        if (!i->argument)
+                                return log_oom();
                 }
-
                 break;
 
         case WRITE_FILE:
@@ -1243,8 +1264,9 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
 
         case COPY_FILES:
                 if (!i->argument) {
-                        log_error("[%s:%u] Copy files requires argument.", fname, line);
-                        return -EBADMSG;
+                        i->argument = strappend("/usr/share/factory", i->path);
+                        if (!i->argument)
+                                return log_oom();
                 }
 
                 if (!path_is_absolute(i->argument)) {