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