Skip to content

Commit

Permalink
copyfile: allow copying empty files to non-existing destination dirs
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
  • Loading branch information
troglobit committed Apr 2, 2023
1 parent d61b847 commit 4e9b5c1
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/copyfile.c
Expand Up @@ -38,6 +38,7 @@ static int adjust_target(const char *src, char **dst)
{
int isdir = 0;

retry:
if (fisdir(*dst)) {
int slash = 0;
char *tmp, *ptr = strrchr(src, '/');
Expand All @@ -58,6 +59,13 @@ static int adjust_target(const char *src, char **dst)

sprintf(tmp, "%s%s%s", *dst, slash ? "" : "/", ptr);
*dst = tmp;
} else {
struct stat st;

if (stat(*dst, &st) && fisslashdir(*dst)) {
makedir(*dst, 0755);
goto retry;
}
}

return isdir;
Expand All @@ -67,7 +75,7 @@ static int adjust_target(const char *src, char **dst)
* breaks loop on error and EOF */
static ssize_t do_copy(int in, int out, size_t num, char *buffer, size_t len)
{
ssize_t ret = 0, size = 0;
ssize_t ret = 0, size = -1;

do {
size_t count = num > len ? len : num;
Expand All @@ -76,6 +84,8 @@ static ssize_t do_copy(int in, int out, size_t num, char *buffer, size_t len)
if (ret <= 0) {
if (ret == -1 && EINTR == errno)
continue;
if (ret == 0 && size == -1)
size = 0;
break;
}

Expand Down Expand Up @@ -194,7 +204,7 @@ ssize_t copyfile(const char *src, const char *dst, int len, int opt)
}

size = do_copy(in, out, num, buffer, BUFSIZ);
if (!size && errno)
if (size < 0)
saved_errno = errno;
else if (keep_mtim)
set_mtime(in, out);
Expand Down

0 comments on commit 4e9b5c1

Please sign in to comment.