HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/tar-util.c
Git blob: 8d6306a20d6dbbd4054199b130c7fa24d02d2fd7
Lines 1001–1682 of 1,682
if (r < 0)
return r;
switch (filetype) {
case S_IFREG:
if (FLAGS_SET(flags, TAR_OCI_WHITEOUTS)) {
if (streq(e, ".wh..wh..opq")) {
r = archive_unpack_opaque(a, entry, parent_fd, empty_to_root(parent_path));
if (r < 0)
return r;
/* NB: this does not create an inode! */
break;
}
const char *w = startswith(e, ".wh.");
if (w) {
if (!filename_is_valid(w))
return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
"Invalid whiteout file entry '%s', refusing.", e);
r = archive_unpack_whiteout(a, entry, parent_fd, empty_to_root(parent_path), w, j);
if (r < 0)
return r;
/* NB: this does not create an inode! */
break;
}
}
fd = archive_unpack_regular(a, entry, parent_fd, e, j, fflags);
if (fd < 0)
return fd;
break;
case S_IFDIR:
fd = archive_unpack_directory(a, entry, parent_fd, e, j, fflags);
if (fd < 0)
return fd;
break;
case S_IFLNK:
fd = archive_unpack_symlink(a, entry, parent_fd, e, j);
if (fd < 0)
return fd;
break;
case S_IFCHR:
case S_IFBLK:
case S_IFIFO:
case S_IFSOCK:
fd = archive_unpack_special_inode(a, entry, parent_fd, e, j, filetype);
if (fd < 0)
return fd;
break;
default:
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Unexpected file type %i of '%s', refusing.",
(int) filetype, j);
}
} else {
/* This is some intermediary node in the path that we haven't opened yet. Create it with default attributes */
fd = open_mkdir_at(parent_fd, e, O_CLOEXEC, 0700);
if (fd < 0)
return log_error_errno(fd, "Failed to create directory '%s': %m", j);
filetype = S_IFDIR;
}
/* Now store a reference to the inode we just created in our stack array. Note that
* we have not applied file ownership, access mode, mtime here, we'll do that only
* when we are finished with the inode, since we have to apply them *after* we are
* fully done with the inode (i.e. after creating further inodes inside of dir inodes
* for example), due to permission problems this might create or that the mtime
* changes we do might still be affected by our changes. */
if (fd >= 0) {
open_inodes[n_open_inodes++] = (OpenInode) {
.fd = TAKE_FD(fd),
.path = TAKE_PTR(j),
.filetype = filetype,
.mode = mode,
.mtime = mtime,
.uid = uid,
.gid = gid,
.fflags = fflags,
.acl_access = TAKE_PTR(acl_access),
.acl_default = TAKE_PTR(acl_default),
.xattr = TAKE_PTR(xa),
.n_xattr = n_xa,
};
n_xa = 0;
}
}
}
r = open_inode_finalize_many(&open_inodes, &n_open_inodes);
if (r < 0)
return r;
return 0;
}
int tar_hardlink_db_new(void) {
/* Creates a tmpfs superblock to store our hardlink db in. We can do this if we run in our own
* userns, or if we are privileged. This is preferable, since it means the db is cleaned up
* automatically once we are done. Moreover, since this is a new superblock owned by us, we do not
* need to set up any uid mapping shenanigans */
_cleanup_close_ int superblock_fd = fsopen("tmpfs", FSOPEN_CLOEXEC);
if (superblock_fd < 0)
return log_debug_errno(errno, "Failed to allocate tmpfs superblock: %m");
(void) fsconfig(superblock_fd, FSCONFIG_SET_STRING, "source", "hardlink", /* aux= */ 0);
(void) fsconfig(superblock_fd, FSCONFIG_SET_STRING, "mode", "0700", /* aux= */ 0);
if (fsconfig(superblock_fd, FSCONFIG_CMD_CREATE, /* key= */ NULL, /* value= */ NULL, /* aux= */ 0) < 0)
return log_debug_errno(errno, "Failed to finalize superblock: %m");
_cleanup_close_ int mount_fd = fsmount(superblock_fd, FSMOUNT_CLOEXEC, MS_NODEV|MS_NOEXEC|MS_NOSUID);
if (mount_fd < 0)
return log_debug_errno(errno, "Failed to turn tmpfs superblock into mount: %m");
return TAKE_FD(mount_fd);
}
struct make_archive_data {
struct archive *archive;
TarFlags flags;
int hardlink_db_fd;
bool hardlink_db_fd_owned; /* Whether we allocated the db ourselves, i.e. whether it is ours to close */
char *hardlink_db_path;
int have_unique_mount_id;
};
static int filter_item(
int inode_fd,
const struct statx *sx,
const char *path) {
mode_t m;
int r;
assert(inode_fd >= 0);
assert(sx);
assert(path);
if (FLAGS_SET(sx->stx_mask, STATX_TYPE))
m = sx->stx_mode;
else {
struct stat st;
r = RET_NERRNO(fstat(inode_fd, &st));
if (r < 0)
return log_error_errno(r, "Failed to stat '%s': %m", path);
m = st.st_mode;
}
/* Filter out sockets, fifos, and weird misc fds such as eventfds() that have no inode type. */
if (IN_SET(m & S_IFMT, S_IFSOCK, S_IFIFO, 0)) {
log_debug("Skipping '%s' (%s).", path, inode_type_to_string(m) ?: "unknown");
return false;
}
return true;
}
static int hardlink_lookup(
struct make_archive_data *d,
int inode_fd,
const struct statx *sx,
const char *path,
char **ret) {
_cleanup_free_ struct file_handle *handle = NULL;
_cleanup_free_ char *m = NULL, *n = NULL;
int r;
assert(d);
assert(inode_fd >= 0);
assert(sx);
assert(ret);
/* If we know the hardlink count, and it's 1, then don't bother */
if (FLAGS_SET(sx->stx_mask, STATX_NLINK) && sx->stx_nlink == 1)
goto bypass;
/* If this is a directory, then don't bother */
if (FLAGS_SET(sx->stx_mask, STATX_TYPE) && !inode_type_can_hardlink(sx->stx_mode))
goto bypass;
uint64_t unique_mnt_id;
int mnt_id;
r = name_to_handle_at_try_fid(inode_fd, /* path= */ NULL,
&handle,
d->have_unique_mount_id <= 0 ? &mnt_id : NULL,
d->have_unique_mount_id != 0 ? &unique_mnt_id : NULL,
/* flags= */ AT_EMPTY_PATH);
if (r < 0)
return log_error_errno(r, "Failed to get file handle of file: %m");
if (d->have_unique_mount_id < 0)
d->have_unique_mount_id = r > 0;
else
assert(d->have_unique_mount_id == (r > 0));
m = sha256_direct_hex(handle->f_handle, handle->handle_bytes);
if (!m)
return log_oom();
if (d->have_unique_mount_id)
r = asprintf(&n, "%" PRIu64 ":%i:%s", unique_mnt_id, handle->handle_type, m);
else
r = asprintf(&n, "%i:%i:%s", mnt_id, handle->handle_type, m);
if (r < 0)
return log_oom();
if (d->hardlink_db_fd < 0) {
assert(!d->hardlink_db_path);
/* We first try to create our own superblock, which works if we are in a userns, and which
* doesn't require explicit clean-up */
d->hardlink_db_fd = tar_hardlink_db_new();
if (d->hardlink_db_fd < 0) {
log_debug_errno(d->hardlink_db_fd, "Failed to allocate tmpfs superblock for hardlink db, falling back to temporary directory: %m");
const char *vt;
r = var_tmp_dir(&vt);
if (r < 0)
return log_error_errno(r, "Failed to determine /var/tmp/ directory: %m");
_cleanup_free_ char *j = path_join(vt, "make-tar-XXXXXX");
if (!j)
return log_oom();
d->hardlink_db_fd = mkdtemp_open(j, /* flags= */ 0, &d->hardlink_db_path);
if (d->hardlink_db_fd < 0)
return log_error_errno(d->hardlink_db_fd, "Failed to make hardlink database directory: %m");
}
d->hardlink_db_fd_owned = true;
} else {
_cleanup_free_ char *p = NULL;
r = readlinkat_malloc(d->hardlink_db_fd, n, &p);
if (r >= 0) {
/* Found previous hit! */
log_debug("hardlinkdb: found %s → %s", n, p);
*ret = TAKE_PTR(p);
return 1;
}
if (r != -ENOENT)
return log_error_errno(r, "Failed to read symlink '%s': %m", n);
}
/* Store information about this inode */
if (symlinkat(path, d->hardlink_db_fd, n) < 0)
return log_error_errno(errno, "Failed to create symlink '%s' → '%s': %m", n, path);
log_debug("hardlinkdb: created %s → %s", n, path);
bypass:
*ret = NULL;
return 0;
}
static int archive_generate_sparse(struct archive_entry *entry, int fd) {
assert(entry);
assert(fd);
off_t c = 0;
for (;;) {
/* Look for the next hole */
off_t h = lseek(fd, c, SEEK_HOLE);
if (h < 0) {
if (errno != ENXIO)
return log_error_errno(errno, "Failed to issue SEEK_HOLE: %m");
/* If errno == ENXIO, that means we've reached the final data of the file and
* that data isn't followed by anything more */
/* Figure out where the end of the file is */
off_t e = lseek(fd, 0, SEEK_END);
if (e < 0)
return log_error_errno(errno, "Failed to issue SEEK_END: %m");
/* Generate sparse entry for final block */
if (e > c && c != 0) {
log_debug("final sparse block %" PRIu64 "…%" PRIu64, (uint64_t) c, (uint64_t) e);
sym_archive_entry_sparse_add_entry(entry, c, e - c);
}
break;
}
if (h > c) {
log_debug("inner sparse block %" PRIu64 "…%" PRIu64 " (%" PRIu64 ")", (uint64_t) c, (uint64_t) h, (uint64_t) h - (uint64_t) c);
sym_archive_entry_sparse_add_entry(entry, c, h - c);
}
/* Now look for the next data after the hole */
c = lseek(fd, h, SEEK_DATA);
if (c < 0) {
if (errno != ENXIO)
return log_error_errno(errno, "Failed to issue SEEK_DATA: %m");
/* No data anymore */
break;
}
}
if (lseek(fd, 0, SEEK_SET) < 0)
return log_error_errno(errno, "Failed to reset seek offset: %m");
return 0;
}
#if HAVE_ACL
static int archive_write_acl(
struct archive_entry *entry,
acl_type_t ntype,
acl_t acl,
TarFlags flags) {
int r;
assert(entry);
assert(acl);
int type;
if (ntype == ACL_TYPE_ACCESS)
type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS;
else if (ntype == ACL_TYPE_DEFAULT)
type = ARCHIVE_ENTRY_ACL_TYPE_DEFAULT;
else
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unexpected ACL type");
acl_entry_t e;
for (r = sym_acl_get_entry(acl, ACL_FIRST_ENTRY, &e);
;
r = sym_acl_get_entry(acl, ACL_NEXT_ENTRY, &e)) {
if (r < 0)
return log_error_errno(errno, "Failed to get ACL entry: %m");
if (r == 0)
break;
acl_tag_t ntag;
if (sym_acl_get_tag_type(e, &ntag) < 0)
return log_error_errno(errno, "Failed to get ACL entry tag: %m");
int tag = libacl_tag_to_libarchive_acl_tag(ntag);
if (tag == _ARCHIVE_ENTRY_ACL_UNDEFINED)
continue;
id_t qualifier = UID_INVALID;
if (IN_SET(ntag, ACL_USER, ACL_GROUP)) {
id_t *q = sym_acl_get_qualifier(e);
if (!q)
return log_error_errno(errno, "Failed to get ACL entry qualifier: %m");
qualifier = *q;
sym_acl_free(q);
/* Suppress invalid UIDs or those that shall be squashed */
if (!uid_is_valid(qualifier) ||
(FLAGS_SET(flags, TAR_SQUASH_UIDS_ABOVE_64K) && qualifier >= NSRESOURCE_UIDS_64K))
continue;
}
acl_permset_t p;
if (sym_acl_get_permset(e, &p) < 0)
return log_error_errno(errno, "Failed to get ACL entry permission set: %m");
int permset = 0;
r = sym_acl_get_perm(p, ACL_READ);
if (r < 0)
return log_error_errno(errno, "Failed to get ACL entry read bit: %m");
SET_FLAG(permset, ARCHIVE_ENTRY_ACL_READ, r);
r = sym_acl_get_perm(p, ACL_WRITE);
if (r < 0)
return log_error_errno(errno, "Failed to get ACL entry write bit: %m");
SET_FLAG(permset, ARCHIVE_ENTRY_ACL_WRITE, r);
r = sym_acl_get_perm(p, ACL_EXECUTE);
if (r < 0)
return log_error_errno(errno, "Failed to get ACL entry execute bit: %m");
SET_FLAG(permset, ARCHIVE_ENTRY_ACL_EXECUTE, r);
r = sym_archive_entry_acl_add_entry(entry, type, permset, tag, qualifier, /* name= */ NULL);
if (r != ARCHIVE_OK)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Failed to add ACL entry.");
}
return 0;
}
#endif
static int archive_item(
RecurseDirEvent event,
const char *path,
int dir_fd,
int inode_fd,
const struct dirent *de,
const struct statx *sx,
void *userdata) {
struct make_archive_data *d = ASSERT_PTR(userdata);
int r;
assert(path);
if (!IN_SET(event, RECURSE_DIR_ENTER, RECURSE_DIR_ENTRY))
return RECURSE_DIR_CONTINUE;
assert(inode_fd >= 0);
assert(sx);
r = filter_item(inode_fd, sx, path);
if (r < 0)
return r;
if (r == 0)
return RECURSE_DIR_CONTINUE;
log_debug("Archiving '%s'...", path);
_cleanup_(archive_entry_freep) struct archive_entry *entry = NULL;
entry = sym_archive_entry_new();
if (!entry)
return log_oom();
sym_archive_entry_set_pathname(entry, path);
_cleanup_free_ char *hardlink = NULL;
r = hardlink_lookup(d, inode_fd, sx, path, &hardlink);
if (r < 0)
return r;
if (r > 0) {
sym_archive_entry_set_hardlink(entry, hardlink);
if (sym_archive_write_header(d->archive, entry) != ARCHIVE_OK)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Failed to write archive entry header: %s", sym_archive_error_string(d->archive));
return RECURSE_DIR_CONTINUE;
}
assert(FLAGS_SET(sx->stx_mask, STATX_TYPE|STATX_MODE));
sym_archive_entry_set_filetype(entry, sx->stx_mode);
if (!S_ISLNK(sx->stx_mode))
sym_archive_entry_set_perm(entry, sx->stx_mode);
if (FLAGS_SET(sx->stx_mask, STATX_UID))
sym_archive_entry_set_uid(entry, maybe_squash_uid(sx->stx_uid, d->flags));
if (FLAGS_SET(sx->stx_mask, STATX_GID))
sym_archive_entry_set_gid(entry, maybe_squash_gid(sx->stx_gid, d->flags));
if (S_ISREG(sx->stx_mode)) {
if (!FLAGS_SET(sx->stx_mask, STATX_SIZE))
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Unable to determine file size of '%s'.", path);
sym_archive_entry_set_size(entry, sx->stx_size);
}
if (S_ISCHR(sx->stx_mode) || S_ISBLK(sx->stx_mode)) {
sym_archive_entry_set_rdevmajor(entry, sx->stx_rdev_major);
sym_archive_entry_set_rdevminor(entry, sx->stx_rdev_minor);
}
/* We care about a modicum of reproducibility here, hence we don't save atime/btime here */
if (FLAGS_SET(sx->stx_mask, STATX_MTIME))
sym_archive_entry_set_mtime(entry, sx->stx_mtime.tv_sec, sx->stx_mtime.tv_nsec);
if (FLAGS_SET(sx->stx_mask, STATX_CTIME))
sym_archive_entry_set_ctime(entry, sx->stx_ctime.tv_sec, sx->stx_ctime.tv_nsec);
if (S_ISLNK(sx->stx_mode)) {
_cleanup_free_ char *s = NULL;
assert(dir_fd >= 0);
assert(de);
r = readlinkat_malloc(dir_fd, de->d_name, &s);
if (r < 0)
return log_error_errno(r, "Failed to read symlink target of '%s': %m", path);
sym_archive_entry_set_symlink(entry, s);
}
#if HAVE_ACL
if (inode_type_can_acl(sx->stx_mode)) {
r = dlopen_libacl(LOG_DEBUG);
if (r >= 0) {
r = sym_acl_extended_file(FORMAT_PROC_FD_PATH(inode_fd));
if (r < 0 && !ERRNO_IS_NOT_SUPPORTED(errno))
return log_error_errno(errno, "Failed check if '%s' has ACLs: %m", path);
if (r > 0) {
_cleanup_(acl_freep) acl_t acl = NULL;
acl = sym_acl_get_file(FORMAT_PROC_FD_PATH(inode_fd), ACL_TYPE_ACCESS);
if (!acl)
return log_error_errno(errno, "Failed read access ACLs of '%s': %m", path);
archive_write_acl(entry, ACL_TYPE_ACCESS, acl, d->flags);
if (S_ISDIR(sx->stx_mode)) {
sym_acl_free(acl);
acl = sym_acl_get_file(FORMAT_PROC_FD_PATH(inode_fd), ACL_TYPE_DEFAULT);
if (!acl)
return log_error_errno(errno, "Failed to read default ACLs of '%s': %m", path);
archive_write_acl(entry, ACL_TYPE_DEFAULT, acl, d->flags);
}
}
}
}
#endif
_cleanup_free_ char *xattrs = NULL;
r = flistxattr_malloc(inode_fd, &xattrs);
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r) && r != -ENODATA)
return log_error_errno(r, "Failed to read xattr list of '%s': %m", path);
NULSTR_FOREACH(xa, xattrs) {
_cleanup_free_ char *buf = NULL;
size_t size;
if (xattr_is_acl(xa))
continue;
if (!FLAGS_SET(d->flags, TAR_SELINUX) && xattr_is_selinux(xa))
continue;
r = fgetxattr_malloc(inode_fd, xa, &buf, &size);
if (r == -ENODATA) /* deleted by now? ignore... */
continue;
if (r < 0)
return log_error_errno(r, "Failed to read xattr '%s' of '%s': %m", xa, path);
sym_archive_entry_xattr_add_entry(entry, xa, buf, size);
}
_cleanup_close_ int data_fd = -EBADF;
if (S_ISREG(sx->stx_mode)) {
/* Convert the O_PATH fd into a proper fd */
data_fd = fd_reopen(inode_fd, O_RDONLY|O_CLOEXEC);
if (data_fd < 0)
return log_error_errno(data_fd, "Failed to open '%s': %m", path);
r = archive_generate_sparse(entry, data_fd);
if (r < 0)
return r;
}
if (inode_type_can_chattr(sx->stx_mode)) {
unsigned f = 0;
r = read_attr_fd(data_fd >= 0 ? data_fd : inode_fd, &f);
if (r < 0 && !ERRNO_IS_NEG_NOT_SUPPORTED(r))
return log_error_errno(r, "Failed to read file flags of '%s': %m", path);
f &= CHATTR_TAR_FL;
if (f != 0)
sym_archive_entry_set_fflags(entry, f, /* clear= */ 0);
}
if (sym_archive_write_header(d->archive, entry) != ARCHIVE_OK)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Failed to write archive entry header: %s",
sym_archive_error_string(d->archive));
if (S_ISREG(sx->stx_mode)) {
assert(data_fd >= 0);
for (;;) {
char buffer[64*1024];
ssize_t l;
l = read(data_fd, buffer, sizeof(buffer));
if (l < 0)
return log_error_errno(errno, "Failed to read '%s': %m", path);
if (l == 0)
break;
la_ssize_t k;
k = sym_archive_write_data(d->archive, buffer, l);
if (k < 0)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Failed to write archive data: %s",
sym_archive_error_string(d->archive));
}
}
return RECURSE_DIR_CONTINUE;
}
static void make_archive_data_done(struct make_archive_data *d) {
assert(d);
/* Only tear down a db we allocated ourselves: rm_rf_children() consumes the fd it is given, and one
* handed to us by the caller is theirs to close. */
if (d->hardlink_db_fd_owned && d->hardlink_db_fd >= 0)
(void) rm_rf_children(TAKE_FD(d->hardlink_db_fd), REMOVE_PHYSICAL, /* root_dev= */ NULL);
unlink_and_free(d->hardlink_db_path);
}
/* The hardlink db fd is borrowed, not consumed: tar_c() neither closes it nor empties it again, so whoever
* passed it in stays responsible for both. */
int tar_c(int tree_fd, int output_fd, const char *filename, int hardlink_db_fd, TarFlags flags) {
int r;
assert(tree_fd >= 0);
assert(output_fd >= 0);
r = dlopen_libarchive(LOG_DEBUG);
if (r < 0)
return r;
_cleanup_(archive_write_freep) struct archive *a = sym_archive_write_new();
if (!a)
return log_oom();
if (filename)
r = sym_archive_write_set_format_filter_by_ext(a, filename);
else
r = sym_archive_write_set_format_pax(a);
if (r != ARCHIVE_OK)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Failed to set libarchive output format: %s", sym_archive_error_string(a));
r = sym_archive_write_open_fd(a, output_fd);
if (r != ARCHIVE_OK)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Failed to set libarchive output file: %s", sym_archive_error_string(a));
_cleanup_(make_archive_data_done) struct make_archive_data data = {
.archive = a,
.flags = flags,
.hardlink_db_fd = hardlink_db_fd,
.have_unique_mount_id = -1,
};
r = recurse_dir(tree_fd,
".",
STATX_TYPE|STATX_MODE|STATX_UID|STATX_GID|STATX_SIZE|STATX_ATIME|STATX_CTIME,
UINT_MAX,
RECURSE_DIR_SORT|RECURSE_DIR_INODE_FD|RECURSE_DIR_TOPLEVEL,
archive_item,
&data);
if (r < 0)
return log_error_errno(r, "Failed to make archive: %m");
r = sym_archive_write_close(a);
if (r != ARCHIVE_OK)
return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
"Unable to finish writing archive: %s", sym_archive_error_string(a));
return 0;
}
#else
int tar_x(int input_fd, int tree_fd, TarFlags flags) {
assert(input_fd >= 0);
assert(tree_fd >= 0);
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libarchive support not available.");
}
int tar_c(int tree_fd, int output_fd, const char *filename, int hardlink_db_fd, TarFlags flags) {
assert(tree_fd >= 0);
assert(output_fd >= 0);
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libarchive support not available.");
}
int tar_hardlink_db_new(void) {
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "libarchive support not available.");
}
#endif