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