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