about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-06-16 20:05:30 -0400
committerBen Morrison <ben@gbmor.dev>2020-06-16 20:14:37 -0400
commit0fe685c34c5cdbff278b70c98241b0aa7c5bd6ed (patch)
tree5468c2a6d278d901f28085d3b2f1b41a1df4f860 /src
parent40bb48bb701f88c902b912fb51fea88b5ae9a8b8 (diff)
downloadclinte-0fe685c34c5cdbff278b70c98241b0aa7c5bd6ed.tar.gz
moved hashing to shellscript to check for posts
removes dependency on sha2 crate and allows users
to choose their own hashing algorithm easily.

now copies clinte.json to $HOME/.clinte.json once
the posts are viewed. a shellscript can be used to
compare the hash of the global clinte.json to the
hash of the copy. an example script is supplied.

on `make install` the example script is installed
to /etc/profile.d/, where it will be run for users
on login.
Diffstat (limited to 'src')
-rw-r--r--src/db.rs40
-rw-r--r--src/posts.rs17
2 files changed, 16 insertions, 41 deletions
diff --git a/src/db.rs b/src/db.rs
index eeaa27a..20ad5a1 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,6 +1,5 @@
 use fd_lock::FdLock;
 use serde::{Deserialize, Serialize};
-use sha2::{Sha256, Digest};
 
 use std::fs;
 use std::fs::File;
@@ -59,27 +58,6 @@ impl Posts {
         out
     }
 
-    // Hash the contents of clinte.json and store it in your
-    // home directory. Useful for determining if there are
-    // new posts.
-    pub fn hash(&self) -> error::Result<()> {
-        let body = error::helper(serde_json::to_string_pretty(&self), "Couldn't reserialize json to record hash");
-        let mut hasher = Sha256::new();
-        hasher.update(body);
-        let hash = format!("{:x}", hasher.finalize());
-    
-        #[cfg(test)]
-        let homedir = std::env::var("PWD")?;
-
-        #[cfg(not(test))]
-        let homedir = std::env::var("HOME")?;
-        
-        let writepath = format!("{}/.clinte.sha256", homedir);
-        fs::write(writepath, &hash)?;
-
-        Ok(())
-    }
-
     pub fn replace(&mut self, n: usize, post: Post) {
         self.posts[n] = post;
     }
@@ -168,22 +146,4 @@ mod tests {
         all.delete(1);
         all.write();
     }
-
-    #[test]
-    fn check_hash() {
-        let strposts = fs::read_to_string(PATH).unwrap();
-        let mut hasher = Sha256::new();
-        hasher.update(strposts);
-        let rhs = format!("{:x}", hasher.finalize());
-
-        let all = Posts::get_all(PATH);
-        all.hash().unwrap();
-
-        let pwd = std::env::var("PWD").unwrap();
-        let hashpath = format!("{}/.clinte.sha256", pwd);
-        let lhs = fs::read_to_string(hashpath.clone()).unwrap();
-        fs::remove_file(hashpath).unwrap();
-
-        assert_eq!(lhs, rhs);
-    }
 }
diff --git a/src/posts.rs b/src/posts.rs
index 0de3d52..1dfd167 100644
--- a/src/posts.rs
+++ b/src/posts.rs
@@ -1,5 +1,8 @@
 use std::io;
 
+#[cfg(not(test))]
+use std::{env, fs};
+
 use crate::conf;
 use crate::db;
 use crate::error;
@@ -83,7 +86,6 @@ pub fn display() -> error::Result<()> {
         .unwrap_or_else(|_| 80);
 
     let all = db::Posts::get_all(db::PATH);
-    all.hash()?;
 
     let mut postvec = Vec::new();
     all.posts().iter().enumerate().for_each(|(id, post)| {
@@ -117,6 +119,19 @@ pub fn display() -> error::Result<()> {
         }
     }
 
+    // copy the json file to the user's home directory
+    // so it can be compared to the global file to check
+    // for new posts.
+    // previously, I'd hashed it, but I feel like it's
+    // better to leave the hashing to UNIX utilities
+    // than to add a heavy-ish dependency.
+    #[cfg(not(test))]
+    {
+        let homedir = env::var("HOME")?;
+        let localdest = format!("{}/.clinte.json", homedir);
+        fs::copy(db::PATH, localdest)?;
+    }
+
     Ok(())
 }