2020-09-12 14:27:45 +02:00
|
|
|
use crate::addressing::Address;
|
2020-09-12 22:50:14 +02:00
|
|
|
use crate::database::{
|
2020-09-25 02:45:17 +02:00
|
|
|
bulk_retrieve_objects, file_set_valid, insert_entry, insert_file, query_entries,
|
2020-09-29 00:30:00 +02:00
|
|
|
retrieve_all_files, DbPool, Entry, EntryQuery, EntryValue, InnerEntry, QueryComponent,
|
|
|
|
DATABASE_FILENAME,
|
2020-09-12 22:50:14 +02:00
|
|
|
};
|
2020-09-20 19:32:28 +02:00
|
|
|
use crate::hash::Hashable;
|
2020-09-12 14:27:45 +02:00
|
|
|
use crate::models;
|
2020-09-22 00:41:59 +02:00
|
|
|
use crate::models::File;
|
2020-09-20 17:17:43 +02:00
|
|
|
use anyhow::{anyhow, Error, Result};
|
2020-09-20 19:28:44 +02:00
|
|
|
use chrono::prelude::*;
|
|
|
|
use diesel::sqlite::Sqlite;
|
|
|
|
use diesel::Connection;
|
2020-09-15 19:26:47 +02:00
|
|
|
use log::{error, info, trace};
|
2020-09-20 19:28:44 +02:00
|
|
|
use rayon::prelude::*;
|
2020-09-12 14:27:45 +02:00
|
|
|
use serde::export::Formatter;
|
|
|
|
use serde_json::Value;
|
2020-09-12 22:50:14 +02:00
|
|
|
use std::path::{Component, Path, PathBuf};
|
2020-09-20 20:14:05 +02:00
|
|
|
use std::sync::{Arc, RwLock};
|
2020-09-23 23:11:50 +02:00
|
|
|
use std::time::{Instant, UNIX_EPOCH};
|
2020-09-12 22:50:14 +02:00
|
|
|
use std::{fs, iter};
|
2020-09-12 14:27:45 +02:00
|
|
|
use uuid::Uuid;
|
2020-09-20 19:28:44 +02:00
|
|
|
use walkdir::WalkDir;
|
2020-09-12 14:27:45 +02:00
|
|
|
|
|
|
|
const DIR_KEY: &str = "DIR";
|
|
|
|
const DIR_HAS_KEY: &str = "DIR_HAS";
|
2020-09-12 23:07:50 +02:00
|
|
|
|
|
|
|
const FILE_IDENTITY_KEY: &str = "FILE_IS";
|
|
|
|
const FILENAME_KEY: &str = "FILE_NAME";
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2020-09-12 22:50:14 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2020-09-12 14:27:45 +02:00
|
|
|
pub struct UDirectory {
|
|
|
|
name: String,
|
|
|
|
}
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2020-09-12 22:50:14 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2020-09-12 14:27:45 +02:00
|
|
|
pub struct UPath(Vec<UDirectory>);
|
|
|
|
|
|
|
|
const TOP_SEPARATOR: &str = "//";
|
|
|
|
|
|
|
|
impl std::str::FromStr for UPath {
|
|
|
|
type Err = anyhow::Error;
|
|
|
|
|
2020-09-13 19:20:32 +02:00
|
|
|
fn from_str(string: &str) -> Result<Self, Self::Err> {
|
2020-09-14 01:16:01 +02:00
|
|
|
if string.is_empty() {
|
2020-09-12 14:27:45 +02:00
|
|
|
Ok(UPath(vec![]))
|
|
|
|
} else {
|
2020-09-13 19:20:32 +02:00
|
|
|
let result = match string.find(TOP_SEPARATOR) {
|
2020-09-12 14:27:45 +02:00
|
|
|
Some(head_idx) => {
|
2020-09-13 19:20:32 +02:00
|
|
|
let (head, rest) = string.split_at(head_idx);
|
2020-09-12 14:27:45 +02:00
|
|
|
let mut result: Vec<UDirectory> = Vec::new();
|
|
|
|
result.push(UDirectory {
|
|
|
|
name: String::from(head),
|
|
|
|
});
|
|
|
|
result.append(
|
|
|
|
rest[TOP_SEPARATOR.len()..rest.len()]
|
2020-09-13 20:10:18 +02:00
|
|
|
.trim_end_matches('/')
|
2020-09-14 01:16:01 +02:00
|
|
|
.split('/')
|
2020-09-12 14:27:45 +02:00
|
|
|
.map(|part| UDirectory {
|
|
|
|
name: String::from(part),
|
|
|
|
})
|
|
|
|
.collect::<Vec<UDirectory>>()
|
|
|
|
.as_mut(),
|
|
|
|
);
|
2020-09-13 19:20:32 +02:00
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
None => string
|
2020-09-13 20:10:18 +02:00
|
|
|
.trim_end_matches('/')
|
2020-09-14 01:16:01 +02:00
|
|
|
.split('/')
|
2020-09-13 19:20:32 +02:00
|
|
|
.map(|part| UDirectory {
|
|
|
|
name: String::from(part),
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
};
|
|
|
|
|
|
|
|
for directory in &result {
|
2020-09-14 01:16:01 +02:00
|
|
|
if directory.name.is_empty() {
|
2020-09-13 19:20:32 +02:00
|
|
|
return Err(anyhow!("INVALID PATH: Directory name cannot be empty!"));
|
2020-09-12 14:27:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-13 19:20:32 +02:00
|
|
|
|
|
|
|
Ok(UPath(result))
|
2020-09-12 14:27:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for UDirectory {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for UPath {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self.0.len() {
|
|
|
|
0 => write!(f, ""),
|
|
|
|
1 => write!(f, "{}", self.0.first().unwrap().name),
|
|
|
|
_ => {
|
|
|
|
let (head, tail) = self.0.split_first().unwrap();
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}//{}",
|
|
|
|
head.name,
|
|
|
|
tail.iter()
|
|
|
|
.map(|udir| udir.name.clone())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("/")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 20:10:18 +02:00
|
|
|
trait EntryList {
|
|
|
|
fn extract_addresses(&self) -> Vec<Address>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EntryList for Vec<Entry> {
|
|
|
|
fn extract_addresses(&self) -> Vec<Address> {
|
2020-09-14 01:16:01 +02:00
|
|
|
self.iter()
|
2020-09-13 20:10:18 +02:00
|
|
|
.filter_map(|e| {
|
|
|
|
if let EntryValue::Address(address) = &e.value {
|
|
|
|
Some(address.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 19:28:44 +02:00
|
|
|
pub fn list_roots<C: Connection<Backend = Sqlite>>(connection: &C) -> Result<Vec<Entry>> {
|
2020-09-15 19:26:47 +02:00
|
|
|
let all_directories: Vec<Entry> = query_entries(
|
|
|
|
connection,
|
|
|
|
EntryQuery {
|
2020-09-29 00:30:00 +02:00
|
|
|
target: QueryComponent::Any,
|
|
|
|
key: QueryComponent::Exact(DIR_KEY.to_string()),
|
|
|
|
value: QueryComponent::Any,
|
2020-09-15 19:26:47 +02:00
|
|
|
},
|
|
|
|
)?;
|
2020-09-13 20:10:18 +02:00
|
|
|
|
2020-09-15 19:26:47 +02:00
|
|
|
let directories_with_parents: Vec<Address> = query_entries(
|
|
|
|
connection,
|
|
|
|
EntryQuery {
|
2020-09-29 00:30:00 +02:00
|
|
|
target: QueryComponent::Any,
|
|
|
|
key: QueryComponent::Exact(DIR_HAS_KEY.to_string()),
|
|
|
|
value: QueryComponent::Any,
|
2020-09-15 19:26:47 +02:00
|
|
|
},
|
|
|
|
)?
|
|
|
|
.extract_addresses();
|
2020-09-13 20:10:18 +02:00
|
|
|
|
|
|
|
Ok(all_directories
|
|
|
|
.into_iter()
|
|
|
|
.filter(|entry| !directories_with_parents.contains(&entry.target))
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
|
2020-09-15 19:26:47 +02:00
|
|
|
pub async fn list_directory<C: Connection<Backend = Sqlite>>(
|
|
|
|
connection: &C,
|
|
|
|
path: &UPath,
|
|
|
|
) -> Result<Vec<Entry>> {
|
2020-09-13 20:10:18 +02:00
|
|
|
let entry_addresses = match path.0.len() {
|
2020-09-20 19:28:44 +02:00
|
|
|
0 => list_roots(connection)?
|
2020-09-13 20:10:18 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|e| e.target)
|
|
|
|
.collect(),
|
|
|
|
_ => {
|
2020-09-20 19:28:44 +02:00
|
|
|
let resolved_path: Vec<Address> = resolve_path(connection, path, false)?;
|
2020-09-13 20:10:18 +02:00
|
|
|
let last = resolved_path.last().unwrap();
|
|
|
|
|
2020-09-15 19:26:47 +02:00
|
|
|
query_entries(
|
|
|
|
connection,
|
|
|
|
EntryQuery {
|
2020-09-29 00:30:00 +02:00
|
|
|
target: QueryComponent::Exact(last.clone()),
|
|
|
|
key: QueryComponent::Exact(DIR_HAS_KEY.to_string()),
|
|
|
|
value: QueryComponent::Any,
|
2020-09-15 19:26:47 +02:00
|
|
|
},
|
|
|
|
)?
|
|
|
|
.extract_addresses()
|
2020-09-13 20:10:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-25 02:45:17 +02:00
|
|
|
Ok(bulk_retrieve_objects(connection, entry_addresses)?
|
|
|
|
.into_iter()
|
|
|
|
.filter(|e| [DIR_KEY, FILENAME_KEY, FILE_IDENTITY_KEY].contains(&e.key.as_str()))
|
|
|
|
.collect::<Vec<Entry>>())
|
2020-09-13 20:10:18 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 19:28:44 +02:00
|
|
|
pub fn fetch_or_create_dir<C: Connection<Backend = Sqlite>>(
|
2020-09-15 19:26:47 +02:00
|
|
|
connection: &C,
|
2020-09-12 14:27:45 +02:00
|
|
|
parent: Option<Address>,
|
|
|
|
directory: UDirectory,
|
2020-09-13 20:10:18 +02:00
|
|
|
create: bool,
|
2020-09-12 14:27:45 +02:00
|
|
|
) -> Result<Address> {
|
2020-09-12 22:50:14 +02:00
|
|
|
match parent.clone() {
|
|
|
|
Some(address) => trace!("FETCHING/CREATING {}/{:#}", address, directory),
|
|
|
|
None => trace!("FETCHING/CREATING /{:#}", directory),
|
|
|
|
}
|
|
|
|
|
2020-09-12 14:27:45 +02:00
|
|
|
let dir_value = EntryValue::Value(Value::String(directory.name));
|
2020-09-15 19:26:47 +02:00
|
|
|
let directories: Vec<Address> = query_entries(
|
|
|
|
connection,
|
|
|
|
EntryQuery {
|
2020-09-29 00:30:00 +02:00
|
|
|
target: QueryComponent::Any,
|
|
|
|
key: QueryComponent::Exact(String::from(DIR_KEY)),
|
|
|
|
value: QueryComponent::Exact(dir_value.clone()),
|
2020-09-15 19:26:47 +02:00
|
|
|
},
|
|
|
|
)?
|
|
|
|
.into_iter()
|
|
|
|
.map(|e: Entry| e.target)
|
|
|
|
.collect();
|
2020-09-12 14:27:45 +02:00
|
|
|
|
|
|
|
let valid_directories: Vec<Address> = match parent.clone() {
|
|
|
|
Some(address) => {
|
2020-09-15 19:26:47 +02:00
|
|
|
let parent_has: Vec<Address> = query_entries(
|
|
|
|
connection,
|
|
|
|
EntryQuery {
|
2020-09-29 00:30:00 +02:00
|
|
|
target: QueryComponent::Exact(address),
|
|
|
|
key: QueryComponent::Exact(String::from(DIR_HAS_KEY)),
|
|
|
|
value: QueryComponent::Any,
|
2020-09-15 19:26:47 +02:00
|
|
|
},
|
|
|
|
)?
|
|
|
|
.extract_addresses();
|
2020-09-12 14:27:45 +02:00
|
|
|
|
2020-09-14 01:16:01 +02:00
|
|
|
directories
|
2020-09-12 14:27:45 +02:00
|
|
|
.into_iter()
|
|
|
|
.filter(|a| parent_has.contains(a))
|
2020-09-14 01:16:01 +02:00
|
|
|
.collect()
|
2020-09-12 14:27:45 +02:00
|
|
|
}
|
|
|
|
None => directories,
|
|
|
|
};
|
|
|
|
|
|
|
|
match valid_directories.len() {
|
|
|
|
0 => {
|
2020-09-13 20:10:18 +02:00
|
|
|
if create {
|
|
|
|
let new_directory_address = Address::UUID(Uuid::new_v4());
|
|
|
|
let directory_entry = InnerEntry {
|
|
|
|
target: new_directory_address.clone(),
|
|
|
|
key: String::from(DIR_KEY),
|
|
|
|
value: dir_value,
|
2020-09-12 14:27:45 +02:00
|
|
|
};
|
2020-09-15 19:26:47 +02:00
|
|
|
let _ = insert_entry(connection, directory_entry)?;
|
2020-09-13 20:10:18 +02:00
|
|
|
|
2020-09-20 19:32:28 +02:00
|
|
|
if let Some(parent_addr) = parent {
|
2020-09-13 20:10:18 +02:00
|
|
|
let has_entry = InnerEntry {
|
2020-09-20 19:32:28 +02:00
|
|
|
target: parent_addr,
|
2020-09-13 20:10:18 +02:00
|
|
|
key: String::from(DIR_HAS_KEY),
|
|
|
|
value: EntryValue::Address(new_directory_address.clone()),
|
|
|
|
};
|
2020-09-15 19:26:47 +02:00
|
|
|
let _ = insert_entry(connection, has_entry)?;
|
2020-09-13 20:10:18 +02:00
|
|
|
}
|
2020-09-12 14:27:45 +02:00
|
|
|
|
2020-09-13 20:10:18 +02:00
|
|
|
Ok(new_directory_address)
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("Directory does not exist."))
|
|
|
|
}
|
2020-09-12 14:27:45 +02:00
|
|
|
}
|
|
|
|
1 => Ok(valid_directories[0].clone()),
|
|
|
|
_ => Err(anyhow!(
|
|
|
|
"Invalid database state - more than one directory matches the query!"
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 19:28:44 +02:00
|
|
|
pub fn resolve_path<C: Connection<Backend = Sqlite>>(
|
2020-09-15 19:26:47 +02:00
|
|
|
connection: &C,
|
2020-09-12 22:50:14 +02:00
|
|
|
path: &UPath,
|
2020-09-13 20:10:18 +02:00
|
|
|
create: bool,
|
2020-09-12 22:50:14 +02:00
|
|
|
) -> Result<Vec<Address>> {
|
|
|
|
let mut result: Vec<Address> = vec![];
|
|
|
|
let mut path_stack = path.0.to_vec();
|
|
|
|
|
|
|
|
path_stack.reverse();
|
2020-09-14 01:16:01 +02:00
|
|
|
while !path_stack.is_empty() {
|
2020-09-12 22:50:14 +02:00
|
|
|
let dir_address = fetch_or_create_dir(
|
2020-09-15 19:26:47 +02:00
|
|
|
connection,
|
2020-09-12 22:50:14 +02:00
|
|
|
result.last().cloned(),
|
|
|
|
path_stack.pop().unwrap(),
|
2020-09-13 20:10:18 +02:00
|
|
|
create,
|
2020-09-20 19:28:44 +02:00
|
|
|
)?;
|
2020-09-12 22:50:14 +02:00
|
|
|
result.push(dir_address);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
2020-09-12 14:27:45 +02:00
|
|
|
}
|
2020-09-25 02:45:17 +02:00
|
|
|
|
2020-09-20 19:28:44 +02:00
|
|
|
pub async fn reimport_directory(pool: DbPool, directory: PathBuf) {
|
2020-09-20 20:14:05 +02:00
|
|
|
let result = actix_web::web::block(move || _reimport_directory(pool, directory)).await;
|
2020-09-20 16:29:16 +02:00
|
|
|
if result.is_err() {
|
2020-09-20 19:28:44 +02:00
|
|
|
let err = result.err().unwrap();
|
|
|
|
error!("Update did not succeed! {}", err);
|
2020-09-20 16:29:16 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 02:45:17 +02:00
|
|
|
|
2020-09-22 00:41:59 +02:00
|
|
|
fn _reimport_directory<T: AsRef<Path>>(pool: DbPool, directory: T) -> Result<Vec<Result<()>>> {
|
2020-09-23 23:11:50 +02:00
|
|
|
let start = Instant::now();
|
|
|
|
|
2020-09-20 19:28:44 +02:00
|
|
|
let path_entries: Vec<PathBuf> = WalkDir::new(&directory)
|
2020-08-27 00:11:50 +02:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|e| e.ok())
|
2020-09-22 00:41:59 +02:00
|
|
|
.filter(|e| e.path().is_file() && e.file_name() != DATABASE_FILENAME)
|
2020-09-20 19:42:53 +02:00
|
|
|
.map(|e| fs::canonicalize(e.into_path()).unwrap())
|
2020-09-13 20:43:45 +02:00
|
|
|
.collect();
|
|
|
|
|
2020-09-22 00:41:59 +02:00
|
|
|
let rw_pool = Arc::new(RwLock::new(pool.clone()));
|
2020-09-20 16:29:16 +02:00
|
|
|
let absolute_path = fs::canonicalize(&directory)?;
|
2020-09-22 00:41:59 +02:00
|
|
|
let existing_files = Arc::new(RwLock::new(retrieve_all_files(&pool.get()?)?));
|
|
|
|
|
|
|
|
let path_results: Vec<Result<()>> = path_entries
|
2020-09-20 19:28:44 +02:00
|
|
|
.into_par_iter()
|
2020-09-22 00:41:59 +02:00
|
|
|
.map(|path| {
|
|
|
|
Ok(_process_directory_entry(
|
|
|
|
&rw_pool,
|
|
|
|
path,
|
|
|
|
&absolute_path,
|
|
|
|
&existing_files,
|
|
|
|
)?)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let cleanup_results: Vec<Result<()>> = existing_files
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.filter(|f| f.valid)
|
|
|
|
.map(|file| {
|
|
|
|
let connection = pool.get()?;
|
|
|
|
connection.transaction::<_, Error, _>(|| {
|
|
|
|
let _ = file_set_valid(&connection, file.id, false)?;
|
|
|
|
// remove_object(&connection, )?
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-09-23 23:11:50 +02:00
|
|
|
info!(
|
|
|
|
"Finished updating {}. Took {}s.",
|
|
|
|
directory.as_ref().display(),
|
|
|
|
start.elapsed().as_secs()
|
|
|
|
);
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2020-09-22 00:41:59 +02:00
|
|
|
Ok(path_results
|
|
|
|
.into_iter()
|
|
|
|
.chain(cleanup_results.into_iter())
|
|
|
|
.collect())
|
2020-08-27 00:11:50 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 20:14:05 +02:00
|
|
|
fn _process_directory_entry<P: AsRef<Path>>(
|
2020-09-22 00:41:59 +02:00
|
|
|
db_pool: &Arc<RwLock<DbPool>>,
|
2020-09-20 16:29:16 +02:00
|
|
|
path: PathBuf,
|
|
|
|
directory_path: &P,
|
2020-09-22 00:41:59 +02:00
|
|
|
existing_files: &Arc<RwLock<Vec<File>>>,
|
2020-09-20 16:29:16 +02:00
|
|
|
) -> Result<()> {
|
|
|
|
info!("Processing: {:?}", path);
|
|
|
|
|
2020-09-22 00:41:59 +02:00
|
|
|
let db_pool = Arc::clone(&db_pool);
|
|
|
|
let existing_files = Arc::clone(&existing_files);
|
|
|
|
|
|
|
|
let normalized_path = path.strip_prefix(&directory_path)?;
|
|
|
|
let normalized_path_str = normalized_path.to_str().expect("path not valid unicode?!");
|
2020-09-20 20:14:05 +02:00
|
|
|
|
2020-09-20 16:29:16 +02:00
|
|
|
let metadata = fs::metadata(&path)?;
|
|
|
|
let size = metadata.len() as i64;
|
|
|
|
if size < 0 {
|
|
|
|
panic!("File {} too large?!", path.display());
|
2020-08-30 22:11:32 +02:00
|
|
|
}
|
2020-09-22 00:41:59 +02:00
|
|
|
let mtime = metadata
|
|
|
|
.modified()
|
|
|
|
.map(|t| {
|
|
|
|
NaiveDateTime::from_timestamp(t.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64, 0)
|
|
|
|
})
|
|
|
|
.ok();
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut existing_files = existing_files.write().unwrap();
|
|
|
|
|
|
|
|
let maybe_existing_file = existing_files
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, file)| file.path == normalized_path_str)
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
if let Some((idx, existing_file)) = maybe_existing_file {
|
|
|
|
if size == existing_file.size && mtime == existing_file.mtime {
|
|
|
|
if !existing_file.valid {
|
|
|
|
let _ =
|
|
|
|
file_set_valid(&db_pool.write().unwrap().get()?, existing_file.id, true)?;
|
|
|
|
}
|
|
|
|
existing_files.remove(idx);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 16:29:16 +02:00
|
|
|
|
2020-09-20 19:28:44 +02:00
|
|
|
let digest = path.hash()?;
|
2020-09-20 16:29:16 +02:00
|
|
|
|
|
|
|
let new_file = models::NewFile {
|
2020-09-22 00:41:59 +02:00
|
|
|
path: normalized_path_str.to_string(),
|
2020-09-20 16:29:16 +02:00
|
|
|
hash: (digest.clone()).0,
|
2020-09-22 00:41:59 +02:00
|
|
|
added: NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0),
|
2020-09-20 16:29:16 +02:00
|
|
|
size,
|
2020-09-22 00:41:59 +02:00
|
|
|
mtime,
|
2020-09-20 16:29:16 +02:00
|
|
|
};
|
|
|
|
|
2020-09-22 00:41:59 +02:00
|
|
|
let _ = insert_file(&db_pool.write().unwrap().get()?, new_file)?;
|
2020-09-20 16:29:16 +02:00
|
|
|
|
2020-09-22 00:41:59 +02:00
|
|
|
let components = normalized_path.components().collect::<Vec<Component>>();
|
2020-09-20 16:29:16 +02:00
|
|
|
let (filename, dir_path) = components.split_last().unwrap();
|
|
|
|
|
|
|
|
let upath = UPath(
|
|
|
|
iter::once(UDirectory {
|
|
|
|
name: "NATIVE".to_string(),
|
|
|
|
})
|
|
|
|
.chain(dir_path.iter().map(|component| UDirectory {
|
|
|
|
name: component.as_os_str().to_string_lossy().to_string(),
|
|
|
|
}))
|
|
|
|
.collect(),
|
|
|
|
);
|
2020-09-22 00:41:59 +02:00
|
|
|
let resolved_path = resolve_path(&db_pool.write().unwrap().get()?, &upath, true)?;
|
2020-09-20 16:29:16 +02:00
|
|
|
let parent_dir = resolved_path.last().unwrap();
|
|
|
|
|
2020-09-22 00:41:59 +02:00
|
|
|
let _pool = &db_pool.write().unwrap();
|
2020-09-20 20:14:05 +02:00
|
|
|
let connection = _pool.get()?;
|
2020-09-20 17:17:43 +02:00
|
|
|
connection.transaction::<_, Error, _>(|| {
|
|
|
|
let file_address = Address::UUID(Uuid::new_v4());
|
|
|
|
|
|
|
|
let name_entry = InnerEntry {
|
|
|
|
target: file_address.clone(),
|
|
|
|
key: FILENAME_KEY.to_string(),
|
|
|
|
value: EntryValue::Value(Value::String(
|
|
|
|
filename.as_os_str().to_string_lossy().to_string(),
|
|
|
|
)),
|
|
|
|
};
|
2020-09-20 20:14:05 +02:00
|
|
|
let _ = insert_entry(&connection, name_entry)?;
|
2020-09-20 17:17:43 +02:00
|
|
|
|
|
|
|
let identity_entry = InnerEntry {
|
|
|
|
target: file_address.clone(),
|
|
|
|
key: FILE_IDENTITY_KEY.to_string(),
|
|
|
|
value: EntryValue::Address(Address::Hash(digest.clone())),
|
|
|
|
};
|
|
|
|
|
2020-09-20 20:14:05 +02:00
|
|
|
let _ = insert_entry(&connection, identity_entry)?;
|
2020-09-20 17:17:43 +02:00
|
|
|
|
|
|
|
let dir_has_entry = InnerEntry {
|
|
|
|
target: parent_dir.clone(),
|
|
|
|
key: DIR_HAS_KEY.to_string(),
|
|
|
|
value: EntryValue::Address(file_address),
|
|
|
|
};
|
2020-09-20 20:14:05 +02:00
|
|
|
let _ = insert_entry(&connection, dir_has_entry)?;
|
2020-09-20 17:17:43 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
2020-08-30 22:11:32 +02:00
|
|
|
}
|
2020-09-12 14:27:45 +02:00
|
|
|
|
2020-09-29 00:30:00 +02:00
|
|
|
pub fn lookup_by_filename<C: Connection<Backend = Sqlite>>(
|
|
|
|
connection: &C,
|
|
|
|
filename: String,
|
|
|
|
) -> Result<Vec<Entry>> {
|
|
|
|
let dir_value = EntryValue::Value(Value::String(filename));
|
|
|
|
let entity_addresses = query_entries(
|
|
|
|
connection,
|
|
|
|
EntryQuery {
|
|
|
|
target: QueryComponent::Any,
|
|
|
|
key: QueryComponent::In(vec![DIR_KEY.to_string(), FILENAME_KEY.to_string()]), // ?
|
|
|
|
value: QueryComponent::ILike(dir_value),
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
bulk_retrieve_objects(
|
|
|
|
connection,
|
|
|
|
entity_addresses.into_iter().map(|e| e.target).collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-12 14:27:45 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::filesystem::{UDirectory, UPath};
|
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_path_codec() {
|
|
|
|
let path = UPath(vec![
|
|
|
|
UDirectory {
|
|
|
|
name: "top".to_string(),
|
|
|
|
},
|
|
|
|
UDirectory {
|
|
|
|
name: "foo".to_string(),
|
|
|
|
},
|
|
|
|
UDirectory {
|
|
|
|
name: "bar".to_string(),
|
|
|
|
},
|
|
|
|
UDirectory {
|
|
|
|
name: "baz".to_string(),
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
let str_path = path.to_string();
|
|
|
|
assert!(str_path.len() > 0);
|
|
|
|
|
|
|
|
let decoded_path: Result<UPath> = str_path.parse();
|
|
|
|
assert!(decoded_path.is_ok());
|
|
|
|
|
|
|
|
assert_eq!(path, decoded_path.unwrap());
|
|
|
|
}
|
2020-09-13 19:20:32 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_validation() {
|
|
|
|
let invalid_path: Result<UPath> = "a//b/c//d/e/f///g".parse();
|
|
|
|
assert!(invalid_path.is_err())
|
|
|
|
}
|
2020-09-12 14:27:45 +02:00
|
|
|
}
|