~gbmor/clinte

0fe685c34c5cdbff278b70c98241b0aa7c5bd6ed — Ben Morrison 1 year, 3 days ago 40bb48b
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.
6 files changed, 29 insertions(+), 131 deletions(-)

M Cargo.lock
M Cargo.toml
M Makefile
A check_new_clinte_posts.sh
M src/db.rs
M src/posts.rs
M Cargo.lock => Cargo.lock +0 -89
@@ 74,35 74,6 @@ dependencies = [
]

[[package]]
name = "block-buffer"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
 "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
 "byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
 "generic-array 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "block-padding"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "byte-tools"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "byteorder"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 141,7 112,6 @@ dependencies = [
 "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
 "serde 1.0.110 (registry+https://github.com/rust-lang/crates.io-index)",
 "serde_json 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
 "sha2 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "simplelog 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
 "users 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
]


@@ 161,14 131,6 @@ dependencies = [
]

[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "generic-array 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "dirs"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 209,11 171,6 @@ dependencies = [
]

[[package]]
name = "fake-simd"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "fd-lock"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 224,15 181,6 @@ dependencies = [
]

[[package]]
name = "generic-array"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "version_check 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "getrandom"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 301,11 249,6 @@ version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "opaque-debug"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "proc-macro2"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 386,17 329,6 @@ dependencies = [
]

[[package]]
name = "sha2"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
 "block-buffer 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "digest 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
 "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
 "opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
]

[[package]]
name = "simplelog"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 460,11 392,6 @@ dependencies = [
]

[[package]]
name = "typenum"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "unicode-width"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 483,11 410,6 @@ dependencies = [
]

[[package]]
name = "version_check"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"

[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 522,23 444,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum base64 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
"checksum blake2b_simd 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a"
"checksum block-buffer 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dbcf92448676f82bb7a334c58bbce8b0d43580fb5362a9d608b18879d12a3d31"
"checksum block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5"
"checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
"checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
"checksum chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "31850b4a4d6bae316f7a09e691c944c28299298837edc0a03f755618c23cbc01"
"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9"
"checksum constant_time_eq 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
"checksum crossbeam-utils 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "04973fa96e96579258a5091af6003abde64af786b860f18622b82e026cca60e6"
"checksum digest 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
"checksum dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3"
"checksum dirs-sys 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "afa0b23de8fd801745c471deffa6e12d248f962c9fd4b4c33787b055599bde7b"
"checksum failure 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
"checksum failure_derive 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4"
"checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
"checksum fd-lock 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a15bec795244d49f5ee3024bdc6c3883fb035f7f6601d4a4821c3d5d60784454"
"checksum generic-array 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ac746a5f3bbfdadd6106868134545e684693d54d9d44f6e9588a7d54af0bf980"
"checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
"checksum gimli 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bcc8e0c9bce37868955864dbecd2b1ab2bdf967e6f28066d65aaac620444b65c"
"checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772"


@@ 549,7 464,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba"
"checksum num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
"checksum object 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9cbca9424c482ee628fa549d9c812e2cd22f1180b9222c9200fdfa6eb31aecb2"
"checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
"checksum proc-macro2 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "1502d12e458c49a4c9cbff560d0fe0060c252bc29799ed94ca2ed4bb665a0101"
"checksum quote 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "54a21852a652ad6f610c9510194f398ff6f8692e334fd1145fed931f7fbe44ea"
"checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"


@@ 560,7 474,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum serde 1.0.110 (registry+https://github.com/rust-lang/crates.io-index)" = "99e7b308464d16b56eba9964e4972a3eee817760ab60d88c3f86e1fecb08204c"
"checksum serde_derive 1.0.110 (registry+https://github.com/rust-lang/crates.io-index)" = "818fbf6bfa9a42d3bfcaca148547aa00c7b915bec71d1757aa2d44ca68771984"
"checksum serde_json 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)" = "993948e75b189211a9b31a7528f950c6adc21f9720b6438ff80a7fa2f864cea2"
"checksum sha2 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "72377440080fd008550fe9b441e854e43318db116f90181eef92e9ae9aedab48"
"checksum simplelog 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)" = "05a3e303ace6adb0a60a9e9e2fbc6a33e1749d1e43587e2125f7efa9c5e107c5"
"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
"checksum syn 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "ef781e621ee763a2a40721a8861ec519cb76966aee03bb5d00adb6a31dc1c1de"


@@ 568,11 481,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum term 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c0863a3345e70f61d613eab32ee046ccd1bcc5f9105fe402c61fcd0c13eeb8b5"
"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
"checksum typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
"checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
"checksum users 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c72f4267aea0c3ec6d07eaabea6ead7c5ddacfafc5e22bcf8d186706851fb4cf"
"checksum version_check 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
"checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"

M Cargo.toml => Cargo.toml +0 -1
@@ 22,7 22,6 @@ lazy_static = "^1.4"
log = "^0.4"
serde = { version = "^1.0", features = ["derive"] }
serde_json = "^1.0"
sha2 = "^0.9"
simplelog = "^0.7"
users = "^0.9"


M Makefile => Makefile +1 -0
@@ 37,6 37,7 @@ install:
	install -m755 target/release/clinte $(BINDIR)

	@if [ -f "$(DBDIR)/clinte.json" ]; then printf "\n%s\n" "clinte.json exists. Skipping ..."; else install -m666 clinte.json "$(DBDIR)"; fi
	@if [ -e /etc/profile.d ]; then printf "%s\n" "Installing check_new_clinte_posts.sh to /etc/profile.d" && install -m644 check_new_clinte_posts.sh /etc/profile.d/; fi

	@printf "\n%s\n" "...Done!"


A check_new_clinte_posts.sh => check_new_clinte_posts.sh +12 -0
@@ 0,0 1,12 @@
#!/bin/sh

LOCAL_FILE="$HOME/.clinte.json"
LOCAL_HASH=$(sha256sum "$LOCAL_FILE" | cut -d' ' -f1)

DBFILE="/usr/local/clinte/clinte.json"
DBFILE_HASH=$(sha256sum "$DBFILE" | cut -d' ' -f1)

if [ "$LOCAL_HASH" != "$DBFILE_HASH" ]
then
    printf "%s\n" "New posts on clinte!"
fi
\ No newline at end of file

M src/db.rs => src/db.rs +0 -40
@@ 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);
    }
}

M src/posts.rs => src/posts.rs +16 -1
@@ 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(())
}