From 8b00faf07563f0dbee883e73b5e045dd477a8bb5 Mon Sep 17 00:00:00 2001 From: Dhruva Sagar Date: Mon, 9 Jan 2023 10:51:23 +0530 Subject: [PATCH] Fix Temporary File Path #6 Better cross platform support --- src/database.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/database.rs b/src/database.rs index 4713aa7..2982a4e 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,5 +1,6 @@ -use anyhow::Result; use crate::params::Params; +use anyhow::Result; +use std::env::temp_dir; #[derive(Debug, Clone)] pub struct File { @@ -8,9 +9,12 @@ pub struct File { } pub fn get_connection(args: &Params) -> Result { + let mut tmp_file = temp_dir(); + tmp_file.push("deduplicator.db"); + let connection_url = match args.nocache { - false => "/tmp/deduplicator.db", - true => ":memory:" + false => tmp_file.to_str().unwrap(), + true => ":memory:", }; sqlite::open(connection_url).and_then(|conn| { @@ -36,9 +40,7 @@ pub fn put(file: &File, connection: &sqlite::Connection) -> Result<()> { } pub fn indexed_paths(connection: &sqlite::Connection) -> Result> { - let query = format!( - "SELECT * FROM files" - ); + let query = format!("SELECT * FROM files"); let result: Vec = connection .prepare(query)? @@ -49,7 +51,7 @@ pub fn indexed_paths(connection: &sqlite::Connection) -> Result> { let hash = row.read::("hash").to_string(); File { path, hash } }) - .collect(); + .collect(); Ok(result) } @@ -65,7 +67,8 @@ pub fn duplicate_hashes(connection: &sqlite::Connection, path: &String) -> Resul ON a.hash = b.hash WHERE a.file_identifier LIKE \"{}%\" ORDER BY a.file_identifier - ", path + ", + path ); let result: Vec = connection