+ }
+ return 0;
+}
+
+static int
+sort_stream_list_by_wim_position(struct list_head *stream_list)
+{
+ struct list_head *cur;
+ size_t num_streams;
+ struct wim_lookup_table_entry **array;
+ size_t i;
+ size_t array_size;
+
+ num_streams = 0;
+ list_for_each(cur, stream_list)
+ num_streams++;
+ array_size = num_streams * sizeof(array[0]);
+ array = MALLOC(array_size);
+ if (!array) {
+ ERROR("Failed to allocate %zu bytes to sort stream entries",
+ array_size);
+ return WIMLIB_ERR_NOMEM;
+ }
+ cur = stream_list->next;
+ for (i = 0; i < num_streams; i++) {
+ array[i] = container_of(cur, struct wim_lookup_table_entry, extraction_list);
+ cur = cur->next;
+ }
+
+ qsort(array, num_streams, sizeof(array[0]), cmp_streams_by_wim_position);
+
+ INIT_LIST_HEAD(stream_list);
+ for (i = 0; i < num_streams; i++)
+ list_add_tail(&array[i]->extraction_list, stream_list);
+ FREE(array);
+ return 0;
+}
+
+/*
+ * Extract a dentry to standard output.
+ *
+ * This obviously doesn't make sense in all cases. We return an error if the
+ * dentry does not correspond to a regular file. Otherwise we extract the
+ * unnamed data stream only.
+ */
+static int
+extract_dentry_to_stdout(struct wim_dentry *dentry)
+{
+ int ret = 0;
+ if (!dentry_is_regular_file(dentry)) {
+ ERROR("\"%"TS"\" is not a regular file and therefore cannot be "
+ "extracted to standard output", dentry->_full_path);
+ ret = WIMLIB_ERR_NOT_A_REGULAR_FILE;
+ } else {
+ struct wim_lookup_table_entry *lte;
+
+ lte = inode_unnamed_lte_resolved(dentry->d_inode);
+ if (lte) {
+ ret = extract_wim_resource_to_fd(lte, STDOUT_FILENO,
+ wim_resource_size(lte));
+ }
+ }
+ return ret;
+}
+
+/*
+ * extract_tree - Extract a file or directory tree from the currently selected
+ * WIM image.
+ *
+ * @wim: WIMStruct for the WIM file, with the desired image selected
+ * (as wim->current_image).
+ * @wim_source_path:
+ * "Canonical" (i.e. no leading or trailing slashes, path
+ * separators forwald slashes) path inside the WIM image to
+ * extract. An empty string means the full image.
+ * @target:
+ * Filesystem path to extract the file or directory tree to.
+ *
+ * @extract_flags:
+ * WIMLIB_EXTRACT_FLAG_*. Also, the private flag
+ * WIMLIB_EXTRACT_FLAG_MULTI_IMAGE will be set if this is being
+ * called through wimlib_extract_image() with WIMLIB_ALL_IMAGES as
+ * the image.
+ *
+ * @progress_func:
+ * If non-NULL, progress function for the extraction. The messages
+ * we may in this function are:
+ *
+ * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN or
+ * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN;
+ * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_BEGIN;
+ * WIMLIB_PROGRESS_MSG_EXTRACT_DIR_STRUCTURE_END;
+ * WIMLIB_PROGRESS_MSG_EXTRACT_DENTRY;
+ * WIMLIB_PROGRESS_MSG_EXTRACT_STREAMS;
+ * WIMLIB_PROGRESS_MSG_APPLY_TIMESTAMPS;
+ * WIMLIB_PROGRESS_MSG_EXTRACT_TREE_END or
+ * WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_END.
+ *
+ * Returns 0 on success; nonzero on failure.
+ */
+static int
+extract_tree(WIMStruct *wim, const tchar *wim_source_path, const tchar *target,
+ int extract_flags, wimlib_progress_func_t progress_func)
+{
+ int ret;
+ struct list_head stream_list;
+ struct apply_args args;
+ const struct apply_operations *ops;
+ struct wim_dentry *root;
+
+ memset(&args, 0, sizeof(args));
+
+ args.w = wim;
+ args.target = target;
+ args.extract_flags = extract_flags;
+ args.progress_func = progress_func;
+ args.target_nchars = tstrlen(target);
+ args.wim_source_path_nchars = tstrlen(wim_source_path);
+
+ if (progress_func) {
+ args.progress.extract.wimfile_name = wim->filename;
+ args.progress.extract.image = wim->current_image;
+ args.progress.extract.extract_flags = (extract_flags &
+ WIMLIB_EXTRACT_MASK_PUBLIC);
+ args.progress.extract.image_name = wimlib_get_image_name(wim,
+ wim->current_image);
+ args.progress.extract.extract_root_wim_source_path = wim_source_path;
+ args.progress.extract.target = target;
+ }
+
+#ifdef WITH_NTFS_3G
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_NTFS) {
+ args.vol = ntfs_mount(target, 0);
+ if (!args.vol) {
+ ERROR_WITH_ERRNO("Failed to mount NTFS volume `%"TS"'",
+ target);
+ ret = WIMLIB_ERR_NTFS_3G;
+ goto out;
+ }
+ ops = &ntfs_apply_operations;
+ } else
+#endif
+ ops = &normal_apply_operations;
+
+ root = get_dentry(wim, wim_source_path);
+ if (!root) {
+ ERROR("Path \"%"TS"\" does not exist in WIM image %d",
+ wim_source_path, wim->current_image);
+ ret = WIMLIB_ERR_PATH_DOES_NOT_EXIST;
+ goto out_ntfs_umount;
+ }
+ args.extract_root = root;
+
+ ret = calculate_dentry_tree_full_paths(root);
+ if (ret)
+ goto out_ntfs_umount;
+
+ /* Build a list of the streams that need to be extracted */
+ find_streams_for_extraction(root,
+ &stream_list,
+ wim->lookup_table, extract_flags);
+
+ /* Calculate the number of bytes of data that will be extracted */
+ calculate_bytes_to_extract(&stream_list, extract_flags,
+ &args.progress);
+
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_TO_STDOUT) {
+ ret = extract_dentry_to_stdout(root);
+ goto out_dentry_reset_needs_extraction;
+ }
+
+ if (progress_func) {
+ progress_func(*wim_source_path ? WIMLIB_PROGRESS_MSG_EXTRACT_TREE_BEGIN :
+ WIMLIB_PROGRESS_MSG_EXTRACT_IMAGE_BEGIN,
+ &args.progress);
+ }
+
+ /* If a sequential extraction was specified, sort the streams to be
+ * extracted by their position in the WIM file, so that the WIM file can
+ * be read sequentially. */
+ if (extract_flags & WIMLIB_EXTRACT_FLAG_SEQUENTIAL) {
+ ret = sort_stream_list_by_wim_position(&stream_list);
+ if (ret != 0) {
+ WARNING("Falling back to non-sequential extraction");
+ extract_flags &= ~WIMLIB_EXTRACT_FLAG_SEQUENTIAL;