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