-static int extract_regular_file_linked(struct dentry *dentry,
- const char *output_path,
- struct apply_args *args,
- struct lookup_table_entry *lte)
-{
- /* This mode overrides the normal hard-link extraction and
- * instead either symlinks or hardlinks *all* identical files in
- * the WIM, even if they are in a different image (in the case
- * of a multi-image extraction) */
-
- if (args->extract_flags & WIMLIB_EXTRACT_FLAG_HARDLINK) {
- if (link(lte->extracted_file, output_path) != 0) {
- ERROR_WITH_ERRNO("Failed to hard link "
- "`%s' to `%s'",
- output_path, lte->extracted_file);
- return WIMLIB_ERR_LINK;
- }
- } else {
- int num_path_components;
- int num_output_dir_path_components;
- size_t extracted_file_len;
- char *p;
- const char *p2;
- size_t i;
-
- num_path_components =
- get_num_path_components(dentry->full_path_utf8) - 1;
- num_output_dir_path_components =
- get_num_path_components(args->target);
-
- if (args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE) {
- num_path_components++;
- num_output_dir_path_components--;
- }
- extracted_file_len = strlen(lte->extracted_file);
-
- char buf[extracted_file_len + 3 * num_path_components + 1];
- p = &buf[0];
-
- for (i = 0; i < num_path_components; i++) {
- *p++ = '.';
- *p++ = '.';
- *p++ = '/';
- }
- p2 = lte->extracted_file;
- while (*p2 == '/')
- p2++;
- while (num_output_dir_path_components--)
- p2 = path_next_part(p2, NULL);
- strcpy(p, p2);
- if (symlink(buf, output_path) != 0) {
- ERROR_WITH_ERRNO("Failed to symlink `%s' to "
- "`%s'",
- buf, lte->extracted_file);
- return WIMLIB_ERR_LINK;
- }
- }
- return 0;
-}
-
-static int extract_regular_file_unlinked(struct dentry *dentry,
- struct apply_args *args,
- const char *output_path,
- struct lookup_table_entry *lte)
-{
- /* Normal mode of extraction. Regular files and hard links are
- * extracted in the way that they appear in the WIM. */
-
- int out_fd;
- int ret;
- struct inode *inode = dentry->d_inode;
-
- if (!((args->extract_flags & WIMLIB_EXTRACT_FLAG_MULTI_IMAGE)
- && (args->extract_flags & (WIMLIB_EXTRACT_FLAG_SYMLINK |
- WIMLIB_EXTRACT_FLAG_HARDLINK))))
- {
- /* If the dentry is one of a hard link set of at least 2
- * dentries and one of the other dentries has already been
- * extracted, make a hard link to the file corresponding to this
- * already-extracted directory. Otherwise, extract the file,
- * and set the inode->extracted_file field so that other
- * dentries in the hard link group can link to it. */
- if (inode->link_count > 1) {
- if (inode->extracted_file) {
- DEBUG("Extracting hard link `%s' => `%s'",
- output_path, inode->extracted_file);
- if (link(inode->extracted_file, output_path) != 0) {
- ERROR_WITH_ERRNO("Failed to hard link "
- "`%s' to `%s'",
- output_path,
- inode->extracted_file);
- return WIMLIB_ERR_LINK;
- }
- return 0;
- }
- FREE(inode->extracted_file);
- inode->extracted_file = STRDUP(output_path);
- if (!inode->extracted_file) {
- ERROR("Failed to allocate memory for filename");
- return WIMLIB_ERR_NOMEM;
- }
- }
- }
-
- /* Extract the contents of the file to @output_path. */
-
- out_fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- if (out_fd == -1) {
- ERROR_WITH_ERRNO("Failed to open the file `%s' for writing",
- output_path);
- return WIMLIB_ERR_OPEN;
- }
-
- if (!lte) {
- /* Empty file with no lookup table entry */
- DEBUG("Empty file `%s'.", output_path);
- ret = 0;
- goto out;
- }
-
- ret = extract_wim_resource_to_fd(lte, out_fd, wim_resource_size(lte));
- if (ret != 0) {
- ERROR("Failed to extract resource to `%s'", output_path);
- goto out;
- }
- args->progress.extract.completed_bytes += wim_resource_size(lte);
-out:
- if (close(out_fd) != 0) {
- ERROR_WITH_ERRNO("Failed to close file `%s'", output_path);
- ret = WIMLIB_ERR_WRITE;
- }
- return ret;
-}
-
-/*
- * Extracts a regular file from the WIM archive.
- */
-static int extract_regular_file(struct dentry *dentry,
- struct apply_args *args,
- const char *output_path)