2021-12-23 11:10:16 +01:00
|
|
|
use std::borrow::Borrow;
|
2021-08-18 11:06:36 +02:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::path::{Component, Path, PathBuf};
|
2021-12-04 18:33:40 +01:00
|
|
|
use std::sync::{Arc, Mutex, RwLock};
|
2021-08-18 11:06:36 +02:00
|
|
|
use std::time::{Instant, UNIX_EPOCH};
|
|
|
|
use std::{fs, iter};
|
|
|
|
|
2021-03-20 16:49:01 +01:00
|
|
|
use crate::addressing::Address;
|
2021-07-26 21:00:05 +02:00
|
|
|
use crate::database::constants::{
|
2021-12-02 21:30:11 +01:00
|
|
|
HIER_HAS_ATTR, IS_OF_TYPE_ATTR, TYPE_ADDR, TYPE_BASE_ATTR, TYPE_HAS_ATTR,
|
2021-07-26 21:00:05 +02:00
|
|
|
};
|
|
|
|
use crate::database::entry::{Entry, EntryValue, InvariantEntry};
|
2021-12-23 11:10:16 +01:00
|
|
|
use crate::database::hierarchies::{resolve_path_cached, ResolveCache, UHierPath, UNode};
|
2021-07-26 21:14:12 +02:00
|
|
|
use crate::database::inner::models;
|
2021-12-23 11:10:16 +01:00
|
|
|
use crate::database::{UpEndConnection, UpEndDatabase, UPEND_SUBDIR};
|
2021-12-04 16:45:06 +01:00
|
|
|
use crate::util::hash::{Hash, Hashable};
|
2021-07-26 21:14:12 +02:00
|
|
|
use crate::util::jobs::{Job, JobContainer, JobId, State};
|
2021-12-04 16:45:06 +01:00
|
|
|
use anyhow::{Error, Result};
|
2021-03-18 22:42:03 +01:00
|
|
|
use chrono::prelude::*;
|
2021-12-04 21:48:20 +01:00
|
|
|
use log::{debug, error, info, warn};
|
2021-12-04 18:33:40 +01:00
|
|
|
use lru::LruCache;
|
2021-03-18 22:42:03 +01:00
|
|
|
use rayon::prelude::*;
|
|
|
|
use serde_json::Value;
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
2021-06-04 15:14:58 +02:00
|
|
|
const BLOB_TYPE: &str = "BLOB";
|
2021-10-29 18:05:03 +02:00
|
|
|
const ALIAS_KEY: &str = "ALIAS";
|
2021-03-22 23:07:39 +01:00
|
|
|
const FILE_MIME_KEY: &str = "FILE_MIME";
|
2021-03-25 21:29:49 +01:00
|
|
|
const FILE_MTIME_KEY: &str = "FILE_MTIME";
|
|
|
|
const FILE_SIZE_KEY: &str = "FILE_SIZE";
|
2021-12-02 21:30:11 +01:00
|
|
|
|
2021-03-15 22:32:04 +01:00
|
|
|
lazy_static! {
|
2021-06-04 15:14:58 +02:00
|
|
|
static ref BLOB_TYPE_INVARIANT: InvariantEntry = InvariantEntry {
|
2021-12-02 21:30:11 +01:00
|
|
|
attribute: String::from(TYPE_BASE_ATTR),
|
2021-06-04 15:14:58 +02:00
|
|
|
value: EntryValue::Value(Value::from(BLOB_TYPE)),
|
|
|
|
};
|
|
|
|
static ref BLOB_TYPE_ADDR: Address = BLOB_TYPE_INVARIANT.entity().unwrap();
|
2021-03-15 22:32:04 +01:00
|
|
|
}
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2021-12-23 11:10:16 +01:00
|
|
|
fn initialize_types(connection: &UpEndConnection) -> Result<()> {
|
2021-06-04 15:14:58 +02:00
|
|
|
// BLOB_TYPE
|
2021-12-23 11:10:16 +01:00
|
|
|
connection.insert_entry(Entry::try_from(&*BLOB_TYPE_INVARIANT)?)?;
|
|
|
|
upend_insert_addr!(&connection, BLOB_TYPE_ADDR, IS_OF_TYPE_ATTR, TYPE_ADDR);
|
|
|
|
upend_insert_val!(&connection, BLOB_TYPE_ADDR, TYPE_HAS_ATTR, FILE_MTIME_KEY);
|
|
|
|
upend_insert_val!(&connection, BLOB_TYPE_ADDR, TYPE_HAS_ATTR, FILE_SIZE_KEY);
|
|
|
|
upend_insert_val!(&connection, BLOB_TYPE_ADDR, TYPE_HAS_ATTR, FILE_MIME_KEY);
|
2021-06-04 15:14:58 +02:00
|
|
|
|
2021-03-18 23:59:55 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-23 11:10:16 +01:00
|
|
|
pub async fn rescan_vault(db: Arc<UpEndDatabase>, job_container: Arc<RwLock<JobContainer>>) {
|
2021-02-20 17:36:19 +01:00
|
|
|
let job_id = job_container
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.add_job(Job::new("REIMPORT", "Reimporting vault..."))
|
|
|
|
.unwrap();
|
|
|
|
|
2021-06-20 16:46:45 +02:00
|
|
|
let job_container_rescan = job_container.clone();
|
2021-02-20 17:36:19 +01:00
|
|
|
let result =
|
2021-12-23 11:10:16 +01:00
|
|
|
actix_web::web::block(move || _rescan_vault(db, job_container_rescan, job_id)).await;
|
2021-06-20 16:46:45 +02:00
|
|
|
|
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();
|
2021-02-07 20:18:55 +01:00
|
|
|
error!("Update did not succeed! {:?}", err);
|
2021-06-20 16:46:45 +02:00
|
|
|
|
|
|
|
job_container
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.update_state(&job_id, State::Failed)
|
|
|
|
.unwrap();
|
2020-09-20 16:29:16 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-23 11:10:16 +01:00
|
|
|
struct PragmaSynchronousGuard<'a>(&'a UpEndConnection);
|
2021-12-05 23:31:19 +01:00
|
|
|
|
|
|
|
impl Drop for PragmaSynchronousGuard<'_> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
debug!("Re-enabling synchronous mode.");
|
2021-12-23 11:10:16 +01:00
|
|
|
let res = self.0.execute("PRAGMA synchronous = NORMAL;");
|
2021-12-05 23:31:19 +01:00
|
|
|
if let Err(err) = res {
|
|
|
|
error!(
|
|
|
|
"Error setting synchronous mode back to NORMAL! Data loss possible! {}",
|
|
|
|
err
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-25 02:45:17 +02:00
|
|
|
|
2020-09-30 01:31:48 +02:00
|
|
|
type UpdatePathResult = Result<UpdatePathOutcome>;
|
|
|
|
|
2021-12-05 13:22:13 +01:00
|
|
|
#[derive(Debug)]
|
2020-09-30 01:31:48 +02:00
|
|
|
enum UpdatePathOutcome {
|
|
|
|
Added(PathBuf),
|
|
|
|
Unchanged(PathBuf),
|
|
|
|
Removed(PathBuf),
|
2021-12-05 13:22:13 +01:00
|
|
|
Failed(PathBuf, Error),
|
2020-09-30 01:31:48 +02:00
|
|
|
}
|
|
|
|
|
2021-12-23 11:10:16 +01:00
|
|
|
fn _rescan_vault<D: Borrow<UpEndDatabase>>(
|
|
|
|
db: D,
|
2021-02-20 17:36:19 +01:00
|
|
|
job_container: Arc<RwLock<JobContainer>>,
|
|
|
|
job_id: JobId,
|
2021-12-05 13:22:13 +01:00
|
|
|
) -> Result<Vec<UpdatePathOutcome>> {
|
2020-09-23 23:11:50 +02:00
|
|
|
let start = Instant::now();
|
2021-12-05 12:10:41 +01:00
|
|
|
info!("Vault rescan started.");
|
2020-09-23 23:11:50 +02:00
|
|
|
|
2021-12-23 11:10:16 +01:00
|
|
|
let db = db.borrow();
|
|
|
|
let connection = db.connection()?;
|
|
|
|
|
2021-03-14 22:16:28 +01:00
|
|
|
// Initialize types, etc...
|
2021-12-05 12:10:41 +01:00
|
|
|
debug!("Initializing DB types.");
|
2021-12-23 11:10:16 +01:00
|
|
|
initialize_types(&connection)?;
|
2021-03-14 22:16:28 +01:00
|
|
|
|
2021-12-04 21:43:13 +01:00
|
|
|
// Disable syncing in SQLite for the duration of the import
|
2021-12-05 12:10:41 +01:00
|
|
|
debug!("Disabling SQLite synchronous mode");
|
2021-12-23 11:10:16 +01:00
|
|
|
connection.execute("PRAGMA synchronous = OFF;")?;
|
|
|
|
let _guard = PragmaSynchronousGuard(&connection);
|
2021-12-04 21:43:13 +01:00
|
|
|
|
2021-03-14 22:16:28 +01:00
|
|
|
// Walk through the vault, find all paths
|
2021-12-05 12:10:41 +01:00
|
|
|
debug!("Traversing vault directory");
|
2021-12-23 11:10:16 +01:00
|
|
|
let absolute_dir_path = fs::canonicalize(&db.vault_path)?;
|
|
|
|
let path_entries: Vec<PathBuf> = WalkDir::new(&db.vault_path)
|
2021-10-28 17:24:47 +02:00
|
|
|
.follow_links(true)
|
2020-08-27 00:11:50 +02:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|e| e.ok())
|
2020-09-20 19:42:53 +02:00
|
|
|
.map(|e| fs::canonicalize(e.into_path()).unwrap())
|
2021-12-05 13:26:10 +01:00
|
|
|
.filter(|e| e.is_file())
|
2021-12-05 12:43:43 +01:00
|
|
|
.filter(|e| !e.starts_with(&absolute_dir_path.join(UPEND_SUBDIR)))
|
2020-09-13 20:43:45 +02:00
|
|
|
.collect();
|
|
|
|
|
2021-03-14 22:16:28 +01:00
|
|
|
// Prepare for processing
|
2021-12-23 11:10:16 +01:00
|
|
|
let existing_files = Arc::new(RwLock::new(connection.retrieve_all_files()?));
|
2020-09-22 00:41:59 +02:00
|
|
|
|
2021-03-14 22:16:28 +01:00
|
|
|
// Actual processing
|
2021-02-20 17:36:19 +01:00
|
|
|
let count = RwLock::new(0_usize);
|
2021-12-04 18:33:40 +01:00
|
|
|
let resolve_cache = Arc::new(Mutex::new(LruCache::new(256)));
|
2021-02-20 17:36:19 +01:00
|
|
|
let total = path_entries.len() as f32;
|
2021-12-05 13:22:13 +01:00
|
|
|
let path_outcomes: Vec<UpdatePathOutcome> = path_entries
|
2020-09-20 19:28:44 +02:00
|
|
|
.into_par_iter()
|
2020-09-22 00:41:59 +02:00
|
|
|
.map(|path| {
|
2021-12-04 18:33:40 +01:00
|
|
|
let result = _process_directory_entry(
|
2021-12-23 11:10:16 +01:00
|
|
|
db.connection().unwrap(),
|
2021-12-04 18:33:40 +01:00
|
|
|
&resolve_cache,
|
2021-12-05 13:22:13 +01:00
|
|
|
path.clone(),
|
2021-12-05 12:43:43 +01:00
|
|
|
&absolute_dir_path,
|
2021-12-04 18:33:40 +01:00
|
|
|
&existing_files,
|
2021-12-05 13:22:13 +01:00
|
|
|
);
|
2021-02-20 17:36:19 +01:00
|
|
|
|
|
|
|
let mut cnt = count.write().unwrap();
|
|
|
|
*cnt += 1;
|
|
|
|
|
|
|
|
job_container
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.update_progress(&job_id, *cnt as f32 / total * 100.0)
|
|
|
|
.unwrap();
|
|
|
|
|
2021-12-05 13:22:13 +01:00
|
|
|
match result {
|
|
|
|
Ok(result) => result,
|
|
|
|
Err(error) => UpdatePathOutcome::Failed(path, error),
|
|
|
|
}
|
2020-09-22 00:41:59 +02:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-12-05 23:31:19 +01:00
|
|
|
debug!("Processing done, cleaning up...");
|
|
|
|
|
2021-10-28 17:34:01 +02:00
|
|
|
let existing_files = existing_files.read().unwrap();
|
|
|
|
|
|
|
|
let cleanup_results = existing_files.iter().filter(|f| f.valid).map(|file| {
|
2021-12-05 13:22:13 +01:00
|
|
|
let trans_result = connection.transaction::<_, Error, _>(|| {
|
2021-12-23 11:10:16 +01:00
|
|
|
connection.file_set_valid(file.id, false)?;
|
2021-10-28 17:34:01 +02:00
|
|
|
// remove_object(&connection, )?
|
2021-12-05 13:22:13 +01:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
match trans_result {
|
|
|
|
Ok(_) => {
|
|
|
|
info!("Removed: {:?}", file.path);
|
|
|
|
UpdatePathOutcome::Removed(PathBuf::from(file.path.clone()))
|
|
|
|
}
|
|
|
|
Err(error) => UpdatePathOutcome::Failed(PathBuf::from(file.path.clone()), error),
|
|
|
|
}
|
2021-10-28 17:34:01 +02:00
|
|
|
});
|
2020-09-22 00:41:59 +02:00
|
|
|
|
2021-12-05 13:22:13 +01:00
|
|
|
let mut failed: Vec<(&PathBuf, &Error)> = vec![];
|
2021-03-20 16:49:01 +01:00
|
|
|
let mut created = 0;
|
|
|
|
let mut unchanged = 0;
|
|
|
|
let mut deleted = 0;
|
|
|
|
|
2021-12-05 13:22:13 +01:00
|
|
|
for outcome in &path_outcomes {
|
|
|
|
match outcome {
|
|
|
|
UpdatePathOutcome::Added(_) => created += 1,
|
|
|
|
UpdatePathOutcome::Unchanged(_) => unchanged += 1,
|
|
|
|
UpdatePathOutcome::Removed(_) => deleted += 1,
|
|
|
|
UpdatePathOutcome::Failed(path, err) => failed.push((path, err)),
|
2021-03-20 16:49:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !failed.is_empty() {
|
|
|
|
warn!(
|
|
|
|
"{} path updates failed! ({})",
|
|
|
|
failed.len(),
|
|
|
|
failed
|
|
|
|
.iter()
|
2021-12-05 13:22:13 +01:00
|
|
|
.map(|(path, error)| format!("{:?}: {}", path, error))
|
2021-03-20 16:49:01 +01:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", ")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-04 21:43:13 +01:00
|
|
|
// Re-enable SQLite syncing
|
2021-12-05 23:31:19 +01:00
|
|
|
drop(_guard);
|
2021-12-04 21:43:13 +01:00
|
|
|
|
2020-09-23 23:11:50 +02:00
|
|
|
info!(
|
2021-12-23 11:10:16 +01:00
|
|
|
"Finished updating {:?} ({} created, {} deleted, {} left unchanged). Took {}s.",
|
|
|
|
db.vault_path,
|
2021-03-20 16:49:01 +01:00
|
|
|
created,
|
|
|
|
deleted,
|
|
|
|
unchanged,
|
2020-09-23 23:11:50 +02:00
|
|
|
start.elapsed().as_secs()
|
|
|
|
);
|
2020-08-27 00:11:50 +02:00
|
|
|
|
2021-12-05 13:22:13 +01:00
|
|
|
Ok(path_outcomes.into_iter().chain(cleanup_results).collect())
|
2020-08-27 00:11:50 +02:00
|
|
|
}
|
|
|
|
|
2020-09-20 20:14:05 +02:00
|
|
|
fn _process_directory_entry<P: AsRef<Path>>(
|
2021-12-23 11:10:16 +01:00
|
|
|
connection: UpEndConnection,
|
2021-12-04 18:33:40 +01:00
|
|
|
resolve_cache: &Arc<Mutex<ResolveCache>>,
|
2020-09-20 16:29:16 +02:00
|
|
|
path: PathBuf,
|
|
|
|
directory_path: &P,
|
2021-07-26 21:14:12 +02:00
|
|
|
existing_files: &Arc<RwLock<Vec<models::File>>>,
|
2020-09-30 01:31:48 +02:00
|
|
|
) -> UpdatePathResult {
|
2021-12-05 12:10:41 +01:00
|
|
|
debug!("Processing: {:?}", path);
|
2020-09-20 16:29:16 +02:00
|
|
|
|
2021-03-25 21:29:49 +01:00
|
|
|
// Prepare the data
|
2021-10-28 17:34:01 +02:00
|
|
|
let existing_files = Arc::clone(existing_files);
|
2020-09-22 00:41:59 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-12-04 16:45:06 +01:00
|
|
|
let mut file_hash: Option<Hash> = None;
|
2021-05-28 23:36:00 +02:00
|
|
|
|
2021-03-25 21:29:49 +01:00
|
|
|
// Get size & mtime for quick comparison
|
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();
|
|
|
|
|
2021-03-25 21:29:49 +01:00
|
|
|
// Check if the path entry for this file already exists in database
|
2021-12-04 16:45:06 +01:00
|
|
|
let existing_files_read = existing_files.read().unwrap();
|
|
|
|
let maybe_existing_file = existing_files_read
|
|
|
|
.iter()
|
2021-12-04 16:46:13 +01:00
|
|
|
.find(|file| file.path == normalized_path_str);
|
2021-12-04 16:45:06 +01:00
|
|
|
|
|
|
|
if let Some(existing_file) = maybe_existing_file {
|
|
|
|
let existing_file = existing_file.clone();
|
|
|
|
drop(existing_files_read);
|
|
|
|
|
2021-12-05 22:57:47 +01:00
|
|
|
if size == existing_file.size {
|
|
|
|
let same_mtime = mtime.is_some() && mtime == existing_file.mtime;
|
|
|
|
let mut same_hash = false;
|
2021-12-04 16:45:06 +01:00
|
|
|
|
2021-12-05 22:57:47 +01:00
|
|
|
if !same_mtime {
|
|
|
|
file_hash = Some(path.hash()?);
|
|
|
|
same_hash = file_hash.as_ref().unwrap() == &existing_file.hash;
|
2021-12-04 16:45:06 +01:00
|
|
|
}
|
|
|
|
|
2021-12-05 22:57:47 +01:00
|
|
|
if same_mtime || same_hash {
|
|
|
|
if mtime != existing_file.mtime {
|
2021-12-23 11:10:16 +01:00
|
|
|
connection.file_update_mtime(existing_file.id, mtime)?;
|
2021-12-05 22:57:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !existing_file.valid {
|
2021-12-23 11:10:16 +01:00
|
|
|
connection.file_set_valid(existing_file.id, true)?;
|
2021-12-05 22:57:47 +01:00
|
|
|
}
|
|
|
|
|
2021-12-04 16:45:06 +01:00
|
|
|
let mut existing_files_write = existing_files.write().unwrap();
|
|
|
|
let maybe_existing_file = existing_files_write
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, file)| file.path == normalized_path_str)
|
|
|
|
.map(|(idx, _)| idx);
|
|
|
|
|
|
|
|
if let Some(idx) = maybe_existing_file {
|
|
|
|
existing_files_write.swap_remove(idx);
|
2021-12-05 12:10:41 +01:00
|
|
|
debug!("Unchanged: {:?}", path);
|
2021-12-04 16:45:06 +01:00
|
|
|
return Ok(UpdatePathOutcome::Unchanged(path));
|
2020-09-22 00:41:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-04 16:45:06 +01:00
|
|
|
} else {
|
|
|
|
drop(existing_files_read);
|
2020-09-22 00:41:59 +02:00
|
|
|
}
|
2020-09-20 16:29:16 +02:00
|
|
|
|
2021-03-25 21:29:49 +01:00
|
|
|
// If not, add it!
|
2021-12-04 16:45:06 +01:00
|
|
|
if file_hash.is_none() {
|
|
|
|
file_hash = Some(path.hash()?);
|
2021-05-28 23:36:00 +02:00
|
|
|
}
|
2021-12-04 16:45:06 +01:00
|
|
|
let file_hash = file_hash.unwrap();
|
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(),
|
2021-10-29 18:05:03 +02:00
|
|
|
hash: (file_hash.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
|
|
|
};
|
|
|
|
|
2021-03-25 21:29:49 +01:00
|
|
|
// Insert metadata
|
2021-06-04 15:14:58 +02:00
|
|
|
let type_entry = Entry {
|
2021-10-29 18:05:03 +02:00
|
|
|
entity: Address::Hash(file_hash.clone()),
|
2021-06-04 15:14:58 +02:00
|
|
|
attribute: String::from(IS_OF_TYPE_ATTR),
|
|
|
|
value: EntryValue::Address(BLOB_TYPE_ADDR.clone()),
|
|
|
|
};
|
|
|
|
|
2021-03-25 21:29:49 +01:00
|
|
|
let size_entry = Entry {
|
2021-10-29 18:05:03 +02:00
|
|
|
entity: Address::Hash(file_hash.clone()),
|
2021-03-25 21:29:49 +01:00
|
|
|
attribute: FILE_SIZE_KEY.to_string(),
|
|
|
|
value: EntryValue::Value(Value::from(size)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mime_entry = Entry {
|
2021-10-29 18:05:03 +02:00
|
|
|
entity: Address::Hash(file_hash.clone()),
|
2021-03-25 21:29:49 +01:00
|
|
|
attribute: FILE_MIME_KEY.to_string(),
|
|
|
|
value: EntryValue::Value(Value::String(tree_magic::from_filepath(&path))),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Finally, add the appropriate entries w/r/t virtual filesystem location
|
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();
|
|
|
|
|
2021-12-17 23:04:35 +01:00
|
|
|
let upath = UHierPath(
|
2021-08-18 11:06:37 +02:00
|
|
|
iter::once(UNode::new("NATIVE").unwrap())
|
|
|
|
.chain(dir_path.iter().map(|component| {
|
|
|
|
UNode::new(component.as_os_str().to_string_lossy().to_string()).unwrap()
|
|
|
|
}))
|
2021-08-18 11:06:36 +02:00
|
|
|
.collect(),
|
2020-09-20 16:29:16 +02:00
|
|
|
);
|
2021-12-23 11:10:16 +01:00
|
|
|
let resolved_path = resolve_path_cached(&connection, &upath, true, resolve_cache)?;
|
2020-09-20 16:29:16 +02:00
|
|
|
let parent_dir = resolved_path.last().unwrap();
|
|
|
|
|
2020-09-20 17:17:43 +02:00
|
|
|
connection.transaction::<_, Error, _>(|| {
|
2021-12-23 11:10:16 +01:00
|
|
|
connection.insert_file(new_file)?;
|
2021-12-04 18:49:11 +01:00
|
|
|
|
2021-12-23 11:10:16 +01:00
|
|
|
connection.insert_entry(type_entry)?;
|
|
|
|
connection.insert_entry(size_entry)?;
|
|
|
|
connection.insert_entry(mime_entry)?;
|
2021-12-04 18:49:11 +01:00
|
|
|
|
2021-10-29 18:05:03 +02:00
|
|
|
let dir_has_entry = Entry {
|
|
|
|
entity: parent_dir.clone(),
|
|
|
|
attribute: HIER_HAS_ATTR.to_string(),
|
|
|
|
value: EntryValue::Address(Address::Hash(file_hash.clone())),
|
2021-03-14 22:16:28 +01:00
|
|
|
};
|
2021-12-23 11:10:16 +01:00
|
|
|
let dir_has_entry_addr = connection.insert_entry(dir_has_entry)?;
|
2020-09-20 17:17:43 +02:00
|
|
|
|
2021-02-19 20:27:30 +01:00
|
|
|
let name_entry = Entry {
|
2021-10-29 18:05:03 +02:00
|
|
|
entity: dir_has_entry_addr,
|
|
|
|
attribute: ALIAS_KEY.to_string(),
|
2020-09-20 17:17:43 +02:00
|
|
|
value: EntryValue::Value(Value::String(
|
|
|
|
filename.as_os_str().to_string_lossy().to_string(),
|
|
|
|
)),
|
|
|
|
};
|
2021-12-23 11:10:16 +01:00
|
|
|
connection.insert_entry(name_entry)?;
|
2020-09-20 17:17:43 +02:00
|
|
|
|
2021-12-05 12:10:41 +01:00
|
|
|
info!("Added: {:?}", path);
|
2020-09-30 01:31:48 +02:00
|
|
|
Ok(UpdatePathOutcome::Added(path.clone()))
|
2020-09-20 17:17:43 +02:00
|
|
|
})
|
2020-08-30 22:11:32 +02:00
|
|
|
}
|
2021-10-24 13:37:36 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2021-12-23 11:10:16 +01:00
|
|
|
use crate::database::UpEndDatabase;
|
2021-10-24 13:37:36 +02:00
|
|
|
use crate::util;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Write;
|
|
|
|
use tempdir::TempDir;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rescan() {
|
|
|
|
// Prepare temporary filesystem structure
|
|
|
|
let temp_dir = TempDir::new("upend-test").unwrap();
|
|
|
|
|
|
|
|
let file_path = temp_dir.path().join("my-temporary-note.txt");
|
|
|
|
let mut tmp_file = File::create(file_path).unwrap();
|
|
|
|
writeln!(tmp_file, "Brian was here. Briefly.").unwrap();
|
|
|
|
|
|
|
|
let file_path = temp_dir.path().join("hello-world.txt");
|
|
|
|
let mut tmp_file = File::create(file_path).unwrap();
|
|
|
|
writeln!(tmp_file, "Hello, World!").unwrap();
|
|
|
|
|
|
|
|
let file_path = temp_dir.path().join("empty");
|
|
|
|
File::create(file_path).unwrap();
|
|
|
|
|
|
|
|
// Initialize database
|
|
|
|
|
2021-12-23 11:10:16 +01:00
|
|
|
let open_result = UpEndDatabase::open(&temp_dir, None, true).unwrap();
|
2021-10-24 13:37:36 +02:00
|
|
|
let job_container = Arc::new(RwLock::new(util::jobs::JobContainer::default()));
|
|
|
|
let job_id = job_container
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.add_job(util::jobs::Job::new("RESCAN", "TEST JOB"))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Initial scan
|
2021-12-23 11:10:16 +01:00
|
|
|
let rescan_result = _rescan_vault(&open_result.db, job_container.clone(), job_id);
|
2021-10-24 13:37:36 +02:00
|
|
|
|
|
|
|
assert!(rescan_result.is_ok());
|
|
|
|
let rescan_result = rescan_result.unwrap();
|
|
|
|
assert_eq!(rescan_result.len(), 3);
|
2021-12-05 13:22:13 +01:00
|
|
|
rescan_result
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|outcome| assert!(matches!(outcome, UpdatePathOutcome::Added(_))));
|
2021-10-24 13:37:36 +02:00
|
|
|
|
|
|
|
// Modification-less rescan
|
|
|
|
|
2021-12-23 11:10:16 +01:00
|
|
|
let rescan_result = _rescan_vault(&open_result.db, job_container.clone(), job_id);
|
2021-10-24 13:37:36 +02:00
|
|
|
|
|
|
|
assert!(rescan_result.is_ok());
|
|
|
|
let rescan_result = rescan_result.unwrap();
|
|
|
|
assert_eq!(rescan_result.len(), 3);
|
2021-12-05 13:22:13 +01:00
|
|
|
rescan_result
|
|
|
|
.into_iter()
|
|
|
|
.for_each(|outcome| assert!(matches!(outcome, UpdatePathOutcome::Unchanged(_))));
|
2021-10-24 13:37:36 +02:00
|
|
|
|
|
|
|
// Remove a file
|
|
|
|
|
|
|
|
std::fs::remove_file(temp_dir.path().join("hello-world.txt")).unwrap();
|
|
|
|
|
2021-12-23 11:10:16 +01:00
|
|
|
let rescan_result = _rescan_vault(&open_result.db, job_container, job_id);
|
2021-10-24 13:37:36 +02:00
|
|
|
|
|
|
|
assert!(rescan_result.is_ok());
|
|
|
|
let rescan_result = rescan_result.unwrap();
|
|
|
|
assert_eq!(rescan_result.len(), 3);
|
|
|
|
assert_eq!(
|
|
|
|
2,
|
|
|
|
rescan_result
|
|
|
|
.iter()
|
2021-12-05 13:22:13 +01:00
|
|
|
.filter(|upo| matches!(upo, UpdatePathOutcome::Unchanged(_)))
|
2021-10-28 17:34:01 +02:00
|
|
|
.count()
|
2021-10-24 13:37:36 +02:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
1,
|
|
|
|
rescan_result
|
|
|
|
.iter()
|
2021-12-05 13:22:13 +01:00
|
|
|
.filter(|upo| matches!(upo, UpdatePathOutcome::Removed(_)))
|
2021-10-28 17:34:01 +02:00
|
|
|
.count()
|
2021-10-24 13:37:36 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|