4 * Apply a WIM image to a NTFS volume, restoring everything we can, including
5 * security data and alternate data streams. There should be no loss of
10 * Copyright (C) 2012 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU Lesser General Public License as published by the Free
16 * Software Foundation; either version 2.1 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
34 #include "lookup_table.h"
36 #include <ntfs-3g/layout.h>
37 #include <ntfs-3g/acls.h>
38 #include <ntfs-3g/attrib.h>
39 #include <ntfs-3g/misc.h>
40 #include <ntfs-3g/reparse.h>
41 #include <ntfs-3g/security.h>
42 #include <ntfs-3g/volume.h>
46 struct ntfs_apply_args {
52 extern int _ntfs_set_file_security(ntfs_inode *ni, u32 selection,
54 extern int _ntfs_set_file_attributes(ntfs_inode *ni, s32 attrib);
57 * Extracts a WIM resource to a NTFS attribute.
60 extract_wim_resource_to_ntfs_attr(const struct lookup_table_entry *lte,
63 u64 bytes_remaining = wim_resource_size(lte);
64 char buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
67 u8 hash[SHA1_HASH_SIZE];
72 while (bytes_remaining) {
73 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
74 ret = read_wim_resource(lte, buf, to_read, offset, false);
77 sha1_update(&ctx, buf, to_read);
78 if (ntfs_attr_pwrite(na, offset, to_read, buf) != to_read) {
79 ERROR_WITH_ERRNO("Error extracting WIM resource");
80 return WIMLIB_ERR_WRITE;
82 bytes_remaining -= to_read;
85 sha1_final(hash, &ctx);
86 if (!hashes_equal(hash, lte->hash)) {
87 ERROR("Invalid checksum on a WIM resource "
88 "(detected when extracting to NTFS stream file)");
89 ERROR("The following WIM resource is invalid:");
90 print_lookup_table_entry(lte);
91 return WIMLIB_ERR_INVALID_RESOURCE_HASH;
96 /* Writes the data streams to a NTFS file
98 * @ni: The NTFS inode for the file.
99 * @dentry: The directory entry in the WIM file.
100 * @w: The WIMStruct for the WIM containing the image we are applying.
102 * Returns 0 on success, nonzero on failure.
104 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
108 unsigned stream_idx = 0;
109 ntfschar *stream_name = AT_UNNAMED;
110 u32 stream_name_len = 0;
112 DEBUG("Writing %u NTFS data stream%s for `%s'",
114 (dentry->num_ads == 0 ? "" : "s"),
115 dentry->full_path_utf8);
118 struct lookup_table_entry *lte;
121 lte = dentry_stream_lte(dentry, stream_idx, w->lookup_table);
122 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
124 ERROR_WITH_ERRNO("Failed to open a data stream of "
125 "extracted file `%s'",
126 dentry->full_path_utf8);
127 ret = WIMLIB_ERR_NTFS_3G;
131 ret = extract_wim_resource_to_ntfs_attr(lte, na);
135 if (stream_idx == dentry->num_ads)
137 stream_name = (ntfschar*)dentry->ads_entries[stream_idx].stream_name;
138 stream_name_len = dentry->ads_entries[stream_idx].stream_name_len / 2;
145 * Makes a NTFS hard link
147 * It is named @from_dentry->file_name and is located under the directory
148 * specified by @dir_ni, and it is made to point to the previously extracted
149 * file located at @to_dentry->extracted_file.
151 * Return 0 on success, nonzero on failure.
153 static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
154 const struct dentry *to_dentry,
156 ntfs_inode **to_ni_ret)
161 wimlib_assert(dentry_is_regular_file(from_dentry)
162 && dentry_is_regular_file(to_dentry));
164 DEBUG("Extracting NTFS hard link `%s' => `%s'",
165 from_dentry->full_path_utf8, to_dentry->extracted_file);
167 to_ni = ntfs_pathname_to_inode(dir_ni->vol, NULL,
168 to_dentry->extracted_file);
170 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
171 to_dentry->extracted_file);
172 return WIMLIB_ERR_NTFS_3G;
174 ret = ntfs_link(to_ni, dir_ni,
175 (ntfschar*)from_dentry->file_name,
176 from_dentry->file_name_len / 2);
178 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
179 from_dentry->full_path_utf8,
180 to_dentry->extracted_file);
181 ret = WIMLIB_ERR_NTFS_3G;
188 apply_file_attributes_and_security_data(ntfs_inode *ni,
189 const struct dentry *dentry,
192 DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
193 dentry->full_path_utf8, dentry->attributes);
194 if (!_ntfs_set_file_attributes(ni, dentry->attributes)) {
195 ERROR("Failed to set NTFS file attributes on `%s'",
196 dentry->full_path_utf8);
197 return WIMLIB_ERR_NTFS_3G;
200 if (dentry->security_id != -1) {
201 const struct wim_security_data *sd;
203 sd = wim_const_security_data(w);
204 wimlib_assert(dentry->security_id < sd->num_entries);
205 DEBUG("Applying security descriptor %d to `%s'",
206 dentry->security_id, dentry->full_path_utf8);
207 u32 selection = OWNER_SECURITY_INFORMATION |
208 GROUP_SECURITY_INFORMATION |
209 DACL_SECURITY_INFORMATION |
210 SACL_SECURITY_INFORMATION;
212 if (!_ntfs_set_file_security(ni, selection,
213 sd->descriptors[dentry->security_id]))
215 ERROR_WITH_ERRNO("Failed to set security data on `%s'",
216 dentry->full_path_utf8);
217 return WIMLIB_ERR_NTFS_3G;
223 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
226 struct lookup_table_entry *lte;
229 wimlib_assert(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
231 lte = dentry_first_lte(dentry, w->lookup_table);
234 ERROR("Could not find reparse data for `%s'",
235 dentry->full_path_utf8);
236 return WIMLIB_ERR_INVALID_DENTRY;
239 if (wim_resource_size(lte) >= 0xffff) {
240 ERROR("Reparse data of `%s' is too long (%lu bytes)",
241 dentry->full_path_utf8, wim_resource_size(lte));
242 return WIMLIB_ERR_INVALID_DENTRY;
245 char reparse_data_buf[8 + wim_resource_size(lte)];
246 char *p = reparse_data_buf;
247 p = put_u32(p, dentry->reparse_tag); /* ReparseTag */
248 p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
249 p = put_u16(p, 0); /* Reserved */
251 ret = read_full_wim_resource(lte, p);
255 ret = ntfs_set_ntfs_reparse_data(ni, reparse_data_buf,
256 wim_resource_size(lte) + 8, 0);
258 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
259 dentry->full_path_utf8);
260 return WIMLIB_ERR_NTFS_3G;
266 * Applies a WIM dentry to a NTFS filesystem.
268 * @dentry: The WIM dentry to apply
269 * @dir_ni: The NTFS inode for the parent directory
270 * @w: The WIMStruct for the WIM containing the image we are applying.
272 * @return: 0 on success; nonzero on failure.
274 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
279 ntfs_inode *ni = NULL;
280 bool is_hardlink = false;
281 ntfs_volume *vol = dir_ni->vol;
283 if (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY) {
286 struct dentry *other;
289 /* If this dentry is one of a hard link set of at least 2
290 * dentries. If one of the other dentries has already
291 * been extracted, make a hard link to it. Otherwise,
292 * extract the file, and set the dentry->extracted_file
293 * field so that other dentries in the hard link group
295 list_for_each_entry(other, &dentry->link_group_list,
298 if (other->extracted_file) {
300 ret = wim_apply_hardlink_ntfs(dentry,
305 goto out_close_dir_ni;
307 goto out_set_dos_name;
310 FREE(dentry->extracted_file);
311 dentry->extracted_file = STRDUP(dentry->full_path_utf8);
312 if (!dentry->extracted_file) {
313 ERROR("Failed to allocate memory for filename");
314 return WIMLIB_ERR_NOMEM;
319 * Create a directory or file.
321 * Note: For symbolic links that are not directory junctions, pass
322 * S_IFREG here, since we manually set the reparse data later.
324 ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
325 dentry->file_name_len / 2, type);
328 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
329 dentry->full_path_utf8);
330 ret = WIMLIB_ERR_NTFS_3G;
331 goto out_close_dir_ni;
334 /* Write the data streams, unless this is a directory or reparse point
336 if (!dentry_is_directory(dentry) &&
337 !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
338 ret = write_ntfs_data_streams(ni, dentry, w);
340 goto out_close_dir_ni;
344 ret = apply_file_attributes_and_security_data(ni, dentry, w);
346 goto out_close_dir_ni;
348 if (dentry->attributes & FILE_ATTR_REPARSE_POINT) {
349 ret = apply_reparse_data(ni, dentry, w);
351 goto out_close_dir_ni;
355 /* Set DOS (short) name if given */
356 if (dentry->short_name_len != 0) {
358 char *short_name_utf8;
359 size_t short_name_utf8_len;
360 short_name_utf8 = utf16_to_utf8(dentry->short_name,
361 dentry->short_name_len,
362 &short_name_utf8_len);
363 if (!short_name_utf8) {
364 ERROR("Out of memory");
365 ret = WIMLIB_ERR_NOMEM;
366 goto out_close_dir_ni;
372 const char *dir_name;
374 /* ntfs_set_ntfs_dos_name() closes the inodes in the
375 * wrong order if we have applied a hard link. Close
376 * them ourselves, then re-open then. */
377 if (ntfs_inode_close(dir_ni) != 0) {
379 ret = WIMLIB_ERR_NTFS_3G;
380 ERROR_WITH_ERRNO("Failed to close directory inode");
382 if (ntfs_inode_close(ni) != 0) {
384 ret = WIMLIB_ERR_NTFS_3G;
385 ERROR_WITH_ERRNO("Failed to close hard link target inode");
387 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
394 dir_name = dentry->full_path_utf8;
396 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
399 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
401 return WIMLIB_ERR_NTFS_3G;
403 ni = ntfs_pathname_to_inode(vol, dir_ni,
404 dentry->file_name_utf8);
406 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
408 return WIMLIB_ERR_NTFS_3G;
412 DEBUG("Setting short (DOS) name of `%s' to %s",
413 dentry->full_path_utf8, short_name_utf8);
415 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
416 short_name_utf8_len, 0);
417 FREE(short_name_utf8);
419 ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
420 dentry->full_path_utf8);
421 ret = WIMLIB_ERR_NTFS_3G;
423 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
428 if (ntfs_inode_close(dir_ni) != 0) {
430 ret = WIMLIB_ERR_NTFS_3G;
431 ERROR_WITH_ERRNO("Failed to close directory inode");
434 if (ni && ntfs_inode_close(ni) != 0) {
436 ret = WIMLIB_ERR_NTFS_3G;
437 ERROR_WITH_ERRNO("Failed to close inode");
442 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
449 wimlib_assert(dentry_is_directory(dentry));
450 ni = ntfs_pathname_to_inode(vol, NULL, "/");
452 ERROR_WITH_ERRNO("Could not find root NTFS inode");
453 return WIMLIB_ERR_NTFS_3G;
455 ret = apply_file_attributes_and_security_data(ni, dentry, w);
456 if (ntfs_inode_close(ni) != 0) {
457 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
459 ret = WIMLIB_ERR_NTFS_3G;
464 /* Applies a WIM dentry to the NTFS volume */
465 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
467 struct ntfs_apply_args *args = arg;
468 ntfs_volume *vol = args->vol;
469 int extract_flags = args->extract_flags;
470 WIMStruct *w = args->w;
475 ntfs_inode *close_after_dir;
476 const char *dir_name;
478 wimlib_assert(dentry->full_path_utf8);
480 DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
482 if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
483 puts(dentry->full_path_utf8);
485 if (dentry_is_root(dentry))
486 return wim_apply_root_dentry_ntfs(dentry, vol, w);
488 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
495 dir_name = dentry->full_path_utf8;
497 dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
500 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
502 return WIMLIB_ERR_NTFS_3G;
504 DEBUG("Found NTFS inode for `%s'", dir_name);
505 return do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
508 static int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
510 struct ntfs_apply_args *args = arg;
511 ntfs_volume *vol = args->vol;
512 int extract_flags = args->extract_flags;
513 WIMStruct *w = args->w;
520 DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
522 ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
524 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
525 dentry->full_path_utf8);
526 return WIMLIB_ERR_NTFS_3G;
530 p = put_u64(p, dentry->creation_time);
531 p = put_u64(p, dentry->last_write_time);
532 p = put_u64(p, dentry->last_access_time);
533 ret = ntfs_inode_set_times(ni, buf, 3 * sizeof(u64), 0);
535 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
536 dentry->full_path_utf8);
537 ret = WIMLIB_ERR_NTFS_3G;
540 if (ntfs_inode_close(ni) != 0) {
542 ret = WIMLIB_ERR_NTFS_3G;
543 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
544 dentry->full_path_utf8);
549 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
554 vol = ntfs_mount(device, 0);
556 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
557 return WIMLIB_ERR_NTFS_3G;
559 struct ntfs_apply_args args = {
561 .extract_flags = extract_flags,
564 ret = for_dentry_in_tree(wim_root_dentry(w), wim_apply_dentry_ntfs,
569 DEBUG("Setting NTFS timestamps");
570 ret = for_dentry_in_tree_depth(wim_root_dentry(w),
571 wim_apply_dentry_timestamps,
574 if (ntfs_umount(vol, FALSE) != 0) {
575 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
577 ret = WIMLIB_ERR_NTFS_3G;
584 * API entry point for applying a WIM image to a NTFS volume.
586 * Please note that this is a NTFS *volume* and not a directory. The intention
587 * is that the volume contain an empty filesystem, and the WIM image contain a
588 * full filesystem to be applied to the volume.
590 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
591 const char *device, int flags)
596 return WIMLIB_ERR_INVALID_PARAM;
597 if (image == WIM_ALL_IMAGES) {
598 ERROR("Can only apply a single image when applying "
599 "directly to a NTFS volume");
600 return WIMLIB_ERR_INVALID_PARAM;
602 if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
603 ERROR("Cannot specify symlink or hardlink flags when applying ");
604 ERROR("directly to a NTFS volume");
605 return WIMLIB_ERR_INVALID_PARAM;
607 ret = wimlib_select_image(w, image);
613 ERROR("We are not root, but NTFS-3g requires root privileges to set arbitrary");
614 ERROR("security data on the NTFS filesystem. Please run this program as root");
615 ERROR("if you want to extract a WIM image while preserving NTFS-specific");
616 ERROR("information.");
618 return WIMLIB_ERR_NOT_ROOT;
621 return do_wim_apply_image_ntfs(w, device, flags);
624 #else /* WITH_NTFS_3G */
625 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
626 const char *device, int flags)
628 ERROR("wimlib was compiled without support for NTFS-3g, so");
629 ERROR("we cannot apply a WIM image directly to a NTFS volume");
630 return WIMLIB_ERR_UNSUPPORTED;
632 #endif /* WITH_NTFS_3G */