Skip to content

Commit

Permalink
Merge pull request #116 from TidyBee/111-canonicalize-filepaths-in-da…
Browse files Browse the repository at this point in the history
…tabase

111 canonicalize filepaths in database
  • Loading branch information
Bootoyka authored Feb 12, 2024
2 parents 496d152 + fa6d619 commit f5cb0a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 48 deletions.
18 changes: 17 additions & 1 deletion src/file_lister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,30 @@ fn get_file_signature(path: &PathBuf) -> u128 {
xxh3_128(&buffer)
}

#[cfg(not(target_os = "windows"))]
fn fix_canonicalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
path.as_ref().into()
}

#[cfg(target_os = "windows")]
fn fix_canonicalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
const UNCPREFIX: &str = r"\\?\";
let p: String = path.as_ref().display().to_string();
if p.starts_with(UNCPREFIX) {
p[UNCPREFIX.len()..].into()
} else {
p.into()
}
}

pub fn list_directories(directories: Vec<PathBuf>) -> Result<Vec<FileInfo>, std::io::Error> {
let mut files: Vec<FileInfo> = Vec::new();

for directory in directories {
if directory.is_dir() {
for entry in fs::read_dir(&directory)? {
let entry: fs::DirEntry = entry?;
let path: PathBuf = entry.path();
let path: PathBuf = fix_canonicalize_path(fs::canonicalize(entry.path())?);

if path.is_dir() {
files.extend(list_directories(vec![path])?);
Expand Down
69 changes: 22 additions & 47 deletions src/my_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,25 @@ impl MyFiles {
}
}
}
pub fn remove_file_from_db(&self, file_path: &str) -> Result<()> {
match self
.connection_pool
.execute("DELETE FROM my_files WHERE path = ?1", params![file_path])
{

pub fn remove_file_from_db(&self, file_path: PathBuf) -> Result<()> {
let str_filepath = file_path.to_str().unwrap();

match self.connection_pool.execute(
"DELETE FROM my_files WHERE path = ?1",
params![str_filepath],
) {
Ok(_) => {
info!("{} removed from my_files", file_path);
info!("{} removed from my_files", str_filepath);
Ok(())
}
Err(error) => {
error!("Error removing {} from my_files: {}", file_path, error);
error!("Error removing {} from my_files: {}", str_filepath, error);
Err(error)
}
}
}

pub fn add_file_to_db(&self, file: &FileInfo) -> Result<FileInfo> {
let last_modified: DateTime<Utc> = file.last_modified.into();
let last_accessed: DateTime<Utc> = file.last_accessed.into();
Expand Down Expand Up @@ -541,6 +545,8 @@ impl MyFiles {
#[cfg(test)]
mod tests {

use std::env::current_dir;

use super::*;
use crate::{configuration, file_lister};

Expand Down Expand Up @@ -612,62 +618,31 @@ mod tests {
duplicated: None,
unused: true,
};
let mut tests_dir = current_dir().unwrap();
tests_dir.push([r"tests", "assets", "test_folder"].iter().collect::<PathBuf>());

my_files
.set_tidyscore(
[r"tests", "assets", "test_folder", "test-file-1"]
.iter()
.collect(),
&dummy_score,
)
.set_tidyscore(tests_dir.join("test-file-1"), &dummy_score)
.unwrap();
let mut score = my_files
.get_tidyscore(
[r"tests", "assets", "test_folder", "test-file-1"]
.iter()
.collect(),
)
.get_tidyscore(tests_dir.join("test-file-1"))
.unwrap();
let is_misnamed = score.misnamed;
let is_unused = score.unused;
assert!(is_misnamed);
assert!(is_unused);

my_files
.add_duplicated_file_to_db(
[r"tests", "assets", "test_folder", "test-file-1"]
.iter()
.collect(),
[r"tests", "assets", "test_folder", "test-file-1-dup-1"]
.iter()
.collect(),
)
.add_duplicated_file_to_db(tests_dir.join("test-file-1"), tests_dir.join("test-file-2"))
.unwrap();
my_files
.add_duplicated_file_to_db(
[r"tests", "assets", "test_folder", "test-file-1"]
.iter()
.collect(),
[r"tests", "assets", "test_folder", "test-file-1-dup-2"]
.iter()
.collect(),
)
.add_duplicated_file_to_db(tests_dir.join("test-file-1"), tests_dir.join("test-file-3"))
.unwrap();
my_files
.add_duplicated_file_to_db(
[r"tests", "assets", "test_folder", "test-file-1"]
.iter()
.collect(),
[r"tests", "assets", "test_folder", "test-file-1-dup-3"]
.iter()
.collect(),
)
.add_duplicated_file_to_db(tests_dir.join("test-file-1"), tests_dir.join("test-file-4"))
.unwrap();
score = my_files
.get_tidyscore(
[r"tests", "assets", "test_folder", "test-file-1"]
.iter()
.collect(),
)
.get_tidyscore(tests_dir.join("test-file-1"))
.unwrap();
let is_duplicated = match score.duplicated {
Some(duplicated) => !duplicated.is_empty(),
Expand Down

0 comments on commit f5cb0a3

Please sign in to comment.