-
Notifications
You must be signed in to change notification settings - Fork 27
/
locale.rs
104 lines (88 loc) · 2.87 KB
/
locale.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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);
}
}
}
}