]> wimlib.net Git - wimlib/blob - src/ntfs-apply.c
Replace rename()
[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         mbchar *p;
180         const mbchar *dir_name;
181         ntfs_inode *dir_ni;
182         mbchar 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 {
279         int ret;
280         struct SECURITY_CONTEXT ctx;
281         u32 attributes_le32;
282         const struct wim_inode *inode;
283
284         inode = dentry->d_inode;
285
286         DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
287               dentry->full_path, inode->i_attributes);
288
289         attributes_le32 = cpu_to_le32(inode->i_attributes);
290         memset(&ctx, 0, sizeof(ctx));
291         ctx.vol = ni->vol;
292         ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ATTRIB,
293                                          ni, dir_ni,
294                                          (const char*)&attributes_le32,
295                                          sizeof(u32), 0);
296         if (ret != 0) {
297                 ERROR("Failed to set NTFS file attributes on `%s'",
298                        dentry->full_path);
299                 return WIMLIB_ERR_NTFS_3G;
300         }
301         if (inode->i_security_id != -1) {
302                 const char *desc;
303                 const struct wim_security_data *sd;
304
305                 sd = wim_const_security_data(w);
306                 wimlib_assert(inode->i_security_id < sd->num_entries);
307                 desc = (const char *)sd->descriptors[inode->i_security_id];
308                 DEBUG("Applying security descriptor %d to `%s'",
309                       inode->i_security_id, dentry->full_path);
310
311                 ret = ntfs_xattr_system_setxattr(&ctx, XATTR_NTFS_ACL,
312                                                  ni, dir_ni, desc,
313                                                  sd->sizes[inode->i_security_id], 0);
314
315                 if (ret != 0) {
316                         ERROR_WITH_ERRNO("Failed to set security data on `%s'",
317                                         dentry->full_path);
318                         return WIMLIB_ERR_NTFS_3G;
319                 }
320         }
321         return 0;
322 }
323
324 /*
325  * Transfers the reparse data from a WIM inode (which must represent a reparse
326  * point) to a NTFS inode.
327  */
328 static int apply_reparse_data(ntfs_inode *ni, const struct wim_dentry *dentry,
329                               union wimlib_progress_info *progress_info)
330 {
331         struct wim_lookup_table_entry *lte;
332         int ret = 0;
333
334         lte = inode_unnamed_lte_resolved(dentry->d_inode);
335
336         DEBUG("Applying reparse data to `%s'", dentry->full_path);
337
338         if (!lte) {
339                 ERROR("Could not find reparse data for `%s'",
340                       dentry->full_path);
341                 return WIMLIB_ERR_INVALID_DENTRY;
342         }
343
344         if (wim_resource_size(lte) >= 0xffff) {
345                 ERROR("Reparse data of `%s' is too long (%"PRIu64" bytes)",
346                       dentry->full_path, wim_resource_size(lte));
347                 return WIMLIB_ERR_INVALID_DENTRY;
348         }
349
350         u8 reparse_data_buf[8 + wim_resource_size(lte)];
351         u8 *p = reparse_data_buf;
352         p = put_u32(p, dentry->d_inode->i_reparse_tag); /* ReparseTag */
353         DEBUG("ReparseTag = %#x", dentry->d_inode->i_reparse_tag);
354         p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
355         p = put_u16(p, 0); /* Reserved */
356
357         ret = read_full_wim_resource(lte, p, 0);
358         if (ret != 0)
359                 return ret;
360
361         ret = ntfs_set_ntfs_reparse_data(ni, (char*)reparse_data_buf,
362                                          wim_resource_size(lte) + 8, 0);
363         if (ret != 0) {
364                 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
365                                  dentry->full_path);
366                 return WIMLIB_ERR_NTFS_3G;
367         }
368         progress_info->extract.completed_bytes += wim_resource_size(lte);
369         return 0;
370 }
371
372 /*
373  * Applies a WIM dentry to a NTFS filesystem.
374  *
375  * @dentry:  The WIM dentry to apply
376  * @dir_ni:  The NTFS inode for the parent directory
377  *
378  * @return:  0 on success; nonzero on failure.
379  */
380 static int
381 do_apply_dentry_ntfs(struct wim_dentry *dentry, ntfs_inode *dir_ni,
382                      struct apply_args *args)
383 {
384         int ret = 0;
385         mode_t type;
386         ntfs_inode *ni = NULL;
387         struct wim_inode *inode = dentry->d_inode;
388         dentry->is_extracted = 1;
389
390         if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
391                 type = S_IFDIR;
392         } else {
393                 type = S_IFREG;
394                 if (inode->i_nlink > 1) {
395                         /* Inode has multiple dentries referencing it. */
396                         if (inode->i_extracted_file) {
397                                 /* Already extracted another dentry in the hard
398                                  * link group.  Make a hard link instead of
399                                  * extracting the file data. */
400                                 ret = apply_ntfs_hardlink(dentry, inode, dir_ni);
401                                 /* dir_ni was closed */
402                                 goto out;
403                         } else {
404                                 /* None of the dentries of this inode have been
405                                  * extracted yet, so go ahead and extract the
406                                  * first one. */
407                                 FREE(inode->i_extracted_file);
408                                 inode->i_extracted_file = STRDUP(dentry->full_path);
409                                 if (!inode->i_extracted_file) {
410                                         ret = WIMLIB_ERR_NOMEM;
411                                         goto out_close_dir_ni;
412                                 }
413                         }
414                 }
415         }
416
417         /* Create a NTFS directory or file.
418          *
419          * Note: For symbolic links that are not directory junctions, S_IFREG is
420          * passed here, since the reparse data and file attributes are set
421          * later. */
422         ni = ntfs_create(dir_ni, 0, dentry->file_name,
423                          dentry->file_name_nbytes / 2, type);
424
425         if (!ni) {
426                 ERROR_WITH_ERRNO("Could not create NTFS inode for `%s'",
427                                  dentry->full_path);
428                 ret = WIMLIB_ERR_NTFS_3G;
429                 goto out_close_dir_ni;
430         }
431
432         /* Write the data streams, unless this is a directory or reparse point
433          * */
434         if (!(inode->i_attributes & (FILE_ATTRIBUTE_REPARSE_POINT |
435                                    FILE_ATTRIBUTE_DIRECTORY))) {
436                 ret = write_ntfs_data_streams(ni, dentry, &args->progress);
437                 if (ret != 0)
438                         goto out_close_dir_ni;
439         }
440
441         ret = apply_file_attributes_and_security_data(ni, dir_ni, dentry,
442                                                       args->w);
443         if (ret != 0)
444                 goto out_close_dir_ni;
445
446         if (inode->i_attributes & FILE_ATTR_REPARSE_POINT) {
447                 ret = apply_reparse_data(ni, dentry, &args->progress);
448                 if (ret != 0)
449                         goto out_close_dir_ni;
450         }
451
452         /* Set DOS (short) name if given */
453         if (dentry_has_short_name(dentry)) {
454                 mbchar *short_name_mbs;
455                 size_t short_name_mbs_nbytes;
456                 ret = utf16le_to_mbs(dentry->short_name,
457                                      dentry->short_name_nbytes,
458                                      &short_name_mbs,
459                                      &short_name_mbs_nbytes);
460                 if (ret != 0)
461                         goto out_close_dir_ni;
462
463                 DEBUG("Setting short (DOS) name of `%s' to %s",
464                       dentry->full_path, short_name_mbs);
465
466                 ret = ntfs_set_ntfs_dos_name(ni, dir_ni, short_name_mbs,
467                                              short_name_mbs_nbytes, 0);
468                 FREE(short_name_mbs);
469                 if (ret != 0) {
470                         ERROR_WITH_ERRNO("Could not set DOS (short) name for `%s'",
471                                          dentry->full_path);
472                         ret = WIMLIB_ERR_NTFS_3G;
473                 }
474                 /* inodes have been closed by ntfs_set_ntfs_dos_name(). */
475                 goto out;
476         }
477 out_close_dir_ni:
478         if (dir_ni) {
479                 if (ni) {
480                         if (ntfs_inode_close_in_dir(ni, dir_ni)) {
481                                 if (ret == 0)
482                                         ret = WIMLIB_ERR_NTFS_3G;
483                                 ERROR_WITH_ERRNO("Failed to close inode for `%s'",
484                                                  dentry->full_path);
485                         }
486                 }
487                 if (ntfs_inode_close(dir_ni)) {
488                         if (ret == 0)
489                                 ret = WIMLIB_ERR_NTFS_3G;
490                         ERROR_WITH_ERRNO("Failed to close inode of directory "
491                                          "containing `%s'", dentry->full_path);
492                 }
493         }
494 out:
495         return ret;
496 }
497
498 static int
499 apply_root_dentry_ntfs(const struct wim_dentry *dentry,
500                        ntfs_volume *vol, const WIMStruct *w)
501 {
502         ntfs_inode *ni;
503         int ret = 0;
504
505         ni = ntfs_pathname_to_inode(vol, NULL, "/");
506         if (!ni) {
507                 ERROR_WITH_ERRNO("Could not find root NTFS inode");
508                 return WIMLIB_ERR_NTFS_3G;
509         }
510         ret = apply_file_attributes_and_security_data(ni, ni, dentry, w);
511         if (ntfs_inode_close(ni) != 0) {
512                 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
513                                  "directory");
514                 ret = WIMLIB_ERR_NTFS_3G;
515         }
516         return ret;
517 }
518
519 /* Applies a WIM dentry to the NTFS volume */
520 int
521 apply_dentry_ntfs(struct wim_dentry *dentry, void *arg)
522 {
523         struct apply_args *args = arg;
524         ntfs_volume *vol = args->vol;
525         WIMStruct *w = args->w;
526         struct wim_dentry *orig_dentry;
527         struct wim_dentry *other;
528         int ret;
529
530         /* Treat the root dentry specially. */
531         if (dentry_is_root(dentry))
532                 return apply_root_dentry_ntfs(dentry, vol, w);
533
534         /* NTFS filename namespaces need careful consideration.  A name for a
535          * NTFS file may be in either the POSIX, Win32, DOS, or Win32+DOS
536          * namespaces.  A NTFS file (a.k.a. inode) may have multiple names in
537          * multiple directories (i.e. hard links); however, a NTFS file can have
538          * at most 1 DOS name total.  Furthermore, a Win32 name is always
539          * associated with a DOS name (either as a Win32+DOS name, or a Win32
540          * name and a DOS name separately), which implies that a NTFS file can
541          * have at most 1 Win32 name.
542          *
543          * A WIM dentry just contains a "long name", which wimlib makes sure is
544          * non-empty, and a "short name", which may be empty.  So, wimlib must
545          * map these to the correct NTFS names.  wimlib collects all WIM
546          * dentries that map to the same NTFS inode and factors out the common
547          * information into a 'struct wim_inode', so this should make the
548          * mapping a little more obvious.  As a NTFS file can have at most 1 DOS
549          * name, a WIM inode cannot have more than 1 dentry with a non-empty
550          * short name, and this is checked in the verify_inode() function in
551          * verify.c.  Furthermore, a WIM dentry, if any, that has a DOS name
552          * must have a long name that corresponds to a Win32 name or Win32+DOS
553          * name.
554          *
555          * WIM dentries that have a long name but no associated short name are
556          * assumed to be in the POSIX namespace.
557          *
558          * So, given a WIM inode that is to map to a NTFS inode, we must apply
559          * the Win32 and DOS or Win32+DOS names, if they exist, then any
560          * additional (POSIX) names.  A caveat when actually doing this:  as
561          * confirmed by the libntfs-3g authors, ntfs_set_ntfs_dos_name() is only
562          * guaranteed to associate a DOS name with the appropriate long name if
563          * it's called when that long name is the only one in existence for that
564          * file.  So, this implies that the correct ordering of function calls
565          * to extract a NTFS file are:
566          *
567                 if (file has a DOS name) {
568          *              - Call ntfs_create() to create long name associated with
569          *              the DOS name (this initially creates a POSIX name)
570          *              - Call ntfs_set_ntfs_dos_name() to associate a DOS name
571          *              with the long name just created.  This either changes
572          *              the POSIX name to Win32+DOS, or changes the POSIX name
573          *              to Win32 and creates a separate DOS name.
574          *      } else {
575          *              - Call ntfs_create() to create the first link to the
576          *              file in the POSIX namespace
577          *      }
578          *      - Call ntfs_link() to create the other names of the file, in the
579          *      POSIX namespace.
580          */
581 again:
582         orig_dentry = NULL;
583         if (!dentry->d_inode->i_dos_name_extracted &&
584             !dentry_has_short_name(dentry))
585         {
586                 inode_for_each_dentry(other, dentry->d_inode) {
587                         if (dentry_has_short_name(other)) {
588                                 orig_dentry = dentry;
589                                 dentry = other;
590                                 break;
591                         }
592                 }
593         }
594         dentry->d_inode->i_dos_name_extracted = 1;
595         ntfs_inode *dir_ni = dentry_open_parent_ni(dentry, vol);
596         if (dir_ni) {
597                 ret = do_apply_dentry_ntfs(dentry, dir_ni, arg);
598                 if (ret == 0 && orig_dentry != NULL) {
599                         dentry = orig_dentry;
600                         goto again;
601                 }
602         } else {
603                 ret = WIMLIB_ERR_NTFS_3G;
604         }
605         return ret;
606 }
607
608 /* Transfers the 100-nanosecond precision timestamps from a WIM dentry to a NTFS
609  * inode */
610 int
611 apply_dentry_timestamps_ntfs(struct wim_dentry *dentry, void *arg)
612 {
613         struct apply_args *args = arg;
614         ntfs_volume *vol = args->vol;
615         u8 *p;
616         u8 buf[24];
617         ntfs_inode *ni;
618         int ret;
619
620         DEBUG("Setting timestamps on `%s'", dentry->full_path);
621
622         ni = ntfs_pathname_to_inode(vol, NULL, dentry->full_path);
623         if (!ni) {
624                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
625                                  dentry->full_path);
626                 return WIMLIB_ERR_NTFS_3G;
627         }
628
629         p = buf;
630         p = put_u64(p, dentry->d_inode->i_creation_time);
631         p = put_u64(p, dentry->d_inode->i_last_write_time);
632         p = put_u64(p, dentry->d_inode->i_last_access_time);
633         ret = ntfs_inode_set_times(ni, (const char*)buf, 3 * sizeof(u64), 0);
634         if (ret != 0) {
635                 ERROR_WITH_ERRNO("Failed to set NTFS timestamps on `%s'",
636                                  dentry->full_path);
637                 ret = WIMLIB_ERR_NTFS_3G;
638         }
639
640         if (ntfs_inode_close(ni) != 0) {
641                 if (ret == 0)
642                         ret = WIMLIB_ERR_NTFS_3G;
643                 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
644                                  dentry->full_path);
645         }
646         return ret;
647 }
648
649 void
650 libntfs3g_global_init()
651 {
652         ntfs_set_char_encoding(setlocale(LC_ALL, ""));
653 }