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 free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
31 #include <sys/types.h>
34 #include "wimlib/apply.h"
35 #include "wimlib/assert.h"
36 #include "wimlib/blob_table.h"
37 #include "wimlib/dentry.h"
38 #include "wimlib/error.h"
39 #include "wimlib/file_io.h"
40 #include "wimlib/reparse.h"
41 #include "wimlib/timestamp.h"
42 #include "wimlib/unix_data.h"
44 /* We don't require O_NOFOLLOW, but the advantage of having it is that if we
45 * need to extract a file to a location at which there exists a symbolic link,
46 * open(..., O_NOFOLLOW | ...) recognizes the symbolic link rather than
47 * following it and creating the file somewhere else. (Equivalent to
48 * FILE_OPEN_REPARSE_POINT on Windows.) */
54 unix_get_supported_features(const char *target,
55 struct wim_features *supported_features)
57 supported_features->hard_links = 1;
58 supported_features->symlink_reparse_points = 1;
59 supported_features->unix_data = 1;
60 supported_features->timestamps = 1;
61 supported_features->case_sensitive_filenames = 1;
65 #define NUM_PATHBUFS 2 /* We need 2 when creating hard links */
67 struct unix_apply_ctx {
68 /* Extract flags, the pointer to the WIMStruct, etc. */
69 struct apply_ctx common;
71 /* Buffers for building extraction paths (allocated). */
72 char *pathbufs[NUM_PATHBUFS];
74 /* Index of next pathbuf to use */
75 unsigned which_pathbuf;
77 /* Currently open file descriptors for extraction */
78 struct filedes open_fds[MAX_OPEN_FILES];
80 /* Number of currently open file descriptors in open_fds, starting from
81 * the beginning of the array. */
82 unsigned num_open_fds;
84 /* Buffer for reading reparse point data into memory */
85 u8 reparse_data[REPARSE_DATA_MAX_SIZE];
87 /* Pointer to the next byte in @reparse_data to fill */
90 /* Absolute path to the target directory (allocated buffer). Only set
91 * if needed for absolute symbolic link fixups. */
94 /* Number of characters in target_abspath. */
95 size_t target_abspath_nchars;
97 /* Number of special files we couldn't create due to EPERM */
98 unsigned long num_special_files_ignored;
101 /* Returns the number of characters needed to represent the path to the
102 * specified @dentry when extracted, not including the null terminator or the
103 * path to the target directory itself. */
105 unix_dentry_path_length(const struct wim_dentry *dentry)
108 const struct wim_dentry *d;
112 len += d->d_extraction_name_nchars + 1;
114 } while (!dentry_is_root(d) && will_extract_dentry(d));
119 /* Returns the maximum number of characters needed to represent the path to any
120 * dentry in @dentry_list when extracted, including the null terminator and the
121 * path to the target directory itself. */
123 unix_compute_path_max(const struct list_head *dentry_list,
124 const struct unix_apply_ctx *ctx)
128 const struct wim_dentry *dentry;
130 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
131 len = unix_dentry_path_length(dentry);
136 /* Account for target and null terminator. */
137 return ctx->common.target_nchars + max + 1;
140 /* Builds and returns the filesystem path to which to extract @dentry.
141 * This cycles through NUM_PATHBUFS different buffers. */
143 unix_build_extraction_path(const struct wim_dentry *dentry,
144 struct unix_apply_ctx *ctx)
148 const struct wim_dentry *d;
150 pathbuf = ctx->pathbufs[ctx->which_pathbuf];
151 ctx->which_pathbuf = (ctx->which_pathbuf + 1) % NUM_PATHBUFS;
153 p = &pathbuf[ctx->common.target_nchars +
154 unix_dentry_path_length(dentry)];
158 p -= d->d_extraction_name_nchars;
159 memcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
162 } while (!dentry_is_root(d) && will_extract_dentry(d));
167 /* This causes the next call to unix_build_extraction_path() to use the same
168 * path buffer as the previous call. */
170 unix_reuse_pathbuf(struct unix_apply_ctx *ctx)
172 ctx->which_pathbuf = (ctx->which_pathbuf - 1) % NUM_PATHBUFS;
175 /* Builds and returns the filesystem path to which to extract an unspecified
176 * alias of the @inode. This cycles through NUM_PATHBUFS different buffers. */
178 unix_build_inode_extraction_path(const struct wim_inode *inode,
179 struct unix_apply_ctx *ctx)
181 return unix_build_extraction_path(inode_first_extraction_dentry(inode), ctx);
184 /* Sets the timestamps on a file being extracted.
186 * Either @fd or @path must be specified (not -1 and not NULL, respectively).
189 unix_set_timestamps(int fd, const char *path, u64 atime, u64 mtime)
192 struct timespec times[2];
194 times[0] = wim_timestamp_to_timespec(atime);
195 times[1] = wim_timestamp_to_timespec(mtime);
199 if (fd >= 0 && !futimens(fd, times))
202 #ifdef HAVE_UTIMENSAT
203 if (fd < 0 && !utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW))
207 return WIMLIB_ERR_SET_TIMESTAMPS;
210 struct timeval times[2];
212 times[0] = wim_timestamp_to_timeval(atime);
213 times[1] = wim_timestamp_to_timeval(mtime);
215 if (fd >= 0 && !futimes(fd, times))
217 if (fd < 0 && !lutimes(path, times))
219 return WIMLIB_ERR_SET_TIMESTAMPS;
224 unix_set_owner_and_group(int fd, const char *path, uid_t uid, gid_t gid)
226 if (fd >= 0 && !fchown(fd, uid, gid))
228 if (fd < 0 && !lchown(path, uid, gid))
230 return WIMLIB_ERR_SET_SECURITY;
234 unix_set_mode(int fd, const char *path, mode_t mode)
236 if (fd >= 0 && !fchmod(fd, mode))
238 if (fd < 0 && !chmod(path, mode))
240 return WIMLIB_ERR_SET_SECURITY;
244 * Set metadata on an extracted file.
246 * @fd is an open file descriptor to the extracted file, or -1. @path is the
247 * path to the extracted file, or NULL. If valid, this function uses @fd.
248 * Otherwise, if valid, it uses @path. Otherwise, it calculates the path to one
249 * alias of the extracted file and uses it.
252 unix_set_metadata(int fd, const struct wim_inode *inode,
253 const char *path, struct unix_apply_ctx *ctx)
256 struct wimlib_unix_data unix_data;
259 path = unix_build_inode_extraction_path(inode, ctx);
261 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA)
262 && inode_get_unix_data(inode, &unix_data))
264 u32 uid = unix_data.uid;
265 u32 gid = unix_data.gid;
266 u32 mode = unix_data.mode;
268 ret = unix_set_owner_and_group(fd, path, uid, gid);
271 path = unix_build_inode_extraction_path(inode, ctx);
272 if (ctx->common.extract_flags &
273 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
275 ERROR_WITH_ERRNO("Can't set uid=%"PRIu32" and "
276 "gid=%"PRIu32" on \"%s\"",
280 WARNING_WITH_ERRNO("Can't set uid=%"PRIu32" and "
281 "gid=%"PRIu32" on \"%s\"",
287 if (!inode_is_symlink(inode))
288 ret = unix_set_mode(fd, path, mode);
291 path = unix_build_inode_extraction_path(inode, ctx);
292 if (ctx->common.extract_flags &
293 WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
295 ERROR_WITH_ERRNO("Can't set mode=0%"PRIo32" "
296 "on \"%s\"", mode, path);
299 WARNING_WITH_ERRNO("Can't set mode=0%"PRIo32" "
300 "on \"%s\"", mode, path);
305 ret = unix_set_timestamps(fd, path,
306 inode->i_last_access_time,
307 inode->i_last_write_time);
310 path = unix_build_inode_extraction_path(inode, ctx);
311 if (ctx->common.extract_flags &
312 WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS)
314 ERROR_WITH_ERRNO("Can't set timestamps on \"%s\"", path);
317 WARNING_WITH_ERRNO("Can't set timestamps on \"%s\"", path);
323 /* Extract all needed aliases of the @inode, where one alias, corresponding to
324 * @first_dentry, has already been extracted to @first_path. */
326 unix_create_hardlinks(const struct wim_inode *inode,
327 const struct wim_dentry *first_dentry,
328 const char *first_path, struct unix_apply_ctx *ctx)
330 const struct wim_dentry *dentry;
333 list_for_each_entry(dentry, &inode->i_extraction_aliases,
334 d_extraction_alias_node)
336 if (dentry == first_dentry)
339 newpath = unix_build_extraction_path(dentry, ctx);
341 if (link(first_path, newpath)) {
342 if (errno == EEXIST && !unlink(newpath))
344 ERROR_WITH_ERRNO("Can't create hard link "
345 "\"%s\" => \"%s\"", newpath, first_path);
346 return WIMLIB_ERR_LINK;
348 unix_reuse_pathbuf(ctx);
353 /* If @dentry represents a directory, create it. */
355 unix_create_if_directory(const struct wim_dentry *dentry,
356 struct unix_apply_ctx *ctx)
361 if (!dentry_is_directory(dentry))
364 path = unix_build_extraction_path(dentry, ctx);
365 if (mkdir(path, 0755) &&
366 /* It's okay if the path already exists, as long as it's a
368 !(errno == EEXIST && !lstat(path, &stbuf) && S_ISDIR(stbuf.st_mode)))
370 ERROR_WITH_ERRNO("Can't create directory \"%s\"", path);
371 return WIMLIB_ERR_MKDIR;
374 return report_file_created(&ctx->common);
377 /* If @dentry represents an empty regular file or a special file, create it, set
378 * its metadata, and create any needed hard links. */
380 unix_extract_if_empty_file(const struct wim_dentry *dentry,
381 struct unix_apply_ctx *ctx)
383 const struct wim_inode *inode;
384 struct wimlib_unix_data unix_data;
388 inode = dentry->d_inode;
390 /* Extract all aliases only when the "first" comes up. */
391 if (dentry != inode_first_extraction_dentry(inode))
394 /* Is this a directory, a symbolic link, or any type of nonempty file?
396 if (inode_is_directory(inode) || inode_is_symlink(inode) ||
397 inode_get_blob_for_unnamed_data_stream_resolved(inode))
400 /* Recognize special files in UNIX_DATA mode */
401 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
402 inode_get_unix_data(inode, &unix_data) &&
403 !S_ISREG(unix_data.mode))
405 path = unix_build_extraction_path(dentry, ctx);
407 if (mknod(path, unix_data.mode, unix_data.rdev)) {
408 if (errno == EPERM) {
409 WARNING_WITH_ERRNO("Can't create special "
410 "file \"%s\"", path);
411 ctx->num_special_files_ignored++;
414 if (errno == EEXIST && !unlink(path))
416 ERROR_WITH_ERRNO("Can't create special file \"%s\"",
418 return WIMLIB_ERR_MKNOD;
420 /* On special files, we can set timestamps immediately because
421 * we don't need to write any data to them. */
422 ret = unix_set_metadata(-1, inode, path, ctx);
426 path = unix_build_extraction_path(dentry, ctx);
428 fd = open(path, O_TRUNC | O_CREAT | O_WRONLY | O_NOFOLLOW, 0644);
430 if (errno == EEXIST && !unlink(path))
432 ERROR_WITH_ERRNO("Can't create regular file \"%s\"", path);
433 return WIMLIB_ERR_OPEN;
435 /* On empty files, we can set timestamps immediately because we
436 * don't need to write any data to them. */
437 ret = unix_set_metadata(fd, inode, path, ctx);
438 if (close(fd) && !ret) {
439 ERROR_WITH_ERRNO("Error closing \"%s\"", path);
440 ret = WIMLIB_ERR_WRITE;
446 ret = unix_create_hardlinks(inode, dentry, path, ctx);
450 return report_file_created(&ctx->common);
454 unix_create_dirs_and_empty_files(const struct list_head *dentry_list,
455 struct unix_apply_ctx *ctx)
457 const struct wim_dentry *dentry;
460 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
461 ret = unix_create_if_directory(dentry, ctx);
465 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
466 ret = unix_extract_if_empty_file(dentry, ctx);
474 unix_count_dentries(const struct list_head *dentry_list,
475 u64 *dir_count_ret, u64 *empty_file_count_ret)
477 const struct wim_dentry *dentry;
479 u64 empty_file_count = 0;
481 list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
483 const struct wim_inode *inode = dentry->d_inode;
485 if (inode_is_directory(inode))
487 else if ((dentry == inode_first_extraction_dentry(inode)) &&
488 !inode_is_symlink(inode) &&
489 !inode_get_blob_for_unnamed_data_stream_resolved(inode))
493 *dir_count_ret = dir_count;
494 *empty_file_count_ret = empty_file_count;
498 unix_create_symlink(const struct wim_inode *inode, const char *path,
499 const u8 *rpdata, u16 rpdatalen, bool rpfix,
500 const char *apply_dir, size_t apply_dir_nchars)
502 char link_target[REPARSE_DATA_MAX_SIZE];
504 struct blob_descriptor blob_override;
506 blob_set_is_located_in_attached_buffer(&blob_override,
507 (void *)rpdata, rpdatalen);
509 ret = wim_inode_readlink(inode, link_target,
510 sizeof(link_target) - 1, &blob_override);
513 return WIMLIB_ERR_READLINK;
516 link_target[ret] = 0;
518 if (rpfix && link_target[0] == '/') {
520 /* "Fix" the absolute symbolic link by prepending the absolute
521 * path to the target directory. */
523 if (sizeof(link_target) - (ret + 1) < apply_dir_nchars) {
524 errno = ENAMETOOLONG;
525 return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
527 memmove(link_target + apply_dir_nchars, link_target,
529 memcpy(link_target, apply_dir, apply_dir_nchars);
532 if (symlink(link_target, path)) {
533 if (errno == EEXIST && !unlink(path))
535 return WIMLIB_ERR_LINK;
541 unix_cleanup_open_fds(struct unix_apply_ctx *ctx, unsigned offset)
543 for (unsigned i = offset; i < ctx->num_open_fds; i++)
544 filedes_close(&ctx->open_fds[i]);
545 ctx->num_open_fds = 0;
549 unix_begin_extract_blob_instance(const struct blob_descriptor *blob,
550 const struct wim_inode *inode,
551 const struct wim_inode_stream *strm,
552 struct unix_apply_ctx *ctx)
554 const struct wim_dentry *first_dentry;
555 const char *first_path;
558 if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
559 /* On UNIX, symbolic links must be created with symlink(), which
560 * requires that the full link target be available. */
561 if (blob->size > REPARSE_DATA_MAX_SIZE) {
562 ERROR_WITH_ERRNO("Reparse data of \"%s\" has size "
563 "%"PRIu64" bytes (exceeds %u bytes)",
564 inode_first_full_path(inode),
565 blob->size, REPARSE_DATA_MAX_SIZE);
566 return WIMLIB_ERR_INVALID_REPARSE_DATA;
568 ctx->reparse_ptr = ctx->reparse_data;
572 wimlib_assert(stream_is_unnamed_data_stream(strm));
574 /* Unnamed data stream of "regular" file */
576 /* This should be ensured by extract_blob_list() */
577 wimlib_assert(ctx->num_open_fds < MAX_OPEN_FILES);
579 first_dentry = inode_first_extraction_dentry(inode);
580 first_path = unix_build_extraction_path(first_dentry, ctx);
582 fd = open(first_path, O_TRUNC | O_CREAT | O_WRONLY | O_NOFOLLOW, 0644);
584 if (errno == EEXIST && !unlink(first_path))
586 ERROR_WITH_ERRNO("Can't create regular file \"%s\"", first_path);
587 return WIMLIB_ERR_OPEN;
589 filedes_init(&ctx->open_fds[ctx->num_open_fds++], fd);
590 return unix_create_hardlinks(inode, first_dentry, first_path, ctx);
593 /* Called when starting to read a blob for extraction */
595 unix_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
597 struct unix_apply_ctx *ctx = _ctx;
598 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
600 for (u32 i = 0; i < blob->out_refcnt; i++) {
601 int ret = unix_begin_extract_blob_instance(blob,
606 ctx->reparse_ptr = NULL;
607 unix_cleanup_open_fds(ctx, 0);
614 /* Called when the next chunk of a blob has been read for extraction */
616 unix_extract_chunk(const void *chunk, size_t size, void *_ctx)
618 struct unix_apply_ctx *ctx = _ctx;
621 for (unsigned i = 0; i < ctx->num_open_fds; i++) {
622 ret = full_write(&ctx->open_fds[i], chunk, size);
624 ERROR_WITH_ERRNO("Error writing data to filesystem");
628 if (ctx->reparse_ptr)
629 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
633 /* Called when a blob has been fully read for extraction */
635 unix_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
637 struct unix_apply_ctx *ctx = _ctx;
640 const struct blob_extraction_target *targets = blob_extraction_targets(blob);
642 ctx->reparse_ptr = NULL;
645 unix_cleanup_open_fds(ctx, 0);
651 for (u32 i = 0; i < blob->out_refcnt; i++) {
652 struct wim_inode *inode = targets[i].inode;
654 if (inode_is_symlink(inode)) {
655 /* We finally have the symlink data, so we can create
660 rpfix = (ctx->common.extract_flags &
661 WIMLIB_EXTRACT_FLAG_RPFIX) &&
662 !inode->i_not_rpfixed;
664 path = unix_build_inode_extraction_path(inode, ctx);
665 ret = unix_create_symlink(inode, path,
670 ctx->target_abspath_nchars);
672 ERROR_WITH_ERRNO("Can't create symbolic link "
676 ret = unix_set_metadata(-1, inode, path, ctx);
680 /* Set metadata on regular file just before closing it.
682 struct filedes *fd = &ctx->open_fds[j];
684 ret = unix_set_metadata(fd->fd, inode, NULL, ctx);
688 if (filedes_close(fd)) {
689 ERROR_WITH_ERRNO("Error closing \"%s\"",
690 unix_build_inode_extraction_path(inode, ctx));
691 ret = WIMLIB_ERR_WRITE;
697 unix_cleanup_open_fds(ctx, j);
702 unix_set_dir_metadata(struct list_head *dentry_list, struct unix_apply_ctx *ctx)
704 const struct wim_dentry *dentry;
707 list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node) {
708 if (dentry_is_directory(dentry)) {
709 ret = unix_set_metadata(-1, dentry->d_inode, NULL, ctx);
712 ret = report_file_metadata_applied(&ctx->common);
721 unix_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
724 struct unix_apply_ctx *ctx = (struct unix_apply_ctx *)_ctx;
727 u64 empty_file_count;
729 /* Compute the maximum path length that will be needed, then allocate
730 * some path buffers. */
731 path_max = unix_compute_path_max(dentry_list, ctx);
733 for (unsigned i = 0; i < NUM_PATHBUFS; i++) {
734 ctx->pathbufs[i] = MALLOC(path_max);
735 if (!ctx->pathbufs[i]) {
736 ret = WIMLIB_ERR_NOMEM;
739 /* Pre-fill the target in each path buffer. We'll just append
740 * the rest of the paths after this. */
741 memcpy(ctx->pathbufs[i],
742 ctx->common.target, ctx->common.target_nchars);
745 /* Extract directories and empty regular files. Directories are needed
746 * because we can't extract any other files until their directories
747 * exist. Empty files are needed because they don't have
748 * representatives in the blob list. */
750 unix_count_dentries(dentry_list, &dir_count, &empty_file_count);
752 ret = start_file_structure_phase(&ctx->common, dir_count + empty_file_count);
756 ret = unix_create_dirs_and_empty_files(dentry_list, ctx);
760 ret = end_file_structure_phase(&ctx->common);
764 /* Get full path to target if needed for absolute symlink fixups. */
765 if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
766 ctx->common.required_features.symlink_reparse_points)
768 ctx->target_abspath = realpath(ctx->common.target, NULL);
769 if (!ctx->target_abspath) {
770 ret = WIMLIB_ERR_NOMEM;
773 ctx->target_abspath_nchars = strlen(ctx->target_abspath);
776 /* Extract nonempty regular files and symbolic links. */
778 struct read_blob_list_callbacks cbs = {
779 .begin_blob = unix_begin_extract_blob,
780 .begin_blob_ctx = ctx,
781 .consume_chunk = unix_extract_chunk,
782 .consume_chunk_ctx = ctx,
783 .end_blob = unix_end_extract_blob,
786 ret = extract_blob_list(&ctx->common, &cbs);
791 /* Set directory metadata. We do this last so that we get the right
792 * directory timestamps. */
793 ret = start_file_metadata_phase(&ctx->common, dir_count);
797 ret = unix_set_dir_metadata(dentry_list, ctx);
801 ret = end_file_metadata_phase(&ctx->common);
805 if (ctx->num_special_files_ignored) {
806 WARNING("%lu special files were not extracted due to EPERM!",
807 ctx->num_special_files_ignored);
810 for (unsigned i = 0; i < NUM_PATHBUFS; i++)
811 FREE(ctx->pathbufs[i]);
812 FREE(ctx->target_abspath);
816 const struct apply_operations unix_apply_ops = {
818 .get_supported_features = unix_get_supported_features,
819 .extract = unix_extract,
820 .context_size = sizeof(struct unix_apply_ctx),