]> wimlib.net Git - wimlib/blob - src/ntfs-3g_apply.c
Restore comments that were accidentally deleted
[wimlib] / src / ntfs-3g_apply.c
1 /*
2  * ntfs-3g_apply.c
3  *
4  * Apply a WIM image directly to an NTFS volume using libntfs-3g.  Restore as
5  * much information as possible, including security data, file attributes, DOS
6  * names, alternate data streams, and object IDs.
7  *
8  * Note: because NTFS-3G offers inode-based interfaces, we actually don't need
9  * to deal with paths at all!  (Other than for error messages.)
10  */
11
12 /*
13  * Copyright (C) 2012-2016 Eric Biggers
14  *
15  * This file is free software; you can redistribute it and/or modify it under
16  * the terms of the GNU Lesser General Public License as published by the Free
17  * Software Foundation; either version 3 of the License, or (at your option) any
18  * later version.
19  *
20  * This file is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
23  * details.
24  *
25  * You should have received a copy of the GNU Lesser General Public License
26  * along with this file; if not, see http://www.gnu.org/licenses/.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #  include "config.h"
31 #endif
32
33 #include <errno.h>
34 #include <locale.h>
35 #include <string.h>
36
37 #include <ntfs-3g/attrib.h>
38 #include <ntfs-3g/object_id.h>
39 #include <ntfs-3g/reparse.h>
40 #include <ntfs-3g/security.h>
41
42 #include "wimlib/assert.h"
43 #include "wimlib/apply.h"
44 #include "wimlib/blob_table.h"
45 #include "wimlib/dentry.h"
46 #include "wimlib/encoding.h"
47 #include "wimlib/error.h"
48 #include "wimlib/metadata.h"
49 #include "wimlib/ntfs_3g.h"
50 #include "wimlib/object_id.h"
51 #include "wimlib/reparse.h"
52 #include "wimlib/security.h"
53 #include "wimlib/security_descriptor.h"
54
55 static int
56 ntfs_3g_get_supported_features(const char *target,
57                                struct wim_features *supported_features)
58 {
59         supported_features->readonly_files            = 1;
60         supported_features->hidden_files              = 1;
61         supported_features->system_files              = 1;
62         supported_features->archive_files             = 1;
63         supported_features->compressed_files          = 1;
64         supported_features->not_context_indexed_files = 1;
65         supported_features->named_data_streams        = 1;
66         supported_features->hard_links                = 1;
67         supported_features->reparse_points            = 1;
68         supported_features->security_descriptors      = 1;
69         supported_features->short_names               = 1;
70         supported_features->object_ids                = 1;
71         supported_features->timestamps                = 1;
72         supported_features->case_sensitive_filenames  = 1;
73         return 0;
74 }
75
76 struct ntfs_3g_apply_ctx {
77         /* Extract flags, the pointer to the WIMStruct, etc.  */
78         struct apply_ctx common;
79
80         /* Pointer to the open NTFS volume  */
81         ntfs_volume *vol;
82
83         ntfs_attr *open_attrs[MAX_OPEN_FILES];
84         unsigned num_open_attrs;
85         ntfs_inode *open_inodes[MAX_OPEN_FILES];
86         unsigned num_open_inodes;
87
88         struct reparse_buffer_disk rpbuf;
89         u8 *reparse_ptr;
90
91         /* Offset in the blob currently being read  */
92         u64 offset;
93
94         unsigned num_reparse_inodes;
95         ntfs_inode *ntfs_reparse_inodes[MAX_OPEN_FILES];
96         struct wim_inode *wim_reparse_inodes[MAX_OPEN_FILES];
97 };
98
99 static size_t
100 sid_size(const wimlib_SID *sid)
101 {
102         return offsetof(wimlib_SID, sub_authority) +
103                 sizeof(le32) * sid->sub_authority_count;
104 }
105
106 /*
107  * sd_fixup - Fix up a Windows NT security descriptor for libntfs-3g.
108  *
109  * libntfs-3g validates security descriptors before setting them, but old
110  * versions contain bugs causing it to reject unusual but valid security
111  * descriptors:
112  *
113  * - Versions before 2013.1.13 reject security descriptors ending with an empty
114  *   SACL (System Access Control List).  This bug can be worked around either by
115  *   moving the empty SACL earlier in the security descriptor or by removing the
116  *   SACL entirely.  The latter work-around is valid because an empty SACL is
117  *   equivalent to a "null", or non-existent, SACL.
118  * - Versions before 2014.2.15 reject security descriptors ending with an empty
119  *   DACL (Discretionary Access Control List).  This is very similar to the SACL
120  *   bug.  However, removing the DACL is not a valid workaround because this
121  *   changes the meaning of the security descriptor--- an empty DACL allows no
122  *   access, whereas a "null" DACL allows all access.
123  * - Versions before 2016.2.22 reject security descriptors containing SIDs with
124  *   too many subauthorities.  We do not work around this.
125  *
126  * If the security descriptor was fixed, this function returns an allocated
127  * buffer containing the fixed security descriptor, and its size is updated.
128  * Otherwise (or if no memory is available) NULL is returned.
129  */
130 static void *
131 sd_fixup(const void *_desc, size_t *size_p)
132 {
133         u32 owner_offset, group_offset, dacl_offset, sacl_offset;
134         bool owner_valid, group_valid;
135         size_t size = *size_p;
136         const wimlib_SECURITY_DESCRIPTOR_RELATIVE *desc = _desc;
137         wimlib_SECURITY_DESCRIPTOR_RELATIVE *desc_new;
138         const wimlib_SID *owner, *group, *sid;
139
140         /* Don't attempt to fix clearly invalid security descriptors.  */
141         if (size < sizeof(wimlib_SECURITY_DESCRIPTOR_RELATIVE))
142                 return NULL;
143
144         if (le16_to_cpu(desc->control) & wimlib_SE_DACL_PRESENT)
145                 dacl_offset = le32_to_cpu(desc->dacl_offset);
146         else
147                 dacl_offset = 0;
148
149         if (le16_to_cpu(desc->control) & wimlib_SE_SACL_PRESENT)
150                 sacl_offset = le32_to_cpu(desc->sacl_offset);
151         else
152                 sacl_offset = 0;
153
154         /* Check if the security descriptor will be affected by one of the bugs.
155          * If not, do nothing and return.  */
156         if (!((sacl_offset != 0 && sacl_offset == size - sizeof(wimlib_ACL)) ||
157               (dacl_offset != 0 && dacl_offset == size - sizeof(wimlib_ACL))))
158                 return NULL;
159
160         owner_offset = le32_to_cpu(desc->owner_offset);
161         group_offset = le32_to_cpu(desc->group_offset);
162         owner = (const wimlib_SID*)((const u8*)desc + owner_offset);
163         group = (const wimlib_SID*)((const u8*)desc + group_offset);
164
165         /* We'll try to move the owner or group SID to the end of the security
166          * descriptor to avoid the bug.  This is only possible if at least one
167          * is valid.  */
168         owner_valid = (owner_offset != 0) &&
169                         (owner_offset % 4 == 0) &&
170                         (owner_offset <= size - sizeof(SID)) &&
171                         (owner_offset + sid_size(owner) <= size) &&
172                         (owner_offset >= sizeof(wimlib_SECURITY_DESCRIPTOR_RELATIVE));
173         group_valid = (group_offset != 0) &&
174                         (group_offset % 4 == 0) &&
175                         (group_offset <= size - sizeof(SID)) &&
176                         (group_offset + sid_size(group) <= size) &&
177                         (group_offset >= sizeof(wimlib_SECURITY_DESCRIPTOR_RELATIVE));
178         if (owner_valid) {
179                 sid = owner;
180         } else if (group_valid) {
181                 sid = group;
182         } else {
183                 return NULL;
184         }
185
186         desc_new = MALLOC(size + sid_size(sid));
187         if (!desc_new)
188                 return NULL;
189
190         memcpy(desc_new, desc, size);
191         if (owner_valid)
192                 desc_new->owner_offset = cpu_to_le32(size);
193         else if (group_valid)
194                 desc_new->group_offset = cpu_to_le32(size);
195         memcpy((u8*)desc_new + size, sid, sid_size(sid));
196         *size_p = size + sid_size(sid);
197         return desc_new;
198 }
199
200 /* Set the security descriptor @desc of size @desc_size on the NTFS inode @ni.
201   */
202 static int
203 ntfs_3g_set_security_descriptor(ntfs_inode *ni, const void *desc, size_t desc_size)
204 {
205         struct SECURITY_CONTEXT sec_ctx;
206         void *desc_fixed = NULL;
207         int ret = 0;
208
209         memset(&sec_ctx, 0, sizeof(sec_ctx));
210         sec_ctx.vol = ni->vol;
211
212 retry:
213         if (ntfs_set_ntfs_acl(&sec_ctx, ni, desc, desc_size, 0)) {
214                 if (desc_fixed == NULL) {
215                         desc_fixed = sd_fixup(desc, &desc_size);
216                         if (desc_fixed != NULL) {
217                                 desc = desc_fixed;
218                                 goto retry;
219                         }
220                 }
221                 ret = WIMLIB_ERR_SET_SECURITY;
222         }
223
224         FREE(desc_fixed);
225         return ret;
226 }
227
228 static int
229 ntfs_3g_set_timestamps(ntfs_inode *ni, const struct wim_inode *inode)
230 {
231         u64 times[3] = {
232                 inode->i_creation_time,
233                 inode->i_last_write_time,
234                 inode->i_last_access_time,
235         };
236
237         if (ntfs_inode_set_times(ni, (const char *)times, sizeof(times), 0))
238                 return WIMLIB_ERR_SET_TIMESTAMPS;
239         return 0;
240 }
241
242 /* Restore the timestamps on the NTFS inode corresponding to @inode.  */
243 static int
244 ntfs_3g_restore_timestamps(ntfs_volume *vol, const struct wim_inode *inode)
245 {
246         ntfs_inode *ni;
247         int res;
248
249         ni = ntfs_inode_open(vol, inode->i_mft_no);
250         if (!ni)
251                 goto fail;
252
253         res = ntfs_3g_set_timestamps(ni, inode);
254
255         if (ntfs_inode_close(ni) || res)
256                 goto fail;
257
258         return 0;
259
260 fail:
261         ERROR_WITH_ERRNO("Failed to update timestamps of \"%s\" in NTFS volume",
262                          dentry_full_path(inode_first_extraction_dentry(inode)));
263         return WIMLIB_ERR_SET_TIMESTAMPS;
264 }
265
266 /* Restore the DOS name of the @dentry.
267  * This closes both @ni and @dir_ni.
268  * If either is NULL, then they are opened temporarily.  */
269 static int
270 ntfs_3g_restore_dos_name(ntfs_inode *ni, ntfs_inode *dir_ni,
271                          struct wim_dentry *dentry, ntfs_volume *vol)
272 {
273         int ret;
274         const char *dos_name;
275         size_t dos_name_nbytes;
276
277         /* Note: ntfs_set_ntfs_dos_name() closes both inodes (even if it fails).
278          * And it takes in a multibyte string, even though it translates it to
279          * UTF-16LE internally... which is annoying because we currently have
280          * the UTF-16LE string but not the multibyte string.  */
281
282         ret = utf16le_get_tstr(dentry->d_short_name, dentry->d_short_name_nbytes,
283                                &dos_name, &dos_name_nbytes);
284         if (ret)
285                 goto out_close;
286
287         if (!dir_ni)
288                 dir_ni = ntfs_inode_open(vol, dentry->d_parent->d_inode->i_mft_no);
289         if (!ni)
290                 ni = ntfs_inode_open(vol, dentry->d_inode->i_mft_no);
291         if (dir_ni && ni) {
292                 ret = ntfs_set_ntfs_dos_name(ni, dir_ni,
293                                              dos_name, dos_name_nbytes, 0);
294                 dir_ni = NULL;
295                 ni = NULL;
296         } else {
297                 ret = -1;
298         }
299         utf16le_put_tstr(dos_name);
300         if (ret) {
301                 ERROR_WITH_ERRNO("Failed to set DOS name of \"%s\" in NTFS "
302                                  "volume", dentry_full_path(dentry));
303                 ret = WIMLIB_ERR_SET_SHORT_NAME;
304                 goto out_close;
305         }
306
307         /* Unlike most other NTFS-3G functions, ntfs_set_ntfs_dos_name()
308          * changes the directory's last modification timestamp...
309          * Change it back.  */
310         return ntfs_3g_restore_timestamps(vol, dentry->d_parent->d_inode);
311
312 out_close:
313         /* ntfs_inode_close() can take a NULL argument, but it's probably best
314          * not to rely on this behavior.  */
315         if (ni)
316                 ntfs_inode_close(ni);
317         if (dir_ni)
318                 ntfs_inode_close(dir_ni);
319         return ret;
320 }
321
322 static int
323 ntfs_3g_restore_reparse_point(ntfs_inode *ni, const struct wim_inode *inode,
324                               unsigned blob_size, struct ntfs_3g_apply_ctx *ctx)
325 {
326         complete_reparse_point(&ctx->rpbuf, inode, blob_size);
327
328         if (ntfs_set_ntfs_reparse_data(ni, (const char *)&ctx->rpbuf,
329                                        REPARSE_DATA_OFFSET + blob_size, 0))
330         {
331                 int err = errno;
332                 ERROR_WITH_ERRNO("Failed to set reparse data on \"%s\"",
333                                  dentry_full_path(
334                                         inode_first_extraction_dentry(inode)));
335                 if (err == EINVAL && !(inode->i_reparse_tag & 0x80000000)) {
336                         WARNING("This reparse point had a non-Microsoft reparse "
337                                 "tag.  The preceding error may have been caused "
338                                 "by a known bug in libntfs-3g where it does not "
339                                 "correctly validate non-Microsoft reparse "
340                                 "points.  This bug was fixed in NTFS-3G version "
341                                 "2016.2.22.");
342                 }
343                 return WIMLIB_ERR_SET_REPARSE_DATA;
344         }
345
346         return 0;
347 }
348
349 static bool
350 ntfs_3g_has_empty_attributes(const struct wim_inode *inode)
351 {
352         for (unsigned i = 0; i < inode->i_num_streams; i++) {
353                 const struct wim_inode_stream *strm = &inode->i_streams[i];
354
355                 if (stream_blob_resolved(strm) == NULL &&
356                     (strm->stream_type == STREAM_TYPE_REPARSE_POINT ||
357                      stream_is_named_data_stream(strm)))
358                         return true;
359         }
360         return false;
361 }
362
363 /*
364  * Create empty attributes (named data streams and potentially a reparse point)
365  * for the specified file, if there are any.
366  *
367  * Since these won't have blob descriptors, they won't show up in the call to
368  * extract_blob_list().  Hence the need for the special case.
369  *
370  * Keep this in sync with ntfs_3g_has_empty_attributes()!
371  */
372 static int
373 ntfs_3g_create_empty_attributes(ntfs_inode *ni,
374                                 const struct wim_inode *inode,
375                                 struct ntfs_3g_apply_ctx *ctx)
376 {
377         for (unsigned i = 0; i < inode->i_num_streams; i++) {
378
379                 const struct wim_inode_stream *strm = &inode->i_streams[i];
380                 int ret;
381
382                 if (stream_blob_resolved(strm) != NULL)
383                         continue;
384
385                 if (strm->stream_type == STREAM_TYPE_REPARSE_POINT) {
386                         ret = ntfs_3g_restore_reparse_point(ni, inode, 0, ctx);
387                         if (ret)
388                                 return ret;
389                 } else if (stream_is_named_data_stream(strm)) {
390                         if (ntfs_attr_add(ni, AT_DATA, strm->stream_name,
391                                           utf16le_len_chars(strm->stream_name),
392                                           NULL, 0))
393                         {
394                                 ERROR_WITH_ERRNO("Failed to create named data "
395                                                  "stream of \"%s\"",
396                                                  dentry_full_path(
397                                         inode_first_extraction_dentry(inode)));
398                                 return WIMLIB_ERR_NTFS_3G;
399                         }
400                 }
401         }
402         return 0;
403 }
404
405 /* Set attributes, security descriptor, and timestamps on the NTFS inode @ni.
406  */
407 static int
408 ntfs_3g_set_metadata(ntfs_inode *ni, const struct wim_inode *inode,
409                      const struct ntfs_3g_apply_ctx *ctx)
410 {
411         int extract_flags;
412         const struct wim_security_data *sd;
413         struct wim_dentry *one_dentry;
414         int ret;
415
416         extract_flags = ctx->common.extract_flags;
417         sd = wim_get_current_security_data(ctx->common.wim);
418         one_dentry = inode_first_extraction_dentry(inode);
419
420         /* Object ID */
421         {
422                 u32 len;
423                 const void *object_id = inode_get_object_id(inode, &len);
424                 if (unlikely(object_id != NULL) &&
425                     ntfs_set_ntfs_object_id(ni, object_id, len, 0))
426                 {
427                         if (errno == EEXIST) {
428                                 WARNING("Duplicate object ID on file \"%s\"",
429                                         dentry_full_path(one_dentry));
430                         } else {
431                                 ERROR_WITH_ERRNO("Failed to set object ID on "
432                                                  "\"%s\" in NTFS volume",
433                                                  dentry_full_path(one_dentry));
434                                 return WIMLIB_ERR_NTFS_3G;
435                         }
436                 }
437         }
438
439         /* Attributes  */
440         if (!(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ATTRIBUTES)) {
441                 u32 attrib = inode->i_attributes;
442
443                 attrib &= ~(FILE_ATTRIBUTE_SPARSE_FILE |
444                             FILE_ATTRIBUTE_ENCRYPTED);
445
446                 if (ntfs_set_ntfs_attrib(ni, (const char *)&attrib,
447                                          sizeof(attrib), 0))
448                 {
449                         ERROR_WITH_ERRNO("Failed to set attributes on \"%s\" "
450                                          "in NTFS volume",
451                                          dentry_full_path(one_dentry));
452                         return WIMLIB_ERR_SET_ATTRIBUTES;
453                 }
454         }
455
456         /* Security descriptor  */
457         if (inode_has_security_descriptor(inode)
458             && !(extract_flags & WIMLIB_EXTRACT_FLAG_NO_ACLS))
459         {
460                 const void *desc;
461                 size_t desc_size;
462
463                 desc = sd->descriptors[inode->i_security_id];
464                 desc_size = sd->sizes[inode->i_security_id];
465
466                 ret = ntfs_3g_set_security_descriptor(ni, desc, desc_size);
467
468                 if (unlikely(ret)) {
469                         int err = errno;
470                         ERROR_WITH_ERRNO("Failed to set security descriptor on "
471                                          "\"%s\" in NTFS volume",
472                                          dentry_full_path(one_dentry));
473                         if (err == EINVAL && wimlib_print_errors) {
474                                 fprintf(wimlib_error_file,
475                                         "The security descriptor is: ");
476                                 print_byte_field(desc, desc_size, wimlib_error_file);
477                                 fprintf(wimlib_error_file, "\n");
478                                 fprintf(wimlib_error_file,
479                                         "\nThis error occurred because libntfs-3g thinks "
480                                         "the security descriptor is invalid.  If you "
481                                         "are extracting a Windows 10 image, this may be "
482                                         "caused by a known bug in libntfs-3g.  This bug "
483                                         "was fixed in NTFS-3G version 2016.2.22.  See: "
484                                         "https://wimlib.net/forums/viewtopic.php?f=1&t=4 "
485                                         "for more information.\n\n");
486                         }
487                         return ret;
488                 }
489         }
490
491         /* Timestamps  */
492         ret = ntfs_3g_set_timestamps(ni, inode);
493         if (ret) {
494                 ERROR_WITH_ERRNO("Failed to set timestamps on \"%s\" "
495                                  "in NTFS volume",
496                                  dentry_full_path(one_dentry));
497                 return ret;
498         }
499         return 0;
500 }
501
502 /* Recursively creates all the subdirectories of @dir, which has been created as
503  * the NTFS inode @dir_ni.  */
504 static int
505 ntfs_3g_create_dirs_recursive(ntfs_inode *dir_ni, struct wim_dentry *dir,
506                               struct ntfs_3g_apply_ctx *ctx)
507 {
508         struct wim_dentry *child;
509
510         for_dentry_child(child, dir) {
511                 ntfs_inode *ni;
512                 int ret;
513
514                 if (!(child->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
515                         continue;
516                 if (!will_extract_dentry(child))
517                         continue;
518
519                 ni = ntfs_create(dir_ni, 0, child->d_extraction_name,
520                                  child->d_extraction_name_nchars, S_IFDIR);
521                 if (!ni) {
522                         ERROR_WITH_ERRNO("Error creating \"%s\" in NTFS volume",
523                                          dentry_full_path(child));
524                         return WIMLIB_ERR_NTFS_3G;
525                 }
526
527                 child->d_inode->i_mft_no = ni->mft_no;
528
529                 ret = report_file_created(&ctx->common);
530                 if (!ret)
531                         ret = ntfs_3g_set_metadata(ni, child->d_inode, ctx);
532                 if (!ret)
533                         ret = ntfs_3g_create_dirs_recursive(ni, child, ctx);
534
535                 if (ntfs_inode_close_in_dir(ni, dir_ni) && !ret) {
536                         ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
537                                          dentry_full_path(child));
538                         ret = WIMLIB_ERR_NTFS_3G;
539                 }
540                 if (ret)
541                         return ret;
542         }
543         return 0;
544 }
545
546 /* For each WIM dentry in the @root tree that represents a directory, create the
547  * corresponding directory in the NTFS volume @ctx->vol.  */
548 static int
549 ntfs_3g_create_directories(struct wim_dentry *root,
550                            struct list_head *dentry_list,
551                            struct ntfs_3g_apply_ctx *ctx)
552 {
553         ntfs_inode *root_ni;
554         int ret;
555         struct wim_dentry *dentry;
556
557         /* Create the directories using POSIX names.  */
558
559         root_ni = ntfs_inode_open(ctx->vol, FILE_root);
560         if (!root_ni) {
561                 ERROR_WITH_ERRNO("Can't open root of NTFS volume");
562                 return WIMLIB_ERR_NTFS_3G;
563         }
564
565         root->d_inode->i_mft_no = FILE_root;
566
567         ret = ntfs_3g_set_metadata(root_ni, root->d_inode, ctx);
568         if (!ret)
569                 ret = ntfs_3g_create_dirs_recursive(root_ni, root, ctx);
570
571         if (ntfs_inode_close(root_ni) && !ret) {
572                 ERROR_WITH_ERRNO("Error closing root of NTFS volume");
573                 ret = WIMLIB_ERR_NTFS_3G;
574         }
575         if (ret)
576                 return ret;
577
578         /* Set the DOS name of any directory that has one.  In addition, create
579          * empty attributes for directories that have them.  Note that creating
580          * an empty reparse point attribute must happen *after* setting the DOS
581          * name in order to work around a case where ntfs_set_ntfs_dos_name()
582          * fails with EOPNOTSUPP.  This bug was fixed in NTFS-3G version
583          * 2016.2.22.  */
584         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
585                 const struct wim_inode *inode = dentry->d_inode;
586
587                 if (!(inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
588                         continue;
589                 if (dentry_has_short_name(dentry)) {
590                         ret = ntfs_3g_restore_dos_name(NULL, NULL, dentry,
591                                                        ctx->vol);
592                         if (ret)
593                                 return ret;
594                         ret = report_file_created(&ctx->common);
595                         if (ret)
596                                 return ret;
597                 }
598                 if (ntfs_3g_has_empty_attributes(inode)) {
599                         ntfs_inode *ni;
600
601                         ret = WIMLIB_ERR_NTFS_3G;
602                         ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
603                         if (ni) {
604                                 ret = ntfs_3g_create_empty_attributes(ni, inode,
605                                                                       ctx);
606                                 if (ntfs_inode_close(ni) && !ret)
607                                         ret = WIMLIB_ERR_NTFS_3G;
608                         }
609                         if (ret) {
610                                 ERROR_WITH_ERRNO("Failed to create empty "
611                                                  "attributes of directory "
612                                                  "\"%s\" in NTFS volume",
613                                                  dentry_full_path(dentry));
614                                 return ret;
615                         }
616                 }
617         }
618         return 0;
619 }
620
621 /* When creating an inode that will have a short (DOS) name, we create it using
622  * the long name associated with the short name.  This ensures that the short
623  * name gets associated with the correct long name.  */
624 static struct wim_dentry *
625 ntfs_3g_first_extraction_alias(struct wim_inode *inode)
626 {
627         struct wim_dentry *dentry;
628
629         inode_for_each_extraction_alias(dentry, inode)
630                 if (dentry_has_short_name(dentry))
631                         return dentry;
632         return inode_first_extraction_dentry(inode);
633 }
634
635 /*
636  * Add a hard link for the NTFS inode @ni at the location corresponding to the
637  * WIM dentry @dentry.
638  *
639  * The parent directory must have already been created on the NTFS volume.
640  *
641  * Returns 0 on success; returns WIMLIB_ERR_NTFS_3G and sets errno on failure.
642  */
643 static int
644 ntfs_3g_add_link(ntfs_inode *ni, struct wim_dentry *dentry)
645 {
646         ntfs_inode *dir_ni;
647         int res;
648
649         /* Open the inode of the parent directory.  */
650         dir_ni = ntfs_inode_open(ni->vol, dentry->d_parent->d_inode->i_mft_no);
651         if (!dir_ni)
652                 goto fail;
653
654         /* Create the link.  */
655         res = ntfs_link(ni, dir_ni, dentry->d_extraction_name,
656                         dentry->d_extraction_name_nchars);
657
658         /* Close the parent directory.  */
659         if (ntfs_inode_close(dir_ni) || res)
660                 goto fail;
661
662         return 0;
663
664 fail:
665         ERROR_WITH_ERRNO("Can't create link \"%s\" in NTFS volume",
666                          dentry_full_path(dentry));
667         return WIMLIB_ERR_NTFS_3G;
668 }
669
670 static int
671 ntfs_3g_create_nondirectory(struct wim_inode *inode,
672                             struct ntfs_3g_apply_ctx *ctx)
673 {
674         struct wim_dentry *first_dentry;
675         ntfs_inode *dir_ni;
676         ntfs_inode *ni;
677         struct wim_dentry *dentry;
678         int ret;
679
680         first_dentry = ntfs_3g_first_extraction_alias(inode);
681
682         /* Create first link.  */
683
684         dir_ni = ntfs_inode_open(ctx->vol, first_dentry->d_parent->d_inode->i_mft_no);
685         if (!dir_ni) {
686                 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
687                                  dentry_full_path(first_dentry->d_parent));
688                 return WIMLIB_ERR_NTFS_3G;
689         }
690
691         ni = ntfs_create(dir_ni, 0, first_dentry->d_extraction_name,
692                          first_dentry->d_extraction_name_nchars, S_IFREG);
693
694         if (!ni) {
695                 ERROR_WITH_ERRNO("Can't create \"%s\" in NTFS volume",
696                                  dentry_full_path(first_dentry));
697                 ntfs_inode_close(dir_ni);
698                 return WIMLIB_ERR_NTFS_3G;
699         }
700
701         inode->i_mft_no = ni->mft_no;
702
703         /* Set short name if present.  */
704         if (dentry_has_short_name(first_dentry)) {
705
706                 ret = ntfs_3g_restore_dos_name(ni, dir_ni, first_dentry, ctx->vol);
707
708                 /* ntfs_3g_restore_dos_name() closed both 'ni' and 'dir_ni'.  */
709
710                 if (ret)
711                         return ret;
712
713                 /* Reopen the inode.  */
714                 ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
715                 if (!ni) {
716                         ERROR_WITH_ERRNO("Failed to reopen \"%s\" "
717                                          "in NTFS volume",
718                                          dentry_full_path(first_dentry));
719                         return WIMLIB_ERR_NTFS_3G;
720                 }
721         } else {
722                 /* Close the directory in which the first link was created.  */
723                 if (ntfs_inode_close(dir_ni)) {
724                         ERROR_WITH_ERRNO("Failed to close \"%s\" in NTFS volume",
725                                          dentry_full_path(first_dentry->d_parent));
726                         ret = WIMLIB_ERR_NTFS_3G;
727                         goto out_close_ni;
728                 }
729         }
730
731         /* Create additional links if present.  */
732         inode_for_each_extraction_alias(dentry, inode) {
733                 if (dentry != first_dentry) {
734                         ret = ntfs_3g_add_link(ni, dentry);
735                         if (ret)
736                                 goto out_close_ni;
737                 }
738         }
739
740         /* Set metadata.  */
741         ret = ntfs_3g_set_metadata(ni, inode, ctx);
742         if (ret)
743                 goto out_close_ni;
744
745         ret = ntfs_3g_create_empty_attributes(ni, inode, ctx);
746
747 out_close_ni:
748         /* Close the inode.  */
749         if (ntfs_inode_close(ni) && !ret) {
750                 ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
751                                  dentry_full_path(first_dentry));
752                 ret = WIMLIB_ERR_NTFS_3G;
753         }
754         return ret;
755 }
756
757 /* For each WIM dentry in the @dentry_list that represents a nondirectory file,
758  * create the corresponding nondirectory file in the NTFS volume.
759  *
760  * Directories must have already been created.  */
761 static int
762 ntfs_3g_create_nondirectories(struct list_head *dentry_list,
763                               struct ntfs_3g_apply_ctx *ctx)
764 {
765         struct wim_dentry *dentry;
766         struct wim_inode *inode;
767         int ret;
768
769         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
770                 inode = dentry->d_inode;
771                 if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
772                         continue;
773                 if (dentry == inode_first_extraction_dentry(inode)) {
774                         ret = ntfs_3g_create_nondirectory(inode, ctx);
775                         if (ret)
776                                 return ret;
777                 }
778                 ret = report_file_created(&ctx->common);
779                 if (ret)
780                         return ret;
781         }
782         return 0;
783 }
784
785 static int
786 ntfs_3g_begin_extract_blob_instance(struct blob_descriptor *blob,
787                                     ntfs_inode *ni,
788                                     struct wim_inode *inode,
789                                     const struct wim_inode_stream *strm,
790                                     struct ntfs_3g_apply_ctx *ctx)
791 {
792         struct wim_dentry *one_dentry = inode_first_extraction_dentry(inode);
793         ntfschar *stream_name;
794         size_t stream_name_nchars;
795         ntfs_attr *attr;
796
797         if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
798
799                 if (blob->size > REPARSE_DATA_MAX_SIZE) {
800                         ERROR("Reparse data of \"%s\" has size "
801                               "%"PRIu64" bytes (exceeds %u bytes)",
802                               dentry_full_path(one_dentry),
803                               blob->size, REPARSE_DATA_MAX_SIZE);
804                         return WIMLIB_ERR_INVALID_REPARSE_DATA;
805                 }
806                 ctx->reparse_ptr = ctx->rpbuf.rpdata;
807                 ctx->ntfs_reparse_inodes[ctx->num_reparse_inodes] = ni;
808                 ctx->wim_reparse_inodes[ctx->num_reparse_inodes] = inode;
809                 ctx->num_reparse_inodes++;
810                 return 0;
811         }
812
813         /* It's a data stream (may be unnamed or named).  */
814         wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
815
816         if (unlikely(stream_is_named(strm))) {
817                 stream_name = strm->stream_name;
818                 stream_name_nchars = utf16le_len_chars(stream_name);
819
820                 if (ntfs_attr_add(ni, AT_DATA, stream_name,
821                                   stream_name_nchars, NULL, 0))
822                 {
823                         ERROR_WITH_ERRNO("Failed to create named data stream of \"%s\"",
824                                          dentry_full_path(one_dentry));
825                         return WIMLIB_ERR_NTFS_3G;
826                 }
827         } else {
828                 /* Don't pass an empty string other than AT_UNNAMED to
829                  * ntfs_attr_open() --- it violates assumptions made by
830                  * libntfs-3g.  */
831                 stream_name = AT_UNNAMED;
832                 stream_name_nchars = 0;
833         }
834
835         /* This should be ensured by extract_blob_list()  */
836         wimlib_assert(ctx->num_open_attrs < MAX_OPEN_FILES);
837
838         attr = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_nchars);
839         if (!attr) {
840                 ERROR_WITH_ERRNO("Failed to open data stream of \"%s\"",
841                                  dentry_full_path(one_dentry));
842                 return WIMLIB_ERR_NTFS_3G;
843         }
844         ctx->open_attrs[ctx->num_open_attrs++] = attr;
845         ntfs_attr_truncate_solid(attr, blob->size);
846         return 0;
847 }
848
849 static int
850 ntfs_3g_cleanup_blob_extract(struct ntfs_3g_apply_ctx *ctx)
851 {
852         int ret = 0;
853
854         for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
855                 if (ntfs_attr_pclose(ctx->open_attrs[i]))
856                         ret = -1;
857                 ntfs_attr_close(ctx->open_attrs[i]);
858         }
859
860         ctx->num_open_attrs = 0;
861
862         for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
863                 if (ntfs_inode_close(ctx->open_inodes[i]))
864                         ret = -1;
865         }
866         ctx->num_open_inodes = 0;
867
868         ctx->offset = 0;
869         ctx->reparse_ptr = NULL;
870         ctx->num_reparse_inodes = 0;
871         return ret;
872 }
873
874 static ntfs_inode *
875 ntfs_3g_open_inode(struct wim_inode *inode, struct ntfs_3g_apply_ctx *ctx)
876 {
877         ntfs_inode *ni;
878
879         /* If the same blob is being extracted to multiple streams of the same
880          * inode, then we must only open the inode once.  */
881         if (unlikely(inode->i_num_streams > 1)) {
882                 for (unsigned i = 0; i < ctx->num_open_inodes; i++) {
883                         if (ctx->open_inodes[i]->mft_no == inode->i_mft_no) {
884                                 return ctx->open_inodes[i];
885                         }
886                 }
887         }
888
889         ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
890         if (unlikely(!ni)) {
891                 ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
892                                  dentry_full_path(
893                                         inode_first_extraction_dentry(inode)));
894                 return NULL;
895         }
896
897         ctx->open_inodes[ctx->num_open_inodes++] = ni;
898         return ni;
899 }
900
901 static int
902 ntfs_3g_begin_extract_blob(struct blob_descriptor *blob, void *_ctx)
903 {
904         struct ntfs_3g_apply_ctx *ctx = _ctx;
905         const struct blob_extraction_target *targets = blob_extraction_targets(blob);
906         int ret;
907         ntfs_inode *ni;
908
909         for (u32 i = 0; i < blob->out_refcnt; i++) {
910                 ret = WIMLIB_ERR_NTFS_3G;
911                 ni = ntfs_3g_open_inode(targets[i].inode, ctx);
912                 if (!ni)
913                         goto out_cleanup;
914
915                 ret = ntfs_3g_begin_extract_blob_instance(blob, ni,
916                                                           targets[i].inode,
917                                                           targets[i].stream, ctx);
918                 if (ret)
919                         goto out_cleanup;
920         }
921         ret = 0;
922         goto out;
923
924 out_cleanup:
925         ntfs_3g_cleanup_blob_extract(ctx);
926 out:
927         return ret;
928 }
929
930 /* Note: contrary to its documentation, ntfs_attr_pwrite() can return a short
931  * count in non-error cases --- specifically, when writing to a compressed
932  * attribute and the requested count exceeds the size of an NTFS "compression
933  * block".  Therefore, we must continue calling ntfs_attr_pwrite() until all
934  * bytes have been written or a real error has occurred.  */
935 static bool
936 ntfs_3g_full_pwrite(ntfs_attr *na, u64 offset, size_t size, const u8 *data)
937 {
938         while (size) {
939                 s64 res = ntfs_attr_pwrite(na, offset, size, data);
940                 if (unlikely(res <= 0))
941                         return false;
942                 wimlib_assert(res <= size);
943                 offset += res;
944                 size -= res;
945                 data += res;
946         }
947         return true;
948 }
949
950 static int
951 ntfs_3g_extract_chunk(const void *chunk, size_t size, void *_ctx)
952 {
953         struct ntfs_3g_apply_ctx *ctx = _ctx;
954
955         for (unsigned i = 0; i < ctx->num_open_attrs; i++) {
956                 if (!ntfs_3g_full_pwrite(ctx->open_attrs[i],
957                                          ctx->offset, size, chunk))
958                 {
959                         ERROR_WITH_ERRNO("Error writing data to NTFS volume");
960                         return WIMLIB_ERR_NTFS_3G;
961                 }
962         }
963         if (ctx->reparse_ptr)
964                 ctx->reparse_ptr = mempcpy(ctx->reparse_ptr, chunk, size);
965         ctx->offset += size;
966         return 0;
967 }
968
969 static int
970 ntfs_3g_end_extract_blob(struct blob_descriptor *blob, int status, void *_ctx)
971 {
972         struct ntfs_3g_apply_ctx *ctx = _ctx;
973         int ret;
974
975         if (status) {
976                 ret = status;
977                 goto out;
978         }
979
980         for (u32 i = 0; i < ctx->num_reparse_inodes; i++) {
981                 ret = ntfs_3g_restore_reparse_point(ctx->ntfs_reparse_inodes[i],
982                                                     ctx->wim_reparse_inodes[i],
983                                                     blob->size, ctx);
984                 if (ret)
985                         goto out;
986         }
987         ret = 0;
988 out:
989         if (ntfs_3g_cleanup_blob_extract(ctx) && !ret) {
990                 ERROR_WITH_ERRNO("Error writing data to NTFS volume");
991                 ret = WIMLIB_ERR_NTFS_3G;
992         }
993         return ret;
994 }
995
996 static u64
997 ntfs_3g_count_dentries(const struct list_head *dentry_list)
998 {
999         const struct wim_dentry *dentry;
1000         u64 count = 0;
1001
1002         list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
1003                 count++;
1004                 if ((dentry->d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) &&
1005                     dentry_has_short_name(dentry))
1006                 {
1007                         count++;
1008                 }
1009         }
1010
1011         return count;
1012 }
1013
1014 static int
1015 ntfs_3g_extract(struct list_head *dentry_list, struct apply_ctx *_ctx)
1016 {
1017         struct ntfs_3g_apply_ctx *ctx = (struct ntfs_3g_apply_ctx *)_ctx;
1018         ntfs_volume *vol;
1019         struct wim_dentry *root;
1020         int ret;
1021
1022         /* For NTFS-3G extraction mode we require that the dentries to extract
1023          * form a single tree.  */
1024         root = list_first_entry(dentry_list, struct wim_dentry,
1025                                 d_extraction_list_node);
1026
1027         /* Mount the NTFS volume.  */
1028         vol = ntfs_mount(ctx->common.target, 0);
1029         if (!vol) {
1030                 ERROR_WITH_ERRNO("Failed to mount \"%s\" with NTFS-3G",
1031                                  ctx->common.target);
1032                 return WIMLIB_ERR_NTFS_3G;
1033         }
1034         ctx->vol = vol;
1035
1036         /* Create all inodes and aliases, including short names, and set
1037          * metadata (attributes, security descriptors, and timestamps).  */
1038
1039         ret = start_file_structure_phase(&ctx->common,
1040                                          ntfs_3g_count_dentries(dentry_list));
1041         if (ret)
1042                 goto out_unmount;
1043
1044         ret = ntfs_3g_create_directories(root, dentry_list, ctx);
1045         if (ret)
1046                 goto out_unmount;
1047
1048         ret = ntfs_3g_create_nondirectories(dentry_list, ctx);
1049         if (ret)
1050                 goto out_unmount;
1051
1052         ret = end_file_structure_phase(&ctx->common);
1053         if (ret)
1054                 goto out_unmount;
1055
1056         /* Extract blobs.  */
1057         struct read_blob_callbacks cbs = {
1058                 .begin_blob     = ntfs_3g_begin_extract_blob,
1059                 .consume_chunk  = ntfs_3g_extract_chunk,
1060                 .end_blob       = ntfs_3g_end_extract_blob,
1061                 .ctx            = ctx,
1062         };
1063         ret = extract_blob_list(&ctx->common, &cbs);
1064
1065         /* We do not need a final pass to set timestamps because libntfs-3g does
1066          * not update timestamps automatically (exception:
1067          * ntfs_set_ntfs_dos_name() does, but we handle this elsewhere).  */
1068
1069 out_unmount:
1070         if (ntfs_umount(ctx->vol, FALSE) && !ret) {
1071                 ERROR_WITH_ERRNO("Failed to unmount \"%s\" with NTFS-3G",
1072                                  ctx->common.target);
1073                 ret = WIMLIB_ERR_NTFS_3G;
1074         }
1075         return ret;
1076 }
1077
1078 const struct apply_operations ntfs_3g_apply_ops = {
1079         .name                   = "NTFS-3G",
1080         .get_supported_features = ntfs_3g_get_supported_features,
1081         .extract                = ntfs_3g_extract,
1082         .context_size           = sizeof(struct ntfs_3g_apply_ctx),
1083         .single_tree_only       = true,
1084 };
1085
1086 void
1087 libntfs3g_global_init(void)
1088 {
1089        ntfs_set_char_encoding(setlocale(LC_ALL, ""));
1090 }