-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unpacking of filenames with contains UTF-8 characters
Change from the C to C.UTF-8 locale, allowing libarchive to handle filenames in UTF-8. We restrict to change LC_CTYPE only, since libarchive only needs the charset set. See on libarchive Website for a more complete description of the issue: libarchive/libarchive#587 https://github.com/libarchive/libarchive/wiki/Filenames Once we complete the uncompress operation, we restore the original LC_CTYPE after extraction to avoid side effects. Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
- Loading branch information
1 parent
897721b
commit f4f0668
Showing
7 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (C) 2021 O.S. Systems Software LTDA | ||
// | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
/// Change from the C to system locale, allowing libarchive to handle filenames | ||
/// in UTF-8. We restrict to change LC_CTYPE only, since libarchive only needs | ||
/// the charset set. | ||
/// | ||
/// See on libarchive Website for a more complete description of the issue: | ||
/// | ||
/// https://github.com/libarchive/libarchive/issues/587 | ||
/// https://github.com/libarchive/libarchive/wiki/Filenames | ||
pub(crate) use inner::UTF8LocaleGuard; | ||
|
||
#[cfg(unix)] | ||
mod inner { | ||
pub(crate) struct UTF8LocaleGuard { | ||
save: libc::locale_t, | ||
} | ||
|
||
impl UTF8LocaleGuard { | ||
pub(crate) fn new() -> Self { | ||
#[cfg(target_os = "linux")] | ||
let locale = b"\0"; | ||
|
||
#[cfg(target_os = "macos")] | ||
let locale = b"UTF-8\0"; | ||
|
||
let utf8_locale = unsafe { | ||
libc::newlocale( | ||
libc::LC_CTYPE_MASK, | ||
locale.as_ptr() as *const libc::c_char, | ||
std::ptr::null_mut(), | ||
) | ||
}; | ||
|
||
let save = unsafe { libc::uselocale(utf8_locale) }; | ||
|
||
Self { save } | ||
} | ||
} | ||
|
||
impl Drop for UTF8LocaleGuard { | ||
fn drop(&mut self) { | ||
unsafe { libc::uselocale(self.save) }; | ||
} | ||
} | ||
} | ||
|
||
#[cfg(windows)] | ||
mod inner { | ||
extern "C" { | ||
fn _configthreadlocale(arg1: std::os::raw::c_int) -> std::os::raw::c_int; | ||
} | ||
const _ENABLE_PER_THREAD_LOCALE: std::os::raw::c_int = 1; | ||
|
||
pub(crate) struct UTF8LocaleGuard { | ||
save: Option<std::ffi::CString>, | ||
save_thread_config: ::std::os::raw::c_int, | ||
} | ||
|
||
impl UTF8LocaleGuard { | ||
pub(crate) fn new() -> Self { | ||
let locale = b".UTF-8\0"; | ||
|
||
let (save, save_thread_config) = { | ||
let old_locale = unsafe { libc::setlocale(libc::LC_CTYPE, std::ptr::null()) }; | ||
( | ||
if old_locale.is_null() { | ||
None | ||
} else { | ||
Some(unsafe { std::ffi::CStr::from_ptr(old_locale) }.to_owned()) | ||
}, | ||
unsafe { _configthreadlocale(0) }, | ||
) | ||
}; | ||
|
||
unsafe { | ||
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE); | ||
libc::setlocale( | ||
libc::LC_CTYPE, | ||
std::ffi::CStr::from_bytes_with_nul_unchecked(locale).as_ptr(), | ||
) | ||
}; | ||
|
||
Self { | ||
save, | ||
save_thread_config, | ||
} | ||
} | ||
} | ||
|
||
impl Drop for UTF8LocaleGuard { | ||
fn drop(&mut self) { | ||
if let Some(locale) = &self.save { | ||
unsafe { libc::setlocale(libc::LC_CTYPE, locale.as_ptr()) }; | ||
} | ||
|
||
unsafe { | ||
_configthreadlocale(self.save_thread_config); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters