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"
43 #include "wimlib/unix_data.h"
46 # define my_fdopendir(dirfd_p) fdopendir(*(dirfd_p))
49 my_fdopendir(int *dirfd_p)
54 old_pwd = open(".", O_RDONLY);
56 if (!fchdir(*dirfd_p)) {
60 *dirfd_p = dirfd(dir);
71 # define my_openat(full_path, dirfd, relpath, flags) \
72 openat((dirfd), (relpath), (flags))
74 # define my_openat(full_path, dirfd, relpath, flags) \
75 open((full_path), (flags))
78 #ifdef HAVE_READLINKAT
79 # define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
80 readlinkat((dirfd), (relpath), (buf), (bufsize))
82 # define my_readlinkat(full_path, dirfd, relpath, buf, bufsize) \
83 readlink((full_path), (buf), (bufsize))
87 # define my_fstatat(full_path, dirfd, relpath, stbuf, flags) \
88 fstatat((dirfd), (relpath), (stbuf), (flags))
90 # define my_fstatat(full_path, dirfd, relpath, stbuf, flags) \
91 ((flags) & AT_SYMLINK_NOFOLLOW) ? \
92 lstat((full_path), (stbuf)) : \
93 stat((full_path), (stbuf))
97 # define AT_FDCWD -100
100 #ifndef AT_SYMLINK_NOFOLLOW
101 # define AT_SYMLINK_NOFOLLOW 0x100
105 unix_scan_regular_file(const char *path, u64 size, struct wim_inode *inode,
106 struct list_head *unhashed_streams)
108 struct wim_lookup_table_entry *lte;
111 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
113 /* Empty files do not have to have a lookup table entry. */
117 file_on_disk = STRDUP(path);
119 return WIMLIB_ERR_NOMEM;
120 lte = new_lookup_table_entry();
123 return WIMLIB_ERR_NOMEM;
125 lte->file_on_disk = file_on_disk;
126 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
128 add_unhashed_stream(lte, inode, 0, unhashed_streams);
134 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
135 char *path, size_t path_len,
136 int dirfd, const char *relpath,
137 struct add_image_params *params);
140 unix_scan_directory(struct wim_dentry *dir_dentry,
141 char *full_path, size_t full_path_len,
142 int parent_dirfd, const char *dir_relpath,
143 struct add_image_params *params)
150 dirfd = my_openat(full_path, parent_dirfd, dir_relpath, O_RDONLY);
152 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
153 return WIMLIB_ERR_OPENDIR;
156 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
157 dir = my_fdopendir(&dirfd);
159 ERROR_WITH_ERRNO("\"%s\": Can't open directory", full_path);
161 return WIMLIB_ERR_OPENDIR;
166 struct dirent *entry;
167 struct wim_dentry *child;
171 entry = readdir(dir);
174 ret = WIMLIB_ERR_READ;
175 ERROR_WITH_ERRNO("\"%s\": Error reading directory",
181 if (entry->d_name[0] == '.' &&
182 (entry->d_name[1] == '\0' ||
183 (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
186 full_path[full_path_len] = '/';
187 name_len = strlen(entry->d_name);
188 memcpy(&full_path[full_path_len + 1], entry->d_name, name_len + 1);
189 ret = unix_build_dentry_tree_recursive(&child,
191 full_path_len + 1 + name_len,
193 &full_path[full_path_len + 1],
195 full_path[full_path_len] = '\0';
199 dentry_add_child(dir_dentry, child);
205 /* Given an absolute symbolic link target @dest (UNIX-style, beginning
206 * with '/'), determine whether it points into the directory specified by
207 * @ino and @dev. If so, return the target modified to be "absolute"
208 * relative to this directory. Otherwise, return NULL. */
210 unix_fixup_abslink(char *dest, u64 ino, u64 dev)
219 /* Skip non-slashes. */
220 while (*p && *p != '/')
224 while (*p && *p == '/')
227 /* Get inode and device for this prefix. */
230 ret = stat(dest, &stbuf);
234 /* stat() failed. Assume the link points outside the
235 * directory tree being captured. */
239 if (stbuf.st_ino == ino && stbuf.st_dev == dev) {
240 /* Link points inside directory tree being captured.
241 * Return abbreviated path. */
243 while (p > dest && *(p - 1) == '/')
249 /* Link does not point inside directory tree being captured. */
254 unix_scan_symlink(const char *full_path, 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) &&
285 /* RPFIX (reparse point fixup) mode: Change target of absolute
286 * symbolic link to be "absolute" relative to the tree being
288 fixed_dest = unix_fixup_abslink(dest,
289 params->capture_root_ino,
290 params->capture_root_dev);
291 params->progress.scan.cur_path = full_path;
292 params->progress.scan.symlink_target = deref_name_buf;
294 /* Link points inside the tree being captured, so it was
296 inode->i_not_rpfixed = 0;
298 ret = do_capture_progress(params,
299 WIMLIB_SCAN_DENTRY_FIXED_SYMLINK,
302 /* Link points outside the tree being captured, so it
304 ret = do_capture_progress(params,
305 WIMLIB_SCAN_DENTRY_NOT_FIXED_SYMLINK,
311 ret = wim_inode_set_symlink(inode, dest, params->lookup_table);
315 /* Unfortunately, Windows seems to have the concept of "file" symbolic
316 * links as being different from "directory" symbolic links... so
317 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the symbolic link if the
318 * *target* of the symbolic link is a directory. */
320 if (my_fstatat(full_path, dirfd, relpath, &stbuf, 0) == 0 &&
321 S_ISDIR(stbuf.st_mode))
322 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
327 unix_build_dentry_tree_recursive(struct wim_dentry **tree_ret,
328 char *full_path, size_t full_path_len,
329 int dirfd, const char *relpath,
330 struct add_image_params *params)
332 struct wim_dentry *tree = NULL;
333 struct wim_inode *inode = NULL;
338 ret = try_exclude(full_path, full_path_len, params);
339 if (ret < 0) /* Excluded? */
341 if (ret > 0) /* Error? */
344 if (params->add_flags & (WIMLIB_ADD_FLAG_DEREFERENCE |
345 WIMLIB_ADD_FLAG_ROOT))
348 stat_flags = AT_SYMLINK_NOFOLLOW;
350 ret = my_fstatat(full_path, dirfd, relpath, &stbuf, stat_flags);
353 ERROR_WITH_ERRNO("\"%s\": Can't read metadata", full_path);
354 ret = WIMLIB_ERR_STAT;
358 if (!(params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA)) {
359 if (unlikely(!S_ISREG(stbuf.st_mode) &&
360 !S_ISDIR(stbuf.st_mode) &&
361 !S_ISLNK(stbuf.st_mode)))
363 if (params->add_flags &
364 WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
366 ERROR("\"%s\": File type is unsupported",
368 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
371 params->progress.scan.cur_path = full_path;
372 ret = do_capture_progress(params,
373 WIMLIB_SCAN_DENTRY_UNSUPPORTED,
379 ret = inode_table_new_dentry(params->inode_table, relpath,
380 stbuf.st_ino, stbuf.st_dev,
381 S_ISDIR(stbuf.st_mode), &tree);
385 inode = tree->d_inode;
387 /* Already seen this inode? */
388 if (inode->i_nlink > 1)
391 #ifdef HAVE_STAT_NANOSECOND_PRECISION
392 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
393 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
394 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
396 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
397 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
398 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
400 inode->i_resolved = 1;
401 if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
402 struct wimlib_unix_data unix_data;
404 unix_data.uid = stbuf.st_uid;
405 unix_data.gid = stbuf.st_gid;
406 unix_data.mode = stbuf.st_mode;
407 unix_data.rdev = stbuf.st_rdev;
408 if (!inode_set_unix_data(inode, &unix_data, UNIX_DATA_ALL)) {
409 ret = WIMLIB_ERR_NOMEM;
414 if (params->add_flags & WIMLIB_ADD_FLAG_ROOT) {
415 params->capture_root_ino = stbuf.st_ino;
416 params->capture_root_dev = stbuf.st_dev;
417 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
420 if (S_ISREG(stbuf.st_mode)) {
421 ret = unix_scan_regular_file(full_path, stbuf.st_size,
422 inode, params->unhashed_streams);
423 } else if (S_ISDIR(stbuf.st_mode)) {
424 ret = unix_scan_directory(tree, full_path, full_path_len,
425 dirfd, relpath, params);
426 } else if (S_ISLNK(stbuf.st_mode)) {
427 ret = unix_scan_symlink(full_path, dirfd, relpath,
435 params->progress.scan.cur_path = full_path;
437 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
439 ret = do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
441 if (likely(ret == 0))
444 free_dentry_tree(tree, params->lookup_table);
449 * unix_build_dentry_tree():
450 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
451 * version; no NTFS-specific data is captured).
453 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
454 * modified if successful. Set to NULL if the file or directory was
455 * excluded from capture.
457 * @root_disk_path: The path to the root of the directory tree on disk.
459 * @params: See doc for `struct add_image_params'.
461 * @return: 0 on success, nonzero on failure. It is a failure if any of
462 * the files cannot be `stat'ed, or if any of the needed
463 * directories cannot be opened or read. Failure to add the files
464 * to the WIM may still occur later when trying to actually read
465 * the on-disk files during a call to wimlib_write() or
466 * wimlib_overwrite().
469 unix_build_dentry_tree(struct wim_dentry **root_ret,
470 const char *root_disk_path,
471 struct add_image_params *params)
478 path_len = strlen(root_disk_path);
479 path_bufsz = min(32790, PATH_MAX + 1);
481 if (path_len >= path_bufsz)
482 return WIMLIB_ERR_INVALID_PARAM;
484 path_buf = MALLOC(path_bufsz);
486 return WIMLIB_ERR_NOMEM;
487 memcpy(path_buf, root_disk_path, path_len + 1);
489 params->capture_root_nchars = path_len;
491 ret = unix_build_dentry_tree_recursive(root_ret, path_buf, path_len,
492 AT_FDCWD, path_buf, params);
497 #endif /* !__WIN32__ */