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