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