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