History

remove `pwd` crate dependency in favor of using libc directly

…to obtain the home directory of a user with the given name. `pwd` still uses failure and appears unmaintained as PRs remain open for more than a month now.

Sebastian Thiel committed 4 years ago

Commit 4adfa111 parent a1981d44 files · +24 −107Browse files

Unified · Split

4 files
  1. Cargo.lock +1 −96
  2. git-config/Cargo.toml +3 −1
  3. git-config/src/lib.rs +1 −2
  4. git-config/src/values.rs +19 −8
Cargo.lock+1−96
@@ -2,15 +2,6 @@ · 1 unmodified line
2# It is not intended for manual editing.2# It is not intended for manual editing.
3version = 33version = 3
4 4
5[[package]]
6name = "addr2line"
7version = "0.17.0"
8source = "registry+https://github.com/rust-lang/crates.io-index"
9checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b"
10dependencies = [
11 "gimli",
12]
13
14[[package]]5[[package]]
15name = "adler"6name = "adler"
16version = "1.0.2"7version = "1.0.2"
@@ -231,21 +222,6 @@ version = "1.1.0" · 214 unmodified lines
231source = "registry+https://github.com/rust-lang/crates.io-index"222source = "registry+https://github.com/rust-lang/crates.io-index"
232checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"223checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
233 224
234[[package]]
235name = "backtrace"
236version = "0.3.65"
237source = "registry+https://github.com/rust-lang/crates.io-index"
238checksum = "11a17d453482a265fd5f8479f2a3f405566e6ca627837aaddb85af8b1ab8ef61"
239dependencies = [
240 "addr2line",
241 "cc",
242 "cfg-if",
243 "libc",
244 "miniz_oxide",
245 "object",
246 "rustc-demangle",
247]
248
249[[package]]225[[package]]
250name = "base64"226name = "base64"
251version = "0.13.0"227version = "0.13.0"
@@ -934,28 +910,6 @@ version = "2.5.2" · 682 unmodified lines
934source = "registry+https://github.com/rust-lang/crates.io-index"910source = "registry+https://github.com/rust-lang/crates.io-index"
935checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71"911checksum = "77f3309417938f28bf8228fcff79a4a37103981e3e186d2ccd19c74b38f4eb71"
936 912
937[[package]]
938name = "failure"
939version = "0.1.8"
940source = "registry+https://github.com/rust-lang/crates.io-index"
941checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
942dependencies = [
943 "backtrace",
944 "failure_derive",
945]
946
947[[package]]
948name = "failure_derive"
949version = "0.1.8"
950source = "registry+https://github.com/rust-lang/crates.io-index"
951checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4"
952dependencies = [
953 "proc-macro2",
954 "quote",
955 "syn",
956 "synstructure",
957]
958
959[[package]]913[[package]]
960name = "fastrand"914name = "fastrand"
961version = "1.7.0"915version = "1.7.0"
@@ -1085,12 +1039,6 @@ dependencies = [ · 123 unmodified lines
1085 "wasi",1039 "wasi",
1086]1040]
1087 1041
1088[[package]]
1089name = "gimli"
1090version = "0.26.1"
1091source = "registry+https://github.com/rust-lang/crates.io-index"
1092checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4"
1093
1094[[package]]1042[[package]]
1095name = "git-actor"1043name = "git-actor"
1096version = "0.10.0"1044version = "0.10.0"
@@ -1165,9 +1113,9 @@ dependencies = [ · 68 unmodified lines
1165 "git-ref",1113 "git-ref",
1166 "git-sec",1114 "git-sec",
1167 "git-testtools",1115 "git-testtools",
1116 "libc",
1168 "memchr",1117 "memchr",
1169 "nom",1118 "nom",
1170 "pwd",
1171 "serde",1119 "serde",
1172 "serde_derive",1120 "serde_derive",
1173 "serial_test",1121 "serial_test",
@@ -2187,15 +2135,6 @@ dependencies = [ · 1013 unmodified lines
2187 "libc",2135 "libc",
2188]2136]
2189 2137
2190[[package]]
2191name = "object"
2192version = "0.28.4"
2193source = "registry+https://github.com/rust-lang/crates.io-index"
2194checksum = "e42c982f2d955fac81dd7e1d0e1426a7d702acd9c98d19ab01083a6a0328c424"
2195dependencies = [
2196 "memchr",
2197]
2198
2199[[package]]2138[[package]]
2200name = "object-access"2139name = "object-access"
2201version = "0.1.0"2140version = "0.1.0"
@@ -2463,16 +2402,6 @@ dependencies = [ · 261 unmodified lines
2463 "unicase",2402 "unicase",
2464]2403]
2465 2404
2466[[package]]
2467name = "pwd"
2468version = "1.3.1"
2469source = "registry+https://github.com/rust-lang/crates.io-index"
2470checksum = "f9ca0304857594109dca88140120427c7a65027be6b77d86a5938588e79cb07b"
2471dependencies = [
2472 "failure",
2473 "libc",
2474]
2475
2476[[package]]2405[[package]]
2477name = "quick-error"2406name = "quick-error"
2478version = "2.0.1"2407version = "2.0.1"
@@ -2562,12 +2491,6 @@ dependencies = [ · 83 unmodified lines
2562 "winapi",2491 "winapi",
2563]2492]
2564 2493
2565[[package]]
2566name = "rustc-demangle"
2567version = "0.1.21"
2568source = "registry+https://github.com/rust-lang/crates.io-index"
2569checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
2570
2571[[package]]2494[[package]]
2572name = "rustc-hash"2495name = "rustc-hash"
2573version = "1.1.0"2496version = "1.1.0"
@@ -2835,18 +2758,6 @@ dependencies = [ · 261 unmodified lines
2835 "unicode-ident",2758 "unicode-ident",
2836]2759]
2837 2760
2838[[package]]
2839name = "synstructure"
2840version = "0.12.6"
2841source = "registry+https://github.com/rust-lang/crates.io-index"
2842checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f"
2843dependencies = [
2844 "proc-macro2",
2845 "quote",
2846 "syn",
2847 "unicode-xid",
2848]
2849
2850[[package]]2761[[package]]
2851name = "tar"2762name = "tar"
2852version = "0.4.38"2763version = "0.4.38"
@@ -3080,12 +2991,6 @@ version = "0.1.9" · 227 unmodified lines
3080source = "registry+https://github.com/rust-lang/crates.io-index"2991source = "registry+https://github.com/rust-lang/crates.io-index"
3081checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"2992checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
3082 2993
3083[[package]]
3084name = "unicode-xid"
3085version = "0.2.3"
3086source = "registry+https://github.com/rust-lang/crates.io-index"
3087checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
3088
3089[[package]]2994[[package]]
3090name = "url"2995name = "url"
3091version = "2.2.2"2996version = "2.2.2"
git-config/Cargo.toml+3−1
@@ -24,11 +24,13 @@ dirs = "4" · 23 unmodified lines
24nom = { version = "7", default_features = false, features = [ "std" ] }24nom = { version = "7", default_features = false, features = [ "std" ] }
25memchr = "2"25memchr = "2"
26serde_crate = { version = "1", package = "serde", optional = true }26serde_crate = { version = "1", package = "serde", optional = true }
27pwd = "1.3.1"
28thiserror = "1.0.26"27thiserror = "1.0.26"
29unicode-bom = "1.1.4"28unicode-bom = "1.1.4"
30bstr = { version = "0.2.13", default-features = false, features = ["std"] }29bstr = { version = "0.2.13", default-features = false, features = ["std"] }
31 30
31[target.'cfg(not(windows))'.dependencies]
32libc = "0.2"
33
32[dev-dependencies]34[dev-dependencies]
33git-testtools = { path = "../tests/tools"}35git-testtools = { path = "../tests/tools"}
34serial_test = "0.6.0"36serial_test = "0.6.0"
git-config/src/lib.rs+1−2
@@ -1,5 +1,4 @@
1#![forbid(unsafe_code)]1#![deny(unsafe_code, rust_2018_idioms)]
2#![deny(rust_2018_idioms)]
3// #![warn(missing_docs)]2// #![warn(missing_docs)]
4// #![warn(clippy::pedantic, clippy::nursery)]3// #![warn(clippy::pedantic, clippy::nursery)]
5 4
git-config/src/values.rs+19−8
@@ -191,7 +191,23 @@ pub mod path { · 190 unmodified lines
191 use std::borrow::Cow;191 use std::borrow::Cow;
192 192
193 #[cfg(not(any(target_os = "android", target_os = "windows")))]193 #[cfg(not(any(target_os = "android", target_os = "windows")))]
194 use pwd::Passwd;194 fn home_for_user(name: &str) -> Option<std::path::PathBuf> {
195 let cname = std::ffi::CString::new(name).ok()?;
196 // SAFETY: calling this in a threaded program that modifies the pw database is not actually safe.
197 // TODO: use the `*_r` version, but it's much harder to use.
198 #[allow(unsafe_code)]
199 let pwd = unsafe { libc::getpwnam(cname.as_ptr()) };
200 if pwd.is_null() {
201 None
202 } else {
203 use std::os::unix::ffi::OsStrExt;
204 // SAFETY: pw_dir is a cstr and it lives as long as… well, we hope nobody changes the pw database while we are at it
205 // from another thread. Otherwise it lives long enough.
206 #[allow(unsafe_code)]
207 let cstr = unsafe { std::ffi::CStr::from_ptr((*pwd).pw_dir) };
208 Some(std::ffi::OsStr::from_bytes(cstr.to_bytes()).into())
209 }
210 }
195 211
196 use crate::values::Path;212 use crate::values::Path;
197 213
@@ -210,8 +226,6 @@ pub mod path { · 12 unmodified lines
210 },226 },
211 #[error("Ill-formed UTF-8 in username")]227 #[error("Ill-formed UTF-8 in username")]
212 UsernameConversion(#[from] std::str::Utf8Error),228 UsernameConversion(#[from] std::str::Utf8Error),
213 #[error("User home info missing")]
214 PwdFileQuery,
215 #[error("User interpolation is not available on this platform")]229 #[error("User interpolation is not available on this platform")]
216 UserInterpolationUnsupported,230 UserInterpolationUnsupported,
217 }231 }
@@ -281,10 +295,7 @@ pub mod path { · 63 unmodified lines
281 .ok_or(interpolate::Error::Missing { what: "/" })?;295 .ok_or(interpolate::Error::Missing { what: "/" })?;
282 let (username, path_with_leading_slash) = val.split_at(i);296 let (username, path_with_leading_slash) = val.split_at(i);
283 let username = std::str::from_utf8(username)?;297 let username = std::str::from_utf8(username)?;
284 let home = Passwd::from_name(username)298 let home = home_for_user(username).ok_or(interpolate::Error::Missing { what: "pwd user info" })?;
285 .map_err(|_| interpolate::Error::PwdFileQuery)?
286 .ok_or(interpolate::Error::Missing { what: "pwd user info" })?
287 .dir;
288 let path_past_user_prefix =299 let path_past_user_prefix =
289 git_path::try_from_byte_slice(&path_with_leading_slash["/".len()..]).map_err(|err| {300 git_path::try_from_byte_slice(&path_with_leading_slash["/".len()..]).map_err(|err| {
290 interpolate::Error::Utf8Conversion {301 interpolate::Error::Utf8Conversion {
@@ -292,7 +303,7 @@ pub mod path { · 1 unmodified line
292 err,303 err,
293 }304 }
294 })?;305 })?;
295 Ok(std::path::PathBuf::from(home).join(path_past_user_prefix).into())306 Ok(home.join(path_past_user_prefix).into())
296 }307 }
297 }308 }
298}309}