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