Skip to content

Commit f607797

Browse files
committed
feat: obtain identities from_path() or from_process() (#386)
1 parent c5e2346 commit f607797

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

Cargo.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

git-sec/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,10 @@ serde1 = [ "serde" ]
1818

1919
[dependencies]
2020
serde = { version = "1.0.114", optional = true, default-features = false, features = ["std", "derive"] }
21+
22+
[target.'cfg(not(windows))'.dependencies]
23+
libc = "0.2.123"
24+
25+
26+
[dev-dependencies]
27+
tempfile = "3.3.0"

git-sec/src/lib.rs

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#![forbid(unsafe_code)]
2-
#![deny(rust_2018_idioms, missing_docs)]
1+
#![deny(unsafe_code, rust_2018_idioms, missing_docs)]
32
//! A shared trust model for `gitoxide` crates.
43
54
/// Various types to identify entities.
@@ -22,4 +21,64 @@ pub mod identity {
2221
/// The user's password
2322
pub password: String,
2423
}
24+
25+
///
26+
pub mod user_id {
27+
use crate::identity::UserId;
28+
use std::borrow::Cow;
29+
use std::path::Path;
30+
31+
/// Obtain the owner of the given `path`.
32+
pub fn from_path(path: Cow<'_, Path>) -> std::io::Result<UserId> {
33+
impl_::from_path(path)
34+
}
35+
36+
/// Obtain the of the currently running process.
37+
pub fn from_process() -> Result<UserId, from_process::Error> {
38+
impl_::from_process()
39+
}
40+
41+
///
42+
pub mod from_process {
43+
use crate::identity::user_id::impl_;
44+
45+
/// The error returned by [from_process()][super::from_process()].
46+
pub type Error = impl_::FromProcessError;
47+
}
48+
49+
#[cfg(not(windows))]
50+
mod impl_ {
51+
use crate::identity::UserId;
52+
use std::borrow::Cow;
53+
use std::path::Path;
54+
55+
pub fn from_path(path: Cow<'_, Path>) -> std::io::Result<UserId> {
56+
use std::os::unix::fs::MetadataExt;
57+
let meta = std::fs::symlink_metadata(path)?;
58+
Ok(meta.uid())
59+
}
60+
61+
pub type FromProcessError = std::convert::Infallible;
62+
pub fn from_process() -> Result<UserId, FromProcessError> {
63+
// SAFETY: there is no documented possibility for failure
64+
#[allow(unsafe_code)]
65+
let uid = unsafe { libc::geteuid() };
66+
Ok(uid)
67+
}
68+
}
69+
70+
#[cfg(windows)]
71+
mod impl_ {
72+
use crate::identity::UserId;
73+
use std::borrow::Cow;
74+
use std::path::Path;
75+
76+
pub fn from_path(path: Cow<'_, Path>) -> std::io::Result<UserId> {
77+
todo!("unix")
78+
}
79+
pub fn from_process() -> std::io::Result<UserId> {
80+
todo!("process")
81+
}
82+
}
83+
}
2584
}

git-sec/tests/identity/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mod uid {
2+
#[test]
3+
fn from_path() {
4+
let dir = tempfile::tempdir().unwrap();
5+
let owner = git_sec::identity::user_id::from_path(dir.path().into()).unwrap();
6+
assert_eq!(owner, git_sec::identity::user_id::from_process().unwrap());
7+
}
8+
}

git-sec/tests/sec.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
2+
3+
mod identity;

0 commit comments

Comments
 (0)