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