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