2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2014-06-10 23:02:40 +02:00
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2014 Lennart Poettering
|
|
|
|
***/
|
|
|
|
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-06 21:19:20 +01:00
|
|
|
#include <sys/sendfile.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <sys/stat.h>
|
2014-12-26 17:00:39 +01:00
|
|
|
#include <sys/xattr.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2014-11-06 21:19:20 +01:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2014-12-12 03:15:58 +01:00
|
|
|
#include "btrfs-util.h"
|
2015-10-26 20:39:23 +01:00
|
|
|
#include "chattr-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "copy.h"
|
2015-10-26 20:07:55 +01:00
|
|
|
#include "dirent-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-26 18:05:03 +01:00
|
|
|
#include "fileio.h"
|
2015-10-26 21:16:26 +01:00
|
|
|
#include "fs-util.h"
|
2015-10-25 14:08:25 +01:00
|
|
|
#include "io-util.h"
|
2015-11-30 21:43:37 +01:00
|
|
|
#include "macro.h"
|
2016-03-16 00:26:34 +01:00
|
|
|
#include "missing.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2015-07-23 21:41:22 +02:00
|
|
|
#include "strv.h"
|
2015-12-01 23:22:03 +01:00
|
|
|
#include "time-util.h"
|
2015-10-26 23:20:41 +01:00
|
|
|
#include "umask-util.h"
|
2017-02-13 19:24:01 +01:00
|
|
|
#include "user-util.h"
|
2015-10-26 20:26:23 +01:00
|
|
|
#include "xattr-util.h"
|
2014-06-10 23:02:40 +02:00
|
|
|
|
basic/copy: use sendfile smarter
We called sendfile with 16kb (a.k.a. COPY_BUFFER_SIZE) as the maximum
number of bytes to copy. This seems rather inefficient, especially with
large files. Instead, call sendfile with a "large" maximum.
What "large" max means is a bit tricky: current file offset + max
must fit in loff_t. This means that as we call sendfile more than once,
we have to lower the max size.
With this patch, test-copy calls sendfile twice, e.g.:
sendfile(4, 3, NULL, 9223372036854775807) = 738760
sendfile(4, 3, NULL, 9223372036854037047) = 0
The second call is necessary to determine EOF.
2016-03-15 01:15:21 +01:00
|
|
|
#define COPY_BUFFER_SIZE (16*1024u)
|
2014-12-18 01:35:58 +01:00
|
|
|
|
2016-03-16 00:26:34 +01:00
|
|
|
static ssize_t try_copy_file_range(int fd_in, loff_t *off_in,
|
|
|
|
int fd_out, loff_t *off_out,
|
|
|
|
size_t len,
|
|
|
|
unsigned int flags) {
|
|
|
|
static int have = -1;
|
|
|
|
ssize_t r;
|
|
|
|
|
|
|
|
if (have == false)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
r = copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
|
|
|
|
if (_unlikely_(have < 0))
|
|
|
|
have = r >= 0 || errno != ENOSYS;
|
|
|
|
if (r >= 0)
|
|
|
|
return r;
|
|
|
|
else
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:44:15 +01:00
|
|
|
enum {
|
|
|
|
FD_IS_NO_PIPE,
|
|
|
|
FD_IS_BLOCKING_PIPE,
|
|
|
|
FD_IS_NONBLOCKING_PIPE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int fd_is_nonblock_pipe(int fd) {
|
|
|
|
struct stat st;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/* Checks whether the specified file descriptor refers to a pipe, and if so if is has O_NONBLOCK set. */
|
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (!S_ISFIFO(st.st_mode))
|
|
|
|
return FD_IS_NO_PIPE;
|
|
|
|
|
|
|
|
flags = fcntl(fd, F_GETFL);
|
|
|
|
if (flags < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return (flags & O_NONBLOCK) == O_NONBLOCK ? FD_IS_NONBLOCKING_PIPE : FD_IS_BLOCKING_PIPE;
|
|
|
|
}
|
|
|
|
|
2018-03-23 16:41:42 +01:00
|
|
|
int copy_bytes_full(
|
|
|
|
int fdf, int fdt,
|
|
|
|
uint64_t max_bytes,
|
|
|
|
CopyFlags copy_flags,
|
|
|
|
void **ret_remains,
|
|
|
|
size_t *ret_remains_size) {
|
|
|
|
|
2016-03-16 00:26:34 +01:00
|
|
|
bool try_cfr = true, try_sendfile = true, try_splice = true;
|
2018-03-23 17:44:15 +01:00
|
|
|
int r, nonblock_pipe = -1;
|
2016-04-02 23:46:49 +02:00
|
|
|
size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */
|
2014-11-06 21:19:20 +01:00
|
|
|
|
2014-06-10 23:02:40 +02:00
|
|
|
assert(fdf >= 0);
|
|
|
|
assert(fdt >= 0);
|
|
|
|
|
2018-03-09 22:44:47 +01:00
|
|
|
/* Tries to copy bytes from the file descriptor 'fdf' to 'fdt' in the smartest possible way. Copies a maximum
|
|
|
|
* of 'max_bytes', which may be specified as UINT64_MAX, in which no maximum is applied. Returns negative on
|
2018-03-23 16:41:42 +01:00
|
|
|
* error, zero if EOF is hit before the bytes limit is hit and positive otherwise. If the copy fails for some
|
|
|
|
* reason but we read but didn't yet write some data an ret_remains/ret_remains_size is not NULL, then it will
|
|
|
|
* be initialized with an allocated buffer containing this "remaining" data. Note that these two parameters are
|
|
|
|
* initialized with a valid buffer only on failure and only if there's actually data already read. Otherwise
|
|
|
|
* these parameters if non-NULL are set to NULL. */
|
|
|
|
|
|
|
|
if (ret_remains)
|
|
|
|
*ret_remains = NULL;
|
|
|
|
if (ret_remains_size)
|
|
|
|
*ret_remains_size = 0;
|
2018-03-09 22:44:47 +01:00
|
|
|
|
2018-03-23 16:36:56 +01:00
|
|
|
/* Try btrfs reflinks first. This only works on regular, seekable files, hence let's check the file offsets of
|
|
|
|
* source and destination first. */
|
|
|
|
if ((copy_flags & COPY_REFLINK)) {
|
|
|
|
off_t foffset;
|
|
|
|
|
|
|
|
foffset = lseek(fdf, 0, SEEK_CUR);
|
|
|
|
if (foffset >= 0) {
|
|
|
|
off_t toffset;
|
|
|
|
|
|
|
|
toffset = lseek(fdt, 0, SEEK_CUR);
|
|
|
|
if (toffset >= 0) {
|
|
|
|
|
|
|
|
if (foffset == 0 && toffset == 0 && max_bytes == UINT64_MAX)
|
|
|
|
r = btrfs_reflink(fdf, fdt); /* full file reflink */
|
|
|
|
else
|
|
|
|
r = btrfs_clone_range(fdf, foffset, fdt, toffset, max_bytes == UINT64_MAX ? 0 : max_bytes); /* partial reflink */
|
|
|
|
if (r >= 0) {
|
|
|
|
off_t t;
|
|
|
|
|
|
|
|
/* This worked, yay! Now — to be fully correct — let's adjust the file pointers */
|
|
|
|
if (max_bytes == UINT64_MAX) {
|
|
|
|
|
|
|
|
/* We cloned to the end of the source file, let's position the read
|
|
|
|
* pointer there, and query it at the same time. */
|
|
|
|
t = lseek(fdf, 0, SEEK_END);
|
|
|
|
if (t < 0)
|
|
|
|
return -errno;
|
|
|
|
if (t < foffset)
|
|
|
|
return -ESPIPE;
|
|
|
|
|
|
|
|
/* Let's adjust the destination file write pointer by the same number
|
|
|
|
* of bytes. */
|
|
|
|
t = lseek(fdt, toffset + (t - foffset), SEEK_SET);
|
|
|
|
if (t < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0; /* we copied the whole thing, hence hit EOF, return 0 */
|
|
|
|
} else {
|
|
|
|
t = lseek(fdf, foffset + max_bytes, SEEK_SET);
|
|
|
|
if (t < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
t = lseek(fdt, toffset + max_bytes, SEEK_SET);
|
|
|
|
if (t < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 1; /* we copied only some number of bytes, which worked, but this means we didn't hit EOF, return 1 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug_errno(r, "Reflinking didn't work, falling back to non-reflink copying: %m");
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 03:20:53 +01:00
|
|
|
}
|
|
|
|
|
2014-06-10 23:02:40 +02:00
|
|
|
for (;;) {
|
2014-11-06 21:19:20 +01:00
|
|
|
ssize_t n;
|
2014-06-23 16:28:05 +02:00
|
|
|
|
2015-09-10 18:16:18 +02:00
|
|
|
if (max_bytes != (uint64_t) -1) {
|
2014-06-23 16:28:05 +02:00
|
|
|
if (max_bytes <= 0)
|
2015-09-17 01:20:56 +02:00
|
|
|
return 1; /* return > 0 if we hit the max_bytes limit */
|
2014-06-23 16:28:05 +02:00
|
|
|
|
2016-04-03 00:20:12 +02:00
|
|
|
if (m > max_bytes)
|
|
|
|
m = max_bytes;
|
2014-06-23 16:28:05 +02:00
|
|
|
}
|
|
|
|
|
2016-03-16 00:26:34 +01:00
|
|
|
/* First try copy_file_range(), unless we already tried */
|
|
|
|
if (try_cfr) {
|
|
|
|
n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
|
|
|
|
if (n < 0) {
|
2016-04-21 12:47:36 +02:00
|
|
|
if (!IN_SET(n, -EINVAL, -ENOSYS, -EXDEV, -EBADF))
|
2016-03-16 00:26:34 +01:00
|
|
|
return n;
|
|
|
|
|
|
|
|
try_cfr = false;
|
|
|
|
/* use fallback below */
|
|
|
|
} else if (n == 0) /* EOF */
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
/* Success! */
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2014-11-06 21:19:20 +01:00
|
|
|
/* First try sendfile(), unless we already tried */
|
|
|
|
if (try_sendfile) {
|
|
|
|
n = sendfile(fdt, fdf, NULL, m);
|
|
|
|
if (n < 0) {
|
basic/copy: use sendfile smarter
We called sendfile with 16kb (a.k.a. COPY_BUFFER_SIZE) as the maximum
number of bytes to copy. This seems rather inefficient, especially with
large files. Instead, call sendfile with a "large" maximum.
What "large" max means is a bit tricky: current file offset + max
must fit in loff_t. This means that as we call sendfile more than once,
we have to lower the max size.
With this patch, test-copy calls sendfile twice, e.g.:
sendfile(4, 3, NULL, 9223372036854775807) = 738760
sendfile(4, 3, NULL, 9223372036854037047) = 0
The second call is necessary to determine EOF.
2016-03-15 01:15:21 +01:00
|
|
|
if (!IN_SET(errno, EINVAL, ENOSYS))
|
2014-11-06 21:19:20 +01:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
try_sendfile = false;
|
|
|
|
/* use fallback below */
|
|
|
|
} else if (n == 0) /* EOF */
|
|
|
|
break;
|
basic/copy: use sendfile smarter
We called sendfile with 16kb (a.k.a. COPY_BUFFER_SIZE) as the maximum
number of bytes to copy. This seems rather inefficient, especially with
large files. Instead, call sendfile with a "large" maximum.
What "large" max means is a bit tricky: current file offset + max
must fit in loff_t. This means that as we call sendfile more than once,
we have to lower the max size.
With this patch, test-copy calls sendfile twice, e.g.:
sendfile(4, 3, NULL, 9223372036854775807) = 738760
sendfile(4, 3, NULL, 9223372036854037047) = 0
The second call is necessary to determine EOF.
2016-03-15 01:15:21 +01:00
|
|
|
else
|
2015-08-28 00:10:35 +02:00
|
|
|
/* Success! */
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2018-03-23 17:44:15 +01:00
|
|
|
/* Then try splice, unless we already tried. */
|
|
|
|
if (try_splice) {
|
|
|
|
|
|
|
|
/* splice()'s asynchronous I/O support is a bit weird. When it encounters a pipe file
|
|
|
|
* descriptor, then it will ignore its O_NONBLOCK flag and instead only honour the
|
|
|
|
* SPLICE_F_NONBLOCK flag specified in its flag parameter. Let's hide this behaviour here, and
|
|
|
|
* check if either of the specified fds are a pipe, and if so, let's pass the flag
|
|
|
|
* automatically, depending on O_NONBLOCK being set.
|
|
|
|
*
|
|
|
|
* Here's a twist though: when we use it to move data between two pipes of which one has
|
|
|
|
* O_NONBLOCK set and the other has not, then we have no individual control over O_NONBLOCK
|
|
|
|
* behaviour. Hence in that case we can't use splice() and still guarantee systematic
|
|
|
|
* O_NONBLOCK behaviour, hence don't. */
|
|
|
|
|
|
|
|
if (nonblock_pipe < 0) {
|
|
|
|
int a, b;
|
|
|
|
|
|
|
|
/* Check if either of these fds is a pipe, and if so non-blocking or not */
|
|
|
|
a = fd_is_nonblock_pipe(fdf);
|
|
|
|
if (a < 0)
|
|
|
|
return a;
|
|
|
|
|
|
|
|
b = fd_is_nonblock_pipe(fdt);
|
|
|
|
if (b < 0)
|
|
|
|
return b;
|
|
|
|
|
|
|
|
if ((a == FD_IS_NO_PIPE && b == FD_IS_NO_PIPE) ||
|
|
|
|
(a == FD_IS_BLOCKING_PIPE && b == FD_IS_NONBLOCKING_PIPE) ||
|
|
|
|
(a == FD_IS_NONBLOCKING_PIPE && b == FD_IS_BLOCKING_PIPE))
|
|
|
|
|
|
|
|
/* splice() only works if one of the fds is a pipe. If neither is, let's skip
|
|
|
|
* this step right-away. As mentioned above, if one of the two fds refers to a
|
|
|
|
* blocking pipe and the other to a non-blocking pipe, we can't use splice()
|
|
|
|
* either, hence don't try either. This hence means we can only use splice() if
|
|
|
|
* either only one of the two fds is a pipe, or if both are pipes with the same
|
|
|
|
* nonblocking flag setting. */
|
|
|
|
|
|
|
|
try_splice = false;
|
|
|
|
else
|
|
|
|
nonblock_pipe = a == FD_IS_NONBLOCKING_PIPE || b == FD_IS_NONBLOCKING_PIPE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-28 00:10:35 +02:00
|
|
|
if (try_splice) {
|
2018-03-23 17:44:15 +01:00
|
|
|
n = splice(fdf, NULL, fdt, NULL, m, nonblock_pipe ? SPLICE_F_NONBLOCK : 0);
|
2015-08-28 00:10:35 +02:00
|
|
|
if (n < 0) {
|
basic/copy: use sendfile smarter
We called sendfile with 16kb (a.k.a. COPY_BUFFER_SIZE) as the maximum
number of bytes to copy. This seems rather inefficient, especially with
large files. Instead, call sendfile with a "large" maximum.
What "large" max means is a bit tricky: current file offset + max
must fit in loff_t. This means that as we call sendfile more than once,
we have to lower the max size.
With this patch, test-copy calls sendfile twice, e.g.:
sendfile(4, 3, NULL, 9223372036854775807) = 738760
sendfile(4, 3, NULL, 9223372036854037047) = 0
The second call is necessary to determine EOF.
2016-03-15 01:15:21 +01:00
|
|
|
if (!IN_SET(errno, EINVAL, ENOSYS))
|
2015-08-28 00:10:35 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
try_splice = false;
|
|
|
|
/* use fallback below */
|
|
|
|
} else if (n == 0) /* EOF */
|
|
|
|
break;
|
basic/copy: use sendfile smarter
We called sendfile with 16kb (a.k.a. COPY_BUFFER_SIZE) as the maximum
number of bytes to copy. This seems rather inefficient, especially with
large files. Instead, call sendfile with a "large" maximum.
What "large" max means is a bit tricky: current file offset + max
must fit in loff_t. This means that as we call sendfile more than once,
we have to lower the max size.
With this patch, test-copy calls sendfile twice, e.g.:
sendfile(4, 3, NULL, 9223372036854775807) = 738760
sendfile(4, 3, NULL, 9223372036854037047) = 0
The second call is necessary to determine EOF.
2016-03-15 01:15:21 +01:00
|
|
|
else
|
2015-08-28 00:10:35 +02:00
|
|
|
/* Success! */
|
2014-11-06 21:19:20 +01:00
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* As a fallback just copy bits by hand */
|
|
|
|
{
|
2018-03-23 16:41:42 +01:00
|
|
|
uint8_t buf[MIN(m, COPY_BUFFER_SIZE)], *p = buf;
|
|
|
|
ssize_t z;
|
2014-06-10 23:02:40 +02:00
|
|
|
|
basic/copy: use sendfile smarter
We called sendfile with 16kb (a.k.a. COPY_BUFFER_SIZE) as the maximum
number of bytes to copy. This seems rather inefficient, especially with
large files. Instead, call sendfile with a "large" maximum.
What "large" max means is a bit tricky: current file offset + max
must fit in loff_t. This means that as we call sendfile more than once,
we have to lower the max size.
With this patch, test-copy calls sendfile twice, e.g.:
sendfile(4, 3, NULL, 9223372036854775807) = 738760
sendfile(4, 3, NULL, 9223372036854037047) = 0
The second call is necessary to determine EOF.
2016-03-15 01:15:21 +01:00
|
|
|
n = read(fdf, buf, sizeof buf);
|
2014-11-06 21:19:20 +01:00
|
|
|
if (n < 0)
|
|
|
|
return -errno;
|
|
|
|
if (n == 0) /* EOF */
|
|
|
|
break;
|
|
|
|
|
2018-03-23 16:41:42 +01:00
|
|
|
z = (size_t) n;
|
|
|
|
do {
|
|
|
|
ssize_t k;
|
|
|
|
|
|
|
|
k = write(fdt, p, z);
|
|
|
|
if (k < 0) {
|
|
|
|
r = -errno;
|
|
|
|
|
|
|
|
if (ret_remains) {
|
|
|
|
void *copy;
|
|
|
|
|
|
|
|
copy = memdup(p, z);
|
|
|
|
if (!copy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*ret_remains = copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret_remains_size)
|
|
|
|
*ret_remains_size = z;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(k <= z);
|
|
|
|
z -= k;
|
|
|
|
p += k;
|
|
|
|
} while (z > 0);
|
2014-11-06 21:19:20 +01:00
|
|
|
}
|
2014-06-23 16:28:05 +02:00
|
|
|
|
2014-11-06 21:19:20 +01:00
|
|
|
next:
|
2015-09-10 18:16:18 +02:00
|
|
|
if (max_bytes != (uint64_t) -1) {
|
|
|
|
assert(max_bytes >= (uint64_t) n);
|
2014-06-23 16:28:05 +02:00
|
|
|
max_bytes -= n;
|
|
|
|
}
|
basic/copy: use sendfile smarter
We called sendfile with 16kb (a.k.a. COPY_BUFFER_SIZE) as the maximum
number of bytes to copy. This seems rather inefficient, especially with
large files. Instead, call sendfile with a "large" maximum.
What "large" max means is a bit tricky: current file offset + max
must fit in loff_t. This means that as we call sendfile more than once,
we have to lower the max size.
With this patch, test-copy calls sendfile twice, e.g.:
sendfile(4, 3, NULL, 9223372036854775807) = 738760
sendfile(4, 3, NULL, 9223372036854037047) = 0
The second call is necessary to determine EOF.
2016-03-15 01:15:21 +01:00
|
|
|
/* sendfile accepts at most SSIZE_MAX-offset bytes to copy,
|
|
|
|
* so reduce our maximum by the amount we already copied,
|
|
|
|
* but don't go below our copy buffer size, unless we are
|
2016-07-10 14:48:23 +02:00
|
|
|
* close the limit of bytes we are allowed to copy. */
|
basic/copy: use sendfile smarter
We called sendfile with 16kb (a.k.a. COPY_BUFFER_SIZE) as the maximum
number of bytes to copy. This seems rather inefficient, especially with
large files. Instead, call sendfile with a "large" maximum.
What "large" max means is a bit tricky: current file offset + max
must fit in loff_t. This means that as we call sendfile more than once,
we have to lower the max size.
With this patch, test-copy calls sendfile twice, e.g.:
sendfile(4, 3, NULL, 9223372036854775807) = 738760
sendfile(4, 3, NULL, 9223372036854037047) = 0
The second call is necessary to determine EOF.
2016-03-15 01:15:21 +01:00
|
|
|
m = MAX(MIN(COPY_BUFFER_SIZE, max_bytes), m - n);
|
2014-06-10 23:02:40 +02:00
|
|
|
}
|
|
|
|
|
2015-09-17 01:20:56 +02:00
|
|
|
return 0; /* return 0 if we hit EOF earlier than the size limit */
|
2014-06-10 23:02:40 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
static int fd_copy_symlink(
|
|
|
|
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) {
|
|
|
|
|
2014-06-10 23:02:40 +02:00
|
|
|
_cleanup_free_ char *target = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(from);
|
|
|
|
assert(st);
|
|
|
|
assert(to);
|
|
|
|
|
|
|
|
r = readlinkat_malloc(df, from, &target);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-06-19 19:36:08 +02:00
|
|
|
if (symlinkat(target, dt, to) < 0)
|
2014-06-10 23:02:40 +02:00
|
|
|
return -errno;
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
if (fchownat(dt, to,
|
|
|
|
uid_is_valid(override_uid) ? override_uid : st->st_uid,
|
|
|
|
gid_is_valid(override_gid) ? override_gid : st->st_gid,
|
|
|
|
AT_SYMLINK_NOFOLLOW) < 0)
|
2014-06-10 23:02:40 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
static int fd_copy_regular(
|
|
|
|
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) {
|
|
|
|
|
2014-06-10 23:02:40 +02:00
|
|
|
_cleanup_close_ int fdf = -1, fdt = -1;
|
2014-12-27 17:44:04 +01:00
|
|
|
struct timespec ts[2];
|
2014-06-10 23:02:40 +02:00
|
|
|
int r, q;
|
|
|
|
|
|
|
|
assert(from);
|
|
|
|
assert(st);
|
|
|
|
assert(to);
|
|
|
|
|
|
|
|
fdf = openat(df, from, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
|
|
|
|
if (fdf < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
fdt = openat(dt, to, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, st->st_mode & 07777);
|
2014-06-19 19:36:08 +02:00
|
|
|
if (fdt < 0)
|
2014-06-10 23:02:40 +02:00
|
|
|
return -errno;
|
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
|
2014-06-10 23:02:40 +02:00
|
|
|
if (r < 0) {
|
2018-02-09 09:50:13 +01:00
|
|
|
(void) unlinkat(dt, to, 0);
|
2014-06-10 23:02:40 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
if (fchown(fdt,
|
|
|
|
uid_is_valid(override_uid) ? override_uid : st->st_uid,
|
|
|
|
gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
|
2014-06-10 23:02:40 +02:00
|
|
|
r = -errno;
|
|
|
|
|
|
|
|
if (fchmod(fdt, st->st_mode & 07777) < 0)
|
|
|
|
r = -errno;
|
|
|
|
|
2014-12-27 17:44:04 +01:00
|
|
|
ts[0] = st->st_atim;
|
|
|
|
ts[1] = st->st_mtim;
|
|
|
|
(void) futimens(fdt, ts);
|
2014-12-26 17:00:39 +01:00
|
|
|
(void) copy_xattr(fdf, fdt);
|
|
|
|
|
2014-06-10 23:02:40 +02:00
|
|
|
q = close(fdt);
|
|
|
|
fdt = -1;
|
|
|
|
|
|
|
|
if (q < 0) {
|
|
|
|
r = -errno;
|
2018-02-09 09:50:13 +01:00
|
|
|
(void) unlinkat(dt, to, 0);
|
2014-06-10 23:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
static int fd_copy_fifo(
|
|
|
|
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) {
|
2014-06-10 23:02:40 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(from);
|
|
|
|
assert(st);
|
|
|
|
assert(to);
|
|
|
|
|
|
|
|
r = mkfifoat(dt, to, st->st_mode & 07777);
|
2014-06-19 19:36:08 +02:00
|
|
|
if (r < 0)
|
2014-06-10 23:02:40 +02:00
|
|
|
return -errno;
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
if (fchownat(dt, to,
|
|
|
|
uid_is_valid(override_uid) ? override_uid : st->st_uid,
|
|
|
|
gid_is_valid(override_gid) ? override_gid : st->st_gid,
|
|
|
|
AT_SYMLINK_NOFOLLOW) < 0)
|
2014-06-10 23:02:40 +02:00
|
|
|
r = -errno;
|
|
|
|
|
|
|
|
if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
|
|
|
|
r = -errno;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
static int fd_copy_node(
|
|
|
|
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) {
|
2014-06-10 23:02:40 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(from);
|
|
|
|
assert(st);
|
|
|
|
assert(to);
|
|
|
|
|
|
|
|
r = mknodat(dt, to, st->st_mode, st->st_rdev);
|
2014-06-19 19:36:08 +02:00
|
|
|
if (r < 0)
|
2014-06-10 23:02:40 +02:00
|
|
|
return -errno;
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
if (fchownat(dt, to,
|
|
|
|
uid_is_valid(override_uid) ? override_uid : st->st_uid,
|
|
|
|
gid_is_valid(override_gid) ? override_gid : st->st_gid,
|
|
|
|
AT_SYMLINK_NOFOLLOW) < 0)
|
2014-06-10 23:02:40 +02:00
|
|
|
r = -errno;
|
|
|
|
|
|
|
|
if (fchmodat(dt, to, st->st_mode & 07777, 0) < 0)
|
|
|
|
r = -errno;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-12-12 03:15:58 +01:00
|
|
|
static int fd_copy_directory(
|
|
|
|
int df,
|
|
|
|
const char *from,
|
|
|
|
const struct stat *st,
|
|
|
|
int dt,
|
|
|
|
const char *to,
|
|
|
|
dev_t original_device,
|
2017-02-13 19:24:01 +01:00
|
|
|
uid_t override_uid,
|
|
|
|
gid_t override_gid,
|
2017-02-13 19:00:22 +01:00
|
|
|
CopyFlags copy_flags) {
|
2014-12-12 03:15:58 +01:00
|
|
|
|
2014-06-10 23:02:40 +02:00
|
|
|
_cleanup_close_ int fdf = -1, fdt = -1;
|
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
|
|
|
struct dirent *de;
|
|
|
|
bool created;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(st);
|
|
|
|
assert(to);
|
|
|
|
|
2014-12-12 03:15:58 +01:00
|
|
|
if (from)
|
|
|
|
fdf = openat(df, from, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
|
|
|
|
else
|
|
|
|
fdf = fcntl(df, F_DUPFD_CLOEXEC, 3);
|
2016-04-29 19:42:07 +02:00
|
|
|
if (fdf < 0)
|
|
|
|
return -errno;
|
2014-06-10 23:02:40 +02:00
|
|
|
|
|
|
|
d = fdopendir(fdf);
|
|
|
|
if (!d)
|
|
|
|
return -errno;
|
|
|
|
fdf = -1;
|
|
|
|
|
|
|
|
r = mkdirat(dt, to, st->st_mode & 07777);
|
|
|
|
if (r >= 0)
|
|
|
|
created = true;
|
2017-02-13 19:00:22 +01:00
|
|
|
else if (errno == EEXIST && (copy_flags & COPY_MERGE))
|
2014-06-10 23:02:40 +02:00
|
|
|
created = false;
|
|
|
|
else
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
fdt = openat(dt, to, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
|
|
|
|
if (fdt < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2014-06-20 00:20:35 +02:00
|
|
|
r = 0;
|
|
|
|
|
2015-07-23 21:41:22 +02:00
|
|
|
FOREACH_DIRENT_ALL(de, d, return -errno) {
|
2014-06-10 23:02:40 +02:00
|
|
|
struct stat buf;
|
|
|
|
int q;
|
|
|
|
|
2017-02-02 00:06:18 +01:00
|
|
|
if (dot_or_dot_dot(de->d_name))
|
2015-07-23 21:41:22 +02:00
|
|
|
continue;
|
|
|
|
|
2014-06-10 23:02:40 +02:00
|
|
|
if (fstatat(dirfd(d), de->d_name, &buf, AT_SYMLINK_NOFOLLOW) < 0) {
|
|
|
|
r = -errno;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf.st_dev != original_device)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (S_ISREG(buf.st_mode))
|
2017-02-13 19:24:01 +01:00
|
|
|
q = fd_copy_regular(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
|
2014-06-10 23:02:40 +02:00
|
|
|
else if (S_ISDIR(buf.st_mode))
|
2017-02-13 19:24:01 +01:00
|
|
|
q = fd_copy_directory(dirfd(d), de->d_name, &buf, fdt, de->d_name, original_device, override_uid, override_gid, copy_flags);
|
2014-06-10 23:02:40 +02:00
|
|
|
else if (S_ISLNK(buf.st_mode))
|
2017-02-13 19:24:01 +01:00
|
|
|
q = fd_copy_symlink(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
|
2014-06-10 23:02:40 +02:00
|
|
|
else if (S_ISFIFO(buf.st_mode))
|
2017-02-13 19:24:01 +01:00
|
|
|
q = fd_copy_fifo(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
|
2016-04-29 14:21:22 +02:00
|
|
|
else if (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode) || S_ISSOCK(buf.st_mode))
|
2017-02-13 19:24:01 +01:00
|
|
|
q = fd_copy_node(dirfd(d), de->d_name, &buf, fdt, de->d_name, override_uid, override_gid, copy_flags);
|
2014-06-10 23:02:40 +02:00
|
|
|
else
|
2015-03-13 14:08:00 +01:00
|
|
|
q = -EOPNOTSUPP;
|
2014-06-10 23:02:40 +02:00
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
if (q == -EEXIST && (copy_flags & COPY_MERGE))
|
2014-06-19 19:36:08 +02:00
|
|
|
q = 0;
|
|
|
|
|
2014-06-10 23:02:40 +02:00
|
|
|
if (q < 0)
|
|
|
|
r = q;
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:44:59 +02:00
|
|
|
if (created) {
|
|
|
|
struct timespec ut[2] = {
|
|
|
|
st->st_atim,
|
|
|
|
st->st_mtim
|
|
|
|
};
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
if (fchown(fdt,
|
|
|
|
uid_is_valid(override_uid) ? override_uid : st->st_uid,
|
|
|
|
gid_is_valid(override_gid) ? override_gid : st->st_gid) < 0)
|
2016-04-29 19:44:59 +02:00
|
|
|
r = -errno;
|
|
|
|
|
|
|
|
if (fchmod(fdt, st->st_mode & 07777) < 0)
|
|
|
|
r = -errno;
|
|
|
|
|
|
|
|
(void) copy_xattr(dirfd(d), fdt);
|
|
|
|
(void) futimens(fdt, ut);
|
|
|
|
}
|
|
|
|
|
2014-06-10 23:02:40 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
int copy_tree_at(int fdf, const char *from, int fdt, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
|
2014-06-10 23:02:40 +02:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
assert(from);
|
|
|
|
assert(to);
|
|
|
|
|
2014-12-18 01:35:58 +01:00
|
|
|
if (fstatat(fdf, from, &st, AT_SYMLINK_NOFOLLOW) < 0)
|
2014-06-10 23:02:40 +02:00
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (S_ISREG(st.st_mode))
|
2017-02-13 19:24:01 +01:00
|
|
|
return fd_copy_regular(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
|
2014-06-10 23:02:40 +02:00
|
|
|
else if (S_ISDIR(st.st_mode))
|
2017-02-13 19:24:01 +01:00
|
|
|
return fd_copy_directory(fdf, from, &st, fdt, to, st.st_dev, override_uid, override_gid, copy_flags);
|
2014-06-10 23:02:40 +02:00
|
|
|
else if (S_ISLNK(st.st_mode))
|
2017-02-13 19:24:01 +01:00
|
|
|
return fd_copy_symlink(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
|
2014-06-10 23:02:40 +02:00
|
|
|
else if (S_ISFIFO(st.st_mode))
|
2017-02-13 19:24:01 +01:00
|
|
|
return fd_copy_fifo(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
|
2016-04-29 14:21:22 +02:00
|
|
|
else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode) || S_ISSOCK(st.st_mode))
|
2017-02-13 19:24:01 +01:00
|
|
|
return fd_copy_node(fdf, from, &st, fdt, to, override_uid, override_gid, copy_flags);
|
2014-06-10 23:02:40 +02:00
|
|
|
else
|
2015-03-13 14:08:00 +01:00
|
|
|
return -EOPNOTSUPP;
|
2014-06-10 23:02:40 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
int copy_tree(const char *from, const char *to, uid_t override_uid, gid_t override_gid, CopyFlags copy_flags) {
|
|
|
|
return copy_tree_at(AT_FDCWD, from, AT_FDCWD, to, override_uid, override_gid, copy_flags);
|
2014-12-18 01:35:58 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
int copy_directory_fd(int dirfd, const char *to, CopyFlags copy_flags) {
|
2014-12-12 03:15:58 +01:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
assert(dirfd >= 0);
|
|
|
|
assert(to);
|
|
|
|
|
|
|
|
if (fstat(dirfd, &st) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
return -ENOTDIR;
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
|
2014-12-12 03:15:58 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
int copy_directory(const char *from, const char *to, CopyFlags copy_flags) {
|
2016-04-29 20:06:20 +02:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
assert(from);
|
|
|
|
assert(to);
|
|
|
|
|
|
|
|
if (lstat(from, &st) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (!S_ISDIR(st.st_mode))
|
|
|
|
return -ENOTDIR;
|
|
|
|
|
2017-02-13 19:24:01 +01:00
|
|
|
return fd_copy_directory(AT_FDCWD, from, &st, AT_FDCWD, to, st.st_dev, UID_INVALID, GID_INVALID, copy_flags);
|
2016-04-29 20:06:20 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
int copy_file_fd(const char *from, int fdt, CopyFlags copy_flags) {
|
2014-11-06 21:19:20 +01:00
|
|
|
_cleanup_close_ int fdf = -1;
|
2014-12-26 17:00:39 +01:00
|
|
|
int r;
|
2014-06-10 23:02:40 +02:00
|
|
|
|
|
|
|
assert(from);
|
2014-11-06 21:19:20 +01:00
|
|
|
assert(fdt >= 0);
|
2014-06-10 23:02:40 +02:00
|
|
|
|
|
|
|
fdf = open(from, O_RDONLY|O_CLOEXEC|O_NOCTTY);
|
|
|
|
if (fdf < 0)
|
|
|
|
return -errno;
|
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
r = copy_bytes(fdf, fdt, (uint64_t) -1, copy_flags);
|
2014-12-26 17:00:39 +01:00
|
|
|
|
|
|
|
(void) copy_times(fdf, fdt);
|
|
|
|
(void) copy_xattr(fdf, fdt);
|
|
|
|
|
|
|
|
return r;
|
2014-11-06 21:19:20 +01:00
|
|
|
}
|
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
int copy_file(const char *from, const char *to, int flags, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
|
2015-03-27 12:02:49 +01:00
|
|
|
int fdt = -1, r;
|
2014-11-06 21:19:20 +01:00
|
|
|
|
|
|
|
assert(from);
|
|
|
|
assert(to);
|
|
|
|
|
2014-12-27 17:44:04 +01:00
|
|
|
RUN_WITH_UMASK(0000) {
|
|
|
|
fdt = open(to, flags|O_WRONLY|O_CREAT|O_CLOEXEC|O_NOCTTY, mode);
|
|
|
|
if (fdt < 0)
|
|
|
|
return -errno;
|
|
|
|
}
|
2014-06-10 23:02:40 +02:00
|
|
|
|
2015-01-08 19:15:49 +01:00
|
|
|
if (chattr_flags != 0)
|
2015-04-08 20:47:35 +02:00
|
|
|
(void) chattr_fd(fdt, chattr_flags, (unsigned) -1);
|
2015-01-08 19:15:49 +01:00
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
r = copy_file_fd(from, fdt, copy_flags);
|
2014-06-10 23:02:40 +02:00
|
|
|
if (r < 0) {
|
2014-11-06 21:19:20 +01:00
|
|
|
close(fdt);
|
2018-02-09 09:50:13 +01:00
|
|
|
(void) unlink(to);
|
2014-06-10 23:02:40 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-11-06 21:19:20 +01:00
|
|
|
if (close(fdt) < 0) {
|
|
|
|
unlink_noerrno(to);
|
|
|
|
return -errno;
|
2014-06-10 23:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-26 17:00:39 +01:00
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
int copy_file_atomic(const char *from, const char *to, mode_t mode, unsigned chattr_flags, CopyFlags copy_flags) {
|
2015-03-27 12:02:49 +01:00
|
|
|
_cleanup_free_ char *t = NULL;
|
2014-12-27 17:44:04 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(from);
|
|
|
|
assert(to);
|
|
|
|
|
2015-06-15 19:09:02 +02:00
|
|
|
r = tempfn_random(to, NULL, &t);
|
2014-12-27 17:44:04 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
r = copy_file(from, t, O_NOFOLLOW|O_EXCL, mode, chattr_flags, copy_flags);
|
2014-12-27 17:44:04 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2017-02-13 19:00:22 +01:00
|
|
|
if (copy_flags & COPY_REPLACE) {
|
2015-03-10 18:15:52 +01:00
|
|
|
r = renameat(AT_FDCWD, t, AT_FDCWD, to);
|
|
|
|
if (r < 0)
|
|
|
|
r = -errno;
|
|
|
|
} else
|
|
|
|
r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
|
|
|
|
if (r < 0) {
|
2017-02-21 17:13:28 +01:00
|
|
|
(void) unlink(t);
|
2015-03-10 18:15:52 +01:00
|
|
|
return r;
|
2014-12-27 17:44:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-26 17:00:39 +01:00
|
|
|
int copy_times(int fdf, int fdt) {
|
|
|
|
struct timespec ut[2];
|
|
|
|
struct stat st;
|
2015-03-27 12:02:49 +01:00
|
|
|
usec_t crtime = 0;
|
2014-12-26 17:00:39 +01:00
|
|
|
|
|
|
|
assert(fdf >= 0);
|
|
|
|
assert(fdt >= 0);
|
|
|
|
|
|
|
|
if (fstat(fdf, &st) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
ut[0] = st.st_atim;
|
|
|
|
ut[1] = st.st_mtim;
|
|
|
|
|
|
|
|
if (futimens(fdt, ut) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (fd_getcrtime(fdf, &crtime) >= 0)
|
|
|
|
(void) fd_setcrtime(fdt, crtime);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int copy_xattr(int fdf, int fdt) {
|
|
|
|
_cleanup_free_ char *bufa = NULL, *bufb = NULL;
|
|
|
|
size_t sza = 100, szb = 100;
|
|
|
|
ssize_t n;
|
|
|
|
int ret = 0;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
bufa = malloc(sza);
|
|
|
|
if (!bufa)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
n = flistxattr(fdf, bufa, sza);
|
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
if (n > 0)
|
|
|
|
break;
|
|
|
|
if (errno != ERANGE)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
sza *= 2;
|
|
|
|
|
2015-07-31 19:56:38 +02:00
|
|
|
bufa = mfree(bufa);
|
2014-12-26 17:00:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p = bufa;
|
|
|
|
while (n > 0) {
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
l = strlen(p);
|
|
|
|
assert(l < (size_t) n);
|
|
|
|
|
|
|
|
if (startswith(p, "user.")) {
|
|
|
|
ssize_t m;
|
|
|
|
|
|
|
|
if (!bufb) {
|
|
|
|
bufb = malloc(szb);
|
|
|
|
if (!bufb)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
m = fgetxattr(fdf, p, bufb, szb);
|
|
|
|
if (m < 0) {
|
|
|
|
if (errno == ERANGE) {
|
|
|
|
szb *= 2;
|
2015-07-31 19:56:38 +02:00
|
|
|
bufb = mfree(bufb);
|
2014-12-26 17:00:39 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fsetxattr(fdt, p, bufb, m, 0) < 0)
|
|
|
|
ret = -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += l + 1;
|
|
|
|
n -= l + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|