#include "util.h"
#include "copy.h"
-int copy_bytes(int fdf, int fdt) {
+int copy_bytes(int fdf, int fdt, off_t max_bytes) {
assert(fdf >= 0);
assert(fdt >= 0);
for (;;) {
char buf[PIPE_BUF];
ssize_t n, k;
+ size_t m = sizeof(buf);
- n = read(fdf, buf, sizeof(buf));
+ if (max_bytes != (off_t) -1) {
+
+ if (max_bytes <= 0)
+ return -E2BIG;
+
+ if ((off_t) m > max_bytes)
+ m = (size_t) max_bytes;
+ }
+
+ n = read(fdf, buf, m);
if (n < 0)
return -errno;
if (n == 0)
return k;
if (k != n)
return errno ? -errno : -EIO;
+
+ if (max_bytes != (off_t) -1) {
+ assert(max_bytes >= n);
+ max_bytes -= n;
+ }
}
return 0;
if (r < 0)
return r;
- if (symlinkat(target, dt, to) < 0) {
- if (errno == EEXIST)
- return 0;
-
+ if (symlinkat(target, dt, to) < 0)
return -errno;
- }
if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
return -errno;
return -errno;
fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
- if (fdt < 0) {
- if (errno == EEXIST)
- return 0;
-
+ if (fdt < 0)
return -errno;
- }
- r = copy_bytes(fdf, fdt);
+ r = copy_bytes(fdf, fdt, (off_t) -1);
if (r < 0) {
unlinkat(dt, to, 0);
return r;
assert(to);
r = mkfifoat(dt, to, st->st_mode & 07777);
- if (r < 0) {
- if (errno == EEXIST)
- return 0;
-
+ if (r < 0)
return -errno;
- }
if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
r = -errno;
assert(to);
r = mknodat(dt, to, st->st_mode, st->st_rdev);
- if (r < 0) {
- if (errno == EEXIST)
- return 0;
-
+ if (r < 0)
return -errno;
- }
if (fchownat(dt, to, st->st_uid, st->st_gid, AT_SYMLINK_NOFOLLOW) < 0)
r = -errno;
return r;
}
-static int fd_copy_directory(int df, const char *from, const struct stat *st, int dt, const char *to, dev_t original_device) {
+static int fd_copy_directory(int df, const char *from, const struct stat *st, int dt, const char *to, dev_t original_device, bool merge) {
_cleanup_close_ int fdf = -1, fdt = -1;
_cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
r = mkdirat(dt, to, st->st_mode & 07777);
if (r >= 0)
created = true;
- else if (errno == EEXIST)
+ else if (errno == EEXIST && merge)
created = false;
else
return -errno;
if (fdt < 0)
return -errno;
+ r = 0;
+
if (created) {
if (fchown(fdt, st->st_uid, st->st_gid) < 0)
r = -errno;
if (S_ISREG(buf.st_mode))
q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name);
else if (S_ISDIR(buf.st_mode))
- q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device);
+ q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, merge);
else if (S_ISLNK(buf.st_mode))
q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name);
else if (S_ISFIFO(buf.st_mode))
else
q = -ENOTSUP;
+ if (q == -EEXIST && merge)
+ q = 0;
+
if (q < 0)
r = q;
}
return r;
}
-int copy_tree(const char *from, const char *to) {
+int copy_tree(const char *from, const char *to, bool merge) {
struct stat st;
assert(from);
if (S_ISREG(st.st_mode))
return fd_copy_regular(AT_FDCWD, from, &st, AT_FDCWD, to);
else if (S_ISDIR(st.st_mode))
- return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev);
+ return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, merge);
else if (S_ISLNK(st.st_mode))
return fd_copy_symlink(AT_FDCWD, from, &st, AT_FDCWD, to);
else if (S_ISFIFO(st.st_mode))
if (fdt < 0)
return -errno;
- r = copy_bytes(fdf, fdt);
+ r = copy_bytes(fdf, fdt, (off_t) -1);
if (r < 0) {
unlink(to);
return r;