]> wimlib.net Git - wimlib/blob - src/unix_capture.c
unix_capture.c: Optionally use dirfd-relative functions (openat(), etc)
[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
44 #ifdef HAVE_FDOPENDIR
45 #  define my_fdopendir(dirfd_p) fdopendir(*(dirfd_p))
46 #else
47 static DIR *
48 my_fdopendir(int *dirfd_p)
49 {
50         DIR *dir = NULL;
51         int old_pwd;
52
53         old_pwd = open(".", O_RDONLY);
54         if (old_pwd >= 0) {
55                 if (!fchdir(*dirfd_p)) {
56                         dir = opendir(".");
57                         if (dir) {
58                                 close(*dirfd_p);
59                                 *dirfd_p = dirfd(dir);
60                         }
61                         fchdir(old_pwd);
62                 }
63                 close(old_pwd);
64         }
65         return dir;
66 }
67 #endif
68
69 #ifdef HAVE_OPENAT
70 #  define my_openat(full_path, dirfd, relpath, flags) \
71                 openat((dirfd), (relpath), (flags))
72 #else
73 #  define my_openat(full_path, dirfd, relpath, flags) \
74                 open((full_path), (flags))
75 #endif
76
77 #ifdef HAVE_READLINKAT
78 #  define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
79                 readlinkat((dirfd), (relpath), (buf), (bufsize))
80 #else
81 #  define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
82                 readlink((full_path), (buf), (bufsize))
83 #endif
84
85 #ifdef HAVE_FSTATAT
86 #  define my_fstatat(full_path, dirfd, relpath, stbuf, flags)   \
87         fstatat((dirfd), (relpath), (stbuf), (flags))
88 #else
89 #  define my_fstatat(full_path, dirfd, relpath, stbuf, flags)   \
90         ((flags) & AT_SYMLINK_NOFOLLOW) ? \
91                 lstat((full_path), (stbuf)) : \
92                 stat((full_path), (stbuf))
93 #endif
94
95 #ifndef AT_FDCWD
96 #  define AT_FDCWD      -100
97 #endif
98
99 #ifndef AT_SYMLINK_NOFOLLOW
100 #  define AT_SYMLINK_NOFOLLOW   0x100
101 #endif
102
103 static int
104 unix_scan_regular_file(const char *path, u64 size, struct wim_inode *inode,
105                        struct list_head *unhashed_streams)
106 {
107         struct wim_lookup_table_entry *lte;
108         char *file_on_disk;
109
110         inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
111
112         /* Empty files do not have to have a lookup table entry. */
113         if (!size)
114                 return 0;
115
116         file_on_disk = STRDUP(path);
117         if (!file_on_disk)
118                 return WIMLIB_ERR_NOMEM;
119         lte = new_lookup_table_entry();
120         if (!lte) {
121                 FREE(file_on_disk);
122                 return WIMLIB_ERR_NOMEM;
123         }
124         lte->file_on_disk = file_on_disk;
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 add_image_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 add_image_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 static int
205 unix_scan_symlink(struct wim_dentry **root_p, const char *full_path,
206                   int dirfd, const char *relpath,
207                   struct wim_inode *inode, struct add_image_params *params)
208 {
209         char deref_name_buf[4096];
210         ssize_t deref_name_len;
211         char *dest;
212         int ret;
213
214         inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
215         inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
216
217         /* The idea here is to call readlink() to get the UNIX target of the
218          * symbolic link, then turn the target into a reparse point data buffer
219          * that contains a relative or absolute symbolic link. */
220         deref_name_len = my_readlinkat(full_path, dirfd, relpath,
221                                        deref_name_buf, sizeof(deref_name_buf) - 1);
222         if (deref_name_len < 0) {
223                 ERROR_WITH_ERRNO("\"%s\": Can't read target of symbolic link",
224                                  full_path);
225                 return WIMLIB_ERR_READLINK;
226         }
227
228         dest = deref_name_buf;
229
230         dest[deref_name_len] = '\0';
231
232         if ((params->add_flags & WIMLIB_ADD_FLAG_RPFIX) &&
233              dest[0] == '/')
234         {
235                 dest = capture_fixup_absolute_symlink(dest,
236                                                       params->capture_root_ino,
237                                                       params->capture_root_dev);
238                 if (!dest) {
239                         /* RPFIX (reparse point fixup) mode:  Ignore
240                          * absolute symbolic link that points out of the
241                          * tree to be captured.  */
242                         free_dentry(*root_p);
243                         *root_p = NULL;
244                         params->progress.scan.cur_path = full_path;
245                         params->progress.scan.symlink_target = deref_name_buf;
246                         do_capture_progress(params,
247                                             WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK,
248                                             NULL);
249                         return 0;
250                 }
251                 inode->i_not_rpfixed = 0;
252         }
253         ret = wim_inode_set_symlink(inode, dest, params->lookup_table);
254         if (ret)
255                 return ret;
256
257         /* Unfortunately, Windows seems to have the concept of "file" symbolic
258          * links as being different from "directory" symbolic links...  so
259          * FILE_ATTRIBUTE_DIRECTORY needs to be set on the symbolic link if the
260          * *target* of the symbolic link is a directory.  */
261         struct stat stbuf;
262         if (my_fstatat(full_path, dirfd, relpath, &stbuf, 0) == 0 &&
263             S_ISDIR(stbuf.st_mode))
264                 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
265         return 0;
266 }
267
268 static int
269 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
270                                  char *full_path, size_t full_path_len,
271                                  int dirfd, const char *relpath,
272                                  struct add_image_params *params)
273 {
274         struct wim_dentry *tree = NULL;
275         struct wim_inode *inode = NULL;
276         int ret;
277         struct stat stbuf;
278         int stat_flags;
279
280         if (should_exclude_path(full_path + params->capture_root_nchars,
281                                 full_path_len - params->capture_root_nchars,
282                                 params->config))
283         {
284                 ret = 0;
285                 goto out_progress;
286         }
287
288         if (params->add_flags & (WIMLIB_ADD_FLAG_DEREFERENCE |
289                                  WIMLIB_ADD_FLAG_ROOT))
290                 stat_flags = 0;
291         else
292                 stat_flags = AT_SYMLINK_NOFOLLOW;
293
294         ret = my_fstatat(full_path, dirfd, relpath, &stbuf, stat_flags);
295
296         if (ret) {
297                 ERROR_WITH_ERRNO("\"%s\": Can't read metadata", full_path);
298                 ret = WIMLIB_ERR_STAT;
299                 goto out;
300         }
301
302         if (unlikely(!S_ISREG(stbuf.st_mode) &&
303                      !S_ISDIR(stbuf.st_mode) &&
304                      !S_ISLNK(stbuf.st_mode)))
305         {
306                 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
307                 {
308                         ERROR("\"%s\": File type is unsupported", full_path);
309                         ret = WIMLIB_ERR_UNSUPPORTED_FILE;
310                         goto out;
311                 }
312                 params->progress.scan.cur_path = full_path;
313                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
314                 ret = 0;
315                 goto out;
316         }
317
318         ret = inode_table_new_dentry(params->inode_table, relpath,
319                                      stbuf.st_ino, stbuf.st_dev,
320                                      S_ISDIR(stbuf.st_mode), &tree);
321         if (ret)
322                 goto out;
323
324         inode = tree->d_inode;
325
326         if (inode->i_nlink > 1) {
327                 /* Already seen this inode?  */
328                 ret = 0;
329                 goto out_progress;
330         }
331
332 #ifdef HAVE_STAT_NANOSECOND_PRECISION
333         inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
334         inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
335         inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
336 #else
337         inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
338         inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
339         inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
340 #endif
341         inode->i_resolved = 1;
342         if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
343                 ret = inode_set_unix_data(inode, stbuf.st_uid,
344                                           stbuf.st_gid,
345                                           stbuf.st_mode,
346                                           params->lookup_table,
347                                           UNIX_DATA_ALL | UNIX_DATA_CREATE);
348                 if (ret)
349                         goto out;
350         }
351
352         if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
353                 params->capture_root_ino = stbuf.st_ino;
354                 params->capture_root_dev = stbuf.st_dev;
355                 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
356         }
357
358         if (S_ISREG(stbuf.st_mode)) {
359                 ret = unix_scan_regular_file(full_path, stbuf.st_size,
360                                              inode, params->unhashed_streams);
361         } else if (S_ISDIR(stbuf.st_mode)) {
362                 ret = unix_scan_directory(tree, full_path, full_path_len,
363                                           dirfd, relpath, params);
364         } else {
365                 ret = unix_scan_symlink(&tree, full_path, dirfd, relpath,
366                                         inode, params);
367                 if (!tree)
368                         goto out;
369         }
370
371         if (ret)
372                 goto out;
373
374 out_progress:
375         params->progress.scan.cur_path = full_path;
376         if (likely(tree))
377                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
378         else
379                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
380 out:
381         if (likely(ret == 0))
382                 *tree_ret = tree;
383         else
384                 free_dentry_tree(tree, params->lookup_table);
385         return ret;
386 }
387
388 /*
389  * unix_build_dentry_tree():
390  *      Builds a tree of WIM dentries from an on-disk directory tree (UNIX
391  *      version; no NTFS-specific data is captured).
392  *
393  * @root_ret:   Place to return a pointer to the root of the dentry tree.  Only
394  *              modified if successful.  Set to NULL if the file or directory was
395  *              excluded from capture.
396  *
397  * @root_disk_path:  The path to the root of the directory tree on disk.
398  *
399  * @params:     See doc for `struct add_image_params'.
400  *
401  * @return:     0 on success, nonzero on failure.  It is a failure if any of
402  *              the files cannot be `stat'ed, or if any of the needed
403  *              directories cannot be opened or read.  Failure to add the files
404  *              to the WIM may still occur later when trying to actually read
405  *              the on-disk files during a call to wimlib_write() or
406  *              wimlib_overwrite().
407  */
408 int
409 unix_build_dentry_tree(struct wim_dentry **root_ret,
410                        const char *root_disk_path,
411                        struct add_image_params *params)
412 {
413         size_t path_len;
414         size_t path_bufsz;
415         char *path_buf;
416         int ret;
417
418         path_len = strlen(root_disk_path);
419         path_bufsz = min(32790, PATH_MAX + 1);
420
421         if (path_len >= path_bufsz)
422                 return WIMLIB_ERR_INVALID_PARAM;
423
424         path_buf = MALLOC(path_bufsz);
425         if (!path_buf)
426                 return WIMLIB_ERR_NOMEM;
427         memcpy(path_buf, root_disk_path, path_len + 1);
428
429         params->capture_root_nchars = path_len;
430
431         ret = unix_build_dentry_tree_recursive(root_ret, path_buf, path_len,
432                                                AT_FDCWD, path_buf, params);
433         FREE(path_buf);
434         return ret;
435 }
436
437 #endif /* !__WIN32__ */