]> wimlib.net Git - wimlib/blob - src/unix_apply.c
unix_apply.c: extract non-link directory reparse points as directories
[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 /* Should the specified file be extracted as a directory on UNIX?  We extract
185  * the file as a directory if FILE_ATTRIBUTE_DIRECTORY is set and the file does
186  * not have a symlink or junction reparse point.  It *may* have a different type
187  * of reparse point.  */
188 static inline bool
189 should_extract_as_directory(const struct wim_inode *inode)
190 {
191         return (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) &&
192                 !inode_is_symlink(inode);
193 }
194
195 /* Sets the timestamps on a file being extracted.
196  *
197  * Either @fd or @path must be specified (not -1 and not NULL, respectively).
198  */
199 static int
200 unix_set_timestamps(int fd, const char *path, u64 atime, u64 mtime)
201 {
202         {
203                 struct timespec times[2];
204
205                 times[0] = wim_timestamp_to_timespec(atime);
206                 times[1] = wim_timestamp_to_timespec(mtime);
207
208                 errno = ENOSYS;
209 #ifdef HAVE_FUTIMENS
210                 if (fd >= 0 && !futimens(fd, times))
211                         return 0;
212 #endif
213 #ifdef HAVE_UTIMENSAT
214                 if (fd < 0 && !utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW))
215                         return 0;
216 #endif
217                 if (errno != ENOSYS)
218                         return WIMLIB_ERR_SET_TIMESTAMPS;
219         }
220         {
221                 struct timeval times[2];
222
223                 times[0] = wim_timestamp_to_timeval(atime);
224                 times[1] = wim_timestamp_to_timeval(mtime);
225
226                 if (fd >= 0 && !futimes(fd, times))
227                         return 0;
228                 if (fd < 0 && !lutimes(path, times))
229                         return 0;
230                 return WIMLIB_ERR_SET_TIMESTAMPS;
231         }
232 }
233
234 static int
235 unix_set_owner_and_group(int fd, const char *path, uid_t uid, gid_t gid)
236 {
237         if (fd >= 0 && !fchown(fd, uid, gid))
238                 return 0;
239         if (fd < 0 && !lchown(path, uid, gid))
240                 return 0;
241         return WIMLIB_ERR_SET_SECURITY;
242 }
243
244 static int
245 unix_set_mode(int fd, const char *path, mode_t mode)
246 {
247         if (fd >= 0 && !fchmod(fd, mode))
248                 return 0;
249         if (fd < 0 && !chmod(path, mode))
250                 return 0;
251         return WIMLIB_ERR_SET_SECURITY;
252 }
253
254 /*
255  * Set metadata on an extracted file.
256  *
257  * @fd is an open file descriptor to the extracted file, or -1.  @path is the
258  * path to the extracted file, or NULL.  If valid, this function uses @fd.
259  * Otherwise, if valid, it uses @path.  Otherwise, it calculates the path to one
260  * alias of the extracted file and uses it.
261  */
262 static int
263 unix_set_metadata(int fd, const struct wim_inode *inode,
264                   const char *path, struct unix_apply_ctx *ctx)
265 {
266         int ret;
267         struct wimlib_unix_data unix_data;
268
269         if (fd < 0 && !path)
270                 path = unix_build_inode_extraction_path(inode, ctx);
271
272         if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA)
273             && inode_get_unix_data(inode, &unix_data))
274         {
275                 u32 uid = unix_data.uid;
276                 u32 gid = unix_data.gid;
277                 u32 mode = unix_data.mode;
278
279                 ret = unix_set_owner_and_group(fd, path, uid, gid);
280                 if (ret) {
281                         if (!path)
282                                 path = unix_build_inode_extraction_path(inode, ctx);
283                         if (ctx->common.extract_flags &
284                             WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
285                         {
286                                 ERROR_WITH_ERRNO("Can't set uid=%"PRIu32" and "
287                                                  "gid=%"PRIu32" on \"%s\"",
288                                                  uid, gid, path);
289                                 return ret;
290                         } else {
291                                 WARNING_WITH_ERRNO("Can't set uid=%"PRIu32" and "
292                                                    "gid=%"PRIu32" on \"%s\"",
293                                                    uid, gid, path);
294                         }
295                 }
296
297                 ret = 0;
298                 if (!inode_is_symlink(inode))
299                         ret = unix_set_mode(fd, path, mode);
300                 if (ret) {
301                         if (!path)
302                                 path = unix_build_inode_extraction_path(inode, ctx);
303                         if (ctx->common.extract_flags &
304                             WIMLIB_EXTRACT_FLAG_STRICT_ACLS)
305                         {
306                                 ERROR_WITH_ERRNO("Can't set mode=0%"PRIo32" "
307                                                  "on \"%s\"", mode, path);
308                                 return ret;
309                         } else {
310                                 WARNING_WITH_ERRNO("Can't set mode=0%"PRIo32" "
311                                                    "on \"%s\"", mode, path);
312                         }
313                 }
314         }
315
316         ret = unix_set_timestamps(fd, path,
317                                   inode->i_last_access_time,
318                                   inode->i_last_write_time);
319         if (ret) {
320                 if (!path)
321                         path = unix_build_inode_extraction_path(inode, ctx);
322                 if (ctx->common.extract_flags &
323                     WIMLIB_EXTRACT_FLAG_STRICT_TIMESTAMPS)
324                 {
325                         ERROR_WITH_ERRNO("Can't set timestamps on \"%s\"", path);
326                         return ret;
327                 } else {
328                         WARNING_WITH_ERRNO("Can't set timestamps on \"%s\"", path);
329                 }
330         }
331         return 0;
332 }
333
334 /* Extract all needed aliases of the @inode, where one alias, corresponding to
335  * @first_dentry, has already been extracted to @first_path.  */
336 static int
337 unix_create_hardlinks(const struct wim_inode *inode,
338                       const struct wim_dentry *first_dentry,
339                       const char *first_path, struct unix_apply_ctx *ctx)
340 {
341         const struct wim_dentry *dentry;
342         const char *newpath;
343
344         inode_for_each_extraction_alias(dentry, inode) {
345                 if (dentry == first_dentry)
346                         continue;
347
348                 newpath = unix_build_extraction_path(dentry, ctx);
349         retry_link:
350                 if (link(first_path, newpath)) {
351                         if (errno == EEXIST && !unlink(newpath))
352                                 goto retry_link;
353                         ERROR_WITH_ERRNO("Can't create hard link "
354                                          "\"%s\" => \"%s\"", newpath, first_path);
355                         return WIMLIB_ERR_LINK;
356                 }
357                 unix_reuse_pathbuf(ctx);
358         }
359         return 0;
360 }
361
362 /* If @dentry represents a directory, create it.  */
363 static int
364 unix_create_if_directory(const struct wim_dentry *dentry,
365                          struct unix_apply_ctx *ctx)
366 {
367         const char *path;
368         struct stat stbuf;
369
370         if (!should_extract_as_directory(dentry->d_inode))
371                 return 0;
372
373         path = unix_build_extraction_path(dentry, ctx);
374         if (mkdir(path, 0755) &&
375             /* It's okay if the path already exists, as long as it's a
376              * directory.  */
377             !(errno == EEXIST && !lstat(path, &stbuf) && S_ISDIR(stbuf.st_mode)))
378         {
379                 ERROR_WITH_ERRNO("Can't create directory \"%s\"", path);
380                 return WIMLIB_ERR_MKDIR;
381         }
382
383         return report_file_created(&ctx->common);
384 }
385
386 /* If @dentry represents an empty regular file or a special file, create it, set
387  * its metadata, and create any needed hard links.  */
388 static int
389 unix_extract_if_empty_file(const struct wim_dentry *dentry,
390                            struct unix_apply_ctx *ctx)
391 {
392         const struct wim_inode *inode;
393         struct wimlib_unix_data unix_data;
394         const char *path;
395         int ret;
396
397         inode = dentry->d_inode;
398
399         /* Extract all aliases only when the "first" comes up.  */
400         if (dentry != inode_first_extraction_dentry(inode))
401                 return 0;
402
403         /* Is this a directory, a symbolic link, or any type of nonempty file?
404          */
405         if (should_extract_as_directory(inode) || inode_is_symlink(inode) ||
406             inode_get_blob_for_unnamed_data_stream_resolved(inode))
407                 return 0;
408
409         /* Recognize special files in UNIX_DATA mode  */
410         if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) &&
411             inode_get_unix_data(inode, &unix_data) &&
412             !S_ISREG(unix_data.mode))
413         {
414                 path = unix_build_extraction_path(dentry, ctx);
415         retry_mknod:
416                 if (mknod(path, unix_data.mode, unix_data.rdev)) {
417                         if (errno == EPERM) {
418                                 WARNING_WITH_ERRNO("Can't create special "
419                                                    "file \"%s\"", path);
420                                 ctx->num_special_files_ignored++;
421                                 return 0;
422                         }
423                         if (errno == EEXIST && !unlink(path))
424                                 goto retry_mknod;
425                         ERROR_WITH_ERRNO("Can't create special file \"%s\"",
426                                          path);
427                         return WIMLIB_ERR_MKNOD;
428                 }
429                 /* On special files, we can set timestamps immediately because
430                  * we don't need to write any data to them.  */
431                 ret = unix_set_metadata(-1, inode, path, ctx);
432         } else {
433                 int fd;
434
435                 path = unix_build_extraction_path(dentry, ctx);
436         retry_create:
437                 fd = open(path, O_TRUNC | O_CREAT | O_WRONLY | O_NOFOLLOW, 0644);
438                 if (fd < 0) {
439                         if (errno == EEXIST && !unlink(path))
440                                 goto retry_create;
441                         ERROR_WITH_ERRNO("Can't create regular file \"%s\"", path);
442                         return WIMLIB_ERR_OPEN;
443                 }
444                 /* On empty files, we can set timestamps immediately because we
445                  * don't need to write any data to them.  */
446                 ret = unix_set_metadata(fd, inode, path, ctx);
447                 if (close(fd) && !ret) {
448                         ERROR_WITH_ERRNO("Error closing \"%s\"", path);
449                         ret = WIMLIB_ERR_WRITE;
450                 }
451         }
452         if (ret)
453                 return ret;
454
455         ret = unix_create_hardlinks(inode, dentry, path, ctx);
456         if (ret)
457                 return ret;
458
459         return report_file_created(&ctx->common);
460 }
461
462 static int
463 unix_create_dirs_and_empty_files(const struct list_head *dentry_list,
464                                  struct unix_apply_ctx *ctx)
465 {
466         const struct wim_dentry *dentry;
467         int ret;
468
469         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
470                 ret = unix_create_if_directory(dentry, ctx);
471                 if (ret)
472                         return ret;
473         }
474         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
475                 ret = unix_extract_if_empty_file(dentry, ctx);
476                 if (ret)
477                         return ret;
478         }
479         return 0;
480 }
481
482 static void
483 unix_count_dentries(const struct list_head *dentry_list,
484                     u64 *dir_count_ret, u64 *empty_file_count_ret)
485 {
486         const struct wim_dentry *dentry;
487         u64 dir_count = 0;
488         u64 empty_file_count = 0;
489
490         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
491
492                 const struct wim_inode *inode = dentry->d_inode;
493
494                 if (should_extract_as_directory(inode))
495                         dir_count++;
496                 else if ((dentry == inode_first_extraction_dentry(inode)) &&
497                          !inode_is_symlink(inode) &&
498                          !inode_get_blob_for_unnamed_data_stream_resolved(inode))
499                         empty_file_count++;
500         }
501
502         *dir_count_ret = dir_count;
503         *empty_file_count_ret = empty_file_count;
504 }
505
506 static int
507 unix_create_symlink(const struct wim_inode *inode, const char *path,
508                     size_t rpdatalen, struct unix_apply_ctx *ctx)
509 {
510         char target[REPARSE_POINT_MAX_SIZE];
511         struct blob_descriptor blob_override;
512         int ret;
513
514         blob_set_is_located_in_attached_buffer(&blob_override,
515                                                ctx->reparse_data, rpdatalen);
516
517         ret = wim_inode_readlink(inode, target, sizeof(target) - 1,
518                                  &blob_override,
519                                  ctx->target_abspath,
520                                  ctx->target_abspath_nchars);
521         if (unlikely(ret < 0)) {
522                 errno = -ret;
523                 return WIMLIB_ERR_READLINK;
524         }
525         target[ret] = '\0';
526
527 retry_symlink:
528         if (symlink(target, path)) {
529                 if (errno == EEXIST && !unlink(path))
530                         goto retry_symlink;
531                 return WIMLIB_ERR_LINK;
532         }
533         return 0;
534 }
535
536 static void
537 unix_cleanup_open_fds(struct unix_apply_ctx *ctx, unsigned offset)
538 {
539         for (unsigned i = offset; i < ctx->num_open_fds; i++)
540                 filedes_close(&ctx->open_fds[i]);
541         ctx->num_open_fds = 0;
542 }
543
544 static int
545 unix_begin_extract_blob_instance(const struct blob_descriptor *blob,
546                                  const struct wim_inode *inode,
547                                  const struct wim_inode_stream *strm,
548                                  struct unix_apply_ctx *ctx)
549 {
550         const struct wim_dentry *first_dentry;
551         const char *first_path;
552         int fd;
553
554         if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
555                 /* On UNIX, symbolic links must be created with symlink(), which
556                  * requires that the full link target be available.  */
557                 if (blob->size > REPARSE_DATA_MAX_SIZE) {
558                         ERROR_WITH_ERRNO("Reparse data of \"%s\" has size "
559                                          "%"PRIu64" bytes (exceeds %u bytes)",
560                                          inode_any_full_path(inode),
561                                          blob->size, REPARSE_DATA_MAX_SIZE);
562                         return WIMLIB_ERR_INVALID_REPARSE_DATA;
563                 }
564                 ctx->reparse_ptr = ctx->reparse_data;
565                 return 0;
566         }
567
568         wimlib_assert(stream_is_unnamed_data_stream(strm));
569
570         /* Unnamed data stream of "regular" file  */
571
572         /* This should be ensured by extract_blob_list()  */
573         wimlib_assert(ctx->num_open_fds < MAX_OPEN_FILES);
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 blob for extraction  */
590 static int
591 unix_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
592 {
593         struct unix_apply_ctx *ctx = _ctx;
594         const struct blob_extraction_target *targets = blob_extraction_targets(blob);
595
596         for (u32 i = 0; i < blob->out_refcnt; i++) {
597                 int ret = unix_begin_extract_blob_instance(blob,
598                                                            targets[i].inode,
599                                                            targets[i].stream,
600                                                            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 blob has been read for extraction  */
611 static int
612 unix_extract_chunk(const void *chunk, size_t size, void *_ctx)
613 {
614         struct unix_apply_ctx *ctx = _ctx;
615         int ret;
616
617         for (unsigned i = 0; i < ctx->num_open_fds; i++) {
618                 ret = full_write(&ctx->open_fds[i], chunk, size);
619                 if (ret) {
620                         ERROR_WITH_ERRNO("Error writing data to filesystem");
621                         return ret;
622                 }
623         }
624         if (ctx->reparse_ptr)
625                 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
626         return 0;
627 }
628
629 /* Called when a blob has been fully read for extraction  */
630 static int
631 unix_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
632 {
633         struct unix_apply_ctx *ctx = _ctx;
634         int ret;
635         unsigned j;
636         const struct blob_extraction_target *targets = blob_extraction_targets(blob);
637
638         ctx->reparse_ptr = NULL;
639
640         if (status) {
641                 unix_cleanup_open_fds(ctx, 0);
642                 return status;
643         }
644
645         j = 0;
646         ret = 0;
647         for (u32 i = 0; i < blob->out_refcnt; i++) {
648                 struct wim_inode *inode = targets[i].inode;
649
650                 if (inode_is_symlink(inode)) {
651                         /* We finally have the symlink data, so we can create
652                          * the symlink.  */
653                         const char *path;
654
655                         path = unix_build_inode_extraction_path(inode, ctx);
656                         ret = unix_create_symlink(inode, path, blob->size, ctx);
657                         if (ret) {
658                                 ERROR_WITH_ERRNO("Can't create symbolic link "
659                                                  "\"%s\"", path);
660                                 break;
661                         }
662                         ret = unix_set_metadata(-1, inode, path, ctx);
663                         if (ret)
664                                 break;
665                 } else {
666                         /* Set metadata on regular file just before closing it.
667                          */
668                         struct filedes *fd = &ctx->open_fds[j];
669
670                         ret = unix_set_metadata(fd->fd, inode, NULL, ctx);
671                         if (ret)
672                                 break;
673
674                         if (filedes_close(fd)) {
675                                 ERROR_WITH_ERRNO("Error closing \"%s\"",
676                                                  unix_build_inode_extraction_path(inode, ctx));
677                                 ret = WIMLIB_ERR_WRITE;
678                                 break;
679                         }
680                         j++;
681                 }
682         }
683         unix_cleanup_open_fds(ctx, j);
684         return ret;
685 }
686
687 static int
688 unix_set_dir_metadata(struct list_head *dentry_list, struct unix_apply_ctx *ctx)
689 {
690         const struct wim_dentry *dentry;
691         int ret;
692
693         list_for_each_entry_reverse(dentry, dentry_list, d_extraction_list_node) {
694                 if (should_extract_as_directory(dentry->d_inode)) {
695                         ret = unix_set_metadata(-1, dentry->d_inode, NULL, ctx);
696                         if (ret)
697                                 return ret;
698                         ret = report_file_metadata_applied(&ctx->common);
699                         if (ret)
700                                 return ret;
701                 }
702         }
703         return 0;
704 }
705
706 static int
707 unix_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
708 {
709         int ret;
710         struct unix_apply_ctx *ctx = (struct unix_apply_ctx *)_ctx;
711         size_t path_max;
712         u64 dir_count;
713         u64 empty_file_count;
714
715         /* Compute the maximum path length that will be needed, then allocate
716          * some path buffers.  */
717         path_max = unix_compute_path_max(dentry_list, ctx);
718
719         for (unsigned i = 0; i < NUM_PATHBUFS; i++) {
720                 ctx->pathbufs[i] = MALLOC(path_max);
721                 if (!ctx->pathbufs[i]) {
722                         ret = WIMLIB_ERR_NOMEM;
723                         goto out;
724                 }
725                 /* Pre-fill the target in each path buffer.  We'll just append
726                  * the rest of the paths after this.  */
727                 memcpy(ctx->pathbufs[i],
728                        ctx->common.target, ctx->common.target_nchars);
729         }
730
731         /* Extract directories and empty regular files.  Directories are needed
732          * because we can't extract any other files until their directories
733          * exist.  Empty files are needed because they don't have
734          * representatives in the blob list.  */
735
736         unix_count_dentries(dentry_list, &dir_count, &empty_file_count);
737
738         ret = start_file_structure_phase(&ctx->common, dir_count + empty_file_count);
739         if (ret)
740                 goto out;
741
742         ret = unix_create_dirs_and_empty_files(dentry_list, ctx);
743         if (ret)
744                 goto out;
745
746         ret = end_file_structure_phase(&ctx->common);
747         if (ret)
748                 goto out;
749
750         /* Get full path to target if needed for absolute symlink fixups.  */
751         if ((ctx->common.extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX) &&
752             ctx->common.required_features.symlink_reparse_points)
753         {
754                 ctx->target_abspath = realpath(ctx->common.target, NULL);
755                 if (!ctx->target_abspath) {
756                         ret = WIMLIB_ERR_NOMEM;
757                         goto out;
758                 }
759                 ctx->target_abspath_nchars = strlen(ctx->target_abspath);
760         }
761
762         /* Extract nonempty regular files and symbolic links.  */
763
764         struct read_blob_callbacks cbs = {
765                 .begin_blob     = unix_begin_extract_blob,
766                 .consume_chunk  = unix_extract_chunk,
767                 .end_blob       = unix_end_extract_blob,
768                 .ctx            = ctx,
769         };
770         ret = extract_blob_list(&ctx->common, &cbs);
771         if (ret)
772                 goto out;
773
774
775         /* Set directory metadata.  We do this last so that we get the right
776          * directory timestamps.  */
777         ret = start_file_metadata_phase(&ctx->common, dir_count);
778         if (ret)
779                 goto out;
780
781         ret = unix_set_dir_metadata(dentry_list, ctx);
782         if (ret)
783                 goto out;
784
785         ret = end_file_metadata_phase(&ctx->common);
786         if (ret)
787                 goto out;
788
789         if (ctx->num_special_files_ignored) {
790                 WARNING("%lu special files were not extracted due to EPERM!",
791                         ctx->num_special_files_ignored);
792         }
793 out:
794         for (unsigned i = 0; i < NUM_PATHBUFS; i++)
795                 FREE(ctx->pathbufs[i]);
796         FREE(ctx->target_abspath);
797         return ret;
798 }
799
800 const struct apply_operations unix_apply_ops = {
801         .name                   = "UNIX",
802         .get_supported_features = unix_get_supported_features,
803         .extract                = unix_extract,
804         .context_size           = sizeof(struct unix_apply_ctx),
805 };