wimlib: strict checks for unassigned flags
[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, CASE_SENSITIVITY_TYPE case_type)
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                                                     case_type);
141                 if (!dentry) {
142                         ret = new_filler_directory(target_path, &dentry);
143                         if (ret)
144                                 return ret;
145                         dentry_add_child(parent, dentry);
146                 }
147                 parent = dentry;
148                 target_path = slash;
149                 /* Skip over slashes.  Note: this cannot overrun the length of
150                  * the string because the last character cannot be a slash, as
151                  * trailing slashes were tripped.  */
152                 do {
153                         ++target_path;
154                 } while (*target_path == WIM_PATH_SEPARATOR);
155         }
156
157         /* If the target path already existed, overlay the branch onto it.
158          * Otherwise, set the branch as the target path. */
159         target = get_dentry_child_with_utf16le_name(parent, branch->file_name,
160                                                     branch->file_name_nbytes,
161                                                     case_type);
162         if (target) {
163                 return do_overlay(target, branch);
164         } else {
165                 dentry_add_child(parent, branch);
166                 return 0;
167         }
168 }
169
170 static int
171 execute_add_command(WIMStruct *wim,
172                     const struct wimlib_update_command *add_cmd,
173                     wimlib_progress_func_t progress_func)
174 {
175         int ret;
176         int add_flags;
177         tchar *fs_source_path;
178         tchar *wim_target_path;
179         struct wim_image_metadata *imd;
180         struct list_head unhashed_streams;
181         struct add_image_params params;
182         int (*capture_tree)(struct wim_dentry **,
183                             const tchar *,
184                             struct add_image_params *);
185         struct wimlib_capture_config *config;
186 #ifdef WITH_NTFS_3G
187         struct _ntfs_volume *ntfs_vol = NULL;
188 #endif
189         void *extra_arg;
190         struct wim_dentry *branch;
191         bool rollback_sd = true;
192
193         wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
194
195         add_flags = add_cmd->add.add_flags;
196         fs_source_path = add_cmd->add.fs_source_path;
197         wim_target_path = add_cmd->add.wim_target_path;
198         config = add_cmd->add.config;
199         DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
200               fs_source_path, wim_target_path, add_flags);
201
202         memset(&params, 0, sizeof(params));
203
204         imd = wim->image_metadata[wim->current_image - 1];
205
206         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
207         #ifdef WITH_NTFS_3G
208                 capture_tree = build_dentry_tree_ntfs;
209                 extra_arg = &ntfs_vol;
210                 if (imd->ntfs_vol != NULL) {
211                         ERROR("NTFS volume already set");
212                         ret = WIMLIB_ERR_INVALID_PARAM;
213                         goto out;
214                 }
215         #else
216                 ret = WIMLIB_ERR_INVALID_PARAM;
217                 goto out;
218         #endif
219         } else {
220         #ifdef __WIN32__
221                 capture_tree = win32_build_dentry_tree;
222         #else
223                 capture_tree = unix_build_dentry_tree;
224         #endif
225                 extra_arg = NULL;
226         }
227
228
229         ret = init_inode_table(&params.inode_table, 9001);
230         if (ret)
231                 goto out;
232
233         ret = init_sd_set(&params.sd_set, imd->security_data);
234         if (ret)
235                 goto out_destroy_inode_table;
236
237         INIT_LIST_HEAD(&unhashed_streams);
238         params.lookup_table = wim->lookup_table;
239         params.unhashed_streams = &unhashed_streams;
240         params.config = config;
241         params.add_flags = add_flags;
242         params.extra_arg = extra_arg;
243
244         params.progress_func = progress_func;
245         params.progress.scan.source = fs_source_path;
246         params.progress.scan.wim_target_path = wim_target_path;
247         if (progress_func)
248                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &params.progress);
249         if (config) {
250                 config->_prefix = fs_source_path;
251                 config->_prefix_num_tchars = tstrlen(fs_source_path);
252         }
253
254         if (wim_target_path[0] == T('\0'))
255                 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
256         ret = (*capture_tree)(&branch, fs_source_path, &params);
257         if (ret)
258                 goto out_destroy_sd_set;
259
260         if (branch) {
261                 /* Use the target name, not the source name, for
262                  * the root of each branch from a capture
263                  * source.  (This will also set the root dentry
264                  * of the entire image to be unnamed.) */
265                 ret = dentry_set_name(branch,
266                                       path_basename(wim_target_path));
267                 if (ret)
268                         goto out_ntfs_umount;
269
270                 ret = attach_branch(&imd->root_dentry, branch, wim_target_path,
271                                     WIMLIB_CASE_PLATFORM_DEFAULT);
272                 if (ret)
273                         goto out_ntfs_umount;
274         }
275         if (progress_func)
276                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &params.progress);
277         list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
278 #ifdef WITH_NTFS_3G
279         imd->ntfs_vol = ntfs_vol;
280 #endif
281         inode_table_prepare_inode_list(&params.inode_table, &imd->inode_list);
282         ret = 0;
283         rollback_sd = false;
284         if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
285                 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
286         goto out_destroy_sd_set;
287 out_ntfs_umount:
288 #ifdef WITH_NTFS_3G
289         if (ntfs_vol)
290                 do_ntfs_umount(ntfs_vol);
291 #endif
292         free_dentry_tree(branch, wim->lookup_table);
293 out_destroy_sd_set:
294         destroy_sd_set(&params.sd_set, rollback_sd);
295 out_destroy_inode_table:
296         destroy_inode_table(&params.inode_table);
297 out:
298         return ret;
299 }
300
301 static int
302 execute_delete_command(WIMStruct *wim,
303                        const struct wimlib_update_command *delete_cmd)
304 {
305         int flags;
306         const tchar *wim_path;
307         struct wim_dentry *tree;
308         bool is_root;
309
310         wimlib_assert(delete_cmd->op == WIMLIB_UPDATE_OP_DELETE);
311         flags = delete_cmd->delete_.delete_flags;
312         wim_path = delete_cmd->delete_.wim_path;
313
314         DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
315
316         tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
317         if (!tree) {
318                 /* Path to delete does not exist in the WIM. */
319                 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
320                         return 0;
321                 } else {
322                         ERROR("Path \"%"TS"\" does not exist in WIM image %d",
323                               wim_path, wim->current_image);
324                         return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
325                 }
326         }
327
328         if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
329                 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
330                       "but a recursive delete was not requested",
331                       wim_path, wim->current_image);
332                 return WIMLIB_ERR_IS_DIRECTORY;
333         }
334
335         is_root = dentry_is_root(tree);
336         unlink_dentry(tree);
337         free_dentry_tree(tree, wim->lookup_table);
338         if (is_root)
339                 wim->image_metadata[wim->current_image - 1]->root_dentry = NULL;
340         return 0;
341 }
342
343 static int
344 execute_rename_command(WIMStruct *wim,
345                        const struct wimlib_update_command *rename_cmd)
346 {
347         int ret;
348
349         wimlib_assert(rename_cmd->op == WIMLIB_UPDATE_OP_RENAME);
350
351         ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
352                               rename_cmd->rename.wim_target_path,
353                               WIMLIB_CASE_PLATFORM_DEFAULT);
354         if (ret) {
355                 ret = -ret;
356                 errno = ret;
357                 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
358                                  rename_cmd->rename.wim_source_path,
359                                  rename_cmd->rename.wim_target_path);
360                 switch (ret) {
361                 case ENOMEM:
362                         ret = WIMLIB_ERR_NOMEM;
363                         break;
364                 case ENOTDIR:
365                         ret = WIMLIB_ERR_NOTDIR;
366                         break;
367                 case ENOTEMPTY:
368                         ret = WIMLIB_ERR_NOTEMPTY;
369                         break;
370                 case EISDIR:
371                         ret = WIMLIB_ERR_IS_DIRECTORY;
372                         break;
373                 case ENOENT:
374                 default:
375                         ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
376                         break;
377                 }
378         }
379         return ret;
380 }
381
382 static inline const tchar *
383 update_op_to_str(int op)
384 {
385         switch (op) {
386         case WIMLIB_UPDATE_OP_ADD:
387                 return T("add");
388         case WIMLIB_UPDATE_OP_DELETE:
389                 return T("delete");
390         case WIMLIB_UPDATE_OP_RENAME:
391                 return T("rename");
392         default:
393                 wimlib_assert(0);
394                 return NULL;
395         }
396 }
397
398 static int
399 execute_update_commands(WIMStruct *wim,
400                         const struct wimlib_update_command *cmds,
401                         size_t num_cmds,
402                         int update_flags,
403                         wimlib_progress_func_t progress_func)
404 {
405         int ret = 0;
406         union wimlib_progress_info info;
407         info.update.completed_commands = 0;
408         info.update.total_commands = num_cmds;
409         for (size_t i = 0; i < num_cmds; i++) {
410                 DEBUG("Executing update command %zu of %zu (op=%"TS")",
411                       i + 1, num_cmds, update_op_to_str(cmds[i].op));
412                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
413                     progress_func)
414                 {
415                         info.update.command = &cmds[i];
416                         (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
417                                          &info);
418                 }
419                 switch (cmds[i].op) {
420                 case WIMLIB_UPDATE_OP_ADD:
421                         ret = execute_add_command(wim, &cmds[i], progress_func);
422                         break;
423                 case WIMLIB_UPDATE_OP_DELETE:
424                         ret = execute_delete_command(wim, &cmds[i]);
425                         break;
426                 case WIMLIB_UPDATE_OP_RENAME:
427                         ret = execute_rename_command(wim, &cmds[i]);
428                         break;
429                 default:
430                         wimlib_assert(0);
431                 }
432                 if (ret)
433                         break;
434                 info.update.completed_commands++;
435                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
436                     progress_func)
437                 {
438                         (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
439                                          &info);
440                 }
441         }
442         return ret;
443 }
444
445
446 tchar *winpats[] = {
447         T("/$ntfs.log"),
448         T("/hiberfil.sys"),
449         T("/pagefile.sys"),
450         T("/System Volume Information"),
451         T("/RECYCLER"),
452         T("/Windows/CSC"),
453 };
454
455 static const struct wimlib_capture_config winconfig = {
456         .exclusion_pats = {
457                 .num_pats = ARRAY_LEN(winpats),
458                 .pats = winpats,
459         },
460 };
461
462 static int
463 check_add_command(struct wimlib_update_command *cmd,
464                   const struct wim_header *hdr)
465 {
466         int add_flags = cmd->add.add_flags;
467
468         if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
469                           WIMLIB_ADD_FLAG_DEREFERENCE |
470                           WIMLIB_ADD_FLAG_VERBOSE |
471                           /* BOOT doesn't make sense for wimlib_update_image()  */
472                           /*WIMLIB_ADD_FLAG_BOOT |*/
473                           WIMLIB_ADD_FLAG_UNIX_DATA |
474                           WIMLIB_ADD_FLAG_NO_ACLS |
475                           WIMLIB_ADD_FLAG_STRICT_ACLS |
476                           WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
477                           WIMLIB_ADD_FLAG_RPFIX |
478                           WIMLIB_ADD_FLAG_NORPFIX |
479                           WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
480                           WIMLIB_ADD_FLAG_WINCONFIG))
481                 return WIMLIB_ERR_INVALID_PARAM;
482
483         /* Are we adding the entire image or not?  An empty wim_target_path
484          * indicates that the tree we're adding is to be placed in the root of
485          * the image.  We consider this to be capturing the entire image,
486          * although it could potentially be an overlay on an existing root as
487          * well. */
488         bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
489
490 #ifdef __WIN32__
491         /* Check for flags not supported on Windows */
492         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
493                 ERROR("wimlib was compiled without support for NTFS-3g, so");
494                 ERROR("we cannot capture a WIM image directly from a NTFS volume");
495                 return WIMLIB_ERR_UNSUPPORTED;
496         }
497         if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
498                 ERROR("Capturing UNIX-specific data is not supported on Windows");
499                 return WIMLIB_ERR_UNSUPPORTED;
500         }
501         if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
502                 ERROR("Dereferencing symbolic links is not supported on Windows");
503                 return WIMLIB_ERR_UNSUPPORTED;
504         }
505 #endif
506
507         /* VERBOSE implies EXCLUDE_VERBOSE */
508         if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
509                 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
510
511         /* Check for contradictory reparse point fixup flags */
512         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
513                           WIMLIB_ADD_FLAG_NORPFIX)) ==
514                 (WIMLIB_ADD_FLAG_RPFIX |
515                  WIMLIB_ADD_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         /* Set default behavior on reparse point fixups if requested */
523         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
524                           WIMLIB_ADD_FLAG_NORPFIX)) == 0)
525         {
526                 /* Do reparse-point fixups by default if we are capturing an
527                  * entire image and either the header flag is set from previous
528                  * images, or if this is the first image being added. */
529                 if (is_entire_image &&
530                     ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
531                         add_flags |= WIMLIB_ADD_FLAG_RPFIX;
532         }
533
534         if (!is_entire_image) {
535                 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
536                         ERROR("Cannot add directly from a NTFS volume "
537                               "when not capturing a full image!");
538                         return WIMLIB_ERR_INVALID_PARAM;
539                 }
540
541                 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
542                         ERROR("Cannot do reparse point fixups when "
543                               "not capturing a full image!");
544                         return WIMLIB_ERR_INVALID_PARAM;
545                 }
546         }
547         /* We may have modified the add flags. */
548         cmd->add.add_flags = add_flags;
549         return 0;
550 }
551
552 static int
553 check_delete_command(const struct wimlib_update_command *cmd)
554 {
555         if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
556                                           WIMLIB_DELETE_FLAG_RECURSIVE))
557                 return WIMLIB_ERR_INVALID_PARAM;
558         return 0;
559 }
560
561 static int
562 check_rename_command(const struct wimlib_update_command *cmd)
563 {
564         if (cmd->rename.rename_flags != 0)
565                 return WIMLIB_ERR_INVALID_PARAM;
566         return 0;
567 }
568
569 static int
570 check_update_command(struct wimlib_update_command *cmd,
571                      const struct wim_header *hdr)
572 {
573         switch (cmd->op) {
574         case WIMLIB_UPDATE_OP_ADD:
575                 return check_add_command(cmd, hdr);
576         case WIMLIB_UPDATE_OP_DELETE:
577                 return check_delete_command(cmd);
578         case WIMLIB_UPDATE_OP_RENAME:
579                 return check_rename_command(cmd);
580         }
581         return 0;
582 }
583
584 static int
585 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
586                       const struct wim_header *hdr)
587 {
588         int ret = 0;
589         for (size_t i = 0; i < num_cmds; i++) {
590                 ret = check_update_command(&cmds[i], hdr);
591                 if (ret)
592                         break;
593         }
594         return ret;
595 }
596
597
598 static void
599 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
600 {
601         if (cmds) {
602                 for (size_t i = 0; i < num_cmds; i++) {
603                         switch (cmds[i].op) {
604                         case WIMLIB_UPDATE_OP_ADD:
605                                 FREE(cmds[i].add.fs_source_path);
606                                 FREE(cmds[i].add.wim_target_path);
607                                 free_capture_config(cmds[i].add.config);
608                                 break;
609                         case WIMLIB_UPDATE_OP_DELETE:
610                                 FREE(cmds[i].delete_.wim_path);
611                                 break;
612                         case WIMLIB_UPDATE_OP_RENAME:
613                                 FREE(cmds[i].rename.wim_source_path);
614                                 FREE(cmds[i].rename.wim_target_path);
615                                 break;
616                         }
617                 }
618                 FREE(cmds);
619         }
620 }
621
622 static int
623 copy_update_commands(const struct wimlib_update_command *cmds,
624                      size_t num_cmds,
625                      struct wimlib_update_command **cmds_copy_ret)
626 {
627         int ret;
628         struct wimlib_update_command *cmds_copy;
629         const struct wimlib_capture_config *config;
630
631         cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
632         if (!cmds_copy)
633                 goto oom;
634
635         for (size_t i = 0; i < num_cmds; i++) {
636                 cmds_copy[i].op = cmds[i].op;
637                 switch (cmds[i].op) {
638                 case WIMLIB_UPDATE_OP_ADD:
639                         cmds_copy[i].add.fs_source_path =
640                                 canonicalize_fs_path(cmds[i].add.fs_source_path);
641                         cmds_copy[i].add.wim_target_path =
642                                 canonicalize_wim_path(cmds[i].add.wim_target_path);
643                         if (!cmds_copy[i].add.fs_source_path ||
644                             !cmds_copy[i].add.wim_target_path)
645                                 goto oom;
646                         config = cmds[i].add.config;
647                         if (cmds[i].add.add_flags & WIMLIB_ADD_FLAG_WINCONFIG)
648                                 config = &winconfig;
649                         if (config) {
650                                 ret = copy_and_canonicalize_capture_config(config,
651                                                                            &cmds_copy[i].add.config);
652                                 if (ret)
653                                         goto err;
654                         }
655                         cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
656                         break;
657                 case WIMLIB_UPDATE_OP_DELETE:
658                         cmds_copy[i].delete_.wim_path =
659                                 canonicalize_wim_path(cmds[i].delete_.wim_path);
660                         if (!cmds_copy[i].delete_.wim_path)
661                                 goto oom;
662                         cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
663                         break;
664                 case WIMLIB_UPDATE_OP_RENAME:
665                         cmds_copy[i].rename.wim_source_path =
666                                 canonicalize_wim_path(cmds[i].rename.wim_source_path);
667                         cmds_copy[i].rename.wim_target_path =
668                                 canonicalize_wim_path(cmds[i].rename.wim_target_path);
669                         if (!cmds_copy[i].rename.wim_source_path ||
670                             !cmds_copy[i].rename.wim_target_path)
671                                 goto oom;
672                         break;
673                 default:
674                         ERROR("Unknown update operation %u", cmds[i].op);
675                         ret = WIMLIB_ERR_INVALID_PARAM;
676                         goto err;
677                 }
678         }
679         *cmds_copy_ret = cmds_copy;
680         ret = 0;
681 out:
682         return ret;
683 oom:
684         ret = WIMLIB_ERR_NOMEM;
685 err:
686         free_update_commands(cmds_copy, num_cmds);
687         goto out;
688 }
689
690 /* API function documented in wimlib.h  */
691 WIMLIBAPI int
692 wimlib_update_image(WIMStruct *wim,
693                     int image,
694                     const struct wimlib_update_command *cmds,
695                     size_t num_cmds,
696                     int update_flags,
697                     wimlib_progress_func_t progress_func)
698 {
699         int ret;
700         struct wimlib_update_command *cmds_copy;
701         bool deletion_requested = false;
702
703         if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
704                 return WIMLIB_ERR_INVALID_PARAM;
705
706         DEBUG("Updating image %d with %zu commands", image, num_cmds);
707
708         for (size_t i = 0; i < num_cmds; i++)
709                 if (cmds[i].op == WIMLIB_UPDATE_OP_DELETE)
710                         deletion_requested = true;
711
712         if (deletion_requested)
713                 ret = can_delete_from_wim(wim);
714         else
715                 ret = can_modify_wim(wim);
716
717         if (ret)
718                 goto out;
719
720         /* Load the metadata for the image to modify (if not loaded already) */
721         ret = select_wim_image(wim, image);
722         if (ret)
723                 goto out;
724
725         /* Short circuit a successful return if no commands were specified.
726          * Avoids problems with trying to allocate 0 bytes of memory. */
727         if (num_cmds == 0)
728                 goto out;
729
730         DEBUG("Preparing %zu update commands", num_cmds);
731
732         /* Make a copy of the update commands, in the process doing certain
733          * canonicalizations on paths (e.g. translating backslashes to forward
734          * slashes).  This is done to avoid modifying the caller's copy of the
735          * commands. */
736         ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
737         if (ret)
738                 goto out;
739
740         /* Perform additional checks on the update commands before we execute
741          * them. */
742         ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
743         if (ret)
744                 goto out_free_cmds_copy;
745
746         /* Actually execute the update commands. */
747         DEBUG("Executing %zu update commands", num_cmds);
748         ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
749                                       progress_func);
750         if (ret)
751                 goto out_free_cmds_copy;
752
753         wim->image_metadata[image - 1]->modified = 1;
754
755         /* Statistics about the WIM image, such as the numbers of files and
756          * directories, may have changed.  Call xml_update_image_info() to
757          * recalculate these statistics. */
758         xml_update_image_info(wim, image);
759 out_free_cmds_copy:
760         free_update_commands(cmds_copy, num_cmds);
761 out:
762         return ret;
763 }