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