]> wimlib.net Git - wimlib/blob - src/modify.c
5694ec202a53342ac7d8b57a10e9234f7dc0431d
[wimlib] / src / modify.c
1 /*
2  * modify.c
3  *
4  * Support for modifying WIM files with image-level operations (delete an image,
5  * add an image, export an imagex from one WIM to another.)  There is nothing
6  * here that lets you change individual files in the WIM; for that you will need
7  * to look at the filesystem implementation in mount.c.
8  */
9
10 /*
11  * Copyright (C) 2012 Eric Biggers
12  *
13  * This file is part of wimlib, a library for working with WIM files.
14  *
15  * wimlib is free software; you can redistribute it and/or modify it under the
16  * terms of the GNU Lesser General Public License as published by the Free
17  * Software Foundation; either version 2.1 of the License, or (at your option)
18  * any later version.
19  *
20  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU Lesser General Public License
26  * along with wimlib; if not, see http://www.gnu.org/licenses/.
27  */
28
29 #include "wimlib_internal.h"
30 #include "util.h"
31 #include "sha1.h"
32 #include "dentry.h"
33 #include "xml.h"
34 #include "lookup_table.h"
35 #include <sys/stat.h>
36 #include <dirent.h>
37 #include <string.h>
38 #include <errno.h>
39
40 static void destroy_image_metadata(struct image_metadata *imd,
41                                    struct lookup_table *lt)
42 {
43         free_dentry_tree(imd->root_dentry, lt, true);
44         free_security_data(imd->security_data);
45
46         /* Get rid of the lookup table entry for this image's metadata resource
47          * */
48         lookup_table_remove(lt, imd->metadata_lte);
49 }
50
51 /* 
52  * Recursively builds a dentry tree from a directory tree on disk, outside the
53  * WIM file.
54  *
55  * @root:  A dentry that has already been created for the root of the dentry
56  *         tree.
57  * @source_path:  The path to the root of the tree on disk. 
58  * @root_stat:   A pointer to a `struct stat' that contains the metadata for the
59  *                      root of the tree on disk. 
60  * @lookup_table: The lookup table for the WIM file.  For each file added to the
61  *              dentry tree being built, an entry is added to the lookup table, 
62  *              unless an identical file is already in the lookup table.  These
63  *              lookup table entries that are added point to the file on disk.
64  *
65  * @return:     0 on success, nonzero on failure.  It is a failure if any of
66  *              the files cannot be `stat'ed, or if any of the needed
67  *              directories cannot be opened or read.  Failure to add the files
68  *              to the WIM may still occur later when trying to actually read 
69  *              the regular files in the tree into the WIM as file resources.
70  */
71 static int build_dentry_tree(struct dentry *root, const char *source_path, 
72                              struct stat *root_stat,
73                              struct lookup_table* lookup_table)
74 {
75         int ret = 0;
76
77         stbuf_to_dentry(root_stat, root);
78         if (dentry_is_directory(root)) {
79                 /* Open the directory on disk */
80                 DIR *dir;
81                 struct dirent *p;
82                 struct stat child_stat;
83                 struct dentry *child;
84
85                 dir = opendir(source_path);
86                 if (!dir) {
87                         ERROR_WITH_ERRNO("Failed to open the directory `%s'",
88                                          source_path);
89                         return WIMLIB_ERR_OPEN;
90                 }
91
92                 /* Buffer for names of files in directory. */
93                 size_t len = strlen(source_path);
94                 char name[len + 1 + FILENAME_MAX + 1];
95                 memcpy(name, source_path, len);
96                 name[len] = '/';
97
98                 /* Create a dentry for each entry in the directory on disk, and recurse
99                  * to any subdirectories. */
100                 while ((p = readdir(dir)) != NULL) {
101                         if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
102                               || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
103                                         continue;
104                         strcpy(name + len + 1, p->d_name);
105                         if (stat(name, &child_stat) != 0) {
106                                 ERROR_WITH_ERRNO("Cannot stat `%s'", name);
107                                 ret = WIMLIB_ERR_STAT;
108                                 break;
109                         }
110                         child = new_dentry(p->d_name);
111                         if (!child) {
112                                 ERROR("No memory to allocate new dentry");
113                                 return WIMLIB_ERR_NOMEM;
114                         }
115                         ret = build_dentry_tree(child, name, &child_stat, 
116                                                 lookup_table);
117                         link_dentry(child, root);
118                         if (ret != 0)
119                                 break;
120                 }
121                 closedir(dir);
122         } else {
123                 struct lookup_table_entry *lte;
124
125                 /* For each non-directory, we must check to see if the file is
126                  * in the lookup table already; if it is, we increment its
127                  * refcnt; otherwise, we create a new lookup table entry and
128                  * insert it. */
129                 ret = sha1sum(source_path, root->hash);
130                 if (ret != 0)
131                         return ret;
132
133                 lte = lookup_resource(lookup_table, root->hash);
134                 if (lte) {
135                         lte->refcnt++;
136                 } else {
137                         char *file_on_disk = STRDUP(source_path);
138                         if (!file_on_disk) {
139                                 ERROR("Failed to allocate memory for file path");
140                                 return WIMLIB_ERR_NOMEM;
141                         }
142                         lte = new_lookup_table_entry();
143                         if (!lte) {
144                                 FREE(file_on_disk);
145                                 return WIMLIB_ERR_NOMEM;
146                         }
147                         lte->file_on_disk = file_on_disk;
148                         lte->resource_entry.flags = 0;
149                         lte->refcnt       = 1;
150                         lte->part_number  = 1;
151                         lte->resource_entry.original_size = root_stat->st_size;
152                         lte->resource_entry.size = root_stat->st_size;
153                         memcpy(lte->hash, root->hash, WIM_HASH_SIZE);
154                         lookup_table_insert(lookup_table, lte);
155                 }
156         }
157         return ret;
158 }
159
160 struct wim_pair {
161         WIMStruct *src_wim;
162         WIMStruct *dest_wim;
163 };
164
165 /* 
166  * This function takes in a dentry that was previously located only in image(s)
167  * in @src_wim, but now is being added to @dest_wim. If there is in fact already a
168  * lookup table entry for this file in the lookup table of the destination WIM
169  * file, we simply increment its reference count.  Otherwise, a new lookup table
170  * entry is created that references the location of the file resource in the
171  * source WIM file through the other_wim_fp field of the lookup table entry.
172  */
173 static int add_lookup_table_entry_to_dest_wim(struct dentry *dentry, void *arg)
174 {
175         WIMStruct *src_wim, *dest_wim;
176         struct lookup_table_entry *src_table_entry;
177         struct lookup_table_entry *dest_table_entry;
178
179         src_wim = ((struct wim_pair*)arg)->src_wim;
180         dest_wim = ((struct wim_pair*)arg)->dest_wim;
181
182         if (dentry_is_directory(dentry))
183                 return 0;
184
185         src_table_entry = wim_lookup_resource(src_wim, dentry);
186         if (!src_table_entry)
187                 return 0;
188
189         dest_table_entry = wim_lookup_resource(dest_wim, dentry);
190         if (dest_table_entry) {
191                 dest_table_entry->refcnt++;
192         } else {
193                 dest_table_entry = new_lookup_table_entry();
194                 if (!dest_table_entry)
195                         return WIMLIB_ERR_NOMEM;
196                 dest_table_entry->other_wim_fp = src_wim->fp;
197                 dest_table_entry->other_wim_ctype = 
198                                 wimlib_get_compression_type(src_wim);
199                 dest_table_entry->refcnt = 1;
200                 memcpy(&dest_table_entry->resource_entry, 
201                        &src_table_entry->resource_entry, 
202                        sizeof(struct resource_entry));
203                 memcpy(dest_table_entry->hash, dentry->hash, WIM_HASH_SIZE);
204                 lookup_table_insert(dest_wim->lookup_table, dest_table_entry);
205         }
206         return 0;
207 }
208
209 /*
210  * Adds an image (given by its dentry tree) to the image metadata array of a WIM
211  * file, adds an entry to the lookup table for the image metadata, updates the
212  * image count in the header, and selects the new image. 
213  *
214  * Does not update the XML data.
215  *
216  * @w:            The WIMStruct for the WIM file.
217  * @root_dentry:  The root of the directory tree for the image.
218  */
219 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry)
220 {
221         struct lookup_table_entry *metadata_lte;
222         struct image_metadata *imd;
223         struct image_metadata *new_imd;
224         struct wim_security_data *sd;
225
226         DEBUG("Reallocating image metadata array for image_count = %u",
227               w->hdr.image_count + 1);
228         imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
229
230         if (!imd) {
231                 ERROR("Failed to allocate memory for new image metadata array");
232                 return WIMLIB_ERR_NOMEM;
233         }
234
235         memcpy(imd, w->image_metadata, 
236                w->hdr.image_count * sizeof(struct image_metadata));
237         
238         metadata_lte = new_lookup_table_entry();
239         if (!metadata_lte)
240                 goto out_free_imd;
241         sd = CALLOC(1, sizeof(struct wim_security_data));
242         if (!sd)
243                 goto out_free_metadata_lte;
244         sd->refcnt = 1;
245         sd->total_length = 8;
246
247         metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
248         randomize_byte_array(metadata_lte->hash, WIM_HASH_SIZE);
249         lookup_table_insert(w->lookup_table, metadata_lte);
250
251         w->hdr.image_count++;
252
253         new_imd                 = &imd[w->hdr.image_count - 1];
254         new_imd->metadata_lte   = metadata_lte;
255         new_imd->modified       = true;
256         new_imd->root_dentry    = root_dentry;
257         new_imd->security_data  = sd;
258         FREE(w->image_metadata);
259         w->image_metadata       = imd;
260
261         /* Change the current image to the new one. */
262         return wimlib_select_image(w, w->hdr.image_count);
263 out_free_metadata_lte:
264         FREE(metadata_lte);
265 out_free_imd:
266         FREE(imd);
267         return WIMLIB_ERR_NOMEM;
268
269 }
270
271 /*
272  * Copies an image, or all the images, from a WIM file, into another WIM file.
273  */
274 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim, 
275                                   int src_image, 
276                                   WIMStruct *dest_wim, 
277                                   const char *dest_name, 
278                                   const char *dest_description, 
279                                   int flags)
280 {
281         int i;
282         int ret;
283         struct dentry *root;
284         struct wim_pair wims;
285
286         if (src_image == WIM_ALL_IMAGES) {
287                 if (src_wim->hdr.image_count > 1) {
288
289                         /* multi-image export. */
290
291                         if ((flags & WIMLIB_EXPORT_FLAG_BOOT) && 
292                               (src_wim->hdr.boot_idx == 0))
293                         {
294                                 /* Specifying the boot flag on a multi-image
295                                  * source WIM makes the boot index default to
296                                  * the bootable image in the source WIM.  It is
297                                  * an error if there is no such bootable image.
298                                  * */
299                                 ERROR("Cannot specify `boot' flag when "
300                                       "exporting multiple images from a WIM "
301                                       "with no bootable images");
302                                 return WIMLIB_ERR_INVALID_PARAM;
303                         }
304                         if (dest_name || dest_description) {
305                                 ERROR("Image name or image description was "
306                                       "specified, but we are exporting "
307                                       "multiple images");
308                                 return WIMLIB_ERR_INVALID_PARAM;
309                         }
310                         for (i = 1; i <= src_wim->hdr.image_count; i++) {
311                                 int export_flags = flags;
312
313                                 if (i != src_wim->hdr.boot_idx)
314                                         export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
315
316                                 ret = wimlib_export_image(src_wim, i, dest_wim, 
317                                                           NULL,
318                                                           dest_description,
319                                                           export_flags);
320                                 if (ret != 0)
321                                         return ret;
322                         }
323                         return 0;
324                 } else {
325                         src_image = 1; 
326                 }
327         }
328
329         ret = wimlib_select_image(src_wim, src_image);
330         if (ret != 0) {
331                 ERROR("Could not select image %d from the WIM `%s' "
332                       "to export it", src_image, src_wim->filename);
333                 return ret;
334         }
335
336         if (!dest_name) {
337                 dest_name = wimlib_get_image_name(src_wim, src_image);
338                 DEBUG("Using name `%s' for source image %d",
339                       dest_name, src_image);
340         }
341
342         DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
343
344         if (wimlib_image_name_in_use(dest_wim, dest_name)) {
345                 ERROR("There is already an image named `%s' in the "
346                       "destination WIM", dest_name);
347                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
348         }
349
350
351         /* Cleaning up here on failure would be hard.  For example, we could
352          * fail to allocate memory in add_lookup_table_entry_to_dest_wim(),
353          * leaving the lookup table entries in the destination WIM in an
354          * inconsistent state.  Until these issues can be resolved,
355          * wimlib_export_image() is documented as leaving dest_wim is an
356          * indeterminate state.  */
357         root = wim_root_dentry(src_wim);
358         for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
359         wims.src_wim = src_wim;
360         wims.dest_wim = dest_wim;
361         ret = for_dentry_in_tree(root, add_lookup_table_entry_to_dest_wim, &wims);
362         if (ret != 0)
363                 return ret;
364         ret = add_new_dentry_tree(dest_wim, root);
365         if (ret != 0)
366                 return ret;
367         /* Bring over old security data */
368         struct wim_security_data *sd = wim_security_data(src_wim);
369         struct image_metadata *new_imd = wim_get_current_image_metadata(dest_wim);
370         wimlib_assert(sd);
371         free_security_data(new_imd->security_data);
372         new_imd->security_data = sd;
373         sd->refcnt++;
374
375         if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
376                 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
377                 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
378         }
379
380         return xml_export_image(src_wim->wim_info, src_image, &dest_wim->wim_info,
381                                 dest_name, dest_description);
382 }
383
384 /* 
385  * Deletes an image from the WIM. 
386  */
387 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
388 {
389         int num_images;
390         int i;
391         int ret;
392
393         if (image == WIM_ALL_IMAGES) {
394                 num_images = w->hdr.image_count;
395                 for (i = 1; i <= num_images; i++) {
396                         /* Always delete the first image, since by the end
397                          * there won't be any more than that!  */
398                         ret = wimlib_delete_image(w, 1);
399                         if (ret != 0)
400                                 return ret;
401                 }
402                 return 0;
403         }
404
405         DEBUG("Deleting image %d", image);
406
407         /* Even if the dentry tree is not allocated, we must select it (and
408          * therefore allocate it) so that we can decrement the reference counts
409          * in the lookup table.  */
410         ret = wimlib_select_image(w, image);
411         if (ret != 0)
412                 return ret;
413
414         /* Free the dentry tree, any lookup table entries that have their
415          * refcnt decremented to 0, and the security data. */
416         destroy_image_metadata(wim_get_current_image_metadata(w),
417                                w->lookup_table);
418
419         /* Get rid of the empty slot in the image metadata array. */
420         memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
421                 (w->hdr.image_count - image) * sizeof(struct image_metadata));
422
423         /* Decrement the image count. */
424         if (--w->hdr.image_count == 0) {
425                 FREE(w->image_metadata);
426                 w->image_metadata = NULL;
427         }
428
429         /* Fix the boot index. */
430         if (w->hdr.boot_idx == image)
431                 w->hdr.boot_idx = 0;
432         else if (w->hdr.boot_idx > image)
433                 w->hdr.boot_idx--;
434
435         w->current_image = WIM_NO_IMAGE;
436
437         /* Remove the image from the XML information. */
438         xml_delete_image(&w->wim_info, image);
439         return 0;
440 }
441
442 /*
443  * Adds an image to a WIM file from a directory tree on disk.
444  */
445 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir, 
446                                const char *name, const char *description, 
447                                const char *flags_element, int flags)
448 {
449         struct dentry *root_dentry;
450         struct stat root_stat;
451         struct image_metadata *imd;
452         int ret;
453
454         DEBUG("Adding dentry tree from dir `%s'.", dir);
455
456         if (!name || !*name) {
457                 ERROR("Must specify a non-empty string for the image name");
458                 return WIMLIB_ERR_INVALID_PARAM;
459         }
460         if (!dir) {
461                 ERROR("Must specify the name of a directory");
462                 return WIMLIB_ERR_INVALID_PARAM;
463         }
464
465         if (wimlib_image_name_in_use(w, name)) {
466                 ERROR("There is already an image named \"%s\" in `%s'",
467                       name, w->filename);
468                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
469         }
470
471         DEBUG("Creating root dentry.");
472
473         root_dentry = new_dentry("");
474         if (!root_dentry)
475                 return WIMLIB_ERR_NOMEM;
476         ret = calculate_dentry_full_path(root_dentry, NULL);
477
478         if (ret != 0)
479                 goto out_free_dentry_tree;
480
481         root_dentry->attributes |= FILE_ATTRIBUTE_DIRECTORY;
482
483         /* Construct the dentry tree from the outside filesystem. */
484         if (stat(dir, &root_stat) != 0) {
485                 ERROR_WITH_ERRNO("Failed to stat `%s'", dir);
486                 ret = WIMLIB_ERR_STAT;
487                 goto out_free_dentry_tree;
488         }
489         if (!S_ISDIR(root_stat.st_mode)) {
490                 ERROR("`%s' is not a directory", dir);
491                 ret = WIMLIB_ERR_NOTDIR;
492                 goto out_free_dentry_tree;
493         }
494         DEBUG("Building dentry tree.");
495         ret = build_dentry_tree(root_dentry, dir, &root_stat, w->lookup_table);
496
497         if (ret != 0) {
498                 ERROR("Failed to build dentry tree for `%s'", dir);
499                 goto out_free_dentry_tree;
500         }
501
502         DEBUG("Recalculating full paths of dentries.");
503         ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
504         if (ret != 0)
505                 goto out_free_dentry_tree;
506
507         ret = add_new_dentry_tree(w, root_dentry);
508         if (ret != 0)
509                 goto out_free_dentry_tree;
510
511         if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
512                 wimlib_set_boot_idx(w, w->hdr.image_count);
513
514         ret = xml_add_image(w, root_dentry, name, description, flags_element);
515         if (ret != 0)
516                 goto out_destroy_imd;
517
518         return 0;
519 out_destroy_imd:
520         destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
521                                w->lookup_table);
522         return ret;
523 out_free_dentry_tree:
524         free_dentry_tree(root_dentry, w->lookup_table, true);
525         return ret;
526 }