]> wimlib.net Git - wimlib/blob - src/unix_apply.c
Support file counts for extract file structure and metadata progress
[wimlib] / src / unix_apply.c
1 /*
2  * unix_apply.c - Code to apply files from a WIM image on UNIX.
3  */
4
5 /*
6  * Copyright (C) 2012, 2013, 2014 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
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)
13  * any later version.
14  *
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
18  * details.
19  *
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/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
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"
36
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <stdlib.h>
41 #include <sys/stat.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <unistd.h>
45
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.)  */
51 #ifndef O_NOFOLLOW
52 #  define O_NOFOLLOW 0
53 #endif
54
55 static int
56 unix_get_supported_features(const char *target,
57                             struct wim_features *supported_features)
58 {
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;
64         return 0;
65 }
66
67 #define NUM_PATHBUFS 2  /* We need 2 when creating hard links  */
68
69 struct unix_apply_ctx {
70         /* Extract flags, the pointer to the WIMStruct, etc.  */
71         struct apply_ctx common;
72
73         /* Buffers for building extraction paths (allocated).  */
74         char *pathbufs[NUM_PATHBUFS];
75
76         /* Index of next pathbuf to use  */
77         unsigned which_pathbuf;
78
79         /* Currently open file descriptors for extraction  */
80         struct filedes open_fds[MAX_OPEN_STREAMS];
81
82         /* Number of currently open file descriptors in open_fds, starting from
83          * the beginning of the array.  */
84         unsigned num_open_fds;
85
86         /* Buffer for reading reparse data streams into memory  */
87         u8 reparse_data[REPARSE_DATA_MAX_SIZE];
88
89         /* Pointer to the next byte in @reparse_data to fill  */
90         u8 *reparse_ptr;
91
92         /* Absolute path to the target directory (allocated buffer).  Only set
93          * if needed for absolute symbolic link fixups.  */
94         char *target_abspath;
95
96         /* Number of characters in target_abspath.  */
97         size_t target_abspath_nchars;
98
99         /* Number of special files we couldn't create due to EPERM  */
100         unsigned long num_special_files_ignored;
101 };
102
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.  */
106 static size_t
107 unix_dentry_path_length(const struct wim_dentry *dentry)
108 {
109         size_t len = 0;
110         const struct wim_dentry *d;
111
112         d = dentry;
113         do {
114                 len += d->d_extraction_name_nchars + 1;
115                 d = d->d_parent;
116         } while (!dentry_is_root(d) && will_extract_dentry(d));
117
118         return len;
119 }
120
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.  */
124 static size_t
125 unix_compute_path_max(const struct list_head *dentry_list,
126                       const struct unix_apply_ctx *ctx)
127 {
128         size_t max = 0;
129         size_t len;
130         const struct wim_dentry *dentry;
131
132         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
133                 len = unix_dentry_path_length(dentry);
134                 if (len > max)
135                         max = len;
136         }
137
138         /* Account for target and null terminator.  */
139         return ctx->common.target_nchars + max + 1;
140 }
141
142 /* Builds and returns the filesystem path to which to extract @dentry.
143  * This cycles through NUM_PATHBUFS different buffers.  */
144 static const char *
145 unix_build_extraction_path(const struct wim_dentry *dentry,
146                            struct unix_apply_ctx *ctx)
147 {
148         char *pathbuf;
149         char *p;
150         const struct wim_dentry *d;
151
152         pathbuf = ctx->pathbufs[ctx->which_pathbuf];
153         ctx->which_pathbuf = (ctx->which_pathbuf + 1) % NUM_PATHBUFS;
154
155         p = &pathbuf[ctx->common.target_nchars +
156                      unix_dentry_path_length(dentry)];
157         *p = '\0';
158         d = dentry;
159         do {
160                 p -= d->d_extraction_name_nchars;
161                 memcpy(p, d->d_extraction_name, d->d_extraction_name_nchars);
162                 *--p = '/';
163                 d = d->d_parent;
164         } while (!dentry_is_root(d) && will_extract_dentry(d));
165
166         return pathbuf;
167 }
168
169 /* This causes the next call to unix_build_extraction_path() to use the same
170  * path buffer as the previous call.  */
171 static void
172 unix_reuse_pathbuf(struct unix_apply_ctx *ctx)
173 {
174         ctx->which_pathbuf = (ctx->which_pathbuf - 1) % NUM_PATHBUFS;
175 }
176
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.  */
179 static const char *
180 unix_build_inode_extraction_path(const struct wim_inode *inode,
181                                  struct unix_apply_ctx *ctx)
182 {
183         return unix_build_extraction_path(inode_first_extraction_dentry(inode), ctx);
184 }
185
186 /* Sets the timestamps on a file being extracted.
187  *
188  * Either @fd or @path must be specified (not -1 and not NULL, respectively).
189  */
190 static int
191 unix_set_timestamps(int fd, const char *path, u64 atime, u64 mtime)
192 {
193         {
194                 struct timespec times[2];
195
196                 times[0] = wim_timestamp_to_timespec(atime);
197                 times[1] = wim_timestamp_to_timespec(mtime);
198
199                 errno = ENOSYS;
200 #ifdef HAVE_FUTIMENS
201                 if (fd >= 0 && !futimens(fd, times))
202                         return 0;
203 #endif
204 #ifdef HAVE_UTIMENSAT
205                 if (fd < 0 && !utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW))
206                         return 0;
207 #endif
208                 if (errno != ENOSYS)
209                         return WIMLIB_ERR_SET_TIMESTAMPS;
210         }
211         {
212                 struct timeval times[2];
213
214                 times[0] = wim_timestamp_to_timeval(atime);
215                 times[1] = wim_timestamp_to_timeval(mtime);
216
217                 if (fd >= 0 && !futimes(fd, times))
218                         return 0;
219                 if (fd < 0 && !lutimes(path, times))
220                         return 0;
221                 return WIMLIB_ERR_SET_TIMESTAMPS;
222         }
223 }
224
225 static int
226 unix_set_owner_and_group(int fd, const char *path, uid_t uid, gid_t gid)
227 {
228         if (fd >= 0 && !fchown(fd, uid, gid))
229                 return 0;
230         if (fd < 0 && !lchown(path, uid, gid))
231                 return 0;
232         return WIMLIB_ERR_SET_SECURITY;
233 }
234
235 static int
236 unix_set_mode(int fd, const char *path, mode_t mode)
237 {
238         if (fd >= 0 && !fchmod(fd, mode))
239                 return 0;
240         if (fd < 0 && !chmod(path, mode))
241                 return 0;
242         return WIMLIB_ERR_SET_SECURITY;
243 }
244
245 /*
246  * Set metadata on an extracted file.
247  *
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.
252  */
253 static int
254 unix_set_metadata(int fd, const struct wim_inode *inode,
255                   const char *path, struct unix_apply_ctx *ctx)
256 {
257         int ret;
258         struct wimlib_unix_data unix_data;
259
260         if (fd < 0 && !path)
261                 path = unix_build_inode_extraction_path(inode, ctx);
262
263         if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA)
264             && inode_get_unix_data(inode, &unix_data))
265         {
266                 u32 uid = unix_data.uid;
267                 u32 gid = unix_data.gid;
268                 u32 mode = unix_data.mode;
269
270                 ret = unix_set_owner_and_group(fd, path, uid, gid);
271                 if (ret) {
272                         if (!path)
273                                 path = unix_build_inode_extraction_path(inode, ctx);
274                         if (ctx->common.extract_flags &
275                             WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
276                         {
277                                 ERROR_WITH_ERRNO("Can't set uid=%"PRIu32" and "
278                                                  "gid=%"PRIu32" on \"%s\"",
279                                                  uid, gid, path);
280                                 return ret;
281                         } else {
282                                 WARNING_WITH_ERRNO("Can't set uid=%"PRIu32" and "
283                                                    "gid=%"PRIu32" on \"%s\"",
284                                                    uid, gid, path);
285                         }
286                 }
287
288                 ret = 0;
289                 if (!inode_is_symlink(inode))
290                         ret = unix_set_mode(fd, path, mode);
291                 if (ret) {
292                         if (!path)
293                                 path = unix_build_inode_extraction_path(inode, ctx);
294                         if (ctx->common.extract_flags &
295                             WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
296                         {
297                                 ERROR_WITH_ERRNO("Can't set mode=0%"PRIo32" "
298                                                  "on \"%s\"", mode, path);
299                                 return ret;
300                         } else {
301                                 WARNING_WITH_ERRNO("Can't set mode=0%"PRIo32" "
302                                                    "on \"%s\"", mode, path);
303                         }
304                 }
305         }
306
307         ret = unix_set_timestamps(fd, path,
308                                   inode->i_last_access_time,
309                                   inode->i_last_write_time);
310         if (ret) {
311                 if (!path)
312                         path = unix_build_inode_extraction_path(inode, ctx);
313                 if (ctx->common.extract_flags &
314                     WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS)
315                 {
316                         ERROR_WITH_ERRNO("Can't set timestamps on \"%s\"", path);
317                         return ret;
318                 } else {
319                         WARNING_WITH_ERRNO("Can't set timestamps on \"%s\"", path);
320                 }
321         }
322         return 0;
323 }
324
325 /* Extract all needed aliases of the @inode, where one alias, corresponding to
326  * @first_dentry, has already been extracted to @first_path.  */
327 static int
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)
331 {
332         const struct wim_dentry *dentry;
333         const char *newpath;
334
335         list_for_each_entry(dentry, &inode->i_extraction_aliases,
336                             d_extraction_alias_node)
337         {
338                 if (dentry == first_dentry)
339                         continue;
340
341                 newpath = unix_build_extraction_path(dentry, ctx);
342         retry_link:
343                 if (link(first_path, newpath)) {
344                         if (errno == EEXIST && !unlink(newpath))
345                                 goto retry_link;
346                         ERROR_WITH_ERRNO("Can't create hard link "
347                                          "\"%s\" => \"%s\"", newpath, first_path);
348                         return WIMLIB_ERR_LINK;
349                 }
350                 unix_reuse_pathbuf(ctx);
351         }
352         return 0;
353 }
354
355 /* If @dentry represents a directory, create it.  */
356 static int
357 unix_create_if_directory(const struct wim_dentry *dentry,
358                          struct unix_apply_ctx *ctx)
359 {
360         const char *path;
361         struct stat stbuf;
362
363         if (!dentry_is_directory(dentry))
364                 return 0;
365
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
369              * directory.  */
370             !(errno == EEXIST && !lstat(path, &stbuf) && S_ISDIR(stbuf.st_mode)))
371         {
372                 ERROR_WITH_ERRNO("Can't create directory \"%s\"", path);
373                 return WIMLIB_ERR_MKDIR;
374         }
375
376         return report_file_created(&ctx->common);
377 }
378
379 /* If @dentry represents an empty regular file or a special file, create it, set
380  * its metadata, and create any needed hard links.  */
381 static int
382 unix_extract_if_empty_file(const struct wim_dentry *dentry,
383                            struct unix_apply_ctx *ctx)
384 {
385         const struct wim_inode *inode;
386         struct wimlib_unix_data unix_data;
387         const char *path;
388         int ret;
389
390         inode = dentry->d_inode;
391
392         /* Extract all aliases only when the "first" comes up.  */
393         if (dentry != inode_first_extraction_dentry(inode))
394                 return 0;
395
396         /* Is this a directory, a symbolic link, or any type of nonempty file?
397          */
398         if (inode_is_directory(inode) || inode_is_symlink(inode) ||
399             inode_unnamed_lte_resolved(inode))
400                 return 0;
401
402         /* Recognize special files in UNIX_DATA mode  */
403         if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
404             inode_get_unix_data(inode, &unix_data) &&
405             !S_ISREG(unix_data.mode))
406         {
407                 path = unix_build_extraction_path(dentry, ctx);
408         retry_mknod:
409                 if (mknod(path, unix_data.mode, unix_data.rdev)) {
410                         if (errno == EPERM) {
411                                 WARNING_WITH_ERRNO("Can't create special "
412                                                    "file \"%s\"", path);
413                                 ctx->num_special_files_ignored++;
414                                 return 0;
415                         }
416                         if (errno == EEXIST && !unlink(path))
417                                 goto retry_mknod;
418                         ERROR_WITH_ERRNO("Can't create special file \"%s\"",
419                                          path);
420                         return WIMLIB_ERR_MKNOD;
421                 }
422                 /* On special files, we can set timestamps immediately because
423                  * we don't need to write any data to them.  */
424                 ret = unix_set_metadata(-1, inode, path, ctx);
425         } else {
426                 int fd;
427
428                 path = unix_build_extraction_path(dentry, ctx);
429         retry_create:
430                 fd = open(path, O_TRUNC | O_CREAT | O_WRONLY | O_NOFOLLOW, 0644);
431                 if (fd < 0) {
432                         if (errno == EEXIST && !unlink(path))
433                                 goto retry_create;
434                         ERROR_WITH_ERRNO("Can't create regular file \"%s\"", path);
435                         return WIMLIB_ERR_OPEN;
436                 }
437                 /* On empty files, we can set timestamps immediately because we
438                  * don't need to write any data to them.  */
439                 ret = unix_set_metadata(fd, inode, path, ctx);
440                 if (close(fd) && !ret) {
441                         ERROR_WITH_ERRNO("Error closing \"%s\"", path);
442                         ret = WIMLIB_ERR_WRITE;
443                 }
444         }
445         if (ret)
446                 return ret;
447
448         ret = unix_create_hardlinks(inode, dentry, path, ctx);
449         if (ret)
450                 return ret;
451
452         return report_file_created(&ctx->common);
453 }
454
455 static int
456 unix_create_dirs_and_empty_files(const struct list_head *dentry_list,
457                                  struct unix_apply_ctx *ctx)
458 {
459         const struct wim_dentry *dentry;
460         int ret;
461
462         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
463                 ret = unix_create_if_directory(dentry, ctx);
464                 if (ret)
465                         return ret;
466         }
467         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
468                 ret = unix_extract_if_empty_file(dentry, ctx);
469                 if (ret)
470                         return ret;
471         }
472         return 0;
473 }
474
475 static void
476 unix_count_dentries(const struct list_head *dentry_list,
477                     uint64_t *dir_count_ret, uint64_t *empty_file_count_ret)
478 {
479         const struct wim_dentry *dentry;
480         uint64_t dir_count = 0;
481         uint64_t empty_file_count = 0;
482
483         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
484
485                 const struct wim_inode *inode = dentry->d_inode;
486
487                 if (inode_is_directory(inode))
488                         dir_count++;
489                 else if ((dentry == inode_first_extraction_dentry(inode)) &&
490                          !inode_unnamed_lte_resolved(inode))
491                         empty_file_count++;
492         }
493
494         *dir_count_ret = dir_count;
495         *empty_file_count_ret = empty_file_count;
496 }
497
498 static int
499 unix_create_symlink(const struct wim_inode *inode, const char *path,
500                     const u8 *rpdata, u16 rpdatalen, bool rpfix,
501                     const char *apply_dir, size_t apply_dir_nchars)
502 {
503         char link_target[REPARSE_DATA_MAX_SIZE];
504         int ret;
505         struct wim_lookup_table_entry lte_override;
506
507         lte_override.resource_location = RESOURCE_IN_ATTACHED_BUFFER;
508         lte_override.attached_buffer = (void *)rpdata;
509         lte_override.size = rpdatalen;
510
511         ret = wim_inode_readlink(inode, link_target,
512                                  sizeof(link_target) - 1, &lte_override);
513         if (ret < 0) {
514                 errno = -ret;
515                 return WIMLIB_ERR_READLINK;
516         }
517
518         link_target[ret] = 0;
519
520         if (rpfix && link_target[0] == '/') {
521
522                 /* "Fix" the absolute symbolic link by prepending the absolute
523                  * path to the target directory.  */
524
525                 if (sizeof(link_target) - (ret + 1) < apply_dir_nchars) {
526                         errno = ENAMETOOLONG;
527                         return WIMLIB_ERR_REPARSE_POINT_FIXUP_FAILED;
528                 }
529                 memmove(link_target + apply_dir_nchars, link_target,
530                         ret + 1);
531                 memcpy(link_target, apply_dir, apply_dir_nchars);
532         }
533 retry_symlink:
534         if (symlink(link_target, path)) {
535                 if (errno == EEXIST && !unlink(path))
536                         goto retry_symlink;
537                 return WIMLIB_ERR_LINK;
538         }
539         return 0;
540 }
541
542 static void
543 unix_cleanup_open_fds(struct unix_apply_ctx *ctx, unsigned offset)
544 {
545         for (unsigned i = offset; i < ctx->num_open_fds; i++)
546                 filedes_close(&ctx->open_fds[i]);
547         ctx->num_open_fds = 0;
548 }
549
550 static int
551 unix_begin_extract_stream_instance(const struct wim_lookup_table_entry *stream,
552                                    const struct wim_inode *inode,
553                                    struct unix_apply_ctx *ctx)
554 {
555         const struct wim_dentry *first_dentry;
556         const char *first_path;
557         int fd;
558
559         if (inode_is_symlink(inode)) {
560                 /* On UNIX, symbolic links must be created with symlink(), which
561                  * requires that the full link target be available.  */
562                 if (stream->size > REPARSE_DATA_MAX_SIZE) {
563                         ERROR_WITH_ERRNO("Reparse data of \"%s\" has size "
564                                          "%"PRIu64" bytes (exceeds %u bytes)",
565                                          inode_first_full_path(inode),
566                                          stream->size, REPARSE_DATA_MAX_SIZE);
567                         return WIMLIB_ERR_INVALID_REPARSE_DATA;
568                 }
569                 ctx->reparse_ptr = ctx->reparse_data;
570                 return 0;
571         }
572
573         /* This should be ensured by extract_stream_list()  */
574         wimlib_assert(ctx->num_open_fds < MAX_OPEN_STREAMS);
575
576         first_dentry = inode_first_extraction_dentry(inode);
577         first_path = unix_build_extraction_path(first_dentry, ctx);
578 retry_create:
579         fd = open(first_path, O_TRUNC | O_CREAT | O_WRONLY | O_NOFOLLOW, 0644);
580         if (fd < 0) {
581                 if (errno == EEXIST && !unlink(first_path))
582                         goto retry_create;
583                 ERROR_WITH_ERRNO("Can't create regular file \"%s\"", first_path);
584                 return WIMLIB_ERR_OPEN;
585         }
586         filedes_init(&ctx->open_fds[ctx->num_open_fds++], fd);
587         return unix_create_hardlinks(inode, first_dentry, first_path, ctx);
588 }
589
590 /* Called when starting to read a single-instance stream for extraction  */
591 static int
592 unix_begin_extract_stream(struct wim_lookup_table_entry *stream, void *_ctx)
593 {
594         struct unix_apply_ctx *ctx = _ctx;
595         const struct stream_owner *owners = stream_owners(stream);
596         int ret;
597
598         for (u32 i = 0; i < stream->out_refcnt; i++) {
599                 const struct wim_inode *inode = owners[i].inode;
600
601                 ret = unix_begin_extract_stream_instance(stream, inode, ctx);
602                 if (ret) {
603                         ctx->reparse_ptr = NULL;
604                         unix_cleanup_open_fds(ctx, 0);
605                         return ret;
606                 }
607         }
608         return 0;
609 }
610
611 /* Called when the next chunk of a single-instance stream has been read for
612  * extraction  */
613 static int
614 unix_extract_chunk(const void *chunk, size_t size, void *_ctx)
615 {
616         struct unix_apply_ctx *ctx = _ctx;
617         int ret;
618
619         for (unsigned i = 0; i < ctx->num_open_fds; i++) {
620                 ret = full_write(&ctx->open_fds[i], chunk, size);
621                 if (ret) {
622                         ERROR_WITH_ERRNO("Error writing data to filesystem");
623                         return ret;
624                 }
625         }
626         if (ctx->reparse_ptr)
627                 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
628         return 0;
629 }
630
631 /* Called when a single-instance stream has been fully read for extraction  */
632 static int
633 unix_end_extract_stream(struct wim_lookup_table_entry *stream, int status,
634                         void *_ctx)
635 {
636         struct unix_apply_ctx *ctx = _ctx;
637         int ret;
638         unsigned j;
639         const struct stream_owner *owners = stream_owners(stream);
640
641         ctx->reparse_ptr = NULL;
642
643         if (status) {
644                 unix_cleanup_open_fds(ctx, 0);
645                 return status;
646         }
647
648         j = 0;
649         ret = 0;
650         for (u32 i = 0; i < stream->out_refcnt; i++) {
651                 struct wim_inode *inode = owners[i].inode;
652
653                 if (inode_is_symlink(inode)) {
654                         /* We finally have the symlink data, so we can create
655                          * the symlink.  */
656                         const char *path;
657                         bool rpfix;
658
659                         rpfix = (ctx->common.extract_flags &
660                                  WIMLIB_EXTRACT_FLAG_RPFIX) &&
661                                         !inode->i_not_rpfixed;
662
663                         path = unix_build_inode_extraction_path(inode, ctx);
664                         ret = unix_create_symlink(inode, path,
665                                                   ctx->reparse_data,
666                                                   stream->size,
667                                                   rpfix,
668                                                   ctx->target_abspath,
669                                                   ctx->target_abspath_nchars);
670                         if (ret) {
671                                 ERROR_WITH_ERRNO("Can't create symbolic link "
672                                                  "\"%s\"", path);
673                                 break;
674                         }
675                         ret = unix_set_metadata(-1, inode, path, ctx);
676                         if (ret)
677                                 break;
678                 } else {
679                         /* Set metadata on regular file just before closing it.
680                          */
681                         struct filedes *fd = &ctx->open_fds[j];
682
683                         ret = unix_set_metadata(fd->fd, inode, NULL, ctx);
684                         if (ret)
685                                 break;
686
687                         if (filedes_close(fd)) {
688                                 ERROR_WITH_ERRNO("Error closing \"%s\"",
689                                                  unix_build_inode_extraction_path(inode, ctx));
690                                 ret = WIMLIB_ERR_WRITE;
691                                 break;
692                         }
693                         j++;
694                 }
695         }
696         unix_cleanup_open_fds(ctx, j);
697         return ret;
698 }
699
700 static int
701 unix_set_dir_metadata(struct list_head *dentry_list, struct unix_apply_ctx *ctx)
702 {
703         const struct wim_dentry *dentry;
704         int ret;
705
706         list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node) {
707                 if (dentry_is_directory(dentry)) {
708                         ret = unix_set_metadata(-1, dentry->d_inode, NULL, ctx);
709                         if (ret)
710                                 return ret;
711                         ret = report_file_metadata_applied(&ctx->common);
712                         if (ret)
713                                 return ret;
714                 }
715         }
716         return 0;
717 }
718
719 static int
720 unix_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
721 {
722         int ret;
723         struct unix_apply_ctx *ctx = (struct unix_apply_ctx *)_ctx;
724         size_t path_max;
725         uint64_t dir_count;
726         uint64_t empty_file_count;
727
728         /* Compute the maximum path length that will be needed, then allocate
729          * some path buffers.  */
730         path_max = unix_compute_path_max(dentry_list, ctx);
731
732         for (unsigned i = 0; i < NUM_PATHBUFS; i++) {
733                 ctx->pathbufs[i] = MALLOC(path_max);
734                 if (!ctx->pathbufs[i]) {
735                         ret = WIMLIB_ERR_NOMEM;
736                         goto out;
737                 }
738                 /* Pre-fill the target in each path buffer.  We'll just append
739                  * the rest of the paths after this.  */
740                 memcpy(ctx->pathbufs[i],
741                        ctx->common.target, ctx->common.target_nchars);
742         }
743
744         /* Extract directories and empty regular files.  Directories are needed
745          * because we can't extract any other files until their directories
746          * exist.  Empty files are needed because they don't have
747          * representatives in the stream list.  */
748
749         unix_count_dentries(dentry_list, &dir_count, &empty_file_count);
750
751         ret = start_file_structure_phase(&ctx->common, dir_count + empty_file_count);
752         if (ret)
753                 goto out;
754
755         ret = unix_create_dirs_and_empty_files(dentry_list, ctx);
756         if (ret)
757                 goto out;
758
759         ret = end_file_structure_phase(&ctx->common);
760         if (ret)
761                 goto out;
762
763         /* Get full path to target if needed for absolute symlink fixups.  */
764         if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
765             ctx->common.required_features.symlink_reparse_points)
766         {
767                 ctx->target_abspath = realpath(ctx->common.target, NULL);
768                 if (!ctx->target_abspath) {
769                         ret = WIMLIB_ERR_NOMEM;
770                         goto out;
771                 }
772                 ctx->target_abspath_nchars = strlen(ctx->target_abspath);
773         }
774
775         /* Extract nonempty regular files and symbolic links.  */
776
777         struct read_stream_list_callbacks cbs = {
778                 .begin_stream      = unix_begin_extract_stream,
779                 .begin_stream_ctx  = ctx,
780                 .consume_chunk     = unix_extract_chunk,
781                 .consume_chunk_ctx = ctx,
782                 .end_stream        = unix_end_extract_stream,
783                 .end_stream_ctx    = ctx,
784         };
785         ret = extract_stream_list(&ctx->common, &cbs);
786         if (ret)
787                 goto out;
788
789
790         /* Set directory metadata.  We do this last so that we get the right
791          * directory timestamps.  */
792         ret = start_file_metadata_phase(&ctx->common, dir_count);
793         if (ret)
794                 goto out;
795
796         ret = unix_set_dir_metadata(dentry_list, ctx);
797         if (ret)
798                 goto out;
799
800         ret = end_file_metadata_phase(&ctx->common);
801         if (ret)
802                 goto out;
803
804         if (ctx->num_special_files_ignored) {
805                 WARNING("%lu special files were not extracted due to EPERM!",
806                         ctx->num_special_files_ignored);
807         }
808 out:
809         for (unsigned i = 0; i < NUM_PATHBUFS; i++)
810                 FREE(ctx->pathbufs[i]);
811         FREE(ctx->target_abspath);
812         return ret;
813 }
814
815 const struct apply_operations unix_apply_ops = {
816         .name                   = "UNIX",
817         .get_supported_features = unix_get_supported_features,
818         .extract                = unix_extract,
819         .context_size           = sizeof(struct unix_apply_ctx),
820 };