diff options
author | Alexander Wilson <rdtscp@fb.com> | 2022-07-22 04:15:08 -0700 |
---|---|---|
committer | Alexander Wilson <rdtscp@fb.com> | 2022-07-27 03:09:35 -0700 |
commit | d3e2a7f7e6f1167b38ee333a34d610e165252a36 (patch) | |
tree | 2c59abc4d10ee84c67453089e5355434c44b92c3 | |
parent | aa386add6772a41b3fcc2220d1c7f6d2d3b16c0a (diff) | |
download | systemd-d3e2a7f7e6f1167b38ee333a34d610e165252a36.tar.gz |
copy.[ch]: Refactor
- Refactor: Move HardlinkContext to header file
- Refactor: Create `fd_copy_tree_generic` which isolates the functionality to check stat type and appropriately copy.
- Refactor: Create `fd_copy_leaf` which handles copying leaf nodes of a file tree.
-rw-r--r-- | src/shared/copy.c | 98 |
1 files changed, 75 insertions, 23 deletions
diff --git a/src/shared/copy.c b/src/shared/copy.c index 4d52b4c26b..3c5f296d2d 100644 --- a/src/shared/copy.c +++ b/src/shared/copy.c @@ -676,6 +676,23 @@ static int memorize_hardlink( return 1; } +static int fd_copy_tree_generic( + int df, + const char *from, + const struct stat *st, + int dt, + const char *to, + dev_t original_device, + unsigned depth_left, + uid_t override_uid, + gid_t override_gid, + CopyFlags copy_flags, + HardlinkContext *hardlink_context, + const char *display_path, + copy_progress_path_t progress_path, + copy_progress_bytes_t progress_bytes, + void *userdata); + static int fd_copy_regular( int df, const char *from, @@ -992,18 +1009,9 @@ static int fd_copy_directory( if (r > 0) continue; } + } - q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, depth_left-1, override_uid, override_gid, copy_flags, hardlink_context, child_display_path, progress_path, progress_bytes, userdata); - } else if (S_ISREG(buf.st_mode)) - q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags, hardlink_context, progress_bytes, userdata); - else if (S_ISLNK(buf.st_mode)) - q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags); - else if (S_ISFIFO(buf.st_mode)) - q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags, hardlink_context); - 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, override_uid, override_gid, copy_flags, hardlink_context); - else - q = -EOPNOTSUPP; + q = fd_copy_tree_generic(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, depth_left-1, override_uid, override_gid, copy_flags, hardlink_context, child_display_path, progress_path, progress_bytes, userdata); if (q == -EINTR) /* Propagate SIGINT/SIGTERM up instantly */ return q; @@ -1034,6 +1042,61 @@ static int fd_copy_directory( return r; } +static int fd_copy_leaf( + int df, + const char *from, + const struct stat *st, + int dt, + const char *to, + uid_t override_uid, + gid_t override_gid, + CopyFlags copy_flags, + HardlinkContext *hardlink_context, + const char *display_path, + copy_progress_bytes_t progress_bytes, + void *userdata) { + int r; + + if (S_ISREG(st->st_mode)) + r = fd_copy_regular(df, from, st, dt, to, override_uid, override_gid, copy_flags, hardlink_context, progress_bytes, userdata); + else if (S_ISLNK(st->st_mode)) + r = fd_copy_symlink(df, from, st, dt, to, override_uid, override_gid, copy_flags); + else if (S_ISFIFO(st->st_mode)) + r = fd_copy_fifo(df, from, st, dt, to, override_uid, override_gid, copy_flags, hardlink_context); + else if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode) || S_ISSOCK(st->st_mode)) + r = fd_copy_node(df, from, st, dt, to, override_uid, override_gid, copy_flags, hardlink_context); + else + r = -EOPNOTSUPP; + + return r; +} + +static int fd_copy_tree_generic( + int df, + const char *from, + const struct stat *st, + int dt, + const char *to, + dev_t original_device, + unsigned depth_left, + uid_t override_uid, + gid_t override_gid, + CopyFlags copy_flags, + HardlinkContext *hardlink_context, + const char *display_path, + copy_progress_path_t progress_path, + copy_progress_bytes_t progress_bytes, + void *userdata) { + int r; + + if (S_ISDIR(st->st_mode)) + r = fd_copy_directory(df, from, st, dt, to, original_device, depth_left-1, override_uid, override_gid, copy_flags, hardlink_context, display_path, progress_path, progress_bytes, userdata); + else + r = fd_copy_leaf(df, from, st, dt, to, override_uid, override_gid, copy_flags, hardlink_context, display_path, progress_bytes, userdata); + + return r; +} + int copy_tree_at_full( int fdf, const char *from, @@ -1055,18 +1118,7 @@ int copy_tree_at_full( if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0) return -errno; - if (S_ISREG(st.st_mode)) - r = fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags, NULL, progress_bytes, userdata); - else if (S_ISDIR(st.st_mode)) - r = fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, COPY_DEPTH_MAX, override_uid, override_gid, copy_flags, NULL, NULL, progress_path, progress_bytes, userdata); - else if (S_ISLNK(st.st_mode)) - r = fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags); - else if (S_ISFIFO(st.st_mode)) - r = fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags, NULL); - else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode)) - r = fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags, NULL); - else - return -EOPNOTSUPP; + r = fd_copy_tree_generic(fdf, from, &st, fdt, to, st.st_dev, COPY_DEPTH_MAX, override_uid, override_gid, copy_flags, NULL, NULL, progress_path, progress_bytes, userdata); if (r < 0) return r; |