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