Skip to content

Commit 4cc2927

Browse files
committed
Move rename out of platform_device_loader to platform_filesystem
1 parent b6d6a42 commit 4cc2927

File tree

15 files changed

+225
-481
lines changed

15 files changed

+225
-481
lines changed

libparsec/crates/platform_device_loader/src/lib.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ mod strategy;
44
use itertools::Itertools;
55
use libparsec_platform_async::stream::StreamExt;
66
use libparsec_platform_filesystem::{
7-
list_files, load_file, remove_file, save_content, ListFilesError, LoadFileError,
8-
RemoveFileError, SaveContentError,
7+
list_files, load_file, remove_file, rename_file, save_content, ListFilesError, LoadFileError,
8+
RemoveFileError, RenameFileError, SaveContentError,
99
};
1010
pub use strategy::*;
1111
#[cfg(not(target_arch = "wasm32"))]
@@ -673,7 +673,25 @@ pub async fn archive_device(
673673
return result;
674674
}
675675

676-
platform::archive_device(device_path).await
676+
let archive_device_path = get_device_archive_path(device_path);
677+
678+
log::debug!(
679+
"Archiving device {} to {}",
680+
device_path.display(),
681+
archive_device_path.display()
682+
);
683+
684+
rename_file(device_path, &archive_device_path)
685+
.await
686+
.map_err(|e| match e {
687+
RenameFileError::StorageNotAvailable | RenameFileError::NoSpaceLeft => {
688+
ArchiveDeviceError::StorageNotAvailable
689+
} // TODO #11955
690+
RenameFileError::InvalidParent
691+
| RenameFileError::InvalidPath
692+
| RenameFileError::NotFound
693+
| RenameFileError::Internal(_) => ArchiveDeviceError::Internal(e.into()),
694+
})
677695
}
678696

679697
#[derive(Debug, thiserror::Error)]

libparsec/crates/platform_device_loader/src/native/mod.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::path::{Path, PathBuf};
77
use uuid::Uuid;
88

99
use crate::{
10-
encrypt_device, get_device_archive_path, AccountVaultOperationsFetchOpaqueKeyError,
11-
AccountVaultOperationsUploadOpaqueKeyError, ArchiveDeviceError, AvailableDevice,
12-
DeviceAccessStrategy, DeviceSaveStrategy, LoadCiphertextKeyError, LoadDeviceError,
10+
encrypt_device, AccountVaultOperationsFetchOpaqueKeyError,
11+
AccountVaultOperationsUploadOpaqueKeyError, AvailableDevice, DeviceAccessStrategy,
12+
DeviceSaveStrategy, LoadCiphertextKeyError, LoadDeviceError,
1313
OpenBaoOperationsFetchOpaqueKeyError, OpenBaoOperationsUploadOpaqueKeyError,
1414
RemoteOperationServer, SaveDeviceError,
1515
};
@@ -400,20 +400,6 @@ pub(super) async fn save_device(
400400
})
401401
}
402402

