cfeee3f6dee4901c987cb39457bb4a37aa97adcb
[wimlib] / src / ntfs-apply.c
1 /*
2  * ntfs-apply.c
3  *
4  * Apply a WIM image to a NTFS volume.  We restore everything we can, including
5  * security data and alternate data streams.
6  */
7
8 /*
9  * Copyright (C) 2012 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib 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
20  * A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #include "config.h"
28 #include "wimlib_internal.h"
29
30
31 #ifdef WITH_NTFS_3G
32 #include "dentry.h"
33 #include "lookup_table.h"
34 #include "io.h"
35 #include <ntfs-3g/layout.h>
36 #include <ntfs-3g/acls.h>
37 #include <ntfs-3g/attrib.h>
38 #include <ntfs-3g/security.h> /* security.h before xattrs.h */
39 #include <ntfs-3g/xattrs.h>
40 #include <ntfs-3g/reparse.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43
44 struct ntfs_apply_args {
45         ntfs_volume *vol;
46         int extract_flags;
47         WIMStruct *w;
48 };
49
50
51 #ifndef WITH_NEW_NTFS_3G
52 extern int ntfs_set_inode_security(ntfs_inode *ni, u32 selection,
53                                    const char *attr);
54 extern int ntfs_set_inode_attributes(ntfs_inode *ni, u32 attrib);
55 #endif
56
57 /* 
58  * Extracts a WIM resource to a NTFS attribute.
59  */
60 static int
61 extract_wim_resource_to_ntfs_attr(const struct lookup_table_entry *lte,
62                                   ntfs_attr *na)
63 {
64         u64 bytes_remaining = wim_resource_size(lte);
65         u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
66         u64 offset = 0;
67         int ret = 0;
68         u8 hash[SHA1_HASH_SIZE];
69
70         SHA_CTX ctx;
71         sha1_init(&ctx);
72
73         while (bytes_remaining) {
74                 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
75                 ret = read_wim_resource(lte, buf, to_read, offset, false);
76                 if (ret != 0)
77                         break;
78                 sha1_update(&ctx, buf, to_read);
79                 if (ntfs_attr_pwrite(na, offset, to_read, buf) != to_read) {
80                         ERROR_WITH_ERRNO("Error extracting WIM resource");
81                         return WIMLIB_ERR_WRITE;
82                 }
83                 bytes_remaining -= to_read;
84                 offset += to_read;
85         }
86         sha1_final(hash, &ctx);
87         if (!hashes_equal(hash, lte->hash)) {
88                 ERROR("Invalid checksum on a WIM resource "
89                       "(detected when extracting to NTFS stream)");
90                 ERROR("The following WIM resource is invalid:");
91                 print_lookup_table_entry(lte);
92                 return WIMLIB_ERR_INVALID_RESOURCE_HASH;
93         }
94         return 0;
95 }
96
97 /* Writes the data streams to a NTFS file
98  *
99  * @ni:      The NTFS inode for the file.
100  * @dentry:  The directory entry in the WIM file.
101  * @w:       The WIMStruct for the WIM containing the image we are applying.
102  *
103  * Returns 0 on success, nonzero on failure.
104  */
105 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
106                                    WIMStruct *w)
107 {
108         int ret = 0;
109         unsigned stream_idx = 0;
110         ntfschar *stream_name = AT_UNNAMED;
111         u32 stream_name_len = 0;
112
113         DEBUG("Writing %u NTFS data stream%s for `%s'",
114               dentry->num_ads + 1,
115               (dentry->num_ads == 0 ? "" : "s"),
116               dentry->full_path_utf8);
117
118         while (1) {
119                 struct lookup_table_entry *lte;
120                 ntfs_attr *na;
121
122                 lte = dentry_stream_lte(dentry, stream_idx, w->lookup_table);
123
124                 if (stream_name_len) {
125                         /* Create an empty named stream. */
126                         ret = ntfs_attr_add(ni, AT_DATA, stream_name,
127                                             stream_name_len, NULL, 0);
128                         if (ret != 0) {
129                                 ERROR_WITH_ERRNO("Failed to create name data "
130                                                  "stream for extracted file "
131                                                  "`%s'",
132                                                  dentry->full_path_utf8);
133                                 ret = WIMLIB_ERR_NTFS_3G;
134                                 break;
135
136                         }
137                 }
138                 /* If there's no lookup table entry, it's an empty stream.
139                  * Otherwise, we must open the attribute and extract the data.
140                  * */
141                 if (lte) {
142                         na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
143                         if (!na) {
144                                 ERROR_WITH_ERRNO("Failed to open a data stream of "
145                                                  "extracted file `%s'",
146                                                  dentry->full_path_utf8);
147                                 ret = WIMLIB_ERR_NTFS_3G;
148                                 break;
149                         }
150                         ret = extract_wim_resource_to_ntfs_attr(lte, na);
151                         if (ret != 0)
152                                 break;
153                         ntfs_attr_close(na);
154                 }
155                 if (stream_idx == dentry->num_ads)
156                         break;
157                 stream_name = (ntfschar*)dentry->ads_entries[stream_idx].stream_name;
158                 stream_name_len = dentry->ads_entries[stream_idx].stream_name_len / 2;
159                 stream_idx++;
160         }
161         return ret;
162 }
163
164 /*
165  * Makes a NTFS hard link
166  *
167  * It is named @from_dentry->file_name and is located under the directory
168  * specified by @dir_ni, and it is made to point to the previously extracted
169  * file located at @to_dentry->extracted_file.
170  *
171  * Return 0 on success, nonzero on failure.
172  */
173 static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
174                                    const struct dentry *to_dentry,
175                                    ntfs_inode *dir_ni,
176                                    ntfs_inode **to_ni_ret)
177 {
178         int ret;
179         char *p;
180         char orig;
181         const char *dir_name;
182
183         ntfs_inode *to_ni;
184         ntfs_volume *vol;
185
186         wimlib_assert(dentry_is_regular_file(from_dentry)
187                         && dentry_is_regular_file(to_dentry));
188
189         if (ntfs_inode_close(dir_ni) != 0) {
190                 ERROR_WITH_ERRNO("Error closing directory");
191                 return WIMLIB_ERR_NTFS_3G;
192         }
193
194         vol = dir_ni->vol;
195
196         DEBUG("Extracting NTFS hard link `%s' => `%s'",
197               from_dentry->full_path_utf8, to_dentry->extracted_file);
198
199         to_ni = ntfs_pathname_to_inode(vol, NULL,
200                                        to_dentry->extracted_file);
201         if (!to_ni) {
202                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
203                                  to_dentry->extracted_file);
204                 return WIMLIB_ERR_NTFS_3G;
205         }
206         p = from_dentry->full_path_utf8 + from_dentry->full_path_utf8_len;
207         do {
208                 p--;
209         } while (*p != '/');
210
211         orig = *p;
212         *p = '\0';
213         dir_name = from_dentry->full_path_utf8;
214
215         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
216         if (!dir_ni) {
217                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
218                                  from_dentry->full_path_utf8);
219                 *p = orig;
220                 return WIMLIB_ERR_NTFS_3G;
221         }
222         *p = orig;
223
224         ret = ntfs_link(to_ni, dir_ni,
225                         (ntfschar*)from_dentry->file_name,
226                         from_dentry->file_name_len / 2);
227         if (ret != 0) {
228                 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
229                                  from_dentry->full_path_utf8,
230                                  to_dentry->extracted_file);
231                 ret = WIMLIB_ERR_NTFS_3G;
232         }
233         *to_ni_ret = to_ni;
234         return ret;
235 }
236
237 /*#define HAVE_NTFS_INODE_FUNCTIONS*/
238
239 static int
240 apply_file_attributes_and_security_data(ntfs_inode *ni,
241                                         ntfs_inode *dir_ni,
242                                         const struct dentry *dentry,
243                                         const WIMStruct *w)
244 {
245         DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
246               dentry->full_path_utf8, dentry->attributes);
247         int ret;
248 #ifdef HAVE_NTFS_INODE_FUNCTIONS
249         ret = ntfs_set_inode_attributes(ni, dentry->attributes);
250 #else
251         struct SECURITY_CONTEXT ctx;
252         u32 attributes_le32;
253         attributes_le32 = cpu_to_le32(dentry->attributes);
254         memset(&ctx, 0, sizeof(ctx));
255         ctx.vol = ni->vol;
256         ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
257                                          ni, dir_ni,
258                                          (const char*)&attributes_le32,
259                                          sizeof(u32), 0);
260 #endif
261         if (ret != 0) {
262                 ERROR("Failed to set NTFS file attributes on `%s'",
263                        dentry->full_path_utf8);
264                 return WIMLIB_ERR_NTFS_3G;
265         }
266         if (dentry->security_id != -1) {
267                 const struct wim_security_data *sd;
268                 const char *descriptor;
269                 
270                 sd = wim_const_security_data(w);
271                 wimlib_assert(dentry->security_id < sd->num_entries);
272                 descriptor = sd->descriptors[dentry->security_id];
273                 DEBUG("Applying security descriptor %d to `%s'",
274                       dentry->security_id, dentry->full_path_utf8);
275
276         #ifdef HAVE_NTFS_INODE_FUNCTIONS
277                 u32 selection = OWNER_SECURITY_INFORMATION |
278                                 GROUP_SECURITY_INFORMATION |
279                                 DACL_SECURITY_INFORMATION  |
280                                 SACL_SECURITY_INFORMATION;
281                 ret = ntfs_set_inode_security(ni, selection, descriptor);
282         #else
283                 ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
284                                            ni, dir_ni, descriptor,
285                                            sd->sizes[dentry->security_id], 0);
286         #endif
287                                 
288                 if (ret != 0) {
289                         ERROR_WITH_ERRNO("Failed to set security data on `%s'",
290                                         dentry->full_path_utf8);
291                         return WIMLIB_ERR_NTFS_3G;
292                 }
293         }
294         return 0;
295 }
296
297 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
298                               const WIMStruct *w)
299 {
300         struct lookup_table_entry *lte;
301         int ret = 0;
302
303         wimlib_assert(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
304
305         lte = dentry_unnamed_lte(dentry, w->lookup_table);
306
307         DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
308
309         if (!lte) {
310                 ERROR("Could not find reparse data for `%s'",
311                       dentry->full_path_utf8);
312                 return WIMLIB_ERR_INVALID_DENTRY;
313         }
314
315         if (wim_resource_size(lte) >= 0xffff) {
316                 ERROR("Reparse data of `%s' is too long (%lu bytes)",
317                       dentry->full_path_utf8, wim_resource_size(lte));
318                 return WIMLIB_ERR_INVALID_DENTRY;
319         }
320
321         u8 reparse_data_buf[8 + wim_resource_size(lte)];
322         u8 *p = reparse_data_buf;
323         p = put_u32(p, dentry->reparse_tag); /* ReparseTag */
324         p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
325         p = put_u16(p, 0); /* Reserved */
326
327         ret = read_full_wim_resource(lte, p);
328         if (ret != 0)
329                 return ret;
330
331         ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
332                                          wim_resource_size(lte) + 8, 0);
333         if (ret != 0) {
334                 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
335                                  dentry->full_path_utf8);
336                 return WIMLIB_ERR_NTFS_3G;
337         }
338         return 0;
339 }
340
341 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
342                                     WIMStruct *w);
343
344 /* 
345  * If @dentry is part of a hard link group, search for hard-linked dentries in
346  * the same directory that have a nonempty DOS (short) filename.  There should
347  * be exactly 0 or 1 such dentries.  If there is 1, extract that dentry first,
348  * so that the DOS name is correctly associated with the corresponding long name
349  * in the Win32 namespace, and not any of the additional names in the POSIX
350  * namespace created from hard links.
351  */
352 static int preapply_dentry_with_dos_name(struct dentry *dentry,
353                                          ntfs_inode **dir_ni_p,
354                                          WIMStruct *w)
355 {
356         struct dentry *other;
357         struct dentry *dentry_with_dos_name;
358
359         dentry_with_dos_name = NULL;
360         list_for_each_entry(other, &dentry->link_group_list,
361                             link_group_list)
362         {
363                 if (dentry->parent == other->parent && other->short_name_len) {
364                         if (dentry_with_dos_name) {
365                                 ERROR("Found multiple DOS names for file `%s' "
366                                       "in the same directory",
367                                       dentry_with_dos_name->full_path_utf8);
368                                 return WIMLIB_ERR_INVALID_DENTRY;
369                         }
370                         dentry_with_dos_name = other;
371                 }
372         }
373         /* If there's a dentry with a DOS name, extract it first */
374         if (dentry_with_dos_name
375             && !dentry_with_dos_name->extracted_file)
376         {
377                 char *p;
378                 const char *dir_name;
379                 char orig;
380                 int ret;
381                 ntfs_volume *vol = (*dir_ni_p)->vol;
382
383                 DEBUG("pre-applying DOS name `%s'",
384                       dentry_with_dos_name->full_path_utf8);
385                 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
386                                                *dir_ni_p, w);
387                 if (ret != 0)
388                         return ret;
389                 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
390                 do {
391                         p--;
392                 } while (*p != '/');
393
394                 orig = *p;
395                 *p = '\0';
396                 dir_name = dentry->full_path_utf8;
397
398                 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
399                 *p = orig;
400                 if (!*dir_ni_p) {
401                         ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
402                                          dir_name);
403                         return WIMLIB_ERR_NTFS_3G;
404                 }
405         }
406         return 0;
407 }
408
409 /* 
410  * Applies a WIM dentry to a NTFS filesystem.
411  *
412  * @dentry:  The WIM dentry to apply
413  * @dir_ni:  The NTFS inode for the parent directory
414  * @w:       The WIMStruct for the WIM containing the image we are applying.
415  *
416  * @return:  0 on success; nonzero on failure.
417  */
418 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
419                                     WIMStruct *w)
420 {
421         int ret = 0;
422         mode_t type;
423         ntfs_inode *ni = NULL;
424         bool is_hardlink = false;
425         ntfs_volume *vol = dir_ni->vol;
426
427         if (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY) {
428                 type = S_IFDIR;
429         } else {
430                 struct dentry *other;
431
432                 /* Apply hard-linked directory in same directory with DOS name
433                  * (if there is one) before this dentry */
434                 if (dentry->short_name_len == 0) {
435                         ret = preapply_dentry_with_dos_name(dentry,
436                                                             &dir_ni, w);
437                         if (ret != 0)
438                                 return ret;
439                 }
440
441                 type = S_IFREG;
442                 /* See if we can make a hard link */
443                 list_for_each_entry(other, &dentry->link_group_list,
444                                     link_group_list) {
445                         if (other->extracted_file) {
446                                 /* Already extracted another dentry in the hard
447                                  * link group.  We can make a hard link instead
448                                  * of extracting the file data. */
449                                 ret = wim_apply_hardlink_ntfs(dentry, other,
450                                                               dir_ni, &ni);
451                                 is_hardlink = true;
452                                 if (ret) {
453                                         goto out_close_dir_ni;
454                                 } else {
455                                         dentry->extracted_file = dentry->full_path_utf8;
456                                         goto out_set_dos_name;
457                                 }
458                         }
459                 }
460                 /* Can't make a hard link; extract the file itself */
461                 dentry->extracted_file = dentry->full_path_utf8;
462         }
463
464         /* 
465          * Create a directory or file.
466          *
467          * Note: For symbolic links that are not directory junctions, pass
468          * S_IFREG here, since we manually set the reparse data later.
469          */
470         ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
471                          dentry->file_name_len / 2, type);
472
473         if (!ni) {
474                 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
475                                  dentry->full_path_utf8);
476                 ret = WIMLIB_ERR_NTFS_3G;
477                 goto out_close_dir_ni;
478         }
479
480         /* Write the data streams, unless this is a directory or reparse point
481          * */
482         if (!dentry_is_directory(dentry) &&
483              !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
484                 ret = write_ntfs_data_streams(ni, dentry, w);
485                 if (ret != 0)
486                         goto out_close_dir_ni;
487         }
488
489
490         ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry, w);
491         if (ret != 0)
492                 goto out_close_dir_ni;
493
494         if (dentry->attributes & FILE_ATTR_REPARSE_POINT) {
495                 ret = apply_reparse_data(ni, dentry, w);
496                 if (ret != 0)
497                         goto out_close_dir_ni;
498         }
499
500 out_set_dos_name:
501         /* Set DOS (short) name if given */
502         if (dentry->short_name_len != 0) {
503
504                 char *short_name_utf8;
505                 size_t short_name_utf8_len;
506                 short_name_utf8 = utf16_to_utf8(dentry->short_name,
507                                                 dentry->short_name_len,
508                                                 &short_name_utf8_len);
509                 if (!short_name_utf8) {
510                         ERROR("Out of memory");
511                         ret = WIMLIB_ERR_NOMEM;
512                         goto out_close_dir_ni;
513                 }
514
515                 if (is_hardlink) {
516                         char *p;
517                         char orig;
518                         const char *dir_name;
519
520                         /* ntfs_set_ntfs_dos_name() closes the inodes in the
521                          * wrong order if we have applied a hard link.   Close
522                          * them ourselves, then re-open then. */
523                         if (ntfs_inode_close(dir_ni) != 0) {
524                                 if (ret == 0)
525                                         ret = WIMLIB_ERR_NTFS_3G;
526                                 ERROR_WITH_ERRNO("Failed to close directory inode");
527                         }
528                         if (ntfs_inode_close(ni) != 0) {
529                                 if (ret == 0)
530                                         ret = WIMLIB_ERR_NTFS_3G;
531                                 ERROR_WITH_ERRNO("Failed to close hard link target inode");
532                         }
533                         p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
534                         do {
535                                 p--;
536                         } while (*p != '/');
537
538                         orig = *p;
539                         *p = '\0';
540                         dir_name = dentry->full_path_utf8;
541
542                         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
543                         *p = orig;
544                         if (!dir_ni) {
545                                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
546                                                  dir_name);
547                                 return WIMLIB_ERR_NTFS_3G;
548                         }
549                         ni = ntfs_pathname_to_inode(vol, dir_ni,
550                                                     dentry->file_name_utf8);
551                         if (!ni) {
552                                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
553                                                  dir_name);
554                                 return WIMLIB_ERR_NTFS_3G;
555                         }
556                 }
557
558                 DEBUG("Setting short (DOS) name of `%s' to %s",
559                       dentry->full_path_utf8, short_name_utf8);
560
561                 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
562                                              short_name_utf8_len, 0);
563                 FREE(short_name_utf8);
564                 if (ret != 0) {
565                         ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
566                                          dentry->full_path_utf8);
567                         ret = WIMLIB_ERR_NTFS_3G;
568                 }
569                 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
570                 return ret;
571         }
572
573 out_close_dir_ni:
574         if (ntfs_inode_close(dir_ni) != 0) {
575                 if (ret == 0)
576                         ret = WIMLIB_ERR_NTFS_3G;
577                 ERROR_WITH_ERRNO("Failed to close directory inode");
578         }
579         if (ni && ntfs_inode_close(ni) != 0) {
580                 if (ret == 0)
581                         ret = WIMLIB_ERR_NTFS_3G;
582                 ERROR_WITH_ERRNO("Failed to close inode");
583         }
584         return ret;
585 }
586
587 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
588                                       ntfs_volume *vol,
589                                       const WIMStruct *w)
590 {
591         ntfs_inode *ni;
592         int ret = 0;
593
594         wimlib_assert(dentry_is_directory(dentry));
595         ni = ntfs_pathname_to_inode(vol, NULL, "/");
596         if (!ni) {
597                 ERROR_WITH_ERRNO("Could not find root NTFS inode");
598                 return WIMLIB_ERR_NTFS_3G;
599         }
600         ret = apply_file_attributes_and_security_data(ni, ni, dentry, w);
601         if (ntfs_inode_close(ni) != 0) {
602                 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
603                                  "directory");
604                 ret = WIMLIB_ERR_NTFS_3G;
605         }
606         return ret;
607 }
608
609 /* Applies a WIM dentry to the NTFS volume */
610 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
611 {
612         struct ntfs_apply_args *args = arg;
613         ntfs_volume *vol             = args->vol;
614         int extract_flags            = args->extract_flags;
615         WIMStruct *w                 = args->w;
616         ntfs_inode *dir_ni;
617         char *p;
618         char orig;
619         const char *dir_name;
620
621         if (dentry->extracted_file)
622                 return 0;
623
624         wimlib_assert(dentry->full_path_utf8);
625
626         DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
627
628         if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
629                 puts(dentry->full_path_utf8);
630
631         if (dentry_is_root(dentry))
632                 return wim_apply_root_dentry_ntfs(dentry, vol, w);
633
634         p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
635         do {
636                 p--;
637         } while (*p != '/');
638
639         orig = *p;
640         *p = '\0';
641         dir_name = dentry->full_path_utf8;
642
643         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
644         *p = orig;
645         if (!dir_ni) {
646                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
647                                  dir_name);
648                 return WIMLIB_ERR_NTFS_3G;
649         }
650         return do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
651 }
652
653 static int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
654 {
655         struct ntfs_apply_args *args = arg;
656         ntfs_volume *vol             = args->vol;
657         u8 *p;
658         u8 buf[24];
659         ntfs_inode *ni;
660         int ret = 0;
661
662
663         DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
664
665         ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
666         if (!ni) {
667                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
668                                  dentry->full_path_utf8);
669                 return WIMLIB_ERR_NTFS_3G;
670         }
671
672         p = buf;
673         p = put_u64(p, dentry->creation_time);
674         p = put_u64(p, dentry->last_write_time);
675         p = put_u64(p, dentry->last_access_time);
676         ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
677         if (ret != 0) {
678                 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
679                                  dentry->full_path_utf8);
680                 ret = WIMLIB_ERR_NTFS_3G;
681         }
682
683         if (ntfs_inode_close(ni) != 0) {
684                 if (ret == 0)
685                         ret = WIMLIB_ERR_NTFS_3G;
686                 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
687                                  dentry->full_path_utf8);
688         }
689         return ret;
690 }
691
692 static int dentry_clear_extracted_file(struct dentry *dentry, void *ignore)
693 {
694         if (dentry->extracted_file != dentry->full_path_utf8)
695                 FREE(dentry->extracted_file);
696         dentry->extracted_file = NULL;
697         return 0;
698 }
699
700 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
701 {
702         ntfs_volume *vol;
703         int ret;
704         struct dentry *root;
705         struct ntfs_apply_args args;
706         
707         DEBUG("Mounting NTFS volume `%s'", device);
708         vol = ntfs_mount(device, 0);
709         if (!vol) {
710                 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
711                 return WIMLIB_ERR_NTFS_3G;
712         }
713         args.vol = vol;
714         args.extract_flags = extract_flags;
715         args.w = w;
716         root = wim_root_dentry(w);
717
718         for_dentry_in_tree(root, dentry_clear_extracted_file, NULL);
719
720         ret = for_dentry_in_tree(root, wim_apply_dentry_ntfs, &args);
721         if (ret != 0)
722                 goto out;
723
724         if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
725                 printf("Setting timestamps of extracted files on NTFS "
726                        "volume `%s'\n", device);
727         ret = for_dentry_in_tree_depth(root, wim_apply_dentry_timestamps,
728                                        &args);
729
730         if (ret == 0 && (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE))
731                 printf("Finished applying image %d of %s to NTFS "
732                        "volume `%s'\n",
733                        w->current_image,
734                        w->filename ? w->filename : "WIM",
735                        device);
736 out:
737         DEBUG("Unmounting NTFS volume `%s'", device);
738         if (ntfs_umount(vol, FALSE) != 0) {
739                 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
740                 if (ret == 0)
741                         ret = WIMLIB_ERR_NTFS_3G;
742         }
743         return ret;
744 }
745
746
747 /* 
748  * API entry point for applying a WIM image to a NTFS volume.
749  *
750  * Please note that this is a NTFS *volume* and not a directory.  The intention
751  * is that the volume contain an empty filesystem, and the WIM image contain a
752  * full filesystem to be applied to the volume.
753  */
754 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
755                                                 const char *device, int flags,
756                                                 WIMStruct **additional_swms,
757                                                 unsigned num_additional_swms)
758 {
759         struct lookup_table *joined_tab, *w_tab_save;
760         int ret;
761
762         DEBUG("w->filename = %s, image = %d, device = %s, flags = 0x%x, "
763               "num_additional_swms = %u",
764               w->filename, image, device, flags, num_additional_swms);
765
766         if (!w || !device)
767                 return WIMLIB_ERR_INVALID_PARAM;
768         if (image == WIM_ALL_IMAGES) {
769                 ERROR("Can only apply a single image when applying "
770                       "directly to a NTFS volume");
771                 return WIMLIB_ERR_INVALID_PARAM;
772         }
773         if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
774                 ERROR("Cannot specify symlink or hardlink flags when applying ");
775                 ERROR("directly to a NTFS volume");
776                 return WIMLIB_ERR_INVALID_PARAM;
777         }
778
779         ret = verify_swm_set(w, additional_swms, num_additional_swms);
780         if (ret != 0)
781                 return ret;
782
783         if (num_additional_swms) {
784                 ret = new_joined_lookup_table(w, additional_swms,
785                                               num_additional_swms, &joined_tab);
786                 if (ret != 0)
787                         return ret;
788                 w_tab_save = w->lookup_table;
789                 w->lookup_table = joined_tab;
790         }
791
792         ret = wimlib_select_image(w, image);
793         if (ret != 0)
794                 goto out;
795
796         ret = do_wim_apply_image_ntfs(w, device, flags);
797
798 out:
799         if (num_additional_swms) {
800                 free_lookup_table(w->lookup_table);
801                 w->lookup_table = w_tab_save;
802         }
803         return ret;
804 }
805
806 #else /* WITH_NTFS_3G */
807 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
808                                                 const char *device, int flags,
809                                                 WIMStruct **additional_swms,
810                                                 unsigned num_additional_swms)
811 {
812         ERROR("wimlib was compiled without support for NTFS-3g, so");
813         ERROR("we cannot apply a WIM image directly to a NTFS volume");
814         return WIMLIB_ERR_UNSUPPORTED;
815 }
816 #endif /* WITH_NTFS_3G */