]> wimlib.net Git - wimlib/blob - src/update_image.c
Add wrappers around wimlib_update_image()
[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                 return ret;
406
407         dentry->file_name = new_name;
408         dentry->file_name_nbytes = new_name_nbytes;
409
410         /* Clear the short name.  */
411         prim.type = CHANGE_SHORT_NAME;
412         prim.name.subject = dentry;
413         prim.name.old_name = dentry->short_name;
414         ret = record_update_primitive(j, prim);
415         if (ret)
416                 return ret;
417
418         dentry->short_name = NULL;
419         dentry->short_name_nbytes = 0;
420         return 0;
421 }
422
423 static void
424 next_command(struct update_command_journal *j)
425 {
426         j->cur_cmd++;
427 }
428
429 static void
430 commit_update(struct update_command_journal *j)
431 {
432         for (size_t i = 0; i < j->num_cmds; i++)
433         {
434                 for (size_t k = 0; k < j->cmd_prims[i].num_entries; k++)
435                 {
436                         if (j->cmd_prims[i].entries[k].type == CHANGE_FILE_NAME ||
437                             j->cmd_prims[i].entries[k].type == CHANGE_SHORT_NAME)
438                         {
439                                 FREE(j->cmd_prims[i].entries[k].name.old_name);
440                         }
441                 }
442         }
443         free_update_command_journal(j);
444 }
445
446 static void
447 rollback_update(struct update_command_journal *j)
448 {
449         /* Rollback each logical update command, in reverse order.  */
450         size_t i = j->cur_cmd;
451         if (i < j->num_cmds)
452                 i++;
453         while (i--)
454                 rollback_update_command(&j->cmd_prims[i], j->root_p, &j->orphans);
455         free_update_command_journal(j);
456 }
457
458 static int
459 handle_conflict(struct wim_dentry *branch, struct wim_dentry *existing,
460                 struct update_command_journal *j,
461                 int add_flags, wimlib_progress_func_t progress_func)
462 {
463         bool branch_is_dir = dentry_is_directory(branch);
464         bool existing_is_dir = dentry_is_directory(existing);
465
466         if (branch_is_dir != existing_is_dir) {
467                 if (existing_is_dir)  {
468                         ERROR("\"%"TS"\" is a directory!\n"
469                               "        Specify the path at which "
470                               "to place the file inside this directory.",
471                               dentry_full_path(existing));
472                         return WIMLIB_ERR_IS_DIRECTORY;
473                 } else {
474                         ERROR("Can't place directory at \"%"TS"\" because "
475                               "a nondirectory file already exists there!",
476                               dentry_full_path(existing));
477                         return WIMLIB_ERR_NOTDIR;
478                 }
479         }
480
481         if (branch_is_dir) {
482                 /* Directory overlay  */
483                 while (dentry_has_children(branch)) {
484                         struct wim_dentry *new_child;
485                         struct wim_dentry *existing_child;
486                         int ret;
487
488                         new_child = dentry_any_child(branch);
489
490                         existing_child =
491                                 get_dentry_child_with_utf16le_name(existing,
492                                                                    new_child->file_name,
493                                                                    new_child->file_name_nbytes,
494                                                                    WIMLIB_CASE_PLATFORM_DEFAULT);
495                         unlink_dentry(new_child);
496                         if (existing_child) {
497                                 ret = handle_conflict(new_child, existing_child,
498                                                       j, add_flags, progress_func);
499                         } else {
500                                 ret = journaled_link(j, new_child, existing);
501                         }
502                         if (ret) {
503                                 dentry_add_child(branch, new_child);
504                                 return ret;
505                         }
506                 }
507                 free_dentry(branch);
508                 return 0;
509         } else if (add_flags & WIMLIB_ADD_FLAG_NO_REPLACE) {
510                 /* Can't replace nondirectory file  */
511                 ERROR("Refusing to overwrite nondirectory file \"%"TS"\"",
512                       dentry_full_path(existing));
513                 return WIMLIB_ERR_INVALID_OVERLAY;
514         } else {
515                 /* Replace nondirectory file  */
516                 struct wim_dentry *parent;
517                 int ret;
518
519                 parent = existing->parent;
520
521                 ret = calculate_dentry_full_path(existing);
522                 if (ret)
523                         return ret;
524
525                 ret = journaled_unlink(j, existing);
526                 if (ret)
527                         return ret;
528
529                 ret = journaled_link(j, branch, parent);
530                 if (ret)
531                         return ret;
532
533                 if (progress_func && (add_flags & WIMLIB_ADD_FLAG_VERBOSE)) {
534                         union wimlib_progress_info info;
535
536                         info.replace.path_in_wim = existing->_full_path;
537                         progress_func(WIMLIB_PROGRESS_MSG_REPLACE_FILE_IN_WIM, &info);
538                 }
539                 return 0;
540         }
541 }
542
543 static int
544 do_attach_branch(struct wim_dentry *branch, const utf16lechar *target,
545                  struct update_command_journal *j,
546                  int add_flags, wimlib_progress_func_t progress_func)
547 {
548         struct wim_dentry *parent;
549         struct wim_dentry *existing;
550         const utf16lechar empty_name[1] = {0};
551         const utf16lechar *cur_component_name;
552         size_t cur_component_nbytes;
553         const utf16lechar *next_component_name;
554         int ret;
555
556         /* Attempt to create root directory before proceeding to the "real"
557          * first component  */
558         parent = NULL;
559         existing = *j->root_p;
560         cur_component_name = empty_name;
561         cur_component_nbytes = 0;
562
563         /* Skip leading slashes  */
564         next_component_name = target;
565         while (*next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR))
566                 next_component_name++;
567
568         while (*next_component_name) { /* While not the last component ... */
569                 const utf16lechar *end;
570
571                 if (existing) {
572                         /* Descend into existing directory  */
573                         if (!dentry_is_directory(existing)) {
574                                 ERROR("\"%"TS"\" in the WIM image "
575                                       "is not a directory!",
576                                       dentry_full_path(existing));
577                                 return WIMLIB_ERR_NOTDIR;
578                         }
579                 } else {
580                         /* A parent directory of the target didn't exist.  Make
581                          * the way by creating a filler directory.  */
582                         struct wim_dentry *filler;
583
584                         ret = new_filler_directory(&filler);
585                         if (ret)
586                                 return ret;
587                         ret = dentry_set_name_utf16le(filler,
588                                                       cur_component_name,
589                                                       cur_component_nbytes);
590                         if (ret) {
591                                 free_dentry(filler);
592                                 return ret;
593                         }
594                         ret = journaled_link(j, filler, parent);
595                         if (ret) {
596                                 free_dentry(filler);
597                                 return ret;
598                         }
599                         existing = filler;
600                 }
601
602                 /* Advance to next component  */
603
604                 cur_component_name = next_component_name;
605                 end = cur_component_name + 1;
606                 while (*end && *end != cpu_to_le16(WIM_PATH_SEPARATOR))
607                         end++;
608
609                 next_component_name = end;
610                 if (*end) {
611                         /* There will still be more components after this.  */
612                         do {
613                         } while (*++next_component_name == cpu_to_le16(WIM_PATH_SEPARATOR));
614                         wimlib_assert(*next_component_name);  /* No trailing slashes  */
615                 } else {
616                         /* This will be the last component  */
617                         next_component_name = end;
618                 }
619                 parent = existing;
620                 cur_component_nbytes = (end - cur_component_name) * sizeof(utf16lechar);
621                 existing = get_dentry_child_with_utf16le_name(
622                                         parent,
623                                         cur_component_name,
624                                         cur_component_nbytes,
625                                         WIMLIB_CASE_PLATFORM_DEFAULT);
626         }
627
628         /* Last component  */
629         if (existing) {
630                 return handle_conflict(branch, existing, j,
631                                        add_flags, progress_func);
632         } else {
633                 return journaled_link(j, branch, parent);
634         }
635 }
636
637 /*
638  * Place the directory entry tree @branch at the path @target_tstr in the WIM
639  * image.
640  *
641  * @target_tstr cannot contain trailing slashes, and all path separators must be
642  * WIM_PATH_SEPARATOR.
643  *
644  * On success, @branch is committed to the journal @j.
645  * Otherwise @branch is freed.
646  *
647  * The relevant @add_flags are WIMLIB_ADD_FLAG_NO_REPLACE and
648  * WIMLIB_ADD_FLAG_VERBOSE.
649  */
650 static int
651 attach_branch(struct wim_dentry *branch, const tchar *target_tstr,
652               struct update_command_journal *j,
653               int add_flags, wimlib_progress_func_t progress_func)
654 {
655         int ret;
656         const utf16lechar *target;
657
658         ret = 0;
659         if (unlikely(!branch))
660                 goto out;
661
662         ret = tstr_get_utf16le(target_tstr, &target);
663         if (ret)
664                 goto out_free_branch;
665
666         BUILD_BUG_ON(WIM_PATH_SEPARATOR != OS_PREFERRED_PATH_SEPARATOR);
667         ret = dentry_set_name(branch, path_basename(target_tstr));
668         if (ret)
669                 goto out_free_target;
670
671         ret = do_attach_branch(branch, target, j, add_flags, progress_func);
672         if (ret)
673                 goto out_free_target;
674         /* branch was successfully committed to the journal  */
675         branch = NULL;
676 out_free_target:
677         tstr_put_utf16le(target);
678 out_free_branch:
679         free_dentry_tree(branch, j->lookup_table);
680 out:
681         return ret;
682 }
683
684 static const char wincfg[] =
685 "[ExclusionList]\n"
686 "/$ntfs.log\n"
687 "/hiberfil.sys\n"
688 "/pagefile.sys\n"
689 "/swapfile.sys\n"
690 "/System Volume Information\n"
691 "/RECYCLER\n"
692 "/Windows/CSC\n";
693
694 static const tchar *wimboot_cfgfile =
695             WIMLIB_WIM_PATH_SEPARATOR_STRING T("Windows")
696             WIMLIB_WIM_PATH_SEPARATOR_STRING T("System32")
697             WIMLIB_WIM_PATH_SEPARATOR_STRING T("WimBootCompress.ini");
698
699 static int
700 get_capture_config(const tchar *config_file, struct capture_config *config,
701                    int add_flags, const tchar *fs_source_path)
702 {
703         int ret;
704         tchar *tmp_config_file = NULL;
705
706         memset(config, 0, sizeof(*config));
707
708         /* For WIMBoot capture, check for default capture configuration file
709          * unless one was explicitly specified.  */
710         if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
711
712                 /* XXX: Handle loading file correctly when in NTFS volume.  */
713
714                 size_t len = tstrlen(fs_source_path) +
715                              tstrlen(wimboot_cfgfile);
716                 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
717                 struct stat st;
718
719                 tsprintf(tmp_config_file, T("%"TS"%"TS),
720                          fs_source_path, wimboot_cfgfile);
721                 if (!tstat(tmp_config_file, &st)) {
722                         config_file = tmp_config_file;
723                         add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
724                 } else {
725                         WARNING("\"%"TS"\" does not exist.\n"
726                                 "          Using default capture configuration!",
727                                 tmp_config_file);
728                 }
729         }
730
731         if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
732                 /* Use Windows default.  */
733                 if (config_file)
734                         return WIMLIB_ERR_INVALID_PARAM;
735                 ret = read_capture_config(T("wincfg"), wincfg,
736                                           sizeof(wincfg) - 1, config);
737         } else if (config_file) {
738                 /* Use the specified configuration file.  */
739                 ret = read_capture_config(config_file, NULL, 0, config);
740         } else {
741                 /* ... Or don't use any configuration file at all.  No files
742                  * will be excluded from capture, all files will be compressed,
743                  * etc.  */
744                 ret = 0;
745         }
746         FREE(tmp_config_file);
747         return ret;
748 }
749
750 static int
751 execute_add_command(struct update_command_journal *j,
752                     WIMStruct *wim,
753                     const struct wimlib_update_command *add_cmd,
754                     struct wim_inode_table *inode_table,
755                     struct wim_sd_set *sd_set,
756                     struct list_head *unhashed_streams,
757                     wimlib_progress_func_t progress_func)
758 {
759         int ret;
760         int add_flags;
761         tchar *fs_source_path;
762         tchar *wim_target_path;
763         const tchar *config_file;
764         struct add_image_params params;
765         struct capture_config config;
766         capture_tree_t capture_tree = platform_default_capture_tree;
767 #ifdef WITH_NTFS_3G
768         struct _ntfs_volume *ntfs_vol = NULL;
769 #endif
770         void *extra_arg = NULL;
771         struct wim_dentry *branch;
772
773         add_flags = add_cmd->add.add_flags;
774         fs_source_path = add_cmd->add.fs_source_path;
775         wim_target_path = add_cmd->add.wim_target_path;
776         config_file = add_cmd->add.config_file;
777
778         DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
779               fs_source_path, wim_target_path, add_flags);
780
781         memset(&params, 0, sizeof(params));
782
783 #ifdef WITH_NTFS_3G
784         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
785                 capture_tree = build_dentry_tree_ntfs;
786                 extra_arg = &ntfs_vol;
787                 if (wim_get_current_image_metadata(wim)->ntfs_vol != NULL) {
788                         ERROR("NTFS volume already set");
789                         ret = WIMLIB_ERR_INVALID_PARAM;
790                         goto out;
791                 }
792         }
793 #endif
794
795         ret = get_capture_config(config_file, &config,
796                                  add_flags, fs_source_path);
797         if (ret)
798                 goto out;
799
800         params.lookup_table = wim->lookup_table;
801         params.unhashed_streams = unhashed_streams;
802         params.inode_table = inode_table;
803         params.sd_set = sd_set;
804         params.config = &config;
805         params.add_flags = add_flags;
806         params.extra_arg = extra_arg;
807
808         params.progress_func = progress_func;
809         params.progress.scan.source = fs_source_path;
810         params.progress.scan.wim_target_path = wim_target_path;
811         if (progress_func)
812                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &params.progress);
813
814         if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
815                 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
816         ret = (*capture_tree)(&branch, fs_source_path, &params);
817         if (ret)
818                 goto out_destroy_config;
819
820         if (progress_func)
821                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &params.progress);
822
823         if (WIMLIB_IS_WIM_ROOT_PATH(wim_target_path) &&
824             branch && !dentry_is_directory(branch))
825         {
826                 ERROR("\"%"TS"\" is not a directory!", fs_source_path);
827                 ret = WIMLIB_ERR_NOTDIR;
828                 free_dentry_tree(branch, wim->lookup_table);
829                 goto out_cleanup_after_capture;
830         }
831
832         ret = attach_branch(branch, wim_target_path, j,
833                             add_flags, params.progress_func);
834         if (ret)
835                 goto out_cleanup_after_capture;
836
837         if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
838             WIMLIB_IS_WIM_ROOT_PATH(wim_target_path))
839         {
840                 params.add_flags = 0;
841                 params.progress_func = NULL;
842                 params.config = NULL;
843
844                 /* If a capture configuration file was explicitly specified when
845                  * capturing an image in WIMBoot mode, save it as
846                  * /Windows/System32/WimBootCompress.ini in the WIM image. */
847                 ret = platform_default_capture_tree(&branch, config_file, &params);
848                 if (ret)
849                         goto out_cleanup_after_capture;
850
851                 ret = attach_branch(branch, wimboot_cfgfile, j, 0, NULL);
852                 if (ret)
853                         goto out_cleanup_after_capture;
854         }
855
856 #ifdef WITH_NTFS_3G
857         wim_get_current_image_metadata(wim)->ntfs_vol = ntfs_vol;
858 #endif
859         if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
860                 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
861         ret = 0;
862         goto out_destroy_config;
863 out_cleanup_after_capture:
864 #ifdef WITH_NTFS_3G
865         if (ntfs_vol)
866                 do_ntfs_umount(ntfs_vol);
867 #endif
868 out_destroy_config:
869         destroy_capture_config(&config);
870 out:
871         return ret;
872 }
873
874 static int
875 execute_delete_command(struct update_command_journal *j,
876                        WIMStruct *wim,
877                        const struct wimlib_update_command *delete_cmd)
878 {
879         int flags;
880         const tchar *wim_path;
881         struct wim_dentry *tree;
882
883         flags = delete_cmd->delete_.delete_flags;
884         wim_path = delete_cmd->delete_.wim_path;
885
886         DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
887
888         tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
889         if (!tree) {
890                 /* Path to delete does not exist in the WIM. */
891                 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
892                         return 0;
893                 } else {
894                         ERROR("Path \"%"TS"\" does not exist in WIM image %d",
895                               wim_path, wim->current_image);
896                         return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
897                 }
898         }
899
900         if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
901                 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
902                       "but a recursive delete was not requested",
903                       wim_path, wim->current_image);
904                 return WIMLIB_ERR_IS_DIRECTORY;
905         }
906
907         return journaled_unlink(j, tree);
908 }
909
910 static int
911 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
912 {
913         FREE(dentry->_full_path);
914         dentry->_full_path = NULL;
915         return 0;
916 }
917
918 /* Is @d1 a (possibly nonproper) ancestor of @d2?  */
919 static bool
920 is_ancestor(struct wim_dentry *d1, struct wim_dentry *d2)
921 {
922         for (;;) {
923                 if (d2 == d1)
924                         return true;
925                 if (dentry_is_root(d2))
926                         return false;
927                 d2 = d2->parent;
928         }
929 }
930
931 /* Rename a file or directory in the WIM.
932  *
933  * This returns a -errno value.
934  *
935  * The journal @j is optional.
936  */
937 int
938 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to,
939                 CASE_SENSITIVITY_TYPE case_type,
940                 struct update_command_journal *j)
941 {
942         struct wim_dentry *src;
943         struct wim_dentry *dst;
944         struct wim_dentry *parent_of_dst;
945         int ret;
946
947         /* This rename() implementation currently only supports actual files
948          * (not alternate data streams) */
949
950         src = get_dentry(wim, from, case_type);
951         if (!src)
952                 return -errno;
953
954         dst = get_dentry(wim, to, case_type);
955
956         if (dst) {
957                 /* Destination file exists */
958
959                 if (src == dst) /* Same file */
960                         return 0;
961
962                 if (!dentry_is_directory(src)) {
963                         /* Cannot rename non-directory to directory. */
964                         if (dentry_is_directory(dst))
965                                 return -EISDIR;
966                 } else {
967                         /* Cannot rename directory to a non-directory or a non-empty
968                          * directory */
969                         if (!dentry_is_directory(dst))
970                                 return -ENOTDIR;
971                         if (dentry_has_children(dst))
972                                 return -ENOTEMPTY;
973                 }
974                 parent_of_dst = dst->parent;
975         } else {
976                 /* Destination does not exist */
977                 parent_of_dst = get_parent_dentry(wim, to, case_type);
978                 if (!parent_of_dst)
979                         return -errno;
980
981                 if (!dentry_is_directory(parent_of_dst))
982                         return -ENOTDIR;
983         }
984
985         /* @src can't be an ancestor of @dst.  Otherwise we're unlinking @src
986          * from the tree and creating a loop...  */
987         if (is_ancestor(src, parent_of_dst))
988                 return -EBUSY;
989
990         if (j) {
991                 if (dst)
992                         if (journaled_unlink(j, dst))
993                                 return -ENOMEM;
994                 if (journaled_unlink(j, src))
995                         return -ENOMEM;
996                 if (journaled_change_name(j, src, path_basename(to)))
997                         return -ENOMEM;
998                 if (journaled_link(j, src, parent_of_dst))
999                         return -ENOMEM;
1000         } else {
1001                 ret = dentry_set_name(src, path_basename(to));
1002                 if (ret)
1003                         return -ENOMEM;
1004                 if (dst) {
1005                         unlink_dentry(dst);
1006                         free_dentry_tree(dst, wim->lookup_table);
1007                 }
1008                 unlink_dentry(src);
1009                 dentry_add_child(parent_of_dst, src);
1010         }
1011         if (src->_full_path)
1012                 for_dentry_in_tree(src, free_dentry_full_path, NULL);
1013         return 0;
1014 }
1015
1016
1017 static int
1018 execute_rename_command(struct update_command_journal *j,
1019                        WIMStruct *wim,
1020                        const struct wimlib_update_command *rename_cmd)
1021 {
1022         int ret;
1023
1024         ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
1025                               rename_cmd->rename.wim_target_path,
1026                               WIMLIB_CASE_PLATFORM_DEFAULT, j);
1027         if (ret) {
1028                 ret = -ret;
1029                 errno = ret;
1030                 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
1031                                  rename_cmd->rename.wim_source_path,
1032                                  rename_cmd->rename.wim_target_path);
1033                 switch (ret) {
1034                 case ENOMEM:
1035                         ret = WIMLIB_ERR_NOMEM;
1036                         break;
1037                 case ENOTDIR:
1038                         ret = WIMLIB_ERR_NOTDIR;
1039                         break;
1040                 case ENOTEMPTY:
1041                 case EBUSY:
1042                         /* XXX: EBUSY is returned when the rename would create a
1043                          * loop.  It maybe should have its own error code.  */
1044                         ret = WIMLIB_ERR_NOTEMPTY;
1045                         break;
1046                 case EISDIR:
1047                         ret = WIMLIB_ERR_IS_DIRECTORY;
1048                         break;
1049                 case ENOENT:
1050                 default:
1051                         ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
1052                         break;
1053                 }
1054         }
1055         return ret;
1056 }
1057
1058 static inline const tchar *
1059 update_op_to_str(int op)
1060 {
1061         switch (op) {
1062         case WIMLIB_UPDATE_OP_ADD:
1063                 return T("add");
1064         case WIMLIB_UPDATE_OP_DELETE:
1065                 return T("delete");
1066         case WIMLIB_UPDATE_OP_RENAME:
1067                 return T("rename");
1068         default:
1069                 wimlib_assert(0);
1070                 return NULL;
1071         }
1072 }
1073
1074 static bool
1075 have_command_type(const struct wimlib_update_command *cmds, size_t num_cmds,
1076                   enum wimlib_update_op op)
1077 {
1078         for (size_t i = 0; i < num_cmds; i++)
1079                 if (cmds[i].op == op)
1080                         return true;
1081         return false;
1082 }
1083
1084 static int
1085 execute_update_commands(WIMStruct *wim,
1086                         const struct wimlib_update_command *cmds,
1087                         size_t num_cmds,
1088                         int update_flags,
1089                         wimlib_progress_func_t progress_func)
1090 {
1091         struct wim_inode_table *inode_table;
1092         struct wim_sd_set *sd_set;
1093         struct list_head unhashed_streams;
1094         struct update_command_journal *j;
1095         union wimlib_progress_info info;
1096         int ret;
1097
1098         if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_ADD)) {
1099                 /* If we have at least one "add" command, create the inode and
1100                  * security descriptor tables to index new inodes and new
1101                  * security descriptors, respectively.  */
1102                 inode_table = alloca(sizeof(struct wim_inode_table));
1103                 sd_set = alloca(sizeof(struct wim_sd_set));
1104
1105                 ret = init_inode_table(inode_table, 9001);
1106                 if (ret)
1107                         goto out;
1108
1109                 ret = init_sd_set(sd_set, wim_get_current_security_data(wim));
1110                 if (ret)
1111                         goto out_destroy_inode_table;
1112
1113                 INIT_LIST_HEAD(&unhashed_streams);
1114         } else {
1115                 inode_table = NULL;
1116                 sd_set = NULL;
1117         }
1118
1119         /* Start an in-memory journal to allow rollback if something goes wrong
1120          */
1121         j = new_update_command_journal(num_cmds,
1122                                        &wim_get_current_image_metadata(wim)->root_dentry,
1123                                        wim->lookup_table);
1124         if (!j) {
1125                 ret = WIMLIB_ERR_NOMEM;
1126                 goto out_destroy_sd_set;
1127         }
1128
1129         info.update.completed_commands = 0;
1130         info.update.total_commands = num_cmds;
1131         ret = 0;
1132         for (size_t i = 0; i < num_cmds; i++) {
1133                 DEBUG("Executing update command %zu of %zu (op=%"TS")",
1134                       i + 1, num_cmds, update_op_to_str(cmds[i].op));
1135                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
1136                     progress_func)
1137                 {
1138                         info.update.command = &cmds[i];
1139                         (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
1140                                          &info);
1141                 }
1142                 ret = WIMLIB_ERR_INVALID_PARAM;
1143                 switch (cmds[i].op) {
1144                 case WIMLIB_UPDATE_OP_ADD:
1145                         ret = execute_add_command(j, wim, &cmds[i], inode_table,
1146                                                   sd_set, &unhashed_streams,
1147                                                   progress_func);
1148                         break;
1149                 case WIMLIB_UPDATE_OP_DELETE:
1150                         ret = execute_delete_command(j, wim, &cmds[i]);
1151                         break;
1152                 case WIMLIB_UPDATE_OP_RENAME:
1153                         ret = execute_rename_command(j, wim, &cmds[i]);
1154                         break;
1155                 }
1156                 if (unlikely(ret))
1157                         goto rollback;
1158                 info.update.completed_commands++;
1159                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
1160                     progress_func)
1161                 {
1162                         (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
1163                                          &info);
1164                 }
1165                 next_command(j);
1166         }
1167
1168         commit_update(j);
1169         if (inode_table) {
1170                 struct wim_image_metadata *imd;
1171
1172                 imd = wim_get_current_image_metadata(wim);
1173
1174                 list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
1175                 inode_table_prepare_inode_list(inode_table, &imd->inode_list);
1176         }
1177         goto out_destroy_sd_set;
1178
1179 rollback:
1180         if (sd_set)
1181                 rollback_new_security_descriptors(sd_set);
1182         rollback_update(j);
1183 out_destroy_sd_set:
1184         if (sd_set)
1185                 destroy_sd_set(sd_set);
1186 out_destroy_inode_table:
1187         if (inode_table)
1188                 destroy_inode_table(inode_table);
1189 out:
1190         return ret;
1191 }
1192
1193
1194 static int
1195 check_add_command(struct wimlib_update_command *cmd,
1196                   const struct wim_header *hdr)
1197 {
1198         int add_flags = cmd->add.add_flags;
1199
1200         if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
1201                           WIMLIB_ADD_FLAG_DEREFERENCE |
1202                           WIMLIB_ADD_FLAG_VERBOSE |
1203                           /* BOOT doesn't make sense for wimlib_update_image().  */
1204                           /*WIMLIB_ADD_FLAG_BOOT |*/
1205                           WIMLIB_ADD_FLAG_UNIX_DATA |
1206                           WIMLIB_ADD_FLAG_NO_ACLS |
1207                           WIMLIB_ADD_FLAG_STRICT_ACLS |
1208                           WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
1209                           WIMLIB_ADD_FLAG_RPFIX |
1210                           WIMLIB_ADD_FLAG_NORPFIX |
1211                           WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
1212                           WIMLIB_ADD_FLAG_WINCONFIG |
1213                           WIMLIB_ADD_FLAG_WIMBOOT |
1214                           WIMLIB_ADD_FLAG_NO_REPLACE))
1215                 return WIMLIB_ERR_INVALID_PARAM;
1216
1217         bool is_entire_image = WIMLIB_IS_WIM_ROOT_PATH(cmd->add.wim_target_path);
1218
1219 #ifndef WITH_NTFS_3G
1220         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1221                 ERROR("wimlib was compiled without support for NTFS-3g, so\n"
1222                       "        we cannot capture a WIM image directly "
1223                       "from an NTFS volume");
1224                 return WIMLIB_ERR_UNSUPPORTED;
1225         }
1226 #endif
1227
1228 #ifdef __WIN32__
1229         /* Check for flags not supported on Windows */
1230         if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
1231                 ERROR("Capturing UNIX-specific data is not supported on Windows");
1232                 return WIMLIB_ERR_UNSUPPORTED;
1233         }
1234         if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
1235                 ERROR("Dereferencing symbolic links is not supported on Windows");
1236                 return WIMLIB_ERR_UNSUPPORTED;
1237         }
1238 #endif
1239
1240         /* VERBOSE implies EXCLUDE_VERBOSE */
1241         if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
1242                 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
1243
1244         /* Check for contradictory reparse point fixup flags */
1245         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1246                           WIMLIB_ADD_FLAG_NORPFIX)) ==
1247                 (WIMLIB_ADD_FLAG_RPFIX |
1248                  WIMLIB_ADD_FLAG_NORPFIX))
1249         {
1250                 ERROR("Cannot specify RPFIX and NORPFIX flags "
1251                       "at the same time!");
1252                 return WIMLIB_ERR_INVALID_PARAM;
1253         }
1254
1255         /* Set default behavior on reparse point fixups if requested */
1256         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
1257                           WIMLIB_ADD_FLAG_NORPFIX)) == 0)
1258         {
1259                 /* Do reparse-point fixups by default if we are capturing an
1260                  * entire image and either the header flag is set from previous
1261                  * images, or if this is the first image being added. */
1262                 if (is_entire_image &&
1263                     ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
1264                         add_flags |= WIMLIB_ADD_FLAG_RPFIX;
1265         }
1266
1267         if (!is_entire_image) {
1268                 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
1269                         ERROR("Cannot add directly from a NTFS volume "
1270                               "when not capturing a full image!");
1271                         return WIMLIB_ERR_INVALID_PARAM;
1272                 }
1273
1274                 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
1275                         ERROR("Cannot do reparse point fixups when "
1276                               "not capturing a full image!");
1277                         return WIMLIB_ERR_INVALID_PARAM;
1278                 }
1279         }
1280         /* We may have modified the add flags. */
1281         cmd->add.add_flags = add_flags;
1282         return 0;
1283 }
1284
1285 static int
1286 check_delete_command(const struct wimlib_update_command *cmd)
1287 {
1288         if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
1289                                           WIMLIB_DELETE_FLAG_RECURSIVE))
1290                 return WIMLIB_ERR_INVALID_PARAM;
1291         return 0;
1292 }
1293
1294 static int
1295 check_rename_command(const struct wimlib_update_command *cmd)
1296 {
1297         if (cmd->rename.rename_flags != 0)
1298                 return WIMLIB_ERR_INVALID_PARAM;
1299         return 0;
1300 }
1301
1302 static int
1303 check_update_command(struct wimlib_update_command *cmd,
1304                      const struct wim_header *hdr)
1305 {
1306         switch (cmd->op) {
1307         case WIMLIB_UPDATE_OP_ADD:
1308                 return check_add_command(cmd, hdr);
1309         case WIMLIB_UPDATE_OP_DELETE:
1310                 return check_delete_command(cmd);
1311         case WIMLIB_UPDATE_OP_RENAME:
1312                 return check_rename_command(cmd);
1313         }
1314         return 0;
1315 }
1316
1317 static int
1318 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
1319                       const struct wim_header *hdr)
1320 {
1321         int ret = 0;
1322         for (size_t i = 0; i < num_cmds; i++) {
1323                 ret = check_update_command(&cmds[i], hdr);
1324                 if (ret)
1325                         break;
1326         }
1327         return ret;
1328 }
1329
1330
1331 static void
1332 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
1333 {
1334         if (cmds) {
1335                 for (size_t i = 0; i < num_cmds; i++) {
1336                         switch (cmds[i].op) {
1337                         case WIMLIB_UPDATE_OP_ADD:
1338                                 FREE(cmds[i].add.wim_target_path);
1339                                 break;
1340                         case WIMLIB_UPDATE_OP_DELETE:
1341                                 FREE(cmds[i].delete_.wim_path);
1342                                 break;
1343                         case WIMLIB_UPDATE_OP_RENAME:
1344                                 FREE(cmds[i].rename.wim_source_path);
1345                                 FREE(cmds[i].rename.wim_target_path);
1346                                 break;
1347                         }
1348                 }
1349                 FREE(cmds);
1350         }
1351 }
1352
1353 static int
1354 copy_update_commands(const struct wimlib_update_command *cmds,
1355                      size_t num_cmds,
1356                      struct wimlib_update_command **cmds_copy_ret)
1357 {
1358         int ret;
1359         struct wimlib_update_command *cmds_copy;
1360
1361         cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
1362         if (!cmds_copy)
1363                 goto oom;
1364
1365         for (size_t i = 0; i < num_cmds; i++) {
1366                 cmds_copy[i].op = cmds[i].op;
1367                 switch (cmds[i].op) {
1368                 case WIMLIB_UPDATE_OP_ADD:
1369                         cmds_copy[i].add.fs_source_path = cmds[i].add.fs_source_path;
1370                         cmds_copy[i].add.wim_target_path =
1371                                 canonicalize_wim_path(cmds[i].add.wim_target_path);
1372                         if (!cmds_copy[i].add.wim_target_path)
1373                                 goto oom;
1374                         cmds_copy[i].add.config_file = cmds[i].add.config_file;
1375                         cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
1376                         break;
1377                 case WIMLIB_UPDATE_OP_DELETE:
1378                         cmds_copy[i].delete_.wim_path =
1379                                 canonicalize_wim_path(cmds[i].delete_.wim_path);
1380                         if (!cmds_copy[i].delete_.wim_path)
1381                                 goto oom;
1382                         cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
1383                         break;
1384                 case WIMLIB_UPDATE_OP_RENAME:
1385                         cmds_copy[i].rename.wim_source_path =
1386                                 canonicalize_wim_path(cmds[i].rename.wim_source_path);
1387                         cmds_copy[i].rename.wim_target_path =
1388                                 canonicalize_wim_path(cmds[i].rename.wim_target_path);
1389                         if (!cmds_copy[i].rename.wim_source_path ||
1390                             !cmds_copy[i].rename.wim_target_path)
1391                                 goto oom;
1392                         break;
1393                 default:
1394                         ERROR("Unknown update operation %u", cmds[i].op);
1395                         ret = WIMLIB_ERR_INVALID_PARAM;
1396                         goto err;
1397                 }
1398         }
1399         *cmds_copy_ret = cmds_copy;
1400         ret = 0;
1401 out:
1402         return ret;
1403 oom:
1404         ret = WIMLIB_ERR_NOMEM;
1405 err:
1406         free_update_commands(cmds_copy, num_cmds);
1407         goto out;
1408 }
1409
1410 /* API function documented in wimlib.h  */
1411 WIMLIBAPI int
1412 wimlib_update_image(WIMStruct *wim,
1413                     int image,
1414                     const struct wimlib_update_command *cmds,
1415                     size_t num_cmds,
1416                     int update_flags,
1417                     wimlib_progress_func_t progress_func)
1418 {
1419         int ret;
1420         struct wimlib_update_command *cmds_copy;
1421
1422         if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
1423                 return WIMLIB_ERR_INVALID_PARAM;
1424
1425         DEBUG("Updating image %d with %zu commands", image, num_cmds);
1426
1427         if (have_command_type(cmds, num_cmds, WIMLIB_UPDATE_OP_DELETE))
1428                 ret = can_delete_from_wim(wim);
1429         else
1430                 ret = can_modify_wim(wim);
1431
1432         if (ret)
1433                 goto out;
1434
1435         /* Load the metadata for the image to modify (if not loaded already) */
1436         ret = select_wim_image(wim, image);
1437         if (ret)
1438                 goto out;
1439
1440         DEBUG("Preparing %zu update commands", num_cmds);
1441
1442         /* Make a copy of the update commands, in the process doing certain
1443          * canonicalizations on paths (e.g. translating backslashes to forward
1444          * slashes).  This is done to avoid modifying the caller's copy of the
1445          * commands. */
1446         ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
1447         if (ret)
1448                 goto out;
1449
1450         /* Perform additional checks on the update commands before we execute
1451          * them. */
1452         ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
1453         if (ret)
1454                 goto out_free_cmds_copy;
1455
1456         /* Actually execute the update commands. */
1457         DEBUG("Executing %zu update commands", num_cmds);
1458         ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
1459                                       progress_func);
1460         if (ret)
1461                 goto out_free_cmds_copy;
1462
1463         wim->image_metadata[image - 1]->modified = 1;
1464
1465         /* Statistics about the WIM image, such as the numbers of files and
1466          * directories, may have changed.  Call xml_update_image_info() to
1467          * recalculate these statistics. */
1468         xml_update_image_info(wim, image);
1469 out_free_cmds_copy:
1470         free_update_commands(cmds_copy, num_cmds);
1471 out:
1472         return ret;
1473 }
1474
1475 static int
1476 update1(WIMStruct *wim, int image, const struct wimlib_update_command *cmd)
1477 {
1478         return wimlib_update_image(wim, image, cmd, 1, 0, NULL);
1479 }
1480
1481 WIMLIBAPI int
1482 wimlib_delete_path(WIMStruct *wim, int image,
1483                    const tchar *path, int delete_flags)
1484 {
1485         struct wimlib_update_command cmd;
1486
1487         cmd.op = WIMLIB_UPDATE_OP_DELETE;
1488         cmd.delete_.wim_path = (tchar *)path;
1489         cmd.delete_.delete_flags = delete_flags;
1490
1491         return update1(wim, image, &cmd);
1492 }
1493
1494 WIMLIBAPI int
1495 wimlib_rename_path(WIMStruct *wim, int image,
1496                    const tchar *source_path, const tchar *dest_path)
1497 {
1498         struct wimlib_update_command cmd;
1499
1500         cmd.op = WIMLIB_UPDATE_OP_RENAME;
1501         cmd.rename.wim_source_path = (tchar *)source_path;
1502         cmd.rename.wim_target_path = (tchar *)dest_path;
1503         cmd.rename.rename_flags = 0;
1504
1505         return update1(wim, image, &cmd);
1506 }
1507
1508 WIMLIBAPI int
1509 wimlib_add_tree(WIMStruct *wim, int image,
1510                 const tchar *fs_source_path, const tchar *wim_target_path,
1511                 int add_flags)
1512 {
1513         struct wimlib_update_command cmd;
1514
1515         cmd.op = WIMLIB_UPDATE_OP_ADD;
1516         cmd.add.fs_source_path = (tchar *)fs_source_path;
1517         cmd.add.wim_target_path = (tchar *)wim_target_path;
1518         cmd.add.add_flags = add_flags;
1519         cmd.add.config_file = NULL;
1520
1521         return update1(wim, image, &cmd);
1522 }