mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-12-01 04:32:13 +01:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
96de4a8f46 | ||
|
|
97d9d671d2 |
No files matched your search
@@ -638,7 +638,7 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
Ok(CtlRequest::Pin { path }) => match route_keep(core, mountpoint, &path) {
|
||||
Ok((routed, rel)) => match routed.pin(&rel, &pin_record(&routed, &rel, &path)) {
|
||||
Ok(name) => {
|
||||
core.touch_profile();
|
||||
core.profile().touch_profile();
|
||||
CtlResponse::Ok {
|
||||
message: format!("pinned {name}"),
|
||||
}
|
||||
@@ -650,7 +650,7 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
Ok(CtlRequest::Unpin { path }) => match route_keep(core, mountpoint, &path) {
|
||||
Ok((routed, rel)) => match routed.unpin(&rel) {
|
||||
Ok(name) => {
|
||||
core.touch_profile();
|
||||
core.profile().touch_profile();
|
||||
CtlResponse::Ok {
|
||||
message: format!("unpinned {name}"),
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ pub(super) fn answer(ask: &Ask<'_>, request: CtlRequest) -> CtlResponse {
|
||||
core, mountpoint, ..
|
||||
} = *ask;
|
||||
match request {
|
||||
CtlRequest::ListDevices => match core.list_devices() {
|
||||
CtlRequest::ListDevices => match core.devices().list_devices() {
|
||||
Ok(items) => CtlResponse::Devices { items },
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
@@ -50,23 +50,23 @@ pub(super) fn answer(ask: &Ask<'_>, request: CtlRequest) -> CtlResponse {
|
||||
Ok(items) => CtlResponse::Locations { items },
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
CtlRequest::RenameDevice { uid, name } => match core.rename_device(&uid, &name) {
|
||||
CtlRequest::RenameDevice { uid, name } => match core.devices().rename_device(&uid, &name) {
|
||||
Ok(()) => CtlResponse::Ok {
|
||||
message: format!("renamed device to {name}"),
|
||||
},
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
CtlRequest::DeleteDevice { uid } => match core.delete_device(&uid) {
|
||||
CtlRequest::DeleteDevice { uid } => match core.devices().delete_device(&uid) {
|
||||
Ok(()) => CtlResponse::Ok {
|
||||
message: "device deleted".to_string(),
|
||||
},
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
CtlRequest::AdoptDevice { uid } => match core.adopt_device(uid.as_deref()) {
|
||||
CtlRequest::AdoptDevice { uid } => match core.devices().adopt_device(uid.as_deref()) {
|
||||
Ok(message) => CtlResponse::Ok { message },
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
CtlRequest::MigrateDevice { uid } => match core.migrate_device(&uid) {
|
||||
CtlRequest::MigrateDevice { uid } => match core.devices().migrate_device(&uid) {
|
||||
Ok(message) => CtlResponse::Ok { message },
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
@@ -84,7 +84,7 @@ pub(super) fn answer(ask: &Ask<'_>, request: CtlRequest) -> CtlResponse {
|
||||
let started = Instant::now();
|
||||
match core.add_sync_folder(&path) {
|
||||
Ok(folder) => {
|
||||
core.touch_profile();
|
||||
core.profile().touch_profile();
|
||||
let name = Path::new(&folder.local_path)
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
@@ -131,7 +131,7 @@ pub(super) fn answer(ask: &Ask<'_>, request: CtlRequest) -> CtlResponse {
|
||||
CtlRequest::RemoveSyncFolder { id, delete_remote } => {
|
||||
match core.remove_sync_folder(id, delete_remote) {
|
||||
Ok(()) => {
|
||||
core.touch_profile();
|
||||
core.profile().touch_profile();
|
||||
CtlResponse::Ok {
|
||||
message: "removed synced folder".to_string(),
|
||||
}
|
||||
@@ -142,7 +142,7 @@ pub(super) fn answer(ask: &Ask<'_>, request: CtlRequest) -> CtlResponse {
|
||||
CtlRequest::SetSyncFolderMode { id, mode } => {
|
||||
match core.request_sync_folder_mode(id, &mode) {
|
||||
Ok(message) => {
|
||||
core.touch_profile();
|
||||
core.profile().touch_profile();
|
||||
core.log_activity(ActivityKind::Upload, &message, "", true);
|
||||
CtlResponse::Ok { message }
|
||||
}
|
||||
@@ -225,12 +225,12 @@ pub(super) fn answer(ask: &Ask<'_>, request: CtlRequest) -> CtlResponse {
|
||||
},
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
CtlRequest::ListRestorableFolders => match core.list_restorable_folders(None) {
|
||||
CtlRequest::ListRestorableFolders => match core.profile().list_restorable_folders(None) {
|
||||
Ok(items) => CtlResponse::RestorableFolders { items },
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
CtlRequest::ListDeviceRestorableFolders { device } => {
|
||||
match core.list_restorable_folders(Some(&device)) {
|
||||
match core.profile().list_restorable_folders(Some(&device)) {
|
||||
Ok(items) => CtlResponse::RestorableFolders { items },
|
||||
Err(e) => CtlResponse::error(e),
|
||||
}
|
||||
@@ -251,7 +251,7 @@ pub(super) fn answer(ask: &Ask<'_>, request: CtlRequest) -> CtlResponse {
|
||||
.spawn_job("pdfs-restore-sync-folders", move || {
|
||||
match core.restore_sync_folders(&items, device.as_deref()) {
|
||||
Ok(outcome) => {
|
||||
core.touch_profile();
|
||||
core.profile().touch_profile();
|
||||
if !outcome.complete() {
|
||||
warn!(skipped = ?outcome.skipped, "restore skipped folders");
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ pub(super) fn answer(ask: &Ask<'_>, request: CtlRequest) -> CtlResponse {
|
||||
.map(|dirs| dirs.update_config(|cfg| cfg.cache_budget = Some(bytes)))
|
||||
{
|
||||
Ok(Ok(())) => {
|
||||
core.touch_profile();
|
||||
core.profile().touch_profile();
|
||||
CtlResponse::Ok {
|
||||
message: format!("cache budget set to {bytes} bytes"),
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ use pdfs_core::mounts::MountMode;
|
||||
use pdfs_core::profile::PROFILE_DIR_NAME;
|
||||
use pdfs_core::{CoreError, CoreResult};
|
||||
use pdfs_store::db::{StoredDevice, StoredSyncFolder};
|
||||
use proton_drive_rs::proton_sdk::error::ProtonError;
|
||||
use proton_drive_rs::proton_sdk::ids::{DeviceUid, NodeUid};
|
||||
use proton_drive_rs::{DeviceType, Node};
|
||||
use tracing::{info, warn};
|
||||
@@ -146,28 +147,70 @@ fn migrated_name(name: &str, device: &str, taken: &[String]) -> Option<String> {
|
||||
.find(|candidate| free(candidate))
|
||||
}
|
||||
|
||||
impl Core {
|
||||
// ---- devices ----------------------------------------------------------
|
||||
/// This account's devices on Drive, and which one is this computer: listing,
|
||||
/// renaming, deleting, adopting and migrating them, and registering this one.
|
||||
///
|
||||
/// It reaches the rest of the daemon through [`DevicesHost`]. [`Core::devices`]
|
||||
/// builds one on demand with the daemon as its host.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Devices {
|
||||
client: proton_drive_rs::ProtonDriveClient,
|
||||
drive: std::sync::Arc<dyn crate::drive::DriveApi>,
|
||||
rt: tokio::runtime::Handle,
|
||||
db: std::sync::Arc<pdfs_store::db::Db>,
|
||||
host: std::sync::Arc<dyn DevicesHost>,
|
||||
}
|
||||
|
||||
/// Unified local-location listing for the control protocol.
|
||||
pub(crate) fn list_locations(&self) -> CoreResult<Vec<MountSpec>> {
|
||||
let progress = self.sync.progress.lock();
|
||||
let mut locations = self
|
||||
.db
|
||||
.mount_list()
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?;
|
||||
for location in &mut locations {
|
||||
location.mounted = self.states.is_mounted_at(Path::new(&location.local_path));
|
||||
if let MountKind::Device { sync_folder_id } | MountKind::Attached { sync_folder_id } =
|
||||
&location.kind
|
||||
{
|
||||
location.progress = progress.get(sync_folder_id).cloned();
|
||||
location.paused = self.sync_folder_paused(*sync_folder_id);
|
||||
}
|
||||
}
|
||||
Ok(locations)
|
||||
/// What [`Devices`] needs from the rest of the daemon, to migrate a device's
|
||||
/// folders onto this one.
|
||||
pub(crate) trait DevicesHost: Send + Sync {
|
||||
/// Move a node under `parent` on the server, renaming it to `name` if
|
||||
/// given, and update the tree to match.
|
||||
fn move_node(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
parent: &NodeUid,
|
||||
name: Option<&str>,
|
||||
) -> Result<(), ProtonError>;
|
||||
/// Run a sync pass for one synced folder now.
|
||||
fn sync_now(&self, id: Option<i64>);
|
||||
/// Re-enumerate a folder's children on next access.
|
||||
fn invalidate_children_of(&self, parent: &NodeUid);
|
||||
}
|
||||
|
||||
impl DevicesHost for Core {
|
||||
fn move_node(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
parent: &NodeUid,
|
||||
name: Option<&str>,
|
||||
) -> Result<(), ProtonError> {
|
||||
self.move_rename_remote(uid, parent, name)
|
||||
}
|
||||
|
||||
fn sync_now(&self, id: Option<i64>) {
|
||||
Core::sync_now(self, id);
|
||||
}
|
||||
|
||||
fn invalidate_children_of(&self, parent: &NodeUid) {
|
||||
Core::invalidate_children_of(self, parent);
|
||||
}
|
||||
}
|
||||
|
||||
impl Core {
|
||||
/// The devices service, with this daemon as its host.
|
||||
pub(crate) fn devices(&self) -> Devices {
|
||||
Devices {
|
||||
client: self.client.clone(),
|
||||
drive: self.drive.clone(),
|
||||
rt: self.rt.clone(),
|
||||
db: self.db.clone(),
|
||||
host: std::sync::Arc::new(self.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Devices {
|
||||
/// List the account's registered devices, flagging the one *this* machine
|
||||
/// syncs to so a front-end can treat it as more than another computer in the
|
||||
/// list — deleting it takes this machine's synced folders down with it.
|
||||
@@ -354,7 +397,10 @@ impl Core {
|
||||
continue;
|
||||
}
|
||||
let new_name = migrated_name(&node.name, &source.name, &taken);
|
||||
if let Err(e) = self.move_rename_remote(&node.uid, &target_root, new_name.as_deref()) {
|
||||
if let Err(e) = self
|
||||
.host
|
||||
.move_node(&node.uid, &target_root, new_name.as_deref())
|
||||
{
|
||||
warn!(name = %node.name, error = %e, "migrate: could not move folder");
|
||||
failed.push(format!("{}: {e}", node.name));
|
||||
continue;
|
||||
@@ -371,11 +417,11 @@ impl Core {
|
||||
if let Err(e) = self.db.sync_folder_set_share(folder.id, &target.share_id) {
|
||||
warn!(id = folder.id, error = ?e, "migrate: could not re-point sync folder");
|
||||
}
|
||||
self.sync_now(Some(folder.id));
|
||||
self.host.sync_now(Some(folder.id));
|
||||
}
|
||||
}
|
||||
self.invalidate_children_of(&source_root);
|
||||
self.invalidate_children_of(&target_root);
|
||||
self.host.invalidate_children_of(&source_root);
|
||||
self.host.invalidate_children_of(&target_root);
|
||||
|
||||
let mut message = format!("moved {moved} folder(s) from {}", source.name);
|
||||
if !renamed.is_empty() {
|
||||
@@ -395,8 +441,6 @@ impl Core {
|
||||
Ok(message)
|
||||
}
|
||||
|
||||
// ---- device folder sync -------------------------
|
||||
|
||||
/// Auto-register (or recover) this machine as a Proton Drive Device, caching
|
||||
/// it so restarts reuse the same device. Recovery matches an existing remote
|
||||
/// Linux device by name before creating a new one, so a lost local record
|
||||
@@ -503,7 +547,7 @@ impl Core {
|
||||
}
|
||||
|
||||
/// Everything directly under a device root, trashed nodes included.
|
||||
pub(super) fn device_root_nodes(&self, root_uid: &NodeUid) -> CoreResult<Vec<Node>> {
|
||||
pub(crate) fn device_root_nodes(&self, root_uid: &NodeUid) -> CoreResult<Vec<Node>> {
|
||||
let uids = self
|
||||
.rt
|
||||
.block_on(self.drive.enumerate_folder_children_node_uids(root_uid))
|
||||
@@ -515,6 +559,31 @@ impl Core {
|
||||
.block_on(self.drive.enumerate_nodes(&uids))
|
||||
.map_err(|e| CoreError::from_api(&e, "resolve device root children"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Core {
|
||||
// ---- devices ----------------------------------------------------------
|
||||
|
||||
/// Unified local-location listing for the control protocol.
|
||||
pub(crate) fn list_locations(&self) -> CoreResult<Vec<MountSpec>> {
|
||||
let progress = self.sync.progress.lock();
|
||||
let mut locations = self
|
||||
.db
|
||||
.mount_list()
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?;
|
||||
for location in &mut locations {
|
||||
location.mounted = self.states.is_mounted_at(Path::new(&location.local_path));
|
||||
if let MountKind::Device { sync_folder_id } | MountKind::Attached { sync_folder_id } =
|
||||
&location.kind
|
||||
{
|
||||
location.progress = progress.get(sync_folder_id).cloned();
|
||||
location.paused = self.sync_folder_paused(*sync_folder_id);
|
||||
}
|
||||
}
|
||||
Ok(locations)
|
||||
}
|
||||
|
||||
// ---- device folder sync -------------------------
|
||||
|
||||
/// Add a local folder to this device's sync set: register the device if
|
||||
/// needed, create a matching folder under the device root, upload the local
|
||||
@@ -568,7 +637,7 @@ impl Core {
|
||||
)));
|
||||
}
|
||||
|
||||
let device = self.ensure_device()?;
|
||||
let device = self.devices().ensure_device()?;
|
||||
let root_uid = parse_uid(&device.root_uid).ok_or_else(|| {
|
||||
CoreError::internal(format!("bad device root uid: {}", device.root_uid))
|
||||
})?;
|
||||
@@ -580,7 +649,7 @@ impl Core {
|
||||
// original, not leave the user with two "Downloads" in their Drive. The
|
||||
// reconcile treats an existing remote tree correctly: unmatched paths read as
|
||||
// a conflict, not as data loss.
|
||||
let remote_root = match self.find_device_child_folder(&root_uid, &name)? {
|
||||
let remote_root = match self.devices().find_device_child_folder(&root_uid, &name)? {
|
||||
Some(uid) => {
|
||||
info!(name, "reusing existing device folder");
|
||||
uid
|
||||
|
||||
+160
-120
@@ -21,8 +21,13 @@ use pdfs_core::{CoreError, CoreResult};
|
||||
use proton_drive_rs::proton_sdk::ids::NodeUid;
|
||||
use tracing::{info, warn};
|
||||
|
||||
use super::devices::Devices;
|
||||
use super::drive::DriveApi;
|
||||
use super::sync;
|
||||
use super::{Core, now_secs, parse_uid, this_hostname};
|
||||
use pdfs_core::lifecycle::ServiceBoard;
|
||||
use pdfs_store::db::Db;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Coalescing flags for background profile saves.
|
||||
///
|
||||
@@ -40,7 +45,146 @@ fn save_state() -> &'static (AtomicBool, AtomicBool) {
|
||||
SAVE_STATE.get_or_init(|| (AtomicBool::new(false), AtomicBool::new(false)))
|
||||
}
|
||||
|
||||
/// The backup of this machine's arrangement on Drive: which folders sync where
|
||||
/// and in which mode. It writes the profile and reads the ones other machines
|
||||
/// wrote, to offer their folders for restore.
|
||||
///
|
||||
/// It needs nothing from the filesystem. [`Core::profile`] builds one on
|
||||
/// demand. A clone shares everything, which is how a save moves onto a
|
||||
/// background job.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ProfileBackup {
|
||||
drive: Arc<dyn DriveApi>,
|
||||
rt: tokio::runtime::Handle,
|
||||
db: Arc<Db>,
|
||||
services: ServiceBoard,
|
||||
devices: Devices,
|
||||
}
|
||||
|
||||
impl Core {
|
||||
/// The profile backup.
|
||||
pub(crate) fn profile(&self) -> ProfileBackup {
|
||||
ProfileBackup {
|
||||
drive: self.drive.clone(),
|
||||
rt: self.rt.clone(),
|
||||
db: self.db.clone(),
|
||||
services: self.services.clone(),
|
||||
devices: self.devices(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Attach remote device folders to local paths and let the sync engine fill
|
||||
/// them in.
|
||||
///
|
||||
/// The download is not written here: a new sync-folder row starts with an
|
||||
/// empty baseline, and an empty baseline against an empty local directory
|
||||
/// and a populated remote already classifies as "download everything". The
|
||||
/// restore is therefore just bookkeeping plus a `Reconcile`, and it reuses
|
||||
/// the engine's conflict handling for the case where the local directory
|
||||
/// turns out not to be empty after all.
|
||||
pub(crate) fn restore_sync_folders(
|
||||
&self,
|
||||
items: &[pdfs_core::control::RestoreItem],
|
||||
device: Option<&str>,
|
||||
) -> CoreResult<RestoreOutcome> {
|
||||
let _registration = super::devices::SYNC_ROOT_REGISTRATION.lock();
|
||||
if items.is_empty() {
|
||||
return Err(CoreError::invalid("nothing to restore"));
|
||||
}
|
||||
let device = self.devices().restore_source(device)?;
|
||||
let existing = self
|
||||
.db
|
||||
.sync_folder_list()
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?;
|
||||
|
||||
// A caller must not smuggle a My files root into a device mapping,
|
||||
// where destructive device-only operations would otherwise be allowed.
|
||||
let device_root = super::parse_uid(&device.root_uid)
|
||||
.ok_or_else(|| CoreError::internal("invalid device root"))?;
|
||||
let children = self.devices().device_root_nodes(&device_root)?;
|
||||
let reserved = crate::devices::sync_root_reserved();
|
||||
let mut roots: Vec<PathBuf> = existing
|
||||
.iter()
|
||||
.map(|f| PathBuf::from(&f.local_path))
|
||||
.collect();
|
||||
let mut restored = 0usize;
|
||||
let mut skipped: Vec<String> = Vec::new();
|
||||
for item in items {
|
||||
if !children.iter().any(|node| {
|
||||
node.uid.to_string() == item.remote_uid
|
||||
&& node.is_folder()
|
||||
&& !node.trashed
|
||||
&& node.name != PROFILE_DIR_NAME
|
||||
}) {
|
||||
skipped.push(format!(
|
||||
"{}: not a folder under this device",
|
||||
item.local_path
|
||||
));
|
||||
continue;
|
||||
}
|
||||
// The path is proposed from the profile another machine wrote, and
|
||||
// may hold a folder name from Drive: checked like any new root.
|
||||
let local = match pdfs_core::syncroot::resolve(Path::new(&item.local_path)) {
|
||||
Ok(local) => local,
|
||||
Err(e) => {
|
||||
skipped.push(format!("{}: {e}", item.local_path));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if existing.iter().any(|f| f.remote_uid == item.remote_uid) {
|
||||
skipped.push(format!("{}: already synced", item.local_path));
|
||||
continue;
|
||||
}
|
||||
if let Some(problem) = pdfs_core::syncroot::problem(&local, &roots, &reserved) {
|
||||
skipped.push(format!("{}: {problem}", item.local_path));
|
||||
continue;
|
||||
}
|
||||
if let Err(e) = std::fs::create_dir_all(&local) {
|
||||
skipped.push(format!("{}: {e}", item.local_path));
|
||||
continue;
|
||||
}
|
||||
let mode = match restore_mode(&item.mode) {
|
||||
Some(mode) => mode,
|
||||
None => {
|
||||
skipped.push(format!("{}: unknown mode {}", item.local_path, item.mode));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let id = match self.db.sync_folder_add(
|
||||
&local.to_string_lossy(),
|
||||
&item.remote_uid,
|
||||
&device.share_id,
|
||||
) {
|
||||
Ok(id) => id,
|
||||
Err(e) => {
|
||||
// The folders restored before this one still need their
|
||||
// watches.
|
||||
let _ = self.sync.tx.send(sync::SyncMsg::Rewatch);
|
||||
return Err(CoreError::internal(format!("db: {e:?}")));
|
||||
}
|
||||
};
|
||||
// `ondemand` is applied through the normal request path, which
|
||||
// mounts the FUSE session and handles the folder being busy — the
|
||||
// restore does not get its own copy of that logic.
|
||||
if mode == MountMode::OnDemand {
|
||||
let _ = self.request_sync_folder_mode(id, "ondemand");
|
||||
}
|
||||
let _ = self.sync.tx.send(sync::SyncMsg::Reconcile(id));
|
||||
roots.push(local);
|
||||
restored += 1;
|
||||
}
|
||||
let _ = self.sync.tx.send(sync::SyncMsg::Rewatch);
|
||||
|
||||
// The reply is gone once the app has shown it; the log keeps why.
|
||||
for reason in &skipped {
|
||||
info!(reason, "restore skipped a folder");
|
||||
}
|
||||
info!(restored, skipped = skipped.len(), "restore complete");
|
||||
Ok(RestoreOutcome { restored, skipped })
|
||||
}
|
||||
}
|
||||
|
||||
impl ProfileBackup {
|
||||
/// Note that the arrangement this machine's profile describes has changed,
|
||||
/// and upload a fresh profile in the background.
|
||||
///
|
||||
@@ -54,11 +198,11 @@ impl Core {
|
||||
if saving.swap(true, Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
let core = self.clone();
|
||||
let this = self.clone();
|
||||
if let Err(error) = self.services.spawn_job("pdfs-profile-backup", move || {
|
||||
let (saving, dirty) = save_state();
|
||||
while dirty.swap(false, Ordering::SeqCst) {
|
||||
if let Err(e) = core.save_profile() {
|
||||
if let Err(e) = this.save_profile() {
|
||||
warn!(error = %e.message, "profile backup failed");
|
||||
break;
|
||||
}
|
||||
@@ -142,7 +286,7 @@ impl Core {
|
||||
}
|
||||
|
||||
/// Snapshot the local arrangement into a [`Profile`].
|
||||
fn build_profile(&self, device_uid: &str, machine_id: &str) -> CoreResult<Profile> {
|
||||
pub(crate) fn build_profile(&self, device_uid: &str, machine_id: &str) -> CoreResult<Profile> {
|
||||
let folders = profile_folders(
|
||||
self.db
|
||||
.sync_folder_list()
|
||||
@@ -180,7 +324,7 @@ impl Core {
|
||||
///
|
||||
/// The sibling of `find_device_child_folder`; kept separate because a folder
|
||||
/// named `profile.json` must not be mistaken for the profile.
|
||||
fn find_device_child_file(
|
||||
pub(crate) fn find_device_child_file(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
@@ -204,7 +348,7 @@ impl Core {
|
||||
|
||||
/// The untrashed files directly under `parent_uid` whose name passes
|
||||
/// `keep`.
|
||||
fn device_child_files(
|
||||
pub(crate) fn device_child_files(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
keep: impl Fn(&str) -> bool,
|
||||
@@ -233,8 +377,11 @@ impl Core {
|
||||
/// Reused by stable identity rather than recreated: a second folder of the
|
||||
/// same name would split the profile across two documents, and the restore
|
||||
/// would have no way to tell which is current.
|
||||
fn ensure_profile_dir(&self, root_uid: &NodeUid) -> CoreResult<NodeUid> {
|
||||
if let Some(uid) = self.find_device_child_folder(root_uid, PROFILE_DIR_NAME)? {
|
||||
pub(crate) fn ensure_profile_dir(&self, root_uid: &NodeUid) -> CoreResult<NodeUid> {
|
||||
if let Some(uid) = self
|
||||
.devices
|
||||
.find_device_child_folder(root_uid, PROFILE_DIR_NAME)?
|
||||
{
|
||||
return Ok(uid);
|
||||
}
|
||||
let uid = self
|
||||
@@ -258,8 +405,11 @@ impl Core {
|
||||
/// is left alone rather than trashed, so an older client on another
|
||||
/// machine keeps working. A profile that cannot be read is left out and
|
||||
/// logged; the others still count.
|
||||
fn load_profiles(&self, root_uid: &NodeUid) -> CoreResult<Vec<Profile>> {
|
||||
let mut uids = match self.find_device_child_folder(root_uid, PROFILE_DIR_NAME)? {
|
||||
pub(crate) fn load_profiles(&self, root_uid: &NodeUid) -> CoreResult<Vec<Profile>> {
|
||||
let mut uids = match self
|
||||
.devices
|
||||
.find_device_child_folder(root_uid, PROFILE_DIR_NAME)?
|
||||
{
|
||||
Some(dir_uid) => self.device_child_files(&dir_uid, is_profile_file_name)?,
|
||||
None => Vec::new(),
|
||||
};
|
||||
@@ -301,7 +451,7 @@ impl Core {
|
||||
&self,
|
||||
device: Option<&str>,
|
||||
) -> CoreResult<Vec<RestorableFolder>> {
|
||||
let device = self.restore_source(device)?;
|
||||
let device = self.devices.restore_source(device)?;
|
||||
let root_uid = parse_uid(&device.root_uid).ok_or_else(|| {
|
||||
CoreError::internal(format!("bad device root uid: {}", device.root_uid))
|
||||
})?;
|
||||
@@ -368,116 +518,6 @@ impl Core {
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Attach remote device folders to local paths and let the sync engine fill
|
||||
/// them in.
|
||||
///
|
||||
/// The download is not written here: a new sync-folder row starts with an
|
||||
/// empty baseline, and an empty baseline against an empty local directory
|
||||
/// and a populated remote already classifies as "download everything". The
|
||||
/// restore is therefore just bookkeeping plus a `Reconcile`, and it reuses
|
||||
/// the engine's conflict handling for the case where the local directory
|
||||
/// turns out not to be empty after all.
|
||||
pub(crate) fn restore_sync_folders(
|
||||
&self,
|
||||
items: &[pdfs_core::control::RestoreItem],
|
||||
device: Option<&str>,
|
||||
) -> CoreResult<RestoreOutcome> {
|
||||
let _registration = super::devices::SYNC_ROOT_REGISTRATION.lock();
|
||||
if items.is_empty() {
|
||||
return Err(CoreError::invalid("nothing to restore"));
|
||||
}
|
||||
let device = self.restore_source(device)?;
|
||||
let existing = self
|
||||
.db
|
||||
.sync_folder_list()
|
||||
.map_err(|e| CoreError::internal(format!("db: {e:?}")))?;
|
||||
|
||||
// A caller must not smuggle a My files root into a device mapping,
|
||||
// where destructive device-only operations would otherwise be allowed.
|
||||
let device_root = super::parse_uid(&device.root_uid)
|
||||
.ok_or_else(|| CoreError::internal("invalid device root"))?;
|
||||
let children = self.device_root_nodes(&device_root)?;
|
||||
let reserved = crate::devices::sync_root_reserved();
|
||||
let mut roots: Vec<PathBuf> = existing
|
||||
.iter()
|
||||
.map(|f| PathBuf::from(&f.local_path))
|
||||
.collect();
|
||||
let mut restored = 0usize;
|
||||
let mut skipped: Vec<String> = Vec::new();
|
||||
for item in items {
|
||||
if !children.iter().any(|node| {
|
||||
node.uid.to_string() == item.remote_uid
|
||||
&& node.is_folder()
|
||||
&& !node.trashed
|
||||
&& node.name != PROFILE_DIR_NAME
|
||||
}) {
|
||||
skipped.push(format!(
|
||||
"{}: not a folder under this device",
|
||||
item.local_path
|
||||
));
|
||||
continue;
|
||||
}
|
||||
// The path is proposed from the profile another machine wrote, and
|
||||
// may hold a folder name from Drive: checked like any new root.
|
||||
let local = match pdfs_core::syncroot::resolve(Path::new(&item.local_path)) {
|
||||
Ok(local) => local,
|
||||
Err(e) => {
|
||||
skipped.push(format!("{}: {e}", item.local_path));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if existing.iter().any(|f| f.remote_uid == item.remote_uid) {
|
||||
skipped.push(format!("{}: already synced", item.local_path));
|
||||
continue;
|
||||
}
|
||||
if let Some(problem) = pdfs_core::syncroot::problem(&local, &roots, &reserved) {
|
||||
skipped.push(format!("{}: {problem}", item.local_path));
|
||||
continue;
|
||||
}
|
||||
if let Err(e) = std::fs::create_dir_all(&local) {
|
||||
skipped.push(format!("{}: {e}", item.local_path));
|
||||
continue;
|
||||
}
|
||||
let mode = match restore_mode(&item.mode) {
|
||||
Some(mode) => mode,
|
||||
None => {
|
||||
skipped.push(format!("{}: unknown mode {}", item.local_path, item.mode));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let id = match self.db.sync_folder_add(
|
||||
&local.to_string_lossy(),
|
||||
&item.remote_uid,
|
||||
&device.share_id,
|
||||
) {
|
||||
Ok(id) => id,
|
||||
Err(e) => {
|
||||
// The folders restored before this one still need their
|
||||
// watches.
|
||||
let _ = self.sync.tx.send(sync::SyncMsg::Rewatch);
|
||||
return Err(CoreError::internal(format!("db: {e:?}")));
|
||||
}
|
||||
};
|
||||
// `ondemand` is applied through the normal request path, which
|
||||
// mounts the FUSE session and handles the folder being busy — the
|
||||
// restore does not get its own copy of that logic.
|
||||
if mode == MountMode::OnDemand {
|
||||
let _ = self.request_sync_folder_mode(id, "ondemand");
|
||||
}
|
||||
let _ = self.sync.tx.send(sync::SyncMsg::Reconcile(id));
|
||||
roots.push(local);
|
||||
restored += 1;
|
||||
}
|
||||
let _ = self.sync.tx.send(sync::SyncMsg::Rewatch);
|
||||
|
||||
// The reply is gone once the app has shown it; the log keeps why.
|
||||
for reason in &skipped {
|
||||
info!(reason, "restore skipped a folder");
|
||||
}
|
||||
info!(restored, skipped = skipped.len(), "restore complete");
|
||||
Ok(RestoreOutcome { restored, skipped })
|
||||
}
|
||||
}
|
||||
|
||||
/// Only device backups belong in the device's cross-machine profile.
|
||||
|
||||
@@ -790,6 +790,15 @@ on demand.
|
||||
the node may be written, drops what describes the old content (cached blocks and open readers)
|
||||
and fetches it again if it is kept offline, and adds an activity entry.
|
||||
|
||||
`devices::Devices` holds the account's devices on Drive: listing, renaming, deleting, adopting
|
||||
and migrating them, and registering this computer. Its `DevicesHost` port moves a folder, runs a
|
||||
sync pass and re-enumerates a folder, which is what migrating a device's folders needs. Synced
|
||||
folders and on-demand mounts are still methods on `Core`.
|
||||
|
||||
`profile::ProfileBackup` writes this machine's profile to its device and reads the profiles other
|
||||
machines wrote, to offer their folders for restore. It needs nothing from the filesystem, so it
|
||||
has no host. Turning the offered folders into synced folders stays on `Core`.
|
||||
|
||||
The control socket answers each area's requests in a module of its own under `control/`:
|
||||
sharing, Photos, synced folders and devices, revisions, and the trash and cache. Each module
|
||||
exports a pattern macro naming its requests, and the main match hands them over in one arm, so
|
||||
|
||||
Reference in new issue
Block a user