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