summaryrefslogtreecommitdiffstats
path: root/src/meta/inode.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/meta/inode.rs')
-rw-r--r--src/meta/inode.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/meta/inode.rs b/src/meta/inode.rs
new file mode 100644
index 0000000..6849947
--- /dev/null
+++ b/src/meta/inode.rs
@@ -0,0 +1,62 @@
+use crate::color::{ColoredString, Colors, Elem};
+use std::fs::Metadata;
+
+#[derive(Debug, PartialEq, Eq, Copy, Clone)]
+pub struct INode {
+ index: Option<u64>,
+}
+
+impl<'a> From<&'a Metadata> for INode {
+ #[cfg(unix)]
+ fn from(meta: &Metadata) -> Self {
+ use std::os::unix::fs::MetadataExt;
+
+ let index = meta.ino();
+
+ Self { index: Some(index) }
+ }
+
+ #[cfg(windows)]
+ fn from(_: &Metadata) -> Self {
+ Self { index: None }
+ }
+}
+
+impl INode {
+ pub fn render(&self, colors: &Colors) -> ColoredString {
+ match self.index {
+ Some(i) => colors.colorize(i.to_string(), &Elem::INode { valid: true }),
+ None => colors.colorize(String::from("-"), &Elem::INode { valid: false }),
+ }
+ }
+}
+
+#[cfg(test)]
+#[cfg(unix)]
+mod tests {
+ use super::INode;
+ use std::env;
+ use std::io;
+ use std::path::Path;
+ use std::process::{Command, ExitStatus};
+
+ fn cross_platform_touch(path: &Path) -> io::Result<ExitStatus> {
+ Command::new("touch").arg(&path).status()
+ }
+
+ #[test]
+ fn test_inode_no_zero() {
+ let mut file_path = env::temp_dir();
+ file_path.push("inode.tmp");
+
+ let success = cross_platform_touch(&file_path).unwrap().success();
+ assert!(success, "failed to exec touch");
+
+ let inode = INode::from(&file_path.metadata().unwrap());
+
+ #[cfg(unix)]
+ assert!(inode.index.is_some());
+ #[cfg(windows)]
+ assert!(inode.index.is_none());
+ }
+}