fix(trash): list a large trash, empty it, and browse it by folder

A large trash never finished listing, so the Trash page kept the rows of
the last refresh that got anywhere, and Empty Trash listed the trash before
sending the delete and hung there (B293). The paging fix is in
proton-drive-rs 0.12.5.

- The refresh reads only nodes it has not stored yet, without unlocking
  file keys, keeps the time the server says each was trashed, and drops
  rows only once the whole listing is in.
- Empty Trash sends the delete at once under the trash call time limit
  and clears the stored listing. Restore and Delete Permanently drop their
  rows right away.
- Schema v47 indexes trash.parent_uid and drops the stored rows once, so
  they are read again with real deletion times.
- The Trash page groups items by the folder they were deleted from, opens
  deleted folders through the new ListTrashedFolder request, and has a
  search box and a switch back to one list. Restore and Delete Permanently
  on a folder row act on everything deleted from it.
- pdfs trash takes --by-folder and --folder UID.
This commit is contained in:
nvrl committed 2026-11-22 02:38:48 +01:00
1 parent f141d88d97
commit d0f9681593
30 files changed
+2661 -1306

No files matched your search

+59
View File
@@ -337,6 +337,12 @@ impl Demo {
Request::ListDir { path } => Response::Entries {
entries: self.dir(path),
},
Request::ListTrash => Response::Entries {
entries: self.trash(),
},
Request::ListTrashedFolder { uid } => Response::Entries {
entries: self.trashed_folder(uid),
},
Request::ReserveFileThumbGeneration => Response::FileThumbGeneration { generation: 1 },
Request::FileThumbs { items, .. } => Response::Thumbs {
items: items
@@ -591,6 +597,59 @@ impl Demo {
.collect()
}
/// The trash: things deleted from a few folders, and a deleted folder with
/// a file deleted from it before it went.
fn trash(&self) -> Vec<DirEntry> {
let item = |name: &str, size: u64, hours: i64, from: &str| DirEntry {
name: name.into(),
is_dir: size == 0,
size,
modified: self.now - 40 * DAY,
uid: format!("demo-trash~{from}/{name}"),
trashed_at: self.now - hours * 3600,
trashed_from: Some(from.into()),
..Default::default()
};
let mut inside = item("Draft 3.odt", 48_120, 30, "");
inside.uid = "demo-trash~Old drafts/Draft 3.odt".into();
inside.trashed_from = None;
inside.trashed_parent = Some("demo-trash~Work/Old drafts".into());
vec![
item("Budget 2025.ods", 51_200, 2, "Documents"),
item("Scan 0042.pdf", 1_204_331, 5, "Documents"),
item("Scan 0043.pdf", 1_198_002, 5, "Documents"),
item("IMG_2041.jpg", 3_402_112, 20, "Pictures/Lisbon"),
item("IMG_2042.jpg", 3_311_870, 20, "Pictures/Lisbon"),
item("IMG_2043.jpg", 3_587_004, 20, "Pictures/Lisbon"),
item("Old drafts", 0, 26, "Work"),
item("Meeting notes (copy).md", 6_120, 50, ""),
item("Invoice 118.pdf", 88_341, 80, "Taxes/2025"),
inside,
]
}
/// What the deleted folder in [`Self::trash`] holds.
fn trashed_folder(&self, uid: &str) -> Vec<DirEntry> {
if uid != "demo-trash~Work/Old drafts" {
return Vec::new();
}
let file = |name: &str, size: u64| DirEntry {
name: name.into(),
size,
modified: self.now - 50 * DAY,
uid: format!("demo-trash~Old drafts/{name}"),
trashed_parent: Some(uid.into()),
..Default::default()
};
let mut gone = file("Draft 3.odt", 48_120);
gone.trashed_at = self.now - 30 * 3600;
vec![
file("Draft 1.odt", 41_002),
file("Draft 2.odt", 44_870),
gone,
]
}
/// My files: folders first, then a few files, some of them pictures with
/// thumbnails. Any folder below the root holds the same few documents.
fn dir(&self, path: &str) -> Vec<DirEntry> {
+17 -2
View File
@@ -515,6 +515,13 @@ pub enum Request {
/// its `uid` — the handle for [`Request::Restore`] and
/// [`Request::DeleteForever`] — and its `path` is empty.
ListTrash,
/// List what a trashed folder holds, by the folder's `uid`. Replies with
/// [`Response::Entries`]; each entry's `trashed_parent` is that folder.
/// The contents went to the trash with the folder, so only the entries
/// [`Request::ListTrash`] also lists can be restored on their own.
ListTrashedFolder {
uid: String,
},
/// Restore trashed nodes, by uid, to the folders they were trashed from.
/// Replies with [`Response::Ok`].
Restore {
@@ -1566,8 +1573,8 @@ pub struct DirEntry {
#[serde(default)]
pub shared_by_unverified: bool,
/// When a [`Request::ListTrash`] entry went to the trash, epoch seconds; 0
/// when unknown, and for anything not in the trash. The API does not report
/// it to this client, so the daemon notes when it first saw the node there.
/// when unknown, and for anything not in the trash. Taken from the server;
/// when it gives none, the daemon notes when it first saw the node there.
#[serde(default)]
pub trashed_at: i64,
/// The folder a [`Request::ListTrash`] entry goes back to on restore, as a
@@ -1575,6 +1582,11 @@ pub struct DirEntry {
/// not known, and for anything not in the trash.
#[serde(default)]
pub trashed_from: Option<String>,
/// The trashed folder a [`Request::ListTrash`] entry was trashed inside, by
/// uid, when that folder is in the trash too; `None` otherwise. Lets a
/// front end show the trash as folders.
#[serde(default)]
pub trashed_parent: Option<String>,
/// Why Drive refused a change queued on the node, for a [`Request::ListDir`]
/// entry; `None` otherwise.
#[serde(default)]
@@ -3625,6 +3637,9 @@ mod tests {
fn trash_requests_roundtrip() {
let reqs = [
Request::ListTrash,
Request::ListTrashedFolder {
uid: "vol~folder".into(),
},
Request::Restore {
uids: vec!["vol~link".into(), "vol~other".into()],
},
+20 -1
View File
@@ -11,7 +11,7 @@ use super::Db;
use crate::Result;
/// Current schema version. Bump on every forward migration added below.
pub(super) const SCHEMA_VERSION: i64 = 46;
pub(super) const SCHEMA_VERSION: i64 = 47;
impl Db {
pub(super) fn migrate(&self) -> Result<()> {
@@ -501,6 +501,16 @@ impl Db {
tx.execute_batch(MIGRATION_V46_ALBUM_PHOTOS)?;
}
}
if current < 47 {
let has_trash: bool = tx.query_row(
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'trash'",
[],
|row| row.get::<_, i64>(0).map(|n| n > 0),
)?;
if has_trash {
tx.execute_batch(MIGRATION_V47)?;
}
}
tx.execute(
"INSERT INTO sync_state (key, value) VALUES ('schema_version', ?1)
ON CONFLICT(key) DO UPDATE SET value = excluded.value",
@@ -1480,3 +1490,12 @@ const MIGRATION_V46_ALBUM_PHOTOS: &str = "
ALTER TABLE album_photos ADD COLUMN resolved INTEGER NOT NULL DEFAULT 0;
UPDATE album_photos SET resolved=1 WHERE name IS NOT NULL;
";
/// Index the trash by parent, for browsing it by folder. The rows are dropped
/// so the next refresh reads them again with the time each was trashed, which
/// earlier builds did not get from the server and made up instead.
const MIGRATION_V47: &str = "
CREATE INDEX IF NOT EXISTS trash_parent ON trash(parent_uid);
DELETE FROM trash;
DELETE FROM sync_state WHERE key IN ('trash_synced_ms', 'trash_complete_ms');
";
+48
View File
@@ -650,6 +650,54 @@ fn trash_replace_lists_folders_first() {
assert!(db.trash_list().unwrap().is_empty());
}
fn stored_trash(uid: &str, name: &str, trashed_at: Option<i64>) -> StoredTrash {
StoredTrash {
uid: uid.into(),
name: name.into(),
is_dir: false,
size: 1,
mtime: 1,
parent_uid: None,
trashed_at,
}
}
#[test]
fn trash_upsert_keeps_other_rows_and_retain_drops_unlisted_ones() {
let db = Db::open_in_memory().unwrap();
db.trash_upsert(&[
stored_trash("a", "a.txt", Some(1)),
stored_trash("b", "b.txt", None),
])
.unwrap();
// A later refresh adds what is new without touching what it kept.
db.trash_upsert(&[
stored_trash("c", "c.txt", Some(3)),
stored_trash("b", "b2.txt", Some(2)),
])
.unwrap();
let mut names: Vec<String> = db
.trash_list()
.unwrap()
.into_iter()
.map(|i| i.name)
.collect();
names.sort();
assert_eq!(names, ["a.txt", "b2.txt", "c.txt"]);
// Rows the server no longer lists go; the listed ones stay.
let keep: std::collections::HashSet<String> = ["a", "c"].map(String::from).into();
assert_eq!(db.trash_retain(&keep).unwrap(), 1);
assert_eq!(db.trash_remove(&["a".into(), "gone".into()]).unwrap(), 1);
let left: Vec<String> = db
.trash_list()
.unwrap()
.into_iter()
.map(|i| i.uid)
.collect();
assert_eq!(left, ["c"]);
}
#[test]
fn state_stamps_round_trip_and_clear() {
let db = Db::open_in_memory().unwrap();
+55 -1
View File
@@ -1,6 +1,6 @@
//! Trashed nodes, persisted so the trash view opens without a round-trip.
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use rusqlite::params;
@@ -48,6 +48,60 @@ impl Db {
Ok(())
}
/// Add or update trashed nodes, keeping every other row.
pub fn trash_upsert(&self, items: &[StoredTrash]) -> Result<()> {
let mut conn = self.conn.lock();
let tx = conn.transaction()?;
{
let mut stmt = tx.prepare(
"INSERT INTO trash (uid, name, is_dir, size, mtime, parent_uid, trashed_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
ON CONFLICT(uid) DO UPDATE SET
name = excluded.name, is_dir = excluded.is_dir, size = excluded.size,
mtime = excluded.mtime, parent_uid = excluded.parent_uid,
trashed_at = excluded.trashed_at",
)?;
for item in items {
stmt.execute(params![
item.uid,
item.name,
item.is_dir as i64,
item.size,
item.mtime,
item.parent_uid,
item.trashed_at
])?;
}
}
tx.commit()?;
Ok(())
}
/// Drop the trashed nodes with these uids. Returns how many rows went.
pub fn trash_remove(&self, uids: &[String]) -> Result<usize> {
let mut conn = self.conn.lock();
let tx = conn.transaction()?;
let mut removed = 0;
{
let mut stmt = tx.prepare("DELETE FROM trash WHERE uid = ?1")?;
for uid in uids {
removed += stmt.execute(params![uid])?;
}
}
tx.commit()?;
Ok(removed)
}
/// Drop every trashed node not in `keep`. Returns how many rows went.
pub fn trash_retain(&self, keep: &HashSet<String>) -> Result<usize> {
let gone: Vec<String> = self
.trash_seen()?
.into_keys()
.filter(|uid| !keep.contains(uid))
.collect();
self.trash_remove(&gone)
}
/// The persisted trash listing, folders first then by name — the order the
/// Trash page shows it in.
pub fn trash_list(&self) -> Result<Vec<StoredTrash>> {