b1d914827beec90c9ac3f6e69067cd19161eaaa9
[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 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
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)
13  * any later version.
14  *
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
18  * details.
19  *
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/.
22  */
23
24 #ifndef __WIN32__
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include <dirent.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <unistd.h>
37 #ifdef HAVE_UTIME_H
38 #  include <utime.h>
39 #endif
40
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"
46
47 /* Returns the number of components of @path.  */
48 static unsigned
49 get_num_path_components(const char *path)
50 {
51         unsigned num_components = 0;
52         while (*path) {
53                 while (*path == '/')
54                         path++;
55                 if (*path)
56                         num_components++;
57                 while (*path && *path != '/')
58                         path++;
59         }
60         return num_components;
61 }
62
63 static const char *
64 path_next_part(const char *path)
65 {
66         while (*path && *path != '/')
67                 path++;
68         while (*path && *path == '/')
69                 path++;
70         return path;
71 }
72
73 static int
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)
78 {
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) */
83
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 "
87                                          "`%s' to `%s'",
88                                          output_path, lte->extracted_file);
89                         return WIMLIB_ERR_LINK;
90                 }
91         } else {
92                 int num_path_components;
93                 int num_output_dir_path_components;
94                 size_t extracted_file_len;
95                 char *p;
96                 const char *p2;
97                 size_t i;
98                 const struct wim_dentry *d;
99
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);
106
107                 if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
108                         num_path_components++;
109                         num_output_dir_path_components--;
110                 }
111                 extracted_file_len = strlen(lte->extracted_file);
112
113                 char buf[extracted_file_len + 3 * num_path_components + 1];
114                 p = &buf[0];
115
116                 for (i = 0; i < num_path_components; i++) {
117                         *p++ = '.';
118                         *p++ = '.';
119                         *p++ = '/';
120                 }
121                 p2 = lte->extracted_file;
122                 while (*p2 == '/')
123                         p2++;
124                 while (num_output_dir_path_components > 0) {
125                         p2 = path_next_part(p2);
126                         num_output_dir_path_components--;
127                 }
128                 strcpy(p, p2);
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;
133                 }
134         }
135         return 0;
136 }
137
138 static int
139 symlink_apply_unix_data(const char *link,
140                         const struct wimlib_unix_data *unix_data)
141 {
142         if (lchown(link, unix_data->uid, unix_data->gid)) {
143                 if (errno == EPERM) {
144                         /* Ignore */
145                         WARNING_WITH_ERRNO("failed to set symlink UNIX "
146                                            "owner/group on \"%s\"", link);
147                 } else {
148                         ERROR_WITH_ERRNO("failed to set symlink UNIX "
149                                          "owner/group on \"%s\"", link);
150                         return WIMLIB_ERR_INVALID_DENTRY;
151                 }
152         }
153         return 0;
154 }
155
156 static int
157 fd_apply_unix_data(int fd, const char *path,
158                    const struct wimlib_unix_data *unix_data,
159                    int extract_flags)
160 {
161         if (extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS)
162                 return 0;
163
164         if (fchown(fd, unix_data->uid, unix_data->gid)) {
165                 if (errno == EPERM &&
166                     !(extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
167                 {
168                         WARNING_WITH_ERRNO("failed to set file UNIX "
169                                            "owner/group on \"%s\"", path);
170                 } else {
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 :
174                                 WIMLIB_ERR_WRITE;
175                 }
176         }
177
178         if (fchmod(fd, unix_data->mode)) {
179                 if (errno == EPERM &&
180                     !(extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
181                 {
182                         WARNING_WITH_ERRNO("failed to set UNIX file mode "
183                                            "on \"%s\"", path);
184                 } else {
185                         ERROR_WITH_ERRNO("failed to set UNIX file mode "
186                                          "on \"%s\"", path);
187                         return (errno == EPERM) ? WIMLIB_ERR_INSUFFICIENT_PRIVILEGES_TO_EXTRACT :
188                                 WIMLIB_ERR_WRITE;
189                 }
190         }
191         return 0;
192 }
193
194 static int
195 dir_apply_unix_data(const char *dir, const struct wimlib_unix_data *unix_data,
196                     int extract_flags)
197 {
198         int dfd = open(dir, O_RDONLY);
199         int ret;
200         if (dfd >= 0) {
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;
205                 }
206         } else {
207                 ERROR_WITH_ERRNO("can't open directory `%s'", dir);
208                 ret = WIMLIB_ERR_OPENDIR;
209         }
210         return ret;
211 }
212
213 static int
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)
218 {
219         /* Normal mode of extraction.  Regular files and hard links are
220          * extracted in the way that they appear in the WIM. */
221
222         int out_fd;
223         int ret;
224         struct wim_inode *inode = dentry->d_inode;
225
226         if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
227                 && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
228                                      WIMLIB_EXTRACT_FLAG_HARDLINK))))
229         {
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 "
242                                                          "`%s' to `%s'",
243                                                          output_path,
244                                                          inode->i_extracted_file);
245                                         return WIMLIB_ERR_LINK;
246                                 }
247                                 return 0;
248                         }
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;
254                         }
255                 }
256         }
257
258         /* Extract the contents of the file to @output_path. */
259
260         out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
261         if (out_fd == -1) {
262                 ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
263                                  output_path);
264                 return WIMLIB_ERR_OPEN;
265         }
266
267         if (!lte) {
268                 /* Empty file with no lookup table entry */
269                 DEBUG("Empty file `%s'.", output_path);
270                 ret = 0;
271                 goto out_extract_unix_data;
272         }
273
274         ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
275         if (ret) {
276                 ERROR("Failed to extract resource to `%s'", output_path);
277                 goto out;
278         }
279
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);
284                 if (ret > 0)
285                         ;
286                 else if (ret < 0)
287                         ret = 0;
288                 else
289                         ret = fd_apply_unix_data(out_fd, output_path, &unix_data,
290                                                  args->extract_flags);
291                 if (ret)
292                         goto out;
293         }
294         if (lte)
295                 args->progress.extract.completed_bytes += wim_resource_size(lte);
296 out:
297         if (close(out_fd) != 0) {
298                 ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
299                 if (ret == 0)
300                         ret = WIMLIB_ERR_WRITE;
301         }
302         return ret;
303 }
304
305 static int
306 unix_extract_regular_file(struct wim_dentry *dentry,
307                           struct apply_args *args,
308                           const char *output_path)
309 {
310         struct wim_lookup_table_entry *lte;
311         const struct wim_inode *inode = dentry->d_inode;
312
313         lte = inode_unnamed_lte_resolved(inode);
314
315         if (lte && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
316                                            WIMLIB_EXTRACT_FLAG_HARDLINK)))
317         {
318                 if (lte->extracted_file) {
319                         return unix_extract_regular_file_linked(dentry,
320                                                                 output_path,
321                                                                 args, lte);
322                 } else {
323                         lte->extracted_file = STRDUP(output_path);
324                         if (!lte->extracted_file)
325                                 return WIMLIB_ERR_NOMEM;
326                 }
327         }
328         return unix_extract_regular_file_unlinked(dentry, args, output_path, lte);
329 }
330
331 static int
332 unix_extract_symlink(struct wim_dentry *dentry,
333                      struct apply_args *args,
334                      const char *output_path)
335 {
336         char target[4096 + args->target_realpath_len];
337         char *fixed_target;
338         const struct wim_inode *inode = dentry->d_inode;
339
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;
344
345         if (ret <= 0) {
346                 ERROR("Could not read the symbolic link from dentry `%s'",
347                       dentry_full_path(dentry));
348                 return WIMLIB_ERR_INVALID_DENTRY;
349         }
350         target[args->target_realpath_len + ret] = '\0';
351         if (target[args->target_realpath_len] == '/' &&
352             args->extract_flags & WIMLIB_EXTRACT_FLAG_RPFIX)
353         {
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;
359         } else {
360                 /* Keep same link target */
361                 fixed_target = target + args->target_realpath_len;
362         }
363         ret = symlink(fixed_target, output_path);
364         if (ret) {
365                 ERROR_WITH_ERRNO("Failed to symlink `%s' to `%s'",
366                                  output_path, fixed_target);
367                 return WIMLIB_ERR_LINK;
368         }
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);
372                 if (ret > 0)
373                         ;
374                 else if (ret < 0)
375                         ret = 0;
376                 else
377                         ret = symlink_apply_unix_data(output_path, &unix_data);
378                 if (ret)
379                         return ret;
380         }
381         lte = inode_unnamed_lte_resolved(inode);
382         wimlib_assert(lte != NULL);
383         args->progress.extract.completed_bytes += wim_resource_size(lte);
384         return 0;
385 }
386
387 static int
388 unix_extract_directory(struct wim_dentry *dentry, const char *output_path,
389                        int extract_flags)
390 {
391         int ret;
392         struct stat stbuf;
393
394         ret = stat(output_path, &stbuf);
395         if (ret == 0) {
396                 if (S_ISDIR(stbuf.st_mode)) {
397                         goto dir_exists;
398                 } else {
399                         ERROR("\"%s\" is not a directory", output_path);
400                         return WIMLIB_ERR_MKDIR;
401                 }
402         } else {
403                 if (errno != ENOENT) {
404                         ERROR_WITH_ERRNO("Failed to stat \"%s\"", output_path);
405                         return WIMLIB_ERR_STAT;
406                 }
407         }
408
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;
412         }
413 dir_exists:
414         ret = 0;
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);
418                 if (ret > 0)
419                         ;
420                 else if (ret < 0)
421                         ret = 0;
422                 else
423                         ret = dir_apply_unix_data(output_path, &unix_data,
424                                                   extract_flags);
425         }
426         return ret;
427 }
428
429 int
430 unix_do_apply_dentry(const char *output_path, size_t output_path_len,
431                      struct wim_dentry *dentry, struct apply_args *args)
432 {
433         const struct wim_inode *inode = dentry->d_inode;
434
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);
439         else
440                 return unix_extract_regular_file(dentry, args, output_path);
441 }
442
443 int
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)
448 {
449         int ret;
450         const struct wim_inode *inode = dentry->d_inode;
451
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. */
456
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);
461         if (ret)
462                 ret = errno;
463 #else
464         ret = ENOSYS;
465 #endif
466
467         if (ret == ENOSYS) {
468                 /* utimensat() not implemented or not available */
469         #ifdef HAVE_LUTIMES
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);
477                 if (ret)
478                         ret = errno;
479         #endif
480         }
481
482         if (ret == ENOSYS) {
483                 /* utimensat() and lutimes() both not implemented or not
484                  * available */
485         #ifdef HAVE_UTIME
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. */
489                 struct utimbuf buf;
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);
493         #endif
494         }
495         if (ret && args->num_utime_warnings < 10) {
496                 WARNING_WITH_ERRNO("Failed to set timestamp on file `%s'",
497                                     output_path);
498                 args->num_utime_warnings++;
499         }
500         return 0;
501 }
502
503 #endif /* !__WIN32__ */