]> wimlib.net Git - wimlib/blobdiff - src/add_image.c
Update timestamp code; use utimensat()
[wimlib] / src / add_image.c
index 0d1ffb29c3b4cc0836f3a152babaed6e81049fb2..67a354e4b27b0e3424f020de322a0d22adfc1a3f 100644 (file)
  * 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
@@ -89,7 +98,7 @@ err:
 
 }
 
-
+#ifndef __WIN32__
 /*
  * build_dentry_tree():
  *     Recursively builds a tree of WIM dentries from an on-disk directory
@@ -99,7 +108,7 @@ err:
  *             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,
@@ -114,7 +123,8 @@ err:
  *
  * @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
@@ -123,25 +133,24 @@ err:
  *             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)
@@ -151,8 +160,7 @@ static int build_dentry_tree(struct wim_dentry **root_ret,
                        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)
@@ -164,6 +172,9 @@ static int build_dentry_tree(struct wim_dentry **root_ret,
                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
@@ -172,7 +183,7 @@ static int build_dentry_tree(struct wim_dentry **root_ret,
        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) &&
@@ -184,46 +195,54 @@ static int build_dentry_tree(struct wim_dentry **root_ret,
                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,
@@ -321,9 +340,11 @@ static int build_dentry_tree(struct wim_dentry **root_ret,
                              || (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)
@@ -380,7 +401,7 @@ out:
                free_dentry_tree(root, lookup_table);
        return ret;
 }
-
+#endif /* !__WIN32__ */
 
 enum pattern_type {
        NONE = 0,
@@ -448,11 +469,10 @@ static int pattern_list_add_pattern(struct pattern_list *list,
 
 /* 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;
@@ -469,17 +489,10 @@ static int init_capture_config(const char *_config_str, size_t config_len,
                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;
@@ -554,6 +567,19 @@ out_destroy:
        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)
 {
@@ -571,11 +597,14 @@ static bool match_pattern(const char *path, const char *path_basename,
                                /* 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);
@@ -615,7 +644,7 @@ static const char *canonicalize_target_path(char *target_path)
 {
        char *p;
        if (target_path == NULL)
-               target_path = "";
+               return "";
        for (;;) {
                if (*target_path == '\0')
                        return target_path;
@@ -631,6 +660,17 @@ static const char *canonicalize_target_path(char *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)
@@ -639,19 +679,24 @@ static void canonicalize_targets(struct wimlib_capture_source *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);
 }
 
@@ -659,7 +704,7 @@ static int capture_source_cmp(const void *p1, const void *p2)
  * 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)
 {
@@ -728,7 +773,8 @@ new_filler_directory(const char *name)
        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;
        }
@@ -738,31 +784,35 @@ new_filler_directory(const char *name)
 /* 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;
 
 }
@@ -775,7 +825,8 @@ static int do_overlay(struct wim_dentry *target,
  * @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,
@@ -784,6 +835,9 @@ static int attach_branch(struct wim_dentry **root_p,
        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) {
@@ -818,9 +872,11 @@ static int attach_branch(struct wim_dentry **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 == '/');
        }
 
@@ -850,7 +906,8 @@ WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
                            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;
@@ -875,10 +932,25 @@ WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
                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;
@@ -900,14 +972,15 @@ WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
                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;
@@ -924,27 +997,35 @@ WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
        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;
@@ -961,21 +1042,20 @@ WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
                                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);
@@ -1006,19 +1086,24 @@ WIMLIBAPI int wimlib_add_image_multisource(WIMStruct *w,
        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;
 }