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