summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob L. Kreuze <zerodaysfordays@sdf.org>2020-05-17 22:52:42 -0400
committerJakob L. Kreuze <zerodaysfordays@sdf.org>2020-05-17 22:52:42 -0400
commit668606d9a584e6a55ba698ee5f6fb4892cf02a37 (patch)
treec69bb15b2a726b4647502e39d3433434ea68add0
Initial commit.
-rw-r--r--.gitignore1
-rw-r--r--Cargo.lock187
-rw-r--r--Cargo.toml14
-rw-r--r--src/main.rs82
4 files changed, 284 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ea8c4bf
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..4e7ac4a
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,187 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
+name = "blake2"
+version = "0.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "94cb07b0da6a73955f8fb85d24c466778e70cda767a568229b104f0264089330"
+dependencies = [
+ "byte-tools",
+ "crypto-mac",
+ "digest",
+ "opaque-debug",
+]
+
+[[package]]
+name = "byte-tools"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7"
+
+[[package]]
+name = "crypto-mac"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5"
+dependencies = [
+ "generic-array",
+ "subtle",
+]
+
+[[package]]
+name = "digest"
+version = "0.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5"
+dependencies = [
+ "generic-array",
+]
+
+[[package]]
+name = "fallible-iterator"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
+
+[[package]]
+name = "fallible-streaming-iterator"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
+
+[[package]]
+name = "generic-array"
+version = "0.12.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec"
+dependencies = [
+ "typenum",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.70"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3baa92041a6fec78c687fa0cc2b3fae8884f743d672cf551bed1d6dac6988d0f"
+
+[[package]]
+name = "libsqlite3-sys"
+version = "0.17.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "56d90181c2904c287e5390186be820e5ef311a3c62edebb7d6ca3d6a48ce041d"
+dependencies = [
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "linked-hash-map"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
+
+[[package]]
+name = "lru-cache"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c"
+dependencies = [
+ "linked-hash-map",
+]
+
+[[package]]
+name = "memchr"
+version = "2.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
+
+[[package]]
+name = "opaque-debug"
+version = "0.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
+
+[[package]]
+name = "pkg-config"
+version = "0.3.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
+
+[[package]]
+name = "roru"
+version = "0.1.0"
+dependencies = [
+ "blake2",
+ "rusqlite",
+]
+
+[[package]]
+name = "rusqlite"
+version = "0.21.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "64a656821bb6317a84b257737b7934f79c0dbb7eb694710475908280ebad3e64"
+dependencies = [
+ "bitflags",
+ "fallible-iterator",
+ "fallible-streaming-iterator",
+ "libsqlite3-sys",
+ "lru-cache",
+ "memchr",
+ "time",
+]
+
+[[package]]
+name = "subtle"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee"
+
+[[package]]
+name = "time"
+version = "0.1.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
+dependencies = [
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "typenum"
+version = "1.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
+
+[[package]]
+name = "vcpkg"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168"
+
+[[package]]
+name = "winapi"
+version = "0.3.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 0000000..99c4e70
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "roru"
+version = "0.1.0"
+authors = ["Jakob L. Kreuze <zerodaysfordays@sdf.org>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+blake2 = "0.8"
+
+[dependencies.rusqlite]
+version = "0.21.0"
+# features = ["bundled"]
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..07cf4d6
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,82 @@
+extern crate rusqlite;
+
+use blake2::{Blake2b, Digest};
+use rusqlite::{params, Connection, Result};
+use std::io;
+use std::path::Path;
+
+/// Return a hex-encoded string representing the contents of `hash`.
+fn hexlify(hash: &[u8]) -> String {
+ hash.iter()
+ .map(|b| format!("{:x}", b))
+ .collect::<Vec<String>>()
+ .join("")
+}
+
+#[derive(Debug)]
+struct TagBase {
+ conn: Connection,
+}
+
+impl TagBase {
+ /// Instantiate a new `TagBase` whose backing database is at `path`.
+ fn new(path: &str) -> Result<Self> {
+ let conn = Connection::open(path)?;
+ conn.execute(
+ "CREATE TABLE IF NOT EXISTS images (
+ id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+ blake2 TEXT NOT NULL,
+ filename TEXT NOT NULL,
+ orig_dir TEXT NOT NULL
+ )",
+ params![],
+ )?;
+ conn.execute(
+ "CREATE TABLE IF NOT EXISTS tags (
+ id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+ name TEXT NOT NULL
+ )",
+ params![],
+ )?;
+ conn.execute(
+ "CREATE TABLE IF NOT EXISTS mapping (
+ image INTEGER NOT NULL,
+ tag INTEGER NOT NULL
+ )",
+ params![],
+ )?;
+ Ok(TagBase { conn })
+ }
+
+ // TODO: Return a proper error.
+ fn import_image(&self, path: &Path) -> Result<()> {
+ if path.is_dir() {
+ return Err(rusqlite::Error::InvalidQuery);
+ }
+
+ let mut file = std::fs::File::open(path).unwrap();
+ let mut hasher = Blake2b::new();
+ let n = io::copy(&mut file, &mut hasher).unwrap();
+ let hash = hexlify(&hasher.result());
+
+ let parsed_or_none = path.file_name().and_then(|os| os.to_str());
+ if let (Some(path), Some(file_name)) = (path.to_str(), parsed_or_none) {
+ let split_index = path.len() - file_name.len();
+ let parent_directory = &path[0..split_index];
+ self.conn.execute(
+ "INSERT INTO images (sha256, filename, orig_dir) VALUES(?,?,?)",
+ params![0, file_name, parent_directory],
+ )?;
+ Ok(())
+ } else {
+ Err(rusqlite::Error::InvalidQuery)
+ }
+ }
+}
+
+fn main() {
+ let tb = TagBase::new("/tmp/test.db").unwrap();
+ tb.import_image(Path::new("/home/jakob/Sync/06fc9ae71549eb4d.jpeg"))
+ .unwrap();
+ println!("{:?}", tb);
+}