use crate::Lb;
use crate::io::server::Server;
use crate::model::ValidationFailure;
use crate::model::api::METADATA_FEE;
use crate::model::crypto::AESKey;
use crate::model::errors::{LbErr, LbErrKind, LbResult};
use crate::model::file::File;
use crate::model::file_like::FileLike;
use crate::model::file_metadata::FileType;
use crate::model::symkey;
use crate::model::tree_like::TreeLike;
use crate::service::documents::{compress_encrypt_document, document_hmac};
use crate::service::events::Actor;
use futures::StreamExt;
use futures::stream::FuturesUnordered;
use std::collections::hash_map::Entry as Slot;
use std::collections::{HashMap, HashSet};
use std::fs::{self, Metadata, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use tokio::sync::Semaphore;
use uuid::Uuid;
static EXPORT_DOCUMENTS: Semaphore = Semaphore::const_new(16);
pub enum ImportStatus {
CalculatedTotal(usize),
StartingItem(String),
FinishedItem(File),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Existing {
Copy,
Update,
}
#[derive(Clone, Debug)]
pub struct ImportPlan {
pub dest: Uuid,
pub entries: Vec<Entry>,
}
#[derive(Clone, Debug)]
pub struct Entry {
pub disk_path: PathBuf,
pub file_type: EntryType,
pub action: Action,
pub skip: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EntryType {
Folder,
Document {
disk_bytes: u64,
stored_bytes: u64,
added_bytes: u64,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Action {
Create,
Replace(Uuid),
Noop(Uuid),
Ignore(IgnoreReason),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IgnoreReason {
Hidden,
Unreadable,
NotAFile,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct DocTotals {
pub files: usize,
pub added_bytes: u64,
}
impl DocTotals {
fn add(&mut self, added_bytes: u64) {
self.files += 1;
self.added_bytes += added_bytes;
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ImportSummary {
pub new: usize,
pub changed: usize,
pub same: usize,
pub disk_bytes: u64,
pub stored_bytes: u64,
pub added_bytes: u64,
pub skipped: DocTotals,
pub hidden: usize,
pub unreadable: usize,
pub not_a_file: usize,
}
impl Entry {
pub fn name(&self) -> String {
self.disk_path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default()
}
pub fn writes(&self) -> bool {
matches!(self.action, Action::Create | Action::Replace(_)) && !self.skip
}
}
impl<S: Server> Lb<S> {
async fn create_imported(
&self, name: &str, parent: Uuid, content: Option<&[u8]>,
) -> LbResult<File> {
let id = Uuid::new_v4();
let key = symkey::generate_key();
let document = match content {
Some(content) => {
let (hmac, encrypted) = compress_encrypt_document(&key, content)?;
let pending = self.docs.insert_pending(id, hmac, &encrypted).await?;
Some((pending, hmac, encrypted.value.len()))
}
None => None,
};
let file_type = if document.is_some() { FileType::Document } else { FileType::Folder };
let mut tx = self.begin_tx().await;
let db = tx.db();
let mut tree = (&db.base_metadata)
.to_staged(&mut db.local_metadata)
.to_lazy();
let content = match document {
Some((pending, hmac, size)) => {
pending.promote().await?;
Some((hmac, size))
}
None => None,
};
let mut free_name = name.to_string();
for tries in 1.. {
match tree.create(id, key, &parent, &free_name, file_type, &self.keychain) {
Err(err)
if matches!(
err.kind,
LbErrKind::Validation(ValidationFailure::PathConflict(_))
) =>
{
free_name = format!("{name}-{tries}");
}
result => {
result?;
break;
}
}
}
if let Some((hmac, size)) = content {
tree.overwrite_document_hmac(&id, Some(hmac), Some(size), &self.keychain)?;
}
let file = tree.decrypt(&self.keychain, &id, &db.pub_key_lookup)?;
tx.end();
self.events.meta_changed(Actor::User(None));
if content.is_some() {
self.events.doc_written(id, Actor::User(None));
}
Ok(file)
}
async fn holds(&self, id: Uuid, content: &[u8]) -> LbResult<bool> {
let (hmac, key) = {
let tx = self.ro_tx().await;
let db = tx.db();
let mut tree = (&db.base_metadata).to_staged(&db.local_metadata).to_lazy();
let hmac = tree.find(&id)?.document_hmac().copied();
(hmac, tree.decrypt_key(&id, &self.keychain)?)
};
Ok(match hmac {
Some(hmac) => document_hmac(&key, content)? == hmac,
None => content.is_empty(),
})
}
}
fn stored_size(key: &AESKey, data: &[u8]) -> LbResult<u64> {
Ok(compress_encrypt_document(key, data)?.1.value.len() as u64)
}
fn hidden(name: &str, meta: &Metadata) -> bool {
if name.starts_with('.') {
return true;
}
#[cfg(target_os = "macos")]
{
use std::os::macos::fs::MetadataExt;
const UF_HIDDEN: u32 = 0x8000;
meta.st_flags() & UF_HIDDEN != 0
}
#[cfg(target_os = "ios")]
{
use std::os::ios::fs::MetadataExt;
const UF_HIDDEN: u32 = 0x8000;
meta.st_flags() & UF_HIDDEN != 0
}
#[cfg(windows)]
{
use std::os::windows::fs::MetadataExt;
const FILE_ATTRIBUTE_HIDDEN: u32 = 0x2;
meta.file_attributes() & FILE_ATTRIBUTE_HIDDEN != 0
}
#[cfg(not(any(target_os = "macos", target_os = "ios", windows)))]
{
let _ = meta;
false
}
}
impl<S: Server> Lb<S> {
async fn plan_entry(
&self, disk_path: &Path, parent: Option<Uuid>, top: bool, key: &AESKey,
children: &mut HashMap<Uuid, Vec<File>>,
) -> LbResult<(EntryType, Action)> {
let unread = EntryType::Document { disk_bytes: 0, stored_bytes: 0, added_bytes: 0 };
let Ok(meta) = fs::symlink_metadata(disk_path) else {
return Ok((unread, Action::Ignore(IgnoreReason::Unreadable)));
};
let file_type = if meta.is_dir() { EntryType::Folder } else { unread };
let Some(name) = disk_path.file_name().and_then(|n| n.to_str()) else {
return Ok((file_type, Action::Ignore(IgnoreReason::Unreadable)));
};
if !top && hidden(name, &meta) {
return Ok((file_type, Action::Ignore(IgnoreReason::Hidden)));
}
let existing = match parent {
Some(parent) => {
let listed = match children.entry(parent) {
Slot::Occupied(listed) => listed.into_mut(),
Slot::Vacant(slot) => slot.insert(self.get_children(&parent).await?),
};
listed.iter().find(|f| f.name == name).cloned()
}
None => None,
};
if meta.is_dir() {
let action = match existing.filter(|f| f.is_folder()) {
Some(f) => Action::Noop(f.id),
None => Action::Create,
};
return Ok((file_type, action));
}
if !meta.is_file() {
return Ok((file_type, Action::Ignore(IgnoreReason::NotAFile)));
}
let Ok(data) = fs::read(disk_path) else {
return Ok((file_type, Action::Ignore(IgnoreReason::Unreadable)));
};
let disk_bytes = data.len() as u64;
let existing = existing.filter(|f| f.is_document());
let held = |f: &File| f.size_bytes.saturating_sub(METADATA_FEE);
if let Some(f) = &existing {
if self.holds(f.id, &data).await? {
let (stored_bytes, added_bytes) = (held(f), 0);
return Ok((
EntryType::Document { disk_bytes, stored_bytes, added_bytes },
Action::Noop(f.id),
));
}
}
let stored_bytes = stored_size(key, &data)?;
Ok(match existing {
Some(f) => {
let added_bytes = stored_bytes.saturating_sub(held(&f));
(
EntryType::Document { disk_bytes, stored_bytes, added_bytes },
Action::Replace(f.id),
)
}
None => {
let added_bytes = stored_bytes + METADATA_FEE;
(EntryType::Document { disk_bytes, stored_bytes, added_bytes }, Action::Create)
}
})
}
#[instrument(level = "debug", skip(self), err(Debug))]
pub async fn plan_import(
&self, sources: &[PathBuf], dest: Uuid, existing: Existing,
) -> LbResult<ImportPlan> {
let parent = self.get_file_by_id(dest).await?;
if !parent.is_folder() {
Err(LbErrKind::Validation(ValidationFailure::NonFolderWithChildren(dest)))?;
}
let key = symkey::generate_key();
let mut children = HashMap::new();
let mut entries = Vec::new();
let mut stack: Vec<(PathBuf, Option<Uuid>, bool)> = sources
.iter()
.rev()
.filter(|s| !sources.iter().any(|o| o != *s && s.starts_with(o)))
.map(|s| (s.clone(), (existing == Existing::Update).then_some(dest), true))
.collect();
while let Some((disk_path, parent, top)) = stack.pop() {
let (file_type, mut action) = self
.plan_entry(&disk_path, parent, top, &key, &mut children)
.await?;
if file_type == EntryType::Folder && !matches!(action, Action::Ignore(_)) {
let listed: std::io::Result<Vec<PathBuf>> = fs::read_dir(&disk_path)
.and_then(|dir| dir.map(|child| Ok(child?.path())).collect());
match listed {
Ok(mut inside) => {
let here = match action {
Action::Noop(id) => Some(id),
_ => None,
};
inside.sort();
stack.extend(inside.into_iter().rev().map(|c| (c, here, false)));
}
Err(_) => action = Action::Ignore(IgnoreReason::Unreadable),
}
}
entries.push(Entry { disk_path, file_type, action, skip: false });
}
Ok(ImportPlan { dest, entries })
}
}
impl ImportPlan {
pub fn summary(&self) -> ImportSummary {
let mut s = ImportSummary::default();
for e in &self.entries {
match (e.action, e.file_type) {
(Action::Ignore(IgnoreReason::Hidden), _) => s.hidden += 1,
(Action::Ignore(IgnoreReason::Unreadable), _) => s.unreadable += 1,
(Action::Ignore(IgnoreReason::NotAFile), _) => s.not_a_file += 1,
(Action::Noop(_), EntryType::Document { .. }) => s.same += 1,
(Action::Noop(_), EntryType::Folder) => {}
(_, EntryType::Folder) => {
s.stored_bytes += METADATA_FEE;
s.added_bytes += METADATA_FEE;
}
(_, EntryType::Document { added_bytes, .. }) if e.skip => {
s.skipped.add(added_bytes)
}
(action, EntryType::Document { disk_bytes, stored_bytes, added_bytes, .. }) => {
if action == Action::Create {
s.new += 1;
s.stored_bytes += stored_bytes + METADATA_FEE;
} else {
s.changed += 1;
s.stored_bytes += stored_bytes;
}
s.disk_bytes += disk_bytes;
s.added_bytes += added_bytes;
}
}
}
s
}
}
impl<S: Server> Lb<S> {
async fn import_entry(&self, e: &Entry, parent: Uuid) -> LbResult<File> {
let name = e
.disk_path
.file_name()
.and_then(|n| n.to_str())
.ok_or(LbErrKind::DiskPathInvalid)?;
if e.file_type == EntryType::Folder {
return self.create_imported(name, parent, None).await;
}
let content = fs::read(&e.disk_path).map_err(LbErr::from)?;
match e.action {
Action::Replace(id) => {
self.write_document(id, &content).await?;
self.get_file_by_id(id).await
}
_ => self.create_imported(name, parent, Some(&content)).await,
}
}
#[instrument(level = "debug", skip(self, plan, update_status), err(Debug))]
pub async fn import_planned<F: Fn(ImportStatus)>(
&self, plan: &ImportPlan, update_status: &F,
) -> LbResult<()> {
let total = plan.entries.iter().filter(|e| e.writes()).count();
update_status(ImportStatus::CalculatedTotal(total));
let folders: HashSet<&Path> = plan
.entries
.iter()
.filter(|e| e.file_type == EntryType::Folder)
.map(|e| e.disk_path.as_path())
.collect();
let mut ids: HashMap<&Path, Uuid> = HashMap::new();
let mut failed = Vec::new();
for e in &plan.entries {
if let (Action::Noop(id), EntryType::Folder) = (e.action, e.file_type) {
ids.insert(&e.disk_path, id);
}
if !e.writes() {
continue;
}
let parent = match e.disk_path.parent().filter(|p| folders.contains(p)) {
Some(folder) => ids.get(folder).copied(),
None => Some(plan.dest),
};
update_status(ImportStatus::StartingItem(e.disk_path.display().to_string()));
let result = match parent {
Some(parent) => self.import_entry(e, parent).await,
None => Err(LbErrKind::FileParentNonexistent.into()),
};
match result {
Ok(file) => {
if e.file_type == EntryType::Folder {
ids.insert(&e.disk_path, file.id);
}
update_status(ImportStatus::FinishedItem(file));
}
Err(err) => {
error!(?err, path = %e.disk_path.display(), "import failed");
failed.push(e.disk_path.clone());
}
}
}
if failed.is_empty() {
Ok(())
} else {
Err(LbErrKind::ImportFailed { paths: failed, total }.into())
}
}
#[instrument(level = "debug", skip(self, update_status), err(Debug))]
pub async fn import_files<F: Fn(ImportStatus)>(
&self, sources: &[PathBuf], dest: Uuid, update_status: &F,
) -> LbResult<()> {
let plan = self.plan_import(sources, dest, Existing::Copy).await?;
let mut failed: Vec<PathBuf> = plan
.entries
.iter()
.filter(|e| match e.action {
Action::Ignore(IgnoreReason::Unreadable) => true,
Action::Ignore(IgnoreReason::NotAFile) => sources.contains(&e.disk_path),
_ => false,
})
.map(|e| e.disk_path.clone())
.collect();
let total = failed.len() + plan.entries.iter().filter(|e| e.writes()).count();
if let Err(err) = self.import_planned(&plan, update_status).await {
let LbErrKind::ImportFailed { paths, .. } = &err.kind else {
return Err(err);
};
failed.extend(paths.iter().cloned());
}
if failed.is_empty() {
Ok(())
} else {
Err(LbErrKind::ImportFailed { paths: failed, total }.into())
}
}
}
impl<S: Server> Lb<S> {
#[instrument(level = "debug", skip(self, update_status), err(Debug))]
pub async fn export_file<F: Fn(ExportFileInfo)>(
&self, id: Uuid, dest: PathBuf, edit: bool, update_status: &Option<F>,
) -> LbResult<()> {
if dest.is_file() {
return Err(LbErrKind::DiskPathInvalid.into());
}
self.export_file_recursively(id, &dest, edit, update_status)
.await
}
pub async fn export_file_recursively<F: Fn(ExportFileInfo)>(
&self, id: Uuid, disk_path: &Path, edit: bool, update_status: &Option<F>,
) -> LbResult<()> {
let file = self.get_file_by_id(id).await?;
let new_dest = disk_path.join(&file.name);
if let Some(update_status) = update_status {
update_status(ExportFileInfo {
disk_path: disk_path.to_path_buf(),
lockbook_path: self.get_path_by_id(file.id).await?,
});
}
match file.file_type {
FileType::Document => {
let _permit = EXPORT_DOCUMENTS
.acquire()
.await
.expect("export semaphore is never closed");
let content = self.read_document(file.id, true).await?;
let mut disk_file = if edit {
OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(new_dest)
} else {
OpenOptions::new()
.write(true)
.create_new(true)
.open(new_dest)
}
.map_err(LbErr::from)?;
disk_file.write_all(&content).map_err(LbErr::from)?;
}
FileType::Folder => {
fs::create_dir(new_dest.clone()).map_err(LbErr::from)?;
let export_file_futures = FuturesUnordered::new();
for child in self.get_children(&file.id).await? {
let lb = self.clone();
let new_dest = &new_dest;
export_file_futures.push(async move {
lb.export_file_recursively(child.id, new_dest, edit, update_status)
.await
});
}
export_file_futures
.collect::<Vec<LbResult<()>>>()
.await
.into_iter()
.collect::<LbResult<()>>()?;
}
FileType::Link { target } => {
let export_file_futures = FuturesUnordered::new();
let lb = self.clone();
export_file_futures.push(async move {
lb.export_file_recursively(target, disk_path, edit, update_status)
.await
});
export_file_futures
.collect::<Vec<LbResult<()>>>()
.await
.into_iter()
.collect::<LbResult<()>>()?;
}
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct ExportFileInfo {
pub disk_path: PathBuf,
pub lockbook_path: String,
}