2 * unix_capture.c: Capture a directory tree on UNIX.
6 * Copyright (C) 2012, 2013, 2014 Eric Biggers
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
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
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/.
31 #include <limits.h> /* for PATH_MAX */
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"
44 # define my_fdopendir(dirfd_p) fdopendir(*(dirfd_p))
47 my_fdopendir(int *dirfd_p)
52 old_pwd = open(".", O_RDONLY);
54 if (!fchdir(*dirfd_p)) {
58 *dirfd_p = dirfd(dir);
69 # define my_openat(full_path, dirfd, relpath, flags) \
70 openat((dirfd), (relpath), (flags))
72 # define my_openat(full_path, dirfd, relpath, flags) \
73 open((full_path), (flags))
76 #ifdef HAVE_READLINKAT
77 # define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
78 readlinkat((dirfd), (relpath), (buf), (bufsize))
80 # define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
81 readlink((full_path), (buf), (bufsize))
85 # define my_fstatat(full_path, dirfd, relpath, stbuf, flags) \
86 fstatat((dirfd), (relpath), (stbuf), (flags))
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))
95 # define AT_FDCWD -100
98 #ifndef AT_SYMLINK_NOFOLLOW
99 # define AT_SYMLINK_NOFOLLOW 0x100
103 unix_scan_regular_file(const char *path, u64 size, struct wim_inode *inode,
104 struct list_head *unhashed_streams)
106 struct wim_lookup_table_entry *lte;
109 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
111 /* Empty files do not have to have a lookup table entry. */
115 file_on_disk = STRDUP(path);
117 return WIMLIB_ERR_NOMEM;
118 lte = new_lookup_table_entry();
121 return WIMLIB_ERR_NOMEM;
123 lte->file_on_disk = file_on_disk;
124 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
126 add_unhashed_stream(lte, inode, 0, unhashed_streams);
132 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
133 char *path, size_t path_len,
134 int dirfd, const char *relpath,
135 struct add_image_params *params);
138 unix_scan_directory(struct wim_dentry *dir_dentry,
139 char *full_path, size_t full_path_len,
140 int parent_dirfd, const char *dir_relpath,
141 struct add_image_params *params)
148 dirfd = my_openat(full_path, parent_dirfd, dir_relpath, O_RDONLY);
150 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
151 return WIMLIB_ERR_OPENDIR;
154 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
155 dir = my_fdopendir(&dirfd);
157 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
159 return WIMLIB_ERR_OPENDIR;
164 struct dirent *entry;
165 struct wim_dentry *child;
169 entry = readdir(dir);
172 ret = WIMLIB_ERR_READ;
173 ERROR_WITH_ERRNO("\"%s\": Error reading directory",
179 if (entry->d_name[0] == '.' &&
180 (entry->d_name[1] == '\0' ||
181 (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
184 full_path[full_path_len] = '/';
185 name_len = strlen(entry->d_name);
186 memcpy(&full_path[full_path_len + 1], entry->d_name, name_len + 1);
187 ret = unix_build_dentry_tree_recursive(&child,
189 full_path_len + 1 + name_len,
191 &full_path[full_path_len + 1],
193 full_path[full_path_len] = '\0';
197 dentry_add_child(dir_dentry, child);
203 /* Given an absolute symbolic link target @dest (UNIX-style, beginning
204 * with '/'), determine whether it points into the directory specified by
205 * @ino and @dev. If so, return the target modified to be "absolute"
206 * relative to this directory. Otherwise, return NULL. */
208 unix_fixup_abslink(char *dest, u64 ino, u64 dev)
217 /* Skip non-slashes. */
218 while (*p && *p != '/')
222 while (*p && *p == '/')
225 /* Get inode and device for this prefix. */
228 ret = stat(dest, &stbuf);
232 /* stat() failed. Assume the link points outside the
233 * directory tree being captured. */
237 if (stbuf.st_ino == ino && stbuf.st_dev == dev) {
238 /* Link points inside directory tree being captured.
239 * Return abbreviated path. */
241 while (p > dest && *(p - 1) == '/')
247 /* Link does not point inside directory tree being captured. */
252 unix_scan_symlink(const char *full_path, int dirfd, const char *relpath,
253 struct wim_inode *inode, struct add_image_params *params)
255 char deref_name_buf[4096];
256 ssize_t deref_name_len;
260 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
261 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
263 /* The idea here is to call readlink() to get the UNIX target of the
264 * symbolic link, then turn the target into a reparse point data buffer
265 * that contains a relative or absolute symbolic link. */
266 deref_name_len = my_readlinkat(full_path, dirfd, relpath,
267 deref_name_buf, sizeof(deref_name_buf) - 1);
268 if (deref_name_len < 0) {
269 ERROR_WITH_ERRNO("\"%s\": Can't read target of symbolic link",
271 return WIMLIB_ERR_READLINK;
274 dest = deref_name_buf;
276 dest[deref_name_len] = '\0';
278 if ((params->add_flags & WIMLIB_ADD_FLAG_RPFIX) &&
283 /* RPFIX (reparse point fixup) mode: Change target of absolute
284 * symbolic link to be "absolute" relative to the tree being
286 fixed_dest = unix_fixup_abslink(dest,
287 params->capture_root_ino,
288 params->capture_root_dev);
289 params->progress.scan.cur_path = full_path;
290 params->progress.scan.symlink_target = deref_name_buf;
292 /* Link points inside the tree being captured, so it was
294 inode->i_not_rpfixed = 0;
296 ret = do_capture_progress(params,
297 WIMLIB_SCAN_DENTRY_FIXED_SYMLINK,
300 /* Link points outside the tree being captured, so it
302 ret = do_capture_progress(params,
303 WIMLIB_SCAN_DENTRY_NOT_FIXED_SYMLINK,
309 ret = wim_inode_set_symlink(inode, dest, params->lookup_table);
313 /* Unfortunately, Windows seems to have the concept of "file" symbolic
314 * links as being different from "directory" symbolic links... so
315 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the symbolic link if the
316 * *target* of the symbolic link is a directory. */
318 if (my_fstatat(full_path, dirfd, relpath, &stbuf, 0) == 0 &&
319 S_ISDIR(stbuf.st_mode))
320 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
325 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
326 char *full_path, size_t full_path_len,
327 int dirfd, const char *relpath,
328 struct add_image_params *params)
330 struct wim_dentry *tree = NULL;
331 struct wim_inode *inode = NULL;
336 ret = try_exclude(full_path, full_path_len, params);
337 if (ret < 0) /* Excluded? */
339 if (ret > 0) /* Error? */
342 if (params->add_flags & (WIMLIB_ADD_FLAG_DEREFERENCE |
343 WIMLIB_ADD_FLAG_ROOT))
346 stat_flags = AT_SYMLINK_NOFOLLOW;
348 ret = my_fstatat(full_path, dirfd, relpath, &stbuf, stat_flags);
351 ERROR_WITH_ERRNO("\"%s\": Can't read metadata", full_path);
352 ret = WIMLIB_ERR_STAT;
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)))
361 if (params->add_flags &
362 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
364 ERROR("\"%s\": File type is unsupported",
366 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
369 params->progress.scan.cur_path = full_path;
370 ret = do_capture_progress(params,
371 WIMLIB_SCAN_DENTRY_UNSUPPORTED,
377 ret = inode_table_new_dentry(params->inode_table, relpath,
378 stbuf.st_ino, stbuf.st_dev,
379 S_ISDIR(stbuf.st_mode), &tree);
383 inode = tree->d_inode;
385 /* Already seen this inode? */
386 if (inode->i_nlink > 1)
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);
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);
398 inode->i_resolved = 1;
399 if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
400 struct wimlib_unix_data unix_data;
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;
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;
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(full_path, dirfd, relpath,
433 params->progress.scan.cur_path = full_path;
435 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
437 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
439 if (likely(ret == 0))
442 free_dentry_tree(tree, params->lookup_table);
447 * unix_build_dentry_tree():
448 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
449 * version; no NTFS-specific data is captured).
451 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
452 * modified if successful. Set to NULL if the file or directory was
453 * excluded from capture.
455 * @root_disk_path: The path to the root of the directory tree on disk.
457 * @params: See doc for `struct add_image_params'.
459 * @return: 0 on success, nonzero on failure. It is a failure if any of
460 * the files cannot be `stat'ed, or if any of the needed
461 * directories cannot be opened or read. Failure to add the files
462 * to the WIM may still occur later when trying to actually read
463 * the on-disk files during a call to wimlib_write() or
464 * wimlib_overwrite().
467 unix_build_dentry_tree(struct wim_dentry **root_ret,
468 const char *root_disk_path,
469 struct add_image_params *params)
476 path_len = strlen(root_disk_path);
477 path_bufsz = min(32790, PATH_MAX + 1);
479 if (path_len >= path_bufsz)
480 return WIMLIB_ERR_INVALID_PARAM;
482 path_buf = MALLOC(path_bufsz);
484 return WIMLIB_ERR_NOMEM;
485 memcpy(path_buf, root_disk_path, path_len + 1);
487 params->capture_root_nchars = path_len;
489 ret = unix_build_dentry_tree_recursive(root_ret, path_buf, path_len,
490 AT_FDCWD, path_buf, params);
495 #endif /* !__WIN32__ */