2 * unix_apply.c - Code to apply files from a WIM image on UNIX.
6 * Copyright (C) 2012, 2013, 2014 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/apply.h"
29 #include "wimlib/assert.h"
30 #include "wimlib/dentry.h"
31 #include "wimlib/error.h"
32 #include "wimlib/file_io.h"
33 #include "wimlib/reparse.h"
34 #include "wimlib/timestamp.h"
35 #include "wimlib/unix_data.h"
43 #include <sys/types.h>
46 /* We don't require O_NOFOLLOW, but the advantage of having it is that if we
47 * need to extract a file to a location at which there exists a symbolic link,
48 * open(..., O_NOFOLLOW | ...) recognizes the symbolic link rather than
49 * following it and creating the file somewhere else. (Equivalent to
50 * FILE_OPEN_REPARSE_POINT on Windows.) */
56 unix_get_supported_features(const char *target,
57 struct wim_features *supported_features)
59 supported_features->hard_links = 1;
60 supported_features->symlink_reparse_points = 1;
61 supported_features->unix_data = 1;
62 supported_features->timestamps = 1;
63 supported_features->case_sensitive_filenames = 1;
67 #define NUM_PATHBUFS 2 /* We need 2 when creating hard links */
69 struct unix_apply_ctx {
70 /* Extract flags, the pointer to the WIMStruct, etc. */
71 struct apply_ctx common;
73 /* Buffers for building extraction paths (allocated). */
74 char *pathbufs[NUM_PATHBUFS];
76 /* Index of next pathbuf to use */
77 unsigned which_pathbuf;
79 /* Currently open file descriptors for extraction */
80 struct filedes open_fds[MAX_OPEN_STREAMS];
82 /* Number of currently open file descriptors in open_fds, starting from
83 * the beginning of the array. */
84 unsigned num_open_fds;
86 /* Buffer for reading reparse data streams into memory */
87 u8 reparse_data[REPARSE_DATA_MAX_SIZE];
89 /* Pointer to the next byte in @reparse_data to fill */
92 /* Absolute path to the target directory (allocated buffer). Only set
93 * if needed for absolute symbolic link fixups. */
96 /* Number of characters in target_abspath. */
97 size_t target_abspath_nchars;
99 /* Number of special files we couldn't create due to EPERM */
100 unsigned long num_special_files_ignored;
103 /* Returns the number of characters needed to represent the path to the
104 * specified @dentry when extracted, not including the null terminator or the
105 * path to the target directory itself. */
107 unix_dentry_path_length(const struct wim_dentry *dentry)
110 const struct wim_dentry *d;
114 len += d->d_extraction_name_nchars + 1;
116 } while (!dentry_is_root(d) && will_extract_dentry(d));
121 /* Returns the maximum number of characters needed to represent the path to any
122 * dentry in @dentry_list when extracted, including the null terminator and the
123 * path to the target directory itself. */
125 unix_compute_path_max(const struct list_head *dentry_list,
126 const struct unix_apply_ctx *ctx)
130 const struct wim_dentry *dentry;
132 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
133 len = unix_dentry_path_length(dentry);
138 /* Account for target and null terminator. */
139 return ctx->common.target_nchars + max + 1;
142 /* Builds and returns the filesystem path to which to extract @dentry.
143 * This cycles through NUM_PATHBUFS different buffers. */
145 unix_build_extraction_path(const struct wim_dentry *dentry,
146 struct unix_apply_ctx *ctx)
150 const struct wim_dentry *d;
152 pathbuf = ctx->pathbufs[ctx->which_pathbuf];
153 ctx->which_pathbuf = (ctx->which_pathbuf + 1) % NUM_PATHBUFS;
155 p = &pathbuf[ctx->common.target_nchars +
156 unix_dentry_path_length(dentry)];
160 p -= d->d_extraction_name_nchars;
161 memcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
164 } while (!dentry_is_root(d) && will_extract_dentry(d));
169 /* This causes the next call to unix_build_extraction_path() to use the same
170 * path buffer as the previous call. */
172 unix_reuse_pathbuf(struct unix_apply_ctx *ctx)
174 ctx->which_pathbuf = (ctx->which_pathbuf - 1) % NUM_PATHBUFS;
177 /* Builds and returns the filesystem path to which to extract an unspecified
178 * alias of the @inode. This cycles through NUM_PATHBUFS different buffers. */
180 unix_build_inode_extraction_path(const struct wim_inode *inode,
181 struct unix_apply_ctx *ctx)
183 return unix_build_extraction_path(inode_first_extraction_dentry(inode), ctx);
186 /* Sets the timestamps on a file being extracted.
188 * Either @fd or @path must be specified (not -1 and not NULL, respectively).
191 unix_set_timestamps(int fd, const char *path, u64 atime, u64 mtime)
194 struct timespec times[2];
196 times[0] = wim_timestamp_to_timespec(atime);
197 times[1] = wim_timestamp_to_timespec(mtime);
201 if (fd >= 0 && !futimens(fd, times))
204 #ifdef HAVE_UTIMENSAT
205 if (fd < 0 && !utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW))
209 return WIMLIB_ERR_SET_TIMESTAMPS;
212 struct timeval times[2];
214 times[0] = wim_timestamp_to_timeval(atime);
215 times[1] = wim_timestamp_to_timeval(mtime);
217 if (fd >= 0 && !futimes(fd, times))
219 if (fd < 0 && !lutimes(path, times))
221 return WIMLIB_ERR_SET_TIMESTAMPS;
226 unix_set_owner_and_group(int fd, const char *path, uid_t uid, gid_t gid)
228 if (fd >= 0 && !fchown(fd, uid, gid))
230 if (fd < 0 && !lchown(path, uid, gid))
232 return WIMLIB_ERR_SET_SECURITY;
236 unix_set_mode(int fd, const char *path, mode_t mode)
238 if (fd >= 0 && !fchmod(fd, mode))
240 if (fd < 0 && !chmod(path, mode))
242 return WIMLIB_ERR_SET_SECURITY;
246 * Set metadata on an extracted file.
248 * @fd is an open file descriptor to the extracted file, or -1. @path is the
249 * path to the extracted file, or NULL. If valid, this function uses @fd.
250 * Otherwise, if valid, it uses @path. Otherwise, it calculates the path to one
251 * alias of the extracted file and uses it.
254 unix_set_metadata(int fd, const struct wim_inode *inode,
255 const char *path, struct unix_apply_ctx *ctx)
258 struct wimlib_unix_data unix_data;
261 path = unix_build_inode_extraction_path(inode, ctx);
263 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA)
264 && inode_get_unix_data(inode, &unix_data))
266 u32 uid = unix_data.uid;
267 u32 gid = unix_data.gid;
268 u32 mode = unix_data.mode;
270 ret = unix_set_owner_and_group(fd, path, uid, gid);
273 path = unix_build_inode_extraction_path(inode, ctx);
274 if (ctx->common.extract_flags &
275 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
277 ERROR_WITH_ERRNO("Can't set uid=%"PRIu32" and "
278 "gid=%"PRIu32" on \"%s\"",
282 WARNING_WITH_ERRNO("Can't set uid=%"PRIu32" and "
283 "gid=%"PRIu32" on \"%s\"",
289 if (!inode_is_symlink(inode))
290 ret = unix_set_mode(fd, path, mode);
293 path = unix_build_inode_extraction_path(inode, ctx);
294 if (ctx->common.extract_flags &
295 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
297 ERROR_WITH_ERRNO("Can't set mode=0%"PRIo32" "
298 "on \"%s\"", mode, path);
301 WARNING_WITH_ERRNO("Can't set mode=0%"PRIo32" "
302 "on \"%s\"", mode, path);
307 ret = unix_set_timestamps(fd, path,
308 inode->i_last_access_time,
309 inode->i_last_write_time);
312 path = unix_build_inode_extraction_path(inode, ctx);
313 if (ctx->common.extract_flags &
314 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS)
316 ERROR_WITH_ERRNO("Can't set timestamps on \"%s\"", path);
319 WARNING_WITH_ERRNO("Can't set timestamps on \"%s\"", path);
325 /* Extract all needed aliases of the @inode, where one alias, corresponding to
326 * @first_dentry, has already been extracted to @first_path. */
328 unix_create_hardlinks(const struct wim_inode *inode,
329 const struct wim_dentry *first_dentry,
330 const char *first_path, struct unix_apply_ctx *ctx)
332 const struct wim_dentry *dentry;
335 list_for_each_entry(dentry, &inode->i_extraction_aliases,
336 d_extraction_alias_node)
338 if (dentry == first_dentry)
341 newpath = unix_build_extraction_path(dentry, ctx);
343 if (link(first_path, newpath)) {
344 if (errno == EEXIST && !unlink(newpath))
346 ERROR_WITH_ERRNO("Can't create hard link "
347 "\"%s\" => \"%s\"", newpath, first_path);
348 return WIMLIB_ERR_LINK;
350 unix_reuse_pathbuf(ctx);
355 /* If @dentry represents a directory, create it. */
357 unix_create_if_directory(const struct wim_dentry *dentry,
358 struct unix_apply_ctx *ctx)
363 if (!dentry_is_directory(dentry))
366 path = unix_build_extraction_path(dentry, ctx);
367 if (mkdir(path, 0755) &&
368 /* It's okay if the path already exists, as long as it's a
370 !(errno == EEXIST && !lstat(path, &stbuf) && S_ISDIR(stbuf.st_mode)))
372 ERROR_WITH_ERRNO("Can't create directory \"%s\"", path);
373 return WIMLIB_ERR_MKDIR;
378 /* If @dentry represents an empty regular file or a special file, create it, set
379 * its metadata, and create any needed hard links. */
381 unix_extract_if_empty_file(const struct wim_dentry *dentry,
382 struct unix_apply_ctx *ctx)
384 const struct wim_inode *inode;
385 struct wimlib_unix_data unix_data;
389 inode = dentry->d_inode;
391 /* Extract all aliases only when the "first" comes up. */
392 if (dentry != inode_first_extraction_dentry(inode))
395 /* Is this a directory, a symbolic link, or any type of nonempty file?
397 if (inode_is_directory(inode) || inode_is_symlink(inode) ||
398 inode_unnamed_lte_resolved(inode))
401 /* Recognize special files in UNIX_DATA mode */
402 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
403 inode_get_unix_data(inode, &unix_data) &&
404 !S_ISREG(unix_data.mode))
406 path = unix_build_extraction_path(dentry, ctx);
408 if (mknod(path, unix_data.mode, unix_data.rdev)) {
409 if (errno == EPERM) {
410 WARNING_WITH_ERRNO("Can't create special "
411 "file \"%s\"", path);
412 ctx->num_special_files_ignored++;
415 if (errno == EEXIST && !unlink(path))
417 ERROR_WITH_ERRNO("Can't create special file \"%s\"",
419 return WIMLIB_ERR_MKNOD;
421 /* On special files, we can set timestamps immediately because
422 * we don't need to write any data to them. */
423 ret = unix_set_metadata(-1, inode, path, ctx);
427 path = unix_build_extraction_path(dentry, ctx);
429 fd = open(path, O_TRUNC | O_CREAT | O_WRONLY | O_NOFOLLOW, 0644);
431 if (errno == EEXIST && !unlink(path))
433 ERROR_WITH_ERRNO("Can't create regular file \"%s\"", path);
434 return WIMLIB_ERR_OPEN;
436 /* On empty files, we can set timestamps immediately because we
437 * don't need to write any data to them. */
438 ret = unix_set_metadata(fd, inode, path, ctx);
439 if (close(fd) && !ret) {
440 ERROR_WITH_ERRNO("Error closing \"%s\"", path);
441 ret = WIMLIB_ERR_WRITE;
447 return unix_create_hardlinks(inode, dentry, path, ctx);
451 unix_create_dirs_and_empty_files(const struct list_head *dentry_list,
452 struct unix_apply_ctx *ctx)
454 const struct wim_dentry *dentry;
457 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
458 ret = unix_create_if_directory(dentry, ctx);
462 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
463 ret = unix_extract_if_empty_file(dentry, ctx);
471 unix_create_symlink(const struct wim_inode *inode, const char *path,
472 const u8 *rpdata, u16 rpdatalen, bool rpfix,
473 const char *apply_dir, size_t apply_dir_nchars)
475 char link_target[REPARSE_DATA_MAX_SIZE];
477 struct wim_lookup_table_entry lte_override;
479 lte_override.resource_location = RESOURCE_IN_ATTACHED_BUFFER;
480 lte_override.attached_buffer = (void *)rpdata;
481 lte_override.size = rpdatalen;
483 ret = wim_inode_readlink(inode, link_target,
484 sizeof(link_target) - 1, <e_override);
487 return WIMLIB_ERR_READLINK;
490 link_target[ret] = 0;
492 if (rpfix && link_target[0] == '/') {
494 /* "Fix" the absolute symbolic link by prepending the absolute
495 * path to the target directory. */
497 if (sizeof(link_target) - (ret + 1) < apply_dir_nchars) {
498 errno = ENAMETOOLONG;
499 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
501 memmove(link_target + apply_dir_nchars, link_target,
503 memcpy(link_target, apply_dir, apply_dir_nchars);
506 if (symlink(link_target, path)) {
507 if (errno == EEXIST && !unlink(path))
509 return WIMLIB_ERR_LINK;
515 unix_cleanup_open_fds(struct unix_apply_ctx *ctx, unsigned offset)
517 for (unsigned i = offset; i < ctx->num_open_fds; i++)
518 filedes_close(&ctx->open_fds[i]);
519 ctx->num_open_fds = 0;
523 unix_begin_extract_stream_instance(const struct wim_lookup_table_entry *stream,
524 const struct wim_inode *inode,
525 struct unix_apply_ctx *ctx)
527 const struct wim_dentry *first_dentry;
528 const char *first_path;
531 if (inode_is_symlink(inode)) {
532 /* On UNIX, symbolic links must be created with symlink(), which
533 * requires that the full link target be available. */
534 if (stream->size > REPARSE_DATA_MAX_SIZE) {
535 ERROR_WITH_ERRNO("Reparse data of \"%s\" has size "
536 "%"PRIu64" bytes (exceeds %u bytes)",
537 inode_first_full_path(inode),
538 stream->size, REPARSE_DATA_MAX_SIZE);
539 return WIMLIB_ERR_INVALID_REPARSE_DATA;
541 ctx->reparse_ptr = ctx->reparse_data;
545 /* This should be ensured by extract_stream_list() */
546 wimlib_assert(ctx->num_open_fds < MAX_OPEN_STREAMS);
548 first_dentry = inode_first_extraction_dentry(inode);
549 first_path = unix_build_extraction_path(first_dentry, ctx);
551 fd = open(first_path, O_TRUNC | O_CREAT | O_WRONLY | O_NOFOLLOW, 0644);
553 if (errno == EEXIST && !unlink(first_path))
555 ERROR_WITH_ERRNO("Can't create regular file \"%s\"", first_path);
556 return WIMLIB_ERR_OPEN;
558 filedes_init(&ctx->open_fds[ctx->num_open_fds++], fd);
559 return unix_create_hardlinks(inode, first_dentry, first_path, ctx);
562 /* Called when starting to read a single-instance stream for extraction */
564 unix_begin_extract_stream(struct wim_lookup_table_entry *stream,
565 u32 flags, void *_ctx)
567 struct unix_apply_ctx *ctx = _ctx;
568 const struct stream_owner *owners = stream_owners(stream);
571 for (u32 i = 0; i < stream->out_refcnt; i++) {
572 const struct wim_inode *inode = owners[i].inode;
574 ret = unix_begin_extract_stream_instance(stream, inode, ctx);
576 ctx->reparse_ptr = NULL;
577 unix_cleanup_open_fds(ctx, 0);
584 /* Called when the next chunk of a single-instance stream has been read for
587 unix_extract_chunk(const void *chunk, size_t size, void *_ctx)
589 struct unix_apply_ctx *ctx = _ctx;
592 for (unsigned i = 0; i < ctx->num_open_fds; i++) {
593 ret = full_write(&ctx->open_fds[i], chunk, size);
595 ERROR_WITH_ERRNO("Error writing data to filesystem");
599 if (ctx->reparse_ptr)
600 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
604 /* Called when a single-instance stream has been fully read for extraction */
606 unix_end_extract_stream(struct wim_lookup_table_entry *stream, int status,
609 struct unix_apply_ctx *ctx = _ctx;
612 const struct stream_owner *owners = stream_owners(stream);
614 ctx->reparse_ptr = NULL;
617 unix_cleanup_open_fds(ctx, 0);
623 for (u32 i = 0; i < stream->out_refcnt; i++) {
624 struct wim_inode *inode = owners[i].inode;
626 if (inode_is_symlink(inode)) {
627 /* We finally have the symlink data, so we can create
632 rpfix = (ctx->common.extract_flags &
633 WIMLIB_EXTRACT_FLAG_RPFIX) &&
634 !inode->i_not_rpfixed;
636 path = unix_build_inode_extraction_path(inode, ctx);
637 ret = unix_create_symlink(inode, path,
642 ctx->target_abspath_nchars);
644 ERROR_WITH_ERRNO("Can't create symbolic link "
648 ret = unix_set_metadata(-1, inode, path, ctx);
652 /* Set metadata on regular file just before closing it.
654 struct filedes *fd = &ctx->open_fds[j];
656 ret = unix_set_metadata(fd->fd, inode, NULL, ctx);
660 if (filedes_close(fd)) {
661 ERROR_WITH_ERRNO("Error closing \"%s\"",
662 unix_build_inode_extraction_path(inode, ctx));
663 ret = WIMLIB_ERR_WRITE;
669 unix_cleanup_open_fds(ctx, j);
674 unix_set_dir_metadata(struct list_head *dentry_list, struct unix_apply_ctx *ctx)
676 const struct wim_dentry *dentry;
679 list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node) {
680 if (dentry_is_directory(dentry)) {
681 ret = unix_set_metadata(-1, dentry->d_inode, NULL, ctx);
690 unix_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
693 struct unix_apply_ctx *ctx = (struct unix_apply_ctx *)_ctx;
696 /* Compute the maximum path length that will be needed, then allocate
697 * some path buffers. */
698 path_max = unix_compute_path_max(dentry_list, ctx);
700 for (unsigned i = 0; i < NUM_PATHBUFS; i++) {
701 ctx->pathbufs[i] = MALLOC(path_max);
702 if (!ctx->pathbufs[i]) {
703 ret = WIMLIB_ERR_NOMEM;
706 /* Pre-fill the target in each path buffer. We'll just append
707 * the rest of the paths after this. */
708 memcpy(ctx->pathbufs[i],
709 ctx->common.target, ctx->common.target_nchars);
712 /* Extract directories and empty regular files. Directories are needed
713 * because we can't extract any other files until their directories
714 * exist. Empty files are needed because they don't have
715 * representatives in the stream list. */
716 ret = unix_create_dirs_and_empty_files(dentry_list, ctx);
720 /* Get full path to target if needed for absolute symlink fixups. */
721 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
722 ctx->common.required_features.symlink_reparse_points)
724 ctx->target_abspath = realpath(ctx->common.target, NULL);
725 if (!ctx->target_abspath) {
726 ret = WIMLIB_ERR_NOMEM;
729 ctx->target_abspath_nchars = strlen(ctx->target_abspath);
732 /* Extract nonempty regular files and symbolic links. */
734 struct read_stream_list_callbacks cbs = {
735 .begin_stream = unix_begin_extract_stream,
736 .begin_stream_ctx = ctx,
737 .consume_chunk = unix_extract_chunk,
738 .consume_chunk_ctx = ctx,
739 .end_stream = unix_end_extract_stream,
740 .end_stream_ctx = ctx,
742 ret = extract_stream_list(&ctx->common, &cbs);
746 /* Set directory metadata. We do this last so that we get the right
747 * directory timestamps. */
748 ret = unix_set_dir_metadata(dentry_list, ctx);
751 if (ctx->num_special_files_ignored) {
752 WARNING("%lu special files were not extracted due to EPERM!",
753 ctx->num_special_files_ignored);
756 for (unsigned i = 0; i < NUM_PATHBUFS; i++)
757 FREE(ctx->pathbufs[i]);
758 FREE(ctx->target_abspath);
762 const struct apply_operations unix_apply_ops = {
764 .get_supported_features = unix_get_supported_features,
765 .extract = unix_extract,
766 .context_size = sizeof(struct unix_apply_ctx),