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/.
26 #include "wimlib_internal.h"
28 #include "lookup_table.h"
29 #include "timestamp.h"
38 unix_capture_regular_file(const char *path,
40 struct wim_inode *inode,
41 struct wim_lookup_table *lookup_table)
43 inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
45 /* Empty files do not have to have a lookup table entry. */
47 struct wim_lookup_table_entry *lte;
50 file_on_disk = STRDUP(path);
52 return WIMLIB_ERR_NOMEM;
53 lte = new_lookup_table_entry();
56 return WIMLIB_ERR_NOMEM;
58 lte->file_on_disk = file_on_disk;
59 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
60 lte->resource_entry.original_size = size;
61 lookup_table_insert_unhashed(lookup_table, lte, inode, 0);
68 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
71 struct add_image_params *params);
74 unix_capture_directory(struct wim_dentry *dir_dentry,
77 struct add_image_params *params)
82 struct wim_dentry *child;
85 dir_dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
88 ERROR_WITH_ERRNO("Failed to open the directory `%s'",
90 return WIMLIB_ERR_OPENDIR;
93 /* Recurse on directory contents */
100 ret = WIMLIB_ERR_READ;
101 ERROR_WITH_ERRNO("Error reading the "
102 "directory `%s'", path);
107 if (entry->d_name[0] == '.' && (entry->d_name[1] == '\0'
108 || (entry->d_name[1] == '.' && entry->d_name[2] == '\0')))
111 size_t name_len = strlen(entry->d_name);
113 path[path_len] = '/';
114 memcpy(&path[path_len + 1], entry->d_name, name_len + 1);
115 ret = unix_build_dentry_tree_recursive(&child,
117 path_len + 1 + name_len,
122 dentry_add_child(dir_dentry, child);
129 unix_capture_symlink(struct wim_dentry **root_p,
131 struct wim_inode *inode,
132 struct add_image_params *params)
134 char deref_name_buf[4096];
135 ssize_t deref_name_len;
138 inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
139 inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
141 /* The idea here is to call readlink() to get the UNIX target of
142 * the symbolic link, then turn the target into a reparse point
143 * data buffer that contains a relative or absolute symbolic
144 * link (NOT a junction point or *full* path symbolic link with
147 deref_name_len = readlink(path, deref_name_buf,
148 sizeof(deref_name_buf) - 1);
149 if (deref_name_len >= 0) {
150 char *dest = deref_name_buf;
152 dest[deref_name_len] = '\0';
153 DEBUG("Read symlink `%s'", dest);
155 if ((params->add_flags & WIMLIB_ADD_FLAG_RPFIX) &&
158 dest = capture_fixup_absolute_symlink(dest,
159 params->capture_root_ino,
160 params->capture_root_dev);
162 WARNING("Ignoring out of tree absolute symlink "
164 " (Use --norpfix to capture "
165 "absolute symlinks as-is)",
166 path, deref_name_buf);
167 free_dentry(*root_p);
171 inode->i_not_rpfixed = 0;
173 ret = wim_inode_set_symlink(inode, dest, params->lookup_table);
175 /* Unfortunately, Windows seems to have the concept of
176 * "file" symbolic links as being different from
177 * "directory" symbolic links... so
178 * FILE_ATTRIBUTE_DIRECTORY needs to be set on the
179 * symbolic link if the *target* of the symbolic link is
182 if (stat(path, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
183 inode->i_attributes |= FILE_ATTRIBUTE_DIRECTORY;
186 ERROR_WITH_ERRNO("Failed to read target of "
187 "symbolic link `%s'", path);
188 ret = WIMLIB_ERR_READLINK;
194 unix_build_dentry_tree_recursive(struct wim_dentry **root_ret,
197 struct add_image_params *params)
199 struct wim_dentry *root = NULL;
201 struct wim_inode *inode;
203 if (exclude_path(path, path_len, params->config, true)) {
204 if ((params->add_flags & WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE)
205 && params->progress_func)
207 union wimlib_progress_info info;
208 info.scan.cur_path = path;
209 info.scan.excluded = true;
210 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
215 if ((params->add_flags & WIMLIB_ADD_FLAG_VERBOSE)
216 && params->progress_func)
218 union wimlib_progress_info info;
219 info.scan.cur_path = path;
220 info.scan.excluded = false;
221 params->progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
225 int (*stat_fn)(const char *restrict, struct stat *restrict);
226 if ((params->add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) ||
227 (params->add_flags & WIMLIB_ADD_FLAG_ROOT))
232 ret = (*stat_fn)(path, &stbuf);
234 ERROR_WITH_ERRNO("Failed to stat `%s'", path);
235 ret = WIMLIB_ERR_STAT;
238 if (!S_ISREG(stbuf.st_mode) && !S_ISDIR(stbuf.st_mode)
239 && !S_ISLNK(stbuf.st_mode)) {
240 ERROR("`%s' is not a regular file, directory, or symbolic link.",
242 ret = WIMLIB_ERR_SPECIAL_FILE;
246 ret = inode_table_new_dentry(params->inode_table,
247 path_basename_with_len(path, path_len),
248 stbuf.st_ino, stbuf.st_dev, false, &root);
252 inode = root->d_inode;
254 if (inode->i_nlink > 1) /* Already captured this inode? */
257 #ifdef HAVE_STAT_NANOSECOND_PRECISION
258 inode->i_creation_time = timespec_to_wim_timestamp(stbuf.st_mtim);
259 inode->i_last_write_time = timespec_to_wim_timestamp(stbuf.st_mtim);
260 inode->i_last_access_time = timespec_to_wim_timestamp(stbuf.st_atim);
262 inode->i_creation_time = unix_timestamp_to_wim(stbuf.st_mtime);
263 inode->i_last_write_time = unix_timestamp_to_wim(stbuf.st_mtime);
264 inode->i_last_access_time = unix_timestamp_to_wim(stbuf.st_atime);
266 inode->i_resolved = 1;
267 if (params->add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
268 ret = inode_set_unix_data(inode, stbuf.st_uid,
271 params->lookup_table,
272 UNIX_DATA_ALL | UNIX_DATA_CREATE);
276 params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
277 if (S_ISREG(stbuf.st_mode))
278 ret = unix_capture_regular_file(path, stbuf.st_size,
279 inode, params->lookup_table);
280 else if (S_ISDIR(stbuf.st_mode))
281 ret = unix_capture_directory(root, path, path_len, params);
283 ret = unix_capture_symlink(&root, path, inode, params);
288 free_dentry_tree(root, params->lookup_table);
293 * unix_build_dentry_tree():
294 * Builds a tree of WIM dentries from an on-disk directory tree (UNIX
295 * version; no NTFS-specific data is captured).
297 * @root_ret: Place to return a pointer to the root of the dentry tree. Only
298 * modified if successful. Set to NULL if the file or directory was
299 * excluded from capture.
301 * @root_disk_path: The path to the root of the directory tree on disk.
303 * @params: See doc for `struct add_image_params'.
305 * @return: 0 on success, nonzero on failure. It is a failure if any of
306 * the files cannot be `stat'ed, or if any of the needed
307 * directories cannot be opened or read. Failure to add the files
308 * to the WIM may still occur later when trying to actually read
309 * the on-disk files during a call to wimlib_write() or
310 * wimlib_overwrite().
313 unix_build_dentry_tree(struct wim_dentry **root_ret,
314 const char *root_disk_path,
315 struct add_image_params *params)
323 struct stat root_stbuf;
324 if (stat(root_disk_path, &root_stbuf)) {
325 ERROR_WITH_ERRNO("Failed to stat \"%s\"", root_disk_path);
326 return WIMLIB_ERR_STAT;
329 if ((params->add_flags & WIMLIB_ADD_FLAG_ROOT) &&
330 !S_ISDIR(root_stbuf.st_mode))
332 ERROR("Root of capture \"%s\" is not a directory",
334 return WIMLIB_ERR_NOTDIR;
336 params->capture_root_ino = root_stbuf.st_ino;
337 params->capture_root_dev = root_stbuf.st_dev;
340 path_bufsz = min(32790, PATH_MAX + 1);
341 path_len = strlen(root_disk_path);
343 if (path_len >= path_bufsz)
344 return WIMLIB_ERR_INVALID_PARAM;
346 path_buf = MALLOC(path_bufsz);
348 return WIMLIB_ERR_NOMEM;
349 memcpy(path_buf, root_disk_path, path_len + 1);
351 ret = unix_build_dentry_tree_recursive(root_ret, path_buf,
357 #endif /* !__WIN32__ */