]> wimlib.net Git - wimlib/blob - src/update_image.c
Add a new flag for capturing WIMBoot-compatible WIM
[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         DEBUG("Doing overlay \"%"WS"\" => \"%"WS"\"",
46               branch->file_name, target->file_name);
47
48         if (!dentry_is_directory(branch) || !dentry_is_directory(target)) {
49                 ERROR("Cannot overlay \"%"WS"\" onto existing dentry: "
50                       "is not directory-on-directory!", branch->file_name);
51                 return WIMLIB_ERR_INVALID_OVERLAY;
52         }
53
54         LIST_HEAD(moved_children);
55         while (dentry_has_children(branch)) {
56                 struct wim_dentry *child = dentry_any_child(branch);
57                 struct wim_dentry *existing;
58
59                 /* Move @child to the directory @target */
60                 unlink_dentry(child);
61                 existing = dentry_add_child(target, child);
62
63                 /* File or directory with same name already exists */
64                 if (existing) {
65                         int ret;
66                         ret = do_overlay(existing, child);
67                         if (ret) {
68                                 /* Overlay failed.  Revert the changes. */
69                                 dentry_add_child(branch, child);
70                                 list_for_each_entry(child, &moved_children, tmp_list)
71                                 {
72                                         unlink_dentry(child);
73                                         dentry_add_child(branch, child);
74                                 }
75                                 return ret;
76                         }
77                 } else {
78                         list_add(&child->tmp_list, &moved_children);
79                 }
80         }
81         free_dentry(branch);
82         return 0;
83 }
84
85 /* Attach or overlay a branch onto the WIM image.
86  *
87  * @root_p:
88  *      Pointer to the root of the WIM image, or pointer to NULL if it has not
89  *      been created yet.
90  * @branch
91  *      Branch to add.
92  * @target_path:
93  *      Path in the WIM image to add the branch, with leading and trailing
94  *      slashes stripped.
95  */
96 static int
97 attach_branch(struct wim_dentry **root_p, struct wim_dentry *branch,
98               tchar *target_path, CASE_SENSITIVITY_TYPE case_type)
99 {
100         tchar *slash;
101         struct wim_dentry *dentry, *parent, *target;
102         int ret;
103
104         DEBUG("Attaching branch \"%"WS"\" => \"%"TS"\"",
105               branch->file_name, target_path);
106
107         if (*target_path == T('\0')) {
108                 /* Target: root directory */
109                 if (*root_p) {
110                         /* Overlay on existing root */
111                         return do_overlay(*root_p, branch);
112                 } else {
113                         if (!dentry_is_directory(branch)) {
114                                 ERROR("Cannot set non-directory as root of WIM image");
115                                 return WIMLIB_ERR_NOTDIR;
116                         }
117                         /* Set as root */
118                         *root_p = branch;
119                         return 0;
120                 }
121         }
122
123         /* Adding a non-root branch.  Create root if it hasn't been created
124          * already. */
125         if (!*root_p) {
126                 ret  = new_filler_directory(T(""), root_p);
127                 if (ret)
128                         return ret;
129         }
130
131         /* Walk the path to the branch, creating filler directories as needed.
132          * */
133         parent = *root_p;
134         while ((slash = tstrchr(target_path, WIM_PATH_SEPARATOR))) {
135                 *slash = T('\0');
136                 dentry = get_dentry_child_with_name(parent, target_path,
137                                                     case_type);
138                 if (!dentry) {
139                         ret = new_filler_directory(target_path, &dentry);
140                         if (ret)
141                                 return ret;
142                         dentry_add_child(parent, dentry);
143                 }
144                 parent = dentry;
145                 target_path = slash;
146                 /* Skip over slashes.  Note: this cannot overrun the length of
147                  * the string because the last character cannot be a slash, as
148                  * trailing slashes were tripped.  */
149                 do {
150                         ++target_path;
151                 } while (*target_path == WIM_PATH_SEPARATOR);
152         }
153
154         /* If the target path already existed, overlay the branch onto it.
155          * Otherwise, set the branch as the target path. */
156         target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
157                                                     branch->file_name_nbytes,
158                                                     case_type);
159         if (target) {
160                 return do_overlay(target, branch);
161         } else {
162                 dentry_add_child(parent, branch);
163                 return 0;
164         }
165 }
166
167 static int
168 execute_add_command(WIMStruct *wim,
169                     const struct wimlib_update_command *add_cmd,
170                     wimlib_progress_func_t progress_func)
171 {
172         int ret;
173         int add_flags;
174         tchar *fs_source_path;
175         tchar *wim_target_path;
176         struct wim_image_metadata *imd;
177         struct list_head unhashed_streams;
178         struct add_image_params params;
179         int (*capture_tree)(struct wim_dentry **,
180                             const tchar *,
181                             struct add_image_params *);
182         struct wimlib_capture_config *config;
183 #ifdef WITH_NTFS_3G
184         struct _ntfs_volume *ntfs_vol = NULL;
185 #endif
186         void *extra_arg;
187         struct wim_dentry *branch;
188         bool rollback_sd = true;
189
190         wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
191
192         add_flags = add_cmd->add.add_flags;
193         fs_source_path = add_cmd->add.fs_source_path;
194         wim_target_path = add_cmd->add.wim_target_path;
195         config = add_cmd->add.config;
196         DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
197               fs_source_path, wim_target_path, add_flags);
198
199         memset(&params, 0, sizeof(params));
200
201         imd = wim->image_metadata[wim->current_image - 1];
202
203         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
204         #ifdef WITH_NTFS_3G
205                 capture_tree = build_dentry_tree_ntfs;
206                 extra_arg = &ntfs_vol;
207                 if (imd->ntfs_vol != NULL) {
208                         ERROR("NTFS volume already set");
209                         ret = WIMLIB_ERR_INVALID_PARAM;
210                         goto out;
211                 }
212         #else
213                 ret = WIMLIB_ERR_INVALID_PARAM;
214                 goto out;
215         #endif
216         } else {
217         #ifdef __WIN32__
218                 capture_tree = win32_build_dentry_tree;
219         #else
220                 capture_tree = unix_build_dentry_tree;
221         #endif
222                 extra_arg = NULL;
223         }
224
225
226         ret = init_inode_table(&params.inode_table, 9001);
227         if (ret)
228                 goto out;
229
230         ret = init_sd_set(&params.sd_set, imd->security_data);
231         if (ret)
232                 goto out_destroy_inode_table;
233
234         INIT_LIST_HEAD(&unhashed_streams);
235         params.lookup_table = wim->lookup_table;
236         params.unhashed_streams = &unhashed_streams;
237         params.config = config;
238         params.add_flags = add_flags;
239         params.extra_arg = extra_arg;
240
241         params.progress_func = progress_func;
242         params.progress.scan.source = fs_source_path;
243         params.progress.scan.wim_target_path = wim_target_path;
244         if (progress_func)
245                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &params.progress);
246         if (config) {
247                 config->_prefix = fs_source_path;
248                 config->_prefix_num_tchars = tstrlen(fs_source_path);
249         }
250
251         if (wim_target_path[0] == T('\0'))
252                 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
253         ret = (*capture_tree)(&branch, fs_source_path, &params);
254         if (ret)
255                 goto out_destroy_sd_set;
256
257         if (branch) {
258                 /* Use the target name, not the source name, for
259                  * the root of each branch from a capture
260                  * source.  (This will also set the root dentry
261                  * of the entire image to be unnamed.) */
262                 ret = dentry_set_name(branch,
263                                       path_basename(wim_target_path));
264                 if (ret)
265                         goto out_ntfs_umount;
266
267                 ret = attach_branch(&imd->root_dentry, branch, wim_target_path,
268                                     WIMLIB_CASE_PLATFORM_DEFAULT);
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, WIMLIB_CASE_PLATFORM_DEFAULT);
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 execute_rename_command(WIMStruct *wim,
342                        const struct wimlib_update_command *rename_cmd)
343 {
344         int ret;
345
346         wimlib_assert(rename_cmd->op == WIMLIB_UPDATE_OP_RENAME);
347
348         ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
349                               rename_cmd->rename.wim_target_path,
350                               WIMLIB_CASE_PLATFORM_DEFAULT);
351         if (ret) {
352                 ret = -ret;
353                 errno = ret;
354                 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
355                                  rename_cmd->rename.wim_source_path,
356                                  rename_cmd->rename.wim_target_path);
357                 switch (ret) {
358                 case ENOMEM:
359                         ret = WIMLIB_ERR_NOMEM;
360                         break;
361                 case ENOTDIR:
362                         ret = WIMLIB_ERR_NOTDIR;
363                         break;
364                 case ENOTEMPTY:
365                         ret = WIMLIB_ERR_NOTEMPTY;
366                         break;
367                 case EISDIR:
368                         ret = WIMLIB_ERR_IS_DIRECTORY;
369                         break;
370                 case ENOENT:
371                 default:
372                         ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
373                         break;
374                 }
375         }
376         return ret;
377 }
378
379 static inline const tchar *
380 update_op_to_str(int op)
381 {
382         switch (op) {
383         case WIMLIB_UPDATE_OP_ADD:
384                 return T("add");
385         case WIMLIB_UPDATE_OP_DELETE:
386                 return T("delete");
387         case WIMLIB_UPDATE_OP_RENAME:
388                 return T("rename");
389         default:
390                 wimlib_assert(0);
391                 return NULL;
392         }
393 }
394
395 static int
396 execute_update_commands(WIMStruct *wim,
397                         const struct wimlib_update_command *cmds,
398                         size_t num_cmds,
399                         int update_flags,
400                         wimlib_progress_func_t progress_func)
401 {
402         int ret = 0;
403         union wimlib_progress_info info;
404         info.update.completed_commands = 0;
405         info.update.total_commands = num_cmds;
406         for (size_t i = 0; i < num_cmds; i++) {
407                 DEBUG("Executing update command %zu of %zu (op=%"TS")",
408                       i + 1, num_cmds, update_op_to_str(cmds[i].op));
409                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
410                     progress_func)
411                 {
412                         info.update.command = &cmds[i];
413                         (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
414                                          &info);
415                 }
416                 switch (cmds[i].op) {
417                 case WIMLIB_UPDATE_OP_ADD:
418                         ret = execute_add_command(wim, &cmds[i], progress_func);
419                         break;
420                 case WIMLIB_UPDATE_OP_DELETE:
421                         ret = execute_delete_command(wim, &cmds[i]);
422                         break;
423                 case WIMLIB_UPDATE_OP_RENAME:
424                         ret = execute_rename_command(wim, &cmds[i]);
425                         break;
426                 default:
427                         wimlib_assert(0);
428                 }
429                 if (ret)
430                         break;
431                 info.update.completed_commands++;
432                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
433                     progress_func)
434                 {
435                         (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
436                                          &info);
437                 }
438         }
439         return ret;
440 }
441
442
443 tchar *winpats[] = {
444         T("/$ntfs.log"),
445         T("/hiberfil.sys"),
446         T("/pagefile.sys"),
447         T("/System Volume Information"),
448         T("/RECYCLER"),
449         T("/Windows/CSC"),
450 };
451
452 static const struct wimlib_capture_config winconfig = {
453         .exclusion_pats = {
454                 .num_pats = ARRAY_LEN(winpats),
455                 .pats = winpats,
456         },
457 };
458
459 static int
460 check_add_command(struct wimlib_update_command *cmd,
461                   const struct wim_header *hdr)
462 {
463         int add_flags = cmd->add.add_flags;
464
465         if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
466                           WIMLIB_ADD_FLAG_DEREFERENCE |
467                           WIMLIB_ADD_FLAG_VERBOSE |
468                           /* BOOT doesn't make sense for wimlib_update_image().
469                            * Same with WIMBOOT.  */
470                           /*WIMLIB_ADD_FLAG_BOOT |*/
471                           /*WIMLIB_ADD_FLAG_WIMBOOT |*/
472                           WIMLIB_ADD_FLAG_UNIX_DATA |
473                           WIMLIB_ADD_FLAG_NO_ACLS |
474                           WIMLIB_ADD_FLAG_STRICT_ACLS |
475                           WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
476                           WIMLIB_ADD_FLAG_RPFIX |
477                           WIMLIB_ADD_FLAG_NORPFIX |
478                           WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
479                           WIMLIB_ADD_FLAG_WINCONFIG))
480                 return WIMLIB_ERR_INVALID_PARAM;
481
482         /* Are we adding the entire image or not?  An empty wim_target_path
483          * indicates that the tree we're adding is to be placed in the root of
484          * the image.  We consider this to be capturing the entire image,
485          * although it could potentially be an overlay on an existing root as
486          * well. */
487         bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
488
489 #ifdef __WIN32__
490         /* Check for flags not supported on Windows */
491         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
492                 ERROR("wimlib was compiled without support for NTFS-3g, so");
493                 ERROR("we cannot capture a WIM image directly from a NTFS volume");
494                 return WIMLIB_ERR_UNSUPPORTED;
495         }
496         if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
497                 ERROR("Capturing UNIX-specific data is not supported on Windows");
498                 return WIMLIB_ERR_UNSUPPORTED;
499         }
500         if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
501                 ERROR("Dereferencing symbolic links is not supported on Windows");
502                 return WIMLIB_ERR_UNSUPPORTED;
503         }
504 #endif
505
506         /* VERBOSE implies EXCLUDE_VERBOSE */
507         if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
508                 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
509
510         /* Check for contradictory reparse point fixup flags */
511         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
512                           WIMLIB_ADD_FLAG_NORPFIX)) ==
513                 (WIMLIB_ADD_FLAG_RPFIX |
514                  WIMLIB_ADD_FLAG_NORPFIX))
515         {
516                 ERROR("Cannot specify RPFIX and NORPFIX flags "
517                       "at the same time!");
518                 return WIMLIB_ERR_INVALID_PARAM;
519         }
520
521         /* Set default behavior on reparse point fixups if requested */
522         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
523                           WIMLIB_ADD_FLAG_NORPFIX)) == 0)
524         {
525                 /* Do reparse-point fixups by default if we are capturing an
526                  * entire image and either the header flag is set from previous
527                  * images, or if this is the first image being added. */
528                 if (is_entire_image &&
529                     ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
530                         add_flags |= WIMLIB_ADD_FLAG_RPFIX;
531         }
532
533         if (!is_entire_image) {
534                 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
535                         ERROR("Cannot add directly from a NTFS volume "
536                               "when not capturing a full image!");
537                         return WIMLIB_ERR_INVALID_PARAM;
538                 }
539
540                 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
541                         ERROR("Cannot do reparse point fixups when "
542                               "not capturing a full image!");
543                         return WIMLIB_ERR_INVALID_PARAM;
544                 }
545         }
546         /* We may have modified the add flags. */
547         cmd->add.add_flags = add_flags;
548         return 0;
549 }
550
551 static int
552 check_delete_command(const struct wimlib_update_command *cmd)
553 {
554         if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
555                                           WIMLIB_DELETE_FLAG_RECURSIVE))
556                 return WIMLIB_ERR_INVALID_PARAM;
557         return 0;
558 }
559
560 static int
561 check_rename_command(const struct wimlib_update_command *cmd)
562 {
563         if (cmd->rename.rename_flags != 0)
564                 return WIMLIB_ERR_INVALID_PARAM;
565         return 0;
566 }
567
568 static int
569 check_update_command(struct wimlib_update_command *cmd,
570                      const struct wim_header *hdr)
571 {
572         switch (cmd->op) {
573         case WIMLIB_UPDATE_OP_ADD:
574                 return check_add_command(cmd, hdr);
575         case WIMLIB_UPDATE_OP_DELETE:
576                 return check_delete_command(cmd);
577         case WIMLIB_UPDATE_OP_RENAME:
578                 return check_rename_command(cmd);
579         }
580         return 0;
581 }
582
583 static int
584 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
585                       const struct wim_header *hdr)
586 {
587         int ret = 0;
588         for (size_t i = 0; i < num_cmds; i++) {
589                 ret = check_update_command(&cmds[i], hdr);
590                 if (ret)
591                         break;
592         }
593         return ret;
594 }
595
596
597 static void
598 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
599 {
600         if (cmds) {
601                 for (size_t i = 0; i < num_cmds; i++) {
602                         switch (cmds[i].op) {
603                         case WIMLIB_UPDATE_OP_ADD:
604                                 FREE(cmds[i].add.fs_source_path);
605                                 FREE(cmds[i].add.wim_target_path);
606                                 free_capture_config(cmds[i].add.config);
607                                 break;
608                         case WIMLIB_UPDATE_OP_DELETE:
609                                 FREE(cmds[i].delete_.wim_path);
610                                 break;
611                         case WIMLIB_UPDATE_OP_RENAME:
612                                 FREE(cmds[i].rename.wim_source_path);
613                                 FREE(cmds[i].rename.wim_target_path);
614                                 break;
615                         }
616                 }
617                 FREE(cmds);
618         }
619 }
620
621 static int
622 copy_update_commands(const struct wimlib_update_command *cmds,
623                      size_t num_cmds,
624                      struct wimlib_update_command **cmds_copy_ret)
625 {
626         int ret;
627         struct wimlib_update_command *cmds_copy;
628         const struct wimlib_capture_config *config;
629
630         cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
631         if (!cmds_copy)
632                 goto oom;
633
634         for (size_t i = 0; i < num_cmds; i++) {
635                 cmds_copy[i].op = cmds[i].op;
636                 switch (cmds[i].op) {
637                 case WIMLIB_UPDATE_OP_ADD:
638                         cmds_copy[i].add.fs_source_path =
639                                 canonicalize_fs_path(cmds[i].add.fs_source_path);
640                         cmds_copy[i].add.wim_target_path =
641                                 canonicalize_wim_path(cmds[i].add.wim_target_path);
642                         if (!cmds_copy[i].add.fs_source_path ||
643                             !cmds_copy[i].add.wim_target_path)
644                                 goto oom;
645                         config = cmds[i].add.config;
646                         if (cmds[i].add.add_flags & WIMLIB_ADD_FLAG_WINCONFIG)
647                                 config = &winconfig;
648                         if (config) {
649                                 ret = copy_and_canonicalize_capture_config(config,
650                                                                            &cmds_copy[i].add.config);
651                                 if (ret)
652                                         goto err;
653                         }
654                         cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
655                         break;
656                 case WIMLIB_UPDATE_OP_DELETE:
657                         cmds_copy[i].delete_.wim_path =
658                                 canonicalize_wim_path(cmds[i].delete_.wim_path);
659                         if (!cmds_copy[i].delete_.wim_path)
660                                 goto oom;
661                         cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
662                         break;
663                 case WIMLIB_UPDATE_OP_RENAME:
664                         cmds_copy[i].rename.wim_source_path =
665                                 canonicalize_wim_path(cmds[i].rename.wim_source_path);
666                         cmds_copy[i].rename.wim_target_path =
667                                 canonicalize_wim_path(cmds[i].rename.wim_target_path);
668                         if (!cmds_copy[i].rename.wim_source_path ||
669                             !cmds_copy[i].rename.wim_target_path)
670                                 goto oom;
671                         break;
672                 default:
673                         ERROR("Unknown update operation %u", cmds[i].op);
674                         ret = WIMLIB_ERR_INVALID_PARAM;
675                         goto err;
676                 }
677         }
678         *cmds_copy_ret = cmds_copy;
679         ret = 0;
680 out:
681         return ret;
682 oom:
683         ret = WIMLIB_ERR_NOMEM;
684 err:
685         free_update_commands(cmds_copy, num_cmds);
686         goto out;
687 }
688
689 /* API function documented in wimlib.h  */
690 WIMLIBAPI int
691 wimlib_update_image(WIMStruct *wim,
692                     int image,
693                     const struct wimlib_update_command *cmds,
694                     size_t num_cmds,
695                     int update_flags,
696                     wimlib_progress_func_t progress_func)
697 {
698         int ret;
699         struct wimlib_update_command *cmds_copy;
700         bool deletion_requested = false;
701
702         if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
703                 return WIMLIB_ERR_INVALID_PARAM;
704
705         DEBUG("Updating image %d with %zu commands", image, num_cmds);
706
707         for (size_t i = 0; i < num_cmds; i++)
708                 if (cmds[i].op == WIMLIB_UPDATE_OP_DELETE)
709                         deletion_requested = true;
710
711         if (deletion_requested)
712                 ret = can_delete_from_wim(wim);
713         else
714                 ret = can_modify_wim(wim);
715
716         if (ret)
717                 goto out;
718
719         /* Load the metadata for the image to modify (if not loaded already) */
720         ret = select_wim_image(wim, image);
721         if (ret)
722                 goto out;
723
724         /* Short circuit a successful return if no commands were specified.
725          * Avoids problems with trying to allocate 0 bytes of memory. */
726         if (num_cmds == 0)
727                 goto out;
728
729         DEBUG("Preparing %zu update commands", num_cmds);
730
731         /* Make a copy of the update commands, in the process doing certain
732          * canonicalizations on paths (e.g. translating backslashes to forward
733          * slashes).  This is done to avoid modifying the caller's copy of the
734          * commands. */
735         ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
736         if (ret)
737                 goto out;
738
739         /* Perform additional checks on the update commands before we execute
740          * them. */
741         ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
742         if (ret)
743                 goto out_free_cmds_copy;
744
745         /* Actually execute the update commands. */
746         DEBUG("Executing %zu update commands", num_cmds);
747         ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
748                                       progress_func);
749         if (ret)
750                 goto out_free_cmds_copy;
751
752         wim->image_metadata[image - 1]->modified = 1;
753
754         /* Statistics about the WIM image, such as the numbers of files and
755          * directories, may have changed.  Call xml_update_image_info() to
756          * recalculate these statistics. */
757         xml_update_image_info(wim, image);
758 out_free_cmds_copy:
759         free_update_commands(cmds_copy, num_cmds);
760 out:
761         return ret;
762 }