copy: also copy AF_UNIX sockets
authorLennart Poettering <lennart@poettering.net>
Fri, 29 Apr 2016 12:21:22 +0000 (14:21 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 29 Apr 2016 12:28:03 +0000 (14:28 +0200)
We previously would fail with EOPNOTSUPP when encountering an AF_UNIX socket in
the directory tree to copy. Fix that, and copy them too (even if they are dead
in the result).

Fixes: #2914

src/basic/copy.c
src/test/test-copy.c

index 03487a6..3001234 100644 (file)
@@ -364,7 +364,7 @@ static int fd_copy_directory(
                         q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name);
                 else if (S_ISFIFO(buf.st_mode))
                         q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name);
-                else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode))
+                else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
                         q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name);
                 else
                         q = -EOPNOTSUPP;
@@ -396,7 +396,7 @@ int copy_tree_at(int fdf, const char *from, int fdt, const char *to, bool merge)
                 return fd_copy_symlink(fdf, from, &st, fdt, to);
         else if (S_ISFIFO(st.st_mode))
                 return fd_copy_fifo(fdf, from, &st, fdt, to);
-        else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode))
+        else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
                 return fd_copy_node(fdf, from, &st, fdt, to);
         else
                 return -EOPNOTSUPP;
index cb43775..d1bf376 100644 (file)
@@ -95,6 +95,8 @@ static void test_copy_tree(void) {
         char **links = STRV_MAKE("link", "file",
                                  "link2", "dir1/file");
         char **p, **link;
+        const char *unixsockp;
+        struct stat st;
 
         log_info("%s", __func__);
 
@@ -116,6 +118,9 @@ static void test_copy_tree(void) {
                 assert_se(symlink(f, l) == 0);
         }
 
+        unixsockp = strjoina(original_dir, "unixsock");
+        assert_se(mknod(unixsockp, S_IFSOCK|0644, 0) >= 0);
+
         assert_se(copy_tree(original_dir, copy_dir, true) == 0);
 
         STRV_FOREACH(p, files) {
@@ -137,6 +142,10 @@ static void test_copy_tree(void) {
                 assert_se(path_equal(f, target));
         }
 
+        unixsockp = strjoina(copy_dir, "unixsock");
+        assert_se(stat(unixsockp, &st) >= 0);
+        assert_se(S_ISSOCK(st.st_mode));
+
         assert_se(copy_tree(original_dir, copy_dir, false) < 0);
         assert_se(copy_tree("/tmp/inexistent/foo/bar/fsdoi", copy_dir, false) < 0);