]> wimlib.net Git - wimlib/blob - src/update_image.c
Transition wimlib to AVL tree code
[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                           /*WIMLIB_ADD_FLAG_BOOT |*/
470                           WIMLIB_ADD_FLAG_UNIX_DATA |
471                           WIMLIB_ADD_FLAG_NO_ACLS |
472                           WIMLIB_ADD_FLAG_STRICT_ACLS |
473                           WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
474                           WIMLIB_ADD_FLAG_RPFIX |
475                           WIMLIB_ADD_FLAG_NORPFIX |
476                           WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
477                           WIMLIB_ADD_FLAG_WINCONFIG))
478                 return WIMLIB_ERR_INVALID_PARAM;
479
480         /* Are we adding the entire image or not?  An empty wim_target_path
481          * indicates that the tree we're adding is to be placed in the root of
482          * the image.  We consider this to be capturing the entire image,
483          * although it could potentially be an overlay on an existing root as
484          * well. */
485         bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
486
487 #ifdef __WIN32__
488         /* Check for flags not supported on Windows */
489         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
490                 ERROR("wimlib was compiled without support for NTFS-3g, so");
491                 ERROR("we cannot capture a WIM image directly from a NTFS volume");
492                 return WIMLIB_ERR_UNSUPPORTED;
493         }
494         if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
495                 ERROR("Capturing UNIX-specific data is not supported on Windows");
496                 return WIMLIB_ERR_UNSUPPORTED;
497         }
498         if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
499                 ERROR("Dereferencing symbolic links is not supported on Windows");
500                 return WIMLIB_ERR_UNSUPPORTED;
501         }
502 #endif
503
504         /* VERBOSE implies EXCLUDE_VERBOSE */
505         if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
506                 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
507
508         /* Check for contradictory reparse point fixup flags */
509         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
510                           WIMLIB_ADD_FLAG_NORPFIX)) ==
511                 (WIMLIB_ADD_FLAG_RPFIX |
512                  WIMLIB_ADD_FLAG_NORPFIX))
513         {
514                 ERROR("Cannot specify RPFIX and NORPFIX flags "
515                       "at the same time!");
516                 return WIMLIB_ERR_INVALID_PARAM;
517         }
518
519         /* Set default behavior on reparse point fixups if requested */
520         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
521                           WIMLIB_ADD_FLAG_NORPFIX)) == 0)
522         {
523                 /* Do reparse-point fixups by default if we are capturing an
524                  * entire image and either the header flag is set from previous
525                  * images, or if this is the first image being added. */
526                 if (is_entire_image &&
527                     ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
528                         add_flags |= WIMLIB_ADD_FLAG_RPFIX;
529         }
530
531         if (!is_entire_image) {
532                 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
533                         ERROR("Cannot add directly from a NTFS volume "
534                               "when not capturing a full image!");
535                         return WIMLIB_ERR_INVALID_PARAM;
536                 }
537
538                 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
539                         ERROR("Cannot do reparse point fixups when "
540                               "not capturing a full image!");
541                         return WIMLIB_ERR_INVALID_PARAM;
542                 }
543         }
544         /* We may have modified the add flags. */
545         cmd->add.add_flags = add_flags;
546         return 0;
547 }
548
549 static int
550 check_delete_command(const struct wimlib_update_command *cmd)
551 {
552         if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
553                                           WIMLIB_DELETE_FLAG_RECURSIVE))
554                 return WIMLIB_ERR_INVALID_PARAM;
555         return 0;
556 }
557
558 static int
559 check_rename_command(const struct wimlib_update_command *cmd)
560 {
561         if (cmd->rename.rename_flags != 0)
562                 return WIMLIB_ERR_INVALID_PARAM;
563         return 0;
564 }
565
566 static int
567 check_update_command(struct wimlib_update_command *cmd,
568                      const struct wim_header *hdr)
569 {
570         switch (cmd->op) {
571         case WIMLIB_UPDATE_OP_ADD:
572                 return check_add_command(cmd, hdr);
573         case WIMLIB_UPDATE_OP_DELETE:
574                 return check_delete_command(cmd);
575         case WIMLIB_UPDATE_OP_RENAME:
576                 return check_rename_command(cmd);
577         }
578         return 0;
579 }
580
581 static int
582 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
583                       const struct wim_header *hdr)
584 {
585         int ret = 0;
586         for (size_t i = 0; i < num_cmds; i++) {
587                 ret = check_update_command(&cmds[i], hdr);
588                 if (ret)
589                         break;
590         }
591         return ret;
592 }
593
594
595 static void
596 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
597 {
598         if (cmds) {
599                 for (size_t i = 0; i < num_cmds; i++) {
600                         switch (cmds[i].op) {
601                         case WIMLIB_UPDATE_OP_ADD:
602                                 FREE(cmds[i].add.fs_source_path);
603                                 FREE(cmds[i].add.wim_target_path);
604                                 free_capture_config(cmds[i].add.config);
605                                 break;
606                         case WIMLIB_UPDATE_OP_DELETE:
607                                 FREE(cmds[i].delete_.wim_path);
608                                 break;
609                         case WIMLIB_UPDATE_OP_RENAME:
610                                 FREE(cmds[i].rename.wim_source_path);
611                                 FREE(cmds[i].rename.wim_target_path);
612                                 break;
613                         }
614                 }
615                 FREE(cmds);
616         }
617 }
618
619 static int
620 copy_update_commands(const struct wimlib_update_command *cmds,
621                      size_t num_cmds,
622                      struct wimlib_update_command **cmds_copy_ret)
623 {
624         int ret;
625         struct wimlib_update_command *cmds_copy;
626         const struct wimlib_capture_config *config;
627
628         cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
629         if (!cmds_copy)
630                 goto oom;
631
632         for (size_t i = 0; i < num_cmds; i++) {
633                 cmds_copy[i].op = cmds[i].op;
634                 switch (cmds[i].op) {
635                 case WIMLIB_UPDATE_OP_ADD:
636                         cmds_copy[i].add.fs_source_path =
637                                 canonicalize_fs_path(cmds[i].add.fs_source_path);
638                         cmds_copy[i].add.wim_target_path =
639                                 canonicalize_wim_path(cmds[i].add.wim_target_path);
640                         if (!cmds_copy[i].add.fs_source_path ||
641                             !cmds_copy[i].add.wim_target_path)
642                                 goto oom;
643                         config = cmds[i].add.config;
644                         if (cmds[i].add.add_flags & WIMLIB_ADD_FLAG_WINCONFIG)
645                                 config = &winconfig;
646                         if (config) {
647                                 ret = copy_and_canonicalize_capture_config(config,
648                                                                            &cmds_copy[i].add.config);
649                                 if (ret)
650                                         goto err;
651                         }
652                         cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
653                         break;
654                 case WIMLIB_UPDATE_OP_DELETE:
655                         cmds_copy[i].delete_.wim_path =
656                                 canonicalize_wim_path(cmds[i].delete_.wim_path);
657                         if (!cmds_copy[i].delete_.wim_path)
658                                 goto oom;
659                         cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
660                         break;
661                 case WIMLIB_UPDATE_OP_RENAME:
662                         cmds_copy[i].rename.wim_source_path =
663                                 canonicalize_wim_path(cmds[i].rename.wim_source_path);
664                         cmds_copy[i].rename.wim_target_path =
665                                 canonicalize_wim_path(cmds[i].rename.wim_target_path);
666                         if (!cmds_copy[i].rename.wim_source_path ||
667                             !cmds_copy[i].rename.wim_target_path)
668                                 goto oom;
669                         break;
670                 default:
671                         ERROR("Unknown update operation %u", cmds[i].op);
672                         ret = WIMLIB_ERR_INVALID_PARAM;
673                         goto err;
674                 }
675         }
676         *cmds_copy_ret = cmds_copy;
677         ret = 0;
678 out:
679         return ret;
680 oom:
681         ret = WIMLIB_ERR_NOMEM;
682 err:
683         free_update_commands(cmds_copy, num_cmds);
684         goto out;
685 }
686
687 /* API function documented in wimlib.h  */
688 WIMLIBAPI int
689 wimlib_update_image(WIMStruct *wim,
690                     int image,
691                     const struct wimlib_update_command *cmds,
692                     size_t num_cmds,
693                     int update_flags,
694                     wimlib_progress_func_t progress_func)
695 {
696         int ret;
697         struct wimlib_update_command *cmds_copy;
698         bool deletion_requested = false;
699
700         if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
701                 return WIMLIB_ERR_INVALID_PARAM;
702
703         DEBUG("Updating image %d with %zu commands", image, num_cmds);
704
705         for (size_t i = 0; i < num_cmds; i++)
706                 if (cmds[i].op == WIMLIB_UPDATE_OP_DELETE)
707                         deletion_requested = true;
708
709         if (deletion_requested)
710                 ret = can_delete_from_wim(wim);
711         else
712                 ret = can_modify_wim(wim);
713
714         if (ret)
715                 goto out;
716
717         /* Load the metadata for the image to modify (if not loaded already) */
718         ret = select_wim_image(wim, image);
719         if (ret)
720                 goto out;
721
722         /* Short circuit a successful return if no commands were specified.
723          * Avoids problems with trying to allocate 0 bytes of memory. */
724         if (num_cmds == 0)
725                 goto out;
726
727         DEBUG("Preparing %zu update commands", num_cmds);
728
729         /* Make a copy of the update commands, in the process doing certain
730          * canonicalizations on paths (e.g. translating backslashes to forward
731          * slashes).  This is done to avoid modifying the caller's copy of the
732          * commands. */
733         ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
734         if (ret)
735                 goto out;
736
737         /* Perform additional checks on the update commands before we execute
738          * them. */
739         ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
740         if (ret)
741                 goto out_free_cmds_copy;
742
743         /* Actually execute the update commands. */
744         DEBUG("Executing %zu update commands", num_cmds);
745         ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
746                                       progress_func);
747         if (ret)
748                 goto out_free_cmds_copy;
749
750         wim->image_metadata[image - 1]->modified = 1;
751
752         /* Statistics about the WIM image, such as the numbers of files and
753          * directories, may have changed.  Call xml_update_image_info() to
754          * recalculate these statistics. */
755         xml_update_image_info(wim, image);
756 out_free_cmds_copy:
757         free_update_commands(cmds_copy, num_cmds);
758 out:
759         return ret;
760 }