Lots of changes
[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
31 #include <ntfs-3g/endians.h>
32 #include <ntfs-3g/types.h>
33
34 #include "wimlib_internal.h"
35 #include "dentry.h"
36 #include "lookup_table.h"
37 #include "io.h"
38 #include <ntfs-3g/layout.h>
39 #include <ntfs-3g/acls.h>
40 #include <ntfs-3g/attrib.h>
41 #include <ntfs-3g/security.h> /* security.h before xattrs.h */
42 #include <ntfs-3g/xattrs.h>
43 #include <ntfs-3g/reparse.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46
47 /*
48  * Extracts a WIM resource to a NTFS attribute.
49  */
50 static int
51 extract_wim_resource_to_ntfs_attr(const struct lookup_table_entry *lte,
52                                   ntfs_attr *na)
53 {
54         u64 bytes_remaining = wim_resource_size(lte);
55         u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
56         u64 offset = 0;
57         int ret = 0;
58         u8 hash[SHA1_HASH_SIZE];
59
60         SHA_CTX ctx;
61         sha1_init(&ctx);
62
63         while (bytes_remaining) {
64                 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
65                 ret = read_wim_resource(lte, buf, to_read, offset, 0);
66                 if (ret != 0)
67                         break;
68                 sha1_update(&ctx, buf, to_read);
69                 if (ntfs_attr_pwrite(na, offset, to_read, buf) != to_read) {
70                         ERROR_WITH_ERRNO("Error extracting WIM resource");
71                         return WIMLIB_ERR_WRITE;
72                 }
73                 bytes_remaining -= to_read;
74                 offset += to_read;
75         }
76         sha1_final(hash, &ctx);
77         if (!hashes_equal(hash, lte->hash)) {
78                 ERROR("Invalid checksum on a WIM resource "
79                       "(detected when extracting to NTFS stream)");
80                 ERROR("The following WIM resource is invalid:");
81                 print_lookup_table_entry(lte);
82                 return WIMLIB_ERR_INVALID_RESOURCE_HASH;
83         }
84         return 0;
85 }
86
87 /* Writes the data streams to a NTFS file
88  *
89  * @ni:      The NTFS inode for the file.
90  * @inode:   The WIM dentry that has an inode containing the streams.
91  *
92  * Returns 0 on success, nonzero on failure.
93  */
94 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
95                                    struct apply_args *args)
96 {
97         int ret = 0;
98         unsigned stream_idx = 0;
99         ntfschar *stream_name = AT_UNNAMED;
100         u32 stream_name_len = 0;
101         const struct inode *inode = dentry->d_inode;
102
103         DEBUG("Writing %u NTFS data stream%s for `%s'",
104               inode->num_ads + 1,
105               (inode->num_ads == 0 ? "" : "s"),
106               dentry->full_path_utf8);
107
108         while (1) {
109                 struct lookup_table_entry *lte;
110                 ntfs_attr *na;
111
112                 lte = inode_stream_lte_resolved(inode, stream_idx);
113
114                 if (stream_name_len) {
115                         /* Create an empty named stream. */
116                         ret = ntfs_attr_add(ni, AT_DATA, stream_name,
117                                             stream_name_len, NULL, 0);
118                         if (ret != 0) {
119                                 ERROR_WITH_ERRNO("Failed to create name data "
120                                                  "stream for extracted file "
121                                                  "`%s'",
122                                                  dentry->full_path_utf8);
123                                 ret = WIMLIB_ERR_NTFS_3G;
124                                 break;
125
126                         }
127                 }
128                 /* If there's no lookup table entry, it's an empty stream.
129                  * Otherwise, we must open the attribute and extract the data.
130                  * */
131                 if (lte) {
132                         na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
133                         if (!na) {
134                                 ERROR_WITH_ERRNO("Failed to open a data stream of "
135                                                  "extracted file `%s'",
136                                                  dentry->full_path_utf8);
137                                 ret = WIMLIB_ERR_NTFS_3G;
138                                 break;
139                         }
140                         ret = extract_wim_resource_to_ntfs_attr(lte, na);
141                         if (ret != 0)
142                                 break;
143                         args->progress.extract.completed_bytes += wim_resource_size(lte);
144                         ntfs_attr_close(na);
145                 }
146                 if (stream_idx == inode->num_ads)
147                         break;
148                 stream_name = (ntfschar*)inode->ads_entries[stream_idx].stream_name;
149                 stream_name_len = inode->ads_entries[stream_idx].stream_name_len / 2;
150                 stream_idx++;
151         }
152         return ret;
153 }
154
155 /*
156  * Makes a NTFS hard link
157  *
158  * It is named @from_dentry->file_name and is located under the directory
159  * specified by @dir_ni, and it is made to point to the previously extracted
160  * file located at @inode->extracted_file.
161  *
162  * Return 0 on success, nonzero on failure.
163  */
164 static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
165                                    const struct inode *inode,
166                                    ntfs_inode *dir_ni,
167                                    ntfs_inode **to_ni_ret)
168 {
169         int ret;
170         char *p;
171         char orig;
172         const char *dir_name;
173
174         ntfs_inode *to_ni;
175         ntfs_volume *vol;
176
177         wimlib_assert(dentry_is_regular_file(from_dentry)
178                         && inode_is_regular_file(inode));
179
180         if (ntfs_inode_close(dir_ni) != 0) {
181                 ERROR_WITH_ERRNO("Error closing directory");
182                 return WIMLIB_ERR_NTFS_3G;
183         }
184
185         vol = dir_ni->vol;
186
187         DEBUG("Extracting NTFS hard link `%s' => `%s'",
188               from_dentry->full_path_utf8, inode->extracted_file);
189
190         to_ni = ntfs_pathname_to_inode(vol, NULL, inode->extracted_file);
191         if (!to_ni) {
192                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
193                                  inode->extracted_file);
194                 return WIMLIB_ERR_NTFS_3G;
195         }
196         p = from_dentry->full_path_utf8 + from_dentry->full_path_utf8_len;
197         do {
198                 p--;
199         } while (*p != '/');
200
201         orig = *p;
202         *p = '\0';
203         dir_name = from_dentry->full_path_utf8;
204
205         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
206         if (!dir_ni) {
207                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
208                                  from_dentry->full_path_utf8);
209                 *p = orig;
210                 return WIMLIB_ERR_NTFS_3G;
211         }
212         *p = orig;
213
214         ret = ntfs_link(to_ni, dir_ni,
215                         (ntfschar*)from_dentry->file_name,
216                         from_dentry->file_name_len / 2);
217         if (ret != 0) {
218                 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
219                                  from_dentry->full_path_utf8,
220                                  inode->extracted_file);
221                 ret = WIMLIB_ERR_NTFS_3G;
222         }
223         *to_ni_ret = to_ni;
224         return ret;
225 }
226
227 static int
228 apply_file_attributes_and_security_data(ntfs_inode *ni,
229                                         ntfs_inode *dir_ni,
230                                         const struct dentry *dentry,
231                                         const WIMStruct *w)
232 {
233         DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
234               dentry->full_path_utf8, dentry->d_inode->attributes);
235         int ret;
236         struct SECURITY_CONTEXT ctx;
237         u32 attributes_le32;
238         attributes_le32 = cpu_to_le32(dentry->d_inode->attributes);
239         memset(&ctx, 0, sizeof(ctx));
240         ctx.vol = ni->vol;
241         ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
242                                          ni, dir_ni,
243                                          (const char*)&attributes_le32,
244                                          sizeof(u32), 0);
245         if (ret != 0) {
246                 ERROR("Failed to set NTFS file attributes on `%s'",
247                        dentry->full_path_utf8);
248                 return WIMLIB_ERR_NTFS_3G;
249         }
250         if (dentry->d_inode->security_id != -1) {
251                 const struct wim_security_data *sd;
252                 const char *descriptor;
253
254                 sd = wim_const_security_data(w);
255                 wimlib_assert(dentry->d_inode->security_id < sd->num_entries);
256                 descriptor = (const char *)sd->descriptors[dentry->d_inode->security_id];
257                 DEBUG("Applying security descriptor %d to `%s'",
258                       dentry->d_inode->security_id, dentry->full_path_utf8);
259
260                 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
261                                                  ni, dir_ni, descriptor,
262                                                  sd->sizes[dentry->d_inode->security_id], 0);
263
264                 if (ret != 0) {
265                         ERROR_WITH_ERRNO("Failed to set security data on `%s'",
266                                         dentry->full_path_utf8);
267                         return WIMLIB_ERR_NTFS_3G;
268                 }
269         }
270         return 0;
271 }
272
273 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
274                               struct apply_args *args)
275 {
276         struct lookup_table_entry *lte;
277         int ret = 0;
278
279         lte = inode_unnamed_lte_resolved(dentry->d_inode);
280
281         DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
282
283         if (!lte) {
284                 ERROR("Could not find reparse data for `%s'",
285                       dentry->full_path_utf8);
286                 return WIMLIB_ERR_INVALID_DENTRY;
287         }
288
289         if (wim_resource_size(lte) >= 0xffff) {
290                 ERROR("Reparse data of `%s' is too long (%"PRIu64" bytes)",
291                       dentry->full_path_utf8, wim_resource_size(lte));
292                 return WIMLIB_ERR_INVALID_DENTRY;
293         }
294
295         u8 reparse_data_buf[8 + wim_resource_size(lte)];
296         u8 *p = reparse_data_buf;
297         p = put_u32(p, dentry->d_inode->reparse_tag); /* ReparseTag */
298         p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
299         p = put_u16(p, 0); /* Reserved */
300
301         ret = read_full_wim_resource(lte, p, 0);
302         if (ret != 0)
303                 return ret;
304
305         ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
306                                          wim_resource_size(lte) + 8, 0);
307         if (ret != 0) {
308                 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
309                                  dentry->full_path_utf8);
310                 return WIMLIB_ERR_NTFS_3G;
311         }
312         args->progress.extract.completed_bytes += wim_resource_size(lte);
313         return 0;
314 }
315
316 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
317                                     struct apply_args *args);
318
319 /*
320  * If @dentry is part of a hard link group, search for hard-linked dentries in
321  * the same directory that have a nonempty DOS (short) filename.  There should
322  * be exactly 0 or 1 such dentries.  If there is 1, extract that dentry first,
323  * so that the DOS name is correctly associated with the corresponding long name
324  * in the Win32 namespace, and not any of the additional names in the POSIX
325  * namespace created from hard links.
326  */
327 static int preapply_dentry_with_dos_name(struct dentry *dentry,
328                                          ntfs_inode **dir_ni_p,
329                                          struct apply_args *args)
330 {
331         struct dentry *other;
332         struct dentry *dentry_with_dos_name;
333
334         dentry_with_dos_name = NULL;
335         inode_for_each_dentry(other, dentry->d_inode) {
336                 if (other != dentry && (dentry->parent == other->parent)
337                     && other->short_name_len)
338                 {
339                         if (dentry_with_dos_name) {
340                                 ERROR("Found multiple DOS names for file `%s' "
341                                       "in the same directory",
342                                       dentry_with_dos_name->full_path_utf8);
343                                 return WIMLIB_ERR_INVALID_DENTRY;
344                         }
345                         dentry_with_dos_name = other;
346                 }
347         }
348         /* If there's a dentry with a DOS name, extract it first */
349         if (dentry_with_dos_name && !dentry_with_dos_name->is_extracted) {
350                 char *p;
351                 const char *dir_name;
352                 char orig;
353                 int ret;
354                 ntfs_volume *vol = (*dir_ni_p)->vol;
355
356                 DEBUG("pre-applying DOS name `%s'",
357                       dentry_with_dos_name->full_path_utf8);
358                 ret = do_wim_apply_dentry_ntfs(dentry_with_dos_name,
359                                                *dir_ni_p, args);
360                 if (ret != 0)
361                         return ret;
362                 p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
363                 do {
364                         p--;
365                 } while (*p != '/');
366
367                 orig = *p;
368                 *p = '\0';
369                 dir_name = dentry->full_path_utf8;
370
371                 *dir_ni_p = ntfs_pathname_to_inode(vol, NULL, dir_name);
372                 *p = orig;
373                 if (!*dir_ni_p) {
374                         ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
375                                          dir_name);
376                         return WIMLIB_ERR_NTFS_3G;
377                 }
378         }
379         return 0;
380 }
381
382 /*
383  * Applies a WIM dentry to a NTFS filesystem.
384  *
385  * @dentry:  The WIM dentry to apply
386  * @dir_ni:  The NTFS inode for the parent directory
387  *
388  * @return:  0 on success; nonzero on failure.
389  */
390 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
391                                     struct apply_args *args)
392 {
393         int ret = 0;
394         mode_t type;
395         ntfs_inode *ni = NULL;
396         bool is_hardlink = false;
397         ntfs_volume *vol = dir_ni->vol;
398         struct inode *inode = dentry->d_inode;
399         dentry->is_extracted = true;
400
401         if (inode->attributes & FILE_ATTRIBUTE_DIRECTORY) {
402                 type = S_IFDIR;
403         } else {
404                 /* Apply hard-linked directory in same directory with DOS name
405                  * (if there is one) before this dentry */
406                 if (dentry->short_name_len == 0) {
407                         ret = preapply_dentry_with_dos_name(dentry,
408                                                             &dir_ni, args);
409                         if (ret != 0)
410                                 return ret;
411                 }
412
413                 type = S_IFREG;
414
415                 if (inode->link_count > 1) {
416                         /* Already extracted another dentry in the hard link
417                          * group.  We can make a hard link instead of extracting
418                          * the file data. */
419                         if (inode->extracted_file) {
420                                 ret = wim_apply_hardlink_ntfs(dentry, inode,
421                                                               dir_ni, &ni);
422                                 is_hardlink = true;
423                                 if (ret)
424                                         goto out_close_dir_ni;
425                                 else
426                                         goto out_set_dos_name;
427                         }
428                         /* Can't make a hard link; extract the file itself */
429                         FREE(inode->extracted_file);
430                         inode->extracted_file = STRDUP(dentry->full_path_utf8);
431                         if (!inode->extracted_file) {
432                                 ret = WIMLIB_ERR_NOMEM;
433                                 goto out_close_dir_ni;
434                         }
435                 }
436         }
437
438         /*
439          * Create a directory or file.
440          *
441          * Note: For symbolic links that are not directory junctions, pass
442          * S_IFREG here, since we manually set the reparse data later.
443          */
444         ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
445                          dentry->file_name_len / 2, type);
446
447         if (!ni) {
448                 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
449                                  dentry->full_path_utf8);
450                 ret = WIMLIB_ERR_NTFS_3G;
451                 goto out_close_dir_ni;
452         }
453
454         /* Write the data streams, unless this is a directory or reparse point
455          * */
456         if (!(inode->attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
457                                    FILE_ATTRIBUTE_DIRECTORY))) {
458                 ret = write_ntfs_data_streams(ni, dentry, args);
459                 if (ret != 0)
460                         goto out_close_dir_ni;
461         }
462
463
464         ret = apply_file_attributes_and_security_data(ni, dir_ni,
465                                                       dentry, args->w);
466         if (ret != 0)
467                 goto out_close_dir_ni;
468
469         if (inode->attributes & FILE_ATTR_REPARSE_POINT) {
470                 ret = apply_reparse_data(ni, dentry, args);
471                 if (ret != 0)
472                         goto out_close_dir_ni;
473         }
474
475 out_set_dos_name:
476         /* Set DOS (short) name if given */
477         if (dentry->short_name_len != 0) {
478
479                 char *short_name_utf8;
480                 size_t short_name_utf8_len;
481                 short_name_utf8 = utf16_to_utf8(dentry->short_name,
482                                                 dentry->short_name_len,
483                                                 &short_name_utf8_len);
484                 if (!short_name_utf8) {
485                         ERROR("Out of memory");
486                         ret = WIMLIB_ERR_NOMEM;
487                         goto out_close_dir_ni;
488                 }
489
490                 if (is_hardlink) {
491                         char *p;
492                         char orig;
493                         const char *dir_name;
494
495                         /* ntfs_set_ntfs_dos_name() closes the inodes in the
496                          * wrong order if we have applied a hard link.   Close
497                          * them ourselves, then re-open then. */
498                         if (ntfs_inode_close(dir_ni) != 0) {
499                                 if (ret == 0)
500                                         ret = WIMLIB_ERR_NTFS_3G;
501                                 ERROR_WITH_ERRNO("Failed to close directory inode");
502                         }
503                         if (ntfs_inode_close(ni) != 0) {
504                                 if (ret == 0)
505                                         ret = WIMLIB_ERR_NTFS_3G;
506                                 ERROR_WITH_ERRNO("Failed to close hard link target inode");
507                         }
508                         p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
509                         do {
510                                 p--;
511                         } while (*p != '/');
512
513                         orig = *p;
514                         *p = '\0';
515                         dir_name = dentry->full_path_utf8;
516
517                         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
518                         *p = orig;
519                         if (!dir_ni) {
520                                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
521                                                  dir_name);
522                                 return WIMLIB_ERR_NTFS_3G;
523                         }
524                         ni = ntfs_pathname_to_inode(vol, dir_ni,
525                                                     dentry->file_name_utf8);
526                         if (!ni) {
527                                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
528                                                  dir_name);
529                                 return WIMLIB_ERR_NTFS_3G;
530                         }
531                 }
532
533                 DEBUG("Setting short (DOS) name of `%s' to %s",
534                       dentry->full_path_utf8, short_name_utf8);
535
536                 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
537                                              short_name_utf8_len, 0);
538                 FREE(short_name_utf8);
539                 if (ret != 0) {
540                         ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
541                                          dentry->full_path_utf8);
542                         ret = WIMLIB_ERR_NTFS_3G;
543                 }
544                 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
545                 return ret;
546         }
547
548 out_close_dir_ni:
549         if (ntfs_inode_close(dir_ni) != 0) {
550                 if (ret == 0)
551                         ret = WIMLIB_ERR_NTFS_3G;
552                 ERROR_WITH_ERRNO("Failed to close directory inode");
553         }
554         if (ni && ntfs_inode_close(ni) != 0) {
555                 if (ret == 0)
556                         ret = WIMLIB_ERR_NTFS_3G;
557                 ERROR_WITH_ERRNO("Failed to close inode");
558         }
559         return ret;
560 }
561
562 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
563                                       ntfs_volume *vol,
564                                       const WIMStruct *w)
565 {
566         ntfs_inode *ni;
567         int ret = 0;
568
569         wimlib_assert(dentry_is_directory(dentry));
570         ni = ntfs_pathname_to_inode(vol, NULL, "/");
571         if (!ni) {
572                 ERROR_WITH_ERRNO("Could not find root NTFS inode");
573                 return WIMLIB_ERR_NTFS_3G;
574         }
575         ret = apply_file_attributes_and_security_data(ni, ni, dentry, w);
576         if (ntfs_inode_close(ni) != 0) {
577                 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
578                                  "directory");
579                 ret = WIMLIB_ERR_NTFS_3G;
580         }
581         return ret;
582 }
583
584 /* Applies a WIM dentry to the NTFS volume */
585 int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
586 {
587         struct apply_args *args = arg;
588         ntfs_volume *vol             = args->vol;
589         int extract_flags            = args->extract_flags;
590         WIMStruct *w                 = args->w;
591         ntfs_inode *dir_ni;
592         char *p;
593         char orig;
594         const char *dir_name;
595
596         if (dentry->is_extracted)
597                 return 0;
598
599         if (extract_flags & WIMLIB_EXTRACT_FLAG_NO_STREAMS)
600                 if (inode_unnamed_lte_resolved(dentry->d_inode))
601                         return 0;
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, arg);
628 }
629
630 int wim_apply_dentry_timestamps(struct dentry *dentry, void *arg)
631 {
632         struct 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         DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
640
641         ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
642         if (!ni) {
643                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
644                                  dentry->full_path_utf8);
645                 return WIMLIB_ERR_NTFS_3G;
646         }
647
648         p = buf;
649         p = put_u64(p, dentry->d_inode->creation_time);
650         p = put_u64(p, dentry->d_inode->last_write_time);
651         p = put_u64(p, dentry->d_inode->last_access_time);
652         ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
653         if (ret != 0) {
654                 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
655                                  dentry->full_path_utf8);
656                 ret = WIMLIB_ERR_NTFS_3G;
657         }
658
659         if (ntfs_inode_close(ni) != 0) {
660                 if (ret == 0)
661                         ret = WIMLIB_ERR_NTFS_3G;
662                 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
663                                  dentry->full_path_utf8);
664         }
665         return ret;
666 }