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