* along with wimlib; if not, see http://www.gnu.org/licenses/.
*/
+#include "config.h"
+
+#ifdef __WIN32__
+# include "win32.h"
+#else
+# include <dirent.h>
+# include <sys/stat.h>
+# include <fnmatch.h>
+# include "timestamp.h"
+#endif
+
#include "wimlib_internal.h"
#include "dentry.h"
-#include "timestamp.h"
#include "lookup_table.h"
#include "xml.h"
-#include <string.h>
-#include <fnmatch.h>
-#include <stdlib.h>
+
#include <ctype.h>
-#include <sys/stat.h>
-#include <dirent.h>
#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
-#define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
-#define WIMLIB_ADD_IMAGE_FLAG_SOURCE 0x40000000
+#ifdef HAVE_ALLOCA_H
+# include <alloca.h>
+#endif
/*
* Adds the dentry tree and security data for a new image to the image metadata
}
-
+#ifndef __WIN32__
/*
* build_dentry_tree():
* Recursively builds a tree of WIM dentries from an on-disk directory
* modified if successful. Set to NULL if the file or directory was
* excluded from capture.
*
- * @root_disk_path: The path to the root of the directory tree on disk.
+ * @root_disk_path: The path to the root of the directory tree on disk (UTF-8).
*
* @lookup_table: The lookup table for the WIM file. For each file added to the
* dentry tree being built, an entry is added to the lookup table,
*
* @add_flags: Bitwise or of WIMLIB_ADD_IMAGE_FLAG_*
*
- * @extra_arg: Ignored. (Only used in NTFS mode.)
+ * @extra_arg: Ignored in UNIX builds; used to pass sd_set pointer in Windows
+ * builds.
*
* @return: 0 on success, nonzero on failure. It is a failure if any of
* the files cannot be `stat'ed, or if any of the needed
* the on-disk files during a call to wimlib_write() or
* wimlib_overwrite().
*/
-static int build_dentry_tree(struct wim_dentry **root_ret,
- const char *root_disk_path,
- struct wim_lookup_table *lookup_table,
- struct wim_security_data *sd,
- const struct capture_config *config,
- int add_image_flags,
- wimlib_progress_func_t progress_func,
- void *extra_arg)
+static int unix_build_dentry_tree(struct wim_dentry **root_ret,
+ const char *root_disk_path,
+ struct wim_lookup_table *lookup_table,
+ struct wim_security_data *sd,
+ const struct capture_config *config,
+ int add_image_flags,
+ wimlib_progress_func_t progress_func,
+ void *extra_arg)
{
- struct stat root_stbuf;
+ struct wim_dentry *root = NULL;
int ret = 0;
- int (*stat_fn)(const char *restrict, struct stat *restrict);
- struct wim_dentry *root;
struct wim_inode *inode;
if (exclude_path(root_disk_path, config, true)) {
if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) {
ERROR("Cannot exclude the root directory from capture");
- return WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
+ ret = WIMLIB_ERR_INVALID_CAPTURE_CONFIG;
+ goto out;
}
if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
&& progress_func)
info.scan.excluded = true;
progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
}
- *root_ret = NULL;
- return 0;
+ goto out;
}
if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
progress_func(WIMLIB_PROGRESS_MSG_SCAN_DENTRY, &info);
}
+ /* UNIX version of capturing a directory tree */
+ struct stat root_stbuf;
+ int (*stat_fn)(const char *restrict, struct stat *restrict);
if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)
stat_fn = stat;
else
ret = (*stat_fn)(root_disk_path, &root_stbuf);
if (ret != 0) {
ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
- return WIMLIB_ERR_STAT;
+ goto out;
}
if ((add_image_flags & WIMLIB_ADD_IMAGE_FLAG_ROOT) &&
ret = stat(root_disk_path, &root_stbuf);
if (ret != 0) {
ERROR_WITH_ERRNO("Failed to stat `%s'", root_disk_path);
- return WIMLIB_ERR_STAT;
+ ret = WIMLIB_ERR_STAT;
+ goto out;
}
if (!S_ISDIR(root_stbuf.st_mode)) {
ERROR("`%s' is not a directory", root_disk_path);
- return WIMLIB_ERR_NOTDIR;
+ ret = WIMLIB_ERR_NOTDIR;
+ goto out;
}
}
if (!S_ISREG(root_stbuf.st_mode) && !S_ISDIR(root_stbuf.st_mode)
&& !S_ISLNK(root_stbuf.st_mode)) {
ERROR("`%s' is not a regular file, directory, or symbolic link.",
root_disk_path);
- return WIMLIB_ERR_SPECIAL_FILE;
+ ret = WIMLIB_ERR_SPECIAL_FILE;
+ goto out;
}
root = new_dentry_with_timeless_inode(path_basename(root_disk_path));
if (!root) {
if (errno == EILSEQ)
- return WIMLIB_ERR_INVALID_UTF8_STRING;
+ ret = WIMLIB_ERR_INVALID_UTF8_STRING;
else if (errno == ENOMEM)
- return WIMLIB_ERR_NOMEM;
+ ret = WIMLIB_ERR_NOMEM;
else
- return WIMLIB_ERR_ICONV_NOT_AVAILABLE;
+ ret = WIMLIB_ERR_ICONV_NOT_AVAILABLE;
+ goto out;
}
inode = root->d_inode;
#ifdef HAVE_STAT_NANOSECOND_PRECISION
- inode->i_creation_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
- inode->i_last_write_time = timespec_to_wim_timestamp(&root_stbuf.st_mtim);
- inode->i_last_access_time = timespec_to_wim_timestamp(&root_stbuf.st_atim);
+ inode->i_creation_time = timespec_to_wim_timestamp(root_stbuf.st_mtim);
+ inode->i_last_write_time = timespec_to_wim_timestamp(root_stbuf.st_mtim);
+ inode->i_last_access_time = timespec_to_wim_timestamp(root_stbuf.st_atim);
#else
inode->i_creation_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
inode->i_last_write_time = unix_timestamp_to_wim(root_stbuf.st_mtime);
inode->i_last_access_time = unix_timestamp_to_wim(root_stbuf.st_atime);
#endif
- if (sizeof(ino_t) >= 8)
- inode->i_ino = (u64)root_stbuf.st_ino;
- else
- inode->i_ino = (u64)root_stbuf.st_ino |
- ((u64)root_stbuf.st_dev << ((sizeof(ino_t) * 8) & 63));
+ /* Leave the inode number at 0 for directories. */
+ if (!S_ISDIR(root_stbuf.st_mode)) {
+ if (sizeof(ino_t) >= 8)
+ inode->i_ino = (u64)root_stbuf.st_ino;
+ else
+ inode->i_ino = (u64)root_stbuf.st_ino |
+ ((u64)root_stbuf.st_dev <<
+ ((sizeof(ino_t) * 8) & 63));
+ }
inode->i_resolved = 1;
if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
ret = inode_set_unix_data(inode, root_stbuf.st_uid,
|| (result->d_name[1] == '.' && result->d_name[2] == '\0')))
continue;
strcpy(name + len + 1, result->d_name);
- ret = build_dentry_tree(&child, name, lookup_table,
- NULL, config, add_image_flags,
- progress_func, NULL);
+ ret = unix_build_dentry_tree(&child, name,
+ lookup_table,
+ NULL, config,
+ add_image_flags,
+ progress_func, NULL);
if (ret != 0)
break;
if (child)
free_dentry_tree(root, lookup_table);
return ret;
}
-
+#endif /* !__WIN32__ */
enum pattern_type {
NONE = 0,
/* Parses the contents of the image capture configuration file and fills in a
* `struct capture_config'. */
-static int init_capture_config(const char *_config_str, size_t config_len,
- const char *_prefix, struct capture_config *config)
+static int init_capture_config(struct capture_config *config,
+ const char *_config_str, size_t config_len)
{
char *config_str;
- char *prefix;
char *p;
char *eol;
char *next_p;
ERROR("Could not duplicate capture config string");
return WIMLIB_ERR_NOMEM;
}
- prefix = STRDUP(_prefix);
- if (!prefix) {
- FREE(config_str);
- return WIMLIB_ERR_NOMEM;
- }
memcpy(config_str, _config_str, config_len);
next_p = config_str;
config->config_str = config_str;
- config->prefix = prefix;
- config->prefix_len = strlen(prefix);
while (bytes_remaining) {
line_no++;
p = next_p;
return ret;
}
+static int capture_config_set_prefix(struct capture_config *config,
+ const char *_prefix)
+{
+ char *prefix = STRDUP(_prefix);
+
+ if (!prefix)
+ return WIMLIB_ERR_NOMEM;
+ FREE(config->prefix);
+ config->prefix = prefix;
+ config->prefix_len = strlen(prefix);
+ return 0;
+}
+
static bool match_pattern(const char *path, const char *path_basename,
const struct pattern_list *list)
{
/* A file name pattern */
string = path_basename;
}
+
+ /* Warning: on Windows native builds, fnmatch() calls the
+ * replacement function in win32.c. */
if (fnmatch(pat, string, FNM_PATHNAME
- #ifdef FNM_CASEFOLD
- | FNM_CASEFOLD
- #endif
- ) == 0)
+ #ifdef FNM_CASEFOLD
+ | FNM_CASEFOLD
+ #endif
+ ) == 0)
{
DEBUG("`%s' matches the pattern \"%s\"",
string, pat);
{
char *p;
if (target_path == NULL)
- target_path = "";
+ return "";
for (;;) {
if (*target_path == '\0')
return target_path;
return target_path;
}
+#ifdef __WIN32__
+static void zap_backslashes(char *s)
+{
+ while (*s) {
+ if (*s == '\\')
+ *s = '/';
+ s++;
+ }
+}
+#endif
+
/* Strip leading and trailing slashes from the target paths */
static void canonicalize_targets(struct wimlib_capture_source *sources,
size_t num_sources)
DEBUG("Canonicalizing { source: \"%s\", target=\"%s\"}",
sources->fs_source_path,
sources->wim_target_path);
+#ifdef __WIN32__
+ /* The Windows API can handle forward slashes. Just get rid of
+ * backslashes to avoid confusing other parts of the library
+ * code. */
+ zap_backslashes(sources->fs_source_path);
+ if (sources->wim_target_path)
+ zap_backslashes(sources->wim_target_path);
+#endif
sources->wim_target_path =
(char*)canonicalize_target_path(sources->wim_target_path);
- DEBUG("\"%s\"", sources->wim_target_path);
+ DEBUG("Canonical target: \"%s\"", sources->wim_target_path);
sources++;
}
}
static int capture_source_cmp(const void *p1, const void *p2)
{
- const struct wimlib_capture_source *s1, *s2;
-
- s1 = p1;
- s2 = p2;
+ const struct wimlib_capture_source *s1 = p1, *s2 = p2;
return strcmp(s1->wim_target_path, s2->wim_target_path);
}
* after leading and trailing forward slashes are stripped.
*
* One purpose of this is to make sure that target paths that are inside other
- * target paths are extracted after the containing target paths. */
+ * target paths are added after the containing target paths. */
static void sort_sources(struct wimlib_capture_source *sources,
size_t num_sources)
{
DEBUG("Creating filler directory \"%s\"", name);
dentry = new_dentry_with_inode(name);
if (dentry) {
- dentry->d_inode->i_ino = 0;
+ /* Leave the inode number as 0 for now. The final inode number
+ * will be assigned later by assign_inode_numbers(). */
dentry->d_inode->i_resolved = 1;
dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
}
/* Transfers the children of @branch to @target. It is an error if @target is
* not a directory or if both @branch and @target contain a child dentry with
* the same name. */
-static int do_overlay(struct wim_dentry *target,
- struct wim_dentry *branch)
+static int do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
{
struct rb_root *rb_root;
+ DEBUG("Doing overlay %s => %s",
+ branch->file_name_utf8, target->file_name_utf8);
+
if (!dentry_is_directory(target)) {
ERROR("Cannot overlay directory `%s' over non-directory",
branch->file_name_utf8);
- /* XXX Use a different error code */
- return WIMLIB_ERR_INVALID_DENTRY;
+ return WIMLIB_ERR_INVALID_OVERLAY;
}
rb_root = &branch->d_inode->i_children;
while (rb_root->rb_node) { /* While @branch has children... */
- struct wim_dentry *child;
-
- child = container_of(rb_root->rb_node, struct wim_dentry, rb_node);
+ struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
+ /* Move @child to the directory @target */
unlink_dentry(child);
if (!dentry_add_child(target, child)) {
+ /* Revert the change to avoid leaking the directory tree
+ * rooted at @child */
dentry_add_child(branch, child);
- ERROR("Overlay error: file `%s' already exists as child of `%s'",
+ ERROR("Overlay error: file `%s' already exists "
+ "as a child of `%s'",
child->file_name_utf8, target->file_name_utf8);
- return WIMLIB_ERR_INVALID_DENTRY;
+ return WIMLIB_ERR_INVALID_OVERLAY;
}
}
+ free_dentry(branch);
return 0;
}
* @branch
* Branch to add.
* @target_path:
- * Path in the WIM image to add the branch.
+ * Path in the WIM image to add the branch, with leading and trailing
+ * slashes stripped.
*/
static int attach_branch(struct wim_dentry **root_p,
struct wim_dentry *branch,
char *slash;
struct wim_dentry *dentry, *parent, *target;
+ DEBUG("Attaching branch \"%s\" => \"%s\"",
+ branch->file_name_utf8, target_path);
+
if (*target_path == '\0') {
/* Target: root directory */
if (*root_p) {
}
parent = dentry;
target_path = slash;
+ /* Skip over slashes. Note: this cannot overrun the length of
+ * the string because the last character cannot be a slash, as
+ * trailing slashes were tripped. */
do {
++target_path;
- wimlib_assert(*target_path != '\0');
} while (*target_path == '/');
}
const struct capture_config *,
int, wimlib_progress_func_t, void *);
void *extra_arg;
- struct wim_dentry *root_dentry = NULL;
+ struct wim_dentry *root_dentry;
+ struct wim_dentry *branch;
struct wim_security_data *sd;
struct capture_config config;
struct wim_image_metadata *imd;
return WIMLIB_ERR_UNSUPPORTED;
#endif
} else {
- capture_tree = build_dentry_tree;
+ #ifdef __WIN32__
+ capture_tree = win32_build_dentry_tree;
+ #else
+ capture_tree = unix_build_dentry_tree;
+ #endif
extra_arg = NULL;
}
+#ifdef __WIN32__
+ if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) {
+ ERROR("Capturing UNIX-specific data is not supported on Windows");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+ if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE) {
+ ERROR("Dereferencing symbolic links is not supported on Windows");
+ return WIMLIB_ERR_INVALID_PARAM;
+ }
+#endif
+
if (!name || !*name) {
ERROR("Must specify a non-empty string for the image name");
return WIMLIB_ERR_INVALID_PARAM;
config_str = default_config;
config_len = strlen(default_config);
}
- memset(&config, 0, sizeof(struct capture_config));
+ ret = init_capture_config(&config, config_str, config_len);
+ if (ret)
+ goto out;
DEBUG("Allocating security data");
-
sd = CALLOC(1, sizeof(struct wim_security_data));
if (!sd) {
ret = WIMLIB_ERR_NOMEM;
- goto out_destroy_config;
+ goto out_destroy_capture_config;
}
sd->total_length = 8;
sd->refcnt = 1;
DEBUG("Building dentry tree.");
if (num_sources == 0) {
root_dentry = new_filler_directory("");
- if (!root_dentry)
+ if (!root_dentry) {
+ ret = WIMLIB_ERR_NOMEM;
goto out_free_security_data;
+ }
} else {
- size_t i = 0;
- struct wim_dentry *branch;
- int flags;
+ size_t i;
+
+ #ifdef __WIN32__
+ win32_acquire_capture_privileges();
+ #endif
+
+ root_dentry = NULL;
+ i = 0;
do {
+ int flags;
+ union wimlib_progress_info progress;
+
DEBUG("Building dentry tree for source %zu of %zu "
"(\"%s\" => \"%s\")", i + 1, num_sources,
sources[i].fs_source_path,
sources[i].wim_target_path);
- union wimlib_progress_info progress;
if (progress_func) {
memset(&progress, 0, sizeof(progress));
progress.scan.source = sources[i].fs_source_path;
progress.scan.wim_target_path = sources[i].wim_target_path;
progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &progress);
}
- ret = init_capture_config(config_str, config_len,
- sources[i].fs_source_path,
- &config);
+ ret = capture_config_set_prefix(&config,
+ sources[i].fs_source_path);
if (ret)
goto out_free_dentry_tree;
flags = add_image_flags | WIMLIB_ADD_IMAGE_FLAG_SOURCE;
goto out_free_dentry_tree;
}
if (branch) {
+ /* Use the target name, not the source name, for
+ * the root of each branch from a capture
+ * source. (This will also set the root dentry
+ * of the entire image to be unnamed.) */
ret = set_dentry_name(branch,
path_basename(sources[i].wim_target_path));
- if (ret) {
- free_dentry_tree(branch, w->lookup_table);
- goto out_free_dentry_tree;
- }
+ if (ret)
+ goto out_free_branch;
ret = attach_branch(&root_dentry, branch,
sources[i].wim_target_path);
- if (ret) {
- free_dentry_tree(branch, w->lookup_table);
- goto out_free_dentry_tree;
- }
+ if (ret)
+ goto out_free_branch;
}
- destroy_capture_config(&config);
if (progress_func)
progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &progress);
} while (++i != num_sources);
if (add_image_flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
wimlib_set_boot_idx(w, w->hdr.image_count);
ret = 0;
- goto out;
+ goto out_destroy_capture_config;
out_destroy_imd:
destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
w->lookup_table);
w->hdr.image_count--;
- return ret;
+ goto out;
+out_free_branch:
+ free_dentry_tree(branch, w->lookup_table);
out_free_dentry_tree:
free_dentry_tree(root_dentry, w->lookup_table);
out_free_security_data:
free_security_data(sd);
-out_destroy_config:
+out_destroy_capture_config:
destroy_capture_config(&config);
out:
+#ifdef __WIN32__
+ win32_release_capture_privileges();
+#endif
return ret;
}