]> wimlib.net Git - wimlib/blob - src/unix_capture.c
Various cleanups
[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                 if (entry->d_name[0] == '.' &&
184                     (entry->d_name[1] == '\0' ||
185                      (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
186                         continue;
187
188                 full_path[full_path_len] = '/';
189                 name_len = strlen(entry->d_name);
190                 memcpy(&full_path[full_path_len + 1], entry->d_name, name_len + 1);
191                 ret = unix_build_dentry_tree_recursive(&child,
192                                                        full_path,
193                                                        full_path_len + 1 + name_len,
194                                                        dirfd,
195                                                        &full_path[full_path_len + 1],
196                                                        params);
197                 full_path[full_path_len] = '\0';
198                 if (ret)
199                         break;
200                 if (child)
201                         dentry_add_child(dir_dentry, child);
202         }
203         closedir(dir);
204         return ret;
205 }
206
207 /* Given an absolute symbolic link target @dest (UNIX-style, beginning
208  * with '/'), determine whether it points into the directory specified by
209  * @ino and @dev.  If so, return the target modified to be "absolute"
210  * relative to this directory.  Otherwise, return NULL.  */
211 static char *
212 unix_fixup_abslink(char *dest, u64 ino, u64 dev)
213 {
214         char *p = dest;
215
216         do {
217                 char save;
218                 struct stat stbuf;
219                 int ret;
220
221                 /* Skip non-slashes.  */
222                 while (*p && *p != '/')
223                         p++;
224
225                 /* Skip slashes.  */
226                 while (*p && *p == '/')
227                         p++;
228
229                 /* Get inode and device for this prefix.  */
230                 save = *p;
231                 *p = '\0';
232                 ret = stat(dest, &stbuf);
233                 *p = save;
234
235                 if (ret) {
236                         /* stat() failed.  Assume the link points outside the
237                          * directory tree being captured.  */
238                         break;
239                 }
240
241                 if (stbuf.st_ino == ino && stbuf.st_dev == dev) {
242                         /* Link points inside directory tree being captured.
243                          * Return abbreviated path.  */
244                         *--p = '/';
245                         while (p > dest && *(p - 1) == '/')
246                                 p--;
247                         return p;
248                 }
249         } while (*p);
250
251         /* Link does not point inside directory tree being captured.  */
252         return NULL;
253 }
254
255 static int
256 unix_scan_symlink(const char *full_path, int dirfd, const char *relpath,
257                   struct wim_inode *inode, struct capture_params *params)
258 {
259         char deref_name_buf[4096];
260         ssize_t deref_name_len;
261         char *dest;
262         int ret;
263
264         inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
265         inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
266
267         /* The idea here is to call readlink() to get the UNIX target of the
268          * symbolic link, then turn the target into a reparse point data buffer
269          * that contains a relative or absolute symbolic link. */
270         deref_name_len = my_readlinkat(full_path, dirfd, relpath,
271                                        deref_name_buf, sizeof(deref_name_buf) - 1);
272         if (deref_name_len < 0) {
273                 ERROR_WITH_ERRNO("\"%s\": Can't read target of symbolic link",
274                                  full_path);
275                 return WIMLIB_ERR_READLINK;
276         }
277
278         dest = deref_name_buf;
279
280         dest[deref_name_len] = '\0';
281
282         if ((params->add_flags & WIMLIB_ADD_FLAG_RPFIX) &&
283              dest[0] == '/')
284         {
285                 char *fixed_dest;
286
287                 /* RPFIX (reparse point fixup) mode:  Change target of absolute
288                  * symbolic link to be "absolute" relative to the tree being
289                  * captured.  */
290                 fixed_dest = unix_fixup_abslink(dest,
291                                                 params->capture_root_ino,
292                                                 params->capture_root_dev);
293                 params->progress.scan.cur_path = full_path;
294                 params->progress.scan.symlink_target = deref_name_buf;
295                 if (fixed_dest) {
296                         /* Link points inside the tree being captured, so it was
297                          * fixed.  */
298                         inode->i_not_rpfixed = 0;
299                         dest = fixed_dest;
300                         ret = do_capture_progress(params,
301                                                   WIMLIB_SCAN_DENTRY_FIXED_SYMLINK,
302                                                   NULL);
303                 } else {
304                         /* Link points outside the tree being captured, so it
305                          * was not fixed.  */
306                         ret = do_capture_progress(params,
307                                                   WIMLIB_SCAN_DENTRY_NOT_FIXED_SYMLINK,
308                                                   NULL);
309                 }
310                 if (ret)
311                         return ret;
312         }
313         ret = wim_inode_set_symlink(inode, dest, params->blob_table);
314         if (ret)
315                 return ret;
316
317         /* Unfortunately, Windows seems to have the concept of "file" symbolic
318          * links as being different from "directory" symbolic links...  so
319          * FILE_ATTRIBUTE_DIRECTORY needs to be set on the symbolic link if the
320          * *target* of the symbolic link is a directory.  */
321         struct stat stbuf;
322         if (my_fstatat(full_path, dirfd, relpath, &stbuf, 0) == 0 &&
323             S_ISDIR(stbuf.st_mode))
324                 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
325         return 0;
326 }
327
328 static int
329 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
330                                  char *full_path, size_t full_path_len,
331                                  int dirfd, const char *relpath,
332                                  struct capture_params *params)
333 {
334         struct wim_dentry *tree = NULL;
335         struct wim_inode *inode = NULL;
336         int ret;
337         struct stat stbuf;
338         int stat_flags;
339
340         ret = try_exclude(full_path, full_path_len, params);
341         if (ret < 0) /* Excluded? */
342                 goto out_progress;
343         if (ret > 0) /* Error? */
344                 goto out;
345
346         if (params->add_flags & (WIMLIB_ADD_FLAG_DEREFERENCE |
347                                  WIMLIB_ADD_FLAG_ROOT))
348                 stat_flags = 0;
349         else
350                 stat_flags = AT_SYMLINK_NOFOLLOW;
351
352         ret = my_fstatat(full_path, dirfd, relpath, &stbuf, stat_flags);
353
354         if (ret) {
355                 ERROR_WITH_ERRNO("\"%s\": Can't read metadata", full_path);
356                 ret = WIMLIB_ERR_STAT;
357                 goto out;
358         }
359
360         if (!(params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA)) {
361                 if (unlikely(!S_ISREG(stbuf.st_mode) &&
362                              !S_ISDIR(stbuf.st_mode) &&
363                              !S_ISLNK(stbuf.st_mode)))
364                 {
365                         if (params->add_flags &
366                             WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
367                         {
368                                 ERROR("\"%s\": File type is unsupported",
369                                       full_path);
370                                 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
371                                 goto out;
372                         }
373                         params->progress.scan.cur_path = full_path;
374                         ret = do_capture_progress(params,
375                                                   WIMLIB_SCAN_DENTRY_UNSUPPORTED,
376                                                   NULL);
377                         goto out;
378                 }
379         }
380
381         ret = inode_table_new_dentry(params->inode_table, relpath,
382                                      stbuf.st_ino, stbuf.st_dev,
383                                      S_ISDIR(stbuf.st_mode), &tree);
384         if (ret)
385                 goto out;
386
387         inode = tree->d_inode;
388
389         /* Already seen this inode?  */
390         if (inode->i_nlink > 1)
391                 goto out_progress;
392
393 #ifdef HAVE_STAT_NANOSECOND_PRECISION
394         inode->i_creation_time = timespec_to_wim_timestamp(&stbuf.st_mtim);
395         inode->i_last_write_time = timespec_to_wim_timestamp(&stbuf.st_mtim);
396         inode->i_last_access_time = timespec_to_wim_timestamp(&stbuf.st_atim);
397 #else
398         inode->i_creation_time = time_t_to_wim_timestamp(stbuf.st_mtime);
399         inode->i_last_write_time = time_t_to_wim_timestamp(stbuf.st_mtime);
400         inode->i_last_access_time = time_t_to_wim_timestamp(stbuf.st_atime);
401 #endif
402         if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
403                 struct wimlib_unix_data unix_data;
404
405                 unix_data.uid = stbuf.st_uid;
406                 unix_data.gid = stbuf.st_gid;
407                 unix_data.mode = stbuf.st_mode;
408                 unix_data.rdev = stbuf.st_rdev;
409                 if (!inode_set_unix_data(inode, &unix_data, UNIX_DATA_ALL)) {
410                         ret = WIMLIB_ERR_NOMEM;
411                         goto out;
412                 }
413         }
414
415         if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
416                 params->capture_root_ino = stbuf.st_ino;
417                 params->capture_root_dev = stbuf.st_dev;
418                 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
419         }
420
421         if (S_ISREG(stbuf.st_mode)) {
422                 ret = unix_scan_regular_file(full_path, stbuf.st_size,
423                                              inode, params->unhashed_blobs);
424         } else if (S_ISDIR(stbuf.st_mode)) {
425                 ret = unix_scan_directory(tree, full_path, full_path_len,
426                                           dirfd, relpath, params);
427         } else if (S_ISLNK(stbuf.st_mode)) {
428                 ret = unix_scan_symlink(full_path, dirfd, relpath,
429                                         inode, params);
430         }
431
432         if (ret)
433                 goto out;
434
435 out_progress:
436         params->progress.scan.cur_path = full_path;
437         if (likely(tree))
438                 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
439         else
440                 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
441 out:
442         if (unlikely(ret)) {
443                 free_dentry_tree(tree, params->blob_table);
444                 tree = NULL;
445                 ret = report_capture_error(params, ret, full_path);
446         }
447         *tree_ret = tree;
448         return ret;
449 }
450
451 /*
452  * unix_build_dentry_tree():
453  *      Builds a tree of WIM dentries from an on-disk directory tree (UNIX
454  *      version; no NTFS-specific data is captured).
455  *
456  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Only
457  *              modified if successful.  Set to NULL if the file or directory was
458  *              excluded from capture.
459  *
460  * @root_disk_path:  The path to the root of the directory tree on disk.
461  *
462  * @params:     See doc for `struct capture_params'.
463  *
464  * @return:     0 on success, nonzero on failure.  It is a failure if any of
465  *              the files cannot be `stat'ed, or if any of the needed
466  *              directories cannot be opened or read.  Failure to add the files
467  *              to the WIM may still occur later when trying to actually read
468  *              the on-disk files during a call to wimlib_write() or
469  *              wimlib_overwrite().
470  */
471 int
472 unix_build_dentry_tree(struct wim_dentry **root_ret,
473                        const char *root_disk_path,
474                        struct capture_params *params)
475 {
476         size_t path_len;
477         size_t path_bufsz;
478         char *path_buf;
479         int ret;
480
481         path_len = strlen(root_disk_path);
482         path_bufsz = min(32790, PATH_MAX + 1);
483
484         if (path_len >= path_bufsz)
485                 return WIMLIB_ERR_INVALID_PARAM;
486
487         path_buf = MALLOC(path_bufsz);
488         if (!path_buf)
489                 return WIMLIB_ERR_NOMEM;
490         memcpy(path_buf, root_disk_path, path_len + 1);
491
492         params->capture_root_nchars = path_len;
493
494         ret = unix_build_dentry_tree_recursive(root_ret, path_buf, path_len,
495                                                AT_FDCWD, path_buf, params);
496         FREE(path_buf);
497         return ret;
498 }
499
500 #endif /* !__WIN32__ */