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