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