Comments etc.
authorEric Biggers <ebiggers3@gmail.com>
Wed, 29 Aug 2012 04:48:58 +0000 (23:48 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Wed, 29 Aug 2012 04:48:58 +0000 (23:48 -0500)
src/lookup_table.c
src/modify.c
src/ntfs-capture.c

index 1e6c05f4ccfab586ee75b98d4c6c0735c75fe0e3..e8f44eac70355b6a0efdc895b15dd71111813469 100644 (file)
@@ -73,11 +73,31 @@ struct lookup_table_entry *new_lookup_table_entry()
 void free_lookup_table_entry(struct lookup_table_entry *lte)
 {
        if (lte) {
 void free_lookup_table_entry(struct lookup_table_entry *lte)
 {
        if (lte) {
+#ifdef WITH_FUSE
                if (lte->staging_list.next)
                        list_del(&lte->staging_list);
                if (lte->staging_list.next)
                        list_del(&lte->staging_list);
-               if (lte->resource_location != RESOURCE_IN_WIM &&
-                   lte->resource_location != RESOURCE_NONEXISTENT)
+#endif
+               switch (lte->resource_location) {
+               case RESOURCE_IN_STAGING_FILE:
+               case RESOURCE_IN_ATTACHED_BUFFER:
+               case RESOURCE_IN_FILE_ON_DISK:
+                       wimlib_assert(&lte->file_on_disk ==
+                                     &lte->staging_file_name ==
+                                     &lte->attached_buffer);
                        FREE(lte->file_on_disk);
                        FREE(lte->file_on_disk);
+                       break;
+#ifdef WITH_NTFS_3G
+               case RESOURCE_IN_NTFS_VOLUME:
+                       if (lte->ntfs_loc) {
+                               FREE(lte->ntfs_loc->path_utf8);
+                               FREE(lte->ntfs_loc->stream_name_utf16);
+                               FREE(lte->ntfs_loc);
+                       }
+                       break;
+#endif
+               default:
+                       break;
+               }
                FREE(lte);
        }
 }
                FREE(lte);
        }
 }
index 3a40c90e9f1f6ac598610535c7427ba00209548d..681624c6054488629e62e3c750fe8e1a07a4f10a 100644 (file)
@@ -41,7 +41,7 @@
 #include <unistd.h>
 
 /** Private flag: Used to mark that we currently adding the root directory of
 #include <unistd.h>
 
 /** Private flag: Used to mark that we currently adding the root directory of
- * the WIM. */
+ * the WIM image. */
 #define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
 
 void destroy_image_metadata(struct image_metadata *imd,struct lookup_table *lt)
 #define WIMLIB_ADD_IMAGE_FLAG_ROOT 0x80000000
 
 void destroy_image_metadata(struct image_metadata *imd,struct lookup_table *lt)
@@ -523,6 +523,7 @@ enum pattern_type {
        ALIGNMENT_LIST,
 };
 
        ALIGNMENT_LIST,
 };
 
+/* Default capture configuration file when none is specified. */
 static const char *default_config =
 "[ExclusionList]\n"
 "\\$ntfs.log\n"
 static const char *default_config =
 "[ExclusionList]\n"
 "\\$ntfs.log\n"
@@ -570,6 +571,8 @@ static int pattern_list_add_pattern(struct pattern_list *list,
        return 0;
 }
 
        return 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(const char *_config_str, size_t config_len,
                               const char *_prefix, struct capture_config *config)
 {
@@ -723,6 +726,15 @@ static void print_capture_config(const struct capture_config *config)
        }
 }
 
        }
 }
 
