]> wimlib.net Git - wimlib/blob - src/ntfs-apply.c
Win32 capture
[wimlib] / src / ntfs-apply.c
1 /*
2  * ntfs-apply.c
3  *
4  * Apply a WIM image to a NTFS volume.  Restore as much information as possible,
5  * including security data, file attributes, DOS names, and alternate data
6  * streams.
7  */
8
9 /*
10  * Copyright (C) 2012, 2013 Eric Biggers
11  *
12  * This file is part of wimlib, a library for working with WIM files.
13  *
14  * wimlib is free software; you can redistribute it and/or modify it under the
15  * terms of the GNU General Public License as published by the Free
16  * Software Foundation; either version 3 of the License, or (at your option)
17  * any later version.
18  *
19  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21  * A PARTICULAR PURPOSE. See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with wimlib; if not, see http://www.gnu.org/licenses/.
26  */
27
28
29 #include "config.h"
30
31 #include <ntfs-3g/endians.h>
32 #include <ntfs-3g/types.h>
33
34 #include "wimlib_internal.h"
35 #include "buffer_io.h"
36 #include "dentry.h"
37 #include "lookup_table.h"
38
39 #include <ntfs-3g/attrib.h>
40 #include <ntfs-3g/security.h> /* security.h before xattrs.h */
41 #include <ntfs-3g/reparse.h>
42 #include <ntfs-3g/xattrs.h>
43 #include <string.h>
44
45 static int extract_wim_chunk_to_ntfs_attr(const u8 *buf, size_t len,
46                                           u64 offset, void *arg)
47 {
48         ntfs_attr *na = arg;
49         if (ntfs_attr_pwrite(na, offset, len, buf) == len) {
50                 return 0;
51         } else {
52                 ERROR_WITH_ERRNO("Error extracting WIM resource to NTFS attribute");
53                 return WIMLIB_ERR_WRITE;
54         }
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 wim_lookup_table_entry *lte,
62                                   ntfs_attr *na)
63 {
64         return extract_wim_resource(lte, wim_resource_size(lte),
65                                     extract_wim_chunk_to_ntfs_attr, na);
66 }
67
68 /* Writes the data streams of a WIM inode to the data attributes of a NTFS
69  * inode.
70  *
71  * @ni:      The NTFS inode to which the streams are to be extracted.
72  *
73  * @dentry:  The WIM dentry being extracted.  The @d_inode member points to the
74  *           corresponding WIM inode that contains the streams being extracted.
75  *           The WIM dentry itself is only needed to provide a file path for
76  *           better error messages.
77  *
78  * @progress_info:  Progress information for the image application.  The number
79  *                  of extracted bytes will be incremented by the uncompressed
80  *                  size of each stream extracted.
81  *
82  * Returns 0 on success, nonzero on failure.
83  */
84 static int write_ntfs_data_streams(ntfs_inode *ni, const struct wim_dentry *dentry,
85                                    union wimlib_progress_info *progress_info)
86 {
87         int ret = 0;
88         unsigned stream_idx = 0;
89         ntfschar *stream_name = AT_UNNAMED;
90         u32 stream_name_len = 0;
91         const char *stream_name_utf8;
92         const struct wim_inode *inode = dentry->d_inode;
93         struct wim_lookup_table_entry *lte;
94
95         DEBUG("Writing %u NTFS data stream%s for `%s'",
96               inode->i_num_ads + 1,
97               (inode->i_num_ads == 0 ? "" : "s"),
98               dentry->full_path_utf8);
99
100         lte = inode->i_lte;
101         while (1) {
102                 if (stream_name_len) {
103
104                         /* Skip special UNIX data entries (see documentation for
105                          * WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) */
106                         if (stream_name_len == WIMLIB_UNIX_DATA_TAG_LEN
107                             && !memcmp(stream_name_utf8,
108                                        WIMLIB_UNIX_DATA_TAG,
109                                        WIMLIB_UNIX_DATA_TAG_LEN))
110                                 goto cont;
111
112                         /* Create an empty named stream. */
113                         ret = ntfs_attr_add(ni, AT_DATA, stream_name,
114                                             stream_name_len, NULL, 0);
115                         if (ret != 0) {
116                                 ERROR_WITH_ERRNO("Failed to create name data "
117                                                  "stream for extracted file "
118                                                  "`%s'",
119                                                  dentry->full_path_utf8);
120                                 ret = WIMLIB_ERR_NTFS_3G;
121                                 break;
122
123                         }
124                 }
125
126                 /* If there's no lookup table entry, it's an empty stream.
127                  * Otherwise, open the attribute and extract the data. */
128                 if (lte) {
129                         ntfs_attr *na;
130
131                         na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
132                         if (!na) {
133                                 ERROR_WITH_ERRNO("Failed to open a data stream of "
134                                                  "extracted file `%s'",
135                                                  dentry->full_path_utf8);
136                                 ret = WIMLIB_ERR_NTFS_3G;
137                                 break;
138                         }
139
140                         /* The WIM lookup table entry provides the stream
141                          * length, so the NTFS attribute should be resized to
142                          * this length before starting to extract the data. */
143                         ret = ntfs_attr_truncate_solid(na, wim_resource_size(lte));
144                         if (ret != 0) {
145                                 ntfs_attr_close(na);
146                                 break;
147                         }
148
149                         /* Actually extract the stream */
150                         ret = extract_wim_resource_to_ntfs_attr(lte, na);
151
152                         /* Close the attribute */
153                         ntfs_attr_close(na);
154                         if (ret != 0)
155                                 break;
156
157                         /* Record the number of bytes of uncompressed data that
158                          * have been extracted. */
159                         progress_info->extract.completed_bytes += wim_resource_size(lte);
160                 }
161         cont:
162                 if (stream_idx == inode->i_num_ads) /* Has the last stream been extracted? */
163                         break;
164
165                 /* Get the name and lookup table entry for the next stream. */
166                 stream_name = (ntfschar*)inode->i_ads_entries[stream_idx].stream_name;
167                 stream_name_utf8 = inode->i_ads_entries[stream_idx].stream_name_utf8;
168                 stream_name_len = inode->i_ads_entries[stream_idx].stream_name_len / 2;
169                 lte = inode->i_ads_entries[stream_idx].lte;
170                 stream_idx++;
171         }
172         return ret;
173 }
174
175 /* Open the NTFS inode that corresponds to the parent of a WIM dentry.  Returns
176  * the opened inode, or NULL on failure. */
177 static ntfs_inode *dentry_open_parent_ni(const struct wim_dentry *dentry,
178                                          ntfs_volume *vol)
179 {
180         char *p;
181         const char *dir_name;
182         ntfs_inode *dir_ni;
183         char orig;
184
185         p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
186         do {
187                 p--;
188         } while (*p != '/');
189
190         orig = *p;
191         *p = '\0';
192         dir_name = dentry->full_path_utf8;
193         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
194         if (!dir_ni) {
195                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
196                                  dir_name);
197         }
198         *p = orig;
199         return dir_ni;
200 }
201
202 /*
203  * Makes a NTFS hard link.
204  *
205  * The hard link is named @from_dentry->file_name and is located under the
206  * directory specified by @dir_ni, and it is made to point to the previously
207  * extracted file located at @inode->i_extracted_file.
208  *
209  * Or, in other words, this adds a new name @from_dentry->full_path_utf8 to an
210  * existing NTFS inode which already has a name @inode->i_extracted_file.
211  *
212  * The new name is made in the POSIX namespace (this is the behavior of
213  * ntfs_link()).
214  *
215  * Return 0 on success, nonzero on failure.  dir_ni is closed either way.
216  */
217 static int apply_ntfs_hardlink(const struct wim_dentry *from_dentry,
218                                const struct wim_inode *inode,
219                                ntfs_inode *dir_ni)
220 {
221         int ret;
222         ntfs_inode *to_ni;
223         ntfs_volume *vol;
224
225         vol = dir_ni->vol;
226         ret = ntfs_inode_close(dir_ni);
227         if (ret != 0) {
228                 ERROR_WITH_ERRNO("Error closing directory");
229                 return WIMLIB_ERR_NTFS_3G;
230         }
231
232         DEBUG("Extracting NTFS hard link `%s' => `%s'",
233               from_dentry->full_path_utf8, inode->i_extracted_file);
234
235         to_ni = ntfs_pathname_to_inode(vol, NULL, inode->i_extracted_file);
236         if (!to_ni) {
237                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
238                                  inode->i_extracted_file);
239                 return WIMLIB_ERR_NTFS_3G;
240         }
241
242         dir_ni = dentry_open_parent_ni(from_dentry, vol);
243         if (!dir_ni) {
244                 ntfs_inode_close(to_ni);
245                 return WIMLIB_ERR_NTFS_3G;
246         }
247
248         ret = ntfs_link(to_ni, dir_ni,
249                         (ntfschar*)from_dentry->file_name,
250                         from_dentry->file_name_len / 2);
251         ret |= ntfs_inode_close(dir_ni);
252         ret |= ntfs_inode_close(to_ni);
253         if (ret) {
254                 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
255                                  from_dentry->full_path_utf8,
256                                  inode->i_extracted_file);
257                 ret = WIMLIB_ERR_NTFS_3G;
258         }
259         return ret;
260 }
261
262 /* Transfers file attributes and possibly a security descriptor from a WIM inode
263  * to a NTFS inode.
264  *
265  * @ni:      The NTFS inode to apply the metadata to.
266  * @dir_ni:  The NTFS inode for a directory containing @ni.
267  * @dentry:  The WIM dentry whose inode contains the metadata to apply.
268  * @w:       The WIMStruct for the WIM, through which the table of security
269  *              descriptors can be accessed.
270  *
271  * Returns 0 on success, nonzero on failure.
272  */
273 static int
274 apply_file_attributes_and_security_data(ntfs_inode *ni,
275                                         ntfs_inode *dir_ni,
276                                         const struct wim_dentry *dentry,
277                                         const WIMStruct *w)
278 {
279         int ret;
280         struct SECURITY_CONTEXT ctx;
281         u32 attributes_le32;
282         const struct wim_inode *inode;
283
284         inode = dentry->d_inode;
285
286         DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
287               dentry->full_path_utf8, inode->i_attributes);
288
289         attributes_le32 = cpu_to_le32(inode->i_attributes);
290         memset(&ctx, 0, sizeof(ctx));
291         ctx.vol = ni->vol;
292         ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
293                                          ni, dir_ni,
294                                          (const char*)&attributes_le32,
295                                          sizeof(u32), 0);
296         if (ret != 0) {
297                 ERROR("Failed to set NTFS file attributes on `%s'",
298                        dentry->full_path_utf8);
299                 return WIMLIB_ERR_NTFS_3G;
300         }
301         if (inode->i_security_id != -1) {
302                 const char *desc;
303                 const struct wim_security_data *sd;
304
305                 sd = wim_const_security_data(w);
306                 wimlib_assert(inode->i_security_id < sd->num_entries);
307                 desc = (const char *)sd->descriptors[inode->i_security_id];
308                 DEBUG("Applying security descriptor %d to `%s'",
309                       inode->i_security_id, dentry->full_path_utf8);
310
311                 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
312                                                  ni, dir_ni, desc,
313                                                  sd->sizes[inode->i_security_id], 0);
314
315                 if (ret != 0) {
316                         ERROR_WITH_ERRNO("Failed to set security data on `%s'",
317                                         dentry->full_path_utf8);
318                         return WIMLIB_ERR_NTFS_3G;
319                 }
320         }
321         return 0;
322 }
323
324 /*
325  * Transfers the reparse data from a WIM inode (which must represent a reparse
326  * point) to a NTFS inode.
327  */
328 static int apply_reparse_data(ntfs_inode *ni, const struct wim_dentry *dentry,
329                               union wimlib_progress_info *progress_info)
330 {
331         struct wim_lookup_table_entry *lte;
332         int ret = 0;
333
334         lte = inode_unnamed_lte_resolved(dentry->d_inode);
335
336         DEBUG("Applying reparse data to `%s'", dentry->full_path_utf8);
337
338         if (!lte) {
339                 ERROR("Could not find reparse data for `%s'",
340                       dentry->full_path_utf8);
341                 return WIMLIB_ERR_INVALID_DENTRY;
342         }
343
344         if (wim_resource_size(lte) >= 0xffff) {
345                 ERROR("Reparse data of `%s' is too long (%"PRIu64" bytes)",
346                       dentry->full_path_utf8, wim_resource_size(lte));
347                 return WIMLIB_ERR_INVALID_DENTRY;
348         }
349
350         u8 reparse_data_buf[8 + wim_resource_size(lte)];
351         u8 *p = reparse_data_buf;
352         p = put_u32(p, dentry->d_inode->i_reparse_tag); /* ReparseTag */
353         DEBUG("ReparseTag = %#x", dentry->d_inode->i_reparse_tag);
354         p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
355         p = put_u16(p, 0); /* Reserved */
356
357         ret = read_full_wim_resource(lte, p, 0);
358         if (ret != 0)
359                 return ret;
360
361         ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
362                                          wim_resource_size(lte) + 8, 0);
363         if (ret != 0) {
364                 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
365                                  dentry->full_path_utf8);
366                 return WIMLIB_ERR_NTFS_3G;
367         }
368         progress_info->extract.completed_bytes += wim_resource_size(lte);
369         return 0;
370 }
371
372 /*
373  * Applies a WIM dentry to a NTFS filesystem.
374  *
375  * @dentry:  The WIM dentry to apply
376  * @dir_ni:  The NTFS inode for the parent directory
377  *
378  * @return:  0 on success; nonzero on failure.
379  */
380 static int do_apply_dentry_ntfs(struct wim_dentry *dentry, ntfs_inode *dir_ni,
381                                 struct apply_args *args)
382 {
383         int ret = 0;
384         mode_t type;
385         ntfs_inode *ni = NULL;
386         struct wim_inode *inode = dentry->d_inode;
387         dentry->is_extracted = 1;
388
389         if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
390                 type = S_IFDIR;
391         } else {
392                 type = S_IFREG;
393                 if (inode->i_nlink > 1) {
394                         /* Inode has multiple dentries referencing it. */
395                         if (inode->i_extracted_file) {
396                                 /* Already extracted another dentry in the hard
397                                  * link group.  Make a hard link instead of
398                                  * extracting the file data. */
399                                 ret = apply_ntfs_hardlink(dentry, inode, dir_ni);
400                                 /* dir_ni was closed */
401                                 goto out;
402                         } else {
403                                 /* None of the dentries of this inode have been
404                                  * extracted yet, so go ahead and extract the
405                                  * first one. */
406                                 FREE(inode->i_extracted_file);
407                                 inode->i_extracted_file = STRDUP(dentry->full_path_utf8);
408                                 if (!inode->i_extracted_file) {
409                                         ret = WIMLIB_ERR_NOMEM;
410                                         goto out_close_dir_ni;
411                                 }
412                         }
413                 }
414         }
415
416         /* Create a NTFS directory or file.
417          *
418          * Note: For symbolic links that are not directory junctions, S_IFREG is
419          * passed here, since the reparse data and file attributes are set
420          * later. */
421         ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
422                          dentry->file_name_len / 2, type);
423
424         if (!ni) {
425                 ERROR_WITH_ERRNO("Could not create NTFS inode for `%s'",
426                                  dentry->full_path_utf8);
427                 ret = WIMLIB_ERR_NTFS_3G;
428                 goto out_close_dir_ni;
429         }
430
431         /* Write the data streams, unless this is a directory or reparse point
432          * */
433         if (!(inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
434                                    FILE_ATTRIBUTE_DIRECTORY))) {
435                 ret = write_ntfs_data_streams(ni, dentry, &args->progress);
436                 if (ret != 0)
437                         goto out_close_dir_ni;
438         }
439
440
441         ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry,
442                                                       args->w);
443         if (ret != 0)
444                 goto out_close_dir_ni;
445
446         if (inode->i_attributes & FILE_ATTR_REPARSE_POINT) {
447                 ret = apply_reparse_data(ni, dentry, &args->progress);
448                 if (ret != 0)
449                         goto out_close_dir_ni;
450         }
451
452         /* Set DOS (short) name if given */
453         if (dentry->short_name_len != 0) {
454                 char *short_name_utf8;
455                 size_t short_name_utf8_len;
456                 ret = utf16_to_utf8(dentry->short_name,
457                                     dentry->short_name_len,
458                                     &short_name_utf8,
459                                     &short_name_utf8_len);
460                 if (ret != 0)
461                         goto out_close_dir_ni;
462
463                 DEBUG("Setting short (DOS) name of `%s' to %s",
464                       dentry->full_path_utf8, short_name_utf8);
465
466                 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_utf8,
467                                              short_name_utf8_len, 0);
468                 FREE(short_name_utf8);
469                 if (ret != 0) {
470                         ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
471                                          dentry->full_path_utf8);
472                         ret = WIMLIB_ERR_NTFS_3G;
473                 }
474                 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
475                 goto out;
476         }
477 out_close_dir_ni:
478         if (dir_ni) {
479                 if (ni) {
480                         if (ntfs_inode_close_in_dir(ni, dir_ni)) {
481                                 if (ret == 0)
482                                         ret = WIMLIB_ERR_NTFS_3G;
483                                 ERROR_WITH_ERRNO("Failed to close inode for `%s'",
484                                                  dentry->full_path_utf8);
485                         }
486                 }
487                 if (ntfs_inode_close(dir_ni)) {
488                         if (ret == 0)
489                                 ret = WIMLIB_ERR_NTFS_3G;
490                         ERROR_WITH_ERRNO("Failed to close inode of directory "
491                                          "containing `%s'", dentry->full_path_utf8);
492                 }
493         }
494 out:
495         return ret;
496 }
497
498 static int apply_root_dentry_ntfs(const struct wim_dentry *dentry,
499                                   ntfs_volume *vol, const WIMStruct *w)
500 {
501         ntfs_inode *ni;
502         int ret = 0;
503
504         ni = ntfs_pathname_to_inode(vol, NULL, "/");
505         if (!ni) {
506                 ERROR_WITH_ERRNO("Could not find root NTFS inode");
507                 return WIMLIB_ERR_NTFS_3G;
508         }
509         ret = apply_file_attributes_and_security_data(ni, ni, dentry, w);
510         if (ntfs_inode_close(ni) != 0) {
511                 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
512                                  "directory");
513                 ret = WIMLIB_ERR_NTFS_3G;
514         }
515         return ret;
516 }
517
518 /* Applies a WIM dentry to the NTFS volume */
519 int apply_dentry_ntfs(struct wim_dentry *dentry, void *arg)
520 {
521         struct apply_args *args = arg;
522         ntfs_volume *vol = args->vol;
523         WIMStruct *w = args->w;
524         struct wim_dentry *orig_dentry;
525         struct wim_dentry *other;
526         int ret;
527
528         /* Treat the root dentry specially. */
529         if (dentry_is_root(dentry))
530                 return apply_root_dentry_ntfs(dentry, vol, w);
531
532         /* NTFS filename namespaces need careful consideration.  A name for a
533          * NTFS file may be in either the POSIX, Win32, DOS, or Win32+DOS
534          * namespaces.  A NTFS file (a.k.a. inode) may have multiple names in
535          * multiple directories (i.e. hard links); however, a NTFS file can have
536          * at most 1 DOS name total.  Furthermore, a Win32 name is always
537          * associated with a DOS name (either as a Win32+DOS name, or a Win32
538          * name and a DOS name separately), which implies that a NTFS file can
539          * have at most 1 Win32 name.
540          *
541          * A WIM dentry just contains a "long name", which wimlib makes sure is
542          * non-empty, and a "short name", which may be empty.  So, wimlib must
543          * map these to the correct NTFS names.  wimlib collects all WIM
544          * dentries that map to the same NTFS inode and factors out the common
545          * information into a 'struct wim_inode', so this should make the
546          * mapping a little more obvious.  As a NTFS file can have at most 1 DOS
547          * name, a WIM inode cannot have more than 1 dentry with a non-empty
548          * short name, and this is checked in the verify_inode() function in
549          * verify.c.  Furthermore, a WIM dentry, if any, that has a DOS name
550          * must have a long name that corresponds to a Win32 name or Win32+DOS
551          * name.
552          *
553          * WIM dentries that have a long name but no associated short name are
554          * assumed to be in the POSIX namespace.
555          *
556          * So, given a WIM inode that is to map to a NTFS inode, we must apply
557          * the Win32 and DOS or Win32+DOS names, if they exist, then any
558          * additional (POSIX) names.  A caveat when actually doing this:  as
559          * confirmed by the libntfs-3g authors, ntfs_set_ntfs_dos_name() is only
560          * guaranteed to associate a DOS name with the appropriate long name if
561          * it's called when that long name is the only one in existence for that
562          * file.  So, this implies that the correct ordering of function calls
563          * to extract a NTFS file are:
564          *
565          *      if (file has a DOS name) {
566          *              - Call ntfs_create() to create long name associated with
567          *              the DOS name (this initially creates a POSIX name)
568          *              - Call ntfs_set_ntfs_dos_name() to associate a DOS name
569          *              with the long name just created.  This either changes
570          *              the POSIX name to Win32+DOS, or changes the POSIX name
571          *              to Win32 and creates a separate DOS name.
572          *      } else {
573          *              - Call ntfs_create() to create the first link to the
574          *              file in the POSIX namespace
575          *      }
576          *      - Call ntfs_link() to create the other names of the file, in the
577          *      POSIX namespace.
578          */
579 again:
580         orig_dentry = NULL;
581         if (!dentry->d_inode->i_dos_name_extracted &&
582             dentry->short_name_len == 0)
583         {
584                 inode_for_each_dentry(other, dentry->d_inode) {
585                         if (other->short_name_len != 0) {
586                                 orig_dentry = dentry;
587                                 dentry = other;
588                                 break;
589                         }
590                 }
591         }
592         dentry->d_inode->i_dos_name_extracted = 1;
593         ntfs_inode *dir_ni = dentry_open_parent_ni(dentry, vol);
594         if (dir_ni) {
595                 ret = do_apply_dentry_ntfs(dentry, dir_ni, arg);
596                 if (ret == 0 && orig_dentry != NULL) {
597                         dentry = orig_dentry;
598                         goto again;
599                 }
600         } else {
601                 ret = WIMLIB_ERR_NTFS_3G;
602         }
603         return ret;
604 }
605
606 /* Transfers the 100-nanosecond precision timestamps from a WIM dentry to a NTFS
607  * inode */
608 int apply_dentry_timestamps_ntfs(struct wim_dentry *dentry, void *arg)
609 {
610         struct apply_args *args = arg;
611         ntfs_volume *vol = args->vol;
612         u8 *p;
613         u8 buf[24];
614         ntfs_inode *ni;
615         int ret;
616
617         DEBUG("Setting timestamps on `%s'", dentry->full_path_utf8);
618
619         ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path_utf8);
620         if (!ni) {
621                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
622                                  dentry->full_path_utf8);
623                 return WIMLIB_ERR_NTFS_3G;
624         }
625
626         p = buf;
627         p = put_u64(p, dentry->d_inode->i_creation_time);
628         p = put_u64(p, dentry->d_inode->i_last_write_time);
629         p = put_u64(p, dentry->d_inode->i_last_access_time);
630         ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
631         if (ret != 0) {
632                 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
633                                  dentry->full_path_utf8);
634                 ret = WIMLIB_ERR_NTFS_3G;
635         }
636
637         if (ntfs_inode_close(ni) != 0) {
638                 if (ret == 0)
639                         ret = WIMLIB_ERR_NTFS_3G;
640                 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
641                                  dentry->full_path_utf8);
642         }
643         return ret;
644 }