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