+/* Return true if the image capture configuration file indicates we should
+ * exclude the filename @path from capture.
+ *
+ * If @exclude_prefix is %true, the part of the path up and including the name
+ * of the directory being captured is not included in the path for matching
+ * purposes.  This allows, for example, a pattern like /hiberfil.sys to match a
+ * file /mnt/windows7/hiberfil.sys if we are capturing the /mnt/windows7
+ * directory.
+ */
 bool exclude_path(const char *path, const struct capture_config *config,
                  bool exclude_prefix)
 {
 bool exclude_path(const char *path, const struct capture_config *config,
                  bool exclude_prefix)
 {
@@ -740,6 +752,18 @@ bool exclude_path(const char *path, const struct capture_config *config,
 
 
 
 
 
 
+/*
+ * Adds an image to the WIM, delegating the capture of the dentry tree and
+ * security data to the function @capture_tree passed as a parameter.
+ * Currently, @capture_tree may be build_dentry_tree() for capturing a "regular"
+ * directory tree on disk, or build_dentry_tree_ntfs() for capturing a WIM image
+ * directory from a NTFS volume using libntfs-3g.
+ *
+ * The @capture_tree function is also expected to create lookup table entries
+ * for all the file streams it captures and insert them into @lookup_table,
+ * being careful to look for identical entries that already exist and simply
+ * increment the reference count for them rather than duplicating the entry.
+ */
 int do_add_image(WIMStruct *w, const char *dir, const char *name,
                 const char *config_str, size_t config_len,
                 int flags,
 int do_add_image(WIMStruct *w, const char *dir, const char *name,
                 const char *config_str, size_t config_len,
                 int flags,
@@ -756,14 +780,14 @@ int do_add_image(WIMStruct *w, const char *dir, const char *name,
        struct link_group_table *lgt;
        int ret;
 
        struct link_group_table *lgt;
        int ret;
 
-       DEBUG("Adding dentry tree from dir `%s'.", dir);
+       DEBUG("Adding dentry tree from directory or NTFS volume `%s'.", dir);
 
        if (!name || !*name) {
                ERROR("Must specify a non-empty string for the image name");
                return WIMLIB_ERR_INVALID_PARAM;
        }
        if (!dir) {
 
        if (!name || !*name) {
                ERROR("Must specify a non-empty string for the image name");
                return WIMLIB_ERR_INVALID_PARAM;
        }
        if (!dir) {
-               ERROR("Must specify the name of a directory");
+               ERROR("Must specify the name of a directory or NTFS volume");
                return WIMLIB_ERR_INVALID_PARAM;
        }
 
                return WIMLIB_ERR_INVALID_PARAM;
        }
 
index a38a4ecb8b357746a288a5db764d55bfd5664a11..eadedac725a5efc33872a76855ddb5904852b4b0 100644 (file)
@@ -166,6 +166,7 @@ static int sd_set_add_sd(struct sd_set *sd_set, const char descriptor[],
        sd->descriptors[sd->num_entries] = descr_copy;
        sd->sizes[sd->num_entries] = size;
        sd->num_entries++;
        sd->descriptors[sd->num_entries] = descr_copy;
        sd->sizes[sd->num_entries] = size;
        sd->num_entries++;
+       DEBUG("There are now %d security descriptors", sd->num_entries);
        sd->total_length += size + sizeof(sd->sizes[0]);
 
        if (sd_set->root)
        sd->total_length += size + sizeof(sd->sizes[0]);
 
        if (sd_set->root)
@@ -251,7 +252,7 @@ static int capture_ntfs_streams(struct dentry *dentry, ntfs_inode *ni,
        /* Get context to search the streams of the NTFS file. */
        actx = ntfs_attr_get_search_ctx(ni, NULL);
        if (!actx) {
        /* Get context to search the streams of the NTFS file. */
        actx = ntfs_attr_get_search_ctx(ni, NULL);
        if (!actx) {
-               ERROR_WITH_ERRNO("Cannot get attribute search "
+               ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
                                 "context");
                return WIMLIB_ERR_NTFS_3G;
        }
                                 "context");
                return WIMLIB_ERR_NTFS_3G;
        }
@@ -315,9 +316,10 @@ static int capture_ntfs_streams(struct dentry *dentry, ntfs_inode *ni,
                        dentry->lte = lte;
                } else {
                        struct ads_entry *new_ads_entry;
                        dentry->lte = lte;
                } else {
                        struct ads_entry *new_ads_entry;
+                       size_t stream_name_utf8_len;
                        stream_name_utf8 = utf16_to_utf8((const char*)attr_record_name(actx->attr),
                                                         actx->attr->name_length,
                        stream_name_utf8 = utf16_to_utf8((const char*)attr_record_name(actx->attr),
                                                         actx->attr->name_length,
-                                                        &stream_name_utf16_len);
+                                                        &stream_name_utf8_len);
                        if (!stream_name_utf8)
                                goto out_free_lte;
                        new_ads_entry = dentry_add_ads(dentry, stream_name_utf8);
                        if (!stream_name_utf8)
                                goto out_free_lte;
                        new_ads_entry = dentry_add_ads(dentry, stream_name_utf8);
@@ -432,7 +434,8 @@ out:
  */
 static int build_dentry_tree_ntfs_recursive(struct dentry **root_p,
                                            ntfs_inode *ni,
  */
 static int build_dentry_tree_ntfs_recursive(struct dentry **root_p,
                                            ntfs_inode *ni,
-                                           char path[], size_t path_len,
+                                           char path[],
+                                           size_t path_len,
                                            struct lookup_table *lookup_table,
                                            struct sd_set *sd_set,
                                            const struct capture_config *config,
                                            struct lookup_table *lookup_table,
                                            struct sd_set *sd_set,
                                            const struct capture_config *config,
@@ -440,7 +443,7 @@ static int build_dentry_tree_ntfs_recursive(struct dentry **root_p,
 {
        u32 attributes;
        int mrec_flags;
 {
        u32 attributes;
        int mrec_flags;
-       u32 sd_size;
+       u32 sd_size = 0;
        int ret = 0;
        struct dentry *root;
 
        int ret = 0;
        struct dentry *root;
 
@@ -461,7 +464,6 @@ static int build_dentry_tree_ntfs_recursive(struct dentry **root_p,
        root->creation_time    = le64_to_cpu(ni->creation_time);
        root->last_write_time  = le64_to_cpu(ni->last_data_change_time);
        root->last_access_time = le64_to_cpu(ni->last_access_time);
        root->creation_time    = le64_to_cpu(ni->creation_time);
        root->last_write_time  = le64_to_cpu(ni->last_data_change_time);
        root->last_access_time = le64_to_cpu(ni->last_access_time);
-       root->security_id      = le32_to_cpu(ni->security_id);
        root->attributes       = le32_to_cpu(attributes);
        root->link_group_id    = ni->mft_no;
        root->resolved         = true;
        root->attributes       = le32_to_cpu(attributes);
        root->link_group_id    = ni->mft_no;
        root->resolved         = true;
@@ -523,6 +525,10 @@ static int build_dentry_tree_ntfs_recursive(struct dentry **root_p,
                if (ret > 0) {
                        /*print_security_descriptor(sd, sd_size);*/
                        root->security_id = sd_set_add_sd(sd_set, sd, sd_size);
                if (ret > 0) {
                        /*print_security_descriptor(sd, sd_size);*/
                        root->security_id = sd_set_add_sd(sd_set, sd, sd_size);
+                       if (root->security_id == -1) {
+                               ERROR("Out of memory");
+                               return WIMLIB_ERR_NOMEM;
+                       }
                        DEBUG("Added security ID = %u for `%s'",
                              root->security_id, path);
                } else { 
                        DEBUG("Added security ID = %u for `%s'",
                              root->security_id, path);
                } else { 
@@ -545,9 +551,10 @@ static int build_dentry_tree_ntfs(struct dentry **root_p,
        ntfs_volume *vol;
        ntfs_inode *root_ni;
        int ret = 0;
        ntfs_volume *vol;
        ntfs_inode *root_ni;
        int ret = 0;
-       struct sd_set sd_set;
-       sd_set.sd = sd;
-       sd_set.root = NULL;
+       struct sd_set sd_set = {
+               .sd = sd,
+               .root = NULL,
+       };
        ntfs_volume **ntfs_vol_p = extra_arg;
 
        DEBUG("Mounting NTFS volume `%s' read-only", device);
        ntfs_volume **ntfs_vol_p = extra_arg;
 
        DEBUG("Mounting NTFS volume `%s' read-only", device);
@@ -559,6 +566,9 @@ static int build_dentry_tree_ntfs(struct dentry **root_p,
                return WIMLIB_ERR_NTFS_3G;
        }
 
                return WIMLIB_ERR_NTFS_3G;
        }
 
+       /* We don't want to capture the special NTFS files such as $Bitmap.  Not
+        * to be confused with "hidden" or "system" files which are real files
+        * that we do need to capture.  */
        NVolClearShowSysFiles(vol);
 
        DEBUG("Opening root NTFS dentry");
        NVolClearShowSysFiles(vol);
 
        DEBUG("Opening root NTFS dentry");
@@ -569,11 +579,15 @@ static int build_dentry_tree_ntfs(struct dentry **root_p,
                ret = WIMLIB_ERR_NTFS_3G;
                goto out;
        }
                ret = WIMLIB_ERR_NTFS_3G;
                goto out;
        }
-       char *path = MALLOC(32769);
+
+       /* Currently we assume that all the UTF-8 paths fit into this length and
+        * there is no check for overflow. */
+       char *path = MALLOC(32768);
        if (!path) {
                ERROR("Could not allocate memory for NTFS pathname");
                goto out_cleanup;
        }
        if (!path) {
                ERROR("Could not allocate memory for NTFS pathname");
                goto out_cleanup;
        }
+
        path[0] = '/';
        path[1] = '\0';
        ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
        path[0] = '/';
        path[1] = '\0';
        ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
@@ -593,6 +607,8 @@ out:
                                ret = WIMLIB_ERR_NTFS_3G;
                }
        } else {
                                ret = WIMLIB_ERR_NTFS_3G;
                }
        } else {
+               /* We need to leave the NTFS volume mounted so that we can read
+                * the NTFS files again when we are actually writing the WIM */
                *ntfs_vol_p = vol;
        }
        return ret;
                *ntfs_vol_p = vol;
        }
        return ret;