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