]> wimlib.net Git - wimlib/blobdiff - src/add_image.c
Free orphaned branch dentry
[wimlib] / src / add_image.c
index 32150a6316a7711b54ce34565f0400dc4ad7c0ff..28ece7424720a3922887504590d4506d37b6b7dd 100644 (file)
@@ -201,7 +201,7 @@ static int win32_recurse_directory(struct wim_dentry *root,
 
        {
                /* Begin reading the directory by calling FindFirstFileW.
-                * Unlink UNIX opendir(), FindFirstFileW has file globbing built
+                * Unlike UNIX opendir(), FindFirstFileW has file globbing built
                 * into it.  But this isn't what we actually want, so just add a
                 * dummy glob to get all entries. */
                wchar_t pattern_buf[path_utf16_nchars + 3];
@@ -449,7 +449,7 @@ static int win32_capture_stream(const wchar_t *path_utf16,
                 * message digest */
                lte->refcnt++;
        } else {
-               /* Make a new new wim_lookup_table_entry */
+               /* Make a new wim_lookup_table_entry */
                lte = new_lookup_table_entry();
                if (!lte) {
                        ret = WIMLIB_ERR_NOMEM;
@@ -536,6 +536,7 @@ out_find_close:
        FindClose(hFind);
        return ret;
 }
+
 #endif
 
 /*
@@ -674,11 +675,15 @@ static int build_dentry_tree(struct wim_dentry **root_ret,
        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,
@@ -847,7 +852,7 @@ static int build_dentry_tree(struct wim_dentry **root_ret,
        ret = utf8_to_utf16(root_disk_path, strlen(root_disk_path),
                            (char**)&path_utf16, &path_utf16_nchars);
        if (ret)
-               goto out;
+               goto out_destroy_sd_set;
        path_utf16_nchars /= sizeof(wchar_t);
 
        HANDLE hFile = win32_open_file_readonly(path_utf16);
@@ -939,11 +944,11 @@ static int build_dentry_tree(struct wim_dentry **root_ret,
        }
 out_close_handle:
        CloseHandle(hFile);
+out_free_path_utf16:
+       FREE(path_utf16);
 out_destroy_sd_set:
        if (extra_arg == NULL)
                destroy_sd_set(sd_set);
-out_free_path_utf16:
-       FREE(path_utf16);
 #endif
        /* The below lines of code are common to both UNIX and Win32 builds.  It
         * simply returns the captured directory tree if the capture was
@@ -1194,7 +1199,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;
@@ -1234,7 +1239,8 @@ static void canonicalize_targets(struct wimlib_capture_source *sources,
                 * backslashes to avoid confusing other parts of the library
                 * code. */
                zap_backslashes(sources->fs_source_path);
-               zap_backslashes(sources->wim_target_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);
@@ -1253,7 +1259,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)
 {
@@ -1359,6 +1365,7 @@ static int do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
                        return WIMLIB_ERR_INVALID_OVERLAY;
                }
        }
+       free_dentry(branch);
        return 0;
 
 }
@@ -1536,11 +1543,18 @@ 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;
 
+#if defined(__CYGWIN__) || defined(__WIN32__)
+               win32_acquire_privilege(SE_BACKUP_NAME);
+               win32_acquire_privilege(SE_SECURITY_NAME);
+               win32_acquire_privilege(SE_TAKE_OWNERSHIP_NAME);
+#endif
                root_dentry = NULL;
                i = 0;
                do {
@@ -1619,7 +1633,7 @@ 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);
@@ -1634,6 +1648,11 @@ out_free_security_data:
 out_destroy_capture_config:
        destroy_capture_config(&config);
 out:
+#if defined(__CYGWIN__) || defined(__WIN32__)
+       win32_release_privilege(SE_BACKUP_NAME);
+       win32_release_privilege(SE_SECURITY_NAME);
+       win32_release_privilege(SE_TAKE_OWNERSHIP_NAME);
+#endif
        return ret;
 }