]> wimlib.net Git - wimlib/blob - src/unix_capture.c
Add new helper function for ignoring scanned directory entries
[wimlib] / src / unix_capture.c
1 /*
2  * unix_capture.c:  Capture a directory tree 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 #ifndef __WIN32__
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <limits.h> /* for PATH_MAX */
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include "wimlib/blob_table.h"
36 #include "wimlib/capture.h"
37 #include "wimlib/dentry.h"
38 #include "wimlib/error.h"
39 #include "wimlib/reparse.h"
40 #include "wimlib/timestamp.h"
41 #include "wimlib/unix_data.h"
42
43 #ifdef HAVE_FDOPENDIR
44 #  define my_fdopendir(dirfd_p) fdopendir(*(dirfd_p))
45 #else
46 static DIR *
47 my_fdopendir(int *dirfd_p)
48 {
49         DIR *dir = NULL;
50         int old_pwd;
51
52         old_pwd = open(".", O_RDONLY);
53         if (old_pwd >= 0) {
54                 if (!fchdir(*dirfd_p)) {
55                         dir = opendir(".");
56                         if (dir) {
57                                 close(*dirfd_p);
58                                 *dirfd_p = dirfd(dir);
59                         }
60                         fchdir(old_pwd);
61                 }
62                 close(old_pwd);
63         }
64         return dir;
65 }
66 #endif
67
68 #ifdef HAVE_OPENAT
69 #  define my_openat(full_path, dirfd, relpath, flags) \
70                 openat((dirfd), (relpath), (flags))
71 #else
72 #  define my_openat(full_path, dirfd, relpath, flags) \
73                 open((full_path), (flags))
74 #endif
75
76 #ifdef HAVE_READLINKAT
77 #  define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
78                 readlinkat((dirfd), (relpath), (buf), (bufsize))
79 #else
80 #  define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
81                 readlink((full_path), (buf), (bufsize))
82 #endif
83
84 #ifdef HAVE_FSTATAT
85 #  define my_fstatat(full_path, dirfd, relpath, stbuf, flags)   \
86         fstatat((dirfd), (relpath), (stbuf), (flags))
87 #else
88 #  define my_fstatat(full_path, dirfd, relpath, stbuf, flags)   \
89         ((flags) & AT_SYMLINK_NOFOLLOW) ? \
90                 lstat((full_path), (stbuf)) : \
91                 stat((full_path), (stbuf))
92 #endif
93
94 #ifndef AT_FDCWD
95 #  define AT_FDCWD      -100
96 #endif
97
98 #ifndef AT_SYMLINK_NOFOLLOW
99 #  define AT_SYMLINK_NOFOLLOW   0x100
100 #endif
101
102 static int
103 unix_scan_regular_file(const char *path, u64 size, struct wim_inode *inode,
104                        struct list_head *unhashed_blobs)
105 {
106         struct blob_descriptor *blob = NULL;
107         struct wim_inode_stream *strm;
108
109         inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
110
111         if (size) {
112                 blob = new_blob_descriptor();
113                 if (unlikely(!blob))
114                         goto err_nomem;
115                 blob->file_on_disk = STRDUP(path);
116                 if (unlikely(!blob->file_on_disk))
117                         goto err_nomem;
118                 blob->blob_location = BLOB_IN_FILE_ON_DISK;
119                 blob->size = size;
120                 blob->file_inode = inode;
121         }
122
123         strm = inode_add_stream(inode, STREAM_TYPE_DATA, NO_STREAM_NAME, blob);
124         if (unlikely(!strm))
125                 goto err_nomem;
126
127         prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
128         return 0;
129
130 err_nomem:
131         free_blob_descriptor(blob);
132         return WIMLIB_ERR_NOMEM;
133 }
134
135 static int
136 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
137                                  char *path, size_t path_len,
138                                  int dirfd, const char *relpath,
139                                  struct capture_params *params);
140
141 static int
142 unix_scan_directory(struct wim_dentry *dir_dentry,
143                     char *full_path, size_t full_path_len,
144                     int parent_dirfd, const char *dir_relpath,
145                     struct capture_params *params)
146 {
147
148         int dirfd;
149         DIR *dir;
150         int ret;
151
152         dirfd = my_openat(full_path, parent_dirfd, dir_relpath, O_RDONLY);
153         if (dirfd < 0) {
154                 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
155                 return WIMLIB_ERR_OPENDIR;
156         }
157
158         dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
159         dir = my_fdopendir(&dirfd);
160         if (!dir) {
161                 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
162                 close(dirfd);
163                 return WIMLIB_ERR_OPENDIR;
164         }
165
166         ret = 0;
167         for (;;) {
168                 struct dirent *entry;
169                 struct wim_dentry *child;
170                 size_t name_len;
171
172                 errno = 0;
173                 entry = readdir(dir);
174                 if (!entry) {
175                         if (errno) {
176                                 ret = WIMLIB_ERR_READ;
177                                 ERROR_WITH_ERRNO("\"%s\": Error reading directory",
178                                                  full_path);
179                         }
180                         break;
181                 }
182
183                 name_len = strlen(entry->d_name);
184
185                 if (should_ignore_filename(entry->d_name, name_len))
186                         continue;
187
188                 full_path[full_path_len] = '/';
189                 memcpy(&full_path[full_path_len + 1], entry->d_name, name_len + 1);
190                 ret = unix_build_dentry_tree_recursive(&child,
191                                                        full_path,
192                                                        full_path_len + 1 + name_len,
193                                                        dirfd,
194                                                        &full_path[full_path_len + 1],
195                                                        params);
196                 full_path[full_path_len] = '\0';
197                 if (ret)
198                         break;
199                 if (child)
200                         dentry_add_child(dir_dentry, child);
201         }
202         closedir(dir);
203         return ret;
204 }
205
206 /*
207  * Given an absolute symbolic link target (UNIX-style, beginning with '/'),
208  * determine whether it points into the directory identified by @ino and @dev.
209  * If yes, return the suffix of @target which is relative to this directory, but
210  * retaining leading slashes.  If no, return @target.
211  *
212  * Here are some examples, assuming that the @ino/@dev directory is "/home/e":
213  *
214  *      Original target         New target
215  *      ---------------         ----------
216  *      /home/e/test            /test
217  *      /home/e/test/           /test/
218  *      //home//e//test//       //test//
219  *      /home/e                                         (empty string)
220  *      /home/e/                /
221  *      /usr/lib                /usr/lib                (external link)
222  *
223  * Because of the possibility of other links into the @ino/@dev directory and/or
224  * multiple path separators, we can't simply do a string comparison; instead we
225  * need to stat() each ancestor directory.
226  *
227  * If the link points directly to the @ino/@dev directory with no trailing
228  * slashes, then the new target will be an empty string.  This is not a valid
229  * UNIX symlink target, but we store this in the archive anyway since the target
230  * is intended to be de-relativized when the link is extracted.
231  */
232 static char *
233 unix_relativize_link_target(char *target, u64 ino, u64 dev)
234 {
235         char *p = target;
236
237         do {
238                 char save;
239                 struct stat stbuf;
240                 int ret;
241
242                 /* Skip slashes (guaranteed to be at least one here)  */
243                 do {
244                         p++;
245                 } while (*p == '/');
246
247                 /* End of string?  */
248                 if (!*p)
249                         break;
250
251                 /* Skip non-slashes (guaranteed to be at least one here)  */
252                 do {
253                         p++;
254                 } while (*p && *p != '/');
255
256                 /* Get the inode and device numbers for this prefix.  */
257                 save = *p;
258                 *p = '\0';
259                 ret = stat(target, &stbuf);
260                 *p = save;
261
262                 if (ret) {
263                         /* stat() failed.  Assume the link points outside the
264                          * directory tree being captured.  */
265                         break;
266                 }
267
268                 if (stbuf.st_ino == ino && stbuf.st_dev == dev) {
269                         /* Link points inside directory tree being captured.
270                          * Return abbreviated path.  */
271                         return p;
272                 }
273         } while (*p);
274
275         /* Link does not point inside directory tree being captured.  */
276         return target;
277 }
278
279 static noinline_for_stack int
280 unix_scan_symlink(const char *full_path, int dirfd, const char *relpath,
281                   struct wim_inode *inode, struct capture_params *params)
282 {
283         char orig_target[REPARSE_POINT_MAX_SIZE];
284         char *target = orig_target;
285         int ret;
286
287         /* Read the UNIX symbolic link target.  */
288         ret = my_readlinkat(full_path, dirfd, relpath, target,
289                             sizeof(orig_target));
290         if (unlikely(ret < 0)) {
291                 ERROR_WITH_ERRNO("\"%s\": Can't read target of symbolic link",
292                                  full_path);
293                 return WIMLIB_ERR_READLINK;
294         }
295         if (unlikely(ret >= sizeof(orig_target))) {
296                 ERROR("\"%s\": target of symbolic link is too long", full_path);
297                 return WIMLIB_ERR_READLINK;
298         }
299         target[ret] = '\0';
300
301         /* If the link is absolute and reparse point fixups are enabled, then
302          * change it to be "absolute" relative to the tree being captured.  */
303         if (target[0] == '/' && (params->add_flags & WIMLIB_ADD_FLAG_RPFIX)) {
304                 int status = WIMLIB_SCAN_DENTRY_NOT_FIXED_SYMLINK;
305
306                 params->progress.scan.cur_path = full_path;
307                 params->progress.scan.symlink_target = target;
308
309                 target = unix_relativize_link_target(target,
310                                                      params->capture_root_ino,
311                                                      params->capture_root_dev);
312                 if (target != orig_target) {
313                         /* Link target was fixed.  */
314                         inode->i_rp_flags &= ~WIM_RP_FLAG_NOT_FIXED;
315                         status = WIMLIB_SCAN_DENTRY_FIXED_SYMLINK;
316                 }
317                 ret = do_capture_progress(params, status, NULL);
318                 if (ret)
319                         return ret;
320         }
321
322         /* Translate the UNIX symlink target into a Windows reparse point.  */
323         ret = wim_inode_set_symlink(inode, target, params->blob_table);
324         if (ret)
325                 return ret;
326
327         /* On Windows, a reparse point can be set on both directory and
328          * non-directory files.  Usually, a link that is intended to point to a
329          * (non-)directory is stored as a reparse point on a (non-)directory
330          * file.  Replicate this behavior by examining the target file.  */
331         struct stat stbuf;
332         if (my_fstatat(full_path, dirfd, relpath, &stbuf, 0) == 0 &&
333             S_ISDIR(stbuf.st_mode))
334                 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
335         return 0;
336 }
337
338 static int
339 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
340                                  char *full_path, size_t full_path_len,
341                                  int dirfd, const char *relpath,
342                                  struct capture_params *params)
343 {
344         struct wim_dentry *tree = NULL;
345         struct wim_inode *inode = NULL;
346         int ret;
347         struct stat stbuf;
348         int stat_flags;
349
350         ret = try_exclude(full_path, params);
351         if (unlikely(ret < 0)) /* Excluded? */
352                 goto out_progress;
353         if (unlikely(ret > 0)) /* Error? */
354                 goto out;
355
356         if (params->add_flags & (WIMLIB_ADD_FLAG_DEREFERENCE |
357                                  WIMLIB_ADD_FLAG_ROOT))
358                 stat_flags = 0;
359         else
360                 stat_flags = AT_SYMLINK_NOFOLLOW;
361
362         ret = my_fstatat(full_path, dirfd, relpath, &stbuf, stat_flags);
363
364         if (ret) {
365                 ERROR_WITH_ERRNO("\"%s\": Can't read metadata", full_path);
366                 ret = WIMLIB_ERR_STAT;
367                 goto out;
368         }
369
370         if (!(params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA)) {
371                 if (unlikely(!S_ISREG(stbuf.st_mode) &&
372                              !S_ISDIR(stbuf.st_mode) &&
373                              !S_ISLNK(stbuf.st_mode)))
374                 {
375                         if (params->add_flags &
376                             WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
377                         {
378                                 ERROR("\"%s\": File type is unsupported",
379                                       full_path);
380                                 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
381                                 goto out;
382                         }
383                         params->progress.scan.cur_path = full_path;
384                         ret = do_capture_progress(params,
385                                                   WIMLIB_SCAN_DENTRY_UNSUPPORTED,
386                                                   NULL);
387                         goto out;
388                 }
389         }
390
391         ret = inode_table_new_dentry(params->inode_table, relpath,
392                                      stbuf.st_ino, stbuf.st_dev,
393                                      S_ISDIR(stbuf.st_mode), &tree);
394         if (ret)
395                 goto out;
396
397         inode = tree->d_inode;
398
399         /* Already seen this inode?  */
400         if (inode->i_nlink > 1)
401                 goto out_progress;
402
403 #ifdef HAVE_STAT_NANOSECOND_PRECISION
404         inode->i_creation_time = timespec_to_wim_timestamp(&stbuf.st_mtim);
405         inode->i_last_write_time = timespec_to_wim_timestamp(&stbuf.st_mtim);
406         inode->i_last_access_time = timespec_to_wim_timestamp(&stbuf.st_atim);
407 #else
408         inode->i_creation_time = time_t_to_wim_timestamp(stbuf.st_mtime);
409         inode->i_last_write_time = time_t_to_wim_timestamp(stbuf.st_mtime);
410         inode->i_last_access_time = time_t_to_wim_timestamp(stbuf.st_atime);
411 #endif
412         if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
413                 struct wimlib_unix_data unix_data;
414
415                 unix_data.uid = stbuf.st_uid;
416                 unix_data.gid = stbuf.st_gid;
417                 unix_data.mode = stbuf.st_mode;
418                 unix_data.rdev = stbuf.st_rdev;
419                 if (!inode_set_unix_data(inode, &unix_data, UNIX_DATA_ALL)) {
420                         ret = WIMLIB_ERR_NOMEM;
421                         goto out;
422                 }
423         }
424
425         if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
426                 params->capture_root_ino = stbuf.st_ino;
427                 params->capture_root_dev = stbuf.st_dev;
428                 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
429         }
430
431         if (S_ISREG(stbuf.st_mode)) {
432                 ret = unix_scan_regular_file(full_path, stbuf.st_size,
433                                              inode, params->unhashed_blobs);
434         } else if (S_ISDIR(stbuf.st_mode)) {
435                 ret = unix_scan_directory(tree, full_path, full_path_len,
436                                           dirfd, relpath, params);
437         } else if (S_ISLNK(stbuf.st_mode)) {
438                 ret = unix_scan_symlink(full_path, dirfd, relpath,
439                                         inode, params);
440         }
441
442         if (ret)
443                 goto out;
444
445 out_progress:
446         params->progress.scan.cur_path = full_path;
447         if (likely(tree))
448                 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
449         else
450                 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
451 out:
452         if (unlikely(ret)) {
453                 free_dentry_tree(tree, params->blob_table);
454                 tree = NULL;
455                 ret = report_capture_error(params, ret, full_path);
456         }
457         *tree_ret = tree;
458         return ret;
459 }
460
461 /*
462  * unix_build_dentry_tree():
463  *      Builds a tree of WIM dentries from an on-disk directory tree (UNIX
464  *      version; no NTFS-specific data is captured).
465  *
466  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Only
467  *              modified if successful.  Set to NULL if the file or directory was
468  *              excluded from capture.
469  *
470  * @root_disk_path:  The path to the root of the directory tree on disk.
471  *
472  * @params:     See doc for `struct capture_params'.
473  *
474  * @return:     0 on success, nonzero on failure.  It is a failure if any of
475  *              the files cannot be `stat'ed, or if any of the needed
476  *              directories cannot be opened or read.  Failure to add the files
477  *              to the WIM may still occur later when trying to actually read
478  *              the on-disk files during a call to wimlib_write() or
479  *              wimlib_overwrite().
480  */
481 int
482 unix_build_dentry_tree(struct wim_dentry **root_ret,
483                        const char *root_disk_path,
484                        struct capture_params *params)
485 {
486         size_t path_len;
487         size_t path_bufsz;
488         char *path_buf;
489         int ret;
490
491         path_len = strlen(root_disk_path);
492         path_bufsz = min(32790, PATH_MAX + 1);
493
494         if (path_len >= path_bufsz)
495                 return WIMLIB_ERR_INVALID_PARAM;
496
497         path_buf = MALLOC(path_bufsz);
498         if (!path_buf)
499                 return WIMLIB_ERR_NOMEM;
500         memcpy(path_buf, root_disk_path, path_len + 1);
501
502         params->capture_root_nchars = path_len;
503
504         ret = unix_build_dentry_tree_recursive(root_ret, path_buf, path_len,
505                                                AT_FDCWD, path_buf, params);
506         FREE(path_buf);
507         return ret;
508 }
509
510 #endif /* !__WIN32__ */