2 * unix_capture.c: Capture a directory tree on UNIX.
6 * Copyright (C) 2012, 2013, 2014 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
33 #include <limits.h> /* for PATH_MAX */
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"
45 # define my_fdopendir(dirfd_p) fdopendir(*(dirfd_p))
48 my_fdopendir(int *dirfd_p)
53 old_pwd = open(".", O_RDONLY);
55 if (!fchdir(*dirfd_p)) {
59 *dirfd_p = dirfd(dir);
70 # define my_openat(full_path, dirfd, relpath, flags) \
71 openat((dirfd), (relpath), (flags))
73 # define my_openat(full_path, dirfd, relpath, flags) \
74 open((full_path), (flags))
77 #ifdef HAVE_READLINKAT
78 # define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
79 readlinkat((dirfd), (relpath), (buf), (bufsize))
81 # define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
82 readlink((full_path), (buf), (bufsize))
86 # define my_fstatat(full_path, dirfd, relpath, stbuf, flags) \
87 fstatat((dirfd), (relpath), (stbuf), (flags))
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))
96 # define AT_FDCWD -100
99 #ifndef AT_SYMLINK_NOFOLLOW
100 # define AT_SYMLINK_NOFOLLOW 0x100
104 unix_scan_regular_file(const char *path, u64 size, struct wim_inode *inode,
105 struct list_head *unhashed_streams)
107 struct wim_lookup_table_entry *lte;
110 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
112 /* Empty files do not have to have a lookup table entry. */
116 file_on_disk = STRDUP(path);
118 return WIMLIB_ERR_NOMEM;
119 lte = new_lookup_table_entry();
122 return WIMLIB_ERR_NOMEM;
124 lte->file_on_disk = file_on_disk;
125 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
127 add_unhashed_stream(lte, inode, 0, unhashed_streams);
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);
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)
149 dirfd = my_openat(full_path, parent_dirfd, dir_relpath, O_RDONLY);
151 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
152 return WIMLIB_ERR_OPENDIR;
155 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
156 dir = my_fdopendir(&dirfd);
158 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
160 return WIMLIB_ERR_OPENDIR;
165 struct dirent *entry;
166 struct wim_dentry *child;
170 entry = readdir(dir);
173 ret = WIMLIB_ERR_READ;
174 ERROR_WITH_ERRNO("\"%s\": Error reading directory",
180 if (entry->d_name[0] == '.' &&
181 (entry->d_name[1] == '\0' ||
182 (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
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,
190 full_path_len + 1 + name_len,
192 &full_path[full_path_len + 1],
194 full_path[full_path_len] = '\0';
198 dentry_add_child(dir_dentry, child);
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. */
209 unix_fixup_abslink(char *dest, u64 ino, u64 dev)
218 /* Skip non-slashes. */
219 while (*p && *p != '/')
223 while (*p && *p == '/')
226 /* Get inode and device for this prefix. */
229 ret = stat(dest, &stbuf);
233 /* stat() failed. Assume the link points outside the
234 * directory tree being captured. */
238 if (stbuf.st_ino == ino && stbuf.st_dev == dev) {
239 /* Link points inside directory tree being captured.
240 * Return abbreviated path. */
242 while (p > dest && *(p - 1) == '/')
248 /* Link does not point inside directory tree being captured. */
253 unix_scan_symlink(struct wim_dentry **root_p, const char *full_path,
254 int dirfd, const char *relpath,
255 struct wim_inode *inode, struct add_image_params *params)
257 char deref_name_buf[4096];
258 ssize_t deref_name_len;
262 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
263 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
265 /* The idea here is to call readlink() to get the UNIX target of the
266 * symbolic link, then turn the target into a reparse point data buffer
267 * that contains a relative or absolute symbolic link. */
268 deref_name_len = my_readlinkat(full_path, dirfd, relpath,
269 deref_name_buf, sizeof(deref_name_buf) - 1);
270 if (deref_name_len < 0) {
271 ERROR_WITH_ERRNO("\"%s\": Can't read target of symbolic link",
273 return WIMLIB_ERR_READLINK;
276 dest = deref_name_buf;
278 dest[deref_name_len] = '\0';
280 if ((params->add_flags & WIMLIB_ADD_FLAG_RPFIX) &&
283 dest = unix_fixup_abslink(dest,
284 params->capture_root_ino,
285 params->capture_root_dev);
287 /* RPFIX (reparse point fixup) mode: Ignore
288 * absolute symbolic link that points out of the
289 * tree to be captured. */
290 free_dentry(*root_p);
292 params->progress.scan.cur_path = full_path;
293 params->progress.scan.symlink_target = deref_name_buf;
294 do_capture_progress(params,
295 WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK,
299 inode->i_not_rpfixed = 0;
301 ret = wim_inode_set_symlink(inode, dest, params->lookup_table);
305 /* Unfortunately, Windows seems to have the concept of "file" symbolic
306 * links as being different from "directory" symbolic links... so
307 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the symbolic link if the
308 * *target* of the symbolic link is a directory. */
310 if (my_fstatat(full_path, dirfd, relpath, &stbuf, 0) == 0 &&
311 S_ISDIR(stbuf.st_mode))
312 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
317 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
318 char *full_path, size_t full_path_len,
319 int dirfd, const char *relpath,
320 struct add_image_params *params)
322 struct wim_dentry *tree = NULL;
323 struct wim_inode *inode = NULL;
328 if (should_exclude_path(full_path + params->capture_root_nchars,
329 full_path_len - params->capture_root_nchars,
336 if (params->add_flags & (WIMLIB_ADD_FLAG_DEREFERENCE |
337 WIMLIB_ADD_FLAG_ROOT))
340 stat_flags = AT_SYMLINK_NOFOLLOW;
342 ret = my_fstatat(full_path, dirfd, relpath, &stbuf, stat_flags);
345 ERROR_WITH_ERRNO("\"%s\": Can't read metadata", full_path);
346 ret = WIMLIB_ERR_STAT;
350 if (unlikely(!S_ISREG(stbuf.st_mode) &&
351 !S_ISDIR(stbuf.st_mode) &&
352 !S_ISLNK(stbuf.st_mode)))
354 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
356 ERROR("\"%s\": File type is unsupported", full_path);
357 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
360 params->progress.scan.cur_path = full_path;
361 do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
366 ret = inode_table_new_dentry(params->inode_table, relpath,
367 stbuf.st_ino, stbuf.st_dev,
368 S_ISDIR(stbuf.st_mode), &tree);
372 inode = tree->d_inode;
374 if (inode->i_nlink > 1) {
375 /* Already seen this inode? */
380 #ifdef HAVE_STAT_NANOSECOND_PRECISION
381 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
382 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
383 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
385 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
386 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
387 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
389 inode->i_resolved = 1;
390 if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
391 ret = inode_set_unix_data(inode, stbuf.st_uid,
394 params->lookup_table,
395 UNIX_DATA_ALL | UNIX_DATA_CREATE);
400 if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
401 params->capture_root_ino = stbuf.st_ino;
402 params->capture_root_dev = stbuf.st_dev;
403 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
406 if (S_ISREG(stbuf.st_mode)) {
407 ret = unix_scan_regular_file(full_path, stbuf.st_size,
408 inode, params->unhashed_streams);
409 } else if (S_ISDIR(stbuf.st_mode)) {
410 ret = unix_scan_directory(tree, full_path, full_path_len,
411 dirfd, relpath, params);
413 ret = unix_scan_symlink(&tree, full_path, dirfd, relpath,
423 params->progress.scan.cur_path = full_path;
425 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
427 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
429 if (likely(ret == 0))
432 free_dentry_tree(tree, params->lookup_table);
437 * unix_build_dentry_tree():
438 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
439 * version; no NTFS-specific data is captured).
441 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
442 * modified if successful. Set to NULL if the file or directory was
443 * excluded from capture.
445 * @root_disk_path: The path to the root of the directory tree on disk.
447 * @params: See doc for `struct add_image_params'.
449 * @return: 0 on success, nonzero on failure. It is a failure if any of
450 * the files cannot be `stat'ed, or if any of the needed
451 * directories cannot be opened or read. Failure to add the files
452 * to the WIM may still occur later when trying to actually read
453 * the on-disk files during a call to wimlib_write() or
454 * wimlib_overwrite().
457 unix_build_dentry_tree(struct wim_dentry **root_ret,
458 const char *root_disk_path,
459 struct add_image_params *params)
466 path_len = strlen(root_disk_path);
467 path_bufsz = min(32790, PATH_MAX + 1);
469 if (path_len >= path_bufsz)
470 return WIMLIB_ERR_INVALID_PARAM;
472 path_buf = MALLOC(path_bufsz);
474 return WIMLIB_ERR_NOMEM;
475 memcpy(path_buf, root_disk_path, path_len + 1);
477 params->capture_root_nchars = path_len;
479 ret = unix_build_dentry_tree_recursive(root_ret, path_buf, path_len,
480 AT_FDCWD, path_buf, params);
485 #endif /* !__WIN32__ */