* 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;
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,
/* A file name pattern */
string = path_basename;
}
+
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("Canonical target: \"%s\"", sources->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) {
- /* Set the inode number to 0 for now. The final inode number
+ /* Leave the inode number as 0 for now. The final inode number
* will be assigned later by assign_inode_numbers(). */
- dentry->d_inode->i_ino = 0;
dentry->d_inode->i_resolved = 1;
dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
}
{
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);
return WIMLIB_ERR_INVALID_OVERLAY;
}
}
+ free_dentry(branch);
return 0;
}
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) {
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;
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;
+ #ifdef __WIN32__
+ win32_acquire_capture_privileges();
+ #endif
+
root_dentry = NULL;
i = 0;
do {
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);
out_destroy_capture_config:
destroy_capture_config(&config);
out:
+#ifdef __WIN32__
+ win32_release_capture_privileges();
+#endif
return ret;
}