Rename add_image_params => capture_params
[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/capture.h"
36 #include "wimlib/dentry.h"
37 #include "wimlib/error.h"
38 #include "wimlib/lookup_table.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_streams)
105 {
106         struct wim_lookup_table_entry *lte;
107         char *file_on_disk;
108
109         inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
110
111         /* Empty files do not have to have a lookup table entry. */
112         if (!size)
113                 return 0;
114
115         file_on_disk = STRDUP(path);
116         if (!file_on_disk)
117                 return WIMLIB_ERR_NOMEM;
118         lte = new_lookup_table_entry();
119         if (!lte) {
120                 FREE(file_on_disk);
121                 return WIMLIB_ERR_NOMEM;
122         }
123         lte->file_on_disk = file_on_disk;
124         lte->file_inode = inode;
125         lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
126         lte->size = size;
127         add_unhashed_stream(lte, inode, 0, unhashed_streams);
128         inode->i_lte = lte;
129         return 0;
130 }
131
132 static int
133 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
134                                  char *path, size_t path_len,
135                                  int dirfd, const char *relpath,
136                                  struct capture_params *params);
137
138 static int
139 unix_scan_directory(struct wim_dentry *dir_dentry,
140                     char *full_path, size_t full_path_len,
141                     int parent_dirfd, const char *dir_relpath,
142                     struct capture_params *params)
143 {
144
145         int dirfd;
146         DIR *dir;
147         int ret;
148
149         dirfd = my_openat(full_path, parent_dirfd, dir_relpath, O_RDONLY);
150         if (dirfd < 0) {
151                 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
152                 return WIMLIB_ERR_OPENDIR;
153         }
154
155         dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
156         dir = my_fdopendir(&dirfd);
157         if (!dir) {
158                 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
159                 close(dirfd);
160                 return WIMLIB_ERR_OPENDIR;
161         }
162
163         ret = 0;
164         for (;;) {
165                 struct dirent *entry;
166                 struct wim_dentry *child;
167                 size_t name_len;
168
169                 errno = 0;
170                 entry = readdir(dir);
171                 if (!entry) {
172                         if (errno) {
173                                 ret = WIMLIB_ERR_READ;
174                                 ERROR_WITH_ERRNO("\"%s\": Error reading directory",
175                                                  full_path);
176                         }
177                         break;
178                 }
179
180                 if (entry->d_name[0] == '.' &&
181                     (entry->d_name[1] == '\0' ||
182                      (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
183                         continue;
184
185                 full_path[full_path_len] = '/';
186                 name_len = strlen(entry->d_name);
187                 memcpy(&full_path[full_path_len + 1], entry->d_name, name_len + 1);
188                 ret = unix_build_dentry_tree_recursive(&child,
189                                                        full_path,
190                                                        full_path_len + 1 + name_len,
191                                                        dirfd,
192                                                        &full_path[full_path_len + 1],
193                                                        params);
194                 full_path[full_path_len] = '\0';
195                 if (ret)
196                         break;
197                 if (child)
198                         dentry_add_child(dir_dentry, child);
199         }
200         closedir(dir);
201         return ret;
202 }
203
204 /* Given an absolute symbolic link target @dest (UNIX-style, beginning
205  * with '/'), determine whether it points into the directory specified by
206  * @ino and @dev.  If so, return the target modified to be "absolute"
207  * relative to this directory.  Otherwise, return NULL.  */
208 static char *
209 unix_fixup_abslink(char *dest, u64 ino, u64 dev)
210 {
211         char *p = dest;
212
213         do {
214                 char save;
215                 struct stat stbuf;
216                 int ret;
217
218                 /* Skip non-slashes.  */
219                 while (*p && *p != '/')
220                         p++;
221
222                 /* Skip slashes.  */
223                 while (*p && *p == '/')
224                         p++;
225
226                 /* Get inode and device for this prefix.  */
227                 save = *p;
228                 *p = '\0';
229                 ret = stat(dest, &stbuf);
230                 *p = save;
231
232                 if (ret) {
233                         /* stat() failed.  Assume the link points outside the
234                          * directory tree being captured.  */
235                         break;
236                 }
237
238                 if (stbuf.st_ino == ino && stbuf.st_dev == dev) {
239                         /* Link points inside directory tree being captured.
240                          * Return abbreviated path.  */
241                         *--p = '/';
242                         while (p > dest && *(p - 1) == '/')
243                                 p--;
244                         return p;
245                 }
246         } while (*p);
247
248         /* Link does not point inside directory tree being captured.  */
249         return NULL;
250 }
251
252 static int
253 unix_scan_symlink(const char *full_path, int dirfd, const char *relpath,
254                   struct wim_inode *inode, struct capture_params *params)
255 {
256         char deref_name_buf[4096];
257         ssize_t deref_name_len;
258         char *dest;
259         int ret;
260
261         inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
262         inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
263
264         /* The idea here is to call readlink() to get the UNIX target of the
265          * symbolic link, then turn the target into a reparse point data buffer
266          * that contains a relative or absolute symbolic link. */
267         deref_name_len = my_readlinkat(full_path, dirfd, relpath,
268                                        deref_name_buf, sizeof(deref_name_buf) - 1);
269         if (deref_name_len < 0) {
270                 ERROR_WITH_ERRNO("\"%s\": Can't read target of symbolic link",
271                                  full_path);
272                 return WIMLIB_ERR_READLINK;
273         }
274
275         dest = deref_name_buf;
276
277         dest[deref_name_len] = '\0';
278
279         if ((params->add_flags & WIMLIB_ADD_FLAG_RPFIX) &&
280              dest[0] == '/')
281         {
282                 char *fixed_dest;
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                 fixed_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 (fixed_dest) {
293                         /* Link points inside the tree being captured, so it was
294                          * fixed.  */
295                         inode->i_not_rpfixed = 0;
296                         dest = fixed_dest;
297                         ret = do_capture_progress(params,
298                                                   WIMLIB_SCAN_DENTRY_FIXED_SYMLINK,
299                                                   NULL);
300                 } else {
301                         /* Link points outside the tree being captured, so it
302                          * was not fixed.  */
303                         ret = do_capture_progress(params,
304                                                   WIMLIB_SCAN_DENTRY_NOT_FIXED_SYMLINK,
305                                                   NULL);
306                 }
307                 if (ret)
308                         return ret;
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 capture_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         ret = try_exclude(full_path, full_path_len, params);
338         if (ret < 0) /* Excluded? */
339                 goto out_progress;
340         if (ret > 0) /* Error? */
341                 goto out;
342
343         if (params->add_flags & (WIMLIB_ADD_FLAG_DEREFERENCE |
344                                  WIMLIB_ADD_FLAG_ROOT))
345                 stat_flags = 0;
346         else
347                 stat_flags = AT_SYMLINK_NOFOLLOW;
348
349         ret = my_fstatat(full_path, dirfd, relpath, &stbuf, stat_flags);
350
351         if (ret) {
352                 ERROR_WITH_ERRNO("\"%s\": Can't read metadata", full_path);
353                 ret = WIMLIB_ERR_STAT;
354                 goto out;
355         }
356
357         if (!(params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA)) {
358                 if (unlikely(!S_ISREG(stbuf.st_mode) &&
359                              !S_ISDIR(stbuf.st_mode) &&
360                              !S_ISLNK(stbuf.st_mode)))
361                 {
362                         if (params->add_flags &
363                             WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
364                         {
365                                 ERROR("\"%s\": File type is unsupported",
366                                       full_path);
367                                 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
368                                 goto out;
369                         }
370                         params->progress.scan.cur_path = full_path;
371                         ret = do_capture_progress(params,
372                                                   WIMLIB_SCAN_DENTRY_UNSUPPORTED,
373                                                   NULL);
374                         goto out;
375                 }
376         }
377
378         ret = inode_table_new_dentry(params->inode_table, relpath,
379                                      stbuf.st_ino, stbuf.st_dev,
380                                      S_ISDIR(stbuf.st_mode), &tree);
381         if (ret)
382                 goto out;
383
384         inode = tree->d_inode;
385
386         /* Already seen this inode?  */
387         if (inode->i_nlink > 1)
388                 goto out_progress;
389
390 #ifdef HAVE_STAT_NANOSECOND_PRECISION
391         inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
392         inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
393         inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
394 #else
395         inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
396         inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
397         inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
398 #endif
399         inode->i_resolved = 1;
400         if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
401                 struct wimlib_unix_data unix_data;
402
403                 unix_data.uid = stbuf.st_uid;
404                 unix_data.gid = stbuf.st_gid;
405                 unix_data.mode = stbuf.st_mode;
406                 unix_data.rdev = stbuf.st_rdev;
407                 if (!inode_set_unix_data(inode, &unix_data, UNIX_DATA_ALL)) {
408                         ret = WIMLIB_ERR_NOMEM;
409                         goto out;
410                 }
411         }
412
413         if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
414                 params->capture_root_ino = stbuf.st_ino;
415                 params->capture_root_dev = stbuf.st_dev;
416                 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
417         }
418
419         if (S_ISREG(stbuf.st_mode)) {
420                 ret = unix_scan_regular_file(full_path, stbuf.st_size,
421                                              inode, params->unhashed_streams);
422         } else if (S_ISDIR(stbuf.st_mode)) {
423                 ret = unix_scan_directory(tree, full_path, full_path_len,
424                                           dirfd, relpath, params);
425         } else if (S_ISLNK(stbuf.st_mode)) {
426                 ret = unix_scan_symlink(full_path, dirfd, relpath,
427                                         inode, params);
428         }
429
430         if (ret)
431                 goto out;
432
433 out_progress:
434         params->progress.scan.cur_path = full_path;
435         if (likely(tree))
436                 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
437         else
438                 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
439 out:
440         if (unlikely(ret)) {
441                 free_dentry_tree(tree, params->lookup_table);
442                 tree = NULL;
443                 ret = report_capture_error(params, ret, full_path);
444         }
445         *tree_ret = tree;
446         return ret;
447 }
448
449 /*
450  * unix_build_dentry_tree():
451  *      Builds a tree of WIM dentries from an on-disk directory tree (UNIX
452  *      version; no NTFS-specific data is captured).
453  *
454  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Only
455  *              modified if successful.  Set to NULL if the file or directory was
456  *              excluded from capture.
457  *
458  * @root_disk_path:  The path to the root of the directory tree on disk.
459  *
460  * @params:     See doc for `struct capture_params'.
461  *
462  * @return:     0 on success, nonzero on failure.  It is a failure if any of
463  *              the files cannot be `stat'ed, or if any of the needed
464  *              directories cannot be opened or read.  Failure to add the files
465  *              to the WIM may still occur later when trying to actually read
466  *              the on-disk files during a call to wimlib_write() or
467  *              wimlib_overwrite().
468  */
469 int
470 unix_build_dentry_tree(struct wim_dentry **root_ret,
471                        const char *root_disk_path,
472                        struct capture_params *params)
473 {
474         size_t path_len;
475         size_t path_bufsz;
476         char *path_buf;
477         int ret;
478
479         path_len = strlen(root_disk_path);
480         path_bufsz = min(32790, PATH_MAX + 1);
481
482         if (path_len >= path_bufsz)
483                 return WIMLIB_ERR_INVALID_PARAM;
484
485         path_buf = MALLOC(path_bufsz);
486         if (!path_buf)
487                 return WIMLIB_ERR_NOMEM;
488         memcpy(path_buf, root_disk_path, path_len + 1);
489
490         params->capture_root_nchars = path_len;
491
492         ret = unix_build_dentry_tree_recursive(root_ret, path_buf, path_len,
493                                                AT_FDCWD, path_buf, params);
494         FREE(path_buf);
495         return ret;
496 }
497
498 #endif /* !__WIN32__ */