]> wimlib.net Git - wimlib/blob - src/ntfs-apply.c
0ca1951d153f0493bccae8a7fb2e88a7c04abea1
[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_set_file_security(ntfs_inode *ni, u32 selection,
53                                    const char *attr);
54 extern int _ntfs_set_file_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 file)");
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 /*
145  * Makes a NTFS hard link
146  *
147  * It is named @from_dentry->file_name and is located under the directory
148  * specified by @dir_ni, and it is made to point to the previously extracted
149  * file located at @to_dentry->extracted_file.
150  *
151  * Return 0 on success, nonzero on failure.
152  */
153 static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
154                                    const struct dentry *to_dentry,
155                                    ntfs_inode *dir_ni,
156                                    ntfs_inode **to_ni_ret)
157 {
158         int ret;
159         ntfs_inode *to_ni;
160
161         wimlib_assert(dentry_is_regular_file(from_dentry)
162                         && dentry_is_regular_file(to_dentry));
163
164         DEBUG("Extracting NTFS hard link `%s' => `%s'",
165               from_dentry->full_path_utf8, to_dentry->extracted_file);
166
167         to_ni = ntfs_pathname_to_inode(dir_ni->vol, NULL,
168                                        to_dentry->extracted_file);
169         if (!to_ni) {
170                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
171                                  to_dentry->extracted_file);
172                 return WIMLIB_ERR_NTFS_3G;
173         }
174         ret = ntfs_link(to_ni, dir_ni,
175                         (ntfschar*)from_dentry->file_name,
176                         from_dentry->file_name_len / 2);
177         if (ret != 0) {
178                 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
179                                  from_dentry->full_path_utf8,
180                                  to_dentry->extracted_file);
181                 ret = WIMLIB_ERR_NTFS_3G;
182         }
183         *to_ni_ret = to_ni;
184         return ret;
185 }
186
187 static int
188 apply_file_attributes_and_security_data(ntfs_inode *ni,
189                                         const struct dentry *dentry,
190                                         const WIMStruct *w)
191 {
192         DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
193               dentry->full_path_utf8, dentry->attributes);
194         if (!_ntfs_set_file_attributes(ni, dentry->attributes)) {
195                 ERROR("Failed to set NTFS file attributes on `%s'",
196                        dentry->full_path_utf8);
197                 return WIMLIB_ERR_NTFS_3G;
198         }
199
200         if (dentry->security_id != -1) {
201                 const struct wim_security_data *sd;
202                 
203                 sd = wim_const_security_data(w);
204                 wimlib_assert(dentry->security_id < sd->num_entries);
205                 DEBUG("Applying security descriptor %d to `%s'",
206                       dentry->security_id, dentry->full_path_utf8);
207                 u32 selection = OWNER_SECURITY_INFORMATION |
208                                 GROUP_SECURITY_INFORMATION |
209                                 DACL_SECURITY_INFORMATION  |
210                                 SACL_SECURITY_INFORMATION;
211                                 
212                 if (!_ntfs_set_file_security(ni, selection,
213                                              sd->descriptors[dentry->security_id]))
214                 {
215                         ERROR_WITH_ERRNO("Failed to set security data on `%s'",
216                                         dentry->full_path_utf8);
217                         return WIMLIB_ERR_NTFS_3G;
218                 }
219         }
220         return 0;
221 }
222
223 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
224                               const WIMStruct *w)
225 {
226         struct lookup_table_entry *lte;
227         int ret = 0;
228
229         wimlib_assert(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
230
231         lte = dentry_first_lte(dentry, w->lookup_table);
232
233         DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
234
235         if (!lte) {
236                 ERROR("Could not find reparse data for `%s'",
237                       dentry->full_path_utf8);
238                 return WIMLIB_ERR_INVALID_DENTRY;
239         }
240
241         if (wim_resource_size(lte) >= 0xffff) {
242                 ERROR("Reparse data of `%s' is too long (%lu bytes)",
243                       dentry->full_path_utf8, wim_resource_size(lte));
244                 return WIMLIB_ERR_INVALID_DENTRY;
245         }
246
247         char reparse_data_buf[8 + wim_resource_size(lte)];
248         char *p = reparse_data_buf;
249         p = put_u32(p, dentry->reparse_tag); /* ReparseTag */
250         p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
251         p = put_u16(p, 0); /* Reserved */
252
253         ret = read_full_wim_resource(lte, p);
254         if (ret != 0)
255                 return ret;
256
257         ret = ntfs_set_ntfs_reparse_data(ni, reparse_data_buf,
258                                          wim_resource_size(lte) + 8, 0);
259         if (ret != 0) {
260                 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
261                                  dentry->full_path_utf8);
262                 return WIMLIB_ERR_NTFS_3G;
263         }
264         return 0;
265 }
266
267 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
268                                     WIMStruct *w);
269
270 /* 
271  * If @dentry is part of a hard link group, search for hard-linked dentries in
272  * the same directory that have a nonempty DOS (short) filename.  There should
273  * be exactly 0 or 1 such dentries.  If there is 1, extract that dentry first,
274  * so that the DOS name is correctly associated with the corresponding long name
275  * in the Win32 namespace, and not any of the additional names in the POSIX
276  * namespace created from hard links.
277  */
278 static int preapply_dentry_with_dos_name(struct dentry *dentry,
279                                          ntfs_inode **dir_ni_p,
280                                          WIMStruct *w)
281 {
282         int ret;
283         struct dentry *other;
284         struct dentry *dentry_with_dos_name;
285
286         if (dentry->link_group_list.next == &dentry->link_group_list)
287                 return 0;
288
289         dentry_with_dos_name = NULL;
290         list_for_each_entry(other, &dentry->link_group_list,
291                             link_group_list)
292         {
293                 if (dentry->parent == other->parent && other->short_name_len) {
294                         if (dentry_with_dos_name) {
295                                 ERROR("Found multiple DOS names for file `%s' "
296                                       "in the same directory",
297                                       dentry_with_dos_name->full_path_utf8);
298                                 return WIMLIB_ERR_INVALID_DENTRY;
299                         }
300                         dentry_with_dos_name = other;
301                 }
302         }
303         /* If there's a dentry with a DOS name, extract it first */
304         if (dentry_with_dos_name && !dentry_with_dos_name->extracted_file) {
305                 char *p;
306                 const char *dir_name;
307                 char orig;
308                 ntfs_volume *vol = (*dir_ni_p)->vol;
309
310                 DEBUG("pre-applying DOS name `%s'",
311                       dentry_with_dos_name->full_path_utf8);
312                 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
313                                                *dir_ni_p, w);
314                 if (ret != 0)
315                         return ret;
316                 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
317                 do {
318                         p--;
319                 } while (*p != '/');
320
321                 orig = *p;
322                 *p = '\0';
323                 dir_name = dentry->full_path_utf8;
324
325                 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
326                 *p = orig;
327                 if (!*dir_ni_p) {
328                         ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
329                                          dir_name);
330                         return WIMLIB_ERR_NTFS_3G;
331                 }
332         }
333         return 0;
334 }
335
336 /* 
337  * Applies a WIM dentry to a NTFS filesystem.
338  *
339  * @dentry:  The WIM dentry to apply
340  * @dir_ni:  The NTFS inode for the parent directory
341  * @w:       The WIMStruct for the WIM containing the image we are applying.
342  *
343  * @return:  0 on success; nonzero on failure.
344  */
345 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
346                                     WIMStruct *w)
347 {
348         int ret = 0;
349         mode_t type;
350         ntfs_inode *ni = NULL;
351         bool is_hardlink = false;
352         ntfs_volume *vol = dir_ni->vol;
353
354         if (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY) {
355                 type = S_IFDIR;
356         } else {
357                 struct dentry *other;
358
359                 ret = preapply_dentry_with_dos_name(dentry, &dir_ni, w);
360                 if (ret != 0)
361                         return ret;
362
363                 type = S_IFREG;
364                 /* See if we can make a hard link */
365                 list_for_each_entry(other, &dentry->link_group_list,
366                                     link_group_list) {
367                         if (other->extracted_file) {
368                                 ret = wim_apply_hardlink_ntfs(dentry, other,
369                                                               dir_ni, &ni);
370                                 is_hardlink = true;
371                                 if (ret != 0)
372                                         goto out_close_dir_ni;
373                                 else
374                                         goto out_set_dos_name;
375                         }
376                 }
377                 /* Can't make a hard link */
378                 FREE(dentry->extracted_file);
379                 dentry->extracted_file = STRDUP(dentry->full_path_utf8);
380                 if (!dentry->extracted_file) {
381                         ERROR("Failed to allocate memory for filename");
382                         return WIMLIB_ERR_NOMEM;
383                 }
384         }
385
386         /* 
387          * Create a directory or file.
388          *
389          * Note: For symbolic links that are not directory junctions, pass
390          * S_IFREG here, since we manually set the reparse data later.
391          */
392         ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
393                          dentry->file_name_len / 2, type);
394
395         if (!ni) {
396                 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
397                                  dentry->full_path_utf8);
398                 ret = WIMLIB_ERR_NTFS_3G;
399                 goto out_close_dir_ni;
400         }
401
402         /* Write the data streams, unless this is a directory or reparse point
403          * */
404         if (!dentry_is_directory(dentry) &&
405              !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
406                 ret = write_ntfs_data_streams(ni, dentry, w);
407                 if (ret != 0)
408                         goto out_close_dir_ni;
409         }
410
411
412         ret = apply_file_attributes_and_security_data(ni, dentry, w);
413         if (ret != 0)
414                 goto out_close_dir_ni;
415
416         if (dentry->attributes & FILE_ATTR_REPARSE_POINT) {
417                 ret = apply_reparse_data(ni, dentry, w);
418                 if (ret != 0)
419                         goto out_close_dir_ni;
420         }
421
422 out_set_dos_name:
423         /* Set DOS (short) name if given */
424         if (dentry->short_name_len != 0) {
425
426                 char *short_name_utf8;
427                 size_t short_name_utf8_len;
428                 short_name_utf8 = utf16_to_utf8(dentry->short_name,
429                                                 dentry->short_name_len,
430                                                 &short_name_utf8_len);
431                 if (!short_name_utf8) {
432                         ERROR("Out of memory");
433                         ret = WIMLIB_ERR_NOMEM;
434                         goto out_close_dir_ni;
435                 }
436
437                 if (is_hardlink) {
438                         char *p;
439                         char orig;
440                         const char *dir_name;
441
442                         /* ntfs_set_ntfs_dos_name() closes the inodes in the
443                          * wrong order if we have applied a hard link.   Close
444                          * them ourselves, then re-open then. */
445                         if (ntfs_inode_close(dir_ni) != 0) {
446                                 if (ret == 0)
447                                         ret = WIMLIB_ERR_NTFS_3G;
448                                 ERROR_WITH_ERRNO("Failed to close directory inode");
449                         }
450                         if (ntfs_inode_close(ni) != 0) {
451                                 if (ret == 0)
452                                         ret = WIMLIB_ERR_NTFS_3G;
453                                 ERROR_WITH_ERRNO("Failed to close hard link target inode");
454                         }
455                         p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
456                         do {
457                                 p--;
458                         } while (*p != '/');
459
460                         orig = *p;
461                         *p = '\0';
462                         dir_name = dentry->full_path_utf8;
463
464                         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
465                         *p = orig;
466                         if (!dir_ni) {
467                                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
468                                                  dir_name);
469                                 return WIMLIB_ERR_NTFS_3G;
470                         }
471                         ni = ntfs_pathname_to_inode(vol, dir_ni,
472                                                     dentry->file_name_utf8);
473                         if (!ni) {
474                                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
475                                                  dir_name);
476                                 return WIMLIB_ERR_NTFS_3G;
477                         }
478                 }
479
480                 DEBUG("Setting short (DOS) name of `%s' to %s",
481                       dentry->full_path_utf8, short_name_utf8);
482
483                 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
484                                              short_name_utf8_len, 0);
485                 FREE(short_name_utf8);
486                 if (ret != 0) {
487                         ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
488                                          dentry->full_path_utf8);
489                         ret = WIMLIB_ERR_NTFS_3G;
490                 }
491                 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
492                 return ret;
493         }
494
495 out_close_dir_ni:
496         if (ntfs_inode_close(dir_ni) != 0) {
497                 if (ret == 0)
498                         ret = WIMLIB_ERR_NTFS_3G;
499                 ERROR_WITH_ERRNO("Failed to close directory inode");
500         }
501 out_close_ni:
502         if (ni && ntfs_inode_close(ni) != 0) {
503                 if (ret == 0)
504                         ret = WIMLIB_ERR_NTFS_3G;
505                 ERROR_WITH_ERRNO("Failed to close inode");
506         }
507         return ret;
508 }
509
510 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
511                                       ntfs_volume *vol,
512                                       const WIMStruct *w)
513 {
514         ntfs_inode *ni;
515         int ret = 0;
516
517         wimlib_assert(dentry_is_directory(dentry));
518         ni = ntfs_pathname_to_inode(vol, NULL, "/");
519         if (!ni) {
520                 ERROR_WITH_ERRNO("Could not find root NTFS inode");
521                 return WIMLIB_ERR_NTFS_3G;
522         }
523         ret = apply_file_attributes_and_security_data(ni, dentry, w);
524         if (ntfs_inode_close(ni) != 0) {
525                 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
526                                  "directory");
527                 ret = WIMLIB_ERR_NTFS_3G;
528         }
529         return ret;
530 }
531
532 /* Applies a WIM dentry to the NTFS volume */
533 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
534 {
535         struct ntfs_apply_args *args = arg;
536         ntfs_volume *vol             = args->vol;
537         int extract_flags            = args->extract_flags;
538         WIMStruct *w                 = args->w;
539         ntfs_inode *dir_ni;
540         int ret;
541         char *p;
542         char orig;
543         ntfs_inode *close_after_dir;
544         const char *dir_name;
545
546         if (dentry->extracted_file)
547                 return 0;
548
549         wimlib_assert(dentry->full_path_utf8);
550
551         DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
552
553         if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
554                 puts(dentry->full_path_utf8);
555
556         if (dentry_is_root(dentry))
557                 return wim_apply_root_dentry_ntfs(dentry, vol, w);
558
559         p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
560         do {
561                 p--;
562         } while (*p != '/');
563
564         orig = *p;
565         *p = '\0';
566         dir_name = dentry->full_path_utf8;
567
568         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
569         if (dir_ni)
570                 DEBUG("Found NTFS inode for `%s'", dir_name);
571         *p = orig;
572         if (!dir_ni) {
573                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
574                                  dir_name);
575                 return WIMLIB_ERR_NTFS_3G;
576         }
577         return do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
578 }
579
580 static int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
581 {
582         struct ntfs_apply_args *args = arg;
583         ntfs_volume *vol             = args->vol;
584         int extract_flags            = args->extract_flags;
585         WIMStruct *w                 = args->w;
586         char *p;
587         char buf[24];
588         ntfs_inode *ni;
589         int ret = 0;
590
591
592         DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
593
594         ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
595         if (!ni) {
596                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
597                                  dentry->full_path_utf8);
598                 return WIMLIB_ERR_NTFS_3G;
599         }
600
601         p = buf;
602         p = put_u64(p, dentry->creation_time);
603         p = put_u64(p, dentry->last_write_time);
604         p = put_u64(p, dentry->last_access_time);
605         ret = ntfs_inode_set_times(ni, buf, 3 * sizeof(u64), 0);
606         if (ret != 0) {
607                 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
608                                  dentry->full_path_utf8);
609                 ret = WIMLIB_ERR_NTFS_3G;
610         }
611
612         if (ntfs_inode_close(ni) != 0) {
613                 if (ret == 0)
614                         ret = WIMLIB_ERR_NTFS_3G;
615                 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
616                                  dentry->full_path_utf8);
617         }
618         return ret;
619 }
620
621 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
622 {
623         ntfs_volume *vol;
624         int ret;
625         
626         DEBUG("Mounting NTFS volume `%s'", device);
627         vol = ntfs_mount(device, 0);
628         if (!vol) {
629                 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
630                 return WIMLIB_ERR_NTFS_3G;
631         }
632         struct ntfs_apply_args args = {
633                 .vol           = vol,
634                 .extract_flags = extract_flags,
635                 .w             = w,
636         };
637         ret = for_dentry_in_tree(wim_root_dentry(w), wim_apply_dentry_ntfs,
638                                  &args);
639
640         if (ret != 0)
641                 goto out;
642         DEBUG("Setting NTFS timestamps");
643         ret = for_dentry_in_tree_depth(wim_root_dentry(w),
644                                        wim_apply_dentry_timestamps,
645                                        &args);
646 out:
647         DEBUG("Unmounting NTFS volume `%s'", device);
648         if (ntfs_umount(vol, FALSE) != 0) {
649                 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
650                 if (ret == 0)
651                         ret = WIMLIB_ERR_NTFS_3G;
652         }
653         return ret;
654 }
655
656
657 /* 
658  * API entry point for applying a WIM image to a NTFS volume.
659  *
660  * Please note that this is a NTFS *volume* and not a directory.  The intention
661  * is that the volume contain an empty filesystem, and the WIM image contain a
662  * full filesystem to be applied to the volume.
663  */
664 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
665                                                 const char *device, int flags)
666 {
667         int ret;
668
669         if (!device)
670                 return WIMLIB_ERR_INVALID_PARAM;
671         if (image == WIM_ALL_IMAGES) {
672                 ERROR("Can only apply a single image when applying "
673                       "directly to a NTFS volume");
674                 return WIMLIB_ERR_INVALID_PARAM;
675         }
676         if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
677                 ERROR("Cannot specify symlink or hardlink flags when applying ");
678                 ERROR("directly to a NTFS volume");
679                 return WIMLIB_ERR_INVALID_PARAM;
680         }
681         ret = wimlib_select_image(w, image);
682         if (ret != 0)
683                 return ret;
684
685 #if 0
686         if (getuid() != 0) {
687                 ERROR("We are not root, but NTFS-3g requires root privileges to set arbitrary");
688                 ERROR("security data on the NTFS filesystem.  Please run this program as root");
689                 ERROR("if you want to extract a WIM image while preserving NTFS-specific");
690                 ERROR("information.");
691
692                 return WIMLIB_ERR_NOT_ROOT;
693         }
694 #endif
695         return do_wim_apply_image_ntfs(w, device, flags);
696 }
697
698 #else /* WITH_NTFS_3G */
699 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
700                                                 const char *device, int flags)
701 {
702         ERROR("wimlib was compiled without support for NTFS-3g, so");
703         ERROR("we cannot apply a WIM image directly to a NTFS volume");
704         return WIMLIB_ERR_UNSUPPORTED;
705 }
706 #endif /* WITH_NTFS_3G */