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