2 * unix_capture.c: Capture a directory tree on UNIX.
6 * Copyright (C) 2012, 2013 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/.
36 #include "wimlib/capture.h"
37 #include "wimlib/dentry.h"
38 #include "wimlib/error.h"
39 #include "wimlib/lookup_table.h"
40 #include "wimlib/paths.h"
41 #include "wimlib/reparse.h"
42 #include "wimlib/timestamp.h"
45 unix_capture_regular_file(const char *path,
47 struct wim_inode *inode,
48 struct list_head *unhashed_streams)
50 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
52 /* Empty files do not have to have a lookup table entry. */
54 struct wim_lookup_table_entry *lte;
57 file_on_disk = STRDUP(path);
59 return WIMLIB_ERR_NOMEM;
60 lte = new_lookup_table_entry();
63 return WIMLIB_ERR_NOMEM;
65 lte->file_on_disk = file_on_disk;
66 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
68 add_unhashed_stream(lte, inode, 0, unhashed_streams);
75 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
78 struct add_image_params *params);
81 unix_capture_directory(struct wim_dentry *dir_dentry,
84 struct add_image_params *params)
89 struct wim_dentry *child;
92 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
95 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
97 return WIMLIB_ERR_OPENDIR;
100 /* Recurse on directory contents */
104 entry = readdir(dir);
107 ret = WIMLIB_ERR_READ;
108 ERROR_WITH_ERRNO("Error reading the "
109 "directory `%s'", path);
114 if (entry->d_name[0] == '.' && (entry->d_name[1] == '\0'
115 || (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
118 size_t name_len = strlen(entry->d_name);
120 path[path_len] = '/';
121 memcpy(&path[path_len + 1], entry->d_name, name_len + 1);
122 ret = unix_build_dentry_tree_recursive(&child,
124 path_len + 1 + name_len,
129 dentry_add_child(dir_dentry, child);
131 path[path_len] = '\0';
137 unix_capture_symlink(struct wim_dentry **root_p,
139 struct wim_inode *inode,
140 struct add_image_params *params)
142 char deref_name_buf[4096];
143 ssize_t deref_name_len;
147 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
148 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
150 /* The idea here is to call readlink() to get the UNIX target of the
151 * symbolic link, then turn the target into a reparse point data buffer
152 * that contains a relative or absolute symbolic link. */
153 deref_name_len = readlink(path, deref_name_buf,
154 sizeof(deref_name_buf) - 1);
155 if (deref_name_len < 0) {
156 ERROR_WITH_ERRNO("Failed to read target of "
157 "symbolic link `%s'", path);
158 return WIMLIB_ERR_READLINK;
161 dest = deref_name_buf;
163 dest[deref_name_len] = '\0';
164 DEBUG("Read symlink `%s'", dest);
166 if ((params->add_flags & WIMLIB_ADD_FLAG_RPFIX) &&
169 dest = capture_fixup_absolute_symlink(dest,
170 params->capture_root_ino,
171 params->capture_root_dev);
173 /* RPFIX (reparse point fixup) mode: Ignore
174 * absolute symbolic link that points out of the
175 * tree to be captured. */
176 free_dentry(*root_p);
178 params->progress.scan.cur_path = path;
179 params->progress.scan.symlink_target = deref_name_buf;
180 do_capture_progress(params,
181 WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK,
185 inode->i_not_rpfixed = 0;
187 ret = wim_inode_set_symlink(inode, dest, params->lookup_table);
191 /* Unfortunately, Windows seems to have the concept of "file" symbolic
192 * links as being different from "directory" symbolic links... so
193 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the symbolic link if the
194 * *target* of the symbolic link is a directory. */
196 if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
197 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
202 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
205 struct add_image_params *params)
207 struct wim_dentry *root = NULL;
209 struct wim_inode *inode = NULL;
212 if (should_exclude_path(path + params->capture_root_nchars,
213 path_len - params->capture_root_nchars,
220 if ((params->add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) ||
221 (params->add_flags & WIMLIB_ADD_FLAG_ROOT))
222 ret = stat(path, &stbuf);
224 ret = lstat(path, &stbuf);
227 ERROR_WITH_ERRNO("Failed to stat \"%s\"", path);
228 ret = WIMLIB_ERR_STAT;
232 if (!S_ISREG(stbuf.st_mode) &&
233 !S_ISDIR(stbuf.st_mode) &&
234 !S_ISLNK(stbuf.st_mode))
236 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
238 ERROR("Can't archive unsupported file \"%s\"", path);
239 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
242 params->progress.scan.cur_path = path;
243 do_capture_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
248 ret = inode_table_new_dentry(params->inode_table,
249 path_basename_with_len(path, path_len),
250 stbuf.st_ino, stbuf.st_dev,
251 S_ISDIR(stbuf.st_mode), &root);
255 inode = root->d_inode;
257 if (inode->i_nlink > 1) {
258 /* Already captured this inode? */
263 #ifdef HAVE_STAT_NANOSECOND_PRECISION
264 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
265 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
266 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
268 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
269 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
270 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
272 inode->i_resolved = 1;
273 if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
274 ret = inode_set_unix_data(inode, stbuf.st_uid,
277 params->lookup_table,
278 UNIX_DATA_ALL | UNIX_DATA_CREATE);
282 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
283 if (S_ISREG(stbuf.st_mode))
284 ret = unix_capture_regular_file(path, stbuf.st_size,
285 inode, params->unhashed_streams);
286 else if (S_ISDIR(stbuf.st_mode))
287 ret = unix_capture_directory(root, path, path_len, params);
289 ret = unix_capture_symlink(&root, path, inode, params);
298 params->progress.scan.cur_path = path;
300 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
302 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
305 free_dentry_tree(root, params->lookup_table);
312 * unix_build_dentry_tree():
313 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
314 * version; no NTFS-specific data is captured).
316 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
317 * modified if successful. Set to NULL if the file or directory was
318 * excluded from capture.
320 * @root_disk_path: The path to the root of the directory tree on disk.
322 * @params: See doc for `struct add_image_params'.
324 * @return: 0 on success, nonzero on failure. It is a failure if any of
325 * the files cannot be `stat'ed, or if any of the needed
326 * directories cannot be opened or read. Failure to add the files
327 * to the WIM may still occur later when trying to actually read
328 * the on-disk files during a call to wimlib_write() or
329 * wimlib_overwrite().
332 unix_build_dentry_tree(struct wim_dentry **root_ret,
333 const char *root_disk_path,
334 struct add_image_params *params)
342 struct stat root_stbuf;
343 if (stat(root_disk_path, &root_stbuf)) {
344 ERROR_WITH_ERRNO("Failed to stat \"%s\"", root_disk_path);
345 return WIMLIB_ERR_STAT;
348 params->capture_root_ino = root_stbuf.st_ino;
349 params->capture_root_dev = root_stbuf.st_dev;
352 path_bufsz = min(32790, PATH_MAX + 1);
353 path_len = strlen(root_disk_path);
355 if (path_len >= path_bufsz)
356 return WIMLIB_ERR_INVALID_PARAM;
358 path_buf = MALLOC(path_bufsz);
360 return WIMLIB_ERR_NOMEM;
361 memcpy(path_buf, root_disk_path, path_len + 1);
363 params->capture_root_nchars = path_len;
365 ret = unix_build_dentry_tree_recursive(root_ret, path_buf,
371 #endif /* !__WIN32__ */