/*
- * update_image.c - Update a WIM image.
+ * update_image.c - see description below
*/
/*
* Copyright (C) 2013, 2014 Eric Biggers
*
- * This file is part of wimlib, a library for working with WIM files.
+ * This file is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option) any
+ * later version.
*
- * wimlib is free software; you can redistribute it and/or modify it under the
- * terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3 of the License, or (at your option)
- * any later version.
- *
- * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- * A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * This file is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
- * You should have received a copy of the GNU General Public License
- * along with wimlib; if not, see http://www.gnu.org/licenses/.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this file; if not, see http://www.gnu.org/licenses/.
+ */
+
+/*
+ * This file contains the implementation of wimlib_update_image(), which is one
+ * of the two ways by which library users can make changes to a WIM image. (The
+ * other way is by mounting an image read-write.) wimlib_update_image() is also
+ * used in the implementation of wimlib_add_image(), since "create a WIM image
+ * from this directory tree" is equivalent to "create an empty WIM image, then
+ * update it to add this directory tree as the root".
+ *
+ * wimlib_update_image() processes a list of commands passed to it. Currently,
+ * the following types of commands are supported:
+ *
+ * - Add a directory tree from an external source (filesystem or NTFS volume).
+ * This can be used to add new files or to replace existing files.
+ * - Delete a file or directory tree.
+ * - Rename a file or directory tree.
+ *
+ * Not supported are creating links to existing files or changing metadata of
+ * existing files.
+ *
+ * wimlib_update_image() is atomic. If it cannot complete successfully, then
+ * all changes are rolled back and the WIMStruct is left unchanged. Rollback is
+ * implemented by breaking the commands into primitive operations such as "link
+ * this dentry tree here" which can be undone by doing the opposite operations
+ * in reverse order.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+
+#include "wimlib/alloca.h"
+#include "wimlib/assert.h"
#include "wimlib/capture.h"
#include "wimlib/dentry.h"
#include "wimlib/encoding.h"
+#include "wimlib/endianness.h"
#include "wimlib/error.h"
#include "wimlib/lookup_table.h"
#include "wimlib/metadata.h"
# include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
#endif
#include "wimlib/paths.h"
+#include "wimlib/progress.h"
#include "wimlib/xml.h"
-#include <errno.h>
-#include <sys/stat.h>
-#include <stdlib.h>
-
-#ifdef HAVE_ALLOCA_H
-# include <alloca.h>
-#endif
-
/* Saved specification of a "primitive" update operation that was performed. */
struct update_primitive {
enum {
{
if (parent) {
/* Unlink @subject from its @parent. */
- wimlib_assert(subject->parent == parent);
+ wimlib_assert(subject->d_parent == parent);
unlink_dentry(subject);
} else {
/* Unset @subject as the root of the image. */
*root_p = NULL;
}
- subject->parent = subject;
+ subject->d_parent = subject;
}
static void
if (dentry_is_root(subject))
parent = NULL;
else
- parent = subject->parent;
+ parent = subject->d_parent;
prim.type = UNLINK_DENTRY;
prim.link.subject = subject;
struct wim_dentry *dentry, const tchar *new_name_tstr)
{
int ret;
- utf16lechar *new_name = NULL;
- u16 new_name_nbytes = 0;
+ utf16lechar *new_name;
+ size_t new_name_nbytes;
struct update_primitive prim;
/* Set the long name. */
- ret = get_utf16le_string(new_name_tstr, &new_name, &new_name_nbytes);
+ ret = tstr_to_utf16le(new_name_tstr,
+ tstrlen(new_name_tstr) * sizeof(tchar),
+ &new_name, &new_name_nbytes);
if (ret)
return ret;
prim.name.subject = dentry;
prim.name.old_name = dentry->file_name;
ret = record_update_primitive(j, prim);
- if (ret)
+ if (ret) {
+ FREE(new_name);
return ret;
+ }
dentry->file_name = new_name;
dentry->file_name_nbytes = new_name_nbytes;
free_update_command_journal(j);
}
-/*
- * Set the name of @branch for placing it at @target in the WIM image. This
- * assumes that @target is in "canonical form", as produced by
- * canonicalize_wim_path().
- *
- * Note: for the root target this produces the empty name.
- */
-static int
-set_branch_name(struct wim_dentry *branch, const utf16lechar *target)
-{
- const utf16lechar *p;
-
- /* Find end of string. (We can assume it contains at least one
- * character, the leading slash.) */
- wimlib_assert(target[0] == cpu_to_le16(WIM_PATH_SEPARATOR));
- p = target;
- do {
- p++;
- } while (*p);
-
- while (*(p - 1) != cpu_to_le16(WIM_PATH_SEPARATOR))
- p--;
-
-
- /* We're assuming no trailing slashes. */
- wimlib_assert(*p || p == &target[1]);
-
- return dentry_set_name_utf16le(branch, p);
-}
-
static int
handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
struct update_command_journal *j,
- int add_flags, wimlib_progress_func_t progress_func)
+ int add_flags,
+ wimlib_progress_func_t progfunc, void *progctx)
{
bool branch_is_dir = dentry_is_directory(branch);
bool existing_is_dir = dentry_is_directory(existing);
unlink_dentry(new_child);
if (existing_child) {
ret = handle_conflict(new_child, existing_child,
- j, add_flags, progress_func);
+ j, add_flags,
+ progfunc, progctx);
} else {
ret = journaled_link(j, new_child, existing);
}
return ret;
}
}
- free_dentry(branch);
+ free_dentry_tree(branch, j->lookup_table);
return 0;
} else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
/* Can't replace nondirectory file */
struct wim_dentry *parent;
int ret;
- parent = existing->parent;
+ parent = existing->d_parent;
ret = calculate_dentry_full_path(existing);
if (ret)
return ret;
- ret = journaled_unlink(j, existing);
- if (ret)
- return ret;
-
- ret = journaled_link(j, branch, parent);
- if (ret)
- return ret;
-
- if (progress_func && (add_flags & WIMLIB_ADD_FLAG_VERBOSE)) {
+ if (add_flags & WIMLIB_ADD_FLAG_VERBOSE) {
union wimlib_progress_info info;
info.replace.path_in_wim = existing->_full_path;
- progress_func(WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM, &info);
+ ret = call_progress(progfunc,
+ WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM,
+ &info, progctx);
+ if (ret)
+ return ret;
}
- return 0;
+
+ ret = journaled_unlink(j, existing);
+ if (ret)
+ return ret;
+
+ return journaled_link(j, branch, parent);
}
}
static int
-do_attach_branch(struct wim_dentry *branch, utf16lechar *target,
+do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
struct update_command_journal *j,
- int add_flags, wimlib_progress_func_t progress_func)
+ int add_flags, wimlib_progress_func_t progfunc, void *progctx)
{
struct wim_dentry *parent;
struct wim_dentry *existing;
- utf16lechar empty_name[1] = {0};
- utf16lechar *cur_component_name;
- utf16lechar *next_component_name;
+ const utf16lechar empty_name[1] = {0};
+ const utf16lechar *cur_component_name;
+ size_t cur_component_nbytes;
+ const utf16lechar *next_component_name;
int ret;
/* Attempt to create root directory before proceeding to the "real"
parent = NULL;
existing = *j->root_p;
cur_component_name = empty_name;
+ cur_component_nbytes = 0;
/* Skip leading slashes */
next_component_name = target;
next_component_name++;
while (*next_component_name) { /* While not the last component ... */
- utf16lechar *end;
+ const utf16lechar *end;
if (existing) {
/* Descend into existing directory */
* the way by creating a filler directory. */
struct wim_dentry *filler;
- ret = new_filler_directory(T(""), &filler);
+ ret = new_filler_directory(&filler);
if (ret)
return ret;
ret = dentry_set_name_utf16le(filler,
- cur_component_name);
+ cur_component_name,
+ cur_component_nbytes);
if (ret) {
free_dentry(filler);
return ret;
next_component_name = end;
if (*end) {
/* There will still be more components after this. */
- *end = 0;
do {
} while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
wimlib_assert(*next_component_name); /* No trailing slashes */
next_component_name = end;
}
parent = existing;
+ cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
existing = get_dentry_child_with_utf16le_name(
parent,
cur_component_name,
- (end - cur_component_name) * sizeof(utf16lechar),
+ cur_component_nbytes,
WIMLIB_CASE_PLATFORM_DEFAULT);
}
/* Last component */
if (existing) {
- return handle_conflict(branch, existing, j,
- add_flags, progress_func);
+ return handle_conflict(branch, existing, j, add_flags,
+ progfunc, progctx);
} else {
return journaled_link(j, branch, parent);
}
*/
static int
attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
- struct update_command_journal *j,
- int add_flags, wimlib_progress_func_t progress_func)
+ struct update_command_journal *j, int add_flags,
+ wimlib_progress_func_t progfunc, void *progctx)
{
int ret;
- utf16lechar *target;
+ const utf16lechar *target;
+ ret = 0;
if (unlikely(!branch))
- return 0;
+ goto out;
-#if TCHAR_IS_UTF16LE
- target = memdup(target_tstr,
- (tstrlen(target_tstr) + 1) * sizeof(target_tstr[0]));
- if (!target) {
- ret = WIMLIB_ERR_NOMEM;
+ ret = tstr_get_utf16le(target_tstr, &target);
+ if (ret)
goto out_free_branch;
- }
-#else
- {
- size_t target_nbytes;
- ret = tstr_to_utf16le(target_tstr,
- tstrlen(target_tstr) * sizeof(target_tstr[0]),
- &target, &target_nbytes);
- if (ret)
- goto out_free_branch;
- }
-#endif
- ret = set_branch_name(branch, target);
+ BUILD_BUG_ON(WIM_PATH_SEPARATOR != OS_PREFERRED_PATH_SEPARATOR);
+ ret = dentry_set_name(branch, path_basename(target_tstr));
if (ret)
goto out_free_target;
- ret = do_attach_branch(branch, target, j, add_flags, progress_func);
+ ret = do_attach_branch(branch, target, j, add_flags, progfunc, progctx);
if (ret)
goto out_free_target;
/* branch was successfully committed to the journal */
branch = NULL;
out_free_target:
- FREE(target);
+ tstr_put_utf16le(target);
out_free_branch:
free_dentry_tree(branch, j->lookup_table);
+out:
return ret;
}
const struct wimlib_update_command *add_cmd,
struct wim_inode_table *inode_table,
struct wim_sd_set *sd_set,
- struct list_head *unhashed_streams,
- wimlib_progress_func_t progress_func)
+ struct list_head *unhashed_streams)
{
int ret;
int add_flags;
tchar *fs_source_path;
tchar *wim_target_path;
const tchar *config_file;
- struct add_image_params params;
+ struct capture_params params;
struct capture_config config;
capture_tree_t capture_tree = platform_default_capture_tree;
#ifdef WITH_NTFS_3G
params.add_flags = add_flags;
params.extra_arg = extra_arg;
- params.progress_func = progress_func;
+ params.progfunc = wim->progfunc;
+ params.progctx = wim->progctx;
params.progress.scan.source = fs_source_path;
params.progress.scan.wim_target_path = wim_target_path;
- if (progress_func)
- progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, ¶ms.progress);
+ ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_BEGIN,
+ ¶ms.progress, params.progctx);
+ if (ret)
+ goto out_destroy_config;
if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
if (ret)
goto out_destroy_config;
- if (progress_func)
- progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, ¶ms.progress);
+ ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_END,
+ ¶ms.progress, params.progctx);
+ if (ret) {
+ free_dentry_tree(branch, wim->lookup_table);
+ goto out_cleanup_after_capture;
+ }
if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
branch && !dentry_is_directory(branch))
}
ret = attach_branch(branch, wim_target_path, j,
- add_flags, params.progress_func);
+ add_flags, params.progfunc, params.progctx);
if (ret)
goto out_cleanup_after_capture;
WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
{
params.add_flags = 0;
- params.progress_func = NULL;
+ params.progfunc = NULL;
params.config = NULL;
/* If a capture configuration file was explicitly specified when
if (ret)
goto out_cleanup_after_capture;
- ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL);
+ ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL, NULL);
if (ret)
goto out_cleanup_after_capture;
}
#ifdef WITH_NTFS_3G
wim_get_current_image_metadata(wim)->ntfs_vol = ntfs_vol;
#endif
- if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
- wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
ret = 0;
goto out_destroy_config;
out_cleanup_after_capture:
/* Is @d1 a (possibly nonproper) ancestor of @d2? */
static bool
-is_ancestor(struct wim_dentry *d1, struct wim_dentry *d2)
+is_ancestor(const struct wim_dentry *d1, const struct wim_dentry *d2)
{
for (;;) {
if (d2 == d1)
return true;
if (dentry_is_root(d2))
return false;
- d2 = d2->parent;
+ d2 = d2->d_parent;
}
}
if (dentry_has_children(dst))
return -ENOTEMPTY;
}
- parent_of_dst = dst->parent;
+ parent_of_dst = dst->d_parent;
} else {
/* Destination does not exist */
parent_of_dst = get_parent_dentry(wim, to, case_type);
execute_update_commands(WIMStruct *wim,
const struct wimlib_update_command *cmds,
size_t num_cmds,
- int update_flags,
- wimlib_progress_func_t progress_func)
+ int update_flags)
{
struct wim_inode_table *inode_table;
struct wim_sd_set *sd_set;
if (ret)
goto out;
- ret = init_sd_set(sd_set, wim_security_data(wim));
+ ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
if (ret)
goto out_destroy_inode_table;
for (size_t i = 0; i < num_cmds; i++) {
DEBUG("Executing update command %zu of %zu (op=%"TS")",
i + 1, num_cmds, update_op_to_str(cmds[i].op));
- if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
- progress_func)
- {
- info.update.command = &cmds[i];
- (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
- &info);
+ info.update.command = &cmds[i];
+ if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
+ ret = call_progress(wim->progfunc,
+ WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
+ &info, wim->progctx);
+ if (ret)
+ goto rollback;
}
- ret = WIMLIB_ERR_INVALID_PARAM;
+
switch (cmds[i].op) {
case WIMLIB_UPDATE_OP_ADD:
ret = execute_add_command(j, wim, &cmds[i], inode_table,
- sd_set, &unhashed_streams,
- progress_func);
+ sd_set, &unhashed_streams);
break;
case WIMLIB_UPDATE_OP_DELETE:
ret = execute_delete_command(j, wim, &cmds[i]);
if (unlikely(ret))
goto rollback;
info.update.completed_commands++;
- if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
- progress_func)
- {
- (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
- &info);
+ if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
+ ret = call_progress(wim->progfunc,
+ WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
+ &info, wim->progctx);
+ if (ret)
+ goto rollback;
}
next_command(j);
}
WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
WIMLIB_ADD_FLAG_WINCONFIG |
WIMLIB_ADD_FLAG_WIMBOOT |
- WIMLIB_ADD_FLAG_NO_REPLACE))
+ WIMLIB_ADD_FLAG_NO_REPLACE |
+ WIMLIB_ADD_FLAG_TEST_FILE_EXCLUSION))
return WIMLIB_ERR_INVALID_PARAM;
bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
if (!is_entire_image) {
if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
- ERROR("Cannot add directly from a NTFS volume "
+ ERROR("Cannot add directly from an NTFS volume "
"when not capturing a full image!");
return WIMLIB_ERR_INVALID_PARAM;
}
int image,
const struct wimlib_update_command *cmds,
size_t num_cmds,
- int update_flags,
- wimlib_progress_func_t progress_func)
+ int update_flags)
{
int ret;
struct wimlib_update_command *cmds_copy;
DEBUG("Updating image %d with %zu commands", image, num_cmds);
- if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_DELETE))
- ret = can_delete_from_wim(wim);
- else
- ret = can_modify_wim(wim);
-
- if (ret)
- goto out;
-
/* Load the metadata for the image to modify (if not loaded already) */
ret = select_wim_image(wim, image);
if (ret)
/* Actually execute the update commands. */
DEBUG("Executing %zu update commands", num_cmds);
- ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
- progress_func);
+ ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags);
if (ret)
goto out_free_cmds_copy;
* directories, may have changed. Call xml_update_image_info() to
* recalculate these statistics. */
xml_update_image_info(wim, image);
+
+ for (size_t i = 0; i < num_cmds; i++)
+ if (cmds_copy[i].op == WIMLIB_UPDATE_OP_ADD &&
+ cmds_copy[i].add.add_flags & WIMLIB_ADD_FLAG_RPFIX)
+ wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
out_free_cmds_copy:
free_update_commands(cmds_copy, num_cmds);
out:
return ret;
}
+
+WIMLIBAPI int
+wimlib_delete_path(WIMStruct *wim, int image,
+ const tchar *path, int delete_flags)
+{
+ struct wimlib_update_command cmd;
+
+ cmd.op = WIMLIB_UPDATE_OP_DELETE;
+ cmd.delete_.wim_path = (tchar *)path;
+ cmd.delete_.delete_flags = delete_flags;
+
+ return wimlib_update_image(wim, image, &cmd, 1, 0);
+}
+
+WIMLIBAPI int
+wimlib_rename_path(WIMStruct *wim, int image,
+ const tchar *source_path, const tchar *dest_path)
+{
+ struct wimlib_update_command cmd;
+
+ cmd.op = WIMLIB_UPDATE_OP_RENAME;
+ cmd.rename.wim_source_path = (tchar *)source_path;
+ cmd.rename.wim_target_path = (tchar *)dest_path;
+ cmd.rename.rename_flags = 0;
+
+ return wimlib_update_image(wim, image, &cmd, 1, 0);
+}
+
+WIMLIBAPI int
+wimlib_add_tree(WIMStruct *wim, int image,
+ const tchar *fs_source_path, const tchar *wim_target_path,
+ int add_flags)
+{
+ struct wimlib_update_command cmd;
+
+ cmd.op = WIMLIB_UPDATE_OP_ADD;
+ cmd.add.fs_source_path = (tchar *)fs_source_path;
+ cmd.add.wim_target_path = (tchar *)wim_target_path;
+ cmd.add.add_flags = add_flags;
+ cmd.add.config_file = NULL;
+
+ return wimlib_update_image(wim, image, &cmd, 1, 0);
+}