merry_process/
host_resources.rs1use std::path::{Path, PathBuf};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum HostPathKind {
8 Directory,
10 RegularFile,
12 UnixSocket,
14 Other,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct HostPathMetadata {
21 kind: HostPathKind,
22 owner_uid: u32,
23 mode: u32,
24}
25
26impl HostPathMetadata {
27 #[must_use]
29 pub const fn new(kind: HostPathKind, owner_uid: u32, mode: u32) -> Self {
30 Self {
31 kind,
32 owner_uid,
33 mode,
34 }
35 }
36
37 #[cfg(unix)]
39 pub fn inspect(path: &Path) -> std::io::Result<Self> {
40 use std::os::unix::fs::{FileTypeExt, MetadataExt};
41 let metadata = std::fs::metadata(path)?;
42 let file_type = metadata.file_type();
43 let kind = if file_type.is_socket() {
44 HostPathKind::UnixSocket
45 } else if file_type.is_file() {
46 HostPathKind::RegularFile
47 } else if file_type.is_dir() {
48 HostPathKind::Directory
49 } else {
50 HostPathKind::Other
51 };
52 Ok(Self::new(kind, metadata.uid(), metadata.mode()))
53 }
54
55 #[must_use]
57 pub const fn kind(self) -> HostPathKind {
58 self.kind
59 }
60
61 #[must_use]
63 pub const fn owner_uid(self) -> u32 {
64 self.owner_uid
65 }
66
67 #[must_use]
69 pub const fn mode(self) -> u32 {
70 self.mode
71 }
72
73 #[must_use]
75 pub fn is_owned_socket(self, current_uid: u32) -> bool {
76 self.kind == HostPathKind::UnixSocket && self.owner_uid == current_uid
77 }
78}
79
80#[must_use]
83pub fn ssh_known_hosts(home: &Path) -> [PathBuf; 2] {
84 [
85 home.join(".ssh/known_hosts"),
86 home.join(".ssh/known_hosts2"),
87 ]
88}