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