Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[rust] Keep the adoption index in the CLI home (#14310)

Manav Rathief7b39ee92d4
diff --git a/rust/apps/cli/changes/adoption-index-home.md b/rust/apps/cli/changes/adoption-index-home.md
new file mode 100644
index 0000000000..95a6fe71b9
--- /dev/null
+++ b/rust/apps/cli/changes/adoption-index-home.md
@@ -0,0 +1 @@
+- Photos export adoption now keeps its temporary database in the CLI home, respecting `ENTE_CLI_HOME`.
diff --git a/rust/apps/cli/src/photos/export/mod.rs b/rust/apps/cli/src/photos/export/mod.rs
index 6639d50b88..4dfd8747ca 100644
--- a/rust/apps/cli/src/photos/export/mod.rs
+++ b/rust/apps/cli/src/photos/export/mod.rs
@@ -64,7 +64,13 @@ async fn run_locked(args: PhotosExportArgs, selected: Option<&str>, as_json: boo
!export.exists() || db_path.try_exists()? || args.adopt,
"this export is not associated with this CLI home; use --adopt"
);
- home::create(db_path.parent().context("export DB has no parent")?)?;
+ let db_dir = db_path.parent().context("export DB has no parent")?;
+ home::create(db_dir)?;
+ if let Err(error) = std::fs::remove_dir_all(db_dir.join("adoption"))
+ && error.kind() != std::io::ErrorKind::NotFound
+ {
+ return Err(error.into());
+ }
let connection = db::connect(&db_path, &account.db_key, true)?;
let (cancel_send, mut cancel) = watch::channel(false);
let signal_send = cancel_send.clone();
@@ -80,7 +86,14 @@ async fn run_locked(args: PhotosExportArgs, selected: Option<&str>, as_json: boo
export
.prepare(
connection,
- db::scratch,
+ || {
+ let directory = tempfile::Builder::new()
+ .prefix("adoption")
+ .rand_bytes(0)
+ .tempdir_in(db_dir)?;
+ let connection = db::scratch(&directory.path().join("index.db"))?;
+ Ok((directory, connection))
+ },
&mut source,
&session,
&mut cancel,
diff --git a/rust/apps/cli/tests/cli/export.rs b/rust/apps/cli/tests/cli/export.rs
index d132067e87..37ce958da0 100644
--- a/rust/apps/cli/tests/cli/export.rs
+++ b/rust/apps/cli/tests/cli/export.rs
@@ -390,12 +390,25 @@ fn export_adopts_relocated_output_and_verifies_later_selections() -> TestResult
fs::rename(&root, &relocated)?;
let fresh = TestHome::new();
login(&fresh, "photos", &email, &["--host", origin]);
+ let abandoned = fresh
+ .dir
+ .path()
+ .join("accounts")
+ .join(
+ fresh.read_vault()["accounts"][0]["storage_id"]
+ .as_str()
+ .unwrap(),
+ )
+ .join("exports/adoption");
+ fs::create_dir_all(&abandoned)?;
+ fs::write(abandoned.join("index.db"), b"abandoned index")?;
assert!(
failure(&fresh.run(&["photos", "export", relocated.to_str().unwrap()]))
.contains("--adopt")
);
let reads = objects.reads();
export(&fresh, &relocated, &["--adopt", "--album", "Travel"]);
+ assert!(!abandoned.exists());
assert_eq!(objects.reads(), reads);
assert_eq!(fs::read(relocated.join("export.json"))?, root_bytes);
fs::write(relocated.join("Work/Work.jpg"), b"xxxx")?;
@@ -415,7 +428,10 @@ fn export_adopts_relocated_output_and_verifies_later_selections() -> TestResult
assert_eq!(fs::read(relocated.join("Work/Work.jpg"))?, b"xxxx");
assert_eq!(objects.reads(), reads);
fs::remove_file(relocated.join("Work/Work.jpg"))?;
+ fs::create_dir_all(&abandoned)?;
+ fs::write(abandoned.join("index.db"), b"abandoned index")?;
export(&fresh, &relocated, &["--album", "Work"]);
+ assert!(!abandoned.exists());
assert_eq!(objects.reads(), reads + 1);
assert_eq!(fs::read(relocated.join("Work/Work.jpg"))?, b"work");
Ok(())
diff --git a/rust/apps/cli/tests/cli/export_engine.rs b/rust/apps/cli/tests/cli/export_engine.rs
index 81e095457b..bbedd78cf2 100644
--- a/rust/apps/cli/tests/cli/export_engine.rs
+++ b/rust/apps/cli/tests/cli/export_engine.rs
@@ -155,10 +155,11 @@ impl Engine {
let db = self.db();
tokio::runtime::Runtime::new()?.block_on(export.prepare(
db,
- |path| {
- let db = rusqlite::Connection::open(path)?;
+ || {
+ let directory = tempfile::tempdir_in(self.directory.path())?;
+ let db = rusqlite::Connection::open(directory.path().join("index.db"))?;
db.pragma_update(None, "key", b64::encode(Key::generate().as_bytes()))?;
- Ok(db)
+ Ok((directory, db))
},
&mut self.catalog,
&self.session,
diff --git a/rust/crates/photos-export/src/adopt.rs b/rust/crates/photos-export/src/adopt.rs
index 38d0e6a624..40055f735b 100644
--- a/rust/crates/photos-export/src/adopt.rs
+++ b/rust/crates/photos-export/src/adopt.rs
@@ -35,9 +35,8 @@ pub struct Index {
}
impl Index {
- pub fn new(open: impl FnOnce(&Path) -> Result<Connection>) -> Result<Self> {
- let directory = tempfile::tempdir()?;
- let db = open(&directory.path().join("index.db"))?;
+ pub fn new(open: impl FnOnce() -> Result<(tempfile::TempDir, Connection)>) -> Result<Self> {
+ let (directory, db) = open()?;
db.execute_batch(
"PRAGMA temp_store=MEMORY; PRAGMA journal_mode=OFF; PRAGMA synchronous=OFF; BEGIN;",
)?;
diff --git a/rust/crates/photos-export/src/lib.rs b/rust/crates/photos-export/src/lib.rs
index a871a24a24..37215b1424 100644
--- a/rust/crates/photos-export/src/lib.rs
+++ b/rust/crates/photos-export/src/lib.rs
@@ -130,7 +130,7 @@ impl Export {
pub async fn prepare(
&mut self,
connection: rusqlite::Connection,
- open_index: impl FnOnce(&Path) -> Result<rusqlite::Connection>,
+ open_index: impl FnOnce() -> Result<(tempfile::TempDir, rusqlite::Connection)>,
source: &mut impl Source,
session: &ente_core::Session,
cancel: &mut watch::Receiver<bool>,