]> wimlib.net Git - wimlib/blob - src/ntfs-apply.c
245e0b55c3a1feb1d807e5979d556cd9401cb42c
[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 #include <locale.h>
45
46 static int extract_wim_chunk_to_ntfs_attr(const void *buf, size_t len,
47                                           u64 offset, void *arg)
48 {
49         ntfs_attr *na = arg;
50         if (ntfs_attr_pwrite(na, offset, len, buf) == len) {
51                 return 0;
52         } else {
53                 ERROR_WITH_ERRNO("Error extracting WIM resource to NTFS attribute");
54                 return WIMLIB_ERR_WRITE;
55         }
56 }
57
58 /*
59  * Extracts a WIM resource to a NTFS attribute.
60  */
61 static int
62 extract_wim_resource_to_ntfs_attr(const struct wim_lookup_table_entry *lte,
63                                   ntfs_attr *na)
64 {
65         return extract_wim_resource(lte, wim_resource_size(lte),
66                                     extract_wim_chunk_to_ntfs_attr, na);
67 }
68
69 /* Writes the data streams of a WIM inode to the data attributes of a NTFS
70  * inode.
71  *
72  * @ni:      The NTFS inode to which the streams are to be extracted.
73  *
74  * @dentry:  The WIM dentry being extracted.  The @d_inode member points to the
75  *           corresponding WIM inode that contains the streams being extracted.
76  *           The WIM dentry itself is only needed to provide a file path for
77  *           better error messages.
78  *
79  * @progress_info:  Progress information for the image application.  The number
80  *                  of extracted bytes will be incremented by the uncompressed
81  *                  size of each stream extracted.
82  *
83  * Returns 0 on success, nonzero on failure.
84  */
85 static int write_ntfs_data_streams(ntfs_inode *ni, const struct wim_dentry *dentry,
86                                    union wimlib_progress_info *progress_info)
87 {
88         int ret = 0;
89         unsigned stream_idx = 0;
90         ntfschar *stream_name = AT_UNNAMED;
91         u32 stream_name_nbytes = 0;
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);
99
100         lte = inode->i_lte;
101         while (1) {
102                 if (stream_name_nbytes) {
103                         /* Skip special UNIX data entries (see documentation for
104                          * WIMLIB_ADD_IMAGE_FLAG_UNIX_DATA) */
105                         if (stream_name_nbytes == WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES
106                             && !memcmp(stream_name,
107                                        WIMLIB_UNIX_DATA_TAG_UTF16LE,
108                                        WIMLIB_UNIX_DATA_TAG_UTF16LE_NBYTES))
109                                 goto cont;
110
111                         /* Create an empty named stream. */
112                         ret = ntfs_attr_add(ni, AT_DATA, stream_name,
113                                             stream_name_nbytes / 2, NULL, 0);
114                         if (ret != 0) {
115                                 ERROR_WITH_ERRNO("Failed to create named data "
116                                                  "stream for extracted file "
117                                                  "`%s'",
118                                                  dentry->full_path);
119                                 ret = WIMLIB_ERR_NTFS_3G;
120                                 break;
121
122                         }
123                 }
124
125                 /* If there's no lookup table entry, it's an empty stream.
126                  * Otherwise, open the attribute and extract the data. */
127                 if (lte) {
128                         ntfs_attr *na;
129
130                         na = ntfs_attr_open(ni, AT_DATA, stream_name,
131                                             stream_name_nbytes / 2);
132                         if (!na) {
133                                 ERROR_WITH_ERRNO("Failed to open a data stream of "
134                                                  "extracted file `%s'",
135                                                  dentry->full_path);
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 = inode->i_ads_entries[stream_idx].stream_name;
167                 stream_name_nbytes = inode->i_ads_entries[stream_idx].stream_name_nbytes;
168                 lte = inode->i_ads_entries[stream_idx].lte;
169                 stream_idx++;
170         }
171         return ret;
172 }
173
174 /* Open the NTFS inode that corresponds to the parent of a WIM dentry.  Returns
175  * the opened inode, or NULL on failure. */
176 static ntfs_inode *
177 dentry_open_parent_ni(const struct wim_dentry *dentry, ntfs_volume *vol)
178 {
179         char *p;
180         const char *dir_name;
181         ntfs_inode *dir_ni;
182         char orig;
183
184         p = dentry->full_path + dentry->full_path_nbytes;
185         do {
186                 p--;
187         } while (*p != '/');
188
189         orig = *p;
190         *p = '\0';
191         dir_name = dentry->full_path;
192         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
193         if (!dir_ni) {
194                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
195                                  dir_name);
196         }
197         *p = orig;
198         return dir_ni;
199 }
200
201 /*
202  * Makes a NTFS hard link.
203  *
204  * The hard link is named @from_dentry->file_name and is located under the
205  * directory specified by @dir_ni, and it is made to point to the previously
206  * extracted file located at @inode->i_extracted_file.
207  *
208  * Or, in other words, this adds a new name @from_dentry->full_path to an
209  * existing NTFS inode which already has a name @inode->i_extracted_file.
210  *
211  * The new name is made in the POSIX namespace (this is the behavior of
212  * ntfs_link()).
213  *
214  * Return 0 on success, nonzero on failure.  dir_ni is closed either way.
215  */
216 static int
217 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, 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                         from_dentry->file_name,
250                         from_dentry->file_name_nbytes / 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,
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                                         int extract_flags)
279 {
280         int ret;
281         struct SECURITY_CONTEXT ctx;
282         u32 attributes_le32;
283         const struct wim_inode *inode;
284
285         inode = dentry->d_inode;
286
287         DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
288               dentry->full_path, inode->i_attributes);
289
290         attributes_le32 = cpu_to_le32(inode->i_attributes);
291         memset(&ctx, 0, sizeof(ctx));
292         ctx.vol = ni->vol;
293         ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
294                                          ni, dir_ni,
295                                          (const char*)&attributes_le32,
296                                          sizeof(u32), 0);
297         if (ret != 0) {
298                 ERROR("Failed to set NTFS file attributes on `%s'",
299                        dentry->full_path);
300                 return WIMLIB_ERR_NTFS_3G;
301         }
302         if (inode->i_security_id != -1 &&
303             !(extract_flags & WIMLIB_EXTRACT_FLAG_NOACLS))
304         {
305                 const char *desc;
306                 const struct wim_security_data *sd;
307
308                 sd = wim_const_security_data(w);
309                 wimlib_assert(inode->i_security_id < sd->num_entries);
310                 desc = (const char *)sd->descriptors[inode->i_security_id];
311                 DEBUG("Applying security descriptor %d to `%s'",
312                       inode->i_security_id, dentry->full_path);
313
314                 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
315                                                  ni, dir_ni, desc,
316                                                  sd->sizes[inode->i_security_id], 0);
317
318                 if (ret != 0) {
319                         ERROR_WITH_ERRNO("Failed to set security data on `%s'",
320                                         dentry->full_path);
321                         return WIMLIB_ERR_NTFS_3G;
322                 }
323         }
324         return 0;
325 }
326
327 /*
328  * Transfers the reparse data from a WIM inode (which must represent a reparse
329  * point) to a NTFS inode.
330  */
331 static int apply_reparse_data(ntfs_inode *ni, const struct wim_dentry *dentry,
332                               union wimlib_progress_info *progress_info)
333 {
334         struct wim_lookup_table_entry *lte;
335         int ret = 0;
336
337         lte = inode_unnamed_lte_resolved(dentry->d_inode);
338
339         DEBUG("Applying reparse data to `%s'", dentry->full_path);
340
341         if (!lte) {
342                 ERROR("Could not find reparse data for `%s'",
343                       dentry->full_path);
344                 return WIMLIB_ERR_INVALID_DENTRY;
345         }
346
347         if (wim_resource_size(lte) >= 0xffff) {
348                 ERROR("Reparse data of `%s' is too long (%"PRIu64" bytes)",
349                       dentry->full_path, wim_resource_size(lte));
350                 return WIMLIB_ERR_INVALID_DENTRY;
351         }
352
353         u8 reparse_data_buf[8 + wim_resource_size(lte)];
354         u8 *p = reparse_data_buf;
355         p = put_u32(p, dentry->d_inode->i_reparse_tag); /* ReparseTag */
356         DEBUG("ReparseTag = %#x", dentry->d_inode->i_reparse_tag);
357         p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
358         p = put_u16(p, 0); /* Reserved */
359
360         ret = read_full_wim_resource(lte, p, 0);
361         if (ret != 0)
362                 return ret;
363
364         ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
365                                          wim_resource_size(lte) + 8, 0);
366         if (ret != 0) {
367                 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
368                                  dentry->full_path);
369                 return WIMLIB_ERR_NTFS_3G;
370         }
371         progress_info->extract.completed_bytes += wim_resource_size(lte);
372         return 0;
373 }
374
375 /*
376  * Applies a WIM dentry to a NTFS filesystem.
377  *
378  * @dentry:  The WIM dentry to apply
379  * @dir_ni:  The NTFS inode for the parent directory
380  *
381  * @return:  0 on success; nonzero on failure.
382  */
383 static int
384 do_apply_dentry_ntfs(struct wim_dentry *dentry, ntfs_inode *dir_ni,
385                      struct apply_args *args)
386 {
387         int ret = 0;
388         mode_t type;
389         ntfs_inode *ni = NULL;
390         struct wim_inode *inode = dentry->d_inode;
391         dentry->is_extracted = 1;
392
393         if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
394                 type = S_IFDIR;
395         } else {
396                 type = S_IFREG;
397                 if (inode->i_nlink > 1) {
398                         /* Inode has multiple dentries referencing it. */
399                         if (inode->i_extracted_file) {
400                                 /* Already extracted another dentry in the hard
401                                  * link group.  Make a hard link instead of
402                                  * extracting the file data. */
403                                 ret = apply_ntfs_hardlink(dentry, inode, dir_ni);
404                                 /* dir_ni was closed */
405                                 goto out;
406                         } else {
407                                 /* None of the dentries of this inode have been
408                                  * extracted yet, so go ahead and extract the
409                                  * first one. */
410                                 FREE(inode->i_extracted_file);
411                                 inode->i_extracted_file = STRDUP(dentry->full_path);
412                                 if (!inode->i_extracted_file) {
413                                         ret = WIMLIB_ERR_NOMEM;
414                                         goto out_close_dir_ni;
415                                 }
416                         }
417                 }
418         }
419
420         /* Create a NTFS directory or file.
421          *
422          * Note: For symbolic links that are not directory junctions, S_IFREG is
423          * passed here, since the reparse data and file attributes are set
424          * later. */
425         ni = ntfs_create(dir_ni, 0, dentry->file_name,
426                          dentry->file_name_nbytes / 2, type);
427
428         if (!ni) {
429                 ERROR_WITH_ERRNO("Could not create NTFS inode for `%s'",
430                                  dentry->full_path);
431                 ret = WIMLIB_ERR_NTFS_3G;
432                 goto out_close_dir_ni;
433         }
434
435         /* Write the data streams, unless this is a directory or reparse point
436          * */
437         if (!(inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
438                                    FILE_ATTRIBUTE_DIRECTORY))) {
439                 ret = write_ntfs_data_streams(ni, dentry, &args->progress);
440                 if (ret != 0)
441                         goto out_close_dir_ni;
442         }
443
444         ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry,
445                                                       args->w,
446                                                       args->extract_flags);
447         if (ret != 0)
448                 goto out_close_dir_ni;
449
450         if (inode->i_attributes & FILE_ATTR_REPARSE_POINT) {
451                 ret = apply_reparse_data(ni, dentry, &args->progress);
452                 if (ret != 0)
453                         goto out_close_dir_ni;
454         }
455
456         /* Set DOS (short) name if given */
457         if (dentry_has_short_name(dentry)) {
458                 char *short_name_mbs;
459                 size_t short_name_mbs_nbytes;
460                 ret = utf16le_to_tstr(dentry->short_name,
461                                       dentry->short_name_nbytes,
462                                       &short_name_mbs,
463                                       &short_name_mbs_nbytes);
464                 if (ret != 0)
465                         goto out_close_dir_ni;
466
467                 DEBUG("Setting short (DOS) name of `%s' to %s",
468                       dentry->full_path, short_name_mbs);
469
470                 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_mbs,
471                                              short_name_mbs_nbytes, 0);
472                 FREE(short_name_mbs);
473                 if (ret != 0) {
474                         ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
475                                          dentry->full_path);
476                         ret = WIMLIB_ERR_NTFS_3G;
477                 }
478                 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
479                 goto out;
480         }
481 out_close_dir_ni:
482         if (dir_ni) {
483                 if (ni) {
484                         if (ntfs_inode_close_in_dir(ni, dir_ni)) {
485                                 if (ret == 0)
486                                         ret = WIMLIB_ERR_NTFS_3G;
487                                 ERROR_WITH_ERRNO("Failed to close inode for `%s'",
488                                                  dentry->full_path);
489                         }
490                 }
491                 if (ntfs_inode_close(dir_ni)) {
492                         if (ret == 0)
493                                 ret = WIMLIB_ERR_NTFS_3G;
494                         ERROR_WITH_ERRNO("Failed to close inode of directory "
495                                          "containing `%s'", dentry->full_path);
496                 }
497         }
498 out:
499         return ret;
500 }
501
502 static int
503 apply_root_dentry_ntfs(const struct wim_dentry *dentry,
504                        ntfs_volume *vol, const WIMStruct *w,
505                        int extract_flags)
506 {
507         ntfs_inode *ni;
508         int ret = 0;
509
510         ni = ntfs_pathname_to_inode(vol, NULL, "/");
511         if (!ni) {
512                 ERROR_WITH_ERRNO("Could not find root NTFS inode");
513                 return WIMLIB_ERR_NTFS_3G;
514         }
515         ret = apply_file_attributes_and_security_data(ni, ni, dentry, w,
516                                                       extract_flags);
517         if (ntfs_inode_close(ni) != 0) {
518                 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
519                                  "directory");
520                 ret = WIMLIB_ERR_NTFS_3G;
521         }
522         return ret;
523 }
524
525 /* Applies a WIM dentry to the NTFS volume */
526 int
527 apply_dentry_ntfs(struct wim_dentry *dentry, void *arg)
528 {
529         struct apply_args *args = arg;
530         ntfs_volume *vol = args->vol;
531         WIMStruct *w = args->w;
532         struct wim_dentry *orig_dentry;
533         struct wim_dentry *other;
534         int ret;
535
536         /* Treat the root dentry specially. */
537         if (dentry_is_root(dentry))
538                 return apply_root_dentry_ntfs(dentry, vol, w,
539                                               args->extract_flags);
540
541         /* NTFS filename namespaces need careful consideration.  A name for a
542          * NTFS file may be in either the POSIX, Win32, DOS, or Win32+DOS
543          * namespaces.  A NTFS file (a.k.a. inode) may have multiple names in
544          * multiple directories (i.e. hard links); however, a NTFS file can have
545          * at most 1 DOS name total.  Furthermore, a Win32 name is always
546          * associated with a DOS name (either as a Win32+DOS name, or a Win32
547          * name and a DOS name separately), which implies that a NTFS file can
548          * have at most 1 Win32 name.
549          *
550          * A WIM dentry just contains a "long name", which wimlib makes sure is
551          * non-empty, and a "short name", which may be empty.  So, wimlib must
552          * map these to the correct NTFS names.  wimlib collects all WIM
553          * dentries that map to the same NTFS inode and factors out the common
554          * information into a 'struct wim_inode', so this should make the
555          * mapping a little more obvious.  As a NTFS file can have at most 1 DOS
556          * name, a WIM inode cannot have more than 1 dentry with a non-empty
557          * short name, and this is checked in the verify_inode() function in
558          * verify.c.  Furthermore, a WIM dentry, if any, that has a DOS name
559          * must have a long name that corresponds to a Win32 name or Win32+DOS
560          * name.
561          *
562          * WIM dentries that have a long name but no associated short name are
563          * assumed to be in the POSIX namespace.
564          *
565          * So, given a WIM inode that is to map to a NTFS inode, we must apply
566          * the Win32 and DOS or Win32+DOS names, if they exist, then any
567          * additional (POSIX) names.  A caveat when actually doing this:  as
568          * confirmed by the libntfs-3g authors, ntfs_set_ntfs_dos_name() is only
569          * guaranteed to associate a DOS name with the appropriate long name if
570          * it's called when that long name is the only one in existence for that
571          * file.  So, this implies that the correct ordering of function calls
572          * to extract a NTFS file are:
573          *
574          *      if (file has a DOS name) {
575          *              - Call ntfs_create() to create long name associated with
576          *              the DOS name (this initially creates a POSIX name)
577          *              - Call ntfs_set_ntfs_dos_name() to associate a DOS name
578          *              with the long name just created.  This either changes
579          *              the POSIX name to Win32+DOS, or changes the POSIX name
580          *              to Win32 and creates a separate DOS name.
581          *      } else {
582          *              - Call ntfs_create() to create the first link to the
583          *              file in the POSIX namespace
584          *      }
585          *      - Call ntfs_link() to create the other names of the file, in the
586          *      POSIX namespace.
587          */
588 again:
589         orig_dentry = NULL;
590         if (!dentry->d_inode->i_dos_name_extracted &&
591             !dentry_has_short_name(dentry))
592         {
593                 inode_for_each_dentry(other, dentry->d_inode) {
594                         if (dentry_has_short_name(other)) {
595                                 orig_dentry = dentry;
596                                 dentry = other;
597                                 break;
598                         }
599                 }
600         }
601         dentry->d_inode->i_dos_name_extracted = 1;
602         ntfs_inode *dir_ni = dentry_open_parent_ni(dentry, vol);
603         if (dir_ni) {
604                 ret = do_apply_dentry_ntfs(dentry, dir_ni, arg);
605                 if (ret == 0 && orig_dentry != NULL) {
606                         dentry = orig_dentry;
607                         goto again;
608                 }
609         } else {
610                 ret = WIMLIB_ERR_NTFS_3G;
611         }
612         return ret;
613 }
614
615 /* Transfers the 100-nanosecond precision timestamps from a WIM dentry to a NTFS
616  * inode */
617 int
618 apply_dentry_timestamps_ntfs(struct wim_dentry *dentry, void *arg)
619 {
620         struct apply_args *args = arg;
621         ntfs_volume *vol = args->vol;
622         u8 *p;
623         u8 buf[24];
624         ntfs_inode *ni;
625         int ret;
626
627         DEBUG("Setting timestamps on `%s'", dentry->full_path);
628
629         ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path);
630         if (!ni) {
631                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
632                                  dentry->full_path);
633                 return WIMLIB_ERR_NTFS_3G;
634         }
635
636         p = buf;
637         p = put_u64(p, dentry->d_inode->i_creation_time);
638         p = put_u64(p, dentry->d_inode->i_last_write_time);
639         p = put_u64(p, dentry->d_inode->i_last_access_time);
640         ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
641         if (ret != 0) {
642                 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
643                                  dentry->full_path);
644                 ret = WIMLIB_ERR_NTFS_3G;
645         }
646
647         if (ntfs_inode_close(ni) != 0) {
648                 if (ret == 0)
649                         ret = WIMLIB_ERR_NTFS_3G;
650                 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
651                                  dentry->full_path);
652         }
653         return ret;
654 }
655
656 void
657 libntfs3g_global_init()
658 {
659         ntfs_set_char_encoding(setlocale(LC_ALL, ""));
660 }