+/* When creating an inode that will have a short (DOS) name, we create it using
+ * the long name associated with the short name. This ensures that the short
+ * name gets associated with the correct long name. */
+static struct wim_dentry *
+ntfs_3g_first_extraction_alias(struct wim_inode *inode)
+{
+ struct list_head *next = inode->i_extraction_aliases.next;
+ struct wim_dentry *dentry;
+
+ do {
+ dentry = list_entry(next, struct wim_dentry,
+ d_extraction_alias_node);
+ if (dentry_has_short_name(dentry))
+ break;
+ next = next->next;
+ } while (next != &inode->i_extraction_aliases);
+ return dentry;
+}
+
+/*
+ * Add a hard link for the NTFS inode @ni at the location corresponding to the
+ * WIM dentry @dentry.
+ *
+ * The parent directory must have already been created on the NTFS volume.
+ *
+ * Returns 0 on success; returns WIMLIB_ERR_NTFS_3G and sets errno on failure.
+ */
+static int
+ntfs_3g_add_link(ntfs_inode *ni, struct wim_dentry *dentry)
+{
+ ntfs_inode *dir_ni;
+ int res;
+
+ /* Open the inode of the parent directory. */
+ dir_ni = ntfs_inode_open(ni->vol, dentry->d_parent->d_inode->i_mft_no);
+ if (!dir_ni)
+ goto fail;
+
+ /* Create the link. */
+ res = ntfs_link(ni, dir_ni, dentry->d_extraction_name,
+ dentry->d_extraction_name_nchars);
+
+ /* Close the parent directory. */
+ if (ntfs_inode_close(dir_ni) || res)
+ goto fail;
+
+ return 0;
+
+fail:
+ ERROR_WITH_ERRNO("Can't create link \"%s\" in NTFS volume",
+ dentry_full_path(dentry));
+ return WIMLIB_ERR_NTFS_3G;
+}
+
+static int
+ntfs_3g_create_nondirectory(struct wim_inode *inode,
+ const struct ntfs_3g_apply_ctx *ctx)
+{
+ struct wim_dentry *first_dentry;
+ ntfs_inode *dir_ni;
+ ntfs_inode *ni;
+ struct list_head *next;
+ struct wim_dentry *dentry;
+ int ret;
+
+ first_dentry = ntfs_3g_first_extraction_alias(inode);
+
+ /* Create first link. */
+
+ dir_ni = ntfs_inode_open(ctx->vol, first_dentry->d_parent->d_inode->i_mft_no);
+ if (!dir_ni) {
+ ERROR_WITH_ERRNO("Can't open \"%s\" in NTFS volume",
+ dentry_full_path(first_dentry->d_parent));
+ return WIMLIB_ERR_NTFS_3G;
+ }
+
+ ni = ntfs_create(dir_ni, 0, first_dentry->d_extraction_name,
+ first_dentry->d_extraction_name_nchars, S_IFREG);
+
+ if (!ni) {
+ ERROR_WITH_ERRNO("Can't create \"%s\" in NTFS volume",
+ dentry_full_path(first_dentry));
+ ntfs_inode_close(dir_ni);
+ return WIMLIB_ERR_NTFS_3G;
+ }
+
+ inode->i_mft_no = ni->mft_no;
+
+ /* Set short name if present. */
+ if (dentry_has_short_name(first_dentry)) {
+
+ ret = ntfs_3g_restore_dos_name(ni, dir_ni, first_dentry, ctx->vol);
+
+ /* ntfs_3g_restore_dos_name() closed both 'ni' and 'dir_ni'. */
+
+ if (ret)
+ return ret;
+
+ /* Reopen the inode. */
+ ni = ntfs_inode_open(ctx->vol, inode->i_mft_no);
+ if (!ni) {
+ ERROR_WITH_ERRNO("Failed to reopen \"%s\" "
+ "in NTFS volume",
+ dentry_full_path(first_dentry));
+ return WIMLIB_ERR_NTFS_3G;
+ }
+ } else {
+ /* Close the directory in which the first link was created. */
+ if (ntfs_inode_close(dir_ni)) {
+ ERROR_WITH_ERRNO("Failed to close \"%s\" in NTFS volume",
+ dentry_full_path(first_dentry->d_parent));
+ ret = WIMLIB_ERR_NTFS_3G;
+ goto out_close_ni;
+ }
+ }
+
+ /* Create additional links if present. */
+ next = inode->i_extraction_aliases.next;
+ do {
+ dentry = list_entry(next, struct wim_dentry,
+ d_extraction_alias_node);
+ if (dentry != first_dentry) {
+ ret = ntfs_3g_add_link(ni, dentry);
+ if (ret)
+ goto out_close_ni;
+ }
+ next = next->next;
+ } while (next != &inode->i_extraction_aliases);
+
+ /* Set metadata. */
+ ret = ntfs_3g_set_metadata(ni, inode, ctx);
+ if (ret)
+ goto out_close_ni;
+
+ ret = ntfs_3g_create_empty_named_data_streams(ni, inode, ctx);
+
+out_close_ni:
+ /* Close the inode. */
+ if (ntfs_inode_close(ni) && !ret) {
+ ERROR_WITH_ERRNO("Error closing \"%s\" in NTFS volume",
+ dentry_full_path(first_dentry));
+ ret = WIMLIB_ERR_NTFS_3G;
+ }
+ return ret;
+}
+
+/* For each WIM dentry in the @dentry_list that represents a nondirectory file,
+ * create the corresponding nondirectory file in the NTFS volume.
+ *
+ * Directories must have already been created. */
+static int
+ntfs_3g_create_nondirectories(struct list_head *dentry_list,
+ struct ntfs_3g_apply_ctx *ctx)
+{
+ struct wim_dentry *dentry;
+ struct wim_inode *inode;
+ int ret;
+
+ list_for_each_entry(dentry, dentry_list, d_extraction_list_node) {
+ inode = dentry->d_inode;
+ if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)
+ continue;
+ if (dentry == inode_first_extraction_dentry(inode)) {
+ ret = ntfs_3g_create_nondirectory(inode, ctx);
+ if (ret)
+ return ret;
+ }
+ ret = report_file_created(&ctx->common);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
+static int
+ntfs_3g_begin_extract_blob_instance(struct blob_descriptor *blob,
+ ntfs_inode *ni,
+ struct wim_inode *inode,
+ const struct wim_inode_stream *strm,
+ struct ntfs_3g_apply_ctx *ctx)
+{
+ struct wim_dentry *one_dentry = inode_first_extraction_dentry(inode);
+ size_t stream_name_nchars;
+ ntfs_attr *attr;
+
+ if (unlikely(strm->stream_type == STREAM_TYPE_REPARSE_POINT)) {
+
+ if (blob->size > REPARSE_DATA_MAX_SIZE) {
+ ERROR("Reparse data of \"%s\" has size "
+ "%"PRIu64" bytes (exceeds %u bytes)",
+ dentry_full_path(one_dentry),
+ blob->size, REPARSE_DATA_MAX_SIZE);
+ return WIMLIB_ERR_INVALID_REPARSE_DATA;
+ }
+ ctx->reparse_ptr = ctx->rpbuf.rpdata;
+ ctx->rpbuf.rpdatalen = cpu_to_le16(blob->size);
+ ctx->rpbuf.rpreserved = cpu_to_le16(0);
+ ctx->ntfs_reparse_inodes[ctx->num_reparse_inodes] = ni;
+ ctx->wim_reparse_inodes[ctx->num_reparse_inodes] = inode;
+ ctx->num_reparse_inodes++;
+ return 0;
+ }
+
+ /* It's a data stream (may be unnamed or named). */
+ wimlib_assert(strm->stream_type == STREAM_TYPE_DATA);
+
+ stream_name_nchars = utf16le_len_chars(strm->stream_name);
+
+ if (stream_name_nchars &&
+ (ntfs_attr_add(ni, AT_DATA, strm->stream_name,
+ stream_name_nchars, NULL, 0)))
+ {
+ ERROR_WITH_ERRNO("Failed to create named data stream of \"%s\"",
+ dentry_full_path(one_dentry));
+ return WIMLIB_ERR_NTFS_3G;
+ }
+
+ /* This should be ensured by extract_blob_list() */
+ wimlib_assert(ctx->num_open_attrs < MAX_OPEN_FILES);
+
+ attr = ntfs_attr_open(ni, AT_DATA, strm->stream_name,
+ stream_name_nchars);
+ if (!attr) {
+ ERROR_WITH_ERRNO("Failed to open data stream of \"%s\"",
+ dentry_full_path(one_dentry));
+ return WIMLIB_ERR_NTFS_3G;
+ }
+ ctx->open_attrs[ctx->num_open_attrs++] = attr;
+ ntfs_attr_truncate_solid(attr, blob->size);
+ return 0;
+}
+
+static int
+ntfs_3g_cleanup_blob_extract(struct ntfs_3g_apply_ctx *ctx)