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