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/.
37 #include "timestamp.h"
38 #include "wimlib_internal.h"
39 #include "lookup_table.h"
41 /* Returns the number of components of @path. */
43 get_num_path_components(const char *path)
45 unsigned num_components = 0;
51 while (*path && *path != '/')
54 return num_components;
58 path_next_part(const char *path)
60 while (*path && *path != '/')
62 while (*path && *path == '/')
68 unix_extract_regular_file_linked(struct wim_dentry *dentry,
69 const char *output_path,
70 struct apply_args *args,
71 struct wim_lookup_table_entry *lte)
73 /* This mode overrides the normal hard-link extraction and
74 * instead either symlinks or hardlinks *all* identical files in
75 * the WIM, even if they are in a different image (in the case
76 * of a multi-image extraction) */
78 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
79 if (link(lte->extracted_file, output_path) != 0) {
80 ERROR_WITH_ERRNO("Failed to hard link "
82 output_path, lte->extracted_file);
83 return WIMLIB_ERR_LINK;
86 int num_path_components;
87 int num_output_dir_path_components;
88 size_t extracted_file_len;
93 num_path_components = get_num_path_components(dentry->_full_path) - 1;
94 num_output_dir_path_components = get_num_path_components(args->target);
96 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
97 num_path_components++;
98 num_output_dir_path_components--;
100 extracted_file_len = strlen(lte->extracted_file);
102 char buf[extracted_file_len + 3 * num_path_components + 1];
105 for (i = 0; i < num_path_components; i++) {
110 p2 = lte->extracted_file;
113 while (num_output_dir_path_components > 0) {
114 p2 = path_next_part(p2);
115 num_output_dir_path_components--;
118 if (symlink(buf, output_path) != 0) {
119 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
120 buf, lte->extracted_file);
121 return WIMLIB_ERR_LINK;
128 symlink_apply_unix_data(const char *link,
129 const struct wimlib_unix_data *unix_data)
131 if (lchown(link, unix_data->uid, unix_data->gid)) {
132 if (errno == EPERM) {
134 WARNING_WITH_ERRNO("failed to set symlink UNIX "
135 "owner/group on \"%s\"", link);
137 ERROR_WITH_ERRNO("failed to set symlink UNIX "
138 "owner/group on \"%s\"", link);
139 return WIMLIB_ERR_INVALID_DENTRY;
146 fd_apply_unix_data(int fd, const char *path,
147 const struct wimlib_unix_data *unix_data,
150 if (extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)
153 if (fchown(fd, unix_data->uid, unix_data->gid)) {
154 if (errno == EPERM &&
155 !(extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
157 WARNING_WITH_ERRNO("failed to set file UNIX "
158 "owner/group on \"%s\"", path);
160 ERROR_WITH_ERRNO("failed to set file UNIX "
161 "owner/group on \"%s\"", path);
162 return (errno == EPERM) ? WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT :
167 if (fchmod(fd, unix_data->mode)) {
168 if (errno == EPERM &&
169 !(extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
171 WARNING_WITH_ERRNO("failed to set UNIX file mode "
174 ERROR_WITH_ERRNO("failed to set UNIX file mode "
176 return (errno == EPERM) ? WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT :
184 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data,
187 int dfd = open(dir, O_RDONLY);
190 ret = fd_apply_unix_data(dfd, dir, unix_data, extract_flags);
191 if (close(dfd) && ret == 0) {
192 ERROR_WITH_ERRNO("can't close directory `%s'", dir);
193 ret = WIMLIB_ERR_WRITE;
196 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
197 ret = WIMLIB_ERR_OPENDIR;
203 unix_extract_regular_file_unlinked(struct wim_dentry *dentry,
204 struct apply_args *args,
205 const char *output_path,
206 struct wim_lookup_table_entry *lte)
208 /* Normal mode of extraction. Regular files and hard links are
209 * extracted in the way that they appear in the WIM. */
213 struct wim_inode *inode = dentry->d_inode;
215 if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
216 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
217 WIMLIB_EXTRACT_FLAG_HARDLINK))))
219 /* If the dentry is part of a hard link set of at least 2
220 * dentries and one of the other dentries has already been
221 * extracted, make a hard link to the file corresponding to this
222 * already-extracted directory. Otherwise, extract the file and
223 * set the inode->i_extracted_file field so that other dentries
224 * in the hard link group can link to it. */
225 if (inode->i_nlink > 1) {
226 if (inode->i_extracted_file) {
227 DEBUG("Extracting hard link `%s' => `%s'",
228 output_path, inode->i_extracted_file);
229 if (link(inode->i_extracted_file, output_path) != 0) {
230 ERROR_WITH_ERRNO("Failed to hard link "
233 inode->i_extracted_file);
234 return WIMLIB_ERR_LINK;
238 FREE(inode->i_extracted_file);
239 inode->i_extracted_file = STRDUP(output_path);
240 if (!inode->i_extracted_file) {
241 ERROR("Failed to allocate memory for filename");
242 return WIMLIB_ERR_NOMEM;
247 /* Extract the contents of the file to @output_path. */
249 out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
251 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
253 return WIMLIB_ERR_OPEN;
257 /* Empty file with no lookup table entry */
258 DEBUG("Empty file `%s'.", output_path);
260 goto out_extract_unix_data;
263 ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
265 ERROR("Failed to extract resource to `%s'", output_path);
269 out_extract_unix_data:
270 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
271 struct wimlib_unix_data unix_data;
272 ret = inode_get_unix_data(inode, &unix_data, NULL);
278 ret = fd_apply_unix_data(out_fd, output_path, &unix_data,
279 args->extract_flags);
284 args->progress.extract.completed_bytes += wim_resource_size(lte);
286 if (close(out_fd) != 0) {
287 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
289 ret = WIMLIB_ERR_WRITE;
295 unix_extract_regular_file(struct wim_dentry *dentry,
296 struct apply_args *args,
297 const char *output_path)
299 struct wim_lookup_table_entry *lte;
300 const struct wim_inode *inode = dentry->d_inode;
302 lte = inode_unnamed_lte_resolved(inode);
304 if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
305 WIMLIB_EXTRACT_FLAG_HARDLINK)))
307 if (lte->extracted_file) {
308 return unix_extract_regular_file_linked(dentry,
312 lte->extracted_file = STRDUP(output_path);
313 if (!lte->extracted_file)
314 return WIMLIB_ERR_NOMEM;
317 return unix_extract_regular_file_unlinked(dentry, args, output_path, lte);
321 unix_extract_symlink(struct wim_dentry *dentry,
322 struct apply_args *args,
323 const char *output_path)
325 char target[4096 + args->target_realpath_len];
327 const struct wim_inode *inode = dentry->d_inode;
329 ssize_t ret = wim_inode_readlink(inode,
330 target + args->target_realpath_len,
331 sizeof(target) - args->target_realpath_len - 1);
332 struct wim_lookup_table_entry *lte;
335 ERROR("Could not read the symbolic link from dentry `%s'",
337 return WIMLIB_ERR_INVALID_DENTRY;
339 target[args->target_realpath_len + ret] = '\0';
340 if (target[args->target_realpath_len] == '/' &&
341 args->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
343 /* Fix absolute symbolic link target to point into the actual
344 * extraction destination */
345 memcpy(target, args->target_realpath,
346 args->target_realpath_len);
347 fixed_target = target;
349 /* Keep same link target */
350 fixed_target = target + args->target_realpath_len;
352 ret = symlink(fixed_target, output_path);
354 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
355 output_path, fixed_target);
356 return WIMLIB_ERR_LINK;
358 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
359 struct wimlib_unix_data unix_data;
360 ret = inode_get_unix_data(inode, &unix_data, NULL);
366 ret = symlink_apply_unix_data(output_path, &unix_data);
370 lte = inode_unnamed_lte_resolved(inode);
371 wimlib_assert(lte != NULL);
372 args->progress.extract.completed_bytes += wim_resource_size(lte);
377 unix_extract_directory(struct wim_dentry *dentry, const tchar *output_path,
383 ret = tstat(output_path, &stbuf);
385 if (S_ISDIR(stbuf.st_mode)) {
387 /*WARNING("`%s' already exists", output_path);*/
390 ERROR("`%"TS"' is not a directory", output_path);
391 return WIMLIB_ERR_MKDIR;
394 if (errno != ENOENT) {
395 ERROR_WITH_ERRNO("Failed to stat `%"TS"'", output_path);
396 return WIMLIB_ERR_STAT;
400 if (tmkdir(output_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
402 ERROR_WITH_ERRNO("Cannot create directory `%"TS"'", output_path);
403 return WIMLIB_ERR_MKDIR;
408 if (extract_flags & WIMLIB_EXTRACT_FLAG_UNIX_DATA) {
409 struct wimlib_unix_data unix_data;
410 ret = inode_get_unix_data(dentry->d_inode, &unix_data, NULL);
416 ret = dir_apply_unix_data(output_path, &unix_data,
424 unix_do_apply_dentry(const char *output_path, size_t output_path_len,
425 struct wim_dentry *dentry, struct apply_args *args)
427 const struct wim_inode *inode = dentry->d_inode;
429 if (inode_is_symlink(inode))
430 return unix_extract_symlink(dentry, args, output_path);
431 else if (inode_is_directory(inode))
432 return unix_extract_directory(dentry, output_path, args->extract_flags);
434 return unix_extract_regular_file(dentry, args, output_path);
438 unix_do_apply_dentry_timestamps(const char *output_path,
439 size_t output_path_len,
440 struct wim_dentry *dentry,
441 struct apply_args *args)
444 const struct wim_inode *inode = dentry->d_inode;
446 #ifdef HAVE_UTIMENSAT
447 /* Convert the WIM timestamps, which are accurate to 100 nanoseconds,
448 * into `struct timespec's for passing to utimensat(), which is accurate
449 * to 1 nanosecond. */
451 struct timespec ts[2];
452 ts[0] = wim_timestamp_to_timespec(inode->i_last_access_time);
453 ts[1] = wim_timestamp_to_timespec(inode->i_last_write_time);
454 ret = utimensat(AT_FDCWD, output_path, ts, AT_SYMLINK_NOFOLLOW);
462 /* utimensat() not implemented or not available */
464 /* Convert the WIM timestamps, which are accurate to 100
465 * nanoseconds, into `struct timeval's for passing to lutimes(),
466 * which is accurate to 1 microsecond. */
467 struct timeval tv[2];
468 tv[0] = wim_timestamp_to_timeval(inode->i_last_access_time);
469 tv[1] = wim_timestamp_to_timeval(inode->i_last_write_time);
470 ret = lutimes(output_path, tv);
477 /* utimensat() and lutimes() both not implemented or not
480 /* Convert the WIM timestamps, which are accurate to 100
481 * nanoseconds, into a `struct utimbuf's for passing to
482 * utime(), which is accurate to 1 second. */
484 buf.actime = wim_timestamp_to_unix(inode->i_last_access_time);
485 buf.modtime = wim_timestamp_to_unix(inode->i_last_write_time);
486 ret = utime(output_path, &buf);
489 if (ret && args->num_utime_warnings < 10) {
490 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
492 args->num_utime_warnings++;