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