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