]> wimlib.net Git - wimlib/blob - src/modify.c
60149f8b85baa7ce370626374772b9a1f6599314
[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  * Copyright (C) 2012 Eric Biggers
10  *
11  * wimlib - Library for working with WIM files 
12  *
13  * This library is free software; you can redistribute it and/or modify it under
14  * the terms of the GNU Lesser General Public License as published by the Free
15  * Software Foundation; either version 2.1 of the License, or (at your option) any
16  * later version.
17  *
18  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
20  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with this library; if not, write to the Free Software Foundation, Inc., 59
24  * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
25  */
26
27 #include "wimlib_internal.h"
28 #include "util.h"
29 #include "dentry.h"
30 #include "xml.h"
31 #include "lookup_table.h"
32 #include <sys/stat.h>
33 #include <dirent.h>
34 #include <string.h>
35 #include <errno.h>
36
37 /* 
38  * Recursively builds a dentry tree from a directory tree on disk, outside the
39  * WIM file.
40  *
41  * @root:  A dentry that has already been created for the root of the dentry
42  *      tree.
43  * @source_path:  The path to the root of the tree on disk. 
44  * @root_stat:   A pointer to a `struct stat' that contains the metadata for the
45  *      root of the tree on disk. 
46  * @lookup_table: The lookup table for the WIM file.  For each file added to the
47  *              dentry tree being built, an entry is added to the lookup table, 
48  *              unless an identical file is already in the lookup table.  These
49  *              lookup table entries that are added point to the file on disk.
50  *
51  * @return:     0 on success, nonzero on failure.  It is a failure if any of
52  *              the files cannot be `stat'ed, or if any of the needed
53  *              directories cannot be opened or read.  Failure to add the files
54  *              to the WIM may still occur later when trying to actually read 
55  *              the regular files in the tree into the WIM as file resources.
56  */
57 static int build_dentry_tree(struct dentry *root, const char *source_path, 
58                         struct stat *root_stat, struct lookup_table* lookup_table)
59 {
60         int ret = 0;
61
62         stbuf_to_dentry(root_stat, root);
63         if (dentry_is_directory(root)) {
64                 /* Open the directory on disk */
65                 DIR *dir;
66                 struct dirent *p;
67                 struct stat child_stat;
68                 struct dentry *child;
69
70                 dir = opendir(source_path);
71                 if (!dir) {
72                         ERROR("Failed to open the directory `%s': %m\n",
73                                         source_path);
74                         return WIMLIB_ERR_OPEN;
75                 }
76
77                 /* Buffer for names of files in directory. */
78                 size_t len = strlen(source_path);
79                 char name[len + 1 + FILENAME_MAX + 1];
80                 memcpy(name, source_path, len);
81                 name[len] = '/';
82                 errno = 0;
83
84                 /* Create a dentry for each entry in the directory on disk, and recurse
85                  * to any subdirectories. */
86                 while ((p = readdir(dir)) != NULL) {
87                         if (p->d_name[0] == '.' && (p->d_name[1] == '\0'
88                                 || (p->d_name[1] == '.' && p->d_name[2] == '\0')))
89                                         continue;
90                         strcpy(name + len + 1, p->d_name);
91                         if (stat(name, &child_stat) != 0) {
92                                 ERROR("cannot stat `%s': %m\n", name);
93                                 ret = WIMLIB_ERR_STAT;
94                                 break;
95                         }
96                         child = new_dentry(p->d_name);
97                         ret = build_dentry_tree(child, name, &child_stat, 
98                                                 lookup_table);
99                         if (ret != 0)
100                                 break;
101                         link_dentry(child, root);
102                 }
103                 closedir(dir);
104         } else {
105                 struct lookup_table_entry *lte;
106
107                 /* For each non-directory, we must check to see if the file is
108                  * in the lookup table already; if it is, we increment its
109                  * refcnt; otherwise, we create a new lookup table entry and
110                  * insert it. */
111                 ret = sha1sum(source_path, root->hash);
112                 if (ret != 0) {
113                         ERROR("Failed to calculate sha1sum for file `%s'\n", 
114                                                 source_path);
115                         return ret;
116                 }
117
118                 lte = lookup_resource(lookup_table, root->hash);
119                 if (lte) {
120                         lte->refcnt++;
121                 } else {
122                         char *file_on_disk = STRDUP(source_path);
123                         if (!file_on_disk) {
124                                 ERROR("Failed to allocate memory for file "
125                                                 "path!\n");
126                                 return WIMLIB_ERR_NOMEM;
127                         }
128                         lte = new_lookup_table_entry();
129                         if (!lte) {
130                                 ERROR("Failed to allocate memory for new "
131                                                 "lookup table entry!\n");
132                                 FREE(file_on_disk);
133                                 return WIMLIB_ERR_NOMEM;
134                         }
135                         lte->file_on_disk = file_on_disk;
136                         lte->resource_entry.flags = 0;
137                         lte->refcnt       = 1;
138                         lte->part_number  = 1;
139                         lte->resource_entry.original_size = root_stat->st_size;
140                         lte->resource_entry.size = root_stat->st_size;
141                         memcpy(lte->hash, root->hash, WIM_HASH_SIZE);
142                         lookup_table_insert(lookup_table, lte);
143                 }
144         }
145         return ret;
146 }
147
148 struct wim_pair {
149         WIMStruct *src_wim;
150         WIMStruct *dest_wim;
151 };
152
153 /* 
154  * This function takes in a dentry that was previously located only in image(s)
155  * in @src_wim, but now is being added to @dest_wim. If there is in fact already a
156  * lookup table entry for this file in the lookup table of the destination WIM
157  * file, we simply increment its reference count.  Otherwise, a new lookup table
158  * entry is created that references the location of the file resource in the
159  * source WIM file through the other_wim_fp field of the lookup table entry.
160  */
161 static int add_lookup_table_entry_to_dest_wim(struct dentry *dentry, void *arg)
162 {
163         WIMStruct *src_wim, *dest_wim;
164         struct lookup_table_entry *src_table_entry;
165         struct lookup_table_entry *dest_table_entry;
166
167         src_wim = ((struct wim_pair*)arg)->src_wim;
168         dest_wim = ((struct wim_pair*)arg)->dest_wim;
169
170         if (dentry_is_directory(dentry))
171                 return 0;
172
173         src_table_entry = wim_lookup_resource(src_wim, dentry);
174         if (!src_table_entry)
175                 return 0;
176
177         dest_table_entry = wim_lookup_resource(dest_wim, dentry);
178         if (dest_table_entry) {
179                 dest_table_entry->refcnt++;
180         } else {
181                 dest_table_entry = new_lookup_table_entry();
182                 if (!dest_table_entry) {
183                         ERROR("Could not allocate lookup table entry!\n");
184                         return WIMLIB_ERR_NOMEM;
185                 }
186                 dest_table_entry->other_wim_fp = src_wim->fp;
187                 dest_table_entry->other_wim_ctype = 
188                                 wimlib_get_compression_type(src_wim);
189                 dest_table_entry->refcnt = 1;
190                 memcpy(&dest_table_entry->resource_entry, 
191                        &src_table_entry->resource_entry, 
192                        sizeof(struct resource_entry));
193                 memcpy(dest_table_entry->hash, dentry->hash, WIM_HASH_SIZE);
194                 lookup_table_insert(dest_wim->lookup_table, dest_table_entry);
195         }
196         return 0;
197 }
198
199 /*
200  * Adds an image (given by its dentry tree) to the image metadata array of a WIM
201  * file, adds an entry to the lookup table for the image metadata, updates the
202  * image count in the header, and selects the new image. 
203  *
204  * Does not update the XML data.
205  *
206  * @w:            The WIMStruct for the WIM file.
207  * @root_dentry:  The root of the directory tree for the image.
208  */
209 static int add_new_dentry_tree(WIMStruct *w, struct dentry *root_dentry)
210 {
211         struct lookup_table_entry *imd_lookup_entry;
212         struct image_metadata *imd;
213         struct image_metadata *new_imd;
214
215         DEBUG("Reallocing image metadata array for image_count = %u\n",
216                         w->hdr.image_count + 1);
217         imd = CALLOC((w->hdr.image_count + 1), sizeof(struct image_metadata));
218
219         if (!imd) {
220                 ERROR("Failed to allocate memory for new image metadata "
221                                 "array!\n");
222                 return WIMLIB_ERR_NOMEM;
223         }
224
225         memcpy(imd, w->image_metadata, 
226                w->hdr.image_count * sizeof(struct image_metadata));
227         
228         imd_lookup_entry = new_lookup_table_entry();
229         if (!imd_lookup_entry) {
230                 ERROR("Failed to allocate new lookup table entry!\n");
231                 FREE(imd);
232                 return WIMLIB_ERR_NOMEM;
233         }
234
235         imd_lookup_entry->resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
236         randomize_byte_array(imd_lookup_entry->hash, WIM_HASH_SIZE);
237         lookup_table_insert(w->lookup_table, imd_lookup_entry);
238
239         w->hdr.image_count++;
240
241         new_imd = &imd[w->hdr.image_count - 1];
242         new_imd->lookup_table_entry = imd_lookup_entry;
243         new_imd->modified           = true;
244         new_imd->root_dentry        = root_dentry;
245         w->image_metadata = imd;
246
247         /* Change the current image to the new one. */
248         return wimlib_select_image(w, w->hdr.image_count);
249 }
250
251 /*
252  * Copies an image, or all the images, from a WIM file, into another WIM file.
253  */
254 WIMLIBAPI int wimlib_export_image(WIMStruct *src_wim, 
255                                   int src_image, 
256                                   WIMStruct *dest_wim, 
257                                   const char *dest_name, 
258                                   const char *dest_description, 
259                                   int flags)
260 {
261         int boot_idx;
262         int i;
263         int ret;
264         struct dentry *root;
265         struct wim_pair wims;
266
267         if (src_image == WIM_ALL_IMAGES) {
268                 if (src_wim->hdr.image_count > 1) {
269
270                         /* multi-image export. */
271
272                         if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
273
274                                 /* Specifying the boot flag on a multi-image
275                                  * source WIM makes the boot index default to
276                                  * the bootable image in the source WIM.  It is
277                                  * an error if there is no such bootable image.
278                                  * */
279
280                                 if (src_wim->hdr.boot_idx == 0) {
281                                         ERROR("Cannot specify `boot' flag "
282                                                         "when exporting multiple "
283                                                         "images from a WIM with no "
284                                                         "bootable images!\n");
285                                         return WIMLIB_ERR_INVALID_PARAM;
286                                 } else {
287                                         boot_idx = src_wim->hdr.boot_idx;
288                                 }
289                         }
290                         if (dest_name || dest_description) {
291                                 ERROR("Image name or image description "
292                                                 "was specified, but we are exporting "
293                                                 "multiple images!\n");
294                                 return WIMLIB_ERR_INVALID_PARAM;
295                         }
296                         for (i = 1; i <= src_wim->hdr.image_count; i++) {
297                                 int export_flags = flags;
298
299                                 if (i != boot_idx)
300                                         export_flags &= ~WIMLIB_EXPORT_FLAG_BOOT;
301
302                                 ret = wimlib_export_image(src_wim, i, dest_wim, 
303                                                         NULL, dest_description,
304                                                         export_flags);
305                                 if (ret != 0)
306                                         return ret;
307                         }
308                         return 0;
309                 } else {
310                         src_image = 1; 
311                 }
312         }
313
314         ret = wimlib_select_image(src_wim, src_image);
315         if (ret != 0) {
316                 ERROR("Could not select image %d from the WIM `%s' "
317                         "to export it!\n", src_image, src_wim->filename);
318                 return ret;
319         }
320
321         if (!dest_name) {
322                 dest_name = wimlib_get_image_name(src_wim, src_image);
323                 DEBUG("Using name `%s' for source image %d\n", 
324                                 dest_name, src_image);
325         }
326
327         DEBUG("Exporting image %d from `%s'\n", src_image, src_wim->filename);
328
329         if (wimlib_image_name_in_use(dest_wim, dest_name)) {
330                 ERROR("There is already an image named `%s' "
331                         "in the destination WIM!\n", dest_name);
332                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
333         }
334
335
336         root = wim_root_dentry(src_wim);
337         for_dentry_in_tree(root, increment_dentry_refcnt, NULL);
338         wims.src_wim = src_wim;
339         wims.dest_wim = dest_wim;
340         for_dentry_in_tree(root, add_lookup_table_entry_to_dest_wim, &wims);
341         ret = add_new_dentry_tree(dest_wim, root);
342 #ifdef ENABLE_SECURITY_DATA
343         struct wim_security_data *sd = wim_security_data(src_wim);
344         struct image_metadata *new_imd = wim_get_current_image_metadata(dest_wim);
345         new_imd->security_data = sd;
346         if (sd)
347                 sd->refcnt++;
348 #endif
349         if (ret != 0)
350                 return ret;
351
352         if (flags & WIMLIB_EXPORT_FLAG_BOOT) {
353                 DEBUG("Setting boot_idx to %d\n", dest_wim->hdr.image_count);
354                 dest_wim->hdr.boot_idx = dest_wim->hdr.image_count;
355         }
356
357         return xml_export_image(src_wim->wim_info, src_image, &dest_wim->wim_info,
358                         dest_name, dest_description);
359 }
360
361 /* 
362  * Deletes an image from the WIM. 
363  */
364 WIMLIBAPI int wimlib_delete_image(WIMStruct *w, int image)
365 {
366         int num_images;
367         int i;
368         int ret;
369         struct image_metadata *imd;
370
371         if (image == WIM_ALL_IMAGES) {
372                 num_images = w->hdr.image_count;
373                 for (i = 1; i <= num_images; i++) {
374                         /* Always delete the first image, since by the end
375                          * there won't be any more than that!  */
376                         ret = wimlib_delete_image(w, 1);
377                         if (ret != 0)
378                                 return ret;
379                 }
380                 return 0;
381         }
382
383         DEBUG("Deleting image %d\n", image);
384
385         /* Even if the dentry tree is not allocated, we must select it (and
386          * therefore allocate it) so that we can decrement the reference counts
387          * in the lookup table.  */
388         ret = wimlib_select_image(w, image);
389         if (ret != 0)
390                 return ret;
391
392         /* Free the dentry tree, any lookup table entries that have their
393          * refcnt decremented to 0, and the security data. */
394         imd = wim_get_current_image_metadata(w);
395         free_dentry_tree(imd->root_dentry, w->lookup_table, true);
396 #ifdef ENABLE_SECURITY_DATA
397         free_security_data(imd->security_data);
398 #endif
399
400         /* Get rid of the lookup table entry for this image's metadata resource
401          * */
402         lookup_table_remove(w->lookup_table, imd->lookup_table_entry);
403
404         /* Get rid of the empty slot in the image metadata array. */
405         for (i = image - 1; i < w->hdr.image_count - 1; i++)
406                 memcpy(&w->image_metadata[i], &w->image_metadata[i + 1],
407                                 sizeof(struct image_metadata));
408
409         /* Decrement the image count. */
410         w->hdr.image_count--;
411         if (w->hdr.image_count == 0) {
412                 FREE(w->image_metadata);
413                 w->image_metadata = NULL;
414         }
415
416         /* Fix the boot index. */
417         if (w->hdr.boot_idx == image)
418                 w->hdr.boot_idx = 0;
419         else if (w->hdr.boot_idx > image)
420                 w->hdr.boot_idx--;
421
422         w->current_image = WIM_NO_IMAGE;
423
424         /* Remove the image from the XML information. */
425         xml_delete_image(&w->wim_info, image);
426         return 0;
427 }
428
429 /*
430  * Adds an image to a WIM file from a directory tree on disk.
431  */
432 WIMLIBAPI int wimlib_add_image(WIMStruct *w, const char *dir, 
433                                const char *name, const char *description, 
434                                const char *flags_element, int flags)
435 {
436         struct dentry *root_dentry;
437         struct stat root_stat;
438         int ret;
439
440         DEBUG("Adding dentry tree from dir `%s'\n", dir);
441
442         if (!name || !*name) {
443                 ERROR("Must specify a non-empty string for the image name!\n");
444                 return WIMLIB_ERR_INVALID_PARAM;
445         }
446         if (!dir) {
447                 ERROR("Must specify the name of a directory!\n");
448                 return WIMLIB_ERR_INVALID_PARAM;
449         }
450
451         if (wimlib_image_name_in_use(w, name)) {
452                 ERROR("There is already an image named `%s' in %s!\n",
453                                 name, w->filename);
454                 return WIMLIB_ERR_IMAGE_NAME_COLLISION;
455         }
456
457         DEBUG("Creating root dentry.\n");
458
459         root_dentry = new_dentry("");
460         ret = calculate_dentry_full_path(root_dentry, NULL);
461         if (ret != 0)
462                 return ret;
463         root_dentry->attributes |= WIM_FILE_ATTRIBUTE_DIRECTORY;
464
465         /* Construct the dentry tree from the outside filesystem. */
466         if (stat(dir, &root_stat) != 0) {
467                 ERROR("Failed to stat `%s': %m\n", dir);
468                 return WIMLIB_ERR_STAT;
469         }
470         if (!S_ISDIR(root_stat.st_mode)) {
471                 ERROR("`%s' is not a directory!\n", dir);
472                 return WIMLIB_ERR_NOTDIR;
473         }
474         DEBUG("Building dentry tree.\n");
475         ret = build_dentry_tree(root_dentry, dir, &root_stat, 
476                                 w->lookup_table);
477
478         if (ret != 0) {
479                 ERROR("Failed to build dentry tree for `%s'!\n", dir);
480                 goto err1;
481         }
482
483         DEBUG("Recalculating full paths of dentries.\n");
484         ret = for_dentry_in_tree(root_dentry, 
485                                  calculate_dentry_full_path, NULL);
486         if (ret != 0) {
487                 ERROR("Failed to calculate full paths of dentry tree.\n");
488                 goto err1;
489         }
490
491         ret = add_new_dentry_tree(w, root_dentry);
492         if (ret != 0)
493                 goto err1;
494
495         if (flags & WIMLIB_ADD_IMAGE_FLAG_BOOT) {
496                 /* Call wimlib_set_boot_idx rather than set boot_idx directly so
497                  * that the boot metadata resource entry in the header gets
498                  * updated. */
499                 wimlib_set_boot_idx(w, w->hdr.image_count);
500         }
501
502         ret = xml_add_image(w, root_dentry, name, description, flags_element);
503         if (ret != 0)
504                 goto err1;
505
506         return 0;
507 err1:
508         free_dentry_tree(root_dentry, w->lookup_table, true);
509         return ret;
510 }