Skip to content

Commit

Permalink
fix(extract): extracting unicode files into dir on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
muja committed Sep 8, 2024
1 parent 9b6703b commit 4120387
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/open_archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl OpenArchive<Process, CursorBeforeFile> {
/// Extracts the file into the current working directory
/// Returns the OpenArchive for further processing
pub fn extract(self) -> UnrarResult<OpenArchive<Process, CursorBeforeHeader>> {
self.process_file::<Extract>(None, None)
self.dir_extract(None)
}

/// Extracts the file into the specified directory.
Expand All @@ -406,8 +406,7 @@ impl OpenArchive<Process, CursorBeforeFile> {
self,
base: P,
) -> UnrarResult<OpenArchive<Process, CursorBeforeHeader>> {
let path = pathed::construct(&base);
self.process_file::<Extract>(Some(&path), None)
self.dir_extract(Some(base.as_ref()))
}

/// Extracts the file into the specified file.
Expand All @@ -423,6 +422,18 @@ impl OpenArchive<Process, CursorBeforeFile> {
let dest = pathed::construct(&file);
self.process_file::<Extract>(None, Some(&dest))
}

/// extracting into a directory if the filename has unicode characters
/// does not work on Linux, so we must specify the full path for Linux
fn dir_extract(
self,
base: Option<&Path>,
) -> UnrarResult<OpenArchive<Process, CursorBeforeHeader>> {
let mut path = None;
let mut file = None;
pathed::preprocess_extract(base, &self.entry().filename, &mut path, &mut file);
self.process_file::<Extract>(path.as_ref(), file.as_ref())
}
}

fn read_header(handle: &Handle) -> UnrarResult<Option<FileHeader>> {
Expand Down
20 changes: 14 additions & 6 deletions src/pathed/all.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::{Path, PathBuf};
use widestring::WideCString;

pub(crate) type RarPath = WideCString;
Expand All @@ -16,12 +17,19 @@ pub(crate) fn process_file(
unrar_sys::RARProcessFileW(
handle,
operation,
dest_path
.map(|path| path.as_ptr() as *const _)
.unwrap_or(std::ptr::null()),
dest_name
.map(|file| file.as_ptr() as *const _)
.unwrap_or(std::ptr::null()),
dest_path.map(|path| path.as_ptr() as *const _).unwrap_or(std::ptr::null()),
dest_name.map(|file| file.as_ptr() as *const _).unwrap_or(std::ptr::null()),
)
}
}

pub(crate) fn preprocess_extract(
base: Option<&Path>,
_filename: &PathBuf,
path: &mut Option<RarPath>,
_file: &mut Option<RarPath>,
) {
if let Some(base) = base {
*path = Some(construct(base));
}
}
10 changes: 10 additions & 0 deletions src/pathed/linux.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::path::{Path, PathBuf};
use std::ffi::CString;

pub(crate) type RarPath = CString;
Expand Down Expand Up @@ -25,3 +26,12 @@ pub(crate) fn process_file(
)
}
}

pub(crate) fn preprocess_extract(
base: Option<&Path>,
filename: &PathBuf,
_path: &mut Option<RarPath>,
file: &mut Option<RarPath>,
) {
*file = Some(construct(base.unwrap_or(".".as_ref()).join(filename)));
}

0 comments on commit 4120387

Please sign in to comment.