403-
pub(super) async fn archive_device(device_path: &Path) -> Result<(), ArchiveDeviceError> {
404-
let archive_device_path = get_device_archive_path(device_path);
405-
406-
log::debug!(
407-
"Archiving device {} to {}",
408-
device_path.display(),
409-
archive_device_path.display()
410-
);
411-
412-
tokio::fs::rename(device_path, archive_device_path)
413-
.await
414-
.map_err(|e| ArchiveDeviceError::Internal(e.into()))
415-
}
416-
417403
pub(super) fn is_keyring_available() -> bool {
418404
// Using "tmp" as user, because keyring-rs forbids the use of empty string
419405
// due to an issue in macOS. See: https://github.com/hwchen/keyring-rs/pull/87

libparsec/crates/platform_device_loader/src/web/internal.rs

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
// Parsec Cloud (https://parsec.cloud) Copyright (c) BUSL-1.1 2016-present Scille SAS
22

3-
use std::path::{Path, PathBuf};
3+
use std::path::PathBuf;
44

55
use libparsec_platform_filesystem::save_content;
66
use libparsec_types::prelude::*;
77

88
use crate::{
9-
web::wrapper::OpenOptions, AccountVaultOperationsUploadOpaqueKeyError, AvailableDevice,
10-
DeviceSaveStrategy, OpenBaoOperationsUploadOpaqueKeyError, RemoteOperationServer,
11-
SaveDeviceError,
9+
AccountVaultOperationsUploadOpaqueKeyError, AvailableDevice, DeviceSaveStrategy,
10+
OpenBaoOperationsUploadOpaqueKeyError, RemoteOperationServer, SaveDeviceError,
1211
};
1312

14-
use super::{error::*, wrapper::Directory};
15-
16-
pub struct Storage {
17-
root_dir: Directory,
18-
}
13+
pub struct Storage {}
1914

2015
impl Storage {
21-
pub(crate) async fn new() -> Result<Self, NewStorageError> {
22-
let root_dir = Directory::get_root().await?;
23-
Ok(Self { root_dir })
24-
}
25-
2616
pub(crate) async fn save_device(
2717
strategy: &DeviceSaveStrategy,
2818
key_file: PathBuf,
@@ -150,32 +140,4 @@ impl Storage {
150140
ty: strategy.ty(),
151141
})
152142
}
153-
154-
pub(crate) async fn archive_device(&self, path: &Path) -> Result<(), ArchiveDeviceError> {
155-
let old_device = self
156-
.root_dir
157-
.get_file_from_path(path, None)
158-
.await
159-
.map_err(ArchiveDeviceError::GetDeviceToArchive)?;
160-
let old_data = old_device
161-
.read_to_end()
162-
.await
163-
.map_err(ArchiveDeviceError::ReadDeviceToArchive)?;
164-
165-
let archive_path = crate::get_device_archive_path(path);
166-
let archive_device = self
167-
.root_dir
168-
.get_file_from_path(&archive_path, Some(OpenOptions::create()))
169-
.await
170-
.map_err(ArchiveDeviceError::CreateArchiveDevice)?;
171-
archive_device
172-
.write_all(&old_data)
173-
.await
174-
.map_err(ArchiveDeviceError::WriteArchiveDevice)?;
175-
176-
self.root_dir
177-
.remove_entry_from_path(path)
178-
.await
179-
.map_err(Into::into)
180-
}
181143
}

libparsec/crates/platform_device_loader/src/web/mod.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ pub(crate) mod error;
44
pub(crate) mod internal;
55
pub(crate) mod wrapper;
66
use libparsec_types::prelude::*;
7-
use std::path::{Path, PathBuf};
7+
use std::path::PathBuf;
88

99
use crate::{
10-
AccountVaultOperationsFetchOpaqueKeyError, ArchiveDeviceError, AvailableDevice,
11-
DeviceAccessStrategy, DeviceSaveStrategy, LoadCiphertextKeyError,
12-
OpenBaoOperationsFetchOpaqueKeyError, RemoteOperationServer, SaveDeviceError,
10+
AccountVaultOperationsFetchOpaqueKeyError, AvailableDevice, DeviceAccessStrategy,
11+
DeviceSaveStrategy, LoadCiphertextKeyError, OpenBaoOperationsFetchOpaqueKeyError,
12+
RemoteOperationServer, SaveDeviceError,
1313
};
1414
use internal::Storage;
1515

@@ -110,15 +110,3 @@ pub(super) async fn save_device(
110110
.await
111111
.map_err(Into::into)
112112
}
113-
114-
pub(super) async fn archive_device(device_path: &Path) -> Result<(), ArchiveDeviceError> {
115-
let Ok(storage) = Storage::new().await.inspect_err(|e| {
116-
log::error!("Failed to access storage: {e}");
117-
}) else {
118-
return Err(ArchiveDeviceError::StorageNotAvailable);
119-
};
120-
storage
121-
.archive_device(device_path)
122-
.await
123-
.map_err(Into::into)
124-
}

0 commit comments

Comments
 (0)