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