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