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