upend/src/filesystem.rs

444 lines
14 KiB
Rust
Raw Normal View History

use std::convert::TryFrom;
use std::path::{Component, Path, PathBuf};
2021-12-04 18:33:40 +01:00
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Instant, UNIX_EPOCH};
use std::{fs, iter};
2021-03-20 16:49:01 +01:00
use crate::addressing::Address;
use crate::database::constants::{
HIER_HAS_ATTR, IS_OF_TYPE_ATTR, TYPE_ADDR, TYPE_BASE_ATTR, TYPE_HAS_ATTR,
};
use crate::database::entry::{Entry, EntryValue, InvariantEntry};
2021-12-04 18:33:40 +01:00
use crate::database::hierarchies::{resolve_path_cached, ResolveCache, UNode, UPath};
use crate::database::inner::models;
2021-03-20 16:49:01 +01:00
use crate::database::{
file_set_valid, insert_entry, insert_file, retrieve_all_files, DbPool, DATABASE_FILENAME,
2021-03-20 16:49:01 +01:00
};
2021-12-04 16:45:06 +01:00
use crate::util::hash::{Hash, Hashable};
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::*;
use diesel::Connection;
use log::{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";
const ALIAS_KEY: &str = "ALIAS";
const FILE_MIME_KEY: &str = "FILE_MIME";
const FILE_MTIME_KEY: &str = "FILE_MTIME";
const FILE_SIZE_KEY: &str = "FILE_SIZE";
lazy_static! {
2021-06-04 15:14:58 +02:00
static ref BLOB_TYPE_INVARIANT: InvariantEntry = InvariantEntry {
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();
}
2020-08-27 00:11:50 +02:00
fn initialize_types(pool: &DbPool) -> Result<()> {
2021-06-04 15:14:58 +02:00
// BLOB_TYPE
insert_entry(&pool.get()?, Entry::try_from(&*BLOB_TYPE_INVARIANT)?)?;
upend_insert_addr!(&pool.get()?, BLOB_TYPE_ADDR, IS_OF_TYPE_ATTR, TYPE_ADDR);
2021-06-19 15:56:10 +02:00
upend_insert_val!(&pool.get()?, BLOB_TYPE_ADDR, TYPE_HAS_ATTR, FILE_MTIME_KEY);
upend_insert_val!(&pool.get()?, BLOB_TYPE_ADDR, TYPE_HAS_ATTR, FILE_SIZE_KEY);
upend_insert_val!(&pool.get()?, BLOB_TYPE_ADDR, TYPE_HAS_ATTR, FILE_MIME_KEY);
2021-06-04 15:14:58 +02:00
Ok(())
}
pub async fn rescan_vault(
pool: DbPool,
directory: PathBuf,
job_container: Arc<RwLock<JobContainer>>,
) {
let job_id = job_container
.write()
.unwrap()
.add_job(Job::new("REIMPORT", "Reimporting vault..."))
.unwrap();
let job_container_rescan = job_container.clone();
let result =
actix_web::web::block(move || _rescan_vault(pool, directory, job_container_rescan, job_id))
.await;
if result.is_err() {
2020-09-20 19:28:44 +02:00
let err = result.err().unwrap();
error!("Update did not succeed! {:?}", err);
job_container
.write()
.unwrap()
.update_state(&job_id, State::Failed)
.unwrap();
}
}
type UpdatePathResult = Result<UpdatePathOutcome>;
enum UpdatePathOutcome {
Added(PathBuf),
Unchanged(PathBuf),
Removed(PathBuf),
}
fn _rescan_vault<T: AsRef<Path>>(
pool: DbPool,
directory: T,
job_container: Arc<RwLock<JobContainer>>,
job_id: JobId,
) -> Result<Vec<UpdatePathResult>> {
2020-09-23 23:11:50 +02:00
let start = Instant::now();
2021-03-14 22:16:28 +01:00
// Initialize types, etc...
initialize_types(&pool)?;
2021-03-14 22:16:28 +01:00
// Disable syncing in SQLite for the duration of the import
pool.get()?.execute("PRAGMA synchronous = OFF;")?;
2021-03-14 22:16:28 +01:00
// Walk through the vault, find all paths
2020-09-20 19:28:44 +02:00
let path_entries: Vec<PathBuf> = WalkDir::new(&directory)
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())
.filter(|e| e.path().is_file() && e.file_name() != DATABASE_FILENAME)
.map(|e| fs::canonicalize(e.into_path()).unwrap())
.collect();
2021-03-14 22:16:28 +01:00
// Prepare for processing
let rw_pool = Arc::new(RwLock::new(pool.clone()));
let absolute_path = fs::canonicalize(&directory)?;
let existing_files = Arc::new(RwLock::new(retrieve_all_files(&pool.get()?)?));
2021-03-14 22:16:28 +01:00
// Actual processing
let count = RwLock::new(0_usize);
2021-12-04 18:33:40 +01:00
let resolve_cache = Arc::new(Mutex::new(LruCache::new(256)));
let total = path_entries.len() as f32;
let path_results: Vec<UpdatePathResult> = path_entries
2020-09-20 19:28:44 +02:00
.into_par_iter()
.map(|path| {
2021-12-04 18:33:40 +01:00
let result = _process_directory_entry(
&rw_pool,
&resolve_cache,
path,
&absolute_path,
&existing_files,
)?;
let mut cnt = count.write().unwrap();
*cnt += 1;
job_container
.write()
.unwrap()
.update_progress(&job_id, *cnt as f32 / total * 100.0)
.unwrap();
Ok(result)
})
.collect();
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| {
let connection = pool.get()?;
connection.transaction::<_, Error, _>(|| {
file_set_valid(&connection, file.id, false)?;
// remove_object(&connection, )?
Ok(UpdatePathOutcome::Removed(PathBuf::from(file.path.clone())))
})
2021-10-28 17:34:01 +02:00
});
2021-03-20 16:49:01 +01:00
let mut failed: Vec<&Error> = vec![];
let mut created = 0;
let mut unchanged = 0;
let mut deleted = 0;
for result in &path_results {
match result {
Ok(result) => match result {
UpdatePathOutcome::Added(_) => created += 1,
UpdatePathOutcome::Unchanged(_) => unchanged += 1,
UpdatePathOutcome::Removed(_) => deleted += 1,
},
Err(err) => failed.push(err),
}
}
if !failed.is_empty() {
warn!(
"{} path updates failed! ({})",
failed.len(),
failed
.iter()
.map(|e| e.to_string())
.collect::<Vec<String>>()
.join(", ")
)
}
// Re-enable SQLite syncing
pool.get()?.execute("PRAGMA synchronous = NORMAL;")?;
2020-09-23 23:11:50 +02:00
info!(
2021-03-20 16:49:01 +01:00
"Finished updating {} ({} created, {} deleted, {} left unchanged). Took {}s.",
2020-09-23 23:11:50 +02:00
directory.as_ref().display(),
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-10-28 17:34:01 +02:00
Ok(path_results.into_iter().chain(cleanup_results).collect())
2020-08-27 00:11:50 +02:00
}
fn _process_directory_entry<P: AsRef<Path>>(
db_pool: &Arc<RwLock<DbPool>>,
2021-12-04 18:33:40 +01:00
resolve_cache: &Arc<Mutex<ResolveCache>>,
path: PathBuf,
directory_path: &P,
existing_files: &Arc<RwLock<Vec<models::File>>>,
) -> UpdatePathResult {
info!("Processing: {:?}", path);
// Prepare the data
2021-12-04 16:45:06 +01:00
let connection = &db_pool.write().unwrap().get()?;
2021-10-28 17:34:01 +02:00
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?!");
2021-12-04 16:45:06 +01:00
let mut file_hash: Option<Hash> = None;
// Get size & mtime for quick comparison
let metadata = fs::metadata(&path)?;
let size = metadata.len() as i64;
if size < 0 {
panic!("File {} too large?!", path.display());
}
let mtime = metadata
.modified()
.map(|t| {
NaiveDateTime::from_timestamp(t.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64, 0)
})
.ok();
// 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);
let mut same = size == existing_file.size && mtime == existing_file.mtime;
if !same {
file_hash = Some(path.hash()?);
same = file_hash.as_ref().unwrap() == &existing_file.hash;
}
if same {
if !existing_file.valid {
file_set_valid(connection, existing_file.id, true)?;
}
{
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);
return Ok(UpdatePathOutcome::Unchanged(path));
}
}
}
2021-12-04 16:45:06 +01:00
} else {
drop(existing_files_read);
}
// If not, add it!
2021-12-04 16:45:06 +01:00
if file_hash.is_none() {
file_hash = Some(path.hash()?);
}
2021-12-04 16:45:06 +01:00
let file_hash = file_hash.unwrap();
let new_file = models::NewFile {
path: normalized_path_str.to_string(),
hash: (file_hash.clone()).0,
added: NaiveDateTime::from_timestamp(Utc::now().timestamp(), 0),
size,
mtime,
};
// Insert metadata
2021-06-04 15:14:58 +02:00
let type_entry = Entry {
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()),
};
let size_entry = Entry {
entity: Address::Hash(file_hash.clone()),
attribute: FILE_SIZE_KEY.to_string(),
value: EntryValue::Value(Value::from(size)),
};
let mime_entry = Entry {
entity: Address::Hash(file_hash.clone()),
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
let components = normalized_path.components().collect::<Vec<Component>>();
let (filename, dir_path) = components.split_last().unwrap();
let upath = UPath(
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()
}))
.collect(),
);
2021-12-04 18:33:40 +01:00
let resolved_path = resolve_path_cached(connection, &upath, true, resolve_cache)?;
let parent_dir = resolved_path.last().unwrap();
connection.transaction::<_, Error, _>(|| {
insert_file(connection, new_file)?;
insert_entry(connection, type_entry)?;
insert_entry(connection, size_entry)?;
insert_entry(connection, mime_entry)?;
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-04 16:45:06 +01:00
let dir_has_entry_addr = insert_entry(connection, dir_has_entry)?;
let name_entry = Entry {
entity: dir_has_entry_addr,
attribute: ALIAS_KEY.to_string(),
value: EntryValue::Value(Value::String(
filename.as_os_str().to_string_lossy().to_string(),
)),
};
2021-12-04 16:45:06 +01:00
insert_entry(connection, name_entry)?;
Ok(UpdatePathOutcome::Added(path.clone()))
})
}
#[cfg(test)]
mod test {
use crate::database::open_upend;
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
let open_result = open_upend(&temp_dir, None, true).unwrap();
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
let rescan_result = _rescan_vault(
open_result.pool.clone(),
temp_dir.as_ref().to_path_buf(),
job_container.clone(),
job_id,
);
assert!(rescan_result.is_ok());
let rescan_result = rescan_result.unwrap();
assert_eq!(rescan_result.len(), 3);
rescan_result.into_iter().for_each(|outcome| {
assert!(outcome.is_ok());
let outcome = outcome.unwrap();
assert!(matches!(outcome, UpdatePathOutcome::Added(_)))
});
// Modification-less rescan
let rescan_result = _rescan_vault(
open_result.pool.clone(),
temp_dir.as_ref().to_path_buf(),
job_container.clone(),
job_id,
);
assert!(rescan_result.is_ok());
let rescan_result = rescan_result.unwrap();
assert_eq!(rescan_result.len(), 3);
rescan_result.into_iter().for_each(|outcome| {
assert!(outcome.is_ok());
let outcome = outcome.unwrap();
assert!(matches!(outcome, UpdatePathOutcome::Unchanged(_)))
});
// Remove a file
std::fs::remove_file(temp_dir.path().join("hello-world.txt")).unwrap();
let rescan_result = _rescan_vault(
open_result.pool,
temp_dir.as_ref().to_path_buf(),
job_container,
job_id,
);
assert!(rescan_result.is_ok());
let rescan_result = rescan_result.unwrap();
assert_eq!(rescan_result.len(), 3);
assert_eq!(
2,
rescan_result
.iter()
.filter(|upo| matches!(upo.as_ref().unwrap(), UpdatePathOutcome::Unchanged(_)))
2021-10-28 17:34:01 +02:00
.count()
);
assert_eq!(
1,
rescan_result
.iter()
.filter(|upo| matches!(upo.as_ref().unwrap(), UpdatePathOutcome::Removed(_)))
2021-10-28 17:34:01 +02:00
.count()
);
}
}