]> wimlib.net Git - wimlib/blob - src/modify.c
hardlink 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 #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                 void *symlink_buf = make_symlink_reparse_data_buf(deref_name_buf,
157                                                                   &symlink_buf_len);
158                 if (!symlink_buf)
159                         return WIMLIB_ERR_NOMEM;
160                 DEBUG("Made symlink reparse data buf (len = %zu, name len = %zu)",
161                                 symlink_buf_len, ret);
162                 
163                 u8 symlink_buf_hash[WIM_HASH_SIZE];
164                 sha1_buffer(symlink_buf, symlink_buf_len, symlink_buf_hash);
165
166                 ret = dentry_set_symlink_buf(root, symlink_buf_hash);
167
168                 if (ret != 0) {
169                         FREE(symlink_buf);
170                         return ret;
171                 }
172                 DEBUG("Created symlink buf");
173
174                 struct lookup_table_entry *lte;
175                 struct lookup_table_entry *existing_lte;
176
177                 existing_lte = __lookup_resource(lookup_table, symlink_buf_hash);
178                 if (existing_lte) {
179                         existing_lte->refcnt++;
180                 } else {
181                         DEBUG("Creating new lookup table entry");
182                         lte = new_lookup_table_entry();
183                         if (!lte) {
184                                 FREE(symlink_buf);
185                                 return WIMLIB_ERR_NOMEM;
186                         }
187                         lte->symlink_buf = symlink_buf;
188                         lte->resource_entry.original_size = symlink_buf_len;
189                         lte->resource_entry.size = symlink_buf_len;
190                         lte->is_symlink = true;
191                         memcpy(lte->hash, symlink_buf_hash, WIM_HASH_SIZE);
192                         lookup_table_insert(lookup_table, lte);
193                 }
194         } else {
195                 /* Regular file */
196                 struct lookup_table_entry *lte;
197
198                 /* For each regular file, we must check to see if the file is in
199                  * the lookup table already; if it is, we increment its refcnt;
200                  * otherwise, we create a new lookup table entry and insert it.
201                  * */
202                 ret = sha1sum(root_disk_path, root->hash);
203                 if (ret != 0)
204                         return ret;
205
206                 lte = __lookup_resource(lookup_table, root->hash);
207                 if (lte) {
208                         lte->refcnt++;
209                 } else {
210                         char *file_on_disk = STRDUP(root_disk_path);
211                         if (!file_on_disk) {
212                                 ERROR("Failed to allocate memory for file path");
213                                 return WIMLIB_ERR_NOMEM;
214                         }
215                         lte = new_lookup_table_entry();
216                         if (!lte) {
217                                 FREE(file_on_disk);
218                                 return WIMLIB_ERR_NOMEM;
219                         }
220                         lte->file_on_disk = file_on_disk;
221                         lte->resource_entry.original_size = root_stbuf.st_size;
222                         lte->resource_entry.size = root_stbuf.st_size;
223                         memcpy(lte->hash, root->hash, WIM_HASH_SIZE);
224                         lookup_table_insert(lookup_table, lte);
225                 }
226         }
227         return ret;
228 }
229
230 struct wim_pair {
231         WIMStruct *src_wim;
232         WIMStruct *dest_wim;
233 };
234
235 /* 
236  * This function takes in a dentry that was previously located only in image(s)
237  * in @src_wim, but now is being added to @dest_wim. If there is in fact already a
238  * lookup table entry for this file in the lookup table of the destination WIM
239  * file, we simply increment its reference count.  Otherwise, a new lookup table
240  * entry is created that references the location of the file resource in the
241  * source WIM file through the other_wim_fp field of the lookup table entry.
242  */
243 static int add_lookup_table_entry_to_dest_wim(struct dentry *dentry, void *arg)
244 {
245         WIMStruct *src_wim, *dest_wim;
246         struct lookup_table_entry *src_table_entry;
247         struct lookup_table_entry *dest_table_entry;
248
249         src_wim = ((struct wim_pair*)arg)->src_wim;
250         dest_wim = ((struct wim_pair*)arg)->dest_wim;
251
252         if (dentry_is_directory(dentry))
253                 return 0;
254
255         /* XXX ADS */
256         src_table_entry = __lookup_resource(src_wim->lookup_table, dentry->hash);
257         if (!src_table_entry)
258                 return 0;
259
260         /* XXX ADS */
261         dest_table_entry = __lookup_resource(dest_wim->lookup_table, dentry->hash);
262         if (dest_table_entry) {
263                 dest_table_entry->refcnt++;
264         } else {
265                 dest_table_entry = new_lookup_table_entry();
266                 if (!dest_table_entry)
267                         return WIMLIB_ERR_NOMEM;
268                 dest_table_entry->other_wim_fp = src_wim->fp;
269                 dest_table_entry->other_wim_ctype = 
270                                 wimlib_get_compression_type(src_wim);
271                 dest_table_entry->refcnt = 1;
272                 memcpy(&dest_table_entry->resource_entry, 
273                        &src_table_entry->resource_entry, 
274                        sizeof(struct resource_entry));
275                 memcpy(dest_table_entry->hash, dentry->hash, WIM_HASH_SIZE);
276                 lookup_table_insert(dest_wim->lookup_table, dest_table_entry);
277         }
278         return 0;
279 }
280
281 /*
282  * Adds an image (given by its dentry tree) to the image metadata array of a WIM
283  * file, adds an entry to the lookup table for the image metadata, updates the
284  * image count in the header, and selects the new image. 
285  *
286  * Does not update the XML data.
287  *
288  * @w:            The WIMStruct for the WIM file.
289  * @root_dentry:  The root of the directory tree for the image.
290  */
291 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry)
292 {
293         struct lookup_table_entry *metadata_lte;
294         struct image_metadata *imd;
295         struct image_metadata *new_imd;
296         struct wim_security_data *sd;
297         struct link_group_table *lgt;
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         lgt = new_link_group_table(9001);
321         if (!lgt)
322                 goto out_free_security_data;
323
324         metadata_lte->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
325         randomize_byte_array(metadata_lte->hash, WIM_HASH_SIZE);
326         lookup_table_insert(w->lookup_table, metadata_lte);
327
328         new_imd = &imd[w->hdr.image_count];
329
330         new_imd->root_dentry    = root_dentry;
331         new_imd->metadata_lte   = metadata_lte;
332         new_imd->security_data  = sd;
333         new_imd->lgt            = lgt;
334         new_imd->modified       = true;
335
336         FREE(w->image_metadata);
337         w->image_metadata       = imd;
338         w->hdr.image_count++;
339
340         /* Change the current image to the new one. */
341         return wimlib_select_image(w, w->hdr.image_count);
342 out_free_security_data:
343         FREE(sd);
344 out_free_metadata_lte:
345         FREE(metadata_lte);
346 out_free_imd:
347         FREE(imd);
348         return WIMLIB_ERR_NOMEM;
349
350 }
351
352 /*
353  * Copies an image, or all the images, from a WIM file, into another WIM file.
354  */
355 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim, 
356                                   int src_image, 
357                                   WIMStruct *dest_wim, 
358                                   const char *dest_name, 
359                                   const char *dest_description, 
360                                   int flags)
361 {
362         int i;
363         int ret;
364         struct dentry *root;
365         struct wim_pair wims;
366
367         if (src_image == WIM_ALL_IMAGES) {
368                 if (src_wim->hdr.image_count > 1) {
369
370                         /* multi-image export. */
371
372                         if ((flags & WIMLIB_EXPORT_FLAG_BOOT) && 
373                               (src_wim->hdr.boot_idx == 0))
374                         {
375                                 /* Specifying the boot flag on a multi-image
376                                  * source WIM makes the boot index default to
377                                  * the bootable image in the source WIM.  It is
378                                  * an error if there is no such bootable image.
379                                  * */
380                                 ERROR("Cannot specify `boot' flag when "
381                                       "exporting multiple images from a WIM "
382                                       "with no bootable images");
383                                 return WIMLIB_ERR_INVALID_PARAM;
384                         }
385                         if (dest_name || dest_description) {
386                                 ERROR("Image name or image description was "
387                                       "specified, but we are exporting "
388                                       "multiple images");
389                                 return WIMLIB_ERR_INVALID_PARAM;
390                         }
391                         for (i = 1; i <= src_wim->hdr.image_count; i++) {
392                                 int export_flags = flags;
393
394                                 if (i != src_wim->hdr.boot_idx)
395                                         export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
396
397                                 ret = wimlib_export_image(src_wim, i, dest_wim, 
398                                                           NULL,
399                                                           dest_description,
400                                                           export_flags);
401                                 if (ret != 0)
402                                         return ret;
403                         }
404                         return 0;
405                 } else {
406                         src_image = 1; 
407                 }
408         }
409
410         ret = wimlib_select_image(src_wim, src_image);
411         if (ret != 0) {
412                 ERROR("Could not select image %d from the WIM `%s' "
413                       "to export it", src_image, src_wim->filename);
414                 return ret;
415         }
416
417         if (!dest_name) {
418                 dest_name = wimlib_get_image_name(src_wim, src_image);
419                 DEBUG("Using name `%s' for source image %d",
420                       dest_name, src_image);
421         }
422
423         DEBUG("Exporting image %d from `%s'", src_image, src_wim->filename);
424
425         if (wimlib_image_name_in_use(dest_wim, dest_name)) {
426                 ERROR("There is already an image named `%s' in the "
427                       "destination WIM", dest_name);
428                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
429         }
430
431
432         /* Cleaning up here on failure would be hard.  For example, we could
433          * fail to allocate memory in add_lookup_table_entry_to_dest_wim(),
434          * leaving the lookup table entries in the destination WIM in an
435          * inconsistent state.  Until these issues can be resolved,
436          * wimlib_export_image() is documented as leaving dest_wim is an
437          * indeterminate state.  */
438         root = wim_root_dentry(src_wim);
439         for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
440         wims.src_wim = src_wim;
441         wims.dest_wim = dest_wim;
442         ret = for_dentry_in_tree(root, add_lookup_table_entry_to_dest_wim, &wims);
443         if (ret != 0)
444                 return ret;
445         ret = add_new_dentry_tree(dest_wim, root);
446         if (ret != 0)
447                 return ret;
448         /* Bring over old security data */
449         struct wim_security_data *sd = wim_security_data(src_wim);
450         struct image_metadata *new_imd = wim_get_current_image_metadata(dest_wim);
451         wimlib_assert(sd);
452         free_security_data(new_imd->security_data);
453         new_imd->security_data = sd;
454         sd->refcnt++;
455
456         if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
457                 DEBUG("Setting boot_idx to %d", dest_wim->hdr.image_count);
458                 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
459         }
460
461         return xml_export_image(src_wim->wim_info, src_image, &dest_wim->wim_info,
462                                 dest_name, dest_description);
463 }
464
465 /* 
466  * Deletes an image from the WIM. 
467  */
468 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
469 {
470         int num_images;
471         int i;
472         int ret;
473
474         if (image == WIM_ALL_IMAGES) {
475                 num_images = w->hdr.image_count;
476                 for (i = 1; i <= num_images; i++) {
477                         /* Always delete the first image, since by the end
478                          * there won't be any more than that!  */
479                         ret = wimlib_delete_image(w, 1);
480                         if (ret != 0)
481                                 return ret;
482                 }
483                 return 0;
484         }
485
486         DEBUG("Deleting image %d", image);
487
488         /* Even if the dentry tree is not allocated, we must select it (and
489          * therefore allocate it) so that we can decrement the reference counts
490          * in the lookup table.  */
491         ret = wimlib_select_image(w, image);
492         if (ret != 0)
493                 return ret;
494
495         /* Free the dentry tree, any lookup table entries that have their
496          * refcnt decremented to 0, and the security data. */
497         destroy_image_metadata(wim_get_current_image_metadata(w),
498                                w->lookup_table);
499
500         /* Get rid of the empty slot in the image metadata array. */
501         memmove(&w->image_metadata[image - 1], &w->image_metadata[image],
502                 (w->hdr.image_count - image) * sizeof(struct image_metadata));
503
504         /* Decrement the image count. */
505         if (--w->hdr.image_count == 0) {
506                 FREE(w->image_metadata);
507                 w->image_metadata = NULL;
508         }
509
510         /* Fix the boot index. */
511         if (w->hdr.boot_idx == image)
512                 w->hdr.boot_idx = 0;
513         else if (w->hdr.boot_idx > image)
514                 w->hdr.boot_idx--;
515
516         w->current_image = WIM_NO_IMAGE;
517
518         /* Remove the image from the XML information. */
519         xml_delete_image(&w->wim_info, image);
520         return 0;
521 }
522
523 /*
524  * Adds an image to a WIM file from a directory tree on disk.
525  */
526 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir, 
527                                const char *name, const char *description, 
528                                const char *flags_element, int flags)
529 {
530         struct dentry *root_dentry;
531         struct image_metadata *imd;
532         int ret;
533
534         DEBUG("Adding dentry tree from dir `%s'.", dir);
535
536         if (!name || !*name) {
537                 ERROR("Must specify a non-empty string for the image name");
538                 return WIMLIB_ERR_INVALID_PARAM;
539         }
540         if (!dir) {
541                 ERROR("Must specify the name of a directory");
542                 return WIMLIB_ERR_INVALID_PARAM;
543         }
544
545         if (wimlib_image_name_in_use(w, name)) {
546                 ERROR("There is already an image named \"%s\" in `%s'",
547                       name, w->filename);
548                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
549         }
550
551         DEBUG("Creating root dentry.");
552
553         root_dentry = new_dentry("");
554         if (!root_dentry)
555                 return WIMLIB_ERR_NOMEM;
556         ret = calculate_dentry_full_path(root_dentry, NULL);
557
558         if (ret != 0)
559                 goto out_free_dentry_tree;
560
561         root_dentry->attributes |= FILE_ATTRIBUTE_DIRECTORY;
562
563         DEBUG("Building dentry tree.");
564         ret = build_dentry_tree(root_dentry, dir, w->lookup_table,
565                                 flags | WIMLIB_ADD_IMAGE_FLAG_ROOT);
566
567         if (ret != 0) {
568                 ERROR("Failed to build dentry tree for `%s'", dir);
569                 goto out_free_dentry_tree;
570         }
571
572         DEBUG("Recalculating full paths of dentries.");
573         ret = for_dentry_in_tree(root_dentry, calculate_dentry_full_path, NULL);
574         if (ret != 0)
575                 goto out_free_dentry_tree;
576
577         ret = add_new_dentry_tree(w, root_dentry);
578         if (ret != 0)
579                 goto out_free_dentry_tree;
580
581         DEBUG("Inserting dentries into hard link group table");
582         ret = for_dentry_in_tree(root_dentry, link_group_table_insert, 
583                                  w->image_metadata[w->hdr.image_count - 1].lgt);
584         if (ret != 0)
585                 goto out_destroy_imd;
586         DEBUG("Assigning hard link groups");
587         assign_link_groups(w->image_metadata[w->hdr.image_count - 1].lgt);
588
589         if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT)
590                 wimlib_set_boot_idx(w, w->hdr.image_count);
591
592         ret = xml_add_image(w, root_dentry, name, description, flags_element);
593         if (ret != 0)
594                 goto out_destroy_imd;
595
596         return 0;
597 out_destroy_imd:
598         destroy_image_metadata(&w->image_metadata[w->hdr.image_count - 1],
599                                w->lookup_table);
600         w->hdr.image_count--;
601         return ret;
602 out_free_dentry_tree:
603         free_dentry_tree(root_dentry, w->lookup_table, true);
604         return ret;
605 }