]> wimlib.net Git - wimlib/blob - src/update_image.c
WIMBoot: Update WimOverlay.dat directly when WOF not running
[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 const tchar *wimboot_cfgfile =
178                 T("/Windows/System32/WimBootCompress.ini");
179
180 static int
181 get_capture_config(const tchar *config_file, struct capture_config *config,
182                    int add_flags, const tchar *fs_source_path)
183 {
184         int ret;
185         tchar *tmp_config_file = NULL;
186
187         memset(config, 0, sizeof(*config));
188
189         /* For WIMBoot capture, check for default capture configuration file
190          * unless one was explicitly specified.  */
191         if (!config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT)) {
192
193                 /* XXX: Handle loading file correctly when in NTFS volume.  */
194
195                 size_t len = tstrlen(fs_source_path) +
196                              tstrlen(wimboot_cfgfile);
197                 tmp_config_file = MALLOC((len + 1) * sizeof(tchar));
198                 struct stat st;
199
200                 tsprintf(tmp_config_file, T("%"TS"%"TS),
201                          fs_source_path, wimboot_cfgfile);
202                 if (!tstat(tmp_config_file, &st)) {
203                         config_file = tmp_config_file;
204                         add_flags &= ~WIMLIB_ADD_FLAG_WINCONFIG;
205                 } else {
206                         WARNING("\"%"TS"\" does not exist.\n"
207                                 "          Using default capture configuration!",
208                                 tmp_config_file);
209                 }
210         }
211
212         if (add_flags & WIMLIB_ADD_FLAG_WINCONFIG) {
213                 /* Use Windows default.  */
214                 if (config_file)
215                         return WIMLIB_ERR_INVALID_PARAM;
216                 ret = do_read_capture_config_file(T("wincfg"), wincfg,
217                                                   sizeof(wincfg) - 1, config);
218         } else if (config_file) {
219                 /* Use the specified configuration file.  */
220                 ret = do_read_capture_config_file(config_file, NULL, 0, config);
221         } else {
222                 /* ... Or don't use any configuration file at all.  No files
223                  * will be excluded from capture, all files will be compressed,
224                  * etc.  */
225                 ret = 0;
226         }
227         FREE(tmp_config_file);
228         return ret;
229 }
230
231 static int
232 replace_wimboot_cfg(WIMStruct *wim, const tchar *config_file)
233 {
234         struct wimlib_update_command cmds[] = {
235                 {
236                         .op = WIMLIB_UPDATE_OP_DELETE,
237                         .delete_ = {
238                                 .wim_path = (tchar *)wimboot_cfgfile,
239                                 .delete_flags = WIMLIB_DELETE_FLAG_FORCE |
240                                                 WIMLIB_DELETE_FLAG_RECURSIVE,
241                         },
242                 },
243                 {
244                         .op = WIMLIB_UPDATE_OP_ADD,
245                         .add = {
246                                 .fs_source_path = (tchar *)config_file,
247                                 .wim_target_path = (tchar *)wimboot_cfgfile,
248                                 .add_flags = 0,
249                                 .config_file = NULL,
250                         },
251                 },
252         };
253         return wimlib_update_image(wim, wim->current_image,
254                                    cmds, ARRAY_LEN(cmds), 0, NULL);
255 }
256
257 static int
258 execute_add_command(WIMStruct *wim,
259                     const struct wimlib_update_command *add_cmd,
260                     wimlib_progress_func_t progress_func)
261 {
262         int ret;
263         int add_flags;
264         tchar *fs_source_path;
265         tchar *wim_target_path;
266         struct wim_image_metadata *imd;
267         struct list_head unhashed_streams;
268         struct add_image_params params;
269         int (*capture_tree)(struct wim_dentry **,
270                             const tchar *,
271                             struct add_image_params *);
272         const tchar *config_file;
273         struct capture_config config;
274 #ifdef WITH_NTFS_3G
275         struct _ntfs_volume *ntfs_vol = NULL;
276 #endif
277         void *extra_arg;
278         struct wim_dentry *branch;
279         bool rollback_sd = true;
280
281         wimlib_assert(add_cmd->op == WIMLIB_UPDATE_OP_ADD);
282
283         add_flags = add_cmd->add.add_flags;
284         fs_source_path = add_cmd->add.fs_source_path;
285         wim_target_path = add_cmd->add.wim_target_path;
286         config_file = add_cmd->add.config_file;
287
288         DEBUG("fs_source_path=\"%"TS"\", wim_target_path=\"%"TS"\", add_flags=%#x",
289               fs_source_path, wim_target_path, add_flags);
290
291         memset(&params, 0, sizeof(params));
292
293         imd = wim->image_metadata[wim->current_image - 1];
294
295         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
296         #ifdef WITH_NTFS_3G
297                 capture_tree = build_dentry_tree_ntfs;
298                 extra_arg = &ntfs_vol;
299                 if (imd->ntfs_vol != NULL) {
300                         ERROR("NTFS volume already set");
301                         ret = WIMLIB_ERR_INVALID_PARAM;
302                         goto out;
303                 }
304         #else
305                 ret = WIMLIB_ERR_INVALID_PARAM;
306                 goto out;
307         #endif
308         } else {
309         #ifdef __WIN32__
310                 capture_tree = win32_build_dentry_tree;
311         #else
312                 capture_tree = unix_build_dentry_tree;
313         #endif
314                 extra_arg = NULL;
315         }
316
317         ret = get_capture_config(config_file, &config,
318                                  add_flags, fs_source_path);
319         if (ret)
320                 goto out;
321
322         ret = init_inode_table(&params.inode_table, 9001);
323         if (ret)
324                 goto out_destroy_config;
325
326         ret = init_sd_set(&params.sd_set, imd->security_data);
327         if (ret)
328                 goto out_destroy_inode_table;
329
330         INIT_LIST_HEAD(&unhashed_streams);
331         params.lookup_table = wim->lookup_table;
332         params.unhashed_streams = &unhashed_streams;
333         params.config = &config;
334         params.add_flags = add_flags;
335         params.extra_arg = extra_arg;
336
337         params.progress_func = progress_func;
338         params.progress.scan.source = fs_source_path;
339         params.progress.scan.wim_target_path = wim_target_path;
340         if (progress_func)
341                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_BEGIN, &params.progress);
342
343         config.prefix = fs_source_path;
344         config.prefix_num_tchars = tstrlen(fs_source_path);
345
346         if (wim_target_path[0] == T('\0'))
347                 params.add_flags |= WIMLIB_ADD_FLAG_ROOT;
348         ret = (*capture_tree)(&branch, fs_source_path, &params);
349         if (ret)
350                 goto out_destroy_sd_set;
351
352         if (branch) {
353                 /* Use the target name, not the source name, for
354                  * the root of each branch from a capture
355                  * source.  (This will also set the root dentry
356                  * of the entire image to be unnamed.) */
357                 ret = dentry_set_name(branch,
358                                       path_basename(wim_target_path));
359                 if (ret)
360                         goto out_ntfs_umount;
361
362                 ret = attach_branch(&imd->root_dentry, branch, wim_target_path,
363                                     WIMLIB_CASE_PLATFORM_DEFAULT);
364                 if (ret)
365                         goto out_ntfs_umount;
366         }
367
368         if (config_file && (add_flags & WIMLIB_ADD_FLAG_WIMBOOT) &&
369             wim_target_path[0] == T('\0'))
370         {
371                 /* Save configuration file as \Windows\System32\WimBootCompress.ini  */
372                 ret = replace_wimboot_cfg(wim, config_file);
373                 if (ret) {
374                         /* Undo attach_branch()  */
375                         if (imd->root_dentry == branch)
376                                 imd->root_dentry = NULL;
377                         else
378                                 branch = NULL;
379                         goto out_ntfs_umount;
380                 }
381         }
382
383         if (progress_func)
384                 progress_func(WIMLIB_PROGRESS_MSG_SCAN_END, &params.progress);
385         list_splice_tail(&unhashed_streams, &imd->unhashed_streams);
386 #ifdef WITH_NTFS_3G
387         imd->ntfs_vol = ntfs_vol;
388 #endif
389         inode_table_prepare_inode_list(&params.inode_table, &imd->inode_list);
390         ret = 0;
391         rollback_sd = false;
392         if (add_flags & WIMLIB_ADD_FLAG_RPFIX)
393                 wim->hdr.flags |= WIM_HDR_FLAG_RP_FIX;
394         goto out_destroy_sd_set;
395 out_ntfs_umount:
396 #ifdef WITH_NTFS_3G
397         if (ntfs_vol)
398                 do_ntfs_umount(ntfs_vol);
399 #endif
400         free_dentry_tree(branch, wim->lookup_table);
401 out_destroy_sd_set:
402         destroy_sd_set(&params.sd_set, rollback_sd);
403 out_destroy_inode_table:
404         destroy_inode_table(&params.inode_table);
405 out_destroy_config:
406         destroy_capture_config(&config);
407 out:
408         return ret;
409 }
410
411 static int
412 execute_delete_command(WIMStruct *wim,
413                        const struct wimlib_update_command *delete_cmd)
414 {
415         int flags;
416         const tchar *wim_path;
417         struct wim_dentry *tree;
418         bool is_root;
419
420         wimlib_assert(delete_cmd->op == WIMLIB_UPDATE_OP_DELETE);
421         flags = delete_cmd->delete_.delete_flags;
422         wim_path = delete_cmd->delete_.wim_path;
423
424         DEBUG("Deleting WIM path \"%"TS"\" (flags=%#x)", wim_path, flags);
425
426         tree = get_dentry(wim, wim_path, WIMLIB_CASE_PLATFORM_DEFAULT);
427         if (!tree) {
428                 /* Path to delete does not exist in the WIM. */
429                 if (flags & WIMLIB_DELETE_FLAG_FORCE) {
430                         return 0;
431                 } else {
432                         ERROR("Path \"%"TS"\" does not exist in WIM image %d",
433                               wim_path, wim->current_image);
434                         return WIMLIB_ERR_PATH_DOES_NOT_EXIST;
435                 }
436         }
437
438         if (dentry_is_directory(tree) && !(flags & WIMLIB_DELETE_FLAG_RECURSIVE)) {
439                 ERROR("Path \"%"TS"\" in WIM image %d is a directory "
440                       "but a recursive delete was not requested",
441                       wim_path, wim->current_image);
442                 return WIMLIB_ERR_IS_DIRECTORY;
443         }
444
445         is_root = dentry_is_root(tree);
446         unlink_dentry(tree);
447         free_dentry_tree(tree, wim->lookup_table);
448         if (is_root)
449                 wim->image_metadata[wim->current_image - 1]->root_dentry = NULL;
450         return 0;
451 }
452
453 static int
454 execute_rename_command(WIMStruct *wim,
455                        const struct wimlib_update_command *rename_cmd)
456 {
457         int ret;
458
459         wimlib_assert(rename_cmd->op == WIMLIB_UPDATE_OP_RENAME);
460
461         ret = rename_wim_path(wim, rename_cmd->rename.wim_source_path,
462                               rename_cmd->rename.wim_target_path,
463                               WIMLIB_CASE_PLATFORM_DEFAULT);
464         if (ret) {
465                 ret = -ret;
466                 errno = ret;
467                 ERROR_WITH_ERRNO("Can't rename \"%"TS"\" to \"%"TS"\"",
468                                  rename_cmd->rename.wim_source_path,
469                                  rename_cmd->rename.wim_target_path);
470                 switch (ret) {
471                 case ENOMEM:
472                         ret = WIMLIB_ERR_NOMEM;
473                         break;
474                 case ENOTDIR:
475                         ret = WIMLIB_ERR_NOTDIR;
476                         break;
477                 case ENOTEMPTY:
478                         ret = WIMLIB_ERR_NOTEMPTY;
479                         break;
480                 case EISDIR:
481                         ret = WIMLIB_ERR_IS_DIRECTORY;
482                         break;
483                 case ENOENT:
484                 default:
485                         ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
486                         break;
487                 }
488         }
489         return ret;
490 }
491
492 static inline const tchar *
493 update_op_to_str(int op)
494 {
495         switch (op) {
496         case WIMLIB_UPDATE_OP_ADD:
497                 return T("add");
498         case WIMLIB_UPDATE_OP_DELETE:
499                 return T("delete");
500         case WIMLIB_UPDATE_OP_RENAME:
501                 return T("rename");
502         default:
503                 wimlib_assert(0);
504                 return NULL;
505         }
506 }
507
508 static int
509 execute_update_commands(WIMStruct *wim,
510                         const struct wimlib_update_command *cmds,
511                         size_t num_cmds,
512                         int update_flags,
513                         wimlib_progress_func_t progress_func)
514 {
515         int ret = 0;
516         union wimlib_progress_info info;
517         info.update.completed_commands = 0;
518         info.update.total_commands = num_cmds;
519         for (size_t i = 0; i < num_cmds; i++) {
520                 DEBUG("Executing update command %zu of %zu (op=%"TS")",
521                       i + 1, num_cmds, update_op_to_str(cmds[i].op));
522                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
523                     progress_func)
524                 {
525                         info.update.command = &cmds[i];
526                         (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_BEGIN_COMMAND,
527                                          &info);
528                 }
529                 switch (cmds[i].op) {
530                 case WIMLIB_UPDATE_OP_ADD:
531                         ret = execute_add_command(wim, &cmds[i], progress_func);
532                         break;
533                 case WIMLIB_UPDATE_OP_DELETE:
534                         ret = execute_delete_command(wim, &cmds[i]);
535                         break;
536                 case WIMLIB_UPDATE_OP_RENAME:
537                         ret = execute_rename_command(wim, &cmds[i]);
538                         break;
539                 default:
540                         wimlib_assert(0);
541                 }
542                 if (ret)
543                         break;
544                 info.update.completed_commands++;
545                 if (update_flags & WIMLIB_UPDATE_FLAG_SEND_PROGRESS &&
546                     progress_func)
547                 {
548                         (*progress_func)(WIMLIB_PROGRESS_MSG_UPDATE_END_COMMAND,
549                                          &info);
550                 }
551         }
552         return ret;
553 }
554
555
556 static int
557 check_add_command(struct wimlib_update_command *cmd,
558                   const struct wim_header *hdr)
559 {
560         int add_flags = cmd->add.add_flags;
561
562         if (add_flags & ~(WIMLIB_ADD_FLAG_NTFS |
563                           WIMLIB_ADD_FLAG_DEREFERENCE |
564                           WIMLIB_ADD_FLAG_VERBOSE |
565                           /* BOOT doesn't make sense for wimlib_update_image().  */
566                           /*WIMLIB_ADD_FLAG_BOOT |*/
567                           WIMLIB_ADD_FLAG_WIMBOOT |
568                           WIMLIB_ADD_FLAG_UNIX_DATA |
569                           WIMLIB_ADD_FLAG_NO_ACLS |
570                           WIMLIB_ADD_FLAG_STRICT_ACLS |
571                           WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE |
572                           WIMLIB_ADD_FLAG_RPFIX |
573                           WIMLIB_ADD_FLAG_NORPFIX |
574                           WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE |
575                           WIMLIB_ADD_FLAG_WINCONFIG))
576                 return WIMLIB_ERR_INVALID_PARAM;
577
578         /* Are we adding the entire image or not?  An empty wim_target_path
579          * indicates that the tree we're adding is to be placed in the root of
580          * the image.  We consider this to be capturing the entire image,
581          * although it could potentially be an overlay on an existing root as
582          * well. */
583         bool is_entire_image = cmd->add.wim_target_path[0] == T('\0');
584
585 #ifdef __WIN32__
586         /* Check for flags not supported on Windows */
587         if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
588                 ERROR("wimlib was compiled without support for NTFS-3g, so");
589                 ERROR("we cannot capture a WIM image directly from a NTFS volume");
590                 return WIMLIB_ERR_UNSUPPORTED;
591         }
592         if (add_flags & WIMLIB_ADD_FLAG_UNIX_DATA) {
593                 ERROR("Capturing UNIX-specific data is not supported on Windows");
594                 return WIMLIB_ERR_UNSUPPORTED;
595         }
596         if (add_flags & WIMLIB_ADD_FLAG_DEREFERENCE) {
597                 ERROR("Dereferencing symbolic links is not supported on Windows");
598                 return WIMLIB_ERR_UNSUPPORTED;
599         }
600 #endif
601
602         /* VERBOSE implies EXCLUDE_VERBOSE */
603         if (add_flags & WIMLIB_ADD_FLAG_VERBOSE)
604                 add_flags |= WIMLIB_ADD_FLAG_EXCLUDE_VERBOSE;
605
606         /* Check for contradictory reparse point fixup flags */
607         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
608                           WIMLIB_ADD_FLAG_NORPFIX)) ==
609                 (WIMLIB_ADD_FLAG_RPFIX |
610                  WIMLIB_ADD_FLAG_NORPFIX))
611         {
612                 ERROR("Cannot specify RPFIX and NORPFIX flags "
613                       "at the same time!");
614                 return WIMLIB_ERR_INVALID_PARAM;
615         }
616
617         /* Set default behavior on reparse point fixups if requested */
618         if ((add_flags & (WIMLIB_ADD_FLAG_RPFIX |
619                           WIMLIB_ADD_FLAG_NORPFIX)) == 0)
620         {
621                 /* Do reparse-point fixups by default if we are capturing an
622                  * entire image and either the header flag is set from previous
623                  * images, or if this is the first image being added. */
624                 if (is_entire_image &&
625                     ((hdr->flags & WIM_HDR_FLAG_RP_FIX) || hdr->image_count == 1))
626                         add_flags |= WIMLIB_ADD_FLAG_RPFIX;
627         }
628
629         if (!is_entire_image) {
630                 if (add_flags & WIMLIB_ADD_FLAG_NTFS) {
631                         ERROR("Cannot add directly from a NTFS volume "
632                               "when not capturing a full image!");
633                         return WIMLIB_ERR_INVALID_PARAM;
634                 }
635
636                 if (add_flags & WIMLIB_ADD_FLAG_RPFIX) {
637                         ERROR("Cannot do reparse point fixups when "
638                               "not capturing a full image!");
639                         return WIMLIB_ERR_INVALID_PARAM;
640                 }
641         }
642         /* We may have modified the add flags. */
643         cmd->add.add_flags = add_flags;
644         return 0;
645 }
646
647 static int
648 check_delete_command(const struct wimlib_update_command *cmd)
649 {
650         if (cmd->delete_.delete_flags & ~(WIMLIB_DELETE_FLAG_FORCE |
651                                           WIMLIB_DELETE_FLAG_RECURSIVE))
652                 return WIMLIB_ERR_INVALID_PARAM;
653         return 0;
654 }
655
656 static int
657 check_rename_command(const struct wimlib_update_command *cmd)
658 {
659         if (cmd->rename.rename_flags != 0)
660                 return WIMLIB_ERR_INVALID_PARAM;
661         return 0;
662 }
663
664 static int
665 check_update_command(struct wimlib_update_command *cmd,
666                      const struct wim_header *hdr)
667 {
668         switch (cmd->op) {
669         case WIMLIB_UPDATE_OP_ADD:
670                 return check_add_command(cmd, hdr);
671         case WIMLIB_UPDATE_OP_DELETE:
672                 return check_delete_command(cmd);
673         case WIMLIB_UPDATE_OP_RENAME:
674                 return check_rename_command(cmd);
675         }
676         return 0;
677 }
678
679 static int
680 check_update_commands(struct wimlib_update_command *cmds, size_t num_cmds,
681                       const struct wim_header *hdr)
682 {
683         int ret = 0;
684         for (size_t i = 0; i < num_cmds; i++) {
685                 ret = check_update_command(&cmds[i], hdr);
686                 if (ret)
687                         break;
688         }
689         return ret;
690 }
691
692
693 static void
694 free_update_commands(struct wimlib_update_command *cmds, size_t num_cmds)
695 {
696         if (cmds) {
697                 for (size_t i = 0; i < num_cmds; i++) {
698                         switch (cmds[i].op) {
699                         case WIMLIB_UPDATE_OP_ADD:
700                                 FREE(cmds[i].add.fs_source_path);
701                                 FREE(cmds[i].add.wim_target_path);
702                                 FREE(cmds[i].add.config_file);
703                                 break;
704                         case WIMLIB_UPDATE_OP_DELETE:
705                                 FREE(cmds[i].delete_.wim_path);
706                                 break;
707                         case WIMLIB_UPDATE_OP_RENAME:
708                                 FREE(cmds[i].rename.wim_source_path);
709                                 FREE(cmds[i].rename.wim_target_path);
710                                 break;
711                         }
712                 }
713                 FREE(cmds);
714         }
715 }
716
717 static int
718 copy_update_commands(const struct wimlib_update_command *cmds,
719                      size_t num_cmds,
720                      struct wimlib_update_command **cmds_copy_ret)
721 {
722         int ret;
723         struct wimlib_update_command *cmds_copy;
724
725         cmds_copy = CALLOC(num_cmds, sizeof(cmds[0]));
726         if (!cmds_copy)
727                 goto oom;
728
729         for (size_t i = 0; i < num_cmds; i++) {
730                 cmds_copy[i].op = cmds[i].op;
731                 switch (cmds[i].op) {
732                 case WIMLIB_UPDATE_OP_ADD:
733                         cmds_copy[i].add.fs_source_path =
734                                 canonicalize_fs_path(cmds[i].add.fs_source_path);
735                         cmds_copy[i].add.wim_target_path =
736                                 canonicalize_wim_path(cmds[i].add.wim_target_path);
737                         if (!cmds_copy[i].add.fs_source_path ||
738                             !cmds_copy[i].add.wim_target_path)
739                                 goto oom;
740                         if (cmds[i].add.config_file) {
741                                 cmds_copy[i].add.config_file = TSTRDUP(cmds[i].add.config_file);
742                                 if (!cmds_copy[i].add.config_file)
743                                         goto oom;
744                         }
745                         cmds_copy[i].add.add_flags = cmds[i].add.add_flags;
746                         break;
747                 case WIMLIB_UPDATE_OP_DELETE:
748                         cmds_copy[i].delete_.wim_path =
749                                 canonicalize_wim_path(cmds[i].delete_.wim_path);
750                         if (!cmds_copy[i].delete_.wim_path)
751                                 goto oom;
752                         cmds_copy[i].delete_.delete_flags = cmds[i].delete_.delete_flags;
753                         break;
754                 case WIMLIB_UPDATE_OP_RENAME:
755                         cmds_copy[i].rename.wim_source_path =
756                                 canonicalize_wim_path(cmds[i].rename.wim_source_path);
757                         cmds_copy[i].rename.wim_target_path =
758                                 canonicalize_wim_path(cmds[i].rename.wim_target_path);
759                         if (!cmds_copy[i].rename.wim_source_path ||
760                             !cmds_copy[i].rename.wim_target_path)
761                                 goto oom;
762                         break;
763                 default:
764                         ERROR("Unknown update operation %u", cmds[i].op);
765                         ret = WIMLIB_ERR_INVALID_PARAM;
766                         goto err;
767                 }
768         }
769         *cmds_copy_ret = cmds_copy;
770         ret = 0;
771 out:
772         return ret;
773 oom:
774         ret = WIMLIB_ERR_NOMEM;
775 err:
776         free_update_commands(cmds_copy, num_cmds);
777         goto out;
778 }
779
780 /* API function documented in wimlib.h  */
781 WIMLIBAPI int
782 wimlib_update_image(WIMStruct *wim,
783                     int image,
784                     const struct wimlib_update_command *cmds,
785                     size_t num_cmds,
786                     int update_flags,
787                     wimlib_progress_func_t progress_func)
788 {
789         int ret;
790         struct wimlib_update_command *cmds_copy;
791         bool deletion_requested = false;
792
793         if (update_flags & ~WIMLIB_UPDATE_FLAG_SEND_PROGRESS)
794                 return WIMLIB_ERR_INVALID_PARAM;
795
796         DEBUG("Updating image %d with %zu commands", image, num_cmds);
797
798         for (size_t i = 0; i < num_cmds; i++)
799                 if (cmds[i].op == WIMLIB_UPDATE_OP_DELETE)
800                         deletion_requested = true;
801
802         if (deletion_requested)
803                 ret = can_delete_from_wim(wim);
804         else
805                 ret = can_modify_wim(wim);
806
807         if (ret)
808                 goto out;
809
810         /* Load the metadata for the image to modify (if not loaded already) */
811         ret = select_wim_image(wim, image);
812         if (ret)
813                 goto out;
814
815         /* Short circuit a successful return if no commands were specified.
816          * Avoids problems with trying to allocate 0 bytes of memory. */
817         if (num_cmds == 0)
818                 goto out;
819
820         DEBUG("Preparing %zu update commands", num_cmds);
821
822         /* Make a copy of the update commands, in the process doing certain
823          * canonicalizations on paths (e.g. translating backslashes to forward
824          * slashes).  This is done to avoid modifying the caller's copy of the
825          * commands. */
826         ret = copy_update_commands(cmds, num_cmds, &cmds_copy);
827         if (ret)
828                 goto out;
829
830         /* Perform additional checks on the update commands before we execute
831          * them. */
832         ret = check_update_commands(cmds_copy, num_cmds, &wim->hdr);
833         if (ret)
834                 goto out_free_cmds_copy;
835
836         /* Actually execute the update commands. */
837         DEBUG("Executing %zu update commands", num_cmds);
838         ret = execute_update_commands(wim, cmds_copy, num_cmds, update_flags,
839                                       progress_func);
840         if (ret)
841                 goto out_free_cmds_copy;
842
843         wim->image_metadata[image - 1]->modified = 1;
844
845         /* Statistics about the WIM image, such as the numbers of files and
846          * directories, may have changed.  Call xml_update_image_info() to
847          * recalculate these statistics. */
848         xml_update_image_info(wim, image);
849 out_free_cmds_copy:
850         free_update_commands(cmds_copy, num_cmds);
851 out:
852         return ret;
853 }