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;
99 num_path_components = get_num_path_components(dentry->_full_path) - 1;
100 num_output_dir_path_components = get_num_path_components(args->target);
102 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
103 num_path_components++;
104 num_output_dir_path_components--;
106 extracted_file_len = strlen(lte->extracted_file);
108 char buf[extracted_file_len + 3 * num_path_components + 1];
111 for (i = 0; i < num_path_components; i++) {
116 p2 = lte->extracted_file;
119 while (num_output_dir_path_components > 0) {
120 p2 = path_next_part(p2);
121 num_output_dir_path_components--;
124 if (symlink(buf, output_path) != 0) {
125 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
126 buf, lte->extracted_file);
127 return WIMLIB_ERR_LINK;
134 symlink_apply_unix_data(const char *link,
135 const struct wimlib_unix_data *unix_data)
137 if (lchown(link, unix_data->uid, unix_data->gid)) {
138 if (errno == EPERM) {
140 WARNING_WITH_ERRNO("failed to set symlink UNIX "
141 "owner/group on \"%s\"", link);
143 ERROR_WITH_ERRNO("failed to set symlink UNIX "
144 "owner/group on \"%s\"", link);
145 return WIMLIB_ERR_INVALID_DENTRY;
152 fd_apply_unix_data(int fd, const char *path,
153 const struct wimlib_unix_data *unix_data,
156 if (extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)
159 if (fchown(fd, unix_data->uid, unix_data->gid)) {
160 if (errno == EPERM &&
161 !(extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
163 WARNING_WITH_ERRNO("failed to set file UNIX "
164 "owner/group on \"%s\"", path);
166 ERROR_WITH_ERRNO("failed to set file UNIX "
167 "owner/group on \"%s\"", path);
168 return (errno == EPERM) ? WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT :
173 if (fchmod(fd, unix_data->mode)) {
174 if (errno == EPERM &&
175 !(extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
177 WARNING_WITH_ERRNO("failed to set UNIX file mode "
180 ERROR_WITH_ERRNO("failed to set UNIX file mode "
182 return (errno == EPERM) ? WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT :
190 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data,
193 int dfd = open(dir, O_RDONLY);
196 ret = fd_apply_unix_data(dfd, dir, unix_data, extract_flags);
197 if (close(dfd) && ret == 0) {
198 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
199 ret = WIMLIB_ERR_WRITE;
202 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
203 ret = WIMLIB_ERR_OPENDIR;
209 unix_extract_regular_file_unlinked(struct wim_dentry *dentry,
210 struct apply_args *args,
211 const char *output_path,
212 struct wim_lookup_table_entry *lte)
214 /* Normal mode of extraction. Regular files and hard links are
215 * extracted in the way that they appear in the WIM. */
219 struct wim_inode *inode = dentry->d_inode;
221 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
222 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
223 WIMLIB_EXTRACT_FLAG_HARDLINK))))
225 /* If the dentry is part of a hard link set of at least 2
226 * dentries and one of the other dentries has already been
227 * extracted, make a hard link to the file corresponding to this
228 * already-extracted directory. Otherwise, extract the file and
229 * set the inode->i_extracted_file field so that other dentries
230 * in the hard link group can link to it. */
231 if (inode->i_nlink > 1) {
232 if (inode->i_extracted_file) {
233 DEBUG("Extracting hard link `%s' => `%s'",
234 output_path, inode->i_extracted_file);
235 if (link(inode->i_extracted_file, output_path) != 0) {
236 ERROR_WITH_ERRNO("Failed to hard link "
239 inode->i_extracted_file);
240 return WIMLIB_ERR_LINK;
244 FREE(inode->i_extracted_file);
245 inode->i_extracted_file = STRDUP(output_path);
246 if (!inode->i_extracted_file) {
247 ERROR("Failed to allocate memory for filename");
248 return WIMLIB_ERR_NOMEM;
253 /* Extract the contents of the file to @output_path. */
255 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
257 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
259 return WIMLIB_ERR_OPEN;
263 /* Empty file with no lookup table entry */
264 DEBUG("Empty file `%s'.", output_path);
266 goto out_extract_unix_data;
269 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
271 ERROR("Failed to extract resource to `%s'", output_path);
275 out_extract_unix_data:
276 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
277 struct wimlib_unix_data unix_data;
278 ret = inode_get_unix_data(inode, &unix_data, NULL);
284 ret = fd_apply_unix_data(out_fd, output_path, &unix_data,
285 args->extract_flags);
290 args->progress.extract.completed_bytes += wim_resource_size(lte);
292 if (close(out_fd) != 0) {
293 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
295 ret = WIMLIB_ERR_WRITE;
301 unix_extract_regular_file(struct wim_dentry *dentry,
302 struct apply_args *args,
303 const char *output_path)
305 struct wim_lookup_table_entry *lte;
306 const struct wim_inode *inode = dentry->d_inode;
308 lte = inode_unnamed_lte_resolved(inode);
310 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
311 WIMLIB_EXTRACT_FLAG_HARDLINK)))
313 if (lte->extracted_file) {
314 return unix_extract_regular_file_linked(dentry,
318 lte->extracted_file = STRDUP(output_path);
319 if (!lte->extracted_file)
320 return WIMLIB_ERR_NOMEM;
323 return unix_extract_regular_file_unlinked(dentry, args, output_path, lte);
327 unix_extract_symlink(struct wim_dentry *dentry,
328 struct apply_args *args,
329 const char *output_path)
331 char target[4096 + args->target_realpath_len];
333 const struct wim_inode *inode = dentry->d_inode;
335 ssize_t ret = wim_inode_readlink(inode,
336 target + args->target_realpath_len,
337 sizeof(target) - args->target_realpath_len - 1);
338 struct wim_lookup_table_entry *lte;
341 ERROR("Could not read the symbolic link from dentry `%s'",
343 return WIMLIB_ERR_INVALID_DENTRY;
345 target[args->target_realpath_len + ret] = '\0';
346 if (target[args->target_realpath_len] == '/' &&
347 args->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
349 /* Fix absolute symbolic link target to point into the actual
350 * extraction destination */
351 memcpy(target, args->target_realpath,
352 args->target_realpath_len);
353 fixed_target = target;
355 /* Keep same link target */
356 fixed_target = target + args->target_realpath_len;
358 ret = symlink(fixed_target, output_path);
360 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
361 output_path, fixed_target);
362 return WIMLIB_ERR_LINK;
364 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
365 struct wimlib_unix_data unix_data;
366 ret = inode_get_unix_data(inode, &unix_data, NULL);
372 ret = symlink_apply_unix_data(output_path, &unix_data);
376 lte = inode_unnamed_lte_resolved(inode);
377 wimlib_assert(lte != NULL);
378 args->progress.extract.completed_bytes += wim_resource_size(lte);
383 unix_extract_directory(struct wim_dentry *dentry, const tchar *output_path,
389 ret = tstat(output_path, &stbuf);
391 if (S_ISDIR(stbuf.st_mode)) {
394 ERROR("`%"TS"' is not a directory", output_path);
395 return WIMLIB_ERR_MKDIR;
398 if (errno != ENOENT) {
399 ERROR_WITH_ERRNO("Failed to stat `%"TS"'", output_path);
400 return WIMLIB_ERR_STAT;
404 if (tmkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
406 ERROR_WITH_ERRNO("Cannot create directory `%"TS"'", output_path);
407 return WIMLIB_ERR_MKDIR;
412 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
413 struct wimlib_unix_data unix_data;
414 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
420 ret = dir_apply_unix_data(output_path, &unix_data,
428 unix_do_apply_dentry(const char *output_path, size_t output_path_len,
429 struct wim_dentry *dentry, struct apply_args *args)
431 const struct wim_inode *inode = dentry->d_inode;
433 if (inode_is_symlink(inode))
434 return unix_extract_symlink(dentry, args, output_path);
435 else if (inode_is_directory(inode))
436 return unix_extract_directory(dentry, output_path, args->extract_flags);
438 return unix_extract_regular_file(dentry, args, output_path);
442 unix_do_apply_dentry_timestamps(const char *output_path,
443 size_t output_path_len,
444 struct wim_dentry *dentry,
445 struct apply_args *args)
448 const struct wim_inode *inode = dentry->d_inode;
450 #ifdef HAVE_UTIMENSAT
451 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
452 * into `struct timespec's for passing to utimensat(), which is accurate
453 * to 1 nanosecond. */
455 struct timespec ts[2];
456 ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
457 ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
458 ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
466 /* utimensat() not implemented or not available */
468 /* Convert the WIM timestamps, which are accurate to 100
469 * nanoseconds, into `struct timeval's for passing to lutimes(),
470 * which is accurate to 1 microsecond. */
471 struct timeval tv[2];
472 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
473 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
474 ret = lutimes(output_path, tv);
481 /* utimensat() and lutimes() both not implemented or not
484 /* Convert the WIM timestamps, which are accurate to 100
485 * nanoseconds, into a `struct utimbuf's for passing to
486 * utime(), which is accurate to 1 second. */
488 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
489 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
490 ret = utime(output_path, &buf);
493 if (ret && args->num_utime_warnings < 10) {
494 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
496 args->num_utime_warnings++;
501 #endif /* !__WIN32__ */