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