2 * unix_apply.c - Code to apply files from a WIM image on UNIX.
6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
41 #include "wimlib/apply.h"
42 #include "wimlib/error.h"
43 #include "wimlib/lookup_table.h"
44 #include "wimlib/reparse.h"
45 #include "wimlib/timestamp.h"
47 /* Returns the number of components of @path. */
49 get_num_path_components(const char *path)
51 unsigned num_components = 0;
57 while (*path && *path != '/')
60 return num_components;
64 path_next_part(const char *path)
66 while (*path && *path != '/')
68 while (*path && *path == '/')
74 unix_extract_regular_file_linked(struct wim_dentry *dentry,
75 const char *output_path,
76 struct apply_args *args,
77 struct wim_lookup_table_entry *lte)
79 /* This mode overrides the normal hard-link extraction and
80 * instead either symlinks or hardlinks *all* identical files in
81 * the WIM, even if they are in a different image (in the case
82 * of a multi-image extraction) */
84 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
85 if (link(lte->extracted_file, output_path) != 0) {
86 ERROR_WITH_ERRNO("Failed to hard link "
88 output_path, lte->extracted_file);
89 return WIMLIB_ERR_LINK;
92 int num_path_components;
93 int num_output_dir_path_components;
94 size_t extracted_file_len;
98 const struct wim_dentry *d;
100 num_path_components = 0;
101 for (d = dentry; d != args->extract_root; d = d->parent)
102 num_path_components++;
103 wimlib_assert(num_path_components > 0);
104 num_path_components--;
105 num_output_dir_path_components = get_num_path_components(args->target);
107 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
108 num_path_components++;
109 num_output_dir_path_components--;
111 extracted_file_len = strlen(lte->extracted_file);
113 char buf[extracted_file_len + 3 * num_path_components + 1];
116 for (i = 0; i < num_path_components; i++) {
121 p2 = lte->extracted_file;
124 while (num_output_dir_path_components > 0) {
125 p2 = path_next_part(p2);
126 num_output_dir_path_components--;
129 if (symlink(buf, output_path) != 0) {
130 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
131 buf, lte->extracted_file);
132 return WIMLIB_ERR_LINK;
139 symlink_apply_unix_data(const char *link,
140 const struct wimlib_unix_data *unix_data)
142 if (lchown(link, unix_data->uid, unix_data->gid)) {
143 if (errno == EPERM) {
145 WARNING_WITH_ERRNO("failed to set symlink UNIX "
146 "owner/group on \"%s\"", link);
148 ERROR_WITH_ERRNO("failed to set symlink UNIX "
149 "owner/group on \"%s\"", link);
150 return WIMLIB_ERR_INVALID_DENTRY;
157 fd_apply_unix_data(int fd, const char *path,
158 const struct wimlib_unix_data *unix_data,
161 if (extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)
164 if (fchown(fd, unix_data->uid, unix_data->gid)) {
165 if (errno == EPERM &&
166 !(extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
168 WARNING_WITH_ERRNO("failed to set file UNIX "
169 "owner/group on \"%s\"", path);
171 ERROR_WITH_ERRNO("failed to set file UNIX "
172 "owner/group on \"%s\"", path);
173 return (errno == EPERM) ? WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT :
178 if (fchmod(fd, unix_data->mode)) {
179 if (errno == EPERM &&
180 !(extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
182 WARNING_WITH_ERRNO("failed to set UNIX file mode "
185 ERROR_WITH_ERRNO("failed to set UNIX file mode "
187 return (errno == EPERM) ? WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT :
195 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data,
198 int dfd = open(dir, O_RDONLY);
201 ret = fd_apply_unix_data(dfd, dir, unix_data, extract_flags);
202 if (close(dfd) && ret == 0) {
203 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
204 ret = WIMLIB_ERR_WRITE;
207 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
208 ret = WIMLIB_ERR_OPENDIR;
214 unix_extract_regular_file_unlinked(struct wim_dentry *dentry,
215 struct apply_args *args,
216 const char *output_path,
217 struct wim_lookup_table_entry *lte)
219 /* Normal mode of extraction. Regular files and hard links are
220 * extracted in the way that they appear in the WIM. */
224 struct wim_inode *inode = dentry->d_inode;
226 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
227 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
228 WIMLIB_EXTRACT_FLAG_HARDLINK))))
230 /* If the dentry is part of a hard link set of at least 2
231 * dentries and one of the other dentries has already been
232 * extracted, make a hard link to the file corresponding to this
233 * already-extracted directory. Otherwise, extract the file and
234 * set the inode->i_extracted_file field so that other dentries
235 * in the hard link group can link to it. */
236 if (inode->i_nlink > 1) {
237 if (inode->i_extracted_file) {
238 DEBUG("Extracting hard link `%s' => `%s'",
239 output_path, inode->i_extracted_file);
240 if (link(inode->i_extracted_file, output_path) != 0) {
241 ERROR_WITH_ERRNO("Failed to hard link "
244 inode->i_extracted_file);
245 return WIMLIB_ERR_LINK;
249 FREE(inode->i_extracted_file);
250 inode->i_extracted_file = STRDUP(output_path);
251 if (!inode->i_extracted_file) {
252 ERROR("Failed to allocate memory for filename");
253 return WIMLIB_ERR_NOMEM;
258 /* Extract the contents of the file to @output_path. */
260 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
262 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
264 return WIMLIB_ERR_OPEN;
268 /* Empty file with no lookup table entry */
269 DEBUG("Empty file `%s'.", output_path);
271 goto out_extract_unix_data;
274 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
276 ERROR("Failed to extract resource to `%s'", output_path);
280 out_extract_unix_data:
281 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
282 struct wimlib_unix_data unix_data;
283 ret = inode_get_unix_data(inode, &unix_data, NULL);
289 ret = fd_apply_unix_data(out_fd, output_path, &unix_data,
290 args->extract_flags);
295 args->progress.extract.completed_bytes += wim_resource_size(lte);
297 if (close(out_fd) != 0) {
298 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
300 ret = WIMLIB_ERR_WRITE;
306 unix_extract_regular_file(struct wim_dentry *dentry,
307 struct apply_args *args,
308 const char *output_path)
310 struct wim_lookup_table_entry *lte;
311 const struct wim_inode *inode = dentry->d_inode;
313 lte = inode_unnamed_lte_resolved(inode);
315 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
316 WIMLIB_EXTRACT_FLAG_HARDLINK)))
318 if (lte->extracted_file) {
319 return unix_extract_regular_file_linked(dentry,
323 lte->extracted_file = STRDUP(output_path);
324 if (!lte->extracted_file)
325 return WIMLIB_ERR_NOMEM;
328 return unix_extract_regular_file_unlinked(dentry, args, output_path, lte);
332 unix_extract_symlink(struct wim_dentry *dentry,
333 struct apply_args *args,
334 const char *output_path)
336 char target[4096 + args->target_realpath_len];
338 const struct wim_inode *inode = dentry->d_inode;
340 ssize_t ret = wim_inode_readlink(inode,
341 target + args->target_realpath_len,
342 sizeof(target) - args->target_realpath_len - 1);
343 struct wim_lookup_table_entry *lte;
346 ERROR("Could not read the symbolic link from dentry `%s'",
347 dentry_full_path(dentry));
348 return WIMLIB_ERR_INVALID_DENTRY;
350 target[args->target_realpath_len + ret] = '\0';
351 if (target[args->target_realpath_len] == '/' &&
352 args->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
354 /* Fix absolute symbolic link target to point into the actual
355 * extraction destination */
356 memcpy(target, args->target_realpath,
357 args->target_realpath_len);
358 fixed_target = target;
360 /* Keep same link target */
361 fixed_target = target + args->target_realpath_len;
363 ret = symlink(fixed_target, output_path);
365 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
366 output_path, fixed_target);
367 return WIMLIB_ERR_LINK;
369 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
370 struct wimlib_unix_data unix_data;
371 ret = inode_get_unix_data(inode, &unix_data, NULL);
377 ret = symlink_apply_unix_data(output_path, &unix_data);
381 lte = inode_unnamed_lte_resolved(inode);
382 wimlib_assert(lte != NULL);
383 args->progress.extract.completed_bytes += wim_resource_size(lte);
388 unix_extract_directory(struct wim_dentry *dentry, const char *output_path,
394 ret = stat(output_path, &stbuf);
396 if (S_ISDIR(stbuf.st_mode)) {
399 ERROR("\"%s\" is not a directory", output_path);
400 return WIMLIB_ERR_MKDIR;
403 if (errno != ENOENT) {
404 ERROR_WITH_ERRNO("Failed to stat \"%s\"", output_path);
405 return WIMLIB_ERR_STAT;
409 if (mkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
410 ERROR_WITH_ERRNO("Cannot create directory \"%s\"", output_path);
411 return WIMLIB_ERR_MKDIR;
415 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
416 struct wimlib_unix_data unix_data;
417 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
423 ret = dir_apply_unix_data(output_path, &unix_data,
430 unix_do_apply_dentry(const char *output_path, size_t output_path_len,
431 struct wim_dentry *dentry, struct apply_args *args)
433 const struct wim_inode *inode = dentry->d_inode;
435 if (inode_is_symlink(inode))
436 return unix_extract_symlink(dentry, args, output_path);
437 else if (inode_is_directory(inode))
438 return unix_extract_directory(dentry, output_path, args->extract_flags);
440 return unix_extract_regular_file(dentry, args, output_path);
444 unix_do_apply_dentry_timestamps(const char *output_path,
445 size_t output_path_len,
446 struct wim_dentry *dentry,
447 struct apply_args *args)
450 const struct wim_inode *inode = dentry->d_inode;
452 #ifdef HAVE_UTIMENSAT
453 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
454 * into `struct timespec's for passing to utimensat(), which is accurate
455 * to 1 nanosecond. */
457 struct timespec ts[2];
458 ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
459 ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
460 ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
468 /* utimensat() not implemented or not available */
470 /* Convert the WIM timestamps, which are accurate to 100
471 * nanoseconds, into `struct timeval's for passing to lutimes(),
472 * which is accurate to 1 microsecond. */
473 struct timeval tv[2];
474 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
475 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
476 ret = lutimes(output_path, tv);
483 /* utimensat() and lutimes() both not implemented or not
486 /* Convert the WIM timestamps, which are accurate to 100
487 * nanoseconds, into a `struct utimbuf's for passing to
488 * utime(), which is accurate to 1 second. */
490 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
491 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
492 ret = utime(output_path, &buf);
495 if (ret && args->num_utime_warnings < 10) {
496 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
498 args->num_utime_warnings++;
503 #endif /* !__WIN32__ */