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