Skip to content

Commit c7c9e0a

Browse files
committed
refactor: remove Mutex and change to unsafe fn
1 parent e5d8643 commit c7c9e0a

File tree

2 files changed

+50
-64
lines changed

2 files changed

+50
-64
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ bitflags = "1.1.0"
2121
libc = "0.2"
2222
log = "0.4.8"
2323
libgit2-sys = { path = "libgit2-sys", version = "0.12.17" }
24-
once_cell = "1.5"
2524

2625
[target."cfg(all(unix, not(target_os = \"macos\")))".dependencies]
2726
openssl-sys = { version = "0.9.0", optional = true }

src/opts.rs

Lines changed: 50 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,10 @@
11
//! Bindings to libgit2's git_libgit2_opts function.
22
33
use std::ffi::CString;
4-
use std::sync::Mutex;
5-
6-
use once_cell::sync::Lazy;
74

85
use crate::util::Binding;
96
use crate::{call, raw, Buf, ConfigLevel, Error, IntoCString};
107

11-
static SEARCH_PATH: Lazy<Mutex<SearchPath>> = Lazy::new(|| {
12-
crate::init();
13-
Mutex::new(SearchPath)
14-
});
15-
16-
struct SearchPath;
17-
18-
impl SearchPath {
19-
pub fn set<P>(&self, level: ConfigLevel, path: P) -> Result<(), Error>
20-
where
21-
P: IntoCString,
22-
{
23-
let path = path.into_c_string()?;
24-
unsafe {
25-
call::c_try(raw::git_libgit2_opts(
26-
raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
27-
level as libc::c_int,
28-
path.as_ptr(),
29-
))?;
30-
}
31-
Ok(())
32-
}
33-
34-
pub fn reset(&self, level: ConfigLevel) -> Result<(), Error> {
35-
unsafe {
36-
call::c_try(raw::git_libgit2_opts(
37-
raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
38-
level as libc::c_int,
39-
core::ptr::null::<u8>(),
40-
))?;
41-
}
42-
Ok(())
43-
}
44-
45-
pub fn get(&self, level: ConfigLevel) -> Result<CString, Error> {
46-
let buf = Buf::new();
47-
unsafe {
48-
call::c_try(raw::git_libgit2_opts(
49-
raw::GIT_OPT_GET_SEARCH_PATH as libc::c_int,
50-
level as libc::c_int,
51-
buf.raw(),
52-
))?;
53-
}
54-
buf.into_c_string()
55-
}
56-
}
57-
588
/// Set the search path for a level of config data. The search path applied to
599
/// shared attributes and ignore files, too.
6010
///
@@ -64,28 +14,56 @@ impl SearchPath {
6414
/// `path` lists directories delimited by `GIT_PATH_LIST_SEPARATOR`.
6515
/// Use magic path `$PATH` to include the old value of the path
6616
/// (if you want to prepend or append, for instance).
67-
pub fn set_search_path<P>(level: ConfigLevel, path: P) -> Result<(), Error>
17+
///
18+
/// This function is unsafe as it mutates the global state but cannot guarantee
19+
/// thread-safety. It needs to be externally synchronized with calls to access
20+
/// the global state.
21+
pub unsafe fn set_search_path<P>(level: ConfigLevel, path: P) -> Result<(), Error>
6822
where
6923
P: IntoCString,
7024
{
71-
SEARCH_PATH.lock().unwrap().set(level, path)
25+
call::c_try(raw::git_libgit2_opts(
26+
raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
27+
level as libc::c_int,
28+
path.into_c_string()?.as_ptr(),
29+
))?;
30+
Ok(())
7231
}
7332

7433
/// Reset the search path for a given level of config data to the default
7534
/// (generally based on environment variables).
7635
///
7736
/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
7837
/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
79-
pub fn reset_search_path(level: ConfigLevel) -> Result<(), Error> {
80-
SEARCH_PATH.lock().unwrap().reset(level)
38+
///
39+
/// This function is unsafe as it mutates the global state but cannot guarantee
40+
/// thread-safety. It needs to be externally synchronized with calls to access
41+
/// the global state.
42+
pub unsafe fn reset_search_path(level: ConfigLevel) -> Result<(), Error> {
43+
call::c_try(raw::git_libgit2_opts(
44+
raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
45+
level as libc::c_int,
46+
core::ptr::null::<u8>(),
47+
))?;
48+
Ok(())
8149
}
8250

8351
/// Get the search path for a given level of config data.
8452
///
8553
/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
8654
/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
87-
pub fn get_search_path(level: ConfigLevel) -> Result<CString, Error> {
88-
SEARCH_PATH.lock().unwrap().get(level)
55+
///
56+
/// This function is unsafe as it mutates the global state but cannot guarantee
57+
/// thread-safety. It needs to be externally synchronized with calls to access
58+
/// the global state.
59+
pub unsafe fn get_search_path(level: ConfigLevel) -> Result<CString, Error> {
60+
let buf = Buf::new();
61+
call::c_try(raw::git_libgit2_opts(
62+
raw::GIT_OPT_GET_SEARCH_PATH as libc::c_int,
63+
level as libc::c_int,
64+
buf.raw(),
65+
))?;
66+
buf.into_c_string()
8967
}
9068

9169
/// Controls whether or not libgit2 will verify when writing an object that all
@@ -134,25 +112,34 @@ mod test {
134112
#[test]
135113
fn search_path() -> Result<(), Box<dyn std::error::Error>> {
136114
let path = "fake_path";
137-
let original = get_search_path(ConfigLevel::Global);
115+
let original = unsafe { get_search_path(ConfigLevel::Global) };
138116
assert_ne!(original, Ok(path.into_c_string()?));
139117

140118
// Set
141-
set_search_path(ConfigLevel::Global, &path)?;
119+
unsafe {
120+
set_search_path(ConfigLevel::Global, &path)?;
121+
}
142122
assert_eq!(
143-
get_search_path(ConfigLevel::Global),
123+
unsafe { get_search_path(ConfigLevel::Global) },
144124
Ok(path.into_c_string()?)
145125
);
146126

147127
// Append
148128
let paths = join_paths(["$PATH", path].iter())?;
149129
let expected_paths = join_paths([path, path].iter())?.into_c_string()?;
150-
set_search_path(ConfigLevel::Global, paths)?;
151-
assert_eq!(get_search_path(ConfigLevel::Global), Ok(expected_paths));
130+
unsafe {
131+
set_search_path(ConfigLevel::Global, paths)?;
132+
}
133+
assert_eq!(
134+
unsafe { get_search_path(ConfigLevel::Global) },
135+
Ok(expected_paths)
136+
);
152137

153138
// Reset
154-
reset_search_path(ConfigLevel::Global)?;
155-
assert_eq!(get_search_path(ConfigLevel::Global), original);
139+
unsafe {
140+
reset_search_path(ConfigLevel::Global)?;
141+
}
142+
assert_eq!(unsafe { get_search_path(ConfigLevel::Global) }, original);
156143

157144
Ok(())
158145
}

0 commit comments

Comments
 (0)