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