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