wimlib.h: C++ compatibility
[wimlib] / src / update_image.c
1 /*
2  * update_image.c - Update a WIM image.
3  */
4
5 /*
6  * Copyright (C) 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/capture.h"
29 #include "wimlib/dentry.h"
30 #include "wimlib/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/xml.h"
38
39 #include <errno.h>
40
41 /* Overlays @branch onto @target, both of which must be directories. */
42 static int
43 do_overlay(struct wim_dentry *target, struct wim_dentry *branch)
44 {
45         struct rb_root *rb_root;
46
47         DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
48               branch->file_name, target->file_name);
49
50         if (!dentry_is_directory(branch) || !dentry_is_directory(target)) {
51                 ERROR("Cannot overlay \"%"WS"\" onto existing dentry: "
52                       "is not directory-on-directory!", branch->file_name);
53                 return WIMLIB_ERR_INVALID_OVERLAY;
54         }
55
56         rb_root = &branch->d_inode->i_children;
57         LIST_HEAD(moved_children);
58         while (rb_root->rb_node) { /* While @branch has children... */
59                 struct wim_dentry *child = rbnode_dentry(rb_root->rb_node);
60                 struct wim_dentry *existing;
61
62                 /* Move @child to the directory @target */
63                 unlink_dentry(child);
64                 existing = dentry_add_child(target, child);
65
66                 /* File or directory with same name already exists */
67                 if (existing) {
68                         int ret;
69                         ret = do_overlay(existing, child);
70                         if (ret) {
71                                 /* Overlay failed.  Revert the changes. */
72                                 dentry_add_child(branch, child);
73                                 list_for_each_entry(child, &moved_children, tmp_list)
74                                 {
75                                         unlink_dentry(child);
76                                         dentry_add_child(branch, child);
77                                 }
78                                 return ret;
79                         }
80                 } else {
81                         list_add(&child->tmp_list, &moved_children);
82                 }
83         }
84         free_dentry(branch);
85         return 0;
86 }
87
88 /* Attach or overlay a branch onto the WIM image.
89  *
90  * @root_p:
91  *      Pointer to the root of the WIM image, or pointer to NULL if it has not
92  *      been created yet.
93  * @branch
94  *      Branch to add.
95  * @target_path:
96  *      Path in the WIM image to add the branch, with leading and trailing
97  *      slashes stripped.
98  */
99 static int
100 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
101               tchar *target_path)
102 {
103         tchar *slash;
104         struct wim_dentry *dentry, *parent, *target;
105         int ret;
106
107         DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
108               branch->file_name, target_path);
109
110         if (*target_path == T('\0')) {
111                 /* Target: root directory */
112                 if (*root_p) {
113                         /* Overlay on existing root */
114                         return do_overlay(*root_p, branch);
115                 } else {
116                         if (!dentry_is_directory(branch)) {
117                                 ERROR("Cannot set non-directory as root of WIM image");
118                                 return WIMLIB_ERR_NOTDIR;
119                         }
120                         /* Set as root */
121                         *root_p = branch;
122                         return 0;
123                 }
124         }
125
126         /* Adding a non-root branch.  Create root if it hasn't been created
127          * already. */
128         if (!*root_p) {
129                 ret  = new_filler_directory(T(""), root_p);
130                 if (ret)
131                         return ret;
132         }
133
134         /* Walk the path to the branch, creating filler directories as needed.
135          * */
136         parent = *root_p;
137         while ((slash = tstrchr(target_path, WIM_PATH_SEPARATOR))) {
138                 *slash = T('\0');
139                 dentry = get_dentry_child_with_name(parent, target_path);
140                 if (!dentry) {
141                         ret = new_filler_directory(target_path, &dentry);
142                         if (ret)
143                                 return ret;
144                         dentry_add_child(parent, dentry);
145                 }
146                 parent = dentry;
147                 target_path = slash;
148                 /* Skip over slashes.  Note: this cannot overrun the length of
149                  * the string because the last character cannot be a slash, as
150                  * trailing slashes were tripped.  */
151                 do {
152                         ++target_path;
153                 } while (*target_path == WIM_PATH_SEPARATOR);
154         }
155
156         /* If the target path already existed, overlay the branch onto it.
157          * Otherwise, set the branch as the target path. */
158         target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
159                                                     branch->file_name_nbytes);
160         if (target) {
161                 return do_overlay(target, branch);
162         } else {
163                 dentry_add_child(parent, branch);
164                 return 0;
165         }
166 }
167
168 static int
169 execute_add_command(WIMStruct *wim,
170                     const struct wimlib_update_command *add_cmd,
171                     wimlib_progress_func_t progress_func)
172 {
173         int ret;
174         int add_flags;
175         tchar *fs_source_path;
176         tchar *wim_target_path;
177         struct wim_image_metadata *imd;
178         struct list_head unhashed_streams;
179         struct add_image_params params;
180         int (*capture_tree)(struct wim_dentry **,
181                             const tchar *,
182                             struct add_image_params *);
183         struct wimlib_capture_config *config;
184 #ifdef WITH_NTFS_3G
185         struct _ntfs_volume *ntfs_vol = NULL;
186 #endif
187         void *extra_arg;
188         struct wim_dentry *branch;
189         bool rollback_sd = true;
190
191         wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
192
193         add_flags = add_cmd->add.add_flags;
194         fs_source_path = add_cmd->add.fs_source_path;
195         wim_target_path = add_cmd->add.wim_target_path;
196         config = add_cmd->add.config;
197         DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
198               fs_source_path, wim_target_path, add_flags);
199
200         memset(&params, 0, sizeof(params));
201
202         imd = wim->image_metadata[wim->current_image - 1];
203
204         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
205         #ifdef WITH_NTFS_3G
206                 capture_tree = build_dentry_tree_ntfs;
207                 extra_arg = &ntfs_vol;
208                 if (imd->ntfs_vol != NULL) {
209                         ERROR("NTFS volume already set");
210                         ret = WIMLIB_ERR_INVALID_PARAM;
211                         goto out;
212                 }
213         #else
214                 ret = WIMLIB_ERR_INVALID_PARAM;
215                 goto out;
216         #endif
217         } else {
218         #ifdef __WIN32__
219                 capture_tree = win32_build_dentry_tree;
220         #else
221                 capture_tree = unix_build_dentry_tree;
222         #endif
223                 extra_arg = NULL;
224         }
225
226
227         ret = init_inode_table(&params.inode_table, 9001);
228         if (ret)
229                 goto out;
230
231         ret = init_sd_set(&params.sd_set, imd->security_data);
232         if (ret)
233                 goto out_destroy_inode_table;
234
235         INIT_LIST_HEAD(&unhashed_streams);
236         wim->lookup_table->unhashed_streams = &unhashed_streams;
237         params.lookup_table = wim->lookup_table;
238         params.config = config;
239         params.add_flags = add_flags;
240         params.extra_arg = extra_arg;
241
242         params.progress_func = progress_func;
243         params.progress.scan.source = fs_source_path;
244         params.progress.scan.wim_target_path = wim_target_path;
245         if (progress_func)
246                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &params.progress);
247         if (config) {
248                 config->_prefix = fs_source_path;
249                 config->_prefix_num_tchars = tstrlen(fs_source_path);
250         }
251
252         if (wim_target_path[0] == T('\0'))
253                 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
254         ret = (*capture_tree)(&branch, fs_source_path, &params);
255         if (ret)
256                 goto out_destroy_sd_set;
257
258         if (branch) {
259                 /* Use the target name, not the source name, for
260                  * the root of each branch from a capture
261                  * source.  (This will also set the root dentry
262                  * of the entire image to be unnamed.) */
263                 ret = set_dentry_name(branch,
264                                       path_basename(wim_target_path));
265                 if (ret)
266                         goto out_ntfs_umount;
267
268                 ret = attach_branch(&imd->root_dentry, branch, wim_target_path);
269                 if (ret)
270                         goto out_ntfs_umount;
271         }
272         if (progress_func)
273                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &params.progress);
274         list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
275 #ifdef WITH_NTFS_3G
276         imd->ntfs_vol = ntfs_vol;
277 #endif
278         inode_table_prepare_inode_list(&params.inode_table, &imd->inode_list);
279         ret = 0;
280         rollback_sd = false;
281         if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
282                 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
283         goto out_destroy_sd_set;
284 out_ntfs_umount:
285 #ifdef WITH_NTFS_3G
286         if (ntfs_vol)
287                 do_ntfs_umount(ntfs_vol);
288 #endif
289         free_dentry_tree(branch, wim->lookup_table);
290 out_destroy_sd_set:
291         destroy_sd_set(&params.sd_set, rollback_sd);
292 out_destroy_inode_table:
293         destroy_inode_table(&params.inode_table);
294 out:
295         return ret;
296 }
297
298 static int
299 execute_delete_command(WIMStruct *wim,
300                        const struct wimlib_update_command *delete_cmd)
301 {
302         int flags;
303         const tchar *wim_path;
304         struct wim_dentry *tree;
305         bool is_root;
306
307         wimlib_assert(delete_cmd->op == WIMLIB_UPDATE_OP_DELETE);
308         flags = delete_cmd->delete_.delete_flags;
309         wim_path = delete_cmd->delete_.wim_path;
310
311         DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
312
313         tree = get_dentry(wim, wim_path);
314         if (!tree) {
315                 /* Path to delete does not exist in the WIM. */
316                 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
317                         return 0;
318                 } else {
319                         ERROR("Path \"%"TS"\" does not exist in WIM image %d",
320                               wim_path, wim->current_image);
321                         return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
322                 }
323         }
324
325         if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
326                 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
327                       "but a recursive delete was not requested",
328                       wim_path, wim->current_image);
329                 return WIMLIB_ERR_IS_DIRECTORY;
330         }
331
332         is_root = dentry_is_root(tree);
333         unlink_dentry(tree);
334         free_dentry_tree(tree, wim->lookup_table);
335         if (is_root)
336                 wim->image_metadata[wim->current_image - 1]->root_dentry = NULL;
337         return 0;
338 }
339
340 static int
341 free_dentry_full_path(struct wim_dentry *dentry, void *_ignore)
342 {
343         FREE(dentry->_full_path);
344         dentry->_full_path = NULL;
345         return 0;
346 }
347
348 /*
349  * Rename a file or directory in the WIM.
350  *
351  * This is also called from wimfs_rename() in the FUSE mount code.
352  */
353 int
354 rename_wim_path(WIMStruct *wim, const tchar *from, const tchar *to)
355 {
356         struct wim_dentry *src;
357         struct wim_dentry *dst;
358         struct wim_dentry *parent_of_dst;
359         int ret;
360
361         /* This rename() implementation currently only supports actual files
362          * (not alternate data streams) */
363
364         src = get_dentry(wim, from);
365         if (!src)
366                 return -errno;
367
368         dst = get_dentry(wim, to);
369
370         if (dst) {
371                 /* Destination file exists */
372
373                 if (src == dst) /* Same file */
374                         return 0;
375
376                 if (!dentry_is_directory(src)) {
377                         /* Cannot rename non-directory to directory. */
378                         if (dentry_is_directory(dst))
379                                 return -EISDIR;
380                 } else {
381                         /* Cannot rename directory to a non-directory or a non-empty
382                          * directory */
383                         if (!dentry_is_directory(dst))
384                                 return -ENOTDIR;
385                         if (inode_has_children(dst->d_inode))
386                                 return -ENOTEMPTY;
387                 }
388                 parent_of_dst = dst->parent;
389         } else {
390                 /* Destination does not exist */
391                 parent_of_dst = get_parent_dentry(wim, to);
392                 if (!parent_of_dst)
393                         return -errno;
394
395                 if (!dentry_is_directory(parent_of_dst))
396                         return -ENOTDIR;
397         }
398
399         ret = set_dentry_name(src, path_basename(to));
400         if (ret)
401                 return -ENOMEM;
402         if (dst) {
403                 unlink_dentry(dst);
404                 free_dentry_tree(dst, wim->lookup_table);
405         }
406         unlink_dentry(src);
407         dentry_add_child(parent_of_dst, src);
408         if (src->_full_path)
409                 for_dentry_in_tree(src, free_dentry_full_path, NULL);
410         return 0;
411 }
412
413
414 static int
415 execute_rename_command(WIMStruct *wim,
416                        const struct wimlib_update_command *rename_cmd)
417 {
418         int ret;
419
420         wimlib_assert(rename_cmd->op == WIMLIB_UPDATE_OP_RENAME);
421
422         ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
423                               rename_cmd->rename.wim_target_path);
424         if (ret) {
425                 ret = -ret;
426                 errno = ret;
427                 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
428                                  rename_cmd->rename.wim_source_path,
429                                  rename_cmd->rename.wim_target_path);
430                 switch (ret) {
431                 case ENOMEM:
432                         ret = WIMLIB_ERR_NOMEM;
433                         break;
434                 case ENOTDIR:
435                         ret = WIMLIB_ERR_NOTDIR;
436                         break;
437                 case ENOTEMPTY:
438                         ret = WIMLIB_ERR_NOTEMPTY;
439                         break;
440                 case EISDIR:
441                         ret = WIMLIB_ERR_IS_DIRECTORY;
442                         break;
443                 case ENOENT:
444                 default:
445                         ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
446                         break;
447                 }
448         }
449         return ret;
450 }
451
452 static inline const tchar *
453 update_op_to_str(int op)
454 {
455         switch (op) {
456         case WIMLIB_UPDATE_OP_ADD:
457                 return T("add");
458         case WIMLIB_UPDATE_OP_DELETE:
459                 return T("delete");
460         case WIMLIB_UPDATE_OP_RENAME:
461                 return T("rename");
462         default:
463                 wimlib_assert(0);
464                 return NULL;
465         }
466 }
467
468 static int
469 execute_update_commands(WIMStruct *wim,
470                         const struct wimlib_update_command *cmds,
471                         size_t num_cmds,
472                         int update_flags,
473                         wimlib_progress_func_t progress_func)
474 {
475         int ret = 0;
476         union wimlib_progress_info info;
477         info.update.completed_commands = 0;
478         info.update.total_commands = num_cmds;
479         for (size_t i = 0; i < num_cmds; i++) {
480                 DEBUG("Executing update command %zu of %zu (op=%"TS")",
481                       i + 1, num_cmds, update_op_to_str(cmds[i].op));
482                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
483                     progress_func)
484                 {
485                         info.update.command = &cmds[i];
486                         (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
487                                          &info);
488                 }
489                 switch (cmds[i].op) {
490                 case WIMLIB_UPDATE_OP_ADD:
491                         ret = execute_add_command(wim, &cmds[i], progress_func);
492                         break;
493                 case WIMLIB_UPDATE_OP_DELETE:
494                         ret = execute_delete_command(wim, &cmds[i]);
495                         break;
496                 case WIMLIB_UPDATE_OP_RENAME:
497                         ret = execute_rename_command(wim, &cmds[i]);
498                         break;
499                 default:
500                         wimlib_assert(0);
501                 }
502                 if (ret)
503                         break;
504                 info.update.completed_commands++;
505                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
506                     progress_func)
507                 {
508                         (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
509                                          &info);
510                 }
511         }
512         return ret;
513 }
514
515
516 tchar *winpats[] = {
517         T("/$ntfs.log"),
518         T("/hiberfil.sys"),
519         T("/pagefile.sys"),
520         T("/System Volume Information"),
521         T("/RECYCLER"),
522         T("/Windows/CSC"),
523 };
524
525 static const struct wimlib_capture_config winconfig = {
526         .exclusion_pats = {
527                 .num_pats = ARRAY_LEN(winpats),
528                 .pats = winpats,
529         },
530 };
531
532 static int
533 check_add_command(struct wimlib_update_command *cmd,
534                   const struct wim_header *hdr)
535 {
536         int add_flags = cmd->add.add_flags;
537
538         /* Are we adding the entire image or not?  An empty wim_target_path
539          * indicates that the tree we're adding is to be placed in the root of
540          * the image.  We consider this to be capturing the entire image,
541          * although it could potentially be an overlay on an existing root as
542          * well. */
543         bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
544
545 #ifdef __WIN32__
546         /* Check for flags not supported on Windows */
547         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
548                 ERROR("wimlib was compiled without support for NTFS-3g, so");
549                 ERROR("we cannot capture a WIM image directly from a NTFS volume");
550                 return WIMLIB_ERR_UNSUPPORTED;
551         }
552         if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
553                 ERROR("Capturing UNIX-specific data is not supported on Windows");
554                 return WIMLIB_ERR_UNSUPPORTED;
555         }
556         if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
557                 ERROR("Dereferencing symbolic links is not supported on Windows");
558                 return WIMLIB_ERR_UNSUPPORTED;
559         }
560 #endif
561
562         /* VERBOSE implies EXCLUDE_VERBOSE */
563         if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
564                 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
565
566         /* Check for contradictory reparse point fixup flags */
567         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
568                           WIMLIB_ADD_FLAG_NORPFIX)) ==
569                 (WIMLIB_ADD_FLAG_RPFIX |
570                  WIMLIB_ADD_FLAG_NORPFIX))
571         {
572                 ERROR("Cannot specify RPFIX and NORPFIX flags "
573                       "at the same time!");
574                 return WIMLIB_ERR_INVALID_PARAM;
575         }
576
577         /* Set default behavior on reparse point fixups if requested */
578         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
579                           WIMLIB_ADD_FLAG_NORPFIX)) == 0)
580         {
581                 /* Do reparse-point fixups by default if we are capturing an
582                  * entire image and either the header flag is set from previous
583                  * images, or if this is the first image being added. */
584                 if (is_entire_image &&
585                     ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
586                         add_flags |= WIMLIB_ADD_FLAG_RPFIX;
587         }
588
589         if (!is_entire_image) {
590                 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
591                         ERROR("Cannot add directly from a NTFS volume "
592                               "when not capturing a full image!");
593                         return WIMLIB_ERR_INVALID_PARAM;
594                 }
595
596                 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
597                         ERROR("Cannot do reparse point fixups when "
598                               "not capturing a full image!");
599                         return WIMLIB_ERR_INVALID_PARAM;
600                 }
601         }
602         /* We may have modified the add flags. */
603         cmd->add.add_flags = add_flags;
604         return 0;
605 }
606
607 static int
608 check_update_command(struct wimlib_update_command *cmd,
609                      const struct wim_header *hdr)
610 {
611         switch (cmd->op) {
612         case WIMLIB_UPDATE_OP_ADD:
613                 return check_add_command(cmd, hdr);
614         case WIMLIB_UPDATE_OP_DELETE:
615         case WIMLIB_UPDATE_OP_RENAME:
616                 break;
617         }
618         return 0;
619 }
620
621 static int
622 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
623                       const struct wim_header *hdr)
624 {
625         int ret = 0;
626         for (size_t i = 0; i < num_cmds; i++) {
627                 ret = check_update_command(&cmds[i], hdr);
628                 if (ret)
629                         break;
630         }
631         return ret;
632 }
633
634
635 static void
636 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
637 {
638         if (cmds) {
639                 for (size_t i = 0; i < num_cmds; i++) {
640                         switch (cmds[i].op) {
641                         case WIMLIB_UPDATE_OP_ADD:
642                                 FREE(cmds[i].add.fs_source_path);
643                                 FREE(cmds[i].add.wim_target_path);
644                                 free_capture_config(cmds[i].add.config);
645                                 break;
646                         case WIMLIB_UPDATE_OP_DELETE:
647                                 FREE(cmds[i].delete_.wim_path);
648                                 break;
649                         case WIMLIB_UPDATE_OP_RENAME:
650                                 FREE(cmds[i].rename.wim_source_path);
651                                 FREE(cmds[i].rename.wim_target_path);
652                                 break;
653                         }
654                 }
655                 FREE(cmds);
656         }
657 }
658
659 static int
660 copy_update_commands(const struct wimlib_update_command *cmds,
661                      size_t num_cmds,
662                      struct wimlib_update_command **cmds_copy_ret)
663 {
664         int ret;
665         struct wimlib_update_command *cmds_copy;
666         const struct wimlib_capture_config *config;
667
668         cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
669         if (!cmds_copy)
670                 goto oom;
671
672         for (size_t i = 0; i < num_cmds; i++) {
673                 cmds_copy[i].op = cmds[i].op;
674                 switch (cmds[i].op) {
675                 case WIMLIB_UPDATE_OP_ADD:
676                         cmds_copy[i].add.fs_source_path =
677                                 canonicalize_fs_path(cmds[i].add.fs_source_path);
678                         cmds_copy[i].add.wim_target_path =
679                                 canonicalize_wim_path(cmds[i].add.wim_target_path);
680                         if (!cmds_copy[i].add.fs_source_path ||
681                             !cmds_copy[i].add.wim_target_path)
682                                 goto oom;
683                         config = cmds[i].add.config;
684                         if (cmds[i].add.add_flags & WIMLIB_ADD_FLAG_WINCONFIG)
685                                 config = &winconfig;
686                         if (config) {
687                                 ret = copy_and_canonicalize_capture_config(config,
688                                                                            &cmds_copy[i].add.config);
689                                 if (ret)
690                                         goto err;
691                         }
692                         cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
693                         break;
694                 case WIMLIB_UPDATE_OP_DELETE:
695                         cmds_copy[i].delete_.wim_path =
696                                 canonicalize_wim_path(cmds[i].delete_.wim_path);
697                         if (!cmds_copy[i].delete_.wim_path)
698                                 goto oom;
699                         cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
700                         break;
701                 case WIMLIB_UPDATE_OP_RENAME:
702                         cmds_copy[i].rename.wim_source_path =
703                                 canonicalize_wim_path(cmds[i].rename.wim_source_path);
704                         cmds_copy[i].rename.wim_target_path =
705                                 canonicalize_wim_path(cmds[i].rename.wim_target_path);
706                         if (!cmds_copy[i].rename.wim_source_path ||
707                             !cmds_copy[i].rename.wim_target_path)
708                                 goto oom;
709                         break;
710                 default:
711                         ERROR("Unknown update operation %u", cmds[i].op);
712                         ret = WIMLIB_ERR_INVALID_PARAM;
713                         goto err;
714                 }
715         }
716         *cmds_copy_ret = cmds_copy;
717         ret = 0;
718 out:
719         return ret;
720 oom:
721         ret = WIMLIB_ERR_NOMEM;
722 err:
723         free_update_commands(cmds_copy, num_cmds);
724         goto out;
725 }
726
727 /* API function documented in wimlib.h  */
728 WIMLIBAPI int
729 wimlib_update_image(WIMStruct *wim,
730                     int image,
731                     const struct wimlib_update_command *cmds,
732                     size_t num_cmds,
733                     int update_flags,
734                     wimlib_progress_func_t progress_func)
735 {
736         int ret;
737         struct wimlib_update_command *cmds_copy;
738         bool deletion_requested = false;
739
740         DEBUG("Updating image %d with %zu commands", image, num_cmds);
741
742         for (size_t i = 0; i < num_cmds; i++)
743                 if (cmds[i].op == WIMLIB_UPDATE_OP_DELETE)
744                         deletion_requested = true;
745
746         if (deletion_requested)
747                 ret = can_delete_from_wim(wim);
748         else
749                 ret = can_modify_wim(wim);
750
751         if (ret)
752                 goto out;
753
754         /* Load the metadata for the image to modify (if not loaded already) */
755         ret = select_wim_image(wim, image);
756         if (ret)
757                 goto out;
758
759         /* Short circuit a successful return if no commands were specified.
760          * Avoids problems with trying to allocate 0 bytes of memory. */
761         if (num_cmds == 0)
762                 goto out;
763
764         DEBUG("Preparing %zu update commands", num_cmds);
765
766         /* Make a copy of the update commands, in the process doing certain
767          * canonicalizations on paths (e.g. translating backslashes to forward
768          * slashes).  This is done to avoid modifying the caller's copy of the
769          * commands. */
770         ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
771         if (ret)
772                 goto out;
773
774         /* Perform additional checks on the update commands before we execute
775          * them. */
776         ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
777         if (ret)
778                 goto out_free_cmds_copy;
779
780         /* Actually execute the update commands. */
781         DEBUG("Executing %zu update commands", num_cmds);
782         ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
783                                       progress_func);
784         if (ret)
785                 goto out_free_cmds_copy;
786
787         wim->image_metadata[image - 1]->modified = 1;
788
789         /* Statistics about the WIM image, such as the numbers of files and
790          * directories, may have changed.  Call xml_update_image_info() to
791          * recalculate these statistics. */
792         xml_update_image_info(wim, image);
793 out_free_cmds_copy:
794         free_update_commands(cmds_copy, num_cmds);
795 out:
796         return ret;
797 }