]> wimlib.net Git - wimlib/blob - src/update_image.c
update_image.c: Fix call to progress function in handle_conflict()
[wimlib] / src / update_image.c
1 /*
2  * update_image.c - Update a WIM image.
3  */
4
5 /*
6  * Copyright (C) 2013, 2014 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/capture.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/encoding.h"
31 #include "wimlib/error.h"
32 #include "wimlib/lookup_table.h"
33 #include "wimlib/metadata.h"
34 #ifdef WITH_NTFS_3G
35 #  include "wimlib/ntfs_3g.h" /* for do_ntfs_umount() */
36 #endif
37 #include "wimlib/paths.h"
38 #include "wimlib/progress.h"
39 #include "wimlib/xml.h"
40
41 #include <errno.h>
42 #include <sys/stat.h>
43 #include <stdlib.h>
44
45 #ifdef HAVE_ALLOCA_H
46 #  include <alloca.h>
47 #endif
48
49 /* Saved specification of a "primitive" update operation that was performed.  */
50 struct update_primitive {
51         enum {
52                 /* Unlinked a dentry from its parent directory.  */
53                 UNLINK_DENTRY,
54
55                 /* Linked a dentry into its parent directory.  */
56                 LINK_DENTRY,
57
58                 /* Changed the file name of a dentry.  */
59                 CHANGE_FILE_NAME,
60
61                 /* Changed the short name of a dentry.  */
62                 CHANGE_SHORT_NAME,
63         } type;
64
65         union {
66                 /* For UNLINK_DENTRY and LINK_DENTRY operations  */
67                 struct {
68                         /* Dentry that was linked or unlinked.  */
69                         struct wim_dentry *subject;
70
71                         /* For link operations, the directory into which
72                          * @subject was linked, or NULL if @subject was set as
73                          * the root of the image.
74                          *
75                          * For unlink operations, the directory from which
76                          * @subject was unlinked, or NULL if @subject was unset
77                          * as the root of the image.  */
78                         struct wim_dentry *parent;
79                 } link;
80
81                 /* For CHANGE_FILE_NAME and CHANGE_SHORT_NAME operations  */
82                 struct {
83                         /* Dentry that had its name changed.  */
84                         struct wim_dentry *subject;
85
86                         /* The old name.  */
87                         utf16lechar *old_name;
88                 } name;
89         };
90 };
91
92 /* Chronological list of primitive operations that were executed for a single
93  * logical update command, such as 'add', 'delete', or 'rename'.  */
94 struct update_primitive_list {
95         struct update_primitive *entries;
96         struct update_primitive inline_entries[4];
97         size_t num_entries;
98         size_t num_alloc_entries;
99 };
100
101 /* Journal for managing the executing of zero or more logical update commands,
102  * such as 'add', 'delete', or 'rename'.  This allows either committing or
103  * rolling back the commands.  */
104 struct update_command_journal {
105         /* Number of update commands this journal contains.  */
106         size_t num_cmds;
107
108         /* Index of currently executing update command.  */
109         size_t cur_cmd;
110
111         /* Location of the WIM image's root pointer.  */
112         struct wim_dentry **root_p;
113
114         /* Pointer to the lookup table of the WIM (may needed for rollback)  */
115         struct wim_lookup_table *lookup_table;
116
117         /* List of dentries that are currently unlinked from the WIM image.
118          * These must be freed when no longer needed for commit or rollback.  */
119         struct list_head orphans;
120
121         /* Per-command logs.  */
122         struct update_primitive_list cmd_prims[];
123 };
124
125 static void
126 init_update_primitive_list(struct update_primitive_list *l)
127 {
128         l->entries = l->inline_entries;
129         l->num_entries = 0;
130         l->num_alloc_entries = ARRAY_LEN(l->inline_entries);
131 }
132
133 /* Allocates a new journal for managing the execution of up to @num_cmds update
134  * commands.  */
135 static struct update_command_journal *
136 new_update_command_journal(size_t num_cmds, struct wim_dentry **root_p,
137                            struct wim_lookup_table *lookup_table)
138 {
139         struct update_command_journal *j;
140
141         j = MALLOC(sizeof(*j) + num_cmds * sizeof(j->cmd_prims[0]));
142         if (j) {
143                 j->num_cmds = num_cmds;
144                 j->cur_cmd = 0;
145                 j->root_p = root_p;
146                 j->lookup_table = lookup_table;
147                 INIT_LIST_HEAD(&j->orphans);
148                 for (size_t i = 0; i < num_cmds; i++)
149                         init_update_primitive_list(&j->cmd_prims[i]);
150         }
151         return j;
152 }
153
154 /* Don't call this directly; use commit_update() or rollback_update() instead.
155  */
156 static void
157 free_update_command_journal(struct update_command_journal *j)
158 {
159         struct wim_dentry *orphan;
160
161         /* Free orphaned dentry trees  */
162         while (!list_empty(&j->orphans)) {
163                 orphan = list_first_entry(&j->orphans,
164                                           struct wim_dentry, tmp_list);
165                 list_del(&orphan->tmp_list);
166                 free_dentry_tree(orphan, j->lookup_table);
167         }
168
169         for (size_t i = 0; i < j->num_cmds; i++)
170                 if (j->cmd_prims[i].entries != j->cmd_prims[i].inline_entries)
171                         FREE(j->cmd_prims[i].entries);
172         FREE(j);
173 }
174
175 /* Add the entry @prim to the update command journal @j.  */
176 static int
177 record_update_primitive(struct update_command_journal *j,
178                         struct update_primitive prim)
179 {
180         struct update_primitive_list *l;
181
182         l = &j->cmd_prims[j->cur_cmd];
183
184         if (l->num_entries == l->num_alloc_entries) {
185                 struct update_primitive *new_entries;
186                 size_t new_num_alloc_entries;
187                 size_t new_size;
188
189                 new_num_alloc_entries = l->num_alloc_entries * 2;
190                 new_size = new_num_alloc_entries * sizeof(new_entries[0]);
191                 if (l->entries == l->inline_entries) {
192                         new_entries = MALLOC(new_size);
193                         if (!new_entries)
194                                 return WIMLIB_ERR_NOMEM;
195                         memcpy(new_entries, l->inline_entries,
196                                sizeof(l->inline_entries));
197                 } else {
198                         new_entries = REALLOC(l->entries, new_size);
199                         if (!new_entries)
200                                 return WIMLIB_ERR_NOMEM;
201                 }
202                 l->entries = new_entries;
203                 l->num_alloc_entries = new_num_alloc_entries;
204         }
205         l->entries[l->num_entries++] = prim;
206         return 0;
207 }
208
209 static void
210 do_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
211           struct wim_dentry **root_p)
212 {
213         if (parent) {
214                 /* Unlink @subject from its @parent.  */
215                 wimlib_assert(subject->parent == parent);
216                 unlink_dentry(subject);
217         } else {
218                 /* Unset @subject as the root of the image.  */
219                 *root_p = NULL;
220         }
221         subject->parent = subject;
222 }
223
224 static void
225 do_link(struct wim_dentry *subject, struct wim_dentry *parent,
226         struct wim_dentry **root_p)
227 {
228         if (parent) {
229                 /* Link @subject to its @parent  */
230                 struct wim_dentry *existing;
231
232                 existing = dentry_add_child(parent, subject);
233                 wimlib_assert(!existing);
234         } else {
235                 /* Set @subject as root of the image  */
236                 *root_p = subject;
237         }
238 }
239
240 /* Undo a link operation.  */
241 static void
242 rollback_link(struct wim_dentry *subject, struct wim_dentry *parent,
243               struct wim_dentry **root_p, struct list_head *orphans)
244 {
245         /* Unlink is the opposite of link  */
246         do_unlink(subject, parent, root_p);
247
248         /* @subject is now unlinked.  Add it to orphans. */
249         list_add(&subject->tmp_list, orphans);
250         subject->is_orphan = 1;
251 }
252
253 /* Undo an unlink operation.  */
254 static void
255 rollback_unlink(struct wim_dentry *subject, struct wim_dentry *parent,
256                 struct wim_dentry **root_p)
257 {
258         /* Link is the opposite of unlink  */
259         do_link(subject, parent, root_p);
260
261         /* @subject is no longer unlinked.  Delete it from orphans. */
262         list_del(&subject->tmp_list);
263         subject->is_orphan = 0;
264 }
265
266 /* Rollback a name change operation.  */
267 static void
268 rollback_name_change(utf16lechar *old_name,
269                      utf16lechar **name_ptr, u16 *name_nbytes_ptr)
270 {
271         /* Free the new name, then replace it with the old name.  */
272         FREE(*name_ptr);
273         if (old_name) {
274                 *name_ptr = old_name;
275                 *name_nbytes_ptr = utf16le_strlen(old_name);
276         } else {
277                 *name_ptr = NULL;
278                 *name_nbytes_ptr = 0;
279         }
280 }
281
282 /* Rollback a primitive update operation.  */
283 static void
284 rollback_update_primitive(const struct update_primitive *prim,
285                           struct wim_dentry **root_p,
286                           struct list_head *orphans)
287 {
288         switch (prim->type) {
289         case LINK_DENTRY:
290                 rollback_link(prim->link.subject, prim->link.parent, root_p,
291                               orphans);
292                 break;
293         case UNLINK_DENTRY:
294                 rollback_unlink(prim->link.subject, prim->link.parent, root_p);
295                 break;
296         case CHANGE_FILE_NAME:
297                 rollback_name_change(prim->name.old_name,
298                                      &prim->name.subject->file_name,
299                                      &prim->name.subject->file_name_nbytes);
300                 break;
301         case CHANGE_SHORT_NAME:
302                 rollback_name_change(prim->name.old_name,
303                                      &prim->name.subject->short_name,
304                                      &prim->name.subject->short_name_nbytes);
305                 break;
306         }
307 }
308
309 /* Rollback a logical update command  */
310 static void
311 rollback_update_command(const struct update_primitive_list *l,
312                         struct wim_dentry **root_p,
313                         struct list_head *orphans)
314 {
315         size_t i = l->num_entries;
316
317         /* Rollback each primitive operation, in reverse order.  */
318         while (i--)
319                 rollback_update_primitive(&l->entries[i], root_p, orphans);
320 }
321
322 /****************************************************************************/
323
324 /* Link @subject into the directory @parent; or, if @parent is NULL, set
325  * @subject as the root of the WIM image.
326  *
327  * This is the journaled version, so it can be rolled back.  */
328 static int
329 journaled_link(struct update_command_journal *j,
330                struct wim_dentry *subject, struct wim_dentry *parent)
331 {
332         struct update_primitive prim;
333         int ret;
334
335         prim.type = LINK_DENTRY;
336         prim.link.subject = subject;
337         prim.link.parent = parent;
338
339         ret = record_update_primitive(j, prim);
340         if (ret)
341                 return ret;
342
343         do_link(subject, parent, j->root_p);
344
345         if (subject->is_orphan) {
346                 list_del(&subject->tmp_list);
347                 subject->is_orphan = 0;
348         }
349         return 0;
350 }
351
352 /* Unlink @subject from the WIM image.
353  *
354  * This is the journaled version, so it can be rolled back.  */
355 static int
356 journaled_unlink(struct update_command_journal *j, struct wim_dentry *subject)
357 {
358         struct wim_dentry *parent;
359         struct update_primitive prim;
360         int ret;
361
362         if (dentry_is_root(subject))
363                 parent = NULL;
364         else
365                 parent = subject->parent;
366
367         prim.type = UNLINK_DENTRY;
368         prim.link.subject = subject;
369         prim.link.parent = parent;
370
371         ret = record_update_primitive(j, prim);
372         if (ret)
373                 return ret;
374
375         do_unlink(subject, parent, j->root_p);
376
377         list_add(&subject->tmp_list, &j->orphans);
378         subject->is_orphan = 1;
379         return 0;
380 }
381
382 /* Change the name of @dentry to @new_name_tstr.
383  *
384  * This is the journaled version, so it can be rolled back.  */
385 static int
386 journaled_change_name(struct update_command_journal *j,
387                       struct wim_dentry *dentry, const tchar *new_name_tstr)
388 {
389         int ret;
390         utf16lechar *new_name;
391         size_t new_name_nbytes;
392         struct update_primitive prim;
393
394         /* Set the long name.  */
395         ret = tstr_to_utf16le(new_name_tstr,
396                               tstrlen(new_name_tstr) * sizeof(tchar),
397                               &new_name, &new_name_nbytes);
398         if (ret)
399                 return ret;
400
401         prim.type = CHANGE_FILE_NAME;
402         prim.name.subject = dentry;
403         prim.name.old_name = dentry->file_name;
404         ret = record_update_primitive(j, prim);
405         if (ret) {
406                 FREE(new_name);
407                 return ret;
408         }
409
410         dentry->file_name = new_name;
411         dentry->file_name_nbytes = new_name_nbytes;
412
413         /* Clear the short name.  */
414         prim.type = CHANGE_SHORT_NAME;
415         prim.name.subject = dentry;
416         prim.name.old_name = dentry->short_name;
417         ret = record_update_primitive(j, prim);
418         if (ret)
419                 return ret;
420
421         dentry->short_name = NULL;
422         dentry->short_name_nbytes = 0;
423         return 0;
424 }
425
426 static void
427 next_command(struct update_command_journal *j)
428 {
429         j->cur_cmd++;
430 }
431
432 static void
433 commit_update(struct update_command_journal *j)
434 {
435         for (size_t i = 0; i < j->num_cmds; i++)
436         {
437                 for (size_t k = 0; k < j->cmd_prims[i].num_entries; k++)
438                 {
439                         if (j->cmd_prims[i].entries[k].type == CHANGE_FILE_NAME ||
440                             j->cmd_prims[i].entries[k].type == CHANGE_SHORT_NAME)
441                         {
442                                 FREE(j->cmd_prims[i].entries[k].name.old_name);
443                         }
444                 }
445         }
446         free_update_command_journal(j);
447 }
448
449 static void
450 rollback_update(struct update_command_journal *j)
451 {
452         /* Rollback each logical update command, in reverse order.  */
453         size_t i = j->cur_cmd;
454         if (i < j->num_cmds)
455                 i++;
456         while (i--)
457                 rollback_update_command(&j->cmd_prims[i], j->root_p, &j->orphans);
458         free_update_command_journal(j);
459 }
460
461 static int
462 handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
463                 struct update_command_journal *j,
464                 int add_flags,
465                 wimlib_progress_func_t progfunc, void *progctx)
466 {
467         bool branch_is_dir = dentry_is_directory(branch);
468         bool existing_is_dir = dentry_is_directory(existing);
469
470         if (branch_is_dir != existing_is_dir) {
471                 if (existing_is_dir)  {
472                         ERROR("\"%"TS"\" is a directory!\n"
473                               "        Specify the path at which "
474                               "to place the file inside this directory.",
475                               dentry_full_path(existing));
476                         return WIMLIB_ERR_IS_DIRECTORY;
477                 } else {
478                         ERROR("Can't place directory at \"%"TS"\" because "
479                               "a nondirectory file already exists there!",
480                               dentry_full_path(existing));
481                         return WIMLIB_ERR_NOTDIR;
482                 }
483         }
484
485         if (branch_is_dir) {
486                 /* Directory overlay  */
487                 while (dentry_has_children(branch)) {
488                         struct wim_dentry *new_child;
489                         struct wim_dentry *existing_child;
490                         int ret;
491
492                         new_child = dentry_any_child(branch);
493
494                         existing_child =
495                                 get_dentry_child_with_utf16le_name(existing,
496                                                                    new_child->file_name,
497                                                                    new_child->file_name_nbytes,
498                                                                    WIMLIB_CASE_PLATFORM_DEFAULT);
499                         unlink_dentry(new_child);
500                         if (existing_child) {
501                                 ret = handle_conflict(new_child, existing_child,
502                                                       j, add_flags,
503                                                       progfunc, progctx);
504                         } else {
505                                 ret = journaled_link(j, new_child, existing);
506                         }
507                         if (ret) {
508                                 dentry_add_child(branch, new_child);
509                                 return ret;
510                         }
511                 }
512                 free_dentry(branch);
513                 return 0;
514         } else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
515                 /* Can't replace nondirectory file  */
516                 ERROR("Refusing to overwrite nondirectory file \"%"TS"\"",
517                       dentry_full_path(existing));
518                 return WIMLIB_ERR_INVALID_OVERLAY;
519         } else {
520                 /* Replace nondirectory file  */
521                 struct wim_dentry *parent;
522                 int ret;
523
524                 parent = existing->parent;
525
526                 ret = calculate_dentry_full_path(existing);
527                 if (ret)
528                         return ret;
529
530                 if (add_flags & WIMLIB_ADD_FLAG_VERBOSE) {
531                         union wimlib_progress_info info;
532
533                         info.replace.path_in_wim = existing->_full_path;
534                         ret = call_progress(progfunc,
535                                             WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM,
536                                             &info, progctx);
537                         if (ret)
538                                 return ret;
539                 }
540
541                 ret = journaled_unlink(j, existing);
542                 if (ret)
543                         return ret;
544
545                 return journaled_link(j, branch, parent);
546         }
547 }
548
549 static int
550 do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
551                  struct update_command_journal *j,
552                  int add_flags, wimlib_progress_func_t progfunc, void *progctx)
553 {
554         struct wim_dentry *parent;
555         struct wim_dentry *existing;
556         const utf16lechar empty_name[1] = {0};
557         const utf16lechar *cur_component_name;
558         size_t cur_component_nbytes;
559         const utf16lechar *next_component_name;
560         int ret;
561
562         /* Attempt to create root directory before proceeding to the "real"
563          * first component  */
564         parent = NULL;
565         existing = *j->root_p;
566         cur_component_name = empty_name;
567         cur_component_nbytes = 0;
568
569         /* Skip leading slashes  */
570         next_component_name = target;
571         while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
572                 next_component_name++;
573
574         while (*next_component_name) { /* While not the last component ... */
575                 const utf16lechar *end;
576
577                 if (existing) {
578                         /* Descend into existing directory  */
579                         if (!dentry_is_directory(existing)) {
580                                 ERROR("\"%"TS"\" in the WIM image "
581                                       "is not a directory!",
582                                       dentry_full_path(existing));
583                                 return WIMLIB_ERR_NOTDIR;
584                         }
585                 } else {
586                         /* A parent directory of the target didn't exist.  Make
587                          * the way by creating a filler directory.  */
588                         struct wim_dentry *filler;
589
590                         ret = new_filler_directory(&filler);
591                         if (ret)
592                                 return ret;
593                         ret = dentry_set_name_utf16le(filler,
594                                                       cur_component_name,
595                                                       cur_component_nbytes);
596                         if (ret) {
597                                 free_dentry(filler);
598                                 return ret;
599                         }
600                         ret = journaled_link(j, filler, parent);
601                         if (ret) {
602                                 free_dentry(filler);
603                                 return ret;
604                         }
605                         existing = filler;
606                 }
607
608                 /* Advance to next component  */
609
610                 cur_component_name = next_component_name;
611                 end = cur_component_name + 1;
612                 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
613                         end++;
614
615                 next_component_name = end;
616                 if (*end) {
617                         /* There will still be more components after this.  */
618                         do {
619                         } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
620                         wimlib_assert(*next_component_name);  /* No trailing slashes  */
621                 } else {
622                         /* This will be the last component  */
623                         next_component_name = end;
624                 }
625                 parent = existing;
626                 cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
627                 existing = get_dentry_child_with_utf16le_name(
628                                         parent,
629                                         cur_component_name,
630                                         cur_component_nbytes,
631                                         WIMLIB_CASE_PLATFORM_DEFAULT);
632         }
633
634         /* Last component  */
635         if (existing) {
636                 return handle_conflict(branch, existing, j, add_flags,
637                                        progfunc, progctx);
638         } else {
639                 return journaled_link(j, branch, parent);
640         }
641 }
642
643 /*
644  * Place the directory entry tree @branch at the path @target_tstr in the WIM
645  * image.
646  *
647  * @target_tstr cannot contain trailing slashes, and all path separators must be
648  * WIM_PATH_SEPARATOR.
649  *
650  * On success, @branch is committed to the journal @j.
651  * Otherwise @branch is freed.
652  *
653  * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
654  * WIMLIB_ADD_FLAG_VERBOSE.
655  */
656 static int
657 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
658               struct update_command_journal *j, int add_flags,
659               wimlib_progress_func_t progfunc, void *progctx)
660 {
661         int ret;
662         const utf16lechar *target;
663
664         ret = 0;
665         if (unlikely(!branch))
666                 goto out;
667
668         ret = tstr_get_utf16le(target_tstr, &target);
669         if (ret)
670                 goto out_free_branch;
671
672         BUILD_BUG_ON(WIM_PATH_SEPARATOR != OS_PREFERRED_PATH_SEPARATOR);
673         ret = dentry_set_name(branch, path_basename(target_tstr));
674         if (ret)
675                 goto out_free_target;
676
677         ret = do_attach_branch(branch, target, j, add_flags, progfunc, progctx);
678         if (ret)
679                 goto out_free_target;
680         /* branch was successfully committed to the journal  */
681         branch = NULL;
682 out_free_target:
683         tstr_put_utf16le(target);
684 out_free_branch:
685         free_dentry_tree(branch, j->lookup_table);
686 out:
687         return ret;
688 }
689
690 static const char wincfg[] =
691 "[ExclusionList]\n"
692 "/$ntfs.log\n"
693 "/hiberfil.sys\n"
694 "/pagefile.sys\n"
695 "/swapfile.sys\n"
696 "/System Volume Information\n"
697 "/RECYCLER\n"
698 "/Windows/CSC\n";
699
700 static const tchar *wimboot_cfgfile =
701             WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
702             WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
703             WIMLIB_WIM_PATH_SEPARATOR_STRING T("WimBootCompress.ini");
704
705 static int
706 get_capture_config(const tchar *config_file, struct capture_config *config,
707                    int add_flags, const tchar *fs_source_path)
708 {
709         int ret;
710         tchar *tmp_config_file = NULL;
711
712         memset(config, 0, sizeof(*config));
713
714         /* For WIMBoot capture, check for default capture configuration file
715          * unless one was explicitly specified.  */
716         if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
717
718                 /* XXX: Handle loading file correctly when in NTFS volume.  */
719
720                 size_t len = tstrlen(fs_source_path) +
721                              tstrlen(wimboot_cfgfile);
722                 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
723                 struct stat st;
724
725                 tsprintf(tmp_config_file, T("%"TS"%"TS),
726                          fs_source_path, wimboot_cfgfile);
727                 if (!tstat(tmp_config_file, &st)) {
728                         config_file = tmp_config_file;
729                         add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
730                 } else {
731                         WARNING("\"%"TS"\" does not exist.\n"
732                                 "          Using default capture configuration!",
733                                 tmp_config_file);
734                 }
735         }
736
737         if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
738                 /* Use Windows default.  */
739                 if (config_file)
740                         return WIMLIB_ERR_INVALID_PARAM;
741                 ret = read_capture_config(T("wincfg"), wincfg,
742                                           sizeof(wincfg) - 1, config);
743         } else if (config_file) {
744                 /* Use the specified configuration file.  */
745                 ret = read_capture_config(config_file, NULL, 0, config);
746         } else {
747                 /* ... Or don't use any configuration file at all.  No files
748                  * will be excluded from capture, all files will be compressed,
749                  * etc.  */
750                 ret = 0;
751         }
752         FREE(tmp_config_file);
753         return ret;
754 }
755
756 static int
757 execute_add_command(struct update_command_journal *j,
758                     WIMStruct *wim,
759                     const struct wimlib_update_command *add_cmd,
760                     struct wim_inode_table *inode_table,
761                     struct wim_sd_set *sd_set,
762                     struct list_head *unhashed_streams)
763 {
764         int ret;
765         int add_flags;
766         tchar *fs_source_path;
767         tchar *wim_target_path;
768         const tchar *config_file;
769         struct add_image_params params;
770         struct capture_config config;
771         capture_tree_t capture_tree = platform_default_capture_tree;
772 #ifdef WITH_NTFS_3G
773         struct _ntfs_volume *ntfs_vol = NULL;
774 #endif
775         void *extra_arg = NULL;
776         struct wim_dentry *branch;
777
778         add_flags = add_cmd->add.add_flags;
779         fs_source_path = add_cmd->add.fs_source_path;
780         wim_target_path = add_cmd->add.wim_target_path;
781         config_file = add_cmd->add.config_file;
782
783         DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
784               fs_source_path, wim_target_path, add_flags);
785
786         memset(&params, 0, sizeof(params));
787
788 #ifdef WITH_NTFS_3G
789         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
790                 capture_tree = build_dentry_tree_ntfs;
791                 extra_arg = &ntfs_vol;
792                 if (wim_get_current_image_metadata(wim)->ntfs_vol != NULL) {
793                         ERROR("NTFS volume already set");
794                         ret = WIMLIB_ERR_INVALID_PARAM;
795                         goto out;
796                 }
797         }
798 #endif
799
800         ret = get_capture_config(config_file, &config,
801                                  add_flags, fs_source_path);
802         if (ret)
803                 goto out;
804
805         params.lookup_table = wim->lookup_table;
806         params.unhashed_streams = unhashed_streams;
807         params.inode_table = inode_table;
808         params.sd_set = sd_set;
809         params.config = &config;
810         params.add_flags = add_flags;
811         params.extra_arg = extra_arg;
812
813         params.progfunc = wim->progfunc;
814         params.progctx = wim->progctx;
815         params.progress.scan.source = fs_source_path;
816         params.progress.scan.wim_target_path = wim_target_path;
817         ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_BEGIN,
818                             &params.progress, params.progctx);
819         if (ret)
820                 goto out_destroy_config;
821
822         if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
823                 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
824         ret = (*capture_tree)(&branch, fs_source_path, &params);
825         if (ret)
826                 goto out_destroy_config;
827
828         ret = call_progress(params.progfunc, WIMLIB_PROGRESS_MSG_SCAN_END,
829                             &params.progress, params.progctx);
830         if (ret) {
831                 free_dentry_tree(branch, wim->lookup_table);
832                 goto out_cleanup_after_capture;
833         }
834
835         if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
836             branch && !dentry_is_directory(branch))
837         {
838                 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
839                 ret = WIMLIB_ERR_NOTDIR;
840                 free_dentry_tree(branch, wim->lookup_table);
841                 goto out_cleanup_after_capture;
842         }
843
844         ret = attach_branch(branch, wim_target_path, j,
845                             add_flags, params.progfunc, params.progctx);
846         if (ret)
847                 goto out_cleanup_after_capture;
848
849         if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
850             WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
851         {
852                 params.add_flags = 0;
853                 params.progfunc = NULL;
854                 params.config = NULL;
855
856                 /* If a capture configuration file was explicitly specified when
857                  * capturing an image in WIMBoot mode, save it as
858                  * /Windows/System32/WimBootCompress.ini in the WIM image. */
859                 ret = platform_default_capture_tree(&branch, config_file, &params);
860                 if (ret)
861                         goto out_cleanup_after_capture;
862
863                 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL, NULL);
864                 if (ret)
865                         goto out_cleanup_after_capture;
866         }
867
868 #ifdef WITH_NTFS_3G
869         wim_get_current_image_metadata(wim)->ntfs_vol = ntfs_vol;
870 #endif
871         if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
872                 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
873         ret = 0;
874         goto out_destroy_config;
875 out_cleanup_after_capture:
876 #ifdef WITH_NTFS_3G
877         if (ntfs_vol)
878                 do_ntfs_umount(ntfs_vol);
879 #endif
880 out_destroy_config:
881         destroy_capture_config(&config);
882 out:
883         return ret;
884 }
885
886 static int
887 execute_delete_command(struct update_command_journal *j,
888                        WIMStruct *wim,
889                        const struct wimlib_update_command *delete_cmd)
890 {
891         int flags;
892         const tchar *wim_path;
893         struct wim_dentry *tree;
894
895         flags = delete_cmd->delete_.delete_flags;
896         wim_path = delete_cmd->delete_.wim_path;
897
898         DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
899
900         tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
901         if (!tree) {
902                 /* Path to delete does not exist in the WIM. */
903                 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
904                         return 0;
905                 } else {
906                         ERROR("Path \"%"TS"\" does not exist in WIM image %d",
907                               wim_path, wim->current_image);
908                         return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
909                 }
910         }
911
912         if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
913                 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
914                       "but a recursive delete was not requested",
915                       wim_path, wim->current_image);
916                 return WIMLIB_ERR_IS_DIRECTORY;
917         }
918
919         return journaled_unlink(j, tree);
920 }
921
922 static int
923 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
924 {
925         FREE(dentry->_full_path);
926         dentry->_full_path = NULL;
927         return 0;
928 }
929
930 /* Is @d1 a (possibly nonproper) ancestor of @d2?  */
931 static bool
932 is_ancestor(const struct wim_dentry *d1, const struct wim_dentry *d2)
933 {
934         for (;;) {
935                 if (d2 == d1)
936                         return true;
937                 if (dentry_is_root(d2))
938                         return false;
939                 d2 = d2->parent;
940         }
941 }
942
943 /* Rename a file or directory in the WIM.
944  *
945  * This returns a -errno value.
946  *
947  * The journal @j is optional.
948  */
949 int
950 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
951                 CASE_SENSITIVITY_TYPE case_type,
952                 struct update_command_journal *j)
953 {
954         struct wim_dentry *src;
955         struct wim_dentry *dst;
956         struct wim_dentry *parent_of_dst;
957         int ret;
958
959         /* This rename() implementation currently only supports actual files
960          * (not alternate data streams) */
961
962         src = get_dentry(wim, from, case_type);
963         if (!src)
964                 return -errno;
965
966         dst = get_dentry(wim, to, case_type);
967
968         if (dst) {
969                 /* Destination file exists */
970
971                 if (src == dst) /* Same file */
972                         return 0;
973
974                 if (!dentry_is_directory(src)) {
975                         /* Cannot rename non-directory to directory. */
976                         if (dentry_is_directory(dst))
977                                 return -EISDIR;
978                 } else {
979                         /* Cannot rename directory to a non-directory or a non-empty
980                          * directory */
981                         if (!dentry_is_directory(dst))
982                                 return -ENOTDIR;
983                         if (dentry_has_children(dst))
984                                 return -ENOTEMPTY;
985                 }
986                 parent_of_dst = dst->parent;
987         } else {
988                 /* Destination does not exist */
989                 parent_of_dst = get_parent_dentry(wim, to, case_type);
990                 if (!parent_of_dst)
991                         return -errno;
992
993                 if (!dentry_is_directory(parent_of_dst))
994                         return -ENOTDIR;
995         }
996
997         /* @src can't be an ancestor of @dst.  Otherwise we're unlinking @src
998          * from the tree and creating a loop...  */
999         if (is_ancestor(src, parent_of_dst))
1000                 return -EBUSY;
1001
1002         if (j) {
1003                 if (dst)
1004                         if (journaled_unlink(j, dst))
1005                                 return -ENOMEM;
1006                 if (journaled_unlink(j, src))
1007                         return -ENOMEM;
1008                 if (journaled_change_name(j, src, path_basename(to)))
1009                         return -ENOMEM;
1010                 if (journaled_link(j, src, parent_of_dst))
1011                         return -ENOMEM;
1012         } else {
1013                 ret = dentry_set_name(src, path_basename(to));
1014                 if (ret)
1015                         return -ENOMEM;
1016                 if (dst) {
1017                         unlink_dentry(dst);
1018                         free_dentry_tree(dst, wim->lookup_table);
1019                 }
1020                 unlink_dentry(src);
1021                 dentry_add_child(parent_of_dst, src);
1022         }
1023         if (src->_full_path)
1024                 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1025         return 0;
1026 }
1027
1028
1029 static int
1030 execute_rename_command(struct update_command_journal *j,
1031                        WIMStruct *wim,
1032                        const struct wimlib_update_command *rename_cmd)
1033 {
1034         int ret;
1035
1036         ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1037                               rename_cmd->rename.wim_target_path,
1038                               WIMLIB_CASE_PLATFORM_DEFAULT, j);
1039         if (ret) {
1040                 ret = -ret;
1041                 errno = ret;
1042                 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1043                                  rename_cmd->rename.wim_source_path,
1044                                  rename_cmd->rename.wim_target_path);
1045                 switch (ret) {
1046                 case ENOMEM:
1047                         ret = WIMLIB_ERR_NOMEM;
1048                         break;
1049                 case ENOTDIR:
1050                         ret = WIMLIB_ERR_NOTDIR;
1051                         break;
1052                 case ENOTEMPTY:
1053                 case EBUSY:
1054                         /* XXX: EBUSY is returned when the rename would create a
1055                          * loop.  It maybe should have its own error code.  */
1056                         ret = WIMLIB_ERR_NOTEMPTY;
1057                         break;
1058                 case EISDIR:
1059                         ret = WIMLIB_ERR_IS_DIRECTORY;
1060                         break;
1061                 case ENOENT:
1062                 default:
1063                         ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1064                         break;
1065                 }
1066         }
1067         return ret;
1068 }
1069
1070 static inline const tchar *
1071 update_op_to_str(int op)
1072 {
1073         switch (op) {
1074         case WIMLIB_UPDATE_OP_ADD:
1075                 return T("add");
1076         case WIMLIB_UPDATE_OP_DELETE:
1077                 return T("delete");
1078         case WIMLIB_UPDATE_OP_RENAME:
1079                 return T("rename");
1080         default:
1081                 wimlib_assert(0);
1082                 return NULL;
1083         }
1084 }
1085
1086 static bool
1087 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1088                   enum wimlib_update_op op)
1089 {
1090         for (size_t i = 0; i < num_cmds; i++)
1091                 if (cmds[i].op == op)
1092                         return true;
1093         return false;
1094 }
1095
1096 static int
1097 execute_update_commands(WIMStruct *wim,
1098                         const struct wimlib_update_command *cmds,
1099                         size_t num_cmds,
1100                         int update_flags)
1101 {
1102         struct wim_inode_table *inode_table;
1103         struct wim_sd_set *sd_set;
1104         struct list_head unhashed_streams;
1105         struct update_command_journal *j;
1106         union wimlib_progress_info info;
1107         int ret;
1108
1109         if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1110                 /* If we have at least one "add" command, create the inode and
1111                  * security descriptor tables to index new inodes and new
1112                  * security descriptors, respectively.  */
1113                 inode_table = alloca(sizeof(struct wim_inode_table));
1114                 sd_set = alloca(sizeof(struct wim_sd_set));
1115
1116                 ret = init_inode_table(inode_table, 9001);
1117                 if (ret)
1118                         goto out;
1119
1120                 ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
1121                 if (ret)
1122                         goto out_destroy_inode_table;
1123
1124                 INIT_LIST_HEAD(&unhashed_streams);
1125         } else {
1126                 inode_table = NULL;
1127                 sd_set = NULL;
1128         }
1129
1130         /* Start an in-memory journal to allow rollback if something goes wrong
1131          */
1132         j = new_update_command_journal(num_cmds,
1133                                        &wim_get_current_image_metadata(wim)->root_dentry,
1134                                        wim->lookup_table);
1135         if (!j) {
1136                 ret = WIMLIB_ERR_NOMEM;
1137                 goto out_destroy_sd_set;
1138         }
1139
1140         info.update.completed_commands = 0;
1141         info.update.total_commands = num_cmds;
1142         ret = 0;
1143         for (size_t i = 0; i < num_cmds; i++) {
1144                 DEBUG("Executing update command %zu of %zu (op=%"TS")",
1145                       i + 1, num_cmds, update_op_to_str(cmds[i].op));
1146                 info.update.command = &cmds[i];
1147                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1148                         ret = call_progress(wim->progfunc,
1149                                             WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1150                                             &info, wim->progctx);
1151                         if (ret)
1152                                 goto rollback;
1153                 }
1154
1155                 ret = WIMLIB_ERR_INVALID_PARAM;
1156                 switch (cmds[i].op) {
1157                 case WIMLIB_UPDATE_OP_ADD:
1158                         ret = execute_add_command(j, wim, &cmds[i], inode_table,
1159                                                   sd_set, &unhashed_streams);
1160                         break;
1161                 case WIMLIB_UPDATE_OP_DELETE:
1162                         ret = execute_delete_command(j, wim, &cmds[i]);
1163                         break;
1164                 case WIMLIB_UPDATE_OP_RENAME:
1165                         ret = execute_rename_command(j, wim, &cmds[i]);
1166                         break;
1167                 }
1168                 if (unlikely(ret))
1169                         goto rollback;
1170                 info.update.completed_commands++;
1171                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS) {
1172                         ret = call_progress(wim->progfunc,
1173                                             WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1174                                             &info, wim->progctx);
1175                         if (ret)
1176                                 goto rollback;
1177                 }
1178                 next_command(j);
1179         }
1180
1181         commit_update(j);
1182         if (inode_table) {
1183                 struct wim_image_metadata *imd;
1184
1185                 imd = wim_get_current_image_metadata(wim);
1186
1187                 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
1188                 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1189         }
1190         goto out_destroy_sd_set;
1191
1192 rollback:
1193         if (sd_set)
1194                 rollback_new_security_descriptors(sd_set);
1195         rollback_update(j);
1196 out_destroy_sd_set:
1197         if (sd_set)
1198                 destroy_sd_set(sd_set);
1199 out_destroy_inode_table:
1200         if (inode_table)
1201                 destroy_inode_table(inode_table);
1202 out:
1203         return ret;
1204 }
1205
1206
1207 static int
1208 check_add_command(struct wimlib_update_command *cmd,
1209                   const struct wim_header *hdr)
1210 {
1211         int add_flags = cmd->add.add_flags;
1212
1213         if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1214                           WIMLIB_ADD_FLAG_DEREFERENCE |
1215                           WIMLIB_ADD_FLAG_VERBOSE |
1216                           /* BOOT doesn't make sense for wimlib_update_image().  */
1217                           /*WIMLIB_ADD_FLAG_BOOT |*/
1218                           WIMLIB_ADD_FLAG_UNIX_DATA |
1219                           WIMLIB_ADD_FLAG_NO_ACLS |
1220                           WIMLIB_ADD_FLAG_STRICT_ACLS |
1221                           WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1222                           WIMLIB_ADD_FLAG_RPFIX |
1223                           WIMLIB_ADD_FLAG_NORPFIX |
1224                           WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1225                           WIMLIB_ADD_FLAG_WINCONFIG |
1226                           WIMLIB_ADD_FLAG_WIMBOOT |
1227                           WIMLIB_ADD_FLAG_NO_REPLACE))
1228                 return WIMLIB_ERR_INVALID_PARAM;
1229
1230         bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
1231
1232 #ifndef WITH_NTFS_3G
1233         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1234                 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1235                       "        we cannot capture a WIM image directly "
1236                       "from an NTFS volume");
1237                 return WIMLIB_ERR_UNSUPPORTED;
1238         }
1239 #endif
1240
1241 #ifdef __WIN32__
1242         /* Check for flags not supported on Windows */
1243         if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1244                 ERROR("Capturing UNIX-specific data is not supported on Windows");
1245                 return WIMLIB_ERR_UNSUPPORTED;
1246         }
1247         if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1248                 ERROR("Dereferencing symbolic links is not supported on Windows");
1249                 return WIMLIB_ERR_UNSUPPORTED;
1250         }
1251 #endif
1252
1253         /* VERBOSE implies EXCLUDE_VERBOSE */
1254         if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1255                 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1256
1257         /* Check for contradictory reparse point fixup flags */
1258         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1259                           WIMLIB_ADD_FLAG_NORPFIX)) ==
1260                 (WIMLIB_ADD_FLAG_RPFIX |
1261                  WIMLIB_ADD_FLAG_NORPFIX))
1262         {
1263                 ERROR("Cannot specify RPFIX and NORPFIX flags "
1264                       "at the same time!");
1265                 return WIMLIB_ERR_INVALID_PARAM;
1266         }
1267
1268         /* Set default behavior on reparse point fixups if requested */
1269         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1270                           WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1271         {
1272                 /* Do reparse-point fixups by default if we are capturing an
1273                  * entire image and either the header flag is set from previous
1274                  * images, or if this is the first image being added. */
1275                 if (is_entire_image &&
1276                     ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1277                         add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1278         }
1279
1280         if (!is_entire_image) {
1281                 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1282                         ERROR("Cannot add directly from an NTFS volume "
1283                               "when not capturing a full image!");
1284                         return WIMLIB_ERR_INVALID_PARAM;
1285                 }
1286
1287                 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1288                         ERROR("Cannot do reparse point fixups when "
1289                               "not capturing a full image!");
1290                         return WIMLIB_ERR_INVALID_PARAM;
1291                 }
1292         }
1293         /* We may have modified the add flags. */
1294         cmd->add.add_flags = add_flags;
1295         return 0;
1296 }
1297
1298 static int
1299 check_delete_command(const struct wimlib_update_command *cmd)
1300 {
1301         if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1302                                           WIMLIB_DELETE_FLAG_RECURSIVE))
1303                 return WIMLIB_ERR_INVALID_PARAM;
1304         return 0;
1305 }
1306
1307 static int
1308 check_rename_command(const struct wimlib_update_command *cmd)
1309 {
1310         if (cmd->rename.rename_flags != 0)
1311                 return WIMLIB_ERR_INVALID_PARAM;
1312         return 0;
1313 }
1314
1315 static int
1316 check_update_command(struct wimlib_update_command *cmd,
1317                      const struct wim_header *hdr)
1318 {
1319         switch (cmd->op) {
1320         case WIMLIB_UPDATE_OP_ADD:
1321                 return check_add_command(cmd, hdr);
1322         case WIMLIB_UPDATE_OP_DELETE:
1323                 return check_delete_command(cmd);
1324         case WIMLIB_UPDATE_OP_RENAME:
1325                 return check_rename_command(cmd);
1326         }
1327         return 0;
1328 }
1329
1330 static int
1331 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1332                       const struct wim_header *hdr)
1333 {
1334         int ret = 0;
1335         for (size_t i = 0; i < num_cmds; i++) {
1336                 ret = check_update_command(&cmds[i], hdr);
1337                 if (ret)
1338                         break;
1339         }
1340         return ret;
1341 }
1342
1343
1344 static void
1345 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1346 {
1347         if (cmds) {
1348                 for (size_t i = 0; i < num_cmds; i++) {
1349                         switch (cmds[i].op) {
1350                         case WIMLIB_UPDATE_OP_ADD:
1351                                 FREE(cmds[i].add.wim_target_path);
1352                                 break;
1353                         case WIMLIB_UPDATE_OP_DELETE:
1354                                 FREE(cmds[i].delete_.wim_path);
1355                                 break;
1356                         case WIMLIB_UPDATE_OP_RENAME:
1357                                 FREE(cmds[i].rename.wim_source_path);
1358                                 FREE(cmds[i].rename.wim_target_path);
1359                                 break;
1360                         }
1361                 }
1362                 FREE(cmds);
1363         }
1364 }
1365
1366 static int
1367 copy_update_commands(const struct wimlib_update_command *cmds,
1368                      size_t num_cmds,
1369                      struct wimlib_update_command **cmds_copy_ret)
1370 {
1371         int ret;
1372         struct wimlib_update_command *cmds_copy;
1373
1374         cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1375         if (!cmds_copy)
1376                 goto oom;
1377
1378         for (size_t i = 0; i < num_cmds; i++) {
1379                 cmds_copy[i].op = cmds[i].op;
1380                 switch (cmds[i].op) {
1381                 case WIMLIB_UPDATE_OP_ADD:
1382                         cmds_copy[i].add.fs_source_path = cmds[i].add.fs_source_path;
1383                         cmds_copy[i].add.wim_target_path =
1384                                 canonicalize_wim_path(cmds[i].add.wim_target_path);
1385                         if (!cmds_copy[i].add.wim_target_path)
1386                                 goto oom;
1387                         cmds_copy[i].add.config_file = cmds[i].add.config_file;
1388                         cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1389                         break;
1390                 case WIMLIB_UPDATE_OP_DELETE:
1391                         cmds_copy[i].delete_.wim_path =
1392                                 canonicalize_wim_path(cmds[i].delete_.wim_path);
1393                         if (!cmds_copy[i].delete_.wim_path)
1394                                 goto oom;
1395                         cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1396                         break;
1397                 case WIMLIB_UPDATE_OP_RENAME:
1398                         cmds_copy[i].rename.wim_source_path =
1399                                 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1400                         cmds_copy[i].rename.wim_target_path =
1401                                 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1402                         if (!cmds_copy[i].rename.wim_source_path ||
1403                             !cmds_copy[i].rename.wim_target_path)
1404                                 goto oom;
1405                         break;
1406                 default:
1407                         ERROR("Unknown update operation %u", cmds[i].op);
1408                         ret = WIMLIB_ERR_INVALID_PARAM;
1409                         goto err;
1410                 }
1411         }
1412         *cmds_copy_ret = cmds_copy;
1413         ret = 0;
1414 out:
1415         return ret;
1416 oom:
1417         ret = WIMLIB_ERR_NOMEM;
1418 err:
1419         free_update_commands(cmds_copy, num_cmds);
1420         goto out;
1421 }
1422
1423 /* API function documented in wimlib.h  */
1424 WIMLIBAPI int
1425 wimlib_update_image(WIMStruct *wim,
1426                     int image,
1427                     const struct wimlib_update_command *cmds,
1428                     size_t num_cmds,
1429                     int update_flags)
1430 {
1431         int ret;
1432         struct wimlib_update_command *cmds_copy;
1433
1434         if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1435                 return WIMLIB_ERR_INVALID_PARAM;
1436
1437         DEBUG("Updating image %d with %zu commands", image, num_cmds);
1438
1439         if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_DELETE))
1440                 ret = can_delete_from_wim(wim);
1441         else
1442                 ret = can_modify_wim(wim);
1443
1444         if (ret)
1445                 goto out;
1446
1447         /* Load the metadata for the image to modify (if not loaded already) */
1448         ret = select_wim_image(wim, image);
1449         if (ret)
1450                 goto out;
1451
1452         DEBUG("Preparing %zu update commands", num_cmds);
1453
1454         /* Make a copy of the update commands, in the process doing certain
1455          * canonicalizations on paths (e.g. translating backslashes to forward
1456          * slashes).  This is done to avoid modifying the caller's copy of the
1457          * commands. */
1458         ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1459         if (ret)
1460                 goto out;
1461
1462         /* Perform additional checks on the update commands before we execute
1463          * them. */
1464         ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1465         if (ret)
1466                 goto out_free_cmds_copy;
1467
1468         /* Actually execute the update commands. */
1469         DEBUG("Executing %zu update commands", num_cmds);
1470         ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags);
1471         if (ret)
1472                 goto out_free_cmds_copy;
1473
1474         wim->image_metadata[image - 1]->modified = 1;
1475
1476         /* Statistics about the WIM image, such as the numbers of files and
1477          * directories, may have changed.  Call xml_update_image_info() to
1478          * recalculate these statistics. */
1479         xml_update_image_info(wim, image);
1480 out_free_cmds_copy:
1481         free_update_commands(cmds_copy, num_cmds);
1482 out:
1483         return ret;
1484 }
1485
1486 static int
1487 update1(WIMStruct *wim, int image, const struct wimlib_update_command *cmd)
1488 {
1489         return wimlib_update_image(wim, image, cmd, 1, 0);
1490 }
1491
1492 WIMLIBAPI int
1493 wimlib_delete_path(WIMStruct *wim, int image,
1494                    const tchar *path, int delete_flags)
1495 {
1496         struct wimlib_update_command cmd;
1497
1498         cmd.op = WIMLIB_UPDATE_OP_DELETE;
1499         cmd.delete_.wim_path = (tchar *)path;
1500         cmd.delete_.delete_flags = delete_flags;
1501
1502         return update1(wim, image, &cmd);
1503 }
1504
1505 WIMLIBAPI int
1506 wimlib_rename_path(WIMStruct *wim, int image,
1507                    const tchar *source_path, const tchar *dest_path)
1508 {
1509         struct wimlib_update_command cmd;
1510
1511         cmd.op = WIMLIB_UPDATE_OP_RENAME;
1512         cmd.rename.wim_source_path = (tchar *)source_path;
1513         cmd.rename.wim_target_path = (tchar *)dest_path;
1514         cmd.rename.rename_flags = 0;
1515
1516         return update1(wim, image, &cmd);
1517 }
1518
1519 WIMLIBAPI int
1520 wimlib_add_tree(WIMStruct *wim, int image,
1521                 const tchar *fs_source_path, const tchar *wim_target_path,
1522                 int add_flags)
1523 {
1524         struct wimlib_update_command cmd;
1525
1526         cmd.op = WIMLIB_UPDATE_OP_ADD;
1527         cmd.add.fs_source_path = (tchar *)fs_source_path;
1528         cmd.add.wim_target_path = (tchar *)wim_target_path;
1529         cmd.add.add_flags = add_flags;
1530         cmd.add.config_file = NULL;
1531
1532         return update1(wim, image, &cmd);
1533 }