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