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