Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f6456829fb | ||
|
|
327f538fed | ||
|
|
ad8b225cfd | ||
|
|
ea27f2bcd0 | ||
|
|
e4062cc97b | ||
|
|
c34cc93c45 | ||
|
|
7e3df7602d | ||
|
|
61a0d31408 | ||
|
|
644a47eb36 | ||
|
|
cd90c566cd | ||
|
|
1c262eb601 | ||
|
|
bf73a395e5 | ||
|
|
a5138639a8 | ||
|
|
988b78e810 | ||
|
|
2c46d62429 | ||
|
|
97743f17e3 | ||
|
|
3d10338807 | ||
|
|
d53ba1be21 | ||
|
|
c73d5c9436 | ||
|
|
8d92c69279 | ||
|
|
e32959e40a | ||
|
|
0b27c884a7 | ||
|
|
37c04f1e69 | ||
|
|
625776081b | ||
|
|
8cde79b8e3 | ||
|
|
eb53d122cc | ||
|
|
c64252193f | ||
|
|
cc70a21c05 | ||
|
|
644995cc56 | ||
|
|
50483bf3e1 | ||
|
|
4caba0a34c | ||
|
|
e8554d6434 | ||
|
|
ba1fdd65be | ||
|
|
0d4163b9bf | ||
|
|
f3c7a809f4 | ||
|
|
c1b5378045 | ||
|
|
3d6f53c16d | ||
|
|
8ceee5285b | ||
|
|
a181f1a5f0 | ||
|
|
7251f00e91 | ||
|
|
cb38f79aa3 | ||
|
|
54e9921efb | ||
|
|
f252b09757 | ||
|
|
a2992b3b9d | ||
|
|
7b67db5ced | ||
|
|
84c9ade8e3 | ||
|
|
9e26cd0285 | ||
|
|
73e60f1151 | ||
|
|
088e8f3b0d | ||
|
|
261d5dbf84 | ||
|
|
5f9d07aff1 | ||
|
|
3fb7b9be6b | ||
|
|
6c3a50ce7f | ||
|
|
f8304a0e86 | ||
|
|
e63f39f8c2 | ||
|
|
6f100dbe5e | ||
|
|
7c5ef269cd | ||
|
|
f13b6fc0ef | ||
|
|
3d61fc637b | ||
|
|
889b2aa4a9 | ||
|
|
8bd3867cb3 | ||
|
|
9577387c2f | ||
|
|
7a087b8827 | ||
|
|
7099106802 | ||
|
|
43385d36ee | ||
|
|
4b97f401cb | ||
|
|
af869944cf | ||
|
|
4b90abf3b5 | ||
|
|
61266fc9b1 | ||
|
|
d18655538e | ||
|
|
da7ea0ec56 | ||
|
|
127b71efd0 | ||
|
|
2339b17d97 | ||
|
|
bd4f797d1f | ||
|
|
e872cad058 | ||
|
|
081c8fb889 |
No files matched your search
@@ -45,7 +45,7 @@ jobs:
|
||||
run: pacman -Syu --noconfirm --needed git openssh pacman-contrib
|
||||
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v7
|
||||
with:
|
||||
# The updated recipes are committed back to main, not to the tag.
|
||||
ref: main
|
||||
|
||||
@@ -13,7 +13,7 @@ jobs:
|
||||
rust:
|
||||
runs-on: ubuntu-24.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v7
|
||||
|
||||
- name: Install native dependencies
|
||||
run: |
|
||||
@@ -32,8 +32,14 @@ jobs:
|
||||
with:
|
||||
toolchain: '1.96'
|
||||
components: rustfmt, clippy
|
||||
cache: false
|
||||
|
||||
# Tags can only restore caches saved on main, so only main saves one. The
|
||||
# release workflow restores this key for its quality gates.
|
||||
- uses: swatinem/rust-cache@v2
|
||||
with:
|
||||
shared-key: debug
|
||||
save-if: ${{ github.ref == 'refs/heads/main' }}
|
||||
|
||||
- name: Formatting
|
||||
run: cargo fmt --all -- --check
|
||||
@@ -57,3 +63,74 @@ jobs:
|
||||
|
||||
# Real Proton credentials and /dev/fuse deliberately do not run on pull
|
||||
# requests. Follow docs/TESTING.md on a dedicated test-account runner.
|
||||
|
||||
# The seeded simulation runs (docs/MILESTONE-3.0.0.md §8.2): real FUSE
|
||||
# daemons against an in-memory Drive, so they need /dev/fuse but no account.
|
||||
simulation:
|
||||
if: github.event_name != 'pull_request'
|
||||
runs-on: ubuntu-24.04
|
||||
timeout-minutes: 30
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
|
||||
- name: Install native dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y \
|
||||
pkg-config \
|
||||
fuse3 \
|
||||
libfuse3-dev \
|
||||
libgtk-4-dev \
|
||||
libadwaita-1-dev \
|
||||
libwebkitgtk-6.0-dev \
|
||||
libsecret-1-dev \
|
||||
libdbus-1-dev
|
||||
|
||||
- name: Install minimum supported Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: '1.96'
|
||||
cache: false
|
||||
|
||||
- uses: swatinem/rust-cache@v2
|
||||
with:
|
||||
shared-key: debug
|
||||
save-if: false
|
||||
|
||||
- name: Simulation runs
|
||||
run: 'cargo test -p pdfs-fuse --lib --locked sim:: -- --ignored --test-threads=1'
|
||||
|
||||
# Keeps the release-profile dependencies cached on main, where the release
|
||||
# workflow can restore them; a tag cannot reuse the caches of earlier tags.
|
||||
release-cache:
|
||||
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||
runs-on: ubuntu-24.04
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v7
|
||||
|
||||
- name: Install native dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y \
|
||||
pkg-config \
|
||||
libfuse3-dev \
|
||||
libgtk-4-dev \
|
||||
libadwaita-1-dev \
|
||||
libwebkitgtk-6.0-dev \
|
||||
libsecret-1-dev \
|
||||
libdbus-1-dev
|
||||
|
||||
- name: Install minimum supported Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: '1.96'
|
||||
cache: false
|
||||
|
||||
- uses: swatinem/rust-cache@v2
|
||||
with:
|
||||
shared-key: release
|
||||
|
||||
- name: Release build
|
||||
run: cargo build --release --locked
|
||||
@@ -10,14 +10,66 @@ permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
checks:
|
||||
name: Release quality gates
|
||||
runs-on: ubuntu-24.04
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v7
|
||||
|
||||
- name: Install system dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y \
|
||||
pkg-config \
|
||||
libfuse3-dev \
|
||||
libgtk-4-dev \
|
||||
libadwaita-1-dev \
|
||||
libwebkitgtk-6.0-dev \
|
||||
libsecret-1-dev \
|
||||
libdbus-1-dev
|
||||
|
||||
- name: Set up Rust
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: '1.96'
|
||||
components: rustfmt, clippy
|
||||
cache: false
|
||||
|
||||
# A tag can read the caches of main but its own caches are useless to the next
|
||||
# tag, so this only restores what CI saved on main.
|
||||
- name: Cache Cargo target
|
||||
uses: swatinem/rust-cache@v2
|
||||
with:
|
||||
shared-key: debug
|
||||
save-if: false
|
||||
|
||||
- name: Verify tag and manifest versions
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
shell: bash
|
||||
run: |
|
||||
tag_version="${GITHUB_REF_NAME#v}"
|
||||
manifest_version="$(sed -n 's/^version = "\([^"]*\)"/\1/p' Cargo.toml | head -n1)"
|
||||
pkgbuild_version="$(sed -n 's/^pkgver=//p' packaging/PKGBUILD | head -n1)"
|
||||
test "$tag_version" = "$manifest_version"
|
||||
test "$tag_version" = "$pkgbuild_version"
|
||||
|
||||
- name: Release quality gates
|
||||
run: |
|
||||
cargo fmt --all -- --check
|
||||
cargo clippy --workspace --all-targets -- -D warnings
|
||||
cargo test --workspace --locked
|
||||
scripts/fuse-acceptance.sh --offline-only
|
||||
|
||||
build-release:
|
||||
name: Build and Release
|
||||
name: Build packages
|
||||
runs-on: ubuntu-24.04
|
||||
environment: 'rpm signing'
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v7
|
||||
|
||||
- name: Install system dependencies
|
||||
run: |
|
||||
@@ -38,27 +90,14 @@ jobs:
|
||||
uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: '1.96'
|
||||
components: rustfmt, clippy
|
||||
cache: false
|
||||
|
||||
# Restores the release-profile dependencies that CI builds on main.
|
||||
- name: Cache Cargo target
|
||||
uses: swatinem/rust-cache@v2
|
||||
|
||||
- name: Verify tag and manifest versions
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
shell: bash
|
||||
run: |
|
||||
tag_version="${GITHUB_REF_NAME#v}"
|
||||
manifest_version="$(sed -n 's/^version = "\([^"]*\)"/\1/p' Cargo.toml | head -n1)"
|
||||
pkgbuild_version="$(sed -n 's/^pkgver=//p' packaging/PKGBUILD | head -n1)"
|
||||
test "$tag_version" = "$manifest_version"
|
||||
test "$tag_version" = "$pkgbuild_version"
|
||||
|
||||
- name: Release quality gates
|
||||
run: |
|
||||
cargo fmt --all -- --check
|
||||
cargo clippy --workspace --all-targets -- -D warnings
|
||||
cargo test --workspace --locked
|
||||
scripts/fuse-acceptance.sh --offline-only
|
||||
with:
|
||||
shared-key: release
|
||||
save-if: false
|
||||
|
||||
- name: Build release binaries
|
||||
run: |
|
||||
@@ -127,7 +166,7 @@ jobs:
|
||||
Maintainer: Nils Pukropp <contact@narl.io>
|
||||
Depends: fuse3, libgtk-4-1 (>= 4.12.0), libadwaita-1-0 (>= 1.5.0), libsecret-1-0, libwebkitgtk-6.0-4, libimage-exiftool-perl
|
||||
Installed-Size: ${INSTALLED_SIZE}
|
||||
Homepage: https://github.com/narrrl/proton-drive-linux
|
||||
Homepage: https://proton-drive.narl.io
|
||||
Description: Unofficial Proton Drive client for Linux
|
||||
Files-on-demand Proton Drive for the Linux desktop, unaffiliated with Proton AG:
|
||||
- FUSE virtual mount with block-level caching
|
||||
@@ -152,6 +191,7 @@ jobs:
|
||||
--define "_topdir $PWD/rpmbuild" \
|
||||
--define "_sourcedir $PWD" \
|
||||
--define "git_dir $PWD" \
|
||||
--define "prebuilt 1" \
|
||||
--define "version ${RPM_VERSION}" \
|
||||
--define "_dbpath $PWD/rpmbuild/db" \
|
||||
packaging/proton-drive-linux.spec
|
||||
@@ -181,7 +221,7 @@ jobs:
|
||||
|
||||
|
||||
- name: Upload Workflow Artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@v7
|
||||
with:
|
||||
name: proton-drive-linux-builds
|
||||
path: |
|
||||
@@ -190,9 +230,20 @@ jobs:
|
||||
*.rpm
|
||||
if-no-files-found: error
|
||||
|
||||
publish:
|
||||
name: Create GitHub Release
|
||||
needs: [checks, build-release]
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
runs-on: ubuntu-24.04
|
||||
|
||||
steps:
|
||||
- name: Download packages
|
||||
uses: actions/download-artifact@v8
|
||||
with:
|
||||
name: proton-drive-linux-builds
|
||||
|
||||
- name: Create GitHub Release
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
uses: softprops/action-gh-release@v2
|
||||
uses: softprops/action-gh-release@v3
|
||||
with:
|
||||
files: |
|
||||
*.deb
|
||||
@@ -205,7 +256,7 @@ jobs:
|
||||
|
||||
aur:
|
||||
name: Publish AUR packages
|
||||
needs: build-release
|
||||
needs: publish
|
||||
if: startsWith(github.ref, 'refs/tags/')
|
||||
uses: ./.github/workflows/aur.yml
|
||||
with:
|
||||
|
||||
@@ -6,9 +6,10 @@ deliberately high — everything else is ordinary Rust review.
|
||||
|
||||
## Before you start
|
||||
|
||||
- Read [`AGENTS.md`](AGENTS.md) for repository conventions and
|
||||
[`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md) for how the daemon, front ends, and SDK fit
|
||||
together.
|
||||
- Read [`AGENTS.md`](AGENTS.md) for repository conventions,
|
||||
[`docs/DEVELOPMENT.md`](docs/DEVELOPMENT.md) for building, running and debugging from source,
|
||||
and [`docs/ARCHITECTURE.md`](docs/ARCHITECTURE.md) for how the daemon, front ends, and SDK fit
|
||||
together. [`docs/README.md`](docs/README.md) indexes the rest.
|
||||
- Check [`docs/BUGS.md`](docs/BUGS.md) — it is the authoritative issue ledger, and your problem may
|
||||
already be tracked there with context.
|
||||
- For anything larger than a fix, open an issue first so the design can be discussed before you
|
||||
@@ -23,8 +24,9 @@ cargo build --workspace
|
||||
```
|
||||
|
||||
GUI and FUSE targets need the GTK4, libadwaita, WebKitGTK 6, libsecret, D-Bus, and FUSE3
|
||||
development packages listed in the [README](README.md#prerequisites). Minimum supported Rust
|
||||
version is 1.96.
|
||||
development packages listed in [`docs/INSTALL.md`](docs/INSTALL.md#build-dependencies). Minimum
|
||||
supported Rust version is 1.96. [`docs/DEVELOPMENT.md`](docs/DEVELOPMENT.md#running-from-source)
|
||||
explains how to run your build next to, or instead of, the installed service.
|
||||
|
||||
## Quality gates
|
||||
|
||||
@@ -51,6 +53,13 @@ use a dedicated test account, never one holding data you cannot lose.
|
||||
control-socket work stays off GTK's main thread.
|
||||
- **`staging/` and `recovery/` may hold the only copy of user data.** Nothing may clear or purge
|
||||
them opportunistically.
|
||||
- **The GUI is fully translated.** Wrap every user-visible string in the helpers from
|
||||
`crates/pdfs-gui/src/i18n.rs`, run `po/update.sh`, and translate the new entries in every
|
||||
catalog in `po/LINGUAS` in the same change. See [`docs/TRANSLATING.md`](docs/TRANSLATING.md).
|
||||
The CLI and daemon messages stay in English.
|
||||
- **Document what users see.** A new command, setting or behaviour belongs in the matching page
|
||||
under `docs/` (user guide, CLI reference or configuration) and in the `[Unreleased]` section of
|
||||
[`docs/CHANGELOG.md`](docs/CHANGELOG.md).
|
||||
|
||||
## Commits and pull requests
|
||||
|
||||
|
||||
@@ -3474,7 +3474,7 @@ checksum = "2ee67f1008b1ba2321834326597b8e186293b049a023cdef258527550b9935b4"
|
||||
|
||||
[[package]]
|
||||
name = "pdfs-cli"
|
||||
version = "2.4.0"
|
||||
version = "2.10.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
@@ -3489,7 +3489,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pdfs-core"
|
||||
version = "2.4.0"
|
||||
version = "2.10.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"dbus-secret-service-keyring-store",
|
||||
@@ -3513,8 +3513,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pdfs-fuse"
|
||||
version = "2.4.0"
|
||||
version = "2.10.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.23.1",
|
||||
"fuser",
|
||||
"futures",
|
||||
@@ -3528,12 +3529,13 @@ dependencies = [
|
||||
"sha1 0.11.0",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pdfs-gui"
|
||||
version = "2.4.0"
|
||||
version = "2.10.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-channel",
|
||||
@@ -3773,9 +3775,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "proton-drive-rs"
|
||||
version = "0.7.0"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0f6996724e51e99f03d2843828de9477afc39baa764403cb69490cabc1c4d93"
|
||||
checksum = "d6ce113d234474821d34787436ee80e1458e3d8a8f97212219615ed315eb70be"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.23.1",
|
||||
@@ -3800,9 +3802,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "proton-sdk"
|
||||
version = "0.7.0"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91cc25654133bd5714407a1df0996eaf214a6e8e0bb95d8139bc42e7cee31c34"
|
||||
checksum = "2a33c67df9694f90ce60e5b34984275956244138511b4640ca0d100e1dac8b3d"
|
||||
dependencies = [
|
||||
"aes-gcm 0.11.1",
|
||||
"async-trait",
|
||||
|
||||
@@ -3,20 +3,20 @@ resolver = "2"
|
||||
members = ["crates/pdfs-core", "crates/pdfs-fuse", "crates/pdfs-cli", "crates/pdfs-gui"]
|
||||
|
||||
[workspace.package]
|
||||
version = "2.4.0"
|
||||
version = "2.10.0"
|
||||
edition = "2024"
|
||||
license = "MIT"
|
||||
authors = ["Nils Pukropp <contact@narl.io>"]
|
||||
rust-version = "1.96"
|
||||
repository = "https://github.com/narrrl/proton-drive-linux"
|
||||
homepage = "https://github.com/narrrl/proton-drive-linux"
|
||||
homepage = "https://proton-drive.narl.io"
|
||||
readme = "README.md"
|
||||
keywords = ["proton", "drive", "fuse", "sync", "linux"]
|
||||
categories = ["filesystem", "command-line-utilities"]
|
||||
|
||||
[workspace.dependencies]
|
||||
proton-sdk = { version = "0.7.0" }
|
||||
proton-drive-rs = { version = "0.7.0" }
|
||||
proton-sdk = { version = "0.7.2" }
|
||||
proton-drive-rs = { version = "0.7.2" }
|
||||
|
||||
# Internal crates.
|
||||
pdfs-core = { path = "crates/pdfs-core" }
|
||||
|
||||
@@ -4,513 +4,164 @@
|
||||
|
||||
# Proton Drive for Linux
|
||||
|
||||
**Files-on-demand Proton Drive for the Linux desktop** — a FUSE virtual mount with block-level
|
||||
caching, a scriptable CLI, and a native GTK4 app with tray and search launcher.
|
||||
**Proton Drive on the Linux desktop.** Your files as a folder that downloads on demand, synced
|
||||
local folders, Photos, sharing, and a native GTK4 app with tray and search launcher, plus a
|
||||
scriptable CLI.
|
||||
|
||||
**[proton-drive.narl.io](https://proton-drive.narl.io)**
|
||||
|
||||
[](https://github.com/narrrl/proton-drive-linux/actions/workflows/ci.yml)
|
||||
[](https://github.com/narrrl/proton-drive-linux/releases/latest)
|
||||
[](https://aur.archlinux.org/packages/proton-drive-for-linux)
|
||||
[](LICENSE)
|
||||
[](https://www.rust-lang.org)
|
||||
[](#prerequisites)
|
||||
|
||||
<img src="images/files.png" alt="The GTK4 file browser" width="820">
|
||||
<img src="images/files.png" alt="My files in the Proton Drive app" width="820">
|
||||
|
||||
</div>
|
||||
|
||||
> [!IMPORTANT]
|
||||
> This is an **unofficial**, community-built client. It is not affiliated with, endorsed by, or
|
||||
> supported by Proton AG. "Proton" and "Proton Drive" are trademarks of Proton AG, used here only
|
||||
> to describe what the software talks to. Use a test account first, and read
|
||||
> [`docs/RECOVERY.md`](docs/RECOVERY.md) before trusting it with data you cannot lose.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
# Debian / Ubuntu
|
||||
sudo apt install ./proton-drive-linux_*.deb
|
||||
|
||||
# Fedora
|
||||
sudo dnf install ./proton-drive-linux-*.rpm
|
||||
|
||||
# Arch Linux (AUR)
|
||||
paru -S proton-drive-for-linux # or -git / -bin
|
||||
```
|
||||
|
||||
Packages for each release are attached to the [latest
|
||||
release](https://github.com/narrrl/proton-drive-linux/releases/latest); see
|
||||
[Installation & Packages](#installation--packages) for details and
|
||||
[Building from Source](#building-from-source) to compile it yourself.
|
||||
|
||||
```bash
|
||||
pdfs login # sign in (SRP + 2FA, credentials go to the system keyring)
|
||||
pdfs status # mount state, queued writes, cache usage
|
||||
pdfs-app # the desktop app
|
||||
```
|
||||
|
||||
## Contents
|
||||
|
||||
- [Features](#features) · [Quick search](#quick-search-and-media-opening) · [Opening files](#choosing-how-files-open)
|
||||
- [Selective sync](#selective-sync-pdfsignore) · [Diagnostics](#diagnostics--maintenance) · [Scripting](#scripting---json)
|
||||
- [Performance](#performance--caching) · [Filesystem safety](#filesystem-safety) · [Screenshots](#screenshots)
|
||||
- [Prerequisites](#prerequisites) · [Building](#building-from-source) · [Packages](#installation--packages) · [Releases](#automated-releases-cicd)
|
||||
|
||||
**Documentation:** [Architecture](docs/ARCHITECTURE.md) · [Testing](docs/TESTING.md) ·
|
||||
[Recovery](docs/RECOVERY.md) · [Development](docs/DEVELOPMENT.md) · [Translating](docs/TRANSLATING.md) ·
|
||||
[Known issues](docs/BUGS.md) · [Changelog](docs/CHANGELOG.md) ·
|
||||
[Contributing](CONTRIBUTING.md) · [Security policy](SECURITY.md)
|
||||
> to describe what the software talks to. Try it with data you have another copy of first, and
|
||||
> read [what survives a lost machine](docs/RECOVERY.md) before you rely on it.
|
||||
|
||||
## Features
|
||||
|
||||
- **Files-on-Demand (FUSE)**: Mount your Proton Drive as a virtual filesystem where files are downloaded only when opened, utilizing smart block-level caching and disk-backed writes.
|
||||
- **Command-Line Interface (CLI)**: Manage your drive, authenticate, and monitor sync status directly from the terminal.
|
||||
- **Non-Blocking GTK4 Desktop App**: Browse files, manage pins, configure options, and monitor active transfers through a modern, native GUI with a fully non-blocking asynchronous main loop.
|
||||
- **File Thumbnails**: Browse locally cached image previews in Files, Shared, and Trash, including embedded previews from camera RAW files. Build a whole folder tree from the Files toolbar and resize the grid from the status bar.
|
||||
- **Shared Links & Invites**: Browse files shared with you by other users, and view/manage your own public shared links directly in the GUI.
|
||||
- **Local Backup (Computers)**: Sync and back up local directories (like Downloads, Documents, Pictures, etc.) directly to your Proton Drive account.
|
||||
- **Locations**: One page (and `pdfs locations`) listing every place Proton Drive occupies on this machine — the main mount plus each backed-up folder — with its mode, sync state, and whether it is read-only. Switching a folder between a full local copy and on-demand happens here.
|
||||
- **System Tray Integration**: Background indicator for status monitoring, quick actions, and fast sync controls.
|
||||
- **Unified Search Launcher (HUD)**: A resident Google Drive-style launcher (`pdfs-prompt`) that searches Proton Drive and local files together, ranks the best matches, tolerates abbreviations and typos, and is ideal for a system-wide hotkey. It can also present the same search in your own launcher (`--dmenu`) or, searching live as you type, in `fzf` (`--fzf`).
|
||||
- **Secure Credential Storage**: Integrates with the system Secret Service (GNOME Keyring, KWallet, etc.) with smart in-memory credential caching to avoid UI thread blockages.
|
||||
- **Proton Photos Support**: Access your Proton Photos timeline, view thumbnails, and download backed-up media natively (available in the GUI as a navigation tab and via the CLI).
|
||||
- **File Version History**: Every revision Proton Drive still holds for a file, from the browser's details pane (**Versions**) or `pdfs versions list|restore|save|rm`. Restoring is server-side — no re-upload — and an old version can be written out to a local file without touching the live one.
|
||||
- **Photo Favourites**: Star a photo in the lightbox, filter the gallery to favourites, or use `pdfs favorite <uid>` / `pdfs photos --favorites`.
|
||||
- **Photo Albums**: Browse your albums — including the ones shared with you — from the Albums view of the Photos page, or with `pdfs albums` / `pdfs album <uid>`. Album contents open in the same gallery as the timeline.
|
||||
- **Places**: The Places view of the Photos page groups photos by the town they were taken in, using the location stored with each photo, as cards or on a world map. Towns are looked up offline in [GeoNames](https://www.geonames.org/) data (CC BY 4.0), and the map is drawn offline from [Natural Earth](https://www.naturalearthdata.com/) land outlines and country borders (public domain). Each town is marked with one of its photos, and zoomed in, every photo sits where it was taken. A street map from [OpenFreeMap](https://openfreemap.org/) (© [OpenStreetMap](https://www.openstreetmap.org/copyright) contributors) can be turned on under Appearance; it is off by default because the tile server learns which parts of the world you look at. Install `iso-codes` for country names in your language.
|
||||
- **Human Verification (CAPTCHA) Recovery**: Detects sign-in gates (VPN/new IP challenges) and launches an embedded `WebKitWebView` dialog to safely complete the challenge, transparently retrying authentication with the earned token.
|
||||
- **Selective Sync (`.pdfsignore`)**: Keep build trees, dependency directories, and editor leftovers out of synced folders using gitignore-style rules.
|
||||
- **Data-Safe Offline Writes**: Durable scratch/staging files and a transactional pending queue preserve acknowledged writes across network failures and restarts.
|
||||
**Files**
|
||||
|
||||
## Quick Search and Media Opening
|
||||
- **Your Drive as a folder.** `~/ProtonDrive` works with every program. Files download when they
|
||||
are opened, a block at a time, so a video starts playing and a large archive can be read
|
||||
without fetching all of it.
|
||||
- **Available offline.** Keep chosen files and folders fully downloaded; everything else is
|
||||
cached within a size limit you set.
|
||||
- **Synced folders.** Back up folders such as `~/Documents` under your computer in Proton Drive,
|
||||
as a full two-way copy or online only. Gitignore-style `.pdfsignore` rules, conflict copies
|
||||
instead of overwrites, pause and bandwidth limits.
|
||||
- **Safe writes.** Changes are staged on disk and queued before they are acknowledged, so they
|
||||
survive a lost connection, a crash or a reboot and upload when they can.
|
||||
|
||||
`pdfs-prompt` searches Drive metadata and the local home-directory index in one request. Results are ranked together rather than split by source. Matching is case-insensitive and supports exact names, prefixes, parent paths, multiple terms, punctuation-separated words, ordered abbreviations, common typos, and adjacent transpositions.
|
||||
**Desktop app**
|
||||
|
||||
The prompt stays resident after its first launch, so invoking the shortcut again reuses and resets the existing window. Drive folders open from the mount. Audio and video results also open through FUSE, allowing media players to seek and request ranges without downloading the entire file first; ordinary Drive files are materialized into the local cache before opening.
|
||||
- **Browse** My files, Shared with me, Shared by me, Computers and Trash, with thumbnails
|
||||
(including camera RAW), drag and drop, details and **version history**.
|
||||
- **Photos.** Timeline with favorites, albums, and Places on a map; RAW+JPEG pairs shown as one
|
||||
photo; duplicate finder; slideshow; **import from Google Photos** (Takeout).
|
||||
- **Sharing.** Invite people as viewer, editor or admin, create public links with password and
|
||||
expiry, accept invitations.
|
||||
- **Sync overview.** What is uploading, what is waiting, what failed and why, and a history of
|
||||
what happened.
|
||||
- **Tray icon** with status, pause and quick actions, and a **search launcher** (`pdfs-prompt`)
|
||||
that searches Proton Drive and your home folder together, as its own window, through fuzzel,
|
||||
rofi or wofi, or live in `fzf`.
|
||||
- **Restore a computer.** Continue a backup on a new machine and bring its folders back in one
|
||||
step.
|
||||
- **Sign-in** with two-factor authentication and CAPTCHA; the session is kept in the system
|
||||
keyring.
|
||||
- Available in English, German, Spanish, French, Italian, Dutch, Polish and Brazilian
|
||||
Portuguese.
|
||||
|
||||
### Using your own launcher (fuzzel, rofi, wofi, …)
|
||||
**Command line**
|
||||
|
||||
`pdfs-prompt --dmenu` runs the same search through an external dmenu-style
|
||||
launcher instead of the built-in window, so the HUD matches the rest of a
|
||||
tiling-WM desktop:
|
||||
- About 55 `pdfs` commands cover files, sync, sharing, Photos, Trash, versions and diagnostics.
|
||||
Query commands print JSON with `--json`.
|
||||
|
||||
```bash
|
||||
pdfs-prompt --dmenu # fuzzel/rofi/wofi/tofi/bemenu/dmenu, whichever is installed
|
||||
pdfs-prompt --dmenu --menu 'fuzzel --dmenu --width 60'
|
||||
pdfs-prompt --dmenu --query invoice # skip straight to a search
|
||||
```
|
||||
## Install
|
||||
|
||||
A launcher filters a fixed list — it cannot ask for a new one per keystroke — so
|
||||
searching takes two steps, and the prompt says which step you are on:
|
||||
|
||||
- `Search Drive ›` lists your pinned files. Type anything that isn't one of them
|
||||
and press Enter to search for it.
|
||||
- `Drive: invoice ›` lists the results. Enter opens one; typing something else
|
||||
and pressing Enter searches again. Escape closes.
|
||||
|
||||
`--query` skips the first step. fuzzel and rofi also get file-type icons.
|
||||
|
||||
To make an existing keybinding use it without re-binding, and to pin the
|
||||
launcher command, set them in `config.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"prompt": {
|
||||
"mode": "dmenu",
|
||||
"menu": ["fuzzel", "--dmenu", "--width", "60"],
|
||||
"menu_limit": 50
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`--gtk` overrides `"mode": "dmenu"` for one invocation. A `{prompt}` token
|
||||
anywhere in `menu` is replaced by the prompt text; without one, the launcher's
|
||||
own prompt flag is appended.
|
||||
|
||||
### Live launcher search (fzf)
|
||||
|
||||
The two-step above exists because no dmenu-style launcher can call back for new
|
||||
results per keystroke. `fzf` can, so `pdfs-prompt --fzf` searches as you type,
|
||||
exactly like the built-in window:
|
||||
|
||||
```bash
|
||||
pdfs-prompt --fzf # pinned files, then live results as you type
|
||||
pdfs-prompt --fzf --query invoice # start on a search
|
||||
```
|
||||
|
||||
Every keystroke re-queries the daemon (debounced, like the GTK prompt) and fzf's
|
||||
own matcher is switched off, so the ordering you see is the daemon's ranking —
|
||||
abbreviations and typos included, which a launcher filtering a fixed list cannot
|
||||
do. Enter opens; Escape closes.
|
||||
|
||||
fzf is a terminal program, so when it is started without one — from a WM
|
||||
keybinding — it spawns a terminal for itself: foot, ghostty, kitty, alacritty,
|
||||
wezterm or xterm, whichever is installed. Each is launched with the app-id/class
|
||||
`pdfs-prompt`, so one window rule floats it whichever terminal you have. In
|
||||
Hyprland:
|
||||
|
||||
```
|
||||
windowrulev2 = float, class:^(pdfs-prompt)$
|
||||
windowrulev2 = size 900 500, class:^(pdfs-prompt)$
|
||||
windowrulev2 = center, class:^(pdfs-prompt)$
|
||||
bind = SUPER, space, exec, pdfs-prompt --fzf
|
||||
```
|
||||
|
||||
Set it as the default front end, and pick the terminal explicitly, in
|
||||
`config.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"prompt": {
|
||||
"mode": "fzf",
|
||||
"terminal": ["foot", "--app-id=pdfs-prompt", "--"],
|
||||
"menu_limit": 50
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
A `{cmd}` token anywhere in `terminal` is replaced by the command to run;
|
||||
without one it is appended, which is what every detected terminal expects.
|
||||
|
||||
## Choosing How Files Open
|
||||
|
||||
By default every result is handed to `xdg-open`. An `open_with` block in
|
||||
`config.json` overrides that per file type — for instance opening text in
|
||||
Neovim inside Alacritty, while everything else still goes to the desktop:
|
||||
|
||||
```json
|
||||
{
|
||||
"open_with": {
|
||||
"terminal": ["alacritty", "-e"],
|
||||
"rules": [
|
||||
{ "match": ["@text"], "command": ["nvim"], "terminal": true },
|
||||
{ "match": ["*.png", "*.jpg"], "command": ["imv"] }
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- `match` takes file-name globs (`*.md`, `notes-*.txt`) or the classes `@dir`,
|
||||
`@text`, `@document`, `@image`, `@media`, `@any`. First matching rule wins.
|
||||
- `command` is argv, not a shell line. A `{}` token is replaced by the path;
|
||||
without one the path is appended. `$VAR` tokens expand from the environment,
|
||||
so `["$EDITOR"]` follows your editor.
|
||||
- `"terminal": true` wraps the command in `terminal`. When that is unset, the
|
||||
terminal comes from `$PDFS_TERMINAL`, then `$TERMINAL`, then the first known
|
||||
emulator on `PATH`; a bare name like `TERMINAL=alacritty` gains the right
|
||||
"run this" flag automatically.
|
||||
- `"default": [...]` replaces `xdg-open` for everything unmatched.
|
||||
|
||||
The rules apply to the prompt (both front ends) and to the GTK browser, so a
|
||||
file opens the same way wherever it was picked. Patterns are matched against the
|
||||
Drive name — a downloaded file is stored in the cache under its content hash,
|
||||
which says nothing about its type, so matching that would send everything to
|
||||
`xdg-open`.
|
||||
|
||||
## Selective Sync (`.pdfsignore`)
|
||||
|
||||
Two-way synced folders skip paths matched by ignore rules, so syncing a project
|
||||
directory does not upload `node_modules/`, `target/`, or `.git/`.
|
||||
|
||||
Rules come from two places, and both apply:
|
||||
|
||||
1. **Per folder** — a `.pdfsignore` file at the root of the synced folder
|
||||
(`.protonignore` also works). Gitignore syntax, including negation:
|
||||
|
||||
```gitignore
|
||||
# everything build-related
|
||||
build/
|
||||
*.log
|
||||
|
||||
# ...except this one
|
||||
!important.log
|
||||
```
|
||||
|
||||
2. **Globally** — an `ignore_patterns` list in `config.json`, applied to every
|
||||
synced folder. When unset, sensible defaults apply: `.git/`, `.hg/`, `.svn/`,
|
||||
`node_modules/`, `target/`, `.venv/`, `__pycache__/`, `*~`, `*.swp`, `*.tmp`,
|
||||
`.DS_Store`, and `Thumbs.db`.
|
||||
|
||||
```json
|
||||
{
|
||||
"ignore_patterns": ["node_modules/", "target/", "*.iso"]
|
||||
}
|
||||
```
|
||||
|
||||
Set it to `[]` to opt out of the defaults entirely.
|
||||
|
||||
Rules are re-read at the start of every sync pass, so edits take effect on the
|
||||
next pass without restarting the daemon.
|
||||
|
||||
**Ignoring is never destructive.** If a rule starts matching a file that was
|
||||
already synced, its copy on Drive is left untouched — the file simply stops
|
||||
being tracked. Removing the rule later picks the existing remote file back up
|
||||
rather than re-uploading it.
|
||||
|
||||
## Diagnostics & Maintenance
|
||||
|
||||
When something looks wrong, `pdfs diagnose` checks the installation and prints a
|
||||
report. It runs without a daemon on purpose — the state worth diagnosing is
|
||||
usually the state where the daemon will not start:
|
||||
|
||||
```console
|
||||
$ pdfs diagnose
|
||||
Paths
|
||||
[ok ] state dir: /home/you/.local/state/proton-drive-linux
|
||||
[ok ] database: /home/you/.local/state/proton-drive-linux/cache.db (170.3 MiB)
|
||||
|
||||
Account
|
||||
[ok ] keyring session: you@proton.me
|
||||
|
||||
Daemon
|
||||
[ok ] daemon responding
|
||||
[ok ] mounted at: /home/you/ProtonDrive
|
||||
[ok ] queued writes: none
|
||||
|
||||
No problems found.
|
||||
```
|
||||
|
||||
It exits non-zero if any check fails, so it works in a health-check script.
|
||||
|
||||
For the local metadata database and content cache:
|
||||
|
||||
| Command | What it does |
|
||||
| Distribution | Package |
|
||||
|---|---|
|
||||
| `pdfs cache inspect` | Database size, reclaimable space, per-table row counts, cache usage against budget |
|
||||
| `pdfs cache inspect --deep` | Also runs SQLite's integrity check — reads every page, so it is slow on a large database |
|
||||
| `pdfs cache vacuum` | Checkpoints the write-ahead log and compacts the database |
|
||||
| `pdfs cache clear` | Deletes cached file content, keeping pinned files |
|
||||
| Debian, Ubuntu 24.04 or newer | `.deb` from the [latest release](https://github.com/narrrl/proton-drive-linux/releases/latest) |
|
||||
| Fedora 44 or newer | `.rpm` from the [latest release](https://github.com/narrrl/proton-drive-linux/releases/latest) |
|
||||
| Arch Linux | [`proton-drive-for-linux`](https://aur.archlinux.org/packages/proton-drive-for-linux) (also `-bin` and `-git`) in the AUR |
|
||||
| Anything else | [Build from source](docs/INSTALL.md#build-from-source) |
|
||||
|
||||
`vacuum` takes a write lock for its duration and needs room for a second copy of
|
||||
the database while it runs, so it is a deliberate operation rather than
|
||||
something the daemon does on a timer.
|
||||
|
||||
Before a release, run the real-kernel acceptance suite against a dedicated test
|
||||
account and mount. See [`docs/TESTING.md`](docs/TESTING.md); it includes optional
|
||||
two-mount convergence checks and confines destructive operations to a fresh
|
||||
test directory.
|
||||
|
||||
## Scripting (`--json`)
|
||||
|
||||
Query commands accept a global `--json` flag and emit machine-readable output:
|
||||
|
||||
```console
|
||||
$ pdfs --json sync list | jq '.items[] | select(.state != "idle") | .local_path'
|
||||
$ pdfs --json cache inspect | jq '.db_reclaimable_bytes'
|
||||
$ pdfs --json status | jq -r '.mount.mountpoint'
|
||||
```bash
|
||||
sudo apt install ./proton-drive-linux_*.deb # Debian, Ubuntu
|
||||
sudo dnf install ./proton-drive-linux-*.rpm # Fedora
|
||||
paru -S proton-drive-for-linux # Arch
|
||||
```
|
||||
|
||||
Supported on `status`, `ls`, `pins`, `sync list`, `devices list`, `transfers`,
|
||||
`activity`, and `cache inspect`. Commands that perform an action keep their
|
||||
human output — a script that needs to know whether one worked has the exit code.
|
||||
Then open **Proton Drive** from your applications menu and sign in. The app starts the background
|
||||
service, and your files appear in `~/ProtonDrive`.
|
||||
|
||||
Two things worth relying on:
|
||||
Signing in from a terminal instead? `pdfs login` stores the session but does not start the
|
||||
service. Enable it once:
|
||||
|
||||
- **The payload is unwrapped.** Output is `{"items": […]}`, not the daemon's
|
||||
internal `{"SyncFolders": {"items": […]}}`, so scripts never name an internal
|
||||
variant.
|
||||
- **Errors still fail.** A daemon-side error prints its JSON body (with a
|
||||
machine-readable `kind`) on stdout *and* exits non-zero, so `set -e` and
|
||||
`if pdfs …` behave as expected rather than treating a failed lookup as
|
||||
success.
|
||||
```bash
|
||||
pdfs login
|
||||
systemctl --user enable --now proton-drive.service
|
||||
pdfs status
|
||||
```
|
||||
|
||||
## Performance & Caching
|
||||
[docs/INSTALL.md](docs/INSTALL.md) has the requirements, optional extras (tray on GNOME, RAW and
|
||||
video thumbnails), headless use and uninstalling.
|
||||
|
||||
The client includes several optimizations designed for high efficiency, a low memory footprint, and a responsive user experience:
|
||||
## Documentation
|
||||
|
||||
- **On-Demand Block Cache**: Files are read in fixed 4 MiB blocks. For unpinned files, the client fetches and caches only the blocks containing the requested byte ranges. This enables fast sequential and sparse reads (e.g. media streaming or metadata scanning) without downloading entire files.
|
||||
- **Disk-Backed Writes**: Large file writes are staged on disk in temporary scratch files (rather than fully buffered in RAM) and track modified byte intervals. Only the unedited remote segments are fetched at commit time, keeping memory usage minimal.
|
||||
- **Non-Blocking GTK4 Loop**: To prevent UI freezes, all synchronous D-Bus credential checks, control socket requests, and cache usage calculations are offloaded to background worker threads or fetched asynchronously.
|
||||
- **Flicker-Free UI Rendering**: The GTK4 application performs differential rendering of the pins list, only modifying list rows when the list content changes, preserving the user's scroll position and widget focus.
|
||||
- **Durable Staging**: Scratch metadata and staged writes are synced and atomically published before an upload is acknowledged locally. Rapid revisions supersede pending work transactionally.
|
||||
|
||||
## Filesystem Safety
|
||||
|
||||
Version 1.0 strengthens the boundaries around local-only data and destructive reconciliation:
|
||||
|
||||
- Truncate composes with queued revisions, preserving the authoritative prefix and correct zero-filled growth.
|
||||
- Combined move-and-rename operations are queued durably and tolerate partially completed remote state.
|
||||
- Session tombstones prevent successfully unlinked entries from reappearing through eventually consistent listings.
|
||||
- Names are valid UTF-8 and no longer than Linux's 255-byte component limit.
|
||||
- Incomplete mirror scans are non-destructive, and total-wipe protection covers every non-empty baseline.
|
||||
- Failed conflict preservation, local deletion, or staging publication does not settle a new baseline or discard the only local copy.
|
||||
- State, cache, and config directories must be real, current-user-owned `0700` directories; control sockets are `0600`.
|
||||
| | |
|
||||
|---|---|
|
||||
| [User guide](docs/USER_GUIDE.md) | Everything the app, tray and launcher do |
|
||||
| [Command-line reference](docs/CLI.md) | Every `pdfs` command and scripting with `--json` |
|
||||
| [Configuration](docs/CONFIGURATION.md) | `config.json`, file locations, environment variables, the service |
|
||||
| [Troubleshooting](docs/TROUBLESHOOTING.md) | Diagnosing problems and fixing common ones |
|
||||
| [Recovering on a new computer](docs/RECOVERY.md) | Getting your synced folders back after a reinstall or a lost machine |
|
||||
| [Changelog](docs/CHANGELOG.md) | What changed in each release |
|
||||
|
||||
For contributors: [development guide](docs/DEVELOPMENT.md), [architecture](docs/ARCHITECTURE.md),
|
||||
[testing](docs/TESTING.md), [translating](docs/TRANSLATING.md), [roadmap](docs/ROADMAP.md) and the
|
||||
[bug ledger](docs/BUGS.md). The [documentation index](docs/README.md) lists everything.
|
||||
|
||||
## Screenshots
|
||||
|
||||
### GUI Application & Launcher
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td align="center" width="50%"><img src="images/login.png" alt="Login Screen" width="100%"><br><sub><b>Login Screen</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/files.png" alt="Files Browser" width="100%"><br><sub><b>Files Browser</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/files.png" alt="My files" width="100%"><br><sub><b>My files</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/photos.png" alt="Photos timeline" width="100%"><br><sub><b>Photos</b></sub></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" width="50%"><img src="images/shared.png" alt="Shared Links" width="100%"><br><sub><b>Shared Links</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/shared_with_me.png" alt="Shared with me" width="100%"><br><sub><b>Shared with me</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/photos_map.png" alt="Photos places map" width="100%"><br><sub><b>Places</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/computers.png" alt="Computers" width="100%"><br><sub><b>Computers</b></sub></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" width="50%"><img src="images/restore_devices.png" alt="Local Backups (Computers)" width="100%"><br><sub><b>Local Backups (Computers)</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/photos.png" alt="Photos Timeline" width="100%"><br><sub><b>Photos Timeline</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/sync.png" alt="Sync folders" width="100%"><br><sub><b>Sync</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/conflicts.png" alt="Sync history showing resolved conflicts" width="100%"><br><sub><b>Sync history</b></sub></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" width="50%"><img src="images/prompt_gtk.png" alt="Search Launcher Prompt (GTK)" width="100%"><br><sub><b>Search Launcher Prompt (GTK)</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/prompt_fzf.png" alt="Search Launcher Prompt (fzf)" width="100%"><br><sub><b>Search Launcher Prompt (fzf)</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/prompt_gtk.png" alt="Search launcher" width="100%"><br><sub><b>Search launcher</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/prompt_fzf.png" alt="Search launcher in fzf" width="100%"><br><sub><b>Search launcher in fzf</b></sub></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center" width="50%"><img src="images/activity.png" alt="Activity" width="100%"><br><sub><b>Activity</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/settings.png" alt="Settings" width="100%"><br><sub><b>Settings</b></sub></td>
|
||||
<td align="center" width="50%"><img src="images/login.png" alt="Sign in" width="100%"><br><sub><b>Sign in</b></sub></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
---
|
||||
## How it works
|
||||
|
||||
## Prerequisites
|
||||
A background service (`proton-drive.service`, the `pdfs daemon` command) holds your session,
|
||||
mounts `~/ProtonDrive` through FUSE, runs the upload queue and the sync engine, and follows
|
||||
Proton's event stream for changes made elsewhere. The app, the tray, the launcher and the CLI are
|
||||
clients of that service over a local socket. Everything is end-to-end encrypted with your Proton
|
||||
keys before it leaves the machine, through a Rust implementation of Proton's Drive SDK.
|
||||
|
||||
To compile the application from source or run the built binaries, ensure you have the following system libraries installed on your distribution:
|
||||
|
||||
### Ubuntu / Debian (24.04+)
|
||||
```bash
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y \
|
||||
pkg-config \
|
||||
libfuse3-dev \
|
||||
libgtk-4-dev \
|
||||
libadwaita-1-dev \
|
||||
libwebkitgtk-6.0-dev \
|
||||
libsecret-1-dev \
|
||||
libdbus-1-dev \
|
||||
libimage-exiftool-perl
|
||||
```
|
||||
|
||||
### Arch Linux
|
||||
```bash
|
||||
sudo pacman -S --needed pkgconf fuse3 gtk4 libadwaita libsecret dbus webkitgtk-6.0 perl-image-exiftool
|
||||
```
|
||||
|
||||
### Fedora (44+)
|
||||
```bash
|
||||
sudo dnf install -y \
|
||||
pkgconf-pkg-config fuse3-devel gtk4-devel libadwaita-devel \
|
||||
webkitgtk6.0-devel libsecret-devel dbus-devel glib2-devel \
|
||||
perl-Image-ExifTool cargo rust
|
||||
```
|
||||
|
||||
Runtime extras (pick your desktop):
|
||||
```bash
|
||||
# GNOME — keyring + tray (AppIndicator)
|
||||
sudo dnf install -y gnome-keyring gnome-shell-extension-appindicator xdg-utils
|
||||
|
||||
# KDE Plasma — KWallet (Secret Service); tray works via built-in SNI
|
||||
sudo dnf install -y kwallet xdg-utils
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Building from Source
|
||||
|
||||
Ensure you have Rust and Cargo installed (minimum supported Rust version is 1.96).
|
||||
|
||||
1. Clone the repository and navigate into the project directory:
|
||||
```bash
|
||||
git clone https://github.com/narrrl/proton-drive-linux.git
|
||||
cd proton-drive-linux
|
||||
```
|
||||
2. Build the workspace in release mode:
|
||||
```bash
|
||||
cargo build --release --locked
|
||||
```
|
||||
|
||||
The compiled binaries will be available under `target/release/`:
|
||||
- `pdfs`: The CLI utility.
|
||||
- `pdfs-app`: The GTK4 application.
|
||||
- `pdfs-tray`: The tray status notifier.
|
||||
- `pdfs-prompt`: The launcher prompt for quick HUD search.
|
||||
|
||||
---
|
||||
|
||||
## Installation & Packages
|
||||
|
||||
### 1. Debian / Ubuntu (.deb)
|
||||
Install the debian package via `dpkg` or `apt`:
|
||||
```bash
|
||||
sudo apt install ./proton-drive-linux_*.deb
|
||||
```
|
||||
|
||||
### 2. Arch Linux (AUR)
|
||||
Three AUR packages cover the usual choices; they conflict with each other, so
|
||||
install one:
|
||||
|
||||
| Package | Contents |
|
||||
| --- | --- |
|
||||
| [`proton-drive-for-linux`](https://aur.archlinux.org/packages/proton-drive-for-linux) | builds the latest tagged release from source |
|
||||
| [`proton-drive-for-linux-git`](https://aur.archlinux.org/packages/proton-drive-for-linux-git) | builds `main` from git |
|
||||
| [`proton-drive-for-linux-bin`](https://aur.archlinux.org/packages/proton-drive-for-linux-bin) | installs the release binaries, no compile |
|
||||
|
||||
```bash
|
||||
paru -S proton-drive-for-linux # or any AUR helper
|
||||
```
|
||||
|
||||
The package sources live in `packaging/aur/`; see
|
||||
[`packaging/aur/README.md`](packaging/aur/README.md) for how they are updated and
|
||||
published. A local `PKGBUILD` that compiles the working tree in place is still
|
||||
available for development builds:
|
||||
```bash
|
||||
cd packaging && makepkg -fi
|
||||
```
|
||||
|
||||
### 3. Fedora (local RPM)
|
||||
A local `.spec` is available under `packaging/`. From the repository root:
|
||||
```bash
|
||||
sudo dnf install -y rpm-build
|
||||
rpmbuild -bb packaging/proton-drive-linux.spec \
|
||||
--define "git_dir $PWD" \
|
||||
--define "_rpmdir $PWD/packaging/out" \
|
||||
--define "_builddir $PWD/packaging/build" \
|
||||
--define "_sourcedir $PWD" \
|
||||
--define "_specdir $PWD/packaging" \
|
||||
--define "_srcrpmdir $PWD/packaging/out"
|
||||
sudo dnf install packaging/out/x86_64/proton-drive-linux-*.rpm
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Automated Releases (CI/CD)
|
||||
|
||||
This project has a GitHub Actions CI workflow configured under `.github/workflows/release.yml`.
|
||||
|
||||
### How it works:
|
||||
1. **Triggers**:
|
||||
- Pushing a git tag matching `v*` (e.g. `git tag v0.1.0 && git push origin v0.1.0`).
|
||||
- Manual runs via the **Actions** tab in GitHub (**workflow_dispatch**).
|
||||
2. **Quality Gates and Build Process**:
|
||||
- For tagged builds, verifies that the tag, workspace version, and `packaging/PKGBUILD` version agree.
|
||||
- Runs `cargo fmt`, Clippy with warnings denied, the locked workspace test suite, and the account-free FUSE acceptance contract.
|
||||
- Spawns an Ubuntu runner and installs GTK4, Libadwaita, FUSE3, and Secret Service packages.
|
||||
- Sets up the Rust compiler and caches build targets to speed up runs.
|
||||
- Compiles the workspace members in release mode.
|
||||
3. **Artifact Packaging**:
|
||||
- Generates a `.tar.gz` containing the raw binaries (`pdfs`, `pdfs-app`, `pdfs-tray`, `pdfs-prompt`).
|
||||
- Packs them into Debian (`.deb`) and Fedora (`.rpm`) packages.
|
||||
- Includes the systemd user service and tray autostart entry in the Debian package.
|
||||
4. **Publishing**:
|
||||
- Creates a GitHub Release matching the pushed tag and uploads the `.deb`, `.rpm`, and `.tar.gz` packages as release assets.
|
||||
- For manual runs, compiles and exposes the packages as workflow run artifacts for testing.
|
||||
|
||||
---
|
||||
Files you open are decrypted into a local cache so programs can read them. What that means for
|
||||
the security of the machine is spelled out in the
|
||||
[threat model](docs/ARCHITECTURE.md#8-threat-model-what-this-client-writes-to-disk-in-plaintext).
|
||||
|
||||
## Contributing
|
||||
|
||||
Bug reports, packaging fixes, and patches are welcome — see [CONTRIBUTING.md](CONTRIBUTING.md) for
|
||||
the workflow and the quality gates CI enforces, and [SECURITY.md](SECURITY.md) for reporting a
|
||||
vulnerability privately.
|
||||
|
||||
The app, tray and search prompt are available in English, German, Spanish, French, Italian, Dutch,
|
||||
Polish and Brazilian Portuguese. [docs/TRANSLATING.md](docs/TRANSLATING.md) explains how to improve
|
||||
a translation or add a language.
|
||||
Bug reports, translations and patches are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for the
|
||||
workflow and the checks CI runs, and [SECURITY.md](SECURITY.md) to report a vulnerability
|
||||
privately.
|
||||
|
||||
## License
|
||||
|
||||
Released under the [MIT License](LICENSE).
|
||||
|
||||
The Places map uses [GeoNames](https://www.geonames.org/) data (CC BY 4.0) and
|
||||
[Natural Earth](https://www.naturalearthdata.com/) outlines (public domain). The optional street
|
||||
map comes from [OpenFreeMap](https://openfreemap.org/), © [OpenStreetMap](https://www.openstreetmap.org/copyright)
|
||||
contributors.
|
||||
|
||||
Proton Drive is a service of Proton AG. This project is an independent client and carries no
|
||||
affiliation with or endorsement from Proton AG.
|
||||
@@ -30,5 +30,10 @@ the interesting boundaries are:
|
||||
- **Cryptography.** Key derivation, signature handling, and anything that could publish plaintext
|
||||
or the wrong key material to Proton.
|
||||
|
||||
What the client deliberately keeps in plaintext on disk, and why, is documented in the
|
||||
[threat model](docs/ARCHITECTURE.md#8-threat-model-what-this-client-writes-to-disk-in-plaintext).
|
||||
Behaviour described there is known and not a vulnerability by itself; a way to make it worse
|
||||
than described is.
|
||||
|
||||
Out of scope: vulnerabilities in Proton's own service (report those to Proton AG), and issues that
|
||||
require an attacker who already has your user account on the machine.
|
||||
@@ -20,6 +20,39 @@ use pdfs_core::control::{
|
||||
use pdfs_core::db::Db;
|
||||
use pdfs_core::service;
|
||||
|
||||
/// `println!` for this binary, shadowing std's, which panics when stdout is
|
||||
/// closed: `pdfs ls | head` ended in a "Broken pipe" panic once `head` had its
|
||||
/// lines. This one hands the failure to [`stdout_written`] instead.
|
||||
///
|
||||
/// Restoring the default `SIGPIPE` would also stop the panic, but the same
|
||||
/// binary runs the daemon, which must not die when a socket's peer goes away.
|
||||
macro_rules! println {
|
||||
($($arg:tt)*) => {{
|
||||
use std::io::Write as _;
|
||||
$crate::stdout_written(writeln!(std::io::stdout(), $($arg)*))
|
||||
}};
|
||||
}
|
||||
|
||||
/// [`println!`] without the newline.
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => {{
|
||||
use std::io::Write as _;
|
||||
$crate::stdout_written(write!(std::io::stdout(), $($arg)*))
|
||||
}};
|
||||
}
|
||||
|
||||
/// Exit on a failed write to stdout. A closed pipe means the reader has all it
|
||||
/// wanted, so that exit is a success.
|
||||
fn stdout_written(result: std::io::Result<()>) {
|
||||
if let Err(error) = result {
|
||||
if error.kind() == std::io::ErrorKind::BrokenPipe {
|
||||
std::process::exit(0);
|
||||
}
|
||||
eprintln!("Error: writing to stdout: {error}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(
|
||||
name = "pdfs",
|
||||
@@ -30,7 +63,7 @@ struct Cli {
|
||||
/// Emit machine-readable JSON instead of formatted text.
|
||||
///
|
||||
/// Applies to the query commands (`status`, `ls`, `pins`, `sync list`,
|
||||
/// `devices list`, `locations`, `transfers`, `activity`, `cache inspect`). Commands that
|
||||
/// `devices list`, `locations`, `transfers`, `activity`, `trash`, `cache inspect`). Commands that
|
||||
/// perform an action keep their human output — a script that needs to know
|
||||
/// whether one succeeded has the exit code.
|
||||
#[arg(long, global = true)]
|
||||
@@ -285,26 +318,34 @@ enum Command {
|
||||
},
|
||||
/// Rename a file or folder via the running daemon.
|
||||
Rename {
|
||||
/// File/folder path, inside the mountpoint or relative to it.
|
||||
/// File/folder path, inside My files or an on-demand synced folder, or
|
||||
/// relative to the mountpoint.
|
||||
path: PathBuf,
|
||||
/// New name (a single path component).
|
||||
new_name: String,
|
||||
},
|
||||
/// Move a file or folder into another folder via the running daemon.
|
||||
/// Move files or folders into another folder via the running daemon.
|
||||
///
|
||||
/// The paths may be in different locations: My files and any synced
|
||||
/// folder, on-demand or mirrored. The move happens on Proton Drive, so
|
||||
/// nothing is downloaded or uploaded again, unlike `mv` between two
|
||||
/// locations. The last path is the destination folder.
|
||||
Move {
|
||||
/// File/folder path, inside the mountpoint or relative to it.
|
||||
path: PathBuf,
|
||||
/// Destination folder path, inside the mountpoint or relative to it.
|
||||
new_parent: PathBuf,
|
||||
/// Files/folders to move, then the destination folder: each inside a
|
||||
/// location, or relative to the mountpoint.
|
||||
#[arg(required = true, num_args = 2.., value_name = "PATH")]
|
||||
paths: Vec<PathBuf>,
|
||||
},
|
||||
/// Trash a file or folder via the running daemon.
|
||||
Rm {
|
||||
/// File/folder path, inside the mountpoint or relative to it.
|
||||
/// File/folder path, inside My files or an on-demand synced folder, or
|
||||
/// relative to the mountpoint.
|
||||
path: PathBuf,
|
||||
},
|
||||
/// Create a folder via the running daemon.
|
||||
Mkdir {
|
||||
/// Parent folder path, inside the mountpoint or relative to it.
|
||||
/// Parent folder path, inside My files or an on-demand synced folder,
|
||||
/// or relative to the mountpoint.
|
||||
parent: PathBuf,
|
||||
/// New folder name (a single path component).
|
||||
name: String,
|
||||
@@ -800,7 +841,10 @@ fn main() -> Result<()> {
|
||||
scope,
|
||||
} => cmd_search(query, limit, scope),
|
||||
Command::Rename { path, new_name } => cmd_rename(path, new_name),
|
||||
Command::Move { path, new_parent } => cmd_move(path, new_parent),
|
||||
Command::Move { mut paths } => {
|
||||
let new_parent = paths.pop().expect("clap requires at least two paths");
|
||||
cmd_move(paths, new_parent)
|
||||
}
|
||||
Command::Rm { path } => cmd_rm(path),
|
||||
Command::Mkdir { parent, name } => cmd_mkdir(parent, name),
|
||||
Command::Upload { sources, parent } => cmd_upload(sources, parent),
|
||||
@@ -2439,14 +2483,33 @@ fn cmd_rename(path: PathBuf, new_name: String) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn cmd_move(path: PathBuf, new_parent: PathBuf) -> Result<()> {
|
||||
match control_request(CtlRequest::Move {
|
||||
path: path_arg(&path)?,
|
||||
new_parent: path_arg(&new_parent)?,
|
||||
})? {
|
||||
CtlResponse::Ok { message } => println!("{message}"),
|
||||
CtlResponse::Error { message, kind } => bail!("{}", cli_error(kind, &message)),
|
||||
other => bail!("unexpected response: {other:?}"),
|
||||
/// Move each of `sources` into `new_parent`, one request each. A refusal names
|
||||
/// its source and the rest still move; the command fails if any did not.
|
||||
fn cmd_move(sources: Vec<PathBuf>, new_parent: PathBuf) -> Result<()> {
|
||||
let new_parent = path_arg(&new_parent)?;
|
||||
let mut failed = 0;
|
||||
for source in &sources {
|
||||
let outcome = path_arg(source).and_then(|path| {
|
||||
match control_request(CtlRequest::Move {
|
||||
path,
|
||||
new_parent: new_parent.clone(),
|
||||
})? {
|
||||
CtlResponse::Ok { message } => Ok(message),
|
||||
CtlResponse::Error { message, kind } => bail!("{}", cli_error(kind, &message)),
|
||||
other => bail!("unexpected response: {other:?}"),
|
||||
}
|
||||
});
|
||||
match outcome {
|
||||
Ok(message) => println!("{message}"),
|
||||
Err(e) if sources.len() == 1 => return Err(e),
|
||||
Err(e) => {
|
||||
eprintln!("{}: {e:#}", source.display());
|
||||
failed += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if failed > 0 {
|
||||
bail!("{failed} of {} could not be moved", sources.len());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -2524,7 +2587,11 @@ fn cmd_diagnostics() -> Result<()> {
|
||||
}
|
||||
|
||||
fn cmd_trash() -> Result<()> {
|
||||
match control_request(CtlRequest::ListTrash)? {
|
||||
let response = control_request(CtlRequest::ListTrash)?;
|
||||
if emit_json(&response)? {
|
||||
return Ok(());
|
||||
}
|
||||
match response {
|
||||
CtlResponse::Entries { entries } if entries.is_empty() => println!("(trash is empty)"),
|
||||
CtlResponse::Entries { entries } => {
|
||||
for e in entries {
|
||||
@@ -3364,6 +3431,8 @@ mod login_wait_tests {
|
||||
shared_by: String::new(),
|
||||
shared_at: 0,
|
||||
shared_by_unverified: false,
|
||||
trashed_at: 0,
|
||||
trashed_from: None,
|
||||
};
|
||||
assert_eq!(shared_by_line(&entry), None, "no invitation, no line");
|
||||
entry.shared_by = "alice@proton.me".into();
|
||||
|
||||
@@ -365,6 +365,14 @@ pub struct StagedWrite {
|
||||
pub based_on: Option<Baseline>,
|
||||
}
|
||||
|
||||
impl StagedWrite {
|
||||
/// Whether `authored` ranges cover a file of `len` bytes, so the staged
|
||||
/// bytes are its complete new content. An empty file has nothing to cover.
|
||||
pub fn covers(authored: &[(u64, u64)], len: u64) -> bool {
|
||||
len == 0 || authored == [(0, len)]
|
||||
}
|
||||
}
|
||||
|
||||
/// The identity of a remote revision, as far as we can observe it.
|
||||
///
|
||||
/// `revision_id` is the authoritative identity when present: the server assigns a
|
||||
@@ -730,6 +738,60 @@ impl ContentCache {
|
||||
Some(buf)
|
||||
}
|
||||
|
||||
/// Write the whole content of `uid` at `(mtime, size)` to `dst` from the
|
||||
/// cache alone, and report whether that worked.
|
||||
///
|
||||
/// A whole-file blob is cloned. Otherwise every block of the revision has to
|
||||
/// be cached, under its recorded geometry or, failing that, the uniform one
|
||||
/// the read path would use. Anything short of the full content leaves
|
||||
/// nothing at `dst`, so a caller falls back to a download with a clean slate.
|
||||
///
|
||||
/// This is what lets a mirror folder take a file an on-demand mount has
|
||||
/// already read, after a move between them, without fetching it again. The
|
||||
/// trust is the read path's: the same `(mtime, size)` key, and blocks that
|
||||
/// were validated when they were fetched.
|
||||
pub fn copy_cached_to(&self, uid: &NodeUid, mtime: i64, size: u64, dst: &Path) -> bool {
|
||||
if size == 0 {
|
||||
return false;
|
||||
}
|
||||
if let Some(blob) = self.valid_blob(uid, mtime, size) {
|
||||
if Self::clone_or_copy(&blob, dst).is_ok_and(|len| len == size) {
|
||||
self.touch(Self::key(uid));
|
||||
return true;
|
||||
}
|
||||
let _ = std::fs::remove_file(dst);
|
||||
return false;
|
||||
}
|
||||
let geometry = self
|
||||
.block_geometry(uid, mtime, size)
|
||||
.unwrap_or_else(|| BlockGeometry::uniform(size));
|
||||
if geometry.size() != size {
|
||||
return false;
|
||||
}
|
||||
let spans = geometry.spans(0, size);
|
||||
// Checked up front so a file with one block missing costs stats, not a
|
||||
// copy of every block before it.
|
||||
if !spans
|
||||
.iter()
|
||||
.all(|&span| self.has_block(uid, mtime, size, span))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let written = (|| -> Option<()> {
|
||||
use std::io::Write;
|
||||
let mut out = std::fs::File::create(dst).ok()?;
|
||||
for span in spans {
|
||||
out.write_all(&self.cached_block(uid, mtime, size, span)?)
|
||||
.ok()?;
|
||||
}
|
||||
(out.metadata().ok()?.len() == size).then_some(())
|
||||
})();
|
||||
if written.is_none() {
|
||||
let _ = std::fs::remove_file(dst);
|
||||
}
|
||||
written.is_some()
|
||||
}
|
||||
|
||||
/// Store `bytes` as the cached content for `uid`, tagged with `(mtime,
|
||||
/// size)`. The blob is written to a temp file then renamed so a concurrent
|
||||
/// reader never sees a partial blob; the meta tag is written last so a
|
||||
@@ -2002,6 +2064,17 @@ mod tests {
|
||||
NodeUid::new(VolumeId::from("vol"), LinkId::from(link))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_write_covers_the_file_and_a_gap_does_not() {
|
||||
assert!(
|
||||
StagedWrite::covers(&[], 0),
|
||||
"an empty file has nothing to cover"
|
||||
);
|
||||
assert!(StagedWrite::covers(&[(0, 7)], 7));
|
||||
assert!(!StagedWrite::covers(&[(0, 3), (4, 7)], 7));
|
||||
assert!(!StagedWrite::covers(&[], 7));
|
||||
}
|
||||
|
||||
/// A unique temp directory removed on drop; avoids a dev-dependency.
|
||||
struct TempDir(PathBuf);
|
||||
impl TempDir {
|
||||
@@ -2561,6 +2634,46 @@ mod tests {
|
||||
assert!(c.cached_block(&u, 100, 5000, blk(5000, 0)).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cached_blob_is_copied_out_whole() {
|
||||
let (c, d) = cache();
|
||||
let u = uid("blob");
|
||||
c.store(&u, 7, 5, b"hello").unwrap();
|
||||
let dst = d.path().join("out");
|
||||
assert!(c.copy_cached_to(&u, 7, 5, &dst));
|
||||
assert_eq!(std::fs::read(&dst).unwrap(), b"hello");
|
||||
// Another revision is not this one.
|
||||
assert!(!c.copy_cached_to(&u, 8, 5, &d.path().join("stale")));
|
||||
assert!(!d.path().join("stale").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cached_blocks_are_copied_out_in_order() {
|
||||
let (c, d) = cache();
|
||||
let u = uid("blocks");
|
||||
let size = BLOCK_SIZE + 3;
|
||||
let first = vec![1_u8; BLOCK_SIZE as usize];
|
||||
c.store_block(&u, 7, size, blk(size, 0), &first).unwrap();
|
||||
c.store_block(&u, 7, size, blk(size, 1), b"end").unwrap();
|
||||
let dst = d.path().join("out");
|
||||
assert!(c.copy_cached_to(&u, 7, size, &dst));
|
||||
let bytes = std::fs::read(&dst).unwrap();
|
||||
assert_eq!(bytes.len() as u64, size);
|
||||
assert_eq!(&bytes[..BLOCK_SIZE as usize], first.as_slice());
|
||||
assert_eq!(&bytes[BLOCK_SIZE as usize..], b"end");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_with_a_block_missing_is_not_copied_out() {
|
||||
let (c, d) = cache();
|
||||
let u = uid("partial");
|
||||
let size = BLOCK_SIZE + 3;
|
||||
c.store_block(&u, 7, size, blk(size, 1), b"end").unwrap();
|
||||
let dst = d.path().join("out");
|
||||
assert!(!c.copy_cached_to(&u, 7, size, &dst));
|
||||
assert!(!dst.exists(), "a partial copy must not be left behind");
|
||||
}
|
||||
|
||||
/// The [`BlockSpan`] the uniform geometry gives block `idx` of a file of
|
||||
/// `size` bytes — the shape these tests were written against, and the one
|
||||
/// this client's own uploads still have.
|
||||
|
||||
@@ -31,6 +31,13 @@ const WRITE_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
/// answers.
|
||||
const READ_TIMEOUT: Duration = Duration::from_secs(120);
|
||||
|
||||
/// Cap on the read half for the requests front ends poll ([`Request::is_poll`]).
|
||||
/// The daemon answers those from memory or its database, so a reply that takes
|
||||
/// longer than this means it is stuck. Waiting the full [`READ_TIMEOUT`] would
|
||||
/// freeze the poller's read-out for two minutes; failing fast lets it say "not
|
||||
/// responding" and ask again on its next tick.
|
||||
const POLL_READ_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
/// A command sent from the CLI to the daemon.
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum Request {
|
||||
@@ -332,7 +339,10 @@ pub enum Request {
|
||||
/// single path component (no separators). Replies with [`Response::Ok`].
|
||||
Rename { path: String, new_name: String },
|
||||
/// Move a file or folder into a new parent folder. Both `path` and
|
||||
/// `new_parent` are mountpoint-relative. Replies with [`Response::Ok`].
|
||||
/// `new_parent` are mountpoint-relative, or both absolute local paths; two
|
||||
/// absolute paths may lie in different locations (My files, an on-demand
|
||||
/// or a mirror synced folder), and the move is still made on Proton Drive
|
||||
/// without transferring content. Replies with [`Response::Ok`].
|
||||
Move { path: String, new_parent: String },
|
||||
/// Trash a file or folder. `path` is mountpoint-relative. Replies with
|
||||
/// [`Response::Ok`].
|
||||
@@ -672,8 +682,88 @@ pub enum Request {
|
||||
/// passes, …), so a front-end can show a running "what happened" feed
|
||||
/// without re-deriving it from anywhere.
|
||||
ListActivity { limit: usize },
|
||||
|
||||
// ---- push -------------------------------------------------------------
|
||||
/// Keep the connection open and stream [`Event`] lines instead of one
|
||||
/// [`Response`]. See [`subscribe`] for the client side.
|
||||
Subscribe,
|
||||
}
|
||||
|
||||
/// Something a front-end shows that the daemon can say has changed, so the
|
||||
/// front-end reads it again. Carried by [`Event::Changed`].
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum Topic {
|
||||
/// Folder listings in the mount: something was created, renamed, moved,
|
||||
/// trashed or pinned, here or on another device.
|
||||
Files,
|
||||
/// The photos timeline or albums.
|
||||
Photos,
|
||||
Trash,
|
||||
/// Shares, links, invitations and saved links, in either direction.
|
||||
Shares,
|
||||
/// The account's computers.
|
||||
Devices,
|
||||
/// Synced folders, their mode and their live progress.
|
||||
Locations,
|
||||
/// The pending-op queue ([`Request::ListPendingOps`]).
|
||||
Queue,
|
||||
Conflicts,
|
||||
/// The activity log gained entries.
|
||||
Activity,
|
||||
/// A topic from a newer daemon, which this front-end cannot act on.
|
||||
#[serde(other)]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl Topic {
|
||||
/// Every topic a front-end can act on. A new subscription is told all of
|
||||
/// them have changed, so it catches up on whatever happened while it was
|
||||
/// not connected.
|
||||
pub const ALL: [Topic; 9] = [
|
||||
Topic::Files,
|
||||
Topic::Photos,
|
||||
Topic::Trash,
|
||||
Topic::Shares,
|
||||
Topic::Devices,
|
||||
Topic::Locations,
|
||||
Topic::Queue,
|
||||
Topic::Conflicts,
|
||||
Topic::Activity,
|
||||
];
|
||||
}
|
||||
|
||||
/// One line on a [`Request::Subscribe`] connection.
|
||||
///
|
||||
/// The two things front ends show continuously, status and transfers, come
|
||||
/// with their contents, so following them costs no requests. Everything else is
|
||||
/// a [`Topic`] to re-read.
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub enum Event {
|
||||
/// A fresh [`Response::Status`]. Sent when a subscription starts and
|
||||
/// whenever any of its fields changes.
|
||||
Status(Box<Response>),
|
||||
/// A fresh [`Response::Transfers`] snapshot. Sent when a subscription
|
||||
/// starts and whenever the snapshot changes, which is continuously while
|
||||
/// something moves.
|
||||
Transfers {
|
||||
items: Vec<TransferItem>,
|
||||
jobs: Vec<JobItem>,
|
||||
},
|
||||
/// These topics changed. Changes close together arrive as one event.
|
||||
Changed { topics: Vec<Topic> },
|
||||
/// Nothing happened. Sent every [`HEARTBEAT`] so a client can tell a quiet
|
||||
/// daemon from a stuck one.
|
||||
Heartbeat,
|
||||
}
|
||||
|
||||
/// How often a quiet subscription sends [`Event::Heartbeat`].
|
||||
pub const HEARTBEAT: Duration = Duration::from_secs(10);
|
||||
|
||||
/// How long [`Subscription::next_event`] waits for a line before it gives the
|
||||
/// daemon up as stuck: a few missed heartbeats.
|
||||
const SUBSCRIBE_READ_TIMEOUT: Duration = Duration::from_secs(25);
|
||||
|
||||
/// Which kind of share entry a [`ShareEntry`] is, and which collection an
|
||||
/// [`Request::UpdateShareRole`]/[`Request::RemoveShareEntry`] targets.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -739,7 +829,7 @@ pub struct DeviceInfo {
|
||||
|
||||
/// One `(sync-conflict …)` copy and the file it is a copy of (in
|
||||
/// [`Response::Conflicts`]).
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||
pub struct ConflictInfo {
|
||||
/// Mountpoint-relative path of the copy, or its absolute local path when it
|
||||
/// sits in a synced folder.
|
||||
@@ -898,7 +988,7 @@ pub struct ShareEntry {
|
||||
}
|
||||
|
||||
/// A node's public link (in [`Response::Share`] / [`Response::PublicLink`]).
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||
pub struct PublicLinkInfo {
|
||||
/// Public-link id — the handle for [`Request::RemovePublicLink`].
|
||||
pub id: String,
|
||||
@@ -911,6 +1001,9 @@ pub struct PublicLinkInfo {
|
||||
pub expires: Option<i64>,
|
||||
/// Whether a custom password additionally protects the link.
|
||||
pub has_password: bool,
|
||||
/// When the link was made, epoch seconds; 0 when unknown.
|
||||
#[serde(default)]
|
||||
pub created: i64,
|
||||
}
|
||||
|
||||
/// One invitation addressed to me (in [`Response::Invitations`]).
|
||||
@@ -943,7 +1036,7 @@ pub struct BookmarkInfo {
|
||||
/// the share's state so the "Shared" view renders in one pass: how many people
|
||||
/// have access, how many invitations are still pending, and the public link if
|
||||
/// the node has one.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||
pub struct SharedItem {
|
||||
/// Node uid in `volume~link` form — the handle for opening the node's share
|
||||
/// (via its mount path) or its details.
|
||||
@@ -1025,7 +1118,7 @@ pub enum ActivityKind {
|
||||
/// One line in the daemon's activity log (in [`Response::Activity`]). Newest
|
||||
/// first. Records a mutation or transfer the daemon performed, with enough
|
||||
/// context to read as a sentence: "Uploaded report.pdf to /docs".
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||
pub struct ActivityEntry {
|
||||
/// When it happened, epoch seconds.
|
||||
pub time: i64,
|
||||
@@ -1083,7 +1176,7 @@ pub struct TransferItem {
|
||||
}
|
||||
|
||||
/// One entry in a [`Request::ListDir`] listing.
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
|
||||
pub struct DirEntry {
|
||||
/// Decrypted node name.
|
||||
pub name: String,
|
||||
@@ -1131,6 +1224,16 @@ pub struct DirEntry {
|
||||
/// warning worth showing.
|
||||
#[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.
|
||||
#[serde(default)]
|
||||
pub trashed_at: i64,
|
||||
/// The folder a [`Request::ListTrash`] entry goes back to on restore, as a
|
||||
/// mountpoint-relative path (`""` for the root). `None` when that folder is
|
||||
/// not known, and for anything not in the trash.
|
||||
#[serde(default)]
|
||||
pub trashed_from: Option<String>,
|
||||
}
|
||||
|
||||
/// One hit in a [`Request::Search`] result. Like [`DirEntry`] but carries the
|
||||
@@ -2046,13 +2149,45 @@ impl ErrorKind {
|
||||
}
|
||||
}
|
||||
|
||||
impl Request {
|
||||
/// Whether this is one of the cheap state reads that front ends repeat on a
|
||||
/// timer. The daemon answers these without touching the network, so they
|
||||
/// get the short [`POLL_READ_TIMEOUT`].
|
||||
pub fn is_poll(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Request::Status
|
||||
| Request::GetQueueStatus
|
||||
| Request::ListLocations
|
||||
| Request::ListPendingOps
|
||||
| Request::ListConflicts
|
||||
| Request::ListActivity { .. }
|
||||
| Request::ThumbnailBuildStatus
|
||||
| Request::PhotosRefreshStatus
|
||||
| Request::RedateStatus
|
||||
| Request::ImportStatus
|
||||
)
|
||||
}
|
||||
|
||||
/// How long to wait for the daemon's reply to this request.
|
||||
pub fn read_timeout(&self) -> Duration {
|
||||
if self.is_poll() {
|
||||
POLL_READ_TIMEOUT
|
||||
} else {
|
||||
READ_TIMEOUT
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Send one [`Request`] to the daemon listening on `socket` and read its
|
||||
/// [`Response`]. Returns a crate [`crate::Error`] if no daemon is listening.
|
||||
/// [`Response`]. Returns a crate [`crate::Error`] if no daemon is listening, and
|
||||
/// one for which [`crate::Error::is_timeout`] holds if the daemon took longer
|
||||
/// than [`Request::read_timeout`] to answer.
|
||||
///
|
||||
/// Shared by the CLI and GUI so both speak the wire format identically.
|
||||
pub fn send(socket: &Path, req: &Request) -> Result<Response> {
|
||||
let mut stream = UnixStream::connect(socket)?;
|
||||
stream.set_read_timeout(Some(READ_TIMEOUT))?;
|
||||
stream.set_read_timeout(Some(req.read_timeout()))?;
|
||||
stream.set_write_timeout(Some(WRITE_TIMEOUT))?;
|
||||
let mut line = serde_json::to_vec(req)?;
|
||||
line.push(b'\n');
|
||||
@@ -2064,10 +2199,232 @@ pub fn send(socket: &Path, req: &Request) -> Result<Response> {
|
||||
Ok(serde_json::from_str(resp.trim())?)
|
||||
}
|
||||
|
||||
/// An open [`Request::Subscribe`] connection. See [`subscribe`].
|
||||
pub struct Subscription {
|
||||
reader: BufReader<UnixStream>,
|
||||
line: String,
|
||||
}
|
||||
|
||||
/// Start following the daemon listening on `socket`. Fails if no daemon is
|
||||
/// listening. A daemon too old to know [`Request::Subscribe`] answers it with an
|
||||
/// error, which the first [`Subscription::next_event`] returns as
|
||||
/// [`crate::Error::Other`].
|
||||
pub fn subscribe(socket: &Path) -> Result<Subscription> {
|
||||
let mut stream = UnixStream::connect(socket)?;
|
||||
stream.set_read_timeout(Some(SUBSCRIBE_READ_TIMEOUT))?;
|
||||
stream.set_write_timeout(Some(WRITE_TIMEOUT))?;
|
||||
let mut line = serde_json::to_vec(&Request::Subscribe)?;
|
||||
line.push(b'\n');
|
||||
stream.write_all(&line)?;
|
||||
stream.flush()?;
|
||||
Ok(Subscription {
|
||||
reader: BufReader::new(stream),
|
||||
line: String::new(),
|
||||
})
|
||||
}
|
||||
|
||||
impl Subscription {
|
||||
/// Wait for the next event. `Ok(None)` means the daemon closed the
|
||||
/// connection; a timeout means it went [`SUBSCRIBE_READ_TIMEOUT`] without
|
||||
/// even a heartbeat. A line from a newer daemon that this client cannot
|
||||
/// read is skipped rather than ending the subscription.
|
||||
pub fn next_event(&mut self) -> Result<Option<Event>> {
|
||||
loop {
|
||||
self.line.clear();
|
||||
if self.reader.read_line(&mut self.line)? == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let line = self.line.trim();
|
||||
if let Ok(event) = serde_json::from_str::<Event>(line) {
|
||||
return Ok(Some(event));
|
||||
}
|
||||
if let Ok(Response::Error { message, .. }) = serde_json::from_str::<Response>(line) {
|
||||
return Err(crate::Error::Other(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What [`follow`] reports.
|
||||
#[derive(Debug)]
|
||||
pub enum Feed {
|
||||
/// A subscription started. The events after it describe the daemon's
|
||||
/// current state, so a front-end catches up from them.
|
||||
Connected,
|
||||
Event(Event),
|
||||
/// The subscription ended: the daemon stopped, or stopped answering.
|
||||
Disconnected,
|
||||
}
|
||||
|
||||
/// How often [`follow`] polls a daemon that cannot push events.
|
||||
const LEGACY_POLL: Duration = Duration::from_secs(3);
|
||||
|
||||
/// The longest [`follow`] waits between attempts to reach a daemon.
|
||||
const RECONNECT_MAX: Duration = Duration::from_secs(5);
|
||||
|
||||
/// Follow the daemon on `socket` for as long as `on` returns true, across
|
||||
/// daemon restarts. Blocks, so run it on a thread of its own.
|
||||
///
|
||||
/// A daemon that predates [`Request::Subscribe`] (still running after an
|
||||
/// upgrade, until its service restarts) is polled for its status and transfers
|
||||
/// instead, so a front-end sees the same events either way, only later.
|
||||
pub fn follow(socket: &Path, mut on: impl FnMut(Feed) -> bool) {
|
||||
let mut connected = false;
|
||||
let mut backoff = Duration::from_secs(1);
|
||||
loop {
|
||||
let ended = match subscribe(socket) {
|
||||
Ok(mut subscription) => {
|
||||
let mut first = true;
|
||||
loop {
|
||||
match subscription.next_event() {
|
||||
Ok(Some(event)) => {
|
||||
if first {
|
||||
first = false;
|
||||
connected = true;
|
||||
backoff = Duration::from_secs(1);
|
||||
if !on(Feed::Connected) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if !on(Feed::Event(event)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Ok(None) => break Ok(()),
|
||||
Err(e) => break Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
if let Err(crate::Error::Other(_)) = ended {
|
||||
if !connected {
|
||||
connected = true;
|
||||
if !on(Feed::Connected) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if !poll_legacy(socket, &mut on) {
|
||||
return;
|
||||
}
|
||||
std::thread::sleep(LEGACY_POLL);
|
||||
continue;
|
||||
}
|
||||
if connected {
|
||||
connected = false;
|
||||
if !on(Feed::Disconnected) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
std::thread::sleep(backoff);
|
||||
backoff = (backoff * 2).min(RECONNECT_MAX);
|
||||
}
|
||||
}
|
||||
|
||||
/// One round of [`follow`]'s fallback: the status and transfers a subscription
|
||||
/// would have pushed. Returns what `on` returned.
|
||||
fn poll_legacy(socket: &Path, on: &mut impl FnMut(Feed) -> bool) -> bool {
|
||||
match send(socket, &Request::Status) {
|
||||
Ok(status @ Response::Status { .. }) => {
|
||||
if !on(Feed::Event(Event::Status(Box::new(status)))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_ => return true,
|
||||
}
|
||||
if let Ok(Response::Transfers { items, jobs }) = send(socket, &Request::GetQueueStatus) {
|
||||
return on(Feed::Event(Event::Transfers { items, jobs }));
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn polls_wait_briefly_and_real_work_waits_long() {
|
||||
assert_eq!(Request::Status.read_timeout(), POLL_READ_TIMEOUT);
|
||||
assert_eq!(
|
||||
Request::ListActivity { limit: 200 }.read_timeout(),
|
||||
POLL_READ_TIMEOUT
|
||||
);
|
||||
assert_eq!(
|
||||
Request::OpenFile {
|
||||
path: "a.txt".into(),
|
||||
uid: None,
|
||||
}
|
||||
.read_timeout(),
|
||||
READ_TIMEOUT
|
||||
);
|
||||
}
|
||||
|
||||
/// A daemon on a fresh socket that reads the subscription request and
|
||||
/// answers with `lines`, then hangs up.
|
||||
fn fake_daemon(name: &str, lines: &'static str) -> std::path::PathBuf {
|
||||
let socket = std::env::temp_dir().join(format!("pdfs-{name}-{}.sock", std::process::id()));
|
||||
let _ = std::fs::remove_file(&socket);
|
||||
let listener = std::os::unix::net::UnixListener::bind(&socket).unwrap();
|
||||
std::thread::spawn(move || {
|
||||
let (stream, _) = listener.accept().unwrap();
|
||||
let mut reader = BufReader::new(stream);
|
||||
let mut request = String::new();
|
||||
reader.read_line(&mut request).unwrap();
|
||||
assert!(matches!(
|
||||
serde_json::from_str::<Request>(request.trim()),
|
||||
Ok(Request::Subscribe)
|
||||
));
|
||||
reader.get_mut().write_all(lines.as_bytes()).unwrap();
|
||||
});
|
||||
socket
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_subscription_skips_what_it_cannot_read() {
|
||||
let socket = fake_daemon(
|
||||
"subscribe",
|
||||
"not json\n\
|
||||
{\"Changed\":{\"topics\":[\"files\",\"from_a_newer_daemon\"]}}\n\
|
||||
{\"NewEvent\":{}}\n\
|
||||
\"Heartbeat\"\n",
|
||||
);
|
||||
let mut subscription = subscribe(&socket).unwrap();
|
||||
let first = subscription.next_event().unwrap();
|
||||
let second = subscription.next_event().unwrap();
|
||||
let end = subscription.next_event().unwrap();
|
||||
let _ = std::fs::remove_file(&socket);
|
||||
match first {
|
||||
Some(Event::Changed { topics }) => {
|
||||
assert_eq!(topics, vec![Topic::Files, Topic::Unknown]);
|
||||
}
|
||||
other => panic!("expected a change, got {other:?}"),
|
||||
}
|
||||
assert!(matches!(second, Some(Event::Heartbeat)), "{second:?}");
|
||||
assert!(end.is_none(), "{end:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_daemon_without_subscriptions_is_told_apart_from_a_dead_one() {
|
||||
let socket = fake_daemon("legacy", "{\"Error\":{\"message\":\"unknown request\"}}\n");
|
||||
let mut subscription = subscribe(&socket).unwrap();
|
||||
let answer = subscription.next_event();
|
||||
let _ = std::fs::remove_file(&socket);
|
||||
assert!(matches!(answer, Err(crate::Error::Other(_))), "{answer:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_daemon_that_never_answers_times_out_as_not_responding() {
|
||||
let socket = std::env::temp_dir().join(format!("pdfs-silent-{}.sock", std::process::id()));
|
||||
let _ = std::fs::remove_file(&socket);
|
||||
// Bound but never accepted: connect succeeds, no reply ever comes.
|
||||
let _listener = std::os::unix::net::UnixListener::bind(&socket).unwrap();
|
||||
let started = std::time::Instant::now();
|
||||
let err = send(&socket, &Request::Status).unwrap_err();
|
||||
let _ = std::fs::remove_file(&socket);
|
||||
assert!(err.is_timeout(), "{err}");
|
||||
assert!(started.elapsed() < READ_TIMEOUT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn photo_kind_classifies_by_extension_then_mime() {
|
||||
use PhotoKind::*;
|
||||
|
||||
@@ -0,0 +1,249 @@
|
||||
/* WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */
|
||||
PRAGMA foreign_keys=OFF;
|
||||
BEGIN TRANSACTION;
|
||||
CREATE TABLE sync_state (key TEXT PRIMARY KEY, value TEXT);
|
||||
INSERT INTO sync_state VALUES('schema_version','35');
|
||||
CREATE TABLE nodes (
|
||||
uid TEXT PRIMARY KEY,
|
||||
parent_uid TEXT,
|
||||
name TEXT NOT NULL,
|
||||
is_dir INTEGER NOT NULL,
|
||||
size INTEGER,
|
||||
mtime INTEGER NOT NULL,
|
||||
revision_hash TEXT,
|
||||
trashed INTEGER NOT NULL DEFAULT 0
|
||||
, node_json TEXT, listed INTEGER NOT NULL DEFAULT 0, path TEXT);
|
||||
INSERT INTO nodes VALUES('vol~root',NULL,'My Files',1,0,200,NULL,0,'{"uid":{"volume_id":"vol","link_id":"root"},"parent_uid":null,"kind":"Folder","name":"My Files","creation_time":100,"modification_time":200,"trashed":false,"is_shared":false,"is_shared_publicly":false,"signature_email":null,"membership":null,"direct_role":null,"share_id":null,"photo":null,"album":null,"verification":{"name":"NotSigned","passphrase":"NotSigned","content_key":null,"extended_attributes":null}}',0,'');
|
||||
INSERT INTO nodes VALUES('vol~docs','vol~root','Docs',1,0,200,NULL,0,'{"uid":{"volume_id":"vol","link_id":"docs"},"parent_uid":{"volume_id":"vol","link_id":"root"},"kind":"Folder","name":"Docs","creation_time":100,"modification_time":200,"trashed":false,"is_shared":false,"is_shared_publicly":false,"signature_email":null,"membership":null,"direct_role":null,"share_id":null,"photo":null,"album":null,"verification":{"name":"NotSigned","passphrase":"NotSigned","content_key":null,"extended_attributes":null}}',0,'Docs');
|
||||
INSERT INTO nodes VALUES('vol~report','vol~docs','report.txt',0,10,200,NULL,0,'{"uid":{"volume_id":"vol","link_id":"report"},"parent_uid":{"volume_id":"vol","link_id":"docs"},"kind":{"File":{"media_type":"text/plain","total_size_on_storage":20,"active_revision_state":null,"active_revision_id":null,"claimed_size":10,"claimed_modification_time":null,"content_sha1":null}},"name":"report.txt","creation_time":100,"modification_time":200,"trashed":false,"is_shared":false,"is_shared_publicly":false,"signature_email":null,"membership":null,"direct_role":null,"share_id":null,"photo":null,"album":null,"verification":{"name":"NotSigned","passphrase":"NotSigned","content_key":null,"extended_attributes":null}}',0,'Docs/report.txt');
|
||||
INSERT INTO nodes VALUES('vol~old','vol~docs','old.txt',0,3,200,NULL,0,'{"uid":{"volume_id":"vol","link_id":"old"},"parent_uid":{"volume_id":"vol","link_id":"docs"},"kind":{"File":{"media_type":"text/plain","total_size_on_storage":13,"active_revision_state":null,"active_revision_id":null,"claimed_size":3,"claimed_modification_time":null,"content_sha1":null}},"name":"old.txt","creation_time":100,"modification_time":200,"trashed":false,"is_shared":false,"is_shared_publicly":false,"signature_email":null,"membership":null,"direct_role":null,"share_id":null,"photo":null,"album":null,"verification":{"name":"NotSigned","passphrase":"NotSigned","content_key":null,"extended_attributes":null}}',0,'Docs/old.txt');
|
||||
INSERT INTO nodes VALUES('vol~shared','vol~docs','shared.txt',0,4,200,NULL,0,'{"uid":{"volume_id":"vol","link_id":"shared"},"parent_uid":{"volume_id":"vol","link_id":"docs"},"kind":{"File":{"media_type":"text/plain","total_size_on_storage":14,"active_revision_state":null,"active_revision_id":null,"claimed_size":4,"claimed_modification_time":null,"content_sha1":null}},"name":"shared.txt","creation_time":100,"modification_time":200,"trashed":false,"is_shared":false,"is_shared_publicly":false,"signature_email":null,"membership":null,"direct_role":null,"share_id":null,"photo":null,"album":null,"verification":{"name":"NotSigned","passphrase":"NotSigned","content_key":null,"extended_attributes":null}}',0,'Docs/shared.txt');
|
||||
INSERT INTO nodes VALUES('local~dir','vol~root','Offline folder',1,0,200,NULL,0,'{"uid":{"volume_id":"local","link_id":"dir"},"parent_uid":{"volume_id":"vol","link_id":"root"},"kind":"Folder","name":"Offline folder","creation_time":100,"modification_time":200,"trashed":false,"is_shared":false,"is_shared_publicly":false,"signature_email":null,"membership":null,"direct_role":null,"share_id":null,"photo":null,"album":null,"verification":{"name":"NotSigned","passphrase":"NotSigned","content_key":null,"extended_attributes":null}}',1,'Offline folder');
|
||||
INSERT INTO nodes VALUES('local~inner','local~dir','inner.txt',0,5,200,NULL,0,'{"uid":{"volume_id":"local","link_id":"inner"},"parent_uid":{"volume_id":"local","link_id":"dir"},"kind":{"File":{"media_type":"text/plain","total_size_on_storage":15,"active_revision_state":null,"active_revision_id":null,"claimed_size":5,"claimed_modification_time":null,"content_sha1":null}},"name":"inner.txt","creation_time":100,"modification_time":200,"trashed":false,"is_shared":false,"is_shared_publicly":false,"signature_email":null,"membership":null,"direct_role":null,"share_id":null,"photo":null,"album":null,"verification":{"name":"NotSigned","passphrase":"NotSigned","content_key":null,"extended_attributes":null}}',0,'Offline folder/inner.txt');
|
||||
INSERT INTO nodes VALUES('local~part','vol~root','movie.part',0,7,200,NULL,0,'{"uid":{"volume_id":"local","link_id":"part"},"parent_uid":{"volume_id":"vol","link_id":"root"},"kind":{"File":{"media_type":"text/plain","total_size_on_storage":17,"active_revision_state":null,"active_revision_id":null,"claimed_size":7,"claimed_modification_time":null,"content_sha1":null}},"name":"movie.part","creation_time":100,"modification_time":200,"trashed":false,"is_shared":false,"is_shared_publicly":false,"signature_email":null,"membership":null,"direct_role":null,"share_id":null,"photo":null,"album":null,"verification":{"name":"NotSigned","passphrase":"NotSigned","content_key":null,"extended_attributes":null}}',0,'movie.part');
|
||||
CREATE TABLE cache_entries (
|
||||
cache_key TEXT PRIMARY KEY,
|
||||
size_bytes INTEGER,
|
||||
last_accessed INTEGER,
|
||||
is_pinned INTEGER NOT NULL DEFAULT 0
|
||||
, kind TEXT NOT NULL DEFAULT 'blob');
|
||||
CREATE TABLE pins (
|
||||
uid TEXT PRIMARY KEY,
|
||||
path TEXT NOT NULL,
|
||||
recursive INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE local_files (
|
||||
id INTEGER PRIMARY KEY,
|
||||
path TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL,
|
||||
is_dir INTEGER NOT NULL,
|
||||
size INTEGER NOT NULL DEFAULT 0,
|
||||
mtime INTEGER NOT NULL DEFAULT 0,
|
||||
scan_gen INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE photos (
|
||||
uid TEXT PRIMARY KEY,
|
||||
capture_time INTEGER NOT NULL,
|
||||
name TEXT,
|
||||
ratio REAL,
|
||||
thumb_state INTEGER NOT NULL DEFAULT 0,
|
||||
seq INTEGER NOT NULL
|
||||
, media_type TEXT, kind INTEGER NOT NULL DEFAULT 0, favorite INTEGER NOT NULL DEFAULT 0, content_hash TEXT, main_uid TEXT, group_key TEXT, resolved_at INTEGER, latitude REAL, longitude REAL, place_id INTEGER, similar_hash INTEGER);
|
||||
CREATE TABLE trash (
|
||||
uid TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
is_dir INTEGER NOT NULL,
|
||||
size INTEGER NOT NULL DEFAULT 0,
|
||||
mtime INTEGER NOT NULL DEFAULT 0
|
||||
, parent_uid TEXT, trashed_at INTEGER);
|
||||
CREATE TABLE device (
|
||||
uid TEXT PRIMARY KEY,
|
||||
share_id TEXT NOT NULL,
|
||||
root_uid TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
created INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE TABLE sync_folder (
|
||||
id INTEGER PRIMARY KEY,
|
||||
local_path TEXT NOT NULL UNIQUE,
|
||||
remote_uid TEXT NOT NULL,
|
||||
remote_share_id TEXT NOT NULL,
|
||||
mode TEXT NOT NULL DEFAULT 'mirror',
|
||||
state TEXT NOT NULL DEFAULT 'idle',
|
||||
last_sync INTEGER NOT NULL DEFAULT 0
|
||||
, pending_mode TEXT);
|
||||
CREATE TABLE sync_entry (
|
||||
folder_id INTEGER NOT NULL,
|
||||
rel_path TEXT NOT NULL,
|
||||
remote_uid TEXT,
|
||||
local_mtime INTEGER NOT NULL DEFAULT 0,
|
||||
local_size INTEGER NOT NULL DEFAULT 0,
|
||||
remote_hash TEXT,
|
||||
remote_rev TEXT, local_mtime_ns INTEGER,
|
||||
PRIMARY KEY (folder_id, rel_path)
|
||||
);
|
||||
CREATE TABLE activity (
|
||||
id INTEGER PRIMARY KEY,
|
||||
time INTEGER NOT NULL,
|
||||
kind TEXT NOT NULL,
|
||||
target TEXT NOT NULL,
|
||||
detail TEXT NOT NULL DEFAULT '',
|
||||
ok INTEGER NOT NULL DEFAULT 1
|
||||
);
|
||||
CREATE TABLE pending_op (
|
||||
id INTEGER PRIMARY KEY,
|
||||
kind TEXT NOT NULL,
|
||||
uid TEXT NOT NULL,
|
||||
blob_path TEXT,
|
||||
meta_json TEXT,
|
||||
created_at INTEGER NOT NULL,
|
||||
attempts INTEGER NOT NULL DEFAULT 0,
|
||||
last_error TEXT,
|
||||
next_attempt_at INTEGER NOT NULL DEFAULT 0
|
||||
, parent_uid TEXT, name TEXT, claimed_at INTEGER NOT NULL DEFAULT 0, access_deferred_since INTEGER NOT NULL DEFAULT 0);
|
||||
INSERT INTO pending_op VALUES(1,'mkdir','local~dir',NULL,NULL,1700000000000,0,NULL,0,'vol~root','Offline folder',4611686018427387903,0);
|
||||
INSERT INTO pending_op VALUES(2,'create','local~inner','/blobs/inner','{"len":5,"mtime":1700000000,"based_on":null}',1700000000000,0,NULL,0,'local~dir','inner.txt',0,0);
|
||||
INSERT INTO pending_op VALUES(3,'create','local~part','/blobs/part','{"len":5,"mtime":1700000000,"based_on":null}',1700000000000,0,NULL,8000000000000,'vol~root','movie.part',0,0);
|
||||
INSERT INTO pending_op VALUES(4,'revision','vol~report','/blobs/report','{"len":5,"mtime":1700000000,"based_on":null}',1700000000000,2,'network unreachable',1700000900000,NULL,NULL,0,0);
|
||||
INSERT INTO pending_op VALUES(5,'rename','vol~shared',NULL,'{"original_parent_uid":"vol~docs","original_name":"shared.txt"}',1700000000000,0,NULL,1700000700000,'vol~root','shared-moved.txt',0,1700000100000);
|
||||
INSERT INTO pending_op VALUES(6,'trash','vol~old',NULL,NULL,1700000000000,0,NULL,0,NULL,NULL,0,0);
|
||||
CREATE TABLE IF NOT EXISTS 'local_fts_data'(id INTEGER PRIMARY KEY, block BLOB);
|
||||
INSERT INTO local_fts_data VALUES(1,x'000000');
|
||||
INSERT INTO local_fts_data VALUES(10,x'00000000000000');
|
||||
CREATE TABLE IF NOT EXISTS 'local_fts_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID;
|
||||
CREATE TABLE IF NOT EXISTS 'local_fts_docsize'(id INTEGER PRIMARY KEY, sz BLOB);
|
||||
CREATE TABLE IF NOT EXISTS 'local_fts_config'(k PRIMARY KEY, v) WITHOUT ROWID;
|
||||
INSERT INTO local_fts_config VALUES('version',4);
|
||||
CREATE TABLE share_access (
|
||||
root_uid TEXT PRIMARY KEY,
|
||||
access TEXT NOT NULL
|
||||
CHECK (access IN ('owner', 'editor', 'viewer', 'unknown'))
|
||||
);
|
||||
CREATE TABLE mount (
|
||||
id INTEGER PRIMARY KEY,
|
||||
kind TEXT NOT NULL
|
||||
CHECK (kind IN ('myfiles', 'device', 'shared')),
|
||||
sync_folder_id INTEGER REFERENCES sync_folder(id) ON DELETE CASCADE,
|
||||
share_root_uid TEXT,
|
||||
local_path TEXT NOT NULL DEFAULT '',
|
||||
root_uid TEXT NOT NULL DEFAULT '',
|
||||
root_share_id TEXT NOT NULL DEFAULT '',
|
||||
mode TEXT NOT NULL DEFAULT 'unknown',
|
||||
access TEXT NOT NULL DEFAULT 'rw'
|
||||
CHECK (access IN ('rw', 'ro')),
|
||||
CHECK (
|
||||
(kind = 'myfiles' AND sync_folder_id IS NULL AND share_root_uid IS NULL)
|
||||
OR
|
||||
(kind = 'device' AND sync_folder_id IS NOT NULL AND share_root_uid IS NULL)
|
||||
OR
|
||||
(kind = 'shared' AND sync_folder_id IS NULL AND share_root_uid IS NOT NULL)
|
||||
)
|
||||
);
|
||||
CREATE TABLE albums (
|
||||
uid TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
photo_count INTEGER NOT NULL DEFAULT 0,
|
||||
cover_uid TEXT,
|
||||
last_activity INTEGER,
|
||||
shared INTEGER NOT NULL DEFAULT 0,
|
||||
seq INTEGER NOT NULL
|
||||
);
|
||||
CREATE TABLE album_photos (
|
||||
album_uid TEXT NOT NULL,
|
||||
uid TEXT NOT NULL,
|
||||
capture_time INTEGER NOT NULL,
|
||||
name TEXT,
|
||||
media_type TEXT,
|
||||
kind INTEGER NOT NULL DEFAULT 0,
|
||||
ratio REAL,
|
||||
thumb_state INTEGER NOT NULL DEFAULT 0,
|
||||
seq INTEGER NOT NULL,
|
||||
PRIMARY KEY (album_uid, uid)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS 'nodes_fts_data'(id INTEGER PRIMARY KEY, block BLOB);
|
||||
INSERT INTO nodes_fts_data VALUES(1,x'083850');
|
||||
INSERT INTO nodes_fts_data VALUES(10,x'000000000108080008010101020101030101040101050101060101070101080101');
|
||||
INSERT INTO nodes_fts_data VALUES(137438953473,x'000000340430206669010204010366696c0102050103696c6501020601036c657301020701036d79200102020103792066010203040808080808');
|
||||
INSERT INTO nodes_fts_data VALUES(274877906945,x'0000001a0430646f6302080201010201036f6373020803010103040b');
|
||||
INSERT INTO nodes_fts_data VALUES(412316860417,x'0000008b04302e747803080801010d01032f72650306010106010363732f03060101040103646f630306010102010365706f03080301010801036f637303060101030202727403080501010a0103706f7203080401010901037265700308020101070202742e03080601010b0103732f7203060101050103742e7403080701010c0202787403080901010e040b0a0a0a0b0a0a0b0b0a0a0b');
|
||||
INSERT INTO nodes_fts_data VALUES(549755813889,x'0000006b04302e747804080501010a01032f6f6c0406010106010363732f04060101040103642e7404080401010902026f63040601010201036c642e04080301010801036f6373040601010302026c640408020101070103732f6f0406010105010374787404080601010b040b0a0a0b090b0a0a0a');
|
||||
INSERT INTO nodes_fts_data VALUES(687194767361,x'0000008c04302e747805080801010d01032f736805060101060103617265050804010109010363732f05060101040103642e7405080701010c02026f630506010102010365642e05080601010b010368617205080301010801036f63730506010103010372656405080501010a0103732f73050601010502026861050802010107010374787405080901010e040b0a0b0a0b090b0b0a0b0a0a');
|
||||
INSERT INTO nodes_fts_data VALUES(824633720833,x'00000084043020666f060809010109010364657206080d01010d0103652066060808010108010366666c06080301010302026c6906080401010402026f6c06080a01010a0103696e6506080601010601036c646506080c01010c0202696e06080501010501036e652006080701010701036f666606080201010202026c6406080b01010b040b0b0b0b0a0a0b0b0a0b0b');
|
||||
INSERT INTO nodes_fts_data VALUES(962072674305,x'000000da043020666f070601010901032e747807080701011601032f696e07060101100103646572070601010d010365206607060101080202722e07080501011403012f070601010e010366666c070601010302026c69070601010402026f6c070601010a0103696e65070601010603016e07080201011101036c6465070601010c0202696e070601010501036e6520070601010703017207080401011302026e6507080301011201036f6666070601010202026c64070601010b0103722e7407080601011502022f69070601010f0103747874070808010117040a0b0a0a0a0a080a09090a090a090a090a0a090b09');
|
||||
INSERT INTO nodes_fts_data VALUES(1099511627777,x'0000005c04302e706108080701010701036172740808090101090103652e70080806010106010369652e08080501010501036d6f7608080201010201036f766908080301010301037061720808080101080103766965080804010104040b0b0b0b0b0b0b');
|
||||
CREATE TABLE IF NOT EXISTS 'nodes_fts_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID;
|
||||
INSERT INTO nodes_fts_idx VALUES(1,x'',2);
|
||||
INSERT INTO nodes_fts_idx VALUES(2,x'',2);
|
||||
INSERT INTO nodes_fts_idx VALUES(3,x'',2);
|
||||
INSERT INTO nodes_fts_idx VALUES(4,x'',2);
|
||||
INSERT INTO nodes_fts_idx VALUES(5,x'',2);
|
||||
INSERT INTO nodes_fts_idx VALUES(6,x'',2);
|
||||
INSERT INTO nodes_fts_idx VALUES(7,x'',2);
|
||||
INSERT INTO nodes_fts_idx VALUES(8,x'',2);
|
||||
CREATE TABLE IF NOT EXISTS 'nodes_fts_content'(id INTEGER PRIMARY KEY, c0, c1);
|
||||
INSERT INTO nodes_fts_content VALUES(1,'My Files','');
|
||||
INSERT INTO nodes_fts_content VALUES(2,'Docs','Docs');
|
||||
INSERT INTO nodes_fts_content VALUES(3,'report.txt','Docs/report.txt');
|
||||
INSERT INTO nodes_fts_content VALUES(4,'old.txt','Docs/old.txt');
|
||||
INSERT INTO nodes_fts_content VALUES(5,'shared.txt','Docs/shared.txt');
|
||||
INSERT INTO nodes_fts_content VALUES(6,'Offline folder','Offline folder');
|
||||
INSERT INTO nodes_fts_content VALUES(7,'inner.txt','Offline folder/inner.txt');
|
||||
INSERT INTO nodes_fts_content VALUES(8,'movie.part','movie.part');
|
||||
CREATE TABLE IF NOT EXISTS 'nodes_fts_docsize'(id INTEGER PRIMARY KEY, sz BLOB);
|
||||
INSERT INTO nodes_fts_docsize VALUES(1,x'0600');
|
||||
INSERT INTO nodes_fts_docsize VALUES(2,x'0202');
|
||||
INSERT INTO nodes_fts_docsize VALUES(3,x'080d');
|
||||
INSERT INTO nodes_fts_docsize VALUES(4,x'050a');
|
||||
INSERT INTO nodes_fts_docsize VALUES(5,x'080d');
|
||||
INSERT INTO nodes_fts_docsize VALUES(6,x'0c0c');
|
||||
INSERT INTO nodes_fts_docsize VALUES(7,x'0716');
|
||||
INSERT INTO nodes_fts_docsize VALUES(8,x'0808');
|
||||
CREATE TABLE IF NOT EXISTS 'nodes_fts_config'(k PRIMARY KEY, v) WITHOUT ROWID;
|
||||
INSERT INTO nodes_fts_config VALUES('version',4);
|
||||
CREATE TABLE own_sealed_rev (
|
||||
uid TEXT PRIMARY KEY,
|
||||
revision_id TEXT NOT NULL,
|
||||
sealed_at INTEGER NOT NULL
|
||||
);
|
||||
INSERT INTO own_sealed_rev VALUES('vol~report','rev-1',1700000000000);
|
||||
PRAGMA writable_schema=ON;
|
||||
INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)VALUES('table','local_fts','local_fts',0,'CREATE VIRTUAL TABLE local_fts USING fts5(
|
||||
name, path, content=''local_files'', content_rowid=''id'', tokenize=''trigram''
|
||||
)');
|
||||
INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)VALUES('table','nodes_fts','nodes_fts',0,'CREATE VIRTUAL TABLE nodes_fts USING fts5(
|
||||
name, path, tokenize=''trigram''
|
||||
)');
|
||||
CREATE TRIGGER mount_sync_folder_insert
|
||||
AFTER INSERT ON sync_folder
|
||||
BEGIN
|
||||
INSERT OR IGNORE INTO mount (kind, sync_folder_id)
|
||||
VALUES ('device', NEW.id);
|
||||
END;
|
||||
CREATE INDEX idx_nodes_parent ON nodes(parent_uid);
|
||||
CREATE INDEX idx_photos_seq ON photos(seq);
|
||||
CREATE INDEX pending_op_uid ON pending_op(uid);
|
||||
CREATE INDEX pending_op_parent ON pending_op(parent_uid);
|
||||
CREATE INDEX cache_entries_lru ON cache_entries(kind, last_accessed);
|
||||
CREATE INDEX idx_nodes_name_nocase ON nodes(name COLLATE NOCASE);
|
||||
CREATE INDEX idx_local_files_name_nocase
|
||||
ON local_files(name COLLATE NOCASE);
|
||||
CREATE UNIQUE INDEX mount_myfiles
|
||||
ON mount(kind) WHERE kind = 'myfiles';
|
||||
CREATE UNIQUE INDEX mount_device
|
||||
ON mount(sync_folder_id) WHERE kind = 'device';
|
||||
CREATE UNIQUE INDEX mount_shared
|
||||
ON mount(share_root_uid) WHERE kind = 'shared';
|
||||
CREATE INDEX idx_album_photos_seq ON album_photos(album_uid, seq);
|
||||
CREATE INDEX idx_album_photos_uid ON album_photos(uid);
|
||||
CREATE INDEX idx_photos_favorite ON photos(favorite, seq);
|
||||
CREATE INDEX idx_sync_entry_remote_uid ON sync_entry(remote_uid);
|
||||
CREATE INDEX idx_local_files_scan_gen ON local_files(scan_gen);
|
||||
CREATE INDEX idx_nodes_parent_live ON nodes(parent_uid) WHERE trashed = 0;
|
||||
CREATE INDEX idx_pending_op_claimed
|
||||
ON pending_op(uid) WHERE claimed_at <> 0;
|
||||
CREATE INDEX idx_trash_parent ON trash(parent_uid);
|
||||
CREATE INDEX idx_photos_group ON photos(group_key);
|
||||
CREATE INDEX idx_photos_unresolved ON photos(resolved_at);
|
||||
CREATE INDEX idx_photos_place ON photos(place_id);
|
||||
PRAGMA writable_schema=OFF;
|
||||
COMMIT;
|
||||
@@ -9,7 +9,7 @@ use super::Db;
|
||||
use crate::Result;
|
||||
|
||||
/// Current schema version. Bump on every forward migration added below.
|
||||
pub(super) const SCHEMA_VERSION: i64 = 34;
|
||||
pub(super) const SCHEMA_VERSION: i64 = 36;
|
||||
|
||||
impl Db {
|
||||
pub(super) fn migrate(&self) -> Result<()> {
|
||||
@@ -316,6 +316,38 @@ impl Db {
|
||||
tx.execute_batch(MIGRATION_V34)?;
|
||||
}
|
||||
}
|
||||
if current < 35 {
|
||||
// Same guards as V26-V34.
|
||||
let has_column: bool = tx.query_row(
|
||||
"SELECT COUNT(*) FROM pragma_table_info('trash') WHERE name = 'trashed_at'",
|
||||
[],
|
||||
|row| row.get::<_, i64>(0),
|
||||
)? > 0;
|
||||
let has_trash: bool = tx.query_row(
|
||||
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'trash'",
|
||||
[],
|
||||
|row| row.get::<_, i64>(0),
|
||||
)? > 0;
|
||||
if has_trash && !has_column {
|
||||
tx.execute_batch(MIGRATION_V35)?;
|
||||
}
|
||||
}
|
||||
if current < 36 {
|
||||
// Same guards as V26-V35.
|
||||
let has_column: bool = tx.query_row(
|
||||
"SELECT COUNT(*) FROM pragma_table_info('nodes') WHERE name = 'lid'",
|
||||
[],
|
||||
|row| row.get::<_, i64>(0),
|
||||
)? > 0;
|
||||
let has_nodes: bool = tx.query_row(
|
||||
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'nodes'",
|
||||
[],
|
||||
|row| row.get::<_, i64>(0),
|
||||
)? > 0;
|
||||
if has_nodes && !has_column {
|
||||
tx.execute_batch(MIGRATION_V36)?;
|
||||
}
|
||||
}
|
||||
tx.execute(
|
||||
"INSERT INTO sync_state (key, value) VALUES ('schema_version', ?1)
|
||||
ON CONFLICT(key) DO UPDATE SET value = excluded.value",
|
||||
@@ -1033,3 +1065,50 @@ UPDATE photos SET resolved_at = NULL;
|
||||
const MIGRATION_V34: &str = "
|
||||
ALTER TABLE photos ADD COLUMN similar_hash INTEGER;
|
||||
";
|
||||
|
||||
/// Schema v35: when each trashed node went to the trash.
|
||||
///
|
||||
/// The API does not tell this client, so `trashed_at` is when the daemon first
|
||||
/// saw the node in the trash, in epoch seconds. `NULL` when it cannot say: rows
|
||||
/// from before this version, and nodes found by a refresh that did not follow a
|
||||
/// complete one, which may have been in the trash for a long time already.
|
||||
const MIGRATION_V35: &str = "
|
||||
ALTER TABLE trash ADD COLUMN trashed_at INTEGER;
|
||||
";
|
||||
|
||||
/// Schema v36: every node row has a local id (`lid`), phase 2 of
|
||||
/// docs/MILESTONE-3.0.0.md.
|
||||
///
|
||||
/// The `uid` was the row's key, so a node had no identity until Drive minted
|
||||
/// one, and a create that landed moved its row to a new key. The `lid` is given
|
||||
/// when the row is first written and never changes or comes back, which is what
|
||||
/// `AUTOINCREMENT` adds over a plain rowid. It takes the old rowid, so the
|
||||
/// search index, keyed by rowid since v15, stays valid. The `uid` becomes
|
||||
/// nullable for a node that is not on Drive yet.
|
||||
const MIGRATION_V36: &str = "
|
||||
CREATE TABLE nodes_v36 (
|
||||
lid INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
uid TEXT UNIQUE,
|
||||
parent_uid TEXT,
|
||||
name TEXT NOT NULL,
|
||||
is_dir INTEGER NOT NULL,
|
||||
size INTEGER,
|
||||
mtime INTEGER NOT NULL,
|
||||
revision_hash TEXT,
|
||||
trashed INTEGER NOT NULL DEFAULT 0,
|
||||
node_json TEXT,
|
||||
listed INTEGER NOT NULL DEFAULT 0,
|
||||
path TEXT
|
||||
);
|
||||
INSERT INTO nodes_v36
|
||||
(lid, uid, parent_uid, name, is_dir, size, mtime, revision_hash, trashed, node_json, listed,
|
||||
path)
|
||||
SELECT rowid, uid, parent_uid, name, is_dir, size, mtime, revision_hash, trashed, node_json,
|
||||
listed, path
|
||||
FROM nodes;
|
||||
DROP TABLE nodes;
|
||||
ALTER TABLE nodes_v36 RENAME TO nodes;
|
||||
CREATE INDEX idx_nodes_parent ON nodes(parent_uid);
|
||||
CREATE INDEX idx_nodes_name_nocase ON nodes(name COLLATE NOCASE);
|
||||
CREATE INDEX idx_nodes_parent_live ON nodes(parent_uid) WHERE trashed = 0;
|
||||
";
|
||||
@@ -56,8 +56,9 @@ pub use local::LocalFileHit;
|
||||
pub use maintenance::{DbStats, VacuumOutcome};
|
||||
pub use nodes::{PublishedSharedRoot, SearchHit, StoredNode};
|
||||
pub use ops::{
|
||||
AttachedBlob, FAILING_ATTEMPTS, LOCAL_VOLUME, OP_CREATE, OP_MKDIR, OP_RENAME, OP_REVISION,
|
||||
OP_TRASH, PARK_EXPIRY_MS, PARK_UNTIL, PendingCounts, PendingOp, RenameMeta, op_supersedes,
|
||||
AttachedBlob, CreateLanding, FAILING_ATTEMPTS, LOCAL_VOLUME, OP_CREATE, OP_MKDIR, OP_RENAME,
|
||||
OP_REVISION, OP_TRASH, PARK_EXPIRY_MS, PARK_UNTIL, PendingCounts, PendingOp, RenameMeta,
|
||||
op_supersedes,
|
||||
};
|
||||
pub use photos::{StoredPhoto, StoredPlace, THUMB_HAVE, THUMB_NONE, THUMB_UNKNOWN, TimelineRow};
|
||||
pub use pins::PinRow;
|
||||
|
||||
@@ -17,6 +17,8 @@ use super::utils::{
|
||||
pub struct StoredNode {
|
||||
pub node: Node,
|
||||
pub listed: bool,
|
||||
/// The row's local id ([`Db::lid_of`]).
|
||||
pub lid: i64,
|
||||
}
|
||||
|
||||
/// One full-text search match: the stored [`Node`] plus its mountpoint-relative
|
||||
@@ -289,7 +291,8 @@ impl Db {
|
||||
let uid_str = parent_uid.to_string();
|
||||
let conn = self.read();
|
||||
let count: i64 = conn.query_row(
|
||||
"SELECT COUNT(*) FROM nodes WHERE parent_uid = ?1 AND trashed = 0",
|
||||
"SELECT COUNT(*) FROM nodes
|
||||
WHERE parent_uid = ?1 AND trashed = 0 AND node_json IS NOT NULL",
|
||||
params![uid_str],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
@@ -305,9 +308,11 @@ impl Db {
|
||||
pub fn node_path(&self, uid: &str) -> Result<Option<String>> {
|
||||
let conn = self.read();
|
||||
let exists: Option<i64> = conn
|
||||
.query_row("SELECT 1 FROM nodes WHERE uid = ?1", params![uid], |r| {
|
||||
r.get(0)
|
||||
})
|
||||
.query_row(
|
||||
"SELECT 1 FROM nodes WHERE uid = ?1 AND node_json IS NOT NULL",
|
||||
params![uid],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()?;
|
||||
if exists.is_none() {
|
||||
return Ok(None);
|
||||
@@ -511,17 +516,17 @@ impl Db {
|
||||
pub fn load_all(&self) -> Result<Vec<StoredNode>> {
|
||||
let conn = self.read();
|
||||
let mut stmt =
|
||||
conn.prepare("SELECT node_json, listed FROM nodes WHERE node_json IS NOT NULL")?;
|
||||
conn.prepare("SELECT node_json, listed, lid FROM nodes WHERE node_json IS NOT NULL")?;
|
||||
let rows = stmt.query_map([], |row| {
|
||||
let json: String = row.get(0)?;
|
||||
let listed: i64 = row.get(1)?;
|
||||
Ok((json, listed != 0))
|
||||
Ok((json, listed != 0, row.get(2)?))
|
||||
})?;
|
||||
let mut out = Vec::new();
|
||||
for row in rows {
|
||||
let (json, listed) = row?;
|
||||
let (json, listed, lid) = row?;
|
||||
let node: Node = serde_json::from_str(&json)?;
|
||||
out.push(StoredNode { node, listed });
|
||||
out.push(StoredNode { node, listed, lid });
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
@@ -544,6 +549,57 @@ impl Db {
|
||||
}
|
||||
}
|
||||
|
||||
/// The local id of the node stored under `uid`, or `None` when it has no
|
||||
/// row. It stays the same for as long as the row exists, including when a
|
||||
/// create lands and the row takes the uid Drive gave it.
|
||||
pub fn lid_of(&self, uid: &str) -> Result<Option<i64>> {
|
||||
let conn = self.read();
|
||||
Ok(conn
|
||||
.query_row("SELECT lid FROM nodes WHERE uid = ?1", params![uid], |r| {
|
||||
r.get(0)
|
||||
})
|
||||
.optional()?)
|
||||
}
|
||||
|
||||
/// The local id of each node, in order, giving a row to each that has none.
|
||||
///
|
||||
/// A new row is only a stub: it has no `node_json`, so no listing or search
|
||||
/// returns it until the node is upserted, which the caller still owes. That
|
||||
/// keeps this cheap enough to call with the inode lock held, which is
|
||||
/// where an inode number is first needed.
|
||||
pub fn lids_for(&self, nodes: &[&Node]) -> Result<Vec<i64>> {
|
||||
let mut conn = self.conn.lock();
|
||||
let tx = conn.transaction()?;
|
||||
let mut lids = Vec::with_capacity(nodes.len());
|
||||
{
|
||||
let mut known = tx.prepare_cached("SELECT lid FROM nodes WHERE uid = ?1")?;
|
||||
let mut stub = tx.prepare_cached(
|
||||
"INSERT INTO nodes (uid, parent_uid, name, is_dir, mtime, trashed)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
||||
)?;
|
||||
for node in nodes {
|
||||
let uid = node.uid.to_string();
|
||||
let lid = match known.query_row(params![uid], |r| r.get(0)).optional()? {
|
||||
Some(lid) => lid,
|
||||
None => {
|
||||
stub.execute(params![
|
||||
uid,
|
||||
node.parent_uid.as_ref().map(ToString::to_string),
|
||||
node.name,
|
||||
node.is_folder() as i64,
|
||||
node.modification_time,
|
||||
node.trashed as i64,
|
||||
])?;
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
};
|
||||
lids.push(lid);
|
||||
}
|
||||
}
|
||||
tx.commit()?;
|
||||
Ok(lids)
|
||||
}
|
||||
|
||||
/// Read the persisted incremental-sync cursor (a `DriveEventId`), if any.
|
||||
/// The daemon resumes from this on restart instead of reseeding to the
|
||||
/// server head, so changes made while unmounted are still applied (P2).
|
||||
@@ -559,6 +615,15 @@ impl Db {
|
||||
if listed != Some(1) {
|
||||
return Ok(None);
|
||||
}
|
||||
drop(conn);
|
||||
self.known_children(parent).map(Some)
|
||||
}
|
||||
|
||||
/// The children the DB knows under `parent`, whether or not the folder's
|
||||
/// listing is still marked complete. Only for when nothing better can be
|
||||
/// had: a listing dropped as stale may miss or keep a child.
|
||||
pub fn known_children(&self, parent: &NodeUid) -> Result<Vec<Node>> {
|
||||
let conn = self.read();
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT node_json FROM nodes
|
||||
WHERE parent_uid = ?1 AND node_json IS NOT NULL AND trashed = 0",
|
||||
@@ -568,7 +633,7 @@ impl Db {
|
||||
for json in rows {
|
||||
out.push(serde_json::from_str(&json?)?);
|
||||
}
|
||||
Ok(Some(out))
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
// --- Content-cache LRU index (P4) -------------------------------------
|
||||
@@ -590,6 +655,60 @@ struct PriorRow {
|
||||
trashed: bool,
|
||||
}
|
||||
|
||||
/// Hand a drained placeholder's row, and the rows below it, to the real uid it
|
||||
/// landed as.
|
||||
///
|
||||
/// Dropping the placeholder row and waiting for the server's copy of the real
|
||||
/// node left a window with no row for either uid. A queued child drained in
|
||||
/// that window found no authority for its parent, re-read it over the network
|
||||
/// and was deferred five seconds behind a folder that had just landed
|
||||
/// (`docs/BUGS.md` B120). The placeholder's own node, readdressed, stands in
|
||||
/// until the server's copy is upserted over it. A row the real uid already
|
||||
/// has is newer than the placeholder, so its node is kept, under the
|
||||
/// placeholder's `lid`: the node was known by that one first.
|
||||
pub(super) fn adopt_placeholder_row_tx(
|
||||
tx: &Transaction<'_>,
|
||||
local: &str,
|
||||
real: &str,
|
||||
) -> Result<()> {
|
||||
tx.execute(
|
||||
"UPDATE nodes SET parent_uid = ?2 WHERE parent_uid = ?1",
|
||||
params![local, real],
|
||||
)?;
|
||||
let row: Option<(i64, Option<String>)> = tx
|
||||
.query_row(
|
||||
"SELECT lid, node_json FROM nodes WHERE uid = ?1",
|
||||
params![local],
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
)
|
||||
.optional()?;
|
||||
let Some((lid, json)) = row else {
|
||||
return Ok(());
|
||||
};
|
||||
let known: Option<(i64, Option<String>)> = tx
|
||||
.query_row(
|
||||
"SELECT lid, node_json FROM nodes WHERE uid = ?1",
|
||||
params![real],
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
)
|
||||
.optional()?;
|
||||
if let Some((known_lid, _)) = known {
|
||||
tx.execute("DELETE FROM nodes WHERE lid = ?1", params![known_lid])?;
|
||||
tx.execute("DELETE FROM nodes_fts WHERE rowid = ?1", params![known_lid])?;
|
||||
}
|
||||
tx.execute(
|
||||
"UPDATE nodes SET uid = ?2 WHERE lid = ?1",
|
||||
params![lid, real],
|
||||
)?;
|
||||
let json = known.and_then(|(_, known_json)| known_json).or(json);
|
||||
let (Some(json), Some(real)) = (json, parse_node_uid(real)) else {
|
||||
return Ok(());
|
||||
};
|
||||
let mut node: Node = serde_json::from_str(&json)?;
|
||||
node.uid = real;
|
||||
upsert_node_tx(tx, &node)
|
||||
}
|
||||
|
||||
fn upsert_node_tx(tx: &Transaction<'_>, node: &Node) -> Result<()> {
|
||||
let json = serde_json::to_string(node)?;
|
||||
let uid = node.uid.to_string();
|
||||
@@ -741,8 +860,10 @@ fn reindex_subtree_tx(
|
||||
let mut set_path = tx.prepare("UPDATE nodes SET path = ?2 WHERE rowid = ?1")?;
|
||||
let mut unindex = tx.prepare("DELETE FROM nodes_fts WHERE rowid = ?1")?;
|
||||
let mut index = tx.prepare(
|
||||
// Not a stub row (`Db::lids_for`): it is indexed when its node is
|
||||
// written.
|
||||
"INSERT INTO nodes_fts (rowid, name, path)
|
||||
SELECT rowid, name, ?2 FROM nodes WHERE rowid = ?1",
|
||||
SELECT rowid, name, ?2 FROM nodes WHERE rowid = ?1 AND node_json IS NOT NULL",
|
||||
)?;
|
||||
for (rowid, path, blocked) in descendants {
|
||||
set_path.execute(params![rowid, path])?;
|
||||
|
||||
@@ -6,6 +6,7 @@ use rusqlite::{OptionalExtension, params};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::Db;
|
||||
use super::nodes::adopt_placeholder_row_tx;
|
||||
use crate::Result;
|
||||
|
||||
/// The `kind` of a [`PendingOp`] that uploads a staged file as a new revision.
|
||||
@@ -44,6 +45,12 @@ pub const OP_RENAME: &str = "rename";
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RenameMeta {
|
||||
pub original_parent_uid: String,
|
||||
/// The node's name on Drive when the first of its queued renames was made,
|
||||
/// kept across the renames that replace the row, like the parent. With it
|
||||
/// the drain knows where the node should still be when the op lands.
|
||||
/// `None` on a row queued before this field existed.
|
||||
#[serde(default)]
|
||||
pub original_name: Option<String>,
|
||||
}
|
||||
|
||||
/// The `kind` of a [`PendingOp`] that trashes a node the server knows about
|
||||
@@ -66,6 +73,63 @@ pub fn op_supersedes(kind: &str) -> bool {
|
||||
matches!(kind, OP_REVISION | OP_RENAME | OP_TRASH)
|
||||
}
|
||||
|
||||
/// Delete the ops that removing `uid` takes with it, returning the staged blobs
|
||||
/// they held.
|
||||
///
|
||||
/// That is every op for `uid` or for anything queued beneath it, plus the
|
||||
/// revisions of files the node tree places beneath it. A revision op carries
|
||||
/// no `parent_uid`, so the queue alone cannot tell that its file sits inside
|
||||
/// a removed folder: the upload outlived the folder, failed forever against a
|
||||
/// trashed node, and kept its blob on disk (`docs/BUGS.md` B102).
|
||||
///
|
||||
/// "Queued beneath it" starts from every folder the node tree places there,
|
||||
/// not only from `uid`. A create in a subfolder that already exists on Drive
|
||||
/// names that subfolder as its parent, so following the queue from `uid` alone
|
||||
/// missed it: a parked one kept its blob for good, and a due one would have
|
||||
/// been made in the root once its folder turned out trashed (`docs/BUGS.md`
|
||||
/// B116).
|
||||
///
|
||||
/// Other ops are taken from the node tree only for revisions. A queued rename
|
||||
/// that moved a node *into* the folder has to land, or the node would stay
|
||||
/// where it was on the server instead of going to the trash with the folder.
|
||||
fn drop_doomed_ops(tx: &rusqlite::Transaction<'_>, uid: &str) -> Result<Vec<String>> {
|
||||
const DOOMED: &str = "
|
||||
WITH RECURSIVE
|
||||
below(uid) AS (
|
||||
SELECT ?1
|
||||
UNION
|
||||
SELECT n.uid FROM nodes n JOIN below b ON n.parent_uid = b.uid
|
||||
),
|
||||
queued(uid) AS (
|
||||
SELECT ?1
|
||||
UNION
|
||||
SELECT p.uid FROM pending_op p
|
||||
WHERE p.kind IN ('create', 'mkdir')
|
||||
AND p.parent_uid IN (SELECT uid FROM below)
|
||||
UNION
|
||||
SELECT p.uid FROM pending_op p JOIN queued q ON p.parent_uid = q.uid
|
||||
),
|
||||
doomed(id) AS (
|
||||
SELECT id FROM pending_op
|
||||
WHERE uid IN (SELECT uid FROM queued)
|
||||
OR (kind = 'revision' AND uid IN (SELECT uid FROM below))
|
||||
)";
|
||||
let blobs: Vec<String> = {
|
||||
let mut stmt = tx.prepare(&format!(
|
||||
"{DOOMED}
|
||||
SELECT blob_path FROM pending_op
|
||||
WHERE id IN (SELECT id FROM doomed) AND blob_path IS NOT NULL"
|
||||
))?;
|
||||
let rows = stmt.query_map(params![uid], |r| r.get::<_, String>(0))?;
|
||||
rows.collect::<std::result::Result<Vec<_>, _>>()?
|
||||
};
|
||||
tx.execute(
|
||||
&format!("{DOOMED} DELETE FROM pending_op WHERE id IN (SELECT id FROM doomed)"),
|
||||
params![uid],
|
||||
)?;
|
||||
Ok(blobs)
|
||||
}
|
||||
|
||||
/// The volume id given to a node that exists only on this machine, so far. A
|
||||
/// real [`NodeUid`] is `{volume}~{link}`, so a placeholder is `local~<uuid>` and
|
||||
/// round-trips through the same `Display`/parse path as any other uid.
|
||||
@@ -164,6 +228,28 @@ pub struct PendingCounts {
|
||||
/// is retrying at the slowest rate it ever will and has been for minutes.
|
||||
pub const FAILING_ATTEMPTS: i64 = 6;
|
||||
|
||||
/// A queued create that has landed, for [`Db::finish_create`].
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct CreateLanding<'a> {
|
||||
/// The placeholder the node was known by.
|
||||
pub local: &'a str,
|
||||
/// The uid Drive gave it.
|
||||
pub real: &'a str,
|
||||
/// The parent and name the op held when the drain took it.
|
||||
pub sent: (&'a str, &'a str),
|
||||
/// The parent and name the node has on Drive. They differ from `sent`
|
||||
/// when the name was taken or the folder was gone.
|
||||
pub landed: (&'a str, &'a str),
|
||||
}
|
||||
|
||||
struct CreateRow {
|
||||
blob: Option<String>,
|
||||
meta: Option<String>,
|
||||
parent_uid: Option<String>,
|
||||
name: Option<String>,
|
||||
created_at: i64,
|
||||
}
|
||||
|
||||
/// The outcome of folding freshly written bytes into a queued create.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AttachedBlob {
|
||||
@@ -174,6 +260,24 @@ pub struct AttachedBlob {
|
||||
}
|
||||
|
||||
impl Db {
|
||||
/// Whether a queued trash still holds `name` somewhere.
|
||||
///
|
||||
/// A replacing rename or a delete-then-recreate queues the trash of the old
|
||||
/// node ahead of the op that wants its name. With several drain workers the
|
||||
/// later op can run first and find the name still taken; this tells it to
|
||||
/// wait for the trash rather than land under a conflict name.
|
||||
pub fn has_pending_trash_named(&self, name: &str) -> Result<bool> {
|
||||
let conn = self.read();
|
||||
conn.query_row(
|
||||
"SELECT EXISTS(
|
||||
SELECT 1 FROM pending_op WHERE kind = ?1 AND name = ?2
|
||||
)",
|
||||
params![OP_TRASH, name],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
|
||||
/// Whether a specific desired-state operation is still queued for a node.
|
||||
pub fn has_pending_op(&self, uid: &str, kind: &str) -> Result<bool> {
|
||||
let conn = self.read();
|
||||
@@ -252,27 +356,9 @@ impl Db {
|
||||
name: &str,
|
||||
created_at: i64,
|
||||
) -> Result<(i64, Vec<String>)> {
|
||||
const SUBTREE: &str = "
|
||||
WITH RECURSIVE doomed(uid) AS (
|
||||
SELECT ?1
|
||||
UNION
|
||||
SELECT p.uid FROM pending_op p JOIN doomed d ON p.parent_uid = d.uid
|
||||
)";
|
||||
let mut conn = self.conn.lock();
|
||||
let tx = conn.transaction()?;
|
||||
let blobs: Vec<String> = {
|
||||
let mut stmt = tx.prepare(&format!(
|
||||
"{SUBTREE}
|
||||
SELECT blob_path FROM pending_op
|
||||
WHERE uid IN (SELECT uid FROM doomed) AND blob_path IS NOT NULL"
|
||||
))?;
|
||||
let rows = stmt.query_map(params![uid], |row| row.get::<_, String>(0))?;
|
||||
rows.collect::<std::result::Result<Vec<_>, _>>()?
|
||||
};
|
||||
tx.execute(
|
||||
&format!("{SUBTREE} DELETE FROM pending_op WHERE uid IN (SELECT uid FROM doomed)"),
|
||||
params![uid],
|
||||
)?;
|
||||
let blobs = drop_doomed_ops(&tx, uid)?;
|
||||
tx.execute(
|
||||
"INSERT INTO pending_op
|
||||
(kind, uid, parent_uid, name, blob_path, meta_json, created_at, next_attempt_at)
|
||||
@@ -326,6 +412,102 @@ impl Db {
|
||||
Ok(Some(AttachedBlob { id, superseded }))
|
||||
}
|
||||
|
||||
/// Retire a queued create that has just landed as `real`, and repoint
|
||||
/// everything that named its placeholder, in one transaction.
|
||||
///
|
||||
/// The drain uploads the blob the create held when it was picked up. A
|
||||
/// write that closes while that upload is on the wire attaches a newer blob
|
||||
/// to the *same row* (`attach_blob_to_create`), so deleting the row by id
|
||||
/// threw that write away: the server kept the older bytes and nothing was
|
||||
/// left queued to replace them. When the row no longer holds `uploaded`, it
|
||||
/// becomes a revision of `real` instead, with its sidecar passed through
|
||||
/// `rewrite` so it names the real node. Returns that blob and sidecar, or
|
||||
/// `None` when the create is simply done.
|
||||
///
|
||||
/// A rename while the create is on the wire rewrites the row's target
|
||||
/// ([`Db::rewrite_op_target`]), which the create no longer reads. When the
|
||||
/// row names another parent or name than `landing.sent`, a rename of `real`
|
||||
/// to that target is queued in the same transaction, or the move would be
|
||||
/// lost with the row.
|
||||
pub fn finish_create(
|
||||
&self,
|
||||
id: i64,
|
||||
uploaded: Option<&str>,
|
||||
landing: &CreateLanding<'_>,
|
||||
rewrite: impl FnOnce(&str) -> Option<String>,
|
||||
) -> Result<Option<(String, String)>> {
|
||||
let (local, real) = (landing.local, landing.real);
|
||||
let mut conn = self.conn.lock();
|
||||
let tx = conn.transaction()?;
|
||||
let row: Option<CreateRow> = tx
|
||||
.query_row(
|
||||
"SELECT blob_path, meta_json, parent_uid, name, created_at
|
||||
FROM pending_op WHERE id = ?1",
|
||||
params![id],
|
||||
|r| {
|
||||
Ok(CreateRow {
|
||||
blob: r.get(0)?,
|
||||
meta: r.get(1)?,
|
||||
parent_uid: r.get(2)?,
|
||||
name: r.get(3)?,
|
||||
created_at: r.get(4)?,
|
||||
})
|
||||
},
|
||||
)
|
||||
.optional()?;
|
||||
let newer = match &row {
|
||||
Some(CreateRow {
|
||||
blob: Some(blob),
|
||||
meta: Some(meta),
|
||||
..
|
||||
}) if uploaded != Some(blob.as_str()) => rewrite(meta).map(|meta| (blob.clone(), meta)),
|
||||
_ => None,
|
||||
};
|
||||
match &newer {
|
||||
Some((_, meta)) => {
|
||||
tx.execute(
|
||||
"UPDATE pending_op
|
||||
SET kind = ?2, uid = ?3, parent_uid = NULL, name = NULL, meta_json = ?4,
|
||||
attempts = 0, last_error = NULL, next_attempt_at = 0
|
||||
WHERE id = ?1",
|
||||
params![id, OP_REVISION, real, meta],
|
||||
)?;
|
||||
}
|
||||
None => {
|
||||
tx.execute("DELETE FROM pending_op WHERE id = ?1", params![id])?;
|
||||
}
|
||||
}
|
||||
if let Some(CreateRow {
|
||||
parent_uid: Some(parent),
|
||||
name: Some(name),
|
||||
created_at,
|
||||
..
|
||||
}) = &row
|
||||
&& (parent.as_str(), name.as_str()) != landing.sent
|
||||
{
|
||||
let meta = serde_json::to_string(&RenameMeta {
|
||||
original_parent_uid: landing.landed.0.to_string(),
|
||||
original_name: Some(landing.landed.1.to_string()),
|
||||
})?;
|
||||
tx.execute(
|
||||
"DELETE FROM pending_op WHERE uid = ?1 AND kind = ?2",
|
||||
params![real, OP_RENAME],
|
||||
)?;
|
||||
tx.execute(
|
||||
"INSERT INTO pending_op (kind, uid, parent_uid, name, meta_json, created_at)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
||||
params![OP_RENAME, real, parent, name, meta, created_at],
|
||||
)?;
|
||||
}
|
||||
tx.execute(
|
||||
"UPDATE pending_op SET parent_uid = ?2 WHERE parent_uid = ?1",
|
||||
params![local, real],
|
||||
)?;
|
||||
adopt_placeholder_row_tx(&tx, local, real)?;
|
||||
tx.commit()?;
|
||||
Ok(newer)
|
||||
}
|
||||
|
||||
/// Replace the sidecar of a queued op, for a baseline that has moved under
|
||||
/// it.
|
||||
///
|
||||
@@ -428,36 +610,20 @@ impl Db {
|
||||
/// and in the user's pending count, for the life of the database.
|
||||
///
|
||||
/// Only `create`/`mkdir` ops carry a `parent_uid`, so for a file this
|
||||
/// recursion finds nothing and costs one query.
|
||||
/// recursion finds nothing and costs one query. Revisions of files the
|
||||
/// node tree places beneath `uid` go too; see [`drop_doomed_ops`].
|
||||
pub fn delete_ops_for_uid(&self, uid: &str) -> Result<Vec<String>> {
|
||||
const SUBTREE: &str = "
|
||||
WITH RECURSIVE doomed(uid) AS (
|
||||
SELECT ?1
|
||||
UNION
|
||||
SELECT p.uid FROM pending_op p JOIN doomed d ON p.parent_uid = d.uid
|
||||
)";
|
||||
let mut conn = self.conn.lock();
|
||||
let tx = conn.transaction()?;
|
||||
let blobs: Vec<String> = {
|
||||
let mut stmt = tx.prepare(&format!(
|
||||
"{SUBTREE}
|
||||
SELECT blob_path FROM pending_op
|
||||
WHERE uid IN (SELECT uid FROM doomed) AND blob_path IS NOT NULL"
|
||||
))?;
|
||||
let rows = stmt.query_map(params![uid], |r| r.get::<_, String>(0))?;
|
||||
rows.collect::<std::result::Result<Vec<_>, _>>()?
|
||||
};
|
||||
tx.execute(
|
||||
&format!("{SUBTREE} DELETE FROM pending_op WHERE uid IN (SELECT uid FROM doomed)"),
|
||||
params![uid],
|
||||
)?;
|
||||
let blobs = drop_doomed_ops(&tx, uid)?;
|
||||
tx.commit()?;
|
||||
Ok(blobs)
|
||||
}
|
||||
|
||||
/// Rewrite every queued op that points at a placeholder parent, once that
|
||||
/// parent has drained and has a real uid. Also moves the node rows whose
|
||||
/// parent column still names the placeholder, so listings keep resolving.
|
||||
/// parent column still names the placeholder, so listings keep resolving,
|
||||
/// and readdresses the placeholder's own row.
|
||||
pub fn remap_local_uid(&self, local: &str, real: &str) -> Result<()> {
|
||||
let mut conn = self.conn.lock();
|
||||
let tx = conn.transaction()?;
|
||||
@@ -465,11 +631,7 @@ impl Db {
|
||||
"UPDATE pending_op SET parent_uid = ?2 WHERE parent_uid = ?1",
|
||||
params![local, real],
|
||||
)?;
|
||||
tx.execute(
|
||||
"UPDATE nodes SET parent_uid = ?2 WHERE parent_uid = ?1",
|
||||
params![local, real],
|
||||
)?;
|
||||
tx.execute("DELETE FROM nodes WHERE uid = ?1", params![local])?;
|
||||
adopt_placeholder_row_tx(&tx, local, real)?;
|
||||
tx.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -765,6 +927,19 @@ impl Db {
|
||||
/// drain replaces it with the uid the server assigns.
|
||||
pub const LOCAL_VOLUME: &str = "local";
|
||||
|
||||
/// Whether op `id` is still queued. A drain worker that claimed it can find
|
||||
/// it gone: the node was deleted, or a newer write superseded it, while the
|
||||
/// worker was talking to the server.
|
||||
pub fn op_exists(&self, id: i64) -> Result<bool> {
|
||||
let conn = self.read();
|
||||
let count: i64 = conn.query_row(
|
||||
"SELECT COUNT(*) FROM pending_op WHERE id = ?1",
|
||||
params![id],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
Ok(count > 0)
|
||||
}
|
||||
|
||||
/// Drop a queued op, once its upload has actually landed.
|
||||
pub fn delete_op(&self, id: i64) -> Result<()> {
|
||||
let conn = self.conn.lock();
|
||||
|
||||
@@ -98,7 +98,7 @@ impl Db {
|
||||
pub fn effective_node_access(&self, uid: &NodeUid) -> Result<Option<Access>> {
|
||||
let conn = self.read();
|
||||
let exists: bool = conn.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM nodes WHERE uid = ?1)",
|
||||
"SELECT EXISTS(SELECT 1 FROM nodes WHERE uid = ?1 AND node_json IS NOT NULL)",
|
||||
[uid.to_string()],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
|
||||
@@ -260,6 +260,88 @@ impl Db {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Drop the baseline rows of `rel_path` and everything under it.
|
||||
///
|
||||
/// Used when a subtree leaves a mirror folder through a move on Drive: its
|
||||
/// local copy is about to be removed, and rows left behind would read as
|
||||
/// "deleted locally" and trash the moved nodes at their new place.
|
||||
pub fn sync_entries_remove_subtree(&self, folder_id: i64, rel_path: &str) -> Result<()> {
|
||||
let conn = self.conn.lock();
|
||||
conn.execute(
|
||||
"DELETE FROM sync_entry
|
||||
WHERE folder_id = ?1
|
||||
AND (rel_path = ?2 OR substr(rel_path, 1, length(?2) + 1) = ?2 || '/')",
|
||||
params![folder_id, rel_path],
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Move the baseline rows of `from_rel` and everything under it to `to_rel`
|
||||
/// in folder `to_folder`, in one transaction.
|
||||
///
|
||||
/// Used when a move on Drive took a mirror subtree along with its local
|
||||
/// copy: the rows still describe both sides correctly, only under a new
|
||||
/// path, so the next pass on either folder has nothing to do. The two
|
||||
/// folders may be the same one.
|
||||
pub fn sync_entries_move(
|
||||
&self,
|
||||
from_folder: i64,
|
||||
from_rel: &str,
|
||||
to_folder: i64,
|
||||
to_rel: &str,
|
||||
) -> Result<()> {
|
||||
let mut conn = self.conn.lock();
|
||||
let tx = conn.transaction()?;
|
||||
{
|
||||
let mut select = tx.prepare(
|
||||
"SELECT rel_path, remote_uid, local_mtime, local_size, remote_rev, remote_hash,
|
||||
local_mtime_ns
|
||||
FROM sync_entry
|
||||
WHERE folder_id = ?1
|
||||
AND (rel_path = ?2 OR substr(rel_path, 1, length(?2) + 1) = ?2 || '/')",
|
||||
)?;
|
||||
let rows = select
|
||||
.query_map(params![from_folder, from_rel], |r| {
|
||||
Ok(StoredSyncEntry {
|
||||
rel_path: r.get(0)?,
|
||||
remote_uid: r.get(1)?,
|
||||
local_mtime: r.get(2)?,
|
||||
local_size: r.get(3)?,
|
||||
remote_rev: r.get(4)?,
|
||||
remote_hash: r.get(5)?,
|
||||
local_mtime_ns: r.get(6)?,
|
||||
})
|
||||
})?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
let mut delete =
|
||||
tx.prepare("DELETE FROM sync_entry WHERE folder_id = ?1 AND rel_path = ?2")?;
|
||||
for row in &rows {
|
||||
delete.execute(params![from_folder, row.rel_path])?;
|
||||
}
|
||||
let mut insert = tx.prepare(
|
||||
"INSERT OR REPLACE INTO sync_entry
|
||||
(folder_id, rel_path, remote_uid, local_mtime, local_size, remote_rev,
|
||||
remote_hash, local_mtime_ns)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
|
||||
)?;
|
||||
for row in rows {
|
||||
let rel = format!("{to_rel}{}", &row.rel_path[from_rel.len()..]);
|
||||
insert.execute(params![
|
||||
to_folder,
|
||||
rel,
|
||||
row.remote_uid,
|
||||
row.local_mtime,
|
||||
row.local_size,
|
||||
row.remote_rev,
|
||||
row.remote_hash,
|
||||
row.local_mtime_ns,
|
||||
])?;
|
||||
}
|
||||
}
|
||||
tx.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Drop the entire baseline for a folder. Used when flipping ondemand→mirror:
|
||||
/// the local tree was evicted, so the old baseline is stale and would make the
|
||||
/// next reconcile mistake "locally deleted" for "must re-download". Clearing it
|
||||
|
||||
@@ -583,6 +583,7 @@ fn trash_replace_lists_folders_first() {
|
||||
size: 10,
|
||||
mtime: 1,
|
||||
parent_uid: Some("t2".into()),
|
||||
trashed_at: Some(100),
|
||||
},
|
||||
StoredTrash {
|
||||
uid: "t2".into(),
|
||||
@@ -591,6 +592,7 @@ fn trash_replace_lists_folders_first() {
|
||||
size: 0,
|
||||
mtime: 2,
|
||||
parent_uid: None,
|
||||
trashed_at: None,
|
||||
},
|
||||
])
|
||||
.unwrap();
|
||||
@@ -613,6 +615,15 @@ fn trash_replace_lists_folders_first() {
|
||||
]
|
||||
);
|
||||
|
||||
// The first-seen times round-trip, unknown ones included.
|
||||
let mut seen: Vec<_> = db.trash_seen().unwrap().into_iter().collect();
|
||||
seen.sort();
|
||||
assert_eq!(
|
||||
seen,
|
||||
[("t1".to_string(), Some(100)), ("t2".to_string(), None)]
|
||||
);
|
||||
assert_eq!(items[1].trashed_at, Some(100));
|
||||
|
||||
// A replace is a replace: emptying the trash on the server empties it here.
|
||||
db.trash_replace(&[]).unwrap();
|
||||
assert!(db.trash_list().unwrap().is_empty());
|
||||
@@ -777,6 +788,8 @@ fn children_if_listed_gated_on_flag() {
|
||||
|
||||
// Not listed yet → unknown, force a re-fetch.
|
||||
assert!(db.children_if_listed(&uid("root")).unwrap().is_none());
|
||||
// Still known, for a caller that has nothing better while offline.
|
||||
assert_eq!(db.known_children(&uid("root")).unwrap().len(), 2);
|
||||
|
||||
db.set_listed(&uid("root"), true).unwrap();
|
||||
let kids = db.children_if_listed(&uid("root")).unwrap().unwrap();
|
||||
@@ -2447,6 +2460,20 @@ fn atomic_trash_replacement_returns_owned_blobs_after_commit() {
|
||||
assert!(ops[0].blob_path.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_queued_trash_holds_its_name_until_it_lands() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let node = uid("held-name").to_string();
|
||||
assert!(!db.has_pending_trash_named("report.txt").unwrap());
|
||||
|
||||
let (trash_id, _) = db.replace_ops_with_trash(&node, "report.txt", 1).unwrap();
|
||||
assert!(db.has_pending_trash_named("report.txt").unwrap());
|
||||
assert!(!db.has_pending_trash_named("other.txt").unwrap());
|
||||
|
||||
db.delete_op(trash_id).unwrap();
|
||||
assert!(!db.has_pending_trash_named("report.txt").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_atomic_trash_insert_keeps_prior_revision_and_blob_ownership() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
@@ -2487,6 +2514,139 @@ fn failed_atomic_trash_insert_keeps_prior_revision_and_blob_ownership() {
|
||||
);
|
||||
}
|
||||
|
||||
/// A revision op names no parent, so the queue alone cannot see that its file
|
||||
/// sits inside a folder being removed. The node tree can: trashing the folder
|
||||
/// has to take the uploads of the files below it along, or they fail forever
|
||||
/// against a trashed node and keep their bytes on disk (B102). A rename that
|
||||
/// moved a node into the folder still has to land.
|
||||
#[test]
|
||||
fn trashing_a_folder_drops_the_queued_revisions_of_files_below_it() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
db.upsert_nodes(&[
|
||||
folder("root", None, "My Files"),
|
||||
folder("doomed", Some("root"), "doomed"),
|
||||
folder("sub", Some("doomed"), "sub"),
|
||||
file("deep", "sub", "deep.bin", 1),
|
||||
file("moved", "doomed", "moved.txt", 1),
|
||||
file("kept", "root", "kept.bin", 1),
|
||||
])
|
||||
.unwrap();
|
||||
let op = |kind: &str, link: &str, blob: Option<&str>| PendingOp {
|
||||
id: 0,
|
||||
kind: kind.to_string(),
|
||||
uid: uid(link).to_string(),
|
||||
parent_uid: None,
|
||||
name: None,
|
||||
blob_path: blob.map(str::to_string),
|
||||
meta_json: Some("{}".to_string()),
|
||||
created_at: 1,
|
||||
attempts: 0,
|
||||
last_error: None,
|
||||
next_attempt_at: 0,
|
||||
};
|
||||
db.enqueue_op(&op(OP_REVISION, "deep", Some("/staging/deep")))
|
||||
.unwrap();
|
||||
db.enqueue_op(&op(OP_RENAME, "moved", None)).unwrap();
|
||||
db.enqueue_op(&op(OP_REVISION, "kept", Some("/staging/kept")))
|
||||
.unwrap();
|
||||
|
||||
let doomed = uid("doomed").to_string();
|
||||
let (_, blobs) = db.replace_ops_with_trash(&doomed, "doomed", 2).unwrap();
|
||||
assert_eq!(blobs, vec!["/staging/deep"]);
|
||||
|
||||
let mut left: Vec<(String, String)> = db
|
||||
.pending_ops()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|op| (op.kind, op.uid))
|
||||
.collect();
|
||||
left.sort();
|
||||
assert_eq!(
|
||||
left,
|
||||
vec![
|
||||
(OP_RENAME.to_string(), uid("moved").to_string()),
|
||||
(OP_REVISION.to_string(), uid("kept").to_string()),
|
||||
(OP_TRASH.to_string(), doomed.clone()),
|
||||
]
|
||||
);
|
||||
|
||||
// Deleting outright follows the same tree.
|
||||
db.enqueue_op(&op(OP_REVISION, "deep", Some("/staging/deep2")))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
db.delete_ops_for_uid(&uid("sub").to_string()).unwrap(),
|
||||
vec!["/staging/deep2"]
|
||||
);
|
||||
}
|
||||
|
||||
/// A create in a subfolder that already exists on Drive names that subfolder,
|
||||
/// not the folder being trashed, as its parent. It has to go with the folder
|
||||
/// all the same, along with whatever is queued inside it, or a parked one
|
||||
/// keeps its bytes for good and a due one is made in the root (B116).
|
||||
#[test]
|
||||
fn trashing_a_folder_drops_the_queued_creates_in_its_subfolders() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
db.upsert_nodes(&[
|
||||
folder("root", None, "My Files"),
|
||||
folder("doomed", Some("root"), "doomed"),
|
||||
folder("sub", Some("doomed"), "sub"),
|
||||
folder("other", Some("root"), "other"),
|
||||
])
|
||||
.unwrap();
|
||||
let op = |kind: &str, node: &str, parent: &str, blob: Option<&str>| PendingOp {
|
||||
id: 0,
|
||||
kind: kind.to_string(),
|
||||
uid: node.to_string(),
|
||||
parent_uid: Some(parent.to_string()),
|
||||
name: Some(format!("{node}.bin")),
|
||||
blob_path: blob.map(str::to_string),
|
||||
meta_json: None,
|
||||
created_at: 1,
|
||||
attempts: 0,
|
||||
last_error: None,
|
||||
next_attempt_at: 0,
|
||||
};
|
||||
let sub = uid("sub").to_string();
|
||||
db.enqueue_op(&op(OP_CREATE, "local~deep", &sub, Some("/staging/deep")))
|
||||
.unwrap();
|
||||
db.enqueue_op(&op(OP_MKDIR, "local~dir", &sub, None))
|
||||
.unwrap();
|
||||
db.enqueue_op(&op(
|
||||
OP_CREATE,
|
||||
"local~deeper",
|
||||
"local~dir",
|
||||
Some("/staging/deeper"),
|
||||
))
|
||||
.unwrap();
|
||||
db.enqueue_op(&op(
|
||||
OP_CREATE,
|
||||
"local~kept",
|
||||
&uid("other").to_string(),
|
||||
Some("/staging/kept"),
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
let doomed = uid("doomed").to_string();
|
||||
let (_, mut blobs) = db.replace_ops_with_trash(&doomed, "doomed", 2).unwrap();
|
||||
blobs.sort();
|
||||
assert_eq!(blobs, vec!["/staging/deep", "/staging/deeper"]);
|
||||
|
||||
let mut left: Vec<(String, String)> = db
|
||||
.pending_ops()
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|op| (op.kind, op.uid))
|
||||
.collect();
|
||||
left.sort();
|
||||
assert_eq!(
|
||||
left,
|
||||
vec![
|
||||
(OP_CREATE.to_string(), "local~kept".to_string()),
|
||||
(OP_TRASH.to_string(), doomed),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/// Deleting a folder that was created offline must take the ops queued
|
||||
/// underneath it with it: they name a placeholder parent that will now never
|
||||
/// become real, so nothing could ever drain them and nothing is left to
|
||||
@@ -2688,6 +2848,165 @@ fn attaching_to_an_already_drained_create_reports_it_is_gone() {
|
||||
assert!(out.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_write_attached_while_its_create_uploads_becomes_a_revision() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let local = "local~abc";
|
||||
let real = uid("real").to_string();
|
||||
let parent = uid("parent").to_string();
|
||||
let create = |blob: Option<&str>| PendingOp {
|
||||
id: 0,
|
||||
kind: OP_CREATE.to_string(),
|
||||
uid: local.to_string(),
|
||||
parent_uid: Some(uid("parent").to_string()),
|
||||
name: Some("new.txt".to_string()),
|
||||
blob_path: blob.map(str::to_string),
|
||||
meta_json: blob.map(|_| "{\"uid\":\"local~abc\"}".to_string()),
|
||||
created_at: 1,
|
||||
attempts: 0,
|
||||
last_error: None,
|
||||
next_attempt_at: 0,
|
||||
};
|
||||
let (id, _) = db.enqueue_op(&create(None)).unwrap();
|
||||
// The drain picked the create up with no blob; the file's first write
|
||||
// closes while the empty upload is on the wire.
|
||||
db.attach_blob_to_create(local, "/staging/written", "{\"uid\":\"local~abc\"}")
|
||||
.unwrap()
|
||||
.expect("create is still queued");
|
||||
|
||||
let newer = db
|
||||
.finish_create(
|
||||
id,
|
||||
None,
|
||||
&landing(local, &real, &parent, "new.txt"),
|
||||
|json| Some(json.replace("local~abc", "vol~real")),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
newer,
|
||||
Some((
|
||||
"/staging/written".to_string(),
|
||||
"{\"uid\":\"vol~real\"}".to_string()
|
||||
))
|
||||
);
|
||||
let ops = db.pending_ops().unwrap();
|
||||
assert_eq!(ops.len(), 1, "the write is still queued");
|
||||
assert_eq!(ops[0].kind, OP_REVISION);
|
||||
assert_eq!(ops[0].uid, real);
|
||||
assert_eq!(ops[0].blob_path.as_deref(), Some("/staging/written"));
|
||||
assert_eq!(ops[0].parent_uid, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_create_whose_blob_landed_is_retired() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let local = "local~abc";
|
||||
let real = uid("real").to_string();
|
||||
let parent = uid("parent").to_string();
|
||||
let (id, _) = db
|
||||
.enqueue_op(&PendingOp {
|
||||
id: 0,
|
||||
kind: OP_CREATE.to_string(),
|
||||
uid: local.to_string(),
|
||||
parent_uid: Some(uid("parent").to_string()),
|
||||
name: Some("new.txt".to_string()),
|
||||
blob_path: Some("/staging/sent".to_string()),
|
||||
meta_json: Some("{}".to_string()),
|
||||
created_at: 1,
|
||||
attempts: 0,
|
||||
last_error: None,
|
||||
next_attempt_at: 0,
|
||||
})
|
||||
.unwrap();
|
||||
db.enqueue_op(&PendingOp {
|
||||
id: 0,
|
||||
kind: OP_CREATE.to_string(),
|
||||
uid: "local~child".to_string(),
|
||||
parent_uid: Some(local.to_string()),
|
||||
name: Some("inside.txt".to_string()),
|
||||
blob_path: None,
|
||||
meta_json: None,
|
||||
created_at: 2,
|
||||
attempts: 0,
|
||||
last_error: None,
|
||||
next_attempt_at: 0,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let newer = db
|
||||
.finish_create(
|
||||
id,
|
||||
Some("/staging/sent"),
|
||||
&landing(local, &real, &parent, "new.txt"),
|
||||
|_| panic!("an unchanged create has no sidecar to rewrite"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(newer, None);
|
||||
let ops = db.pending_ops().unwrap();
|
||||
assert_eq!(ops.len(), 1, "only the child is left");
|
||||
assert_eq!(ops[0].parent_uid.as_deref(), Some(real.as_str()));
|
||||
}
|
||||
|
||||
/// The parent and name a create was sent with, landing where it was sent.
|
||||
fn landing<'a>(local: &'a str, real: &'a str, parent: &'a str, name: &'a str) -> CreateLanding<'a> {
|
||||
CreateLanding {
|
||||
local,
|
||||
real,
|
||||
sent: (parent, name),
|
||||
landed: (parent, name),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_rename_made_while_its_create_uploads_is_queued_for_the_landed_node() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let local = "local~abc";
|
||||
let real = uid("real").to_string();
|
||||
let parent = uid("parent").to_string();
|
||||
let (id, _) = db
|
||||
.enqueue_op(&PendingOp {
|
||||
id: 0,
|
||||
kind: OP_CREATE.to_string(),
|
||||
uid: local.to_string(),
|
||||
parent_uid: Some(uid("parent").to_string()),
|
||||
name: Some("new.txt".to_string()),
|
||||
blob_path: Some("/staging/sent".to_string()),
|
||||
meta_json: Some("{}".to_string()),
|
||||
created_at: 1,
|
||||
attempts: 0,
|
||||
last_error: None,
|
||||
next_attempt_at: 0,
|
||||
})
|
||||
.unwrap();
|
||||
// The drain took the create; the user moves the file into a folder that
|
||||
// is itself still queued.
|
||||
db.rewrite_op_target(local, "local~dir", "moved.txt")
|
||||
.unwrap();
|
||||
|
||||
let mut landed = landing(local, &real, &parent, "new.txt");
|
||||
landed.landed.1 = "new (conflict).txt";
|
||||
db.finish_create(id, Some("/staging/sent"), &landed, |_| None)
|
||||
.unwrap();
|
||||
|
||||
let ops = db.pending_ops().unwrap();
|
||||
assert_eq!(ops.len(), 1, "the move outlives the create");
|
||||
assert_eq!(ops[0].kind, OP_RENAME);
|
||||
assert_eq!(ops[0].uid, real);
|
||||
assert_eq!(ops[0].parent_uid.as_deref(), Some("local~dir"));
|
||||
assert_eq!(ops[0].name.as_deref(), Some("moved.txt"));
|
||||
let meta: RenameMeta = serde_json::from_str(ops[0].meta_json.as_deref().unwrap()).unwrap();
|
||||
assert_eq!(meta.original_parent_uid, uid("parent").to_string());
|
||||
assert_eq!(meta.original_name.as_deref(), Some("new (conflict).txt"));
|
||||
|
||||
// The folder lands next and the move follows it.
|
||||
db.remap_local_uid("local~dir", &uid("dir").to_string())
|
||||
.unwrap();
|
||||
let ops = db.pending_ops().unwrap();
|
||||
assert_eq!(ops[0].parent_uid, Some(uid("dir").to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn draining_a_folder_repoints_its_queued_children() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
@@ -2717,6 +3036,414 @@ fn draining_a_folder_repoints_its_queued_children() {
|
||||
assert_eq!(ops[0].parent_uid.as_deref(), Some(real_dir.as_str()));
|
||||
}
|
||||
|
||||
/// B120: a child queued inside a folder that has just landed is checked
|
||||
/// against the folder's real uid before the server's copy of it arrives.
|
||||
#[test]
|
||||
fn a_landed_folder_keeps_a_row_until_the_server_copy_replaces_it() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
db.upsert_node(&folder("root", None, "My Files")).unwrap();
|
||||
let local = NodeUid::new(VolumeId::from("local"), LinkId::from("dir"));
|
||||
let mut placeholder = folder("dir", Some("root"), "New folder");
|
||||
placeholder.uid = local.clone();
|
||||
db.upsert_node(&placeholder).unwrap();
|
||||
let mut child = file("child", "root", "inside.txt", 1);
|
||||
child.parent_uid = Some(local.clone());
|
||||
db.upsert_node(&child).unwrap();
|
||||
let (id, _) = db
|
||||
.enqueue_op(&PendingOp {
|
||||
id: 0,
|
||||
kind: OP_MKDIR.to_string(),
|
||||
uid: local.to_string(),
|
||||
parent_uid: Some(uid("root").to_string()),
|
||||
name: Some("New folder".to_string()),
|
||||
blob_path: None,
|
||||
meta_json: None,
|
||||
created_at: 1,
|
||||
attempts: 0,
|
||||
last_error: None,
|
||||
next_attempt_at: 0,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
db.finish_create(
|
||||
id,
|
||||
None,
|
||||
&landing(
|
||||
&local.to_string(),
|
||||
&uid("real").to_string(),
|
||||
&uid("root").to_string(),
|
||||
"New folder",
|
||||
),
|
||||
|_| None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert!(db.node_by_uid(&local.to_string()).unwrap().is_none());
|
||||
let landed = db
|
||||
.node_by_uid(&uid("real").to_string())
|
||||
.unwrap()
|
||||
.expect("the landed folder has a row before the server's copy is read");
|
||||
assert_eq!(landed.uid, uid("real"));
|
||||
assert_eq!(landed.name, "New folder");
|
||||
assert_eq!(
|
||||
db.effective_node_access(&uid("child")).unwrap(),
|
||||
Some(crate::Access::Owner),
|
||||
"the child's authority resolves without a network re-read"
|
||||
);
|
||||
let names: Vec<_> = db
|
||||
.known_children(&uid("real"))
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|n| n.name)
|
||||
.collect();
|
||||
assert_eq!(names, ["inside.txt"]);
|
||||
let found = db.search("inside", 10).unwrap();
|
||||
assert_eq!(found.len(), 1);
|
||||
assert_eq!(found[0].path, "New folder/inside.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_landed_placeholder_never_overwrites_the_server_copy() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
db.upsert_node(&folder("root", None, "My Files")).unwrap();
|
||||
let local = NodeUid::new(VolumeId::from("local"), LinkId::from("dir"));
|
||||
let mut placeholder = folder("dir", Some("root"), "New folder");
|
||||
placeholder.uid = local.clone();
|
||||
db.upsert_node(&placeholder).unwrap();
|
||||
// The event stream reported the folder before the drain got round to it.
|
||||
db.upsert_node(&folder("real", Some("root"), "New folder (1)"))
|
||||
.unwrap();
|
||||
|
||||
db.remap_local_uid(&local.to_string(), &uid("real").to_string())
|
||||
.unwrap();
|
||||
|
||||
assert!(db.node_by_uid(&local.to_string()).unwrap().is_none());
|
||||
assert_eq!(
|
||||
db.node_by_uid(&uid("real").to_string())
|
||||
.unwrap()
|
||||
.map(|n| n.name),
|
||||
Some("New folder (1)".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_node_keeps_its_lid_across_updates_and_a_new_one_never_reuses_it() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
db.upsert_node(&folder("root", None, "My Files")).unwrap();
|
||||
db.upsert_node(&file("a", "root", "a.txt", 1)).unwrap();
|
||||
db.upsert_node(&file("b", "root", "b.txt", 1)).unwrap();
|
||||
let a = db.lid_of(&uid("a").to_string()).unwrap().unwrap();
|
||||
let b = db.lid_of(&uid("b").to_string()).unwrap().unwrap();
|
||||
|
||||
db.upsert_node(&file("a", "root", "renamed.txt", 2))
|
||||
.unwrap();
|
||||
assert_eq!(db.lid_of(&uid("a").to_string()).unwrap(), Some(a));
|
||||
|
||||
db.delete_node(&uid("b")).unwrap();
|
||||
db.upsert_node(&file("c", "root", "c.txt", 1)).unwrap();
|
||||
let c = db.lid_of(&uid("c").to_string()).unwrap().unwrap();
|
||||
assert!(c > b, "lid {c} reuses one a deleted row had");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lid_handed_out_before_the_node_is_stored_reads_as_no_node() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
db.upsert_node(&folder("root", None, "My Files")).unwrap();
|
||||
db.set_share_access(&uid("root"), crate::Access::Owner)
|
||||
.unwrap();
|
||||
let stub = file("stub", "root", "stub.txt", 1);
|
||||
let [lid] = db.lids_for(&[&stub]).unwrap()[..] else {
|
||||
panic!("one lid per node");
|
||||
};
|
||||
|
||||
assert_eq!(db.lid_of(&uid("stub").to_string()).unwrap(), Some(lid));
|
||||
assert_eq!(db.lids_for(&[&stub]).unwrap(), vec![lid]);
|
||||
assert!(db.node_by_uid(&uid("stub").to_string()).unwrap().is_none());
|
||||
assert!(db.effective_node_access(&uid("stub")).unwrap().is_none());
|
||||
assert!(db.node_path(&uid("stub").to_string()).unwrap().is_none());
|
||||
assert!(!db.has_children(&uid("root")).unwrap());
|
||||
assert!(db.search("stub", 10).unwrap().is_empty());
|
||||
assert!(db.load_all().unwrap().iter().all(|n| n.lid != lid));
|
||||
|
||||
db.upsert_node(&stub).unwrap();
|
||||
assert_eq!(db.lid_of(&uid("stub").to_string()).unwrap(), Some(lid));
|
||||
assert!(db.has_children(&uid("root")).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_landed_placeholder_keeps_its_lid() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
db.upsert_node(&folder("root", None, "My Files")).unwrap();
|
||||
for known in [false, true] {
|
||||
let local = NodeUid::new(VolumeId::from("local"), LinkId::from(format!("{known}")));
|
||||
let real = uid(&format!("real-{known}"));
|
||||
let mut placeholder = folder("dir", Some("root"), "New folder");
|
||||
placeholder.uid = local.clone();
|
||||
db.upsert_node(&placeholder).unwrap();
|
||||
let lid = db.lid_of(&local.to_string()).unwrap().unwrap();
|
||||
if known {
|
||||
// The event stream reported the folder before the drain got round
|
||||
// to it.
|
||||
let mut listed = folder("x", Some("root"), "New folder (1)");
|
||||
listed.uid = real.clone();
|
||||
db.upsert_node(&listed).unwrap();
|
||||
}
|
||||
|
||||
db.remap_local_uid(&local.to_string(), &real.to_string())
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(db.lid_of(&local.to_string()).unwrap(), None);
|
||||
assert_eq!(db.lid_of(&real.to_string()).unwrap(), Some(lid));
|
||||
let name = if known {
|
||||
"New folder (1)"
|
||||
} else {
|
||||
"New folder"
|
||||
};
|
||||
assert_eq!(
|
||||
db.node_by_uid(&real.to_string()).unwrap().map(|n| n.name),
|
||||
Some(name.to_string())
|
||||
);
|
||||
assert_eq!(db.search(name, 10).unwrap().len(), 1, "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn migration_v36_gives_every_node_its_rowid_as_lid() {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"pdfs-db-v35-fixture-{}-{}.db",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos()
|
||||
));
|
||||
let local = NodeUid::new(VolumeId::from("local"), LinkId::from("dir"));
|
||||
{
|
||||
let db = Db::open(&path).unwrap();
|
||||
db.upsert_node(&folder("root", None, "My Files")).unwrap();
|
||||
let mut placeholder = folder("dir", Some("root"), "Offline folder");
|
||||
placeholder.uid = local.clone();
|
||||
db.upsert_node(&placeholder).unwrap();
|
||||
db.upsert_node(&file("gone", "root", "gone.txt", 1))
|
||||
.unwrap();
|
||||
db.upsert_node(&file("kept", "root", "kept-report.txt", 1))
|
||||
.unwrap();
|
||||
db.delete_node(&uid("gone")).unwrap();
|
||||
let (id, _) = db
|
||||
.enqueue_op(&PendingOp {
|
||||
id: 0,
|
||||
kind: OP_MKDIR.to_string(),
|
||||
uid: local.to_string(),
|
||||
parent_uid: Some(uid("root").to_string()),
|
||||
name: Some("Offline folder".to_string()),
|
||||
blob_path: None,
|
||||
meta_json: None,
|
||||
created_at: 1,
|
||||
attempts: 0,
|
||||
last_error: None,
|
||||
next_attempt_at: 0,
|
||||
})
|
||||
.unwrap();
|
||||
db.record_op_failure(id, "network unreachable", 5_000)
|
||||
.unwrap();
|
||||
}
|
||||
{
|
||||
// Put the file back in the state a released V35 database was in:
|
||||
// keyed by uid, with the rowids of the rows it held.
|
||||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE nodes_v35 (
|
||||
uid TEXT PRIMARY KEY, parent_uid TEXT, name TEXT NOT NULL,
|
||||
is_dir INTEGER NOT NULL, size INTEGER, mtime INTEGER NOT NULL,
|
||||
revision_hash TEXT, trashed INTEGER NOT NULL DEFAULT 0, node_json TEXT,
|
||||
listed INTEGER NOT NULL DEFAULT 0, path TEXT
|
||||
);
|
||||
INSERT INTO nodes_v35 (rowid, uid, parent_uid, name, is_dir, size, mtime,
|
||||
revision_hash, trashed, node_json, listed, path)
|
||||
SELECT lid, uid, parent_uid, name, is_dir, size, mtime, revision_hash,
|
||||
trashed, node_json, listed, path FROM nodes;
|
||||
DROP TABLE nodes;
|
||||
ALTER TABLE nodes_v35 RENAME TO nodes;
|
||||
CREATE INDEX idx_nodes_parent ON nodes(parent_uid);
|
||||
CREATE INDEX idx_nodes_name_nocase ON nodes(name COLLATE NOCASE);
|
||||
CREATE INDEX idx_nodes_parent_live ON nodes(parent_uid) WHERE trashed = 0;
|
||||
DELETE FROM sqlite_sequence WHERE name = 'nodes';
|
||||
UPDATE sync_state SET value = '35' WHERE key = 'schema_version';",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let rowids: Vec<(String, i64)> = {
|
||||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT uid, rowid FROM nodes ORDER BY rowid")
|
||||
.unwrap();
|
||||
stmt.query_map([], |r| Ok((r.get(0)?, r.get(1)?)))
|
||||
.unwrap()
|
||||
.map(|row| row.unwrap())
|
||||
.collect()
|
||||
};
|
||||
assert_eq!(rowids.len(), 3);
|
||||
|
||||
let db = Db::open(&path).unwrap();
|
||||
for (uid, rowid) in &rowids {
|
||||
assert_eq!(db.lid_of(uid).unwrap(), Some(*rowid), "{uid}");
|
||||
}
|
||||
let found = db.search("report", 10).unwrap();
|
||||
assert_eq!(found.len(), 1, "the search index still finds the node");
|
||||
assert_eq!(found[0].node.uid, uid("kept"));
|
||||
let ops = db.pending_ops().unwrap();
|
||||
assert_eq!(ops.len(), 1);
|
||||
assert_eq!(ops[0].uid, local.to_string());
|
||||
assert_eq!(ops[0].attempts, 1);
|
||||
assert_eq!(ops[0].last_error.as_deref(), Some("network unreachable"));
|
||||
|
||||
db.upsert_node(&file("new", "root", "new.txt", 1)).unwrap();
|
||||
let newest = db.lid_of(&uid("new").to_string()).unwrap().unwrap();
|
||||
assert!(newest > rowids.last().unwrap().1);
|
||||
drop(db);
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
/// A database as 2.8.2 wrote it (`sqlite3 .dump`, schema 35), with queued ops
|
||||
/// in every state the drain leaves them in: due, parked, failing, deferred for
|
||||
/// access, and claimed by a worker when the daemon stopped. The creates below
|
||||
/// a folder made offline still name their parent by its `local~` placeholder.
|
||||
#[test]
|
||||
fn a_2_8_database_migrates_with_every_node_and_op_intact() {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"pdfs-db-2.8.2-{}-{}.db",
|
||||
std::process::id(),
|
||||
now_test_id()
|
||||
));
|
||||
let _ = std::fs::remove_file(&path);
|
||||
rusqlite::Connection::open(&path)
|
||||
.unwrap()
|
||||
.execute_batch(include_str!("fixtures/v2.8.2.sql"))
|
||||
.unwrap();
|
||||
let (nodes, ops) = migrates_intact(&path);
|
||||
assert_eq!((nodes, ops), (8, 6));
|
||||
let db = Db::open(&path).unwrap();
|
||||
let queued = db.pending_ops().unwrap();
|
||||
let inner = queued.iter().find(|op| op.uid == "local~inner").unwrap();
|
||||
assert_eq!(inner.parent_uid.as_deref(), Some("local~dir"));
|
||||
let parked = db.parked_create_ops().unwrap();
|
||||
assert_eq!(parked.len(), 1);
|
||||
assert_eq!(parked[0].name.as_deref(), Some("movie.part"));
|
||||
drop(db);
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
/// The same migration over a copy of a database a released build wrote, which
|
||||
/// the fixtures above cannot stand in for: years of rows, every kind of op, and
|
||||
/// whatever shapes real use left behind. The copy is taken with `VACUUM INTO`
|
||||
/// over a read-only connection, so the database named is never written.
|
||||
///
|
||||
/// `PDFS_MIGRATE_DB=~/.local/state/proton-drive-linux/cache.db cargo test -p
|
||||
/// pdfs-core a_copy_of_a_real_database -- --ignored`
|
||||
#[test]
|
||||
#[ignore = "needs a real database: set PDFS_MIGRATE_DB"]
|
||||
fn a_copy_of_a_real_database_migrates_with_every_node_and_op_intact() {
|
||||
let source = std::env::var("PDFS_MIGRATE_DB").expect("set PDFS_MIGRATE_DB");
|
||||
let path = std::env::temp_dir().join(format!("pdfs-db-real-{}.db", std::process::id()));
|
||||
let _ = std::fs::remove_file(&path);
|
||||
rusqlite::Connection::open_with_flags(&source, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)
|
||||
.unwrap()
|
||||
.execute("VACUUM INTO ?1", rusqlite::params![path.to_str().unwrap()])
|
||||
.unwrap();
|
||||
let (nodes, ops) = migrates_intact(&path);
|
||||
eprintln!("{nodes} nodes, {ops} queued ops");
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
/// Opens the database at `path` and checks that the migration kept every node
|
||||
/// on its row id and every queued op as it was. Returns how many of each.
|
||||
fn migrates_intact(path: &std::path::Path) -> (usize, usize) {
|
||||
let (version, nodes, ops, sealed) = {
|
||||
let conn = rusqlite::Connection::open(path).unwrap();
|
||||
let version: String = conn
|
||||
.query_row(
|
||||
"SELECT value FROM sync_state WHERE key = 'schema_version'",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.unwrap();
|
||||
let nodes: Vec<(String, i64)> = conn
|
||||
.prepare("SELECT uid, rowid FROM nodes")
|
||||
.unwrap()
|
||||
.query_map([], |r| Ok((r.get(0)?, r.get(1)?)))
|
||||
.unwrap()
|
||||
.map(|row| row.unwrap())
|
||||
.collect();
|
||||
let ops = rows(&conn, QUEUED);
|
||||
let sealed = rows(&conn, "SELECT * FROM own_sealed_rev ORDER BY uid");
|
||||
(version, nodes, ops, sealed)
|
||||
};
|
||||
let db = Db::open(path).unwrap();
|
||||
{
|
||||
let conn = rusqlite::Connection::open(path).unwrap();
|
||||
assert_eq!(
|
||||
rows(&conn, QUEUED),
|
||||
ops,
|
||||
"every queued op survives as it was, parked, failing or deferred"
|
||||
);
|
||||
// A claim names a drain worker of the daemon that wrote the file.
|
||||
let claimed: i64 = conn
|
||||
.query_row(
|
||||
"SELECT COUNT(*) FROM pending_op WHERE claimed_at <> 0",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(claimed, 0, "a stopped daemon's claims are released");
|
||||
assert_eq!(
|
||||
rows(&conn, "SELECT * FROM own_sealed_rev ORDER BY uid"),
|
||||
sealed
|
||||
);
|
||||
}
|
||||
if version.parse::<i64>().unwrap() < 36 {
|
||||
for (uid, rowid) in &nodes {
|
||||
assert_eq!(db.lid_of(uid).unwrap(), Some(*rowid), "{uid}");
|
||||
}
|
||||
} else {
|
||||
for (uid, _) in &nodes {
|
||||
assert!(db.lid_of(uid).unwrap().is_some(), "{uid}");
|
||||
}
|
||||
}
|
||||
drop(db);
|
||||
|
||||
let db = Db::open(path).unwrap();
|
||||
let conn = rusqlite::Connection::open(path).unwrap();
|
||||
let count: i64 = conn
|
||||
.query_row("SELECT COUNT(*) FROM nodes", [], |r| r.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(count as usize, nodes.len(), "no node is lost or doubled");
|
||||
let version: String = conn
|
||||
.query_row(
|
||||
"SELECT value FROM sync_state WHERE key = 'schema_version'",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(version, SCHEMA_VERSION.to_string());
|
||||
drop(db);
|
||||
(nodes.len(), ops.len())
|
||||
}
|
||||
|
||||
/// Every column of the queue but the drain's claim.
|
||||
const QUEUED: &str = "SELECT id, kind, uid, parent_uid, name, blob_path, meta_json, created_at,
|
||||
attempts, last_error, next_attempt_at, access_deferred_since
|
||||
FROM pending_op ORDER BY id";
|
||||
|
||||
/// Every row `query` returns, every column.
|
||||
fn rows(conn: &rusqlite::Connection, query: &str) -> Vec<Vec<rusqlite::types::Value>> {
|
||||
let mut stmt = conn.prepare(query).unwrap();
|
||||
let columns = stmt.column_count();
|
||||
stmt.query_map([], |r| (0..columns).map(|i| r.get(i)).collect())
|
||||
.unwrap()
|
||||
.map(|row| row.unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_op_stays_queued_with_backoff() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
@@ -2909,6 +3636,94 @@ fn clearing_a_stale_pending_mode_preserves_the_newer_intent() {
|
||||
assert_eq!(db.sync_folder_get(id).unwrap().unwrap().pending_mode, None);
|
||||
}
|
||||
|
||||
fn baseline_row(rel: &str) -> StoredSyncEntry {
|
||||
StoredSyncEntry {
|
||||
rel_path: rel.into(),
|
||||
remote_uid: Some(format!("vol~{rel}")),
|
||||
local_mtime: 1_700_000_000,
|
||||
local_mtime_ns: Some(1_700_000_000_000_000_001),
|
||||
local_size: 7,
|
||||
remote_rev: Some("1700000000".into()),
|
||||
remote_hash: Some("7".into()),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moving_a_baseline_subtree_takes_its_rows_and_leaves_name_prefixed_siblings() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let from = db.sync_folder_add("/home/me/A", "vol~a", "share").unwrap();
|
||||
let to = db.sync_folder_add("/home/me/B", "vol~b", "share").unwrap();
|
||||
for rel in [
|
||||
"docs",
|
||||
"docs/x.txt",
|
||||
"docs/sub/y.txt",
|
||||
"docsextra.txt",
|
||||
"other",
|
||||
] {
|
||||
db.sync_entry_upsert(from, &baseline_row(rel)).unwrap();
|
||||
}
|
||||
db.sync_entry_upsert(to, &baseline_row("inbox")).unwrap();
|
||||
|
||||
db.sync_entries_move(from, "docs", to, "inbox/docs")
|
||||
.unwrap();
|
||||
|
||||
let left = db.sync_entries(from).unwrap();
|
||||
let mut left: Vec<_> = left.keys().cloned().collect();
|
||||
left.sort();
|
||||
assert_eq!(left, ["docsextra.txt", "other"]);
|
||||
|
||||
let moved = db.sync_entries(to).unwrap();
|
||||
let mut keys: Vec<_> = moved.keys().cloned().collect();
|
||||
keys.sort();
|
||||
assert_eq!(
|
||||
keys,
|
||||
[
|
||||
"inbox",
|
||||
"inbox/docs",
|
||||
"inbox/docs/sub/y.txt",
|
||||
"inbox/docs/x.txt"
|
||||
]
|
||||
);
|
||||
// Only the path changes: the row still describes the same node on both sides.
|
||||
assert_eq!(
|
||||
moved["inbox/docs/x.txt"],
|
||||
StoredSyncEntry {
|
||||
rel_path: "inbox/docs/x.txt".into(),
|
||||
..baseline_row("docs/x.txt")
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn moving_a_baseline_subtree_within_one_folder_renames_it() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let folder = db.sync_folder_add("/home/me/A", "vol~a", "share").unwrap();
|
||||
for rel in ["old", "old/f.txt", "new"] {
|
||||
db.sync_entry_upsert(folder, &baseline_row(rel)).unwrap();
|
||||
}
|
||||
|
||||
db.sync_entries_move(folder, "old", folder, "new/old")
|
||||
.unwrap();
|
||||
|
||||
let mut keys: Vec<_> = db.sync_entries(folder).unwrap().into_keys().collect();
|
||||
keys.sort();
|
||||
assert_eq!(keys, ["new", "new/old", "new/old/f.txt"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removing_a_baseline_subtree_keeps_name_prefixed_siblings() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let folder = db.sync_folder_add("/home/me/A", "vol~a", "share").unwrap();
|
||||
for rel in ["docs", "docs/x.txt", "docsextra.txt"] {
|
||||
db.sync_entry_upsert(folder, &baseline_row(rel)).unwrap();
|
||||
}
|
||||
|
||||
db.sync_entries_remove_subtree(folder, "docs").unwrap();
|
||||
|
||||
let keys: Vec<_> = db.sync_entries(folder).unwrap().into_keys().collect();
|
||||
assert_eq!(keys, ["docsextra.txt"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_uid_lookup_covers_the_root_and_synced_descendants_only() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
@@ -4952,3 +5767,28 @@ fn retry_all_touches_only_ops_that_have_failed() {
|
||||
assert_eq!(due(debounced), 7_000);
|
||||
assert_eq!(due(parked), ops::PARK_UNTIL);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_queued_op_stops_existing_once_the_node_is_deleted() {
|
||||
let db = Db::open_in_memory().unwrap();
|
||||
let (id, _) = db
|
||||
.enqueue_op(&PendingOp {
|
||||
id: 0,
|
||||
kind: OP_REVISION.to_string(),
|
||||
uid: uid("journal").to_string(),
|
||||
parent_uid: None,
|
||||
name: None,
|
||||
blob_path: Some("/staging/journal".to_string()),
|
||||
meta_json: Some("{}".to_string()),
|
||||
created_at: 1,
|
||||
attempts: 0,
|
||||
last_error: None,
|
||||
next_attempt_at: 0,
|
||||
})
|
||||
.unwrap();
|
||||
assert!(db.op_exists(id).unwrap());
|
||||
|
||||
let blobs = db.delete_ops_for_uid(&uid("journal").to_string()).unwrap();
|
||||
assert_eq!(blobs, vec!["/staging/journal"]);
|
||||
assert!(!db.op_exists(id).unwrap());
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
//! Trashed nodes, persisted so the trash view opens without a round-trip.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use rusqlite::params;
|
||||
|
||||
use super::Db;
|
||||
@@ -15,6 +17,9 @@ pub struct StoredTrash {
|
||||
/// row written before schema v29, and for a node whose parent the server
|
||||
/// did not report.
|
||||
pub parent_uid: Option<String>,
|
||||
/// When the daemon first saw the node in the trash, epoch seconds. `None`
|
||||
/// when it cannot say (see schema v35).
|
||||
pub trashed_at: Option<i64>,
|
||||
}
|
||||
|
||||
impl Db {
|
||||
@@ -24,8 +29,8 @@ impl Db {
|
||||
tx.execute("DELETE FROM trash", [])?;
|
||||
{
|
||||
let mut stmt = tx.prepare(
|
||||
"INSERT INTO trash (uid, name, is_dir, size, mtime, parent_uid) \
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
||||
"INSERT INTO trash (uid, name, is_dir, size, mtime, parent_uid, trashed_at) \
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
|
||||
)?;
|
||||
for item in items {
|
||||
stmt.execute(params![
|
||||
@@ -34,7 +39,8 @@ impl Db {
|
||||
item.is_dir as i64,
|
||||
item.size,
|
||||
item.mtime,
|
||||
item.parent_uid
|
||||
item.parent_uid,
|
||||
item.trashed_at
|
||||
])?;
|
||||
}
|
||||
}
|
||||
@@ -47,7 +53,7 @@ impl Db {
|
||||
pub fn trash_list(&self) -> Result<Vec<StoredTrash>> {
|
||||
let conn = self.read();
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT uid, name, is_dir, size, mtime, parent_uid FROM trash
|
||||
"SELECT uid, name, is_dir, size, mtime, parent_uid, trashed_at FROM trash
|
||||
ORDER BY is_dir DESC, name COLLATE NOCASE",
|
||||
)?;
|
||||
let rows = stmt.query_map([], |r| {
|
||||
@@ -58,6 +64,7 @@ impl Db {
|
||||
size: r.get(3)?,
|
||||
mtime: r.get(4)?,
|
||||
parent_uid: r.get(5)?,
|
||||
trashed_at: r.get(6)?,
|
||||
})
|
||||
})?;
|
||||
let mut items = Vec::new();
|
||||
@@ -81,5 +88,19 @@ impl Db {
|
||||
Ok(pairs)
|
||||
}
|
||||
|
||||
/// When each node now in the trash was first seen there, keyed by uid, so
|
||||
/// a refresh can carry the times over into the listing it writes.
|
||||
pub fn trash_seen(&self) -> Result<HashMap<String, Option<i64>>> {
|
||||
let conn = self.read();
|
||||
let mut stmt = conn.prepare("SELECT uid, trashed_at FROM trash")?;
|
||||
let rows = stmt.query_map([], |r| Ok((r.get(0)?, r.get(1)?)))?;
|
||||
let mut seen = HashMap::new();
|
||||
for row in rows {
|
||||
let (uid, at) = row?;
|
||||
seen.insert(uid, at);
|
||||
}
|
||||
Ok(seen)
|
||||
}
|
||||
|
||||
// ---- device sync (devices.md) -----------------------------------------
|
||||
}
|
||||
@@ -66,6 +66,20 @@ impl Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Whether a control-socket round-trip gave up waiting for the reply: the
|
||||
/// daemon is running but stuck, as opposed to not running at all.
|
||||
pub fn is_timeout(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Error::Io(e) if matches!(
|
||||
e.kind(),
|
||||
std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
/// A failure on its way to a front-end: prose for the user, plus the
|
||||
|
||||
@@ -55,9 +55,10 @@ fn systemctl_query(args: &[&str]) -> bool {
|
||||
/// keyring checks). `--now` is a no-op on an active unit, so the daemon would
|
||||
/// keep sleeping and the mount would not appear until the next poll fired.
|
||||
/// Restarting makes the new session take effect immediately, and starts the
|
||||
/// unit when it is not running at all.
|
||||
/// unit when it is not running at all. `reenable` for the reason given on
|
||||
/// [`enable`].
|
||||
pub fn enable_start() -> bool {
|
||||
let enabled = systemctl(&["enable"]);
|
||||
let enabled = systemctl(&["reenable"]);
|
||||
systemctl(&["restart"]) && enabled
|
||||
}
|
||||
|
||||
@@ -97,8 +98,14 @@ pub fn is_enabled() -> bool {
|
||||
/// Enable the service so it auto-starts on future logins, without necessarily
|
||||
/// starting it now beyond what `enable` does. Used by the Settings "Start on
|
||||
/// login" switch; pairs with [`disable`](disable).
|
||||
///
|
||||
/// `reenable` rather than `enable`: older installs were enabled
|
||||
/// into `default.target.wants`, and a plain `enable` would add the new
|
||||
/// `graphical-session.target.wants` link while leaving the old one to start the
|
||||
/// unit too early. `reenable` drops every existing link and recreates them from
|
||||
/// the current `[Install]` section.
|
||||
pub fn enable() -> bool {
|
||||
systemctl(&["enable"])
|
||||
systemctl(&["reenable"])
|
||||
}
|
||||
|
||||
/// Disable auto-start without stopping the running mount. Used by the Settings
|
||||
|
||||
@@ -96,6 +96,26 @@ pub fn is_transient_name(name: &str) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Whether `name` is scratch another program writes and throws away: the
|
||||
/// [`is_transient_name`] set, plus the save-by-rename temporaries of GTK, Qt,
|
||||
/// Chromium and Electron, generic `.tmp` files and Emacs lock files. None of
|
||||
/// them is a file the user made, so a front-end can hide them from a history
|
||||
/// of what happened to the user's files.
|
||||
pub fn is_scratch_name(name: &str) -> bool {
|
||||
if is_transient_name(name) {
|
||||
return true;
|
||||
}
|
||||
let lower = name.to_ascii_lowercase();
|
||||
lower.ends_with(".tmp")
|
||||
|| lower.ends_with(".temp")
|
||||
|| name.starts_with(".goutputstream-")
|
||||
|| name.starts_with(".org.chromium.")
|
||||
|| name.starts_with(".com.google.chrome.")
|
||||
|| name.starts_with(".#")
|
||||
|| (name.starts_with('#') && name.ends_with('#') && name.len() > 1)
|
||||
|| (name.starts_with('.') && name.contains(".tmp."))
|
||||
}
|
||||
|
||||
/// Filenames accepted as a folder's ignore file, in precedence order.
|
||||
const IGNORE_FILE_NAMES: &[&str] = &[".pdfsignore", ".protonignore"];
|
||||
|
||||
@@ -341,6 +361,25 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_by_rename_temporaries_are_scratch() {
|
||||
for name in [
|
||||
".goutputstream-4XK2B2",
|
||||
".org.chromium.Chromium.aBc123",
|
||||
"~$report.docx",
|
||||
".~lock.budget.ods#",
|
||||
".#notes.org",
|
||||
"#notes.org#",
|
||||
"cache.tmp",
|
||||
"page.html.crdownload",
|
||||
] {
|
||||
assert!(is_scratch_name(name), "{name}");
|
||||
}
|
||||
for name in ["report.docx", "notes.org", "#1 hits.txt", ".bashrc"] {
|
||||
assert!(!is_scratch_name(name), "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finished_names_are_not_transient() {
|
||||
for name in [
|
||||
|
||||
@@ -15,6 +15,7 @@ zip.workspace = true
|
||||
sha1.workspace = true
|
||||
proton-drive-rs.workspace = true
|
||||
futures.workspace = true
|
||||
async-trait.workspace = true
|
||||
tokio.workspace = true
|
||||
tracing.workspace = true
|
||||
fuser.workspace = true
|
||||
@@ -24,3 +25,6 @@ base64.workspace = true
|
||||
image.workspace = true
|
||||
notify = "8"
|
||||
parking_lot.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tracing-subscriber.workspace = true
|
||||
@@ -15,7 +15,7 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use pdfs_core::control::{AlbumInfo, PhotoItem};
|
||||
use pdfs_core::control::{AlbumInfo, PhotoItem, Topic};
|
||||
use pdfs_core::db::StoredAlbum;
|
||||
use pdfs_core::{CoreError, CoreResult};
|
||||
|
||||
@@ -137,10 +137,12 @@ impl Core {
|
||||
}
|
||||
let core = self.clone();
|
||||
self.rt.spawn(async move {
|
||||
if let Err(e) = core.refresh_albums().await {
|
||||
warn!(error = %e, "background album refresh failed");
|
||||
}
|
||||
let refreshed = core.refresh_albums().await;
|
||||
core.albums_refreshing.store(false, Ordering::SeqCst);
|
||||
match refreshed {
|
||||
Ok(_) => core.events.publish(&[Topic::Photos]),
|
||||
Err(e) => warn!(error = %e, "background album refresh failed"),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ use crate::shutdown::Shutdown;
|
||||
///
|
||||
/// Returns whether this mount actually held the node, so the caller can evict
|
||||
/// its content blob once if *any* mount did.
|
||||
fn forget_and_notify(st: &mut State, notify: &mut NotifyBatch, uid: &NodeUid) -> bool {
|
||||
pub(crate) fn forget_and_notify(st: &mut State, notify: &mut NotifyBatch, uid: &NodeUid) -> bool {
|
||||
// Capture the inode before `forget` clears the uid mapping.
|
||||
let child = st.by_uid.get(uid).copied();
|
||||
let Some((parent, name)) = st.forget(uid) else {
|
||||
@@ -97,15 +97,13 @@ fn event_serializes_shared_publication(event: &DriveEvent, own_volume: &VolumeId
|
||||
/// entry forces the next `lookup`/`readdir` to re-enumerate from the remote, so
|
||||
/// most events only need to invalidate listings rather than re-fetch eagerly.
|
||||
///
|
||||
/// Applied to **every** mounted inode space, not just the primary one. This task
|
||||
/// is per-daemon while node state is per-mount, and a `DriveEvent` names a uid,
|
||||
/// not a mount — so a file trashed from another device has to be withdrawn from
|
||||
/// whichever of our sessions is showing it, which for anything under a sync
|
||||
/// folder is a [`Core::fork_state`] fork rather than `core.state`. Reaching for
|
||||
/// the primary state alone left forks serving a deleted file indefinitely, the
|
||||
/// read-side twin of `docs/BUGS.md` B74. Inode numbers are per-mount, so each
|
||||
/// mount must be notified through its **own** channel; that pairing is what
|
||||
/// [`Core::for_each_mount`] exists to preserve.
|
||||
/// Applied to the tree every mount shares, and told to **every** mounted
|
||||
/// session. A `DriveEvent` names a uid, not a mount, so a file trashed from
|
||||
/// another device has to be withdrawn from whichever of our sessions is showing
|
||||
/// it — which for anything under a sync folder is a [`Core::fork_state`] fork.
|
||||
/// Telling the primary session alone left forks serving a deleted file, the
|
||||
/// read-side twin of `docs/BUGS.md` B74. [`Core::for_each_mount`] sends what
|
||||
/// the kernel needs to hear to every session.
|
||||
fn apply_event(core: &Core, event: &DriveEvent, dirty: &mut DirtyParents) -> pdfs_core::Result<()> {
|
||||
let foreign_delete = is_foreign_node_delete(event, &core.primary_root_uid.volume_id);
|
||||
let serializes_shared_publication =
|
||||
@@ -233,6 +231,48 @@ fn acknowledge_applied_event(
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// How many applied node-update ids [`RecentEvents`] remembers. Far more than a
|
||||
/// replay can reach back: one comes from a batch whose last event was not its
|
||||
/// newest, so it repeats at most what that batch held.
|
||||
const RECENT_EVENTS: usize = 4096;
|
||||
|
||||
/// The node updates applied lately, by event id, so one that comes round again
|
||||
/// is skipped rather than applied twice.
|
||||
///
|
||||
/// The feed can deliver the same event twice in a batch, and a batch whose
|
||||
/// events come out of order leaves the cursor short of its newest event, so the
|
||||
/// next poll replays what lay past it. An update applied twice is not harmless:
|
||||
/// the first copy of our own change claims the echo [`Core::note_self_change`]
|
||||
/// recorded, and the second, unclaimed, reads as someone else's edit and evicts
|
||||
/// the bytes a partial write is about to gap-fill from (docs/BUGS.md B123).
|
||||
///
|
||||
/// Only node updates: a delete is idempotent, and a continuity loss names the
|
||||
/// cursor it lost, which is an id already applied.
|
||||
#[derive(Default)]
|
||||
struct RecentEvents {
|
||||
ids: HashSet<DriveEventId>,
|
||||
order: VecDeque<DriveEventId>,
|
||||
}
|
||||
|
||||
impl RecentEvents {
|
||||
/// Record `event` as applied, reporting whether it was already.
|
||||
fn repeat(&mut self, event: &DriveEvent) -> bool {
|
||||
let DriveEvent::NodeUpdated { id, .. } = event else {
|
||||
return false;
|
||||
};
|
||||
if !self.ids.insert(id.clone()) {
|
||||
return true;
|
||||
}
|
||||
self.order.push_back(id.clone());
|
||||
if self.order.len() > RECENT_EVENTS
|
||||
&& let Some(old) = self.order.pop_front()
|
||||
{
|
||||
self.ids.remove(&old);
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// The folders one batch of events changed, collected so the listings are
|
||||
/// dropped once at the end of the batch rather than once per event.
|
||||
///
|
||||
@@ -320,10 +360,10 @@ fn flush_dirty_parents(core: &Core, dirty: &DirtyParents) {
|
||||
/// only on fatal error.
|
||||
///
|
||||
/// Takes the whole `Core` rather than the primary mount's pieces: an event has
|
||||
/// to be applied to every mounted inode space, and the registry that enumerates
|
||||
/// them lives on the `Core` (see [`apply_event`]).
|
||||
/// to reach every mounted session, and the registry that enumerates them lives
|
||||
/// on the `Core` (see [`apply_event`]).
|
||||
pub(super) async fn run_event_sync(
|
||||
client: ProtonDriveClient,
|
||||
client: Arc<dyn DriveApi>,
|
||||
scope: DriveEventScopeId,
|
||||
core: Core,
|
||||
) {
|
||||
@@ -377,10 +417,12 @@ pub(super) async fn run_event_sync(
|
||||
};
|
||||
info!(?cursor, "event sync started");
|
||||
|
||||
let mut recent = RecentEvents::default();
|
||||
loop {
|
||||
tokio::time::sleep(POLL_INTERVAL).await;
|
||||
let events = match client.enumerate_events(&scope, cursor.as_ref()).await {
|
||||
core.next_poll(POLL_INTERVAL).await;
|
||||
let events = match link::bounded(client.enumerate_events(&scope, cursor.as_ref())).await {
|
||||
Ok(events) => events,
|
||||
Err(e) if core.lost_link(&e, "poll events") => continue,
|
||||
Err(e) => {
|
||||
warn!(error = %e, "event poll failed; retrying after interval");
|
||||
continue;
|
||||
@@ -396,6 +438,11 @@ pub(super) async fn run_event_sync(
|
||||
// did apply.
|
||||
let mut dirty = DirtyParents::default();
|
||||
for event in &events {
|
||||
if recent.repeat(event) {
|
||||
debug!(id = %event.id().as_str(), "skipping an event already applied");
|
||||
cursor = Some(event.id().clone());
|
||||
continue;
|
||||
}
|
||||
// Converge the SDK's own caches (folder keys, entity cache) on the
|
||||
// server before applying the event to our tree. Without this, a node
|
||||
// re-keyed/moved by another client keeps a stale key in the SDK for
|
||||
@@ -422,6 +469,10 @@ pub(super) async fn run_event_sync(
|
||||
cursor = Some(applied);
|
||||
}
|
||||
flush_dirty_parents(&core, &dirty);
|
||||
// Another device changed the tree: whatever a front-end lists may have
|
||||
// moved, including what is shared and what is in the trash.
|
||||
core.events
|
||||
.publish(&[Topic::Files, Topic::Trash, Topic::Shares, Topic::Conflicts]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -467,9 +518,10 @@ pub(super) async fn run_photos_event_sync(core: Core) {
|
||||
info!(?cursor, "photo event sync started");
|
||||
|
||||
loop {
|
||||
tokio::time::sleep(POLL_INTERVAL).await;
|
||||
let events = match photos.enumerate_events(&scope, cursor.as_ref()).await {
|
||||
core.next_poll(POLL_INTERVAL).await;
|
||||
let events = match link::bounded(photos.enumerate_events(&scope, cursor.as_ref())).await {
|
||||
Ok(events) => events,
|
||||
Err(error) if core.lost_link(&error, "poll photo events") => continue,
|
||||
Err(error) => {
|
||||
warn!(%error, "photo event poll failed; retrying after interval");
|
||||
continue;
|
||||
@@ -479,6 +531,7 @@ pub(super) async fn run_photos_event_sync(core: Core) {
|
||||
continue;
|
||||
};
|
||||
apply_photo_events(&core, &events);
|
||||
core.events.publish(&[Topic::Photos]);
|
||||
// One cursor write per batch, after the batch is applied: a crash in
|
||||
// between replays events whose effects are idempotent.
|
||||
if let Err(error) = core.db.set_state_str(PHOTOS_EVENT_CURSOR, head.as_str()) {
|
||||
@@ -710,6 +763,43 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_node_update_seen_again_is_skipped_but_other_events_are_not() {
|
||||
let update = |id: &str| DriveEvent::NodeUpdated {
|
||||
id: DriveEventId::from(id),
|
||||
node_uid: uid("file"),
|
||||
parent_node_uid: None,
|
||||
is_trashed: false,
|
||||
is_shared: false,
|
||||
};
|
||||
let mut recent = RecentEvents::default();
|
||||
assert!(!recent.repeat(&update("e1")));
|
||||
assert!(!recent.repeat(&update("e2")));
|
||||
assert!(recent.repeat(&update("e1")), "a duplicate or replay");
|
||||
let lost = DriveEvent::ContinuityLost {
|
||||
id: DriveEventId::from("e2"),
|
||||
};
|
||||
assert!(!recent.repeat(&lost), "names the cursor, which was applied");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_events_forget_the_oldest_past_their_bound() {
|
||||
let update = |n: usize| DriveEvent::NodeUpdated {
|
||||
id: DriveEventId::from(format!("e{n}")),
|
||||
node_uid: uid("file"),
|
||||
parent_node_uid: None,
|
||||
is_trashed: false,
|
||||
is_shared: false,
|
||||
};
|
||||
let mut recent = RecentEvents::default();
|
||||
for n in 0..=RECENT_EVENTS {
|
||||
assert!(!recent.repeat(&update(n)));
|
||||
}
|
||||
assert_eq!(recent.ids.len(), RECENT_EVENTS);
|
||||
assert!(!recent.repeat(&update(0)), "the oldest was forgotten");
|
||||
assert!(recent.repeat(&update(RECENT_EVENTS)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_delete_invalidates_an_earlier_refresh_generation() {
|
||||
let own_volume = VolumeId::from("own");
|
||||
|
||||
@@ -17,8 +17,8 @@ use std::time::{Duration, Instant};
|
||||
|
||||
use pdfs_core::config::AppDirs;
|
||||
use pdfs_core::control::{
|
||||
ActivityKind, PhotoMonth, RefreshScope, Request as CtlRequest, Response as CtlResponse,
|
||||
TransferDirection, TrashFailure,
|
||||
ActivityKind, Event, HEARTBEAT, PhotoMonth, RefreshScope, Request as CtlRequest,
|
||||
Response as CtlResponse, Topic, TransferDirection, TrashFailure,
|
||||
};
|
||||
use pdfs_core::{CoreError, CoreResult};
|
||||
use proton_drive_rs::proton_sdk::ids::NodeUid;
|
||||
@@ -163,8 +163,8 @@ fn read_request_line(reader: &mut impl BufRead) -> std::io::Result<Option<String
|
||||
/// Turn a CLI-supplied path into the mount that owns it and the path relative
|
||||
/// to *that* mount's root.
|
||||
///
|
||||
/// [`rel_to_mount`] is the older, primary-only reading, and it is what every
|
||||
/// mutating request still uses. It rejects anything outside the primary
|
||||
/// [`rel_to_mount`] is the older, primary-only reading, and it is what most
|
||||
/// requests still use. It rejects anything outside the primary
|
||||
/// mountpoint, which leaves a secondary on-demand mount — a synced folder
|
||||
/// switched out of mirror mode — with no way to be named at all: `pdfs ls` and
|
||||
/// `pdfs refresh` both answered "is not under the mountpoint", so a folder
|
||||
@@ -185,11 +185,14 @@ fn route_to_mount(core: &Core, mountpoint: &Path, path: &str) -> CoreResult<(Cor
|
||||
rel_to_mount(mountpoint, path).map(|rel| (core.clone(), rel))
|
||||
}
|
||||
|
||||
/// [`route_to_mount`] for a conflict copy. The listing names a copy inside a
|
||||
/// synced folder by its absolute local path (`docs/BUGS.md` B99), so the
|
||||
/// resolve has to run against the on-demand mount serving it. A mirror folder
|
||||
/// has no mount to run against: its copy is an ordinary local file.
|
||||
fn route_conflict(core: &Core, mountpoint: &Path, path: &str) -> CoreResult<(Core, PathBuf)> {
|
||||
/// [`route_to_mount`] for a request that changes what it names: trash, rename,
|
||||
/// create a folder, resolve a conflict. Each has to run against the on-demand
|
||||
/// mount serving the path, and `pdfs rm ~/Downloads/x` answered "is not under
|
||||
/// the mountpoint" while `pdfs move` took the same path. The conflict listing
|
||||
/// names a copy in a synced folder by its absolute local path (`docs/BUGS.md`
|
||||
/// B99), so resolving one needs this too. A mirror folder has no mount to run
|
||||
/// against: a file there is an ordinary local file.
|
||||
fn route_change(core: &Core, mountpoint: &Path, path: &str) -> CoreResult<(Core, PathBuf)> {
|
||||
let routed = route_to_mount(core, mountpoint, path).map_err(|e| {
|
||||
let p = Path::new(path);
|
||||
let mirrored = core
|
||||
@@ -200,7 +203,7 @@ fn route_conflict(core: &Core, mountpoint: &Path, path: &str) -> CoreResult<(Cor
|
||||
.any(|folder| folder.mode == "mirror" && p.starts_with(&folder.local_path));
|
||||
if mirrored {
|
||||
CoreError::invalid(format!(
|
||||
"{path} is in a mirrored folder; rename or delete the local file there"
|
||||
"{path} is in a mirrored folder; change the local file there instead"
|
||||
))
|
||||
} else {
|
||||
e
|
||||
@@ -217,6 +220,27 @@ fn route_conflict(core: &Core, mountpoint: &Path, path: &str) -> CoreResult<(Cor
|
||||
}
|
||||
}
|
||||
|
||||
/// [`route_change`] for a request that trashes or renames the node it names.
|
||||
/// A mount's root is My files or a whole synced folder. An on-demand folder's
|
||||
/// root has a parent on Drive, so nothing further down would stop `pdfs rm
|
||||
/// ~/Downloads` from trashing the folder out from under its own mount.
|
||||
fn route_node(core: &Core, mountpoint: &Path, path: &str) -> CoreResult<(Core, PathBuf)> {
|
||||
let routed = route_change(core, mountpoint, path)?;
|
||||
if below_root(&routed.1) {
|
||||
Ok(routed)
|
||||
} else {
|
||||
Err(CoreError::invalid(format!(
|
||||
"{path} is the root of a synced location and cannot be changed"
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a mount-relative path names something below the mount's root.
|
||||
fn below_root(rel: &Path) -> bool {
|
||||
rel.components()
|
||||
.any(|component| matches!(component, Component::Normal(_)))
|
||||
}
|
||||
|
||||
/// Turn a CLI-supplied path into a mountpoint-relative path. An absolute path
|
||||
/// must live under `mountpoint`; a relative path is taken as already relative to
|
||||
/// the mount root.
|
||||
@@ -286,6 +310,158 @@ impl Drop for RefreshGuard {
|
||||
}
|
||||
}
|
||||
|
||||
/// The reply to [`CtlRequest::Status`], which subscriptions are also sent
|
||||
/// whenever it changes.
|
||||
pub(crate) fn status_response(core: &Core, username: &str, mountpoint: &Path) -> CtlResponse {
|
||||
let pins = core.cache.list_pins();
|
||||
let queued = core.db.pending_op_counts().unwrap_or_default();
|
||||
// Read after the queue: the drain counts an upload as landing before it
|
||||
// retires the op, so one of the two always has it (B119).
|
||||
let landing = core.landing_uploads.load(Ordering::SeqCst);
|
||||
let staging = core.cache.staging_usage();
|
||||
CtlResponse::Status {
|
||||
parked_uploads: queued.parked.max(0) as u64,
|
||||
failing_ops: queued.failing.max(0) as u64,
|
||||
failing_error: queued.last_error.clone(),
|
||||
staged_bytes: staging.bytes,
|
||||
staged_oldest_secs: staging.oldest_secs,
|
||||
paused: core.sync_pause().is_some(),
|
||||
paused_until: core.sync_pause().flatten(),
|
||||
username: username.to_string(),
|
||||
mountpoint: mountpoint.display().to_string(),
|
||||
pinned: pins.len(),
|
||||
used: core.cache.usage(),
|
||||
budget: core.cache.budget(),
|
||||
pins,
|
||||
online: core.online.load(Ordering::Relaxed),
|
||||
pending_uploads: queued.uploads.max(0) as u64 + landing,
|
||||
pending_changes: queued.changes.max(0) as u64,
|
||||
}
|
||||
}
|
||||
|
||||
/// What a front-end shows that `request` changes when it succeeds. Requests
|
||||
/// from the CLI publish the same way as the app's, so an open window follows
|
||||
/// a change made in a terminal.
|
||||
fn touched_topics(request: &CtlRequest) -> &'static [Topic] {
|
||||
use CtlRequest as R;
|
||||
match request {
|
||||
R::Pin { .. } | R::Unpin { .. } => &[Topic::Files],
|
||||
R::CreateAlbum { .. }
|
||||
| R::RenameAlbum { .. }
|
||||
| R::DeleteAlbum { .. }
|
||||
| R::AddToAlbum { .. }
|
||||
| R::RemoveFromAlbum { .. }
|
||||
| R::SetPhotoFavorite { .. }
|
||||
| R::UploadPhoto { .. } => &[Topic::Photos],
|
||||
R::TrashNodes { .. } => &[Topic::Files, Topic::Photos, Topic::Trash, Topic::Conflicts],
|
||||
R::Rename { .. } | R::Move { .. } | R::CreateFolder { .. } | R::UploadPaths { .. } => {
|
||||
&[Topic::Files, Topic::Conflicts]
|
||||
}
|
||||
R::Delete { .. } => &[Topic::Files, Topic::Trash, Topic::Conflicts],
|
||||
R::Restore { .. } | R::DeleteForever { .. } | R::EmptyTrash => {
|
||||
&[Topic::Files, Topic::Photos, Topic::Trash, Topic::Conflicts]
|
||||
}
|
||||
R::Refresh { scope } => match scope {
|
||||
RefreshScope::Dir { .. } => &[Topic::Files],
|
||||
RefreshScope::Trash => &[Topic::Trash],
|
||||
// A photos refresh answers once it has started; the timeline
|
||||
// publishes again when it lands.
|
||||
RefreshScope::Photos { .. } => &[],
|
||||
},
|
||||
R::RenameDevice { .. } | R::DeleteDevice { .. } => &[Topic::Devices],
|
||||
R::AdoptDevice { .. } => &[Topic::Devices, Topic::Locations],
|
||||
R::AddSyncFolder { .. }
|
||||
| R::RemoveSyncFolder { .. }
|
||||
| R::SetSyncFolderMode { .. }
|
||||
| R::SetSyncFolderPaused { .. }
|
||||
| R::RestoreSyncFolders { .. }
|
||||
| R::RestoreDeviceFolders { .. } => &[Topic::Locations, Topic::Devices],
|
||||
R::RetryPendingOp { .. } => &[Topic::Queue],
|
||||
R::ResolveConflict { .. } => &[Topic::Files, Topic::Trash, Topic::Conflicts],
|
||||
R::ShareNode { .. }
|
||||
| R::ShareNodeByUid { .. }
|
||||
| R::UpdateShareRole { .. }
|
||||
| R::UpdateShareRoleByUid { .. }
|
||||
| R::RemoveShareEntry { .. }
|
||||
| R::RemoveShareEntryByUid { .. }
|
||||
| R::CreatePublicLink { .. }
|
||||
| R::CreatePublicLinkByUid { .. }
|
||||
| R::RemovePublicLink { .. }
|
||||
| R::RemovePublicLinkByUid { .. }
|
||||
| R::StopSharing { .. }
|
||||
| R::StopSharingByUid { .. } => &[Topic::Shares, Topic::Files],
|
||||
R::LeaveShared { .. }
|
||||
| R::AcceptInvitation { .. }
|
||||
| R::RejectInvitation { .. }
|
||||
| R::CreateBookmark { .. }
|
||||
| R::DeleteBookmark { .. } => &[Topic::Shares],
|
||||
R::RestoreRevision { .. }
|
||||
| R::RestoreRevisionByUid { .. }
|
||||
| R::SaveRevisionAs { .. }
|
||||
| R::SaveRevisionAsByUid { .. } => &[Topic::Files],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
|
||||
/// How long a subscription's writes may block. A client that stops reading
|
||||
/// is dropped after this rather than holding its handler forever.
|
||||
const SUBSCRIPTION_WRITE_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
/// Serve a [`CtlRequest::Subscribe`]: send the current status and transfers
|
||||
/// and say every topic changed, so the client catches up, then send whatever
|
||||
/// the [`EventHub`](super::events::EventHub) collects, with a heartbeat when
|
||||
/// nothing does. Ends when the client goes away or the daemon stops.
|
||||
fn serve_subscription(core: &Core, username: &str, mountpoint: &Path, mut stream: UnixStream) {
|
||||
let Some(mailbox) = core.events.subscribe() else {
|
||||
warn!("control: refusing a subscription at the limit");
|
||||
if let Ok(mut out) = serde_json::to_vec(&CtlResponse::error(CoreError::remote(
|
||||
"too many subscriptions",
|
||||
))) {
|
||||
out.push(b'\n');
|
||||
let _ = stream.write_all(&out);
|
||||
}
|
||||
return;
|
||||
};
|
||||
struct Unsubscribe<'a>(&'a Core, std::sync::Arc<super::events::Mailbox>);
|
||||
impl Drop for Unsubscribe<'_> {
|
||||
fn drop(&mut self) {
|
||||
self.0.events.unsubscribe(&self.1);
|
||||
}
|
||||
}
|
||||
let _unsubscribe = Unsubscribe(core, mailbox.clone());
|
||||
if let Err(e) = stream.set_write_timeout(Some(SUBSCRIPTION_WRITE_TIMEOUT)) {
|
||||
warn!(error = %e, "control: could not set subscription timeout");
|
||||
return;
|
||||
}
|
||||
let heartbeat = super::events::event_line(&Event::Heartbeat);
|
||||
let mut lines: Vec<_> = [
|
||||
super::events::event_line(&Event::Status(Box::new(status_response(
|
||||
core, username, mountpoint,
|
||||
)))),
|
||||
super::events::transfers_line(core.transfers.snapshot(), core.jobs_snapshot()),
|
||||
super::events::event_line(&Event::Changed {
|
||||
topics: Topic::ALL.to_vec(),
|
||||
}),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect();
|
||||
loop {
|
||||
for line in &lines {
|
||||
if stream.write_all(line.as_bytes()).is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if core.shutdown.is_stopping() {
|
||||
return;
|
||||
}
|
||||
lines = mailbox.take(HEARTBEAT);
|
||||
if lines.is_empty() {
|
||||
lines.extend(heartbeat.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: UnixStream) {
|
||||
if let Err(e) = stream.set_read_timeout(Some(CONTROL_READ_TIMEOUT)) {
|
||||
warn!(error = %e, "control: could not set request timeout");
|
||||
@@ -310,32 +486,21 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
return;
|
||||
}
|
||||
};
|
||||
let request = serde_json::from_str::<CtlRequest>(line.trim());
|
||||
// A subscription lasts as long as its client, so it is not a request in
|
||||
// flight and stays out of the diagnostics' list of them.
|
||||
if let Ok(CtlRequest::Subscribe) = request {
|
||||
serve_subscription(core, username, mountpoint, stream);
|
||||
return;
|
||||
}
|
||||
let touched = request.as_ref().map_or(&[][..], touched_topics);
|
||||
// Registered before the request is even parsed, so a handler that wedges
|
||||
// inside the daemon shows up by name in `pdfs diagnostics`.
|
||||
let _inflight = super::diagnostics::InflightGuard::new(super::diagnostics::request_kind(&line));
|
||||
let response = match serde_json::from_str::<CtlRequest>(line.trim()) {
|
||||
Ok(CtlRequest::Status) => {
|
||||
let pins = core.cache.list_pins();
|
||||
let queued = core.db.pending_op_counts().unwrap_or_default();
|
||||
let staging = core.cache.staging_usage();
|
||||
CtlResponse::Status {
|
||||
parked_uploads: queued.parked.max(0) as u64,
|
||||
failing_ops: queued.failing.max(0) as u64,
|
||||
failing_error: queued.last_error.clone(),
|
||||
staged_bytes: staging.bytes,
|
||||
staged_oldest_secs: staging.oldest_secs,
|
||||
paused: core.sync_pause().is_some(),
|
||||
paused_until: core.sync_pause().flatten(),
|
||||
username: username.to_string(),
|
||||
mountpoint: mountpoint.display().to_string(),
|
||||
pinned: pins.len(),
|
||||
used: core.cache.usage(),
|
||||
budget: core.cache.budget(),
|
||||
pins,
|
||||
online: core.online.load(Ordering::Relaxed),
|
||||
pending_uploads: queued.uploads.max(0) as u64,
|
||||
pending_changes: queued.changes.max(0) as u64,
|
||||
}
|
||||
let response = match request {
|
||||
Ok(CtlRequest::Status) => status_response(core, username, mountpoint),
|
||||
Ok(CtlRequest::Subscribe) => {
|
||||
CtlResponse::error(CoreError::invalid("a subscription is served on its own"))
|
||||
}
|
||||
Ok(CtlRequest::Pin { path }) => match rel_to_mount(mountpoint, &path) {
|
||||
Ok(rel) => match core.pin(&rel) {
|
||||
@@ -821,8 +986,8 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
},
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
Ok(CtlRequest::Rename { path, new_name }) => match rel_to_mount(mountpoint, &path) {
|
||||
Ok(rel) => match core.rename(&rel, &new_name) {
|
||||
Ok(CtlRequest::Rename { path, new_name }) => match route_node(core, mountpoint, &path) {
|
||||
Ok((core, rel)) => match core.rename(&rel, &new_name) {
|
||||
Ok(name) => {
|
||||
core.log_activity(ActivityKind::Rename, &name, format!("was {path}"), true);
|
||||
CtlResponse::Ok {
|
||||
@@ -836,6 +1001,24 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
},
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
// Two absolute paths may name two different locations, which only
|
||||
// `move_between` can resolve; a relative pair keeps the old meaning.
|
||||
Ok(CtlRequest::Move { path, new_parent })
|
||||
if Path::new(&path).is_absolute() && Path::new(&new_parent).is_absolute() =>
|
||||
{
|
||||
match core.move_between(Path::new(&path), Path::new(&new_parent)) {
|
||||
Ok(name) => {
|
||||
core.log_activity(ActivityKind::Move, &name, format!("to {new_parent}"), true);
|
||||
CtlResponse::Ok {
|
||||
message: format!("moved {name}"),
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
core.log_activity(ActivityKind::Move, &path, &e, false);
|
||||
CtlResponse::error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(CtlRequest::Move { path, new_parent }) => {
|
||||
match (
|
||||
rel_to_mount(mountpoint, &path),
|
||||
@@ -861,8 +1044,8 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
(Err(e), _) | (_, Err(e)) => CtlResponse::error(e),
|
||||
}
|
||||
}
|
||||
Ok(CtlRequest::Delete { path }) => match rel_to_mount(mountpoint, &path) {
|
||||
Ok(rel) => match core.delete(&rel) {
|
||||
Ok(CtlRequest::Delete { path }) => match route_node(core, mountpoint, &path) {
|
||||
Ok((core, rel)) => match core.delete(&rel) {
|
||||
Ok(name) => {
|
||||
core.log_activity(ActivityKind::Trash, &name, "", true);
|
||||
CtlResponse::Ok {
|
||||
@@ -876,21 +1059,23 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
},
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
Ok(CtlRequest::CreateFolder { parent, name }) => match rel_to_mount(mountpoint, &parent) {
|
||||
Ok(parent_rel) => match core.create_folder(&parent_rel, &name) {
|
||||
Ok(name) => {
|
||||
core.log_activity(ActivityKind::CreateFolder, &name, "", true);
|
||||
CtlResponse::Ok {
|
||||
message: format!("created folder {name}"),
|
||||
Ok(CtlRequest::CreateFolder { parent, name }) => {
|
||||
match route_change(core, mountpoint, &parent) {
|
||||
Ok((core, parent_rel)) => match core.create_folder(&parent_rel, &name) {
|
||||
Ok(name) => {
|
||||
core.log_activity(ActivityKind::CreateFolder, &name, "", true);
|
||||
CtlResponse::Ok {
|
||||
message: format!("created folder {name}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
core.log_activity(ActivityKind::CreateFolder, &name, &e, false);
|
||||
CtlResponse::error(e)
|
||||
}
|
||||
},
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
Err(e) => {
|
||||
core.log_activity(ActivityKind::CreateFolder, &name, &e, false);
|
||||
CtlResponse::error(e)
|
||||
}
|
||||
},
|
||||
Err(e) => CtlResponse::error(e),
|
||||
}
|
||||
}
|
||||
Ok(CtlRequest::UploadPaths { parent, sources }) => {
|
||||
match rel_to_mount(mountpoint, &parent) {
|
||||
// Ack immediately and upload on a background thread: a directory tree
|
||||
@@ -1207,7 +1392,7 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
Err(e) => CtlResponse::error(e),
|
||||
},
|
||||
Ok(CtlRequest::ResolveConflict { path, keep }) => {
|
||||
match route_conflict(core, mountpoint, &path) {
|
||||
match route_change(core, mountpoint, &path) {
|
||||
Ok((core, rel)) => match core.resolve_conflict(&rel, &keep) {
|
||||
Ok(message) => CtlResponse::Ok { message },
|
||||
Err(e) => CtlResponse::error(e),
|
||||
@@ -1584,6 +1769,9 @@ fn handle_control_conn(core: &Core, username: &str, mountpoint: &Path, stream: U
|
||||
// The request did not parse: the caller sent something malformed.
|
||||
Err(e) => CtlResponse::error(CoreError::invalid(format!("bad request: {e}"))),
|
||||
};
|
||||
if !touched.is_empty() && !matches!(response, CtlResponse::Error { .. }) {
|
||||
core.events.publish(touched);
|
||||
}
|
||||
let mut out = match serde_json::to_vec(&response) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
@@ -1705,6 +1893,18 @@ mod request_limit_tests {
|
||||
assert!(rel_to_mount(mountpoint, "Photos/../../escape").is_err());
|
||||
assert!(rel_to_mount(mountpoint, "/mnt/elsewhere").is_err());
|
||||
}
|
||||
|
||||
/// `pdfs rm` and `pdfs rename` reach on-demand mounts, whose root is a whole
|
||||
/// synced folder with a parent on Drive. Only a path below a root may get
|
||||
/// through to the trash or rename call.
|
||||
#[test]
|
||||
fn only_a_path_below_a_mount_root_names_a_node() {
|
||||
assert!(below_root(Path::new("Photos")));
|
||||
assert!(below_root(Path::new("./Photos/2026")));
|
||||
assert!(!below_root(Path::new("")));
|
||||
assert!(!below_root(Path::new(".")));
|
||||
assert!(!below_root(Path::new("./.")));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -27,7 +27,7 @@ use tracing::{info, warn};
|
||||
|
||||
use super::sync::{self, base_name};
|
||||
use super::{
|
||||
Core, SecondaryInsertRejection, SecondaryMount, State, SwitchBlocked, clear_stale_mount,
|
||||
Core, SecondaryInsertRejection, SecondaryMount, SwitchBlocked, clear_stale_mount,
|
||||
device_type_str, dir_is_empty, evict_dir_contents, fuse_connection_id, is_stale_mount,
|
||||
now_secs, parse_uid, spawn_session, sync_folder_info, this_hostname,
|
||||
};
|
||||
@@ -363,14 +363,14 @@ impl Core {
|
||||
) -> CoreResult<Option<NodeUid>> {
|
||||
let uids = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_folder_children_node_uids(root_uid))
|
||||
.block_on(self.drive.enumerate_folder_children_node_uids(root_uid))
|
||||
.map_err(|e| CoreError::from_api(&e, "list device root"))?;
|
||||
if uids.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let nodes = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_nodes(&uids))
|
||||
.block_on(self.drive.enumerate_nodes(&uids))
|
||||
.map_err(|e| CoreError::from_api(&e, "resolve device root children"))?;
|
||||
Ok(nodes
|
||||
.into_iter()
|
||||
@@ -446,10 +446,7 @@ impl Core {
|
||||
}
|
||||
None => self
|
||||
.rt
|
||||
.block_on(
|
||||
self.client
|
||||
.create_folder(&root_uid, &name, Some(now_secs())),
|
||||
)
|
||||
.block_on(self.drive.create_folder(&root_uid, &name, Some(now_secs())))
|
||||
.map_err(|e| CoreError::from_api(&e, &format!("create device folder {name}")))?,
|
||||
};
|
||||
|
||||
@@ -603,7 +600,7 @@ impl Core {
|
||||
&& let Some(uid) = parse_uid(&folder.remote_uid)
|
||||
&& let Err(e) = self
|
||||
.rt
|
||||
.block_on(self.client.trash_nodes(&[uid]))
|
||||
.block_on(self.drive.trash_nodes(&[uid]))
|
||||
.and_then(batch::into_unit)
|
||||
{
|
||||
warn!(id, error = %e, "delete remote device folder failed");
|
||||
@@ -619,23 +616,16 @@ impl Core {
|
||||
};
|
||||
}
|
||||
|
||||
/// A sibling Core that shares this one's client/rt/cache/db (and transfer,
|
||||
/// activity, mount registries) but gets a **fresh, empty `State`** — its own
|
||||
/// inode space starting at [`ROOT_INO`]. Used to root a secondary FUSE session
|
||||
/// at an `ondemand` sync folder without colliding with the main mount's inodes
|
||||
/// (devices.md Phase 3).
|
||||
/// A sibling Core for a secondary FUSE session rooted at an `ondemand` sync
|
||||
/// folder (devices.md Phase 3). It shares everything with this one,
|
||||
/// the tree included, except what belongs to a session: its root and its
|
||||
/// kernel channel, both filled in when the session is built.
|
||||
pub(crate) fn fork_state(&self) -> Core {
|
||||
let mut fork = self.clone();
|
||||
fork.primary = false;
|
||||
let share_access = self.state().share_access.clone();
|
||||
fork.state = Arc::new(Mutex::new(State::new(self.db.clone(), share_access, 2)));
|
||||
// A fresh inode space needs a fresh notification channel: this fork's
|
||||
// session is the only one that knows these inodes, so it must be the one
|
||||
// notified about them. Filled in by `spawn_ondemand_mount`.
|
||||
fork.root = Arc::new(OnceLock::new());
|
||||
fork.notifier = Arc::new(OnceLock::new());
|
||||
fork.session_live = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
// Size upgrades are keyed by inode, which is per-fork too.
|
||||
fork.size_upgrades = Arc::new(Mutex::new(HashMap::new()));
|
||||
fork
|
||||
}
|
||||
|
||||
@@ -858,7 +848,7 @@ impl Core {
|
||||
})?;
|
||||
let root = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_nodes(std::slice::from_ref(&root_uid)))
|
||||
.block_on(self.drive.enumerate_nodes(std::slice::from_ref(&root_uid)))
|
||||
.map_err(|e| SwitchBlocked::Failed(format!("fetch remote root: {e}")))?
|
||||
.into_iter()
|
||||
.next()
|
||||
@@ -937,8 +927,8 @@ impl Core {
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn a secondary FUSE session rooted at `root` over `local` on a forked
|
||||
/// inode space. Clears any stale kernel mount first (a crashed run can leave
|
||||
/// Spawn a secondary FUSE session rooted at `root` over `local` from a
|
||||
/// [`Core::fork_state`] sibling. Clears any stale kernel mount first (a crashed run can leave
|
||||
/// one, which would fail the fresh mount with EBUSY).
|
||||
pub(crate) fn spawn_ondemand_mount(
|
||||
&self,
|
||||
@@ -1017,7 +1007,7 @@ impl Core {
|
||||
};
|
||||
let root = match self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_nodes(std::slice::from_ref(&root_uid)))
|
||||
.block_on(self.drive.enumerate_nodes(std::slice::from_ref(&root_uid)))
|
||||
{
|
||||
Ok(v) => match v.into_iter().next() {
|
||||
Some(n) => n,
|
||||
|
||||
@@ -24,28 +24,27 @@ use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use parking_lot::Mutex;
|
||||
|
||||
use futures::StreamExt;
|
||||
use pdfs_core::batch;
|
||||
use pdfs_core::cache::{Baseline, StagedWrite};
|
||||
use pdfs_core::control::{ActivityKind, TransferDirection};
|
||||
use pdfs_core::db::{
|
||||
OP_CREATE, OP_MKDIR, OP_RENAME, OP_REVISION, OP_TRASH, PARK_EXPIRY_MS, PendingOp, RenameMeta,
|
||||
CreateLanding, OP_CREATE, OP_MKDIR, OP_RENAME, OP_REVISION, OP_TRASH, PARK_EXPIRY_MS,
|
||||
PendingOp, RenameMeta,
|
||||
};
|
||||
use proton_drive_rs::proton_sdk::error::ProtonError;
|
||||
use proton_drive_rs::proton_sdk::ids::NodeUid;
|
||||
use proton_drive_rs::{Node, NodeMoveItem};
|
||||
use proton_drive_rs::{Node, NodeKind};
|
||||
use tracing::{debug, error, info, warn};
|
||||
|
||||
use super::state::Intervals;
|
||||
use super::state::{Intervals, PendingRevision};
|
||||
use super::transfers::CountingReader;
|
||||
use super::{
|
||||
Core, DRAIN_BACKOFF_MAX, DRAIN_BACKOFF_MIN, DRAIN_IDLE_POLL, DRAIN_REVISION_DEBOUNCE,
|
||||
DRAIN_REVISION_DEBOUNCE_MAX, ROOT_INO, UPLOAD_TIME_MEMORY, WriteAuthority, conflict_name,
|
||||
DRAIN_REVISION_DEBOUNCE_MAX, UPLOAD_TIME_MEMORY, WriteAuthority, conflict_name,
|
||||
is_already_exists, is_gone, is_local_uid_str, media_type_for, node_revision_id, node_size,
|
||||
now_millis, now_secs, parse_node_uid,
|
||||
};
|
||||
@@ -113,6 +112,23 @@ impl Drop for UploadCancel {
|
||||
}
|
||||
}
|
||||
|
||||
/// One upload counted in [`Core::landing_uploads`] from its op's retirement
|
||||
/// until its bytes are cached, and uncounted when dropped, early return or not.
|
||||
struct LandingUpload(Arc<AtomicU64>);
|
||||
|
||||
impl LandingUpload {
|
||||
fn begin(count: &Arc<AtomicU64>) -> Self {
|
||||
count.fetch_add(1, Ordering::SeqCst);
|
||||
Self(count.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LandingUpload {
|
||||
fn drop(&mut self) {
|
||||
self.0.fetch_sub(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
/// The debounce arithmetic of [`Core::revision_debounce`], without the map:
|
||||
/// the last measured upload time for this node, bounded on both sides.
|
||||
///
|
||||
@@ -659,6 +675,15 @@ impl Core {
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
if let Some(meta) = op.meta_json.as_deref() {
|
||||
let meta: RenameMeta = serde_json::from_str(meta)?;
|
||||
if moved_elsewhere(&meta, &node, &parent, &name) {
|
||||
// Someone moved or renamed it while ours was queued. Ours still
|
||||
// wins, as the last change does on Drive.
|
||||
info!(%uid, from = %node.name, to = %name,
|
||||
"node moved remotely while its rename was queued");
|
||||
}
|
||||
}
|
||||
let landed = if node.parent_uid.as_ref() == Some(&parent) {
|
||||
self.drain_rename_in_place(&uid, &node.name, &name)?
|
||||
} else {
|
||||
@@ -667,15 +692,18 @@ impl Core {
|
||||
// no window for the requirements to go stale between two calls —
|
||||
// the race B46 queued this op to get away from.
|
||||
let target = (node.name != name).then_some(name.as_str());
|
||||
match self.move_renaming(&uid, &parent, target) {
|
||||
match self.move_rename_remote(&uid, &parent, target) {
|
||||
Ok(()) => name.clone(),
|
||||
// The destination holds that name already. Landing under a
|
||||
// *different* name is the non-destructive resolution: it neither
|
||||
// clobbers their file nor drops ours, and it is visible.
|
||||
Err(e) if is_already_exists(&e) => {
|
||||
if self.db.has_pending_trash_named(&name)? {
|
||||
return Err(held_by_queued_trash(&name));
|
||||
}
|
||||
let alt = conflict_name(&name, now_secs());
|
||||
warn!(%uid, name, alt, "destination already holds that name; using a conflict name");
|
||||
self.move_renaming(&uid, &parent, Some(&alt))?;
|
||||
self.move_rename_remote(&uid, &parent, Some(&alt))?;
|
||||
self.adopt_drained_name(&uid, &alt);
|
||||
self.log_activity(
|
||||
ActivityKind::Rename,
|
||||
@@ -696,8 +724,8 @@ impl Core {
|
||||
let landed = match self.drain_rename_in_place(&uid, &node.name, &name) {
|
||||
Ok(landed) => landed,
|
||||
// The node went with it; nothing is left to rename.
|
||||
Err(e) if is_gone(&e) => node.name.clone(),
|
||||
Err(e) => return Err(e.into()),
|
||||
Err(e) if is_gone(e.as_ref()) => node.name.clone(),
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
self.log_activity(
|
||||
ActivityKind::Rename,
|
||||
@@ -712,7 +740,6 @@ impl Core {
|
||||
}
|
||||
};
|
||||
self.db.delete_op(op.id)?;
|
||||
self.note_self_change(&uid);
|
||||
info!(%uid, name = %landed, "pending rename landed");
|
||||
Ok(())
|
||||
}
|
||||
@@ -725,16 +752,19 @@ impl Core {
|
||||
uid: &NodeUid,
|
||||
current: &str,
|
||||
name: &str,
|
||||
) -> Result<String, ProtonError> {
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
if current == name {
|
||||
return Ok(current.to_string());
|
||||
}
|
||||
match self.rt.block_on(self.client.rename_node(uid, name, None)) {
|
||||
match self.rename_remote(uid, name) {
|
||||
Ok(()) => Ok(name.to_string()),
|
||||
Err(e) if is_already_exists(&e) => {
|
||||
if self.db.has_pending_trash_named(name)? {
|
||||
return Err(held_by_queued_trash(name));
|
||||
}
|
||||
let alt = conflict_name(name, now_secs());
|
||||
warn!(%uid, name, alt, "rename target name is taken; using a conflict name");
|
||||
self.rt.block_on(self.client.rename_node(uid, &alt, None))?;
|
||||
self.rename_remote(uid, &alt)?;
|
||||
self.adopt_drained_name(uid, &alt);
|
||||
self.log_activity(
|
||||
ActivityKind::Rename,
|
||||
@@ -744,35 +774,10 @@ impl Core {
|
||||
);
|
||||
Ok(alt)
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Move `uid` under `parent` in one request, renaming it to `target_name` on
|
||||
/// the way when one is given.
|
||||
fn move_renaming(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
parent: &NodeUid,
|
||||
target_name: Option<&str>,
|
||||
) -> Result<(), ProtonError> {
|
||||
let item = NodeMoveItem {
|
||||
uid: uid.clone(),
|
||||
target_name: target_name.map(str::to_string),
|
||||
};
|
||||
self.rt.block_on(async {
|
||||
let mut outcomes =
|
||||
std::pin::pin!(self.client.move_nodes_streaming(vec![item], parent.clone()));
|
||||
match outcomes.next().await {
|
||||
Some(Ok((_, outcome))) => outcome,
|
||||
Some(Err(e)) => Err(e),
|
||||
None => Err(ProtonError::invalid_operation(format!(
|
||||
"move of {uid} reported no outcome"
|
||||
))),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Apply a queued trash to the remote.
|
||||
///
|
||||
/// A node that is already gone is a success, not a failure: the outcome the
|
||||
@@ -783,7 +788,7 @@ impl Core {
|
||||
let name = op.name.clone().unwrap_or_else(|| op.uid.clone());
|
||||
match self
|
||||
.rt
|
||||
.block_on(self.client.trash_nodes(std::slice::from_ref(&uid)))
|
||||
.block_on(self.drive.trash_nodes(std::slice::from_ref(&uid)))
|
||||
.and_then(batch::into_unit)
|
||||
{
|
||||
Ok(()) => {}
|
||||
@@ -793,7 +798,6 @@ impl Core {
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
self.db.complete_trash_op(op.id, &uid)?;
|
||||
self.own_sealed_revs.lock().remove(&uid);
|
||||
if let Err(e) = self.db.clear_own_sealed_rev(&uid.to_string()) {
|
||||
debug!(%uid, error = %e, "clearing the sealed-revision record failed");
|
||||
}
|
||||
@@ -842,8 +846,27 @@ impl Core {
|
||||
// reachable only because the op waited, which is the whole point of the
|
||||
// queue. Never overwrite theirs and never drop ours: land under a
|
||||
// conflict name, exactly as the sync engine does.
|
||||
//
|
||||
// Two things can hold the name that are not someone else's file. The
|
||||
// node a create made before the network dropped its answer is ours: the
|
||||
// create was queued because nobody heard back, so adopt it. And a node
|
||||
// whose trash is still queued is about to let go of it.
|
||||
let mut name = wanted.clone();
|
||||
let mut home = parent.clone();
|
||||
let mut adopted = false;
|
||||
let mut real = self.create_drained_node(op, &parent, &name);
|
||||
if real.as_ref().is_err_and(|e| is_already_exists(e.as_ref()))
|
||||
&& let Some(twin) = self.adoptable_twin(op, &parent, &wanted)?
|
||||
{
|
||||
info!(%local, %twin, wanted, "the name is held by our own unanswered create; adopting it");
|
||||
real = Ok(twin);
|
||||
adopted = true;
|
||||
}
|
||||
if real.as_ref().is_err_and(|e| is_already_exists(e.as_ref()))
|
||||
&& self.db.has_pending_trash_named(&wanted)?
|
||||
{
|
||||
return Err(held_by_queued_trash(&wanted));
|
||||
}
|
||||
if real.as_ref().is_err_and(|e| is_already_exists(e.as_ref())) {
|
||||
name = conflict_name(&wanted, now_secs());
|
||||
warn!(%local, wanted, name, "name is taken remotely; creating under a conflict name");
|
||||
@@ -872,6 +895,7 @@ impl Core {
|
||||
name = conflict_name(&wanted, now_secs());
|
||||
real = self.create_drained_node(op, &root, &name);
|
||||
}
|
||||
home = root;
|
||||
if real.is_ok() {
|
||||
self.log_activity(
|
||||
ActivityKind::Upload,
|
||||
@@ -886,20 +910,84 @@ impl Core {
|
||||
// Retire the op before touching anything else: if we crash here the node
|
||||
// exists remotely and the local placeholder is reconciled by the event
|
||||
// sync, whereas a surviving op would create the file a second time.
|
||||
self.db.delete_op(op.id)?;
|
||||
self.adopt_real_uid(&local, &real)?;
|
||||
// An adopted node was made empty, so a blob this op carries has not been
|
||||
// uploaded: it goes on as a revision of the node instead.
|
||||
let uploaded = (!adopted).then_some(op.blob_path.as_deref()).flatten();
|
||||
let landing = CreateLanding {
|
||||
local: &local.to_string(),
|
||||
real: &real.to_string(),
|
||||
sent: (parent_str, &wanted),
|
||||
landed: (&home.to_string(), &name),
|
||||
};
|
||||
let newer = self.retire_create(op, uploaded, &landing, &local, &real)?;
|
||||
// The uploaded blob is the new file's content, so it becomes the cached
|
||||
// content, as for a revision. A partial write fills its gaps from it;
|
||||
// without it the next write open was refused (`docs/BUGS.md` B123).
|
||||
let landed: Option<StagedWrite> = uploaded
|
||||
.filter(|_| !newer)
|
||||
.and(op.meta_json.as_deref())
|
||||
.and_then(|json| serde_json::from_str(json).ok());
|
||||
self.adopt_real_uid(&local, &real, |node| {
|
||||
if let (Some(blob), Some(meta)) = (uploaded, &landed)
|
||||
&& node_size(node) == meta.len
|
||||
&& keeps_landed_upload(self.cache.is_pinned(&real), meta.len, self.cache.budget())
|
||||
{
|
||||
let _ =
|
||||
self.cache
|
||||
.store_file(&real, node.modification_time, meta.len, Path::new(blob));
|
||||
}
|
||||
})?;
|
||||
// The feed will report this create back to us; the tree already has it
|
||||
// under its real uid, so that event is ours to ignore (`Core::self_changes`).
|
||||
self.note_self_change(&real);
|
||||
if let Some(blob) = op.blob_path.as_deref() {
|
||||
// The uploaded blob is done with either way: a write that replaced it
|
||||
// while it was on the wire already discarded it when it attached.
|
||||
if let Some(blob) = uploaded {
|
||||
self.cache.discard_staged(Path::new(blob));
|
||||
}
|
||||
self.pending.lock().remove(&local);
|
||||
if newer {
|
||||
info!(%local, %real, name, "a write landed during the create; queued it as a revision");
|
||||
self.wake_drain();
|
||||
}
|
||||
self.log_activity(ActivityKind::Upload, &name, "created", true);
|
||||
info!(%local, %real, name, kind = %op.kind, "pending create landed");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The node already holding `name` under `parent`, if it is the one an
|
||||
/// earlier attempt at this op made before its answer was lost.
|
||||
///
|
||||
/// A create that timed out may still have landed; the op was queued because
|
||||
/// the caller never heard back. Without this, its replay forked a
|
||||
/// `(sync-conflict)` copy of a file nobody else had touched.
|
||||
fn adoptable_twin(
|
||||
&self,
|
||||
op: &PendingOp,
|
||||
parent: &NodeUid,
|
||||
name: &str,
|
||||
) -> Result<Option<NodeUid>, Box<dyn std::error::Error>> {
|
||||
let uids = self
|
||||
.rt
|
||||
.block_on(self.drive.enumerate_folder_children_node_uids(parent))?;
|
||||
let children = self.rt.block_on(self.drive.enumerate_nodes_light(&uids))?;
|
||||
let Some(twin) = children
|
||||
.into_iter()
|
||||
.find(|node| node.name == name && !node.trashed)
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
// The light listing carries no file size, and an adoptable file is
|
||||
// exactly an empty one.
|
||||
let twin = match twin.kind {
|
||||
NodeKind::Folder => twin,
|
||||
NodeKind::File { .. } => match self.fetch_node_remote(&twin.uid)? {
|
||||
Some(node) => node,
|
||||
None => return Ok(None),
|
||||
},
|
||||
};
|
||||
Ok(adoptable(op.kind == OP_MKDIR, &twin, op.created_at).then_some(twin.uid))
|
||||
}
|
||||
|
||||
/// Make one queued `create`/`mkdir` real under a given name, and hand back
|
||||
/// the API's own error so the caller can tell a name clash from a failure.
|
||||
///
|
||||
@@ -915,7 +1003,7 @@ impl Core {
|
||||
true => {
|
||||
Ok(self
|
||||
.rt
|
||||
.block_on(self.client.create_folder(parent, name, Some(now_secs())))?)
|
||||
.block_on(self.drive.create_folder(parent, name, Some(now_secs())))?)
|
||||
}
|
||||
false => self.upload_created_file(op, parent, name),
|
||||
}
|
||||
@@ -930,7 +1018,7 @@ impl Core {
|
||||
name: &str,
|
||||
) -> Result<NodeUid, Box<dyn std::error::Error>> {
|
||||
let Some(blob) = op.blob_path.as_deref() else {
|
||||
return Ok(self.rt.block_on(self.client.upload_file(
|
||||
return Ok(self.rt.block_on(self.drive.upload_file(
|
||||
parent,
|
||||
name,
|
||||
media_type_for(name),
|
||||
@@ -950,7 +1038,7 @@ impl Core {
|
||||
.transfers
|
||||
.begin(name, op.uid.clone(), TransferDirection::Upload, meta.len);
|
||||
let reader = CountingReader::new(File::open(blob)?, &guard);
|
||||
let uid = self.rt.block_on(self.client.upload_file_from(
|
||||
let uid = self.rt.block_on(self.drive.upload_file_from(
|
||||
parent,
|
||||
name,
|
||||
media_type_for(name),
|
||||
@@ -963,39 +1051,155 @@ impl Core {
|
||||
Ok(uid)
|
||||
}
|
||||
|
||||
/// Retire a landed create's op and move its queued state to the real uid.
|
||||
/// True when a newer write attached to the create while it was uploading,
|
||||
/// which is now queued as a revision of `real`.
|
||||
///
|
||||
/// Runs under the `pending` lock, which `Core::enqueue_staged_write` also
|
||||
/// holds from attaching a blob to a create until it records the pending
|
||||
/// entry. So a write either attached before this ran and is carried over,
|
||||
/// or finds the create gone and queues against the real uid itself. The
|
||||
/// open write handles are repointed right after, for the same reason:
|
||||
/// a handle released later must not queue against the placeholder.
|
||||
fn retire_create(
|
||||
&self,
|
||||
op: &PendingOp,
|
||||
uploaded: Option<&str>,
|
||||
landing: &CreateLanding<'_>,
|
||||
local: &NodeUid,
|
||||
real: &NodeUid,
|
||||
) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
let newer = {
|
||||
let mut pending = self.pending.lock();
|
||||
let newer = self.db.finish_create(op.id, uploaded, landing, |json| {
|
||||
let mut meta: StagedWrite = serde_json::from_str(json).ok()?;
|
||||
meta.uid = landing.real.to_string();
|
||||
serde_json::to_string(&meta).ok()
|
||||
})?;
|
||||
pending.remove(local);
|
||||
if let Some((blob, json)) = &newer {
|
||||
let meta: StagedWrite = serde_json::from_str(json)?;
|
||||
pending.insert(
|
||||
real.clone(),
|
||||
PendingRevision {
|
||||
path: PathBuf::from(blob),
|
||||
meta,
|
||||
},
|
||||
);
|
||||
}
|
||||
newer.is_some()
|
||||
};
|
||||
if !newer && let Some(blob) = uploaded {
|
||||
self.cache_created(op, local, real, blob);
|
||||
}
|
||||
self.for_each_state(|st| {
|
||||
if let Some(ino) = st.by_uid.remove(local) {
|
||||
st.by_uid.insert(real.clone(), ino);
|
||||
if let Some(e) = st.entries.get_mut(&ino) {
|
||||
e.uid = real.clone();
|
||||
}
|
||||
// Its children still name the placeholder as their parent, and
|
||||
// the next write of one would put it back on the row.
|
||||
for kid in st.children.get(&ino).cloned().unwrap_or_default() {
|
||||
if let Some(e) = st.entries.get_mut(&kid)
|
||||
&& e.node.parent_uid.as_ref() == Some(local)
|
||||
{
|
||||
e.node.parent_uid = Some(real.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
for aw in st.active_writes.values_mut() {
|
||||
if aw.uid == *local {
|
||||
aw.uid = real.clone();
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(newer)
|
||||
}
|
||||
|
||||
/// Cache a landed create's uploaded `blob` under `real`, keyed as the tree
|
||||
/// has the file.
|
||||
///
|
||||
/// A write handle open on the file is repointed at `real` right after this,
|
||||
/// and a release fills its gaps from the cache by the size and mtime it
|
||||
/// opened over. Drive lists the new node a moment after the create, so
|
||||
/// `adopt_real_uid` caches the bytes only once it can read the node back.
|
||||
/// A release in between found nothing to fill from, and the next write open
|
||||
/// was refused (docs/BUGS.md B131).
|
||||
fn cache_created(&self, op: &PendingOp, local: &NodeUid, real: &NodeUid, blob: &str) {
|
||||
let Some(meta) = op
|
||||
.meta_json
|
||||
.as_deref()
|
||||
.and_then(|json| serde_json::from_str::<StagedWrite>(json).ok())
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let mut mtime = None;
|
||||
self.for_each_state(|st| {
|
||||
if let Some(entry) = st.by_uid.get(local).and_then(|ino| st.entries.get(ino)) {
|
||||
mtime = Some(entry.node.modification_time);
|
||||
}
|
||||
});
|
||||
if let Some(mtime) = mtime
|
||||
&& keeps_landed_upload(self.cache.is_pinned(real), meta.len, self.cache.budget())
|
||||
{
|
||||
let _ = self
|
||||
.cache
|
||||
.store_file(real, mtime, meta.len, Path::new(blob));
|
||||
}
|
||||
}
|
||||
|
||||
/// Swap a placeholder uid for the real one across everything that keyed off
|
||||
/// it: queued children, the DB, the in-memory tree, and the caches.
|
||||
///
|
||||
/// The inode is deliberately kept, so anything already holding the file open
|
||||
/// keeps working across the drain.
|
||||
///
|
||||
/// Applied to **every** live inode space, not just this Core's. A node
|
||||
/// created inside an on-demand sync folder lives in that fork's `state`,
|
||||
/// while the drain that lands its create belongs to the primary mount — so
|
||||
/// reaching for `self.state` here found nothing and left the fork's entry on
|
||||
/// its `local~` uid, which reads as an empty file for as long as the daemon
|
||||
/// runs (`docs/BUGS.md` B74). A uid is unique across mounts, so at most one
|
||||
/// of them matches and the others are no-ops.
|
||||
/// Applied to the tree every mount shares. A node created inside an
|
||||
/// on-demand sync folder once lived in that fork's own state, where the
|
||||
/// primary mount's drain never looked, and stayed on its `local~` uid —
|
||||
/// an empty file for as long as the daemon ran (`docs/BUGS.md` B74).
|
||||
///
|
||||
/// `adopt` sees the node as Drive made it, before anything is rebased onto
|
||||
/// it.
|
||||
pub(crate) fn adopt_real_uid(
|
||||
&self,
|
||||
local: &NodeUid,
|
||||
real: &NodeUid,
|
||||
adopt: impl FnOnce(&Node),
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let node = self
|
||||
.fetch_node(real)
|
||||
// Drive answers a create before it lists the new node.
|
||||
let mut node = self
|
||||
.read_back(real)
|
||||
.map_err(|e| self.errno_error(e, "fetch node"))?;
|
||||
// Repoints queued children and node rows, and drops the placeholder row.
|
||||
// A rename made while the create was on the wire is queued against the
|
||||
// real uid (`Db::finish_create`). Until it lands, the tree keeps the
|
||||
// name and folder the user gave it.
|
||||
let renaming = self.db.has_pending_op(&real.to_string(), OP_RENAME)?;
|
||||
// Before open handles are rebased onto the node, so one released in
|
||||
// between finds what `adopt` keeps (`docs/BUGS.md` B123).
|
||||
adopt(&node);
|
||||
// Repoints queued children and node rows, and readdresses the placeholder row.
|
||||
// A no-op after `retire_create`, which has already done both.
|
||||
self.db
|
||||
.remap_local_uid(&local.to_string(), &real.to_string())?;
|
||||
|
||||
self.for_each_state(|st| {
|
||||
let Some(ino) = st.by_uid.remove(local) else {
|
||||
if let Some(ino) = st.by_uid.remove(local) {
|
||||
st.by_uid.insert(real.clone(), ino);
|
||||
}
|
||||
let Some(&ino) = st.by_uid.get(real) else {
|
||||
return;
|
||||
};
|
||||
st.by_uid.insert(real.clone(), ino);
|
||||
if renaming && let Some(e) = st.entries.get(&ino) {
|
||||
node.name = e.node.name.clone();
|
||||
node.parent_uid = e.node.parent_uid.clone();
|
||||
}
|
||||
let mut landed = node.clone();
|
||||
st.keep_open_write_size(ino, &mut landed);
|
||||
if let Some(e) = st.entries.get_mut(&ino) {
|
||||
e.uid = real.clone();
|
||||
e.node = node.clone();
|
||||
e.node = landed;
|
||||
}
|
||||
// Where the op said the node goes and where it actually landed can
|
||||
// differ — a conflict re-homes it — so the tree follows the parent
|
||||
@@ -1024,6 +1228,15 @@ impl Core {
|
||||
if let Err(e) = self.db.upsert_node(&node) {
|
||||
warn!(%real, error = %e, "db upsert_node failed after remap");
|
||||
}
|
||||
// A handle still open on the placeholder was based on no revision at
|
||||
// all: a local-clock mtime and no revision id. Released as it is, its
|
||||
// write would land as a conflict copy of the empty file this create just
|
||||
// made (`docs/BUGS.md` B113). Rebased before the restamp below, so a
|
||||
// release that races this is caught there.
|
||||
self.rebase_open_writes(real, &node);
|
||||
// A write carried over from the create was made against no revision at
|
||||
// all; the one just created is what it now replaces.
|
||||
self.rebaseline_pending(real, &node);
|
||||
if node.is_folder() {
|
||||
// It was recorded as listed while local (it was empty and had nothing
|
||||
// to enumerate). That still holds: its queued children re-intern under
|
||||
@@ -1072,16 +1285,11 @@ impl Core {
|
||||
// instead of forking — so the finished bytes win the name rather than
|
||||
// being exiled to a `(sync-conflict)` copy.
|
||||
let remote_rev = node_revision_id(&node);
|
||||
// In memory first, then the table it is written through to: the map is
|
||||
// this process's cache of it, and after a restart only the table has
|
||||
// the answer (which is the whole reason it is persisted).
|
||||
let own_rev = match self.own_sealed_revs.lock().get(uid).cloned() {
|
||||
Some(rev) => Some(rev),
|
||||
None => self
|
||||
.db
|
||||
.own_sealed_rev(&uid.to_string(), now_millis())
|
||||
.unwrap_or_default(),
|
||||
};
|
||||
// The table, so the answer survives a restart (`own_sealed_rev`).
|
||||
let own_rev = self
|
||||
.db
|
||||
.own_sealed_rev(&uid.to_string(), now_millis())
|
||||
.unwrap_or_default();
|
||||
if is_own_self_supersede(meta.complete, remote_rev.as_deref(), own_rev.as_deref()) {
|
||||
debug!(%uid, ?remote_rev,
|
||||
"queued write chains onto our own sealed revision; not a conflict");
|
||||
@@ -1151,7 +1359,7 @@ impl Core {
|
||||
self.transfers
|
||||
.begin(&alt, meta.uid.clone(), TransferDirection::Upload, meta.len);
|
||||
let reader = CountingReader::new(File::open(blob)?, &guard);
|
||||
self.rt.block_on(self.client.upload_file_from(
|
||||
self.rt.block_on(self.drive.upload_file_from(
|
||||
&parent,
|
||||
&alt,
|
||||
media_type_for(&alt),
|
||||
@@ -1184,6 +1392,10 @@ impl Core {
|
||||
format!("{reason}; local changes uploaded as {alt}"),
|
||||
false,
|
||||
);
|
||||
self.events.publish(&[
|
||||
pdfs_core::control::Topic::Files,
|
||||
pdfs_core::control::Topic::Conflicts,
|
||||
]);
|
||||
info!(%uid, name, alt, "queued write landed as a conflict copy");
|
||||
Ok(())
|
||||
}
|
||||
@@ -1344,25 +1556,13 @@ impl Core {
|
||||
|
||||
/// A node's parent uid and name, as the tree currently has them.
|
||||
pub(crate) fn node_place(&self, uid: &NodeUid) -> Option<(NodeUid, String)> {
|
||||
// Across every mount: the drain runs on the primary Core, but a node
|
||||
// written inside a sync folder is interned in that fork's inode space,
|
||||
// so asking `self.state` alone always missed it and fell through to the
|
||||
// DB. That still gave the right answer — the fallback below exists for
|
||||
// exactly this shape of miss — but it went to disk for something already
|
||||
// in memory, and only *because* of the fallback was it not a bug.
|
||||
let mut found = None;
|
||||
self.for_each_state(|st| {
|
||||
if found.is_some() {
|
||||
return;
|
||||
}
|
||||
{
|
||||
let st = self.state();
|
||||
if let Some(entry) = st.by_uid.get(uid).and_then(|ino| st.entries.get(ino))
|
||||
&& let Some(parent) = entry.node.parent_uid.clone()
|
||||
{
|
||||
found = Some((parent, entry.node.name.clone()));
|
||||
return Some((parent, entry.node.name.clone()));
|
||||
}
|
||||
});
|
||||
if found.is_some() {
|
||||
return found;
|
||||
}
|
||||
// The in-memory tree only holds what has been walked to since the daemon
|
||||
// started, and the drain routinely runs before anything has walked to
|
||||
@@ -1397,12 +1597,13 @@ impl Core {
|
||||
/// root yet, which is not where the drain runs; the drain must not panic
|
||||
/// over it regardless, since that would stop the queue for good.
|
||||
pub(crate) fn root_uid(&self) -> Option<NodeUid> {
|
||||
let st = self.state();
|
||||
st.entries.get(&ROOT_INO).map(|e| e.uid.clone())
|
||||
let root = self.root_ino();
|
||||
self.state().entries.get(&root).map(|e| e.uid.clone())
|
||||
}
|
||||
|
||||
/// Upload a staged revision of a file the server already knows about.
|
||||
pub(crate) fn drain_revision(&self, op: &PendingOp) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let op_started = Instant::now();
|
||||
let blob = op
|
||||
.blob_path
|
||||
.clone()
|
||||
@@ -1412,6 +1613,16 @@ impl Core {
|
||||
let uid = parse_node_uid(&meta.uid).ok_or("staged write has an unparseable uid")?;
|
||||
|
||||
if let Some(reason) = self.revision_conflict(&uid, &meta)? {
|
||||
// Trashing the file from the mount drops its queued write, but not
|
||||
// from under a worker already holding it. That worker then finds the
|
||||
// node trashed — by us — and would upload the withdrawn bytes as a
|
||||
// conflict copy (a `recovered-…` file in the root when the tree has
|
||||
// already forgotten the node). A write nobody queues any more is
|
||||
// not a conflict.
|
||||
if !self.db.op_exists(op.id)? {
|
||||
debug!(%uid, reason, "queued write withdrawn while it drained; dropping it");
|
||||
return Ok(());
|
||||
}
|
||||
return self.keep_as_conflict_copy(op, &blob, &meta, &uid, &reason);
|
||||
}
|
||||
|
||||
@@ -1448,9 +1659,15 @@ impl Core {
|
||||
// this is on the wire can stop it rather than paying for a revision the
|
||||
// op it queued will immediately replace.
|
||||
let cancel = self.begin_cancellable_upload(&uid);
|
||||
// An unlink that withdrew this op before the flag above existed had no
|
||||
// upload to cancel; the row is the record that it happened.
|
||||
if !self.db.op_exists(op.id)? {
|
||||
debug!(%uid, "queued write withdrawn before it went out; dropping it");
|
||||
return Ok(());
|
||||
}
|
||||
let reader = CountingReader::new(File::open(&blob)?, &guard).with_cancel(cancel.flag());
|
||||
let started = Instant::now();
|
||||
let sent = self.rt.block_on(self.client.upload_new_revision_from(
|
||||
let sent = self.rt.block_on(self.drive.upload_new_revision_from(
|
||||
&uid,
|
||||
reader,
|
||||
meta.len as i64,
|
||||
@@ -1476,25 +1693,46 @@ impl Core {
|
||||
// is what the next write's debounce is scaled from.
|
||||
self.record_upload_time(&uid, took);
|
||||
|
||||
// Still pending to whoever asks until the bytes are cached below, and
|
||||
// counted before the op goes so the status never reads empty in between.
|
||||
// An empty queue is how a script or the app knows a written file reads
|
||||
// without the network; retiring the op alone left the read-back's round
|
||||
// trip in which it did not (B119). The op itself has to go first: one
|
||||
// restored after a crash would re-send a revision this daemon has not
|
||||
// yet recorded as its own, and come back as a conflict copy.
|
||||
let _landing = LandingUpload::begin(&self.landing_uploads);
|
||||
// Retire the op before dropping the blob: a crash between the two leaves
|
||||
// an orphaned file (harmless), whereas the reverse would leave a queued
|
||||
// op pointing at nothing.
|
||||
self.db.delete_op(op.id)?;
|
||||
self.release_pending(&uid, &blob);
|
||||
|
||||
// The staged blob now matches the sealed revision, so a pinned file keeps
|
||||
// it as its cached content rather than re-downloading what we just sent.
|
||||
// Adopted by link, not by value: the blob is the whole file, and a pinned
|
||||
// video read into a `Vec` here is an OOM. `discard_staged` below drops the
|
||||
// staging name only — the cache keeps the inode.
|
||||
if self.cache.is_pinned(&uid) {
|
||||
let _ = self.cache.store_file(&uid, now_secs(), meta.len, &blob);
|
||||
}
|
||||
self.cache.discard_staged(&blob);
|
||||
self.evict_reader(&uid);
|
||||
self.refresh_after_upload(&uid);
|
||||
// The staged blob now matches the sealed revision, so it becomes the
|
||||
// file's cached content, stamped like the revision just read back.
|
||||
// Reading the file again then needs no network, which an editor or a
|
||||
// build does at once and a flaky link cannot always give (`docs/BUGS.md`
|
||||
// B119). Adopted by link, not by value: the blob is the whole file, and
|
||||
// a video read into a `Vec` here is an OOM. `discard_staged` below drops
|
||||
// the staging name only — the cache keeps the inode.
|
||||
self.refresh_after_upload(&uid, &blob, |node| {
|
||||
if node_size(node) == meta.len
|
||||
&& keeps_landed_upload(self.cache.is_pinned(&uid), meta.len, self.cache.budget())
|
||||
{
|
||||
let _ = self
|
||||
.cache
|
||||
.store_file(&uid, node.modification_time, meta.len, &blob);
|
||||
}
|
||||
});
|
||||
self.cache.discard_staged(&blob);
|
||||
self.log_activity(ActivityKind::Upload, &name, "uploaded", true);
|
||||
info!(%uid, len = meta.len, "pending upload landed");
|
||||
// Both times, so a slow drain shows whether the upload itself or the
|
||||
// checks and gap-fill around it are where the time goes.
|
||||
info!(
|
||||
%uid,
|
||||
len = meta.len,
|
||||
upload_ms = took.as_millis() as u64,
|
||||
total_ms = op_started.elapsed().as_millis() as u64,
|
||||
"pending upload landed"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1518,21 +1756,39 @@ impl Core {
|
||||
/// we just sealed. Skipping both, as this used to, is what produced a file
|
||||
/// that conflicted with itself and left a full-size duplicate behind.
|
||||
///
|
||||
/// `blob` is the revision that landed, which stays the pending one until
|
||||
/// open write handles are rebased off it, and `adopt` gets the node read
|
||||
/// back before that, so the uploaded bytes can be cached under its
|
||||
/// revision first. A handle released in between fills its gaps from one
|
||||
/// or the other; with the blob let go first and the cache filled last, it
|
||||
/// found neither, and the next write open was refused (docs/BUGS.md B123).
|
||||
///
|
||||
/// Best effort: a failure here costs a spurious conflict copy on the next
|
||||
/// write, not this upload, which has already landed.
|
||||
pub(crate) fn refresh_after_upload(&self, uid: &NodeUid) {
|
||||
let node = match self.fetch_node_remote(uid) {
|
||||
/// write, not this upload, which has already landed. Returns the node as
|
||||
/// the server now has it, or `None` when it could not be read back.
|
||||
pub(crate) fn refresh_after_upload(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
blob: &Path,
|
||||
adopt: impl FnOnce(&Node),
|
||||
) -> Option<Node> {
|
||||
let fetched = self.fetch_node_remote(uid);
|
||||
if !matches!(fetched, Ok(Some(_))) {
|
||||
self.release_pending(uid, blob);
|
||||
}
|
||||
let node = match fetched {
|
||||
Ok(Some(node)) => node,
|
||||
// Trashed or deleted under us: the tree will hear it from the event
|
||||
// sync, which is better placed to unhook the inode than we are.
|
||||
Ok(None) => return,
|
||||
Ok(None) => return None,
|
||||
Err(e) => {
|
||||
warn!(%uid, error = %e,
|
||||
"refreshing metadata after an upload failed; \
|
||||
the next write to this file may conflict with itself");
|
||||
return;
|
||||
return None;
|
||||
}
|
||||
};
|
||||
adopt(&node);
|
||||
// This function's whole job is to bring the tree level with the revision
|
||||
// we just sealed, so the feed's report of that revision has nothing left
|
||||
// to tell us. Claimed here rather than at the upload call so it is only
|
||||
@@ -1548,18 +1804,14 @@ impl Core {
|
||||
// This is the open-handle counterpart of `rebaseline_pending`, which
|
||||
// covers the same gap for *queued* ops.
|
||||
{
|
||||
let sealed_mtime = node.modification_time;
|
||||
let sealed_size = node_size(&node);
|
||||
let sealed_rev = node_revision_id(&node);
|
||||
// Remember it as ours, so a queued write that opened over an earlier
|
||||
// revision recognises this as a self-supersede rather than a foreign
|
||||
// change and does not fork (B70 layer B; consulted in
|
||||
// `revision_conflict`).
|
||||
if let Some(rev) = sealed_rev.clone() {
|
||||
self.own_sealed_revs.lock().insert(uid.clone(), rev.clone());
|
||||
// Write through: losing this at shutdown reopened the fork
|
||||
// window B70 layer B closed, for the first drain after a
|
||||
// restart.
|
||||
if let Some(rev) = node_revision_id(&node) {
|
||||
// In the table, not in memory: losing it at shutdown reopened
|
||||
// the fork window B70 layer B closed, for the first drain
|
||||
// after a restart.
|
||||
if let Err(e) = self
|
||||
.db
|
||||
.set_own_sealed_rev(&uid.to_string(), &rev, now_millis())
|
||||
@@ -1567,23 +1819,14 @@ impl Core {
|
||||
warn!(%uid, error = %e, "persisting our sealed revision failed");
|
||||
}
|
||||
}
|
||||
self.for_each_state(|st| {
|
||||
for aw in st.active_writes.values_mut() {
|
||||
if aw.uid == *uid {
|
||||
aw.base_mtime = sealed_mtime;
|
||||
aw.base_size = sealed_size;
|
||||
aw.base_revision_id = sealed_rev.clone();
|
||||
debug!(%uid, mtime = sealed_mtime, size = sealed_size,
|
||||
"rebased open write handle onto the revision just uploaded");
|
||||
}
|
||||
}
|
||||
});
|
||||
self.rebase_open_writes(uid, &node);
|
||||
}
|
||||
self.release_pending(uid, blob);
|
||||
// Ordered so that a write queued *during* the fetch above is still
|
||||
// caught: it took its baseline from the node's optimistic stamp, and
|
||||
// this overwrites it with the revision the server actually holds.
|
||||
if self.rebaseline_pending(uid, &node) {
|
||||
return;
|
||||
return Some(node);
|
||||
}
|
||||
self.for_each_state(|st| {
|
||||
let Some(parent) = st
|
||||
@@ -1596,6 +1839,21 @@ impl Core {
|
||||
};
|
||||
st.intern(parent, node.clone());
|
||||
});
|
||||
Some(node)
|
||||
}
|
||||
|
||||
/// Rebase every write handle open on `uid` onto `node`, a revision this
|
||||
/// daemon has just put there itself (`WriteHandle::rebase_onto`).
|
||||
fn rebase_open_writes(&self, uid: &NodeUid, node: &Node) {
|
||||
self.for_each_state(|st| {
|
||||
for aw in st.active_writes.values_mut() {
|
||||
if aw.uid == *uid {
|
||||
aw.rebase_onto(node);
|
||||
debug!(%uid, mtime = aw.base_mtime, size = aw.base_size,
|
||||
"rebased open write handle onto the revision just landed");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Point a still-queued write at the revision this upload just sealed, and
|
||||
@@ -1686,6 +1944,20 @@ fn is_own_self_supersede(complete: bool, remote_rev: Option<&str>, own_rev: Opti
|
||||
complete && remote_rev.is_some() && remote_rev == own_rev
|
||||
}
|
||||
|
||||
/// Whether Drive has `node` neither where its queued rename found it nor where
|
||||
/// the rename takes it (`parent`, `name`), so someone else moved or renamed it
|
||||
/// in between. Unknown on a row without the original name.
|
||||
fn moved_elsewhere(meta: &RenameMeta, node: &Node, parent: &NodeUid, name: &str) -> bool {
|
||||
let Some(original_name) = meta.original_name.as_deref() else {
|
||||
return false;
|
||||
};
|
||||
let at = |parent: &str, name: &str| {
|
||||
node.parent_uid.as_ref().map(ToString::to_string).as_deref() == Some(parent)
|
||||
&& node.name == name
|
||||
};
|
||||
!at(&meta.original_parent_uid, original_name) && !at(&parent.to_string(), name)
|
||||
}
|
||||
|
||||
/// What the park sweep does with one parked create whose node still exists.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
enum ParkVerdict {
|
||||
@@ -1704,9 +1976,48 @@ fn park_verdict(created_at: i64, now: i64, open: bool) -> ParkVerdict {
|
||||
ParkVerdict::Release
|
||||
}
|
||||
|
||||
/// The retryable error for an op whose name a queued trash has yet to free.
|
||||
fn held_by_queued_trash(name: &str) -> Box<dyn std::error::Error> {
|
||||
format!("{name} is still held by a node whose trash is queued").into()
|
||||
}
|
||||
|
||||
/// How much older than its op a remote node may be and still be the one an
|
||||
/// unanswered call made. The op is queued the moment the call gives up, so the
|
||||
/// node is at most one call's deadline older; the rest is clock skew between
|
||||
/// this machine and Proton.
|
||||
const ADOPT_WINDOW_MS: i64 = 2 * 60 * 1000;
|
||||
|
||||
/// Whether `twin`, found holding the name a queued create wants, is the node an
|
||||
/// earlier, unanswered attempt at that create made: the same kind, created
|
||||
/// around when the op was queued, and for a file still empty, as the mount's
|
||||
/// create leaves it.
|
||||
fn adoptable(is_dir: bool, twin: &Node, op_created_ms: i64) -> bool {
|
||||
if twin.creation_time.saturating_mul(1000) < op_created_ms - ADOPT_WINDOW_MS {
|
||||
return false;
|
||||
}
|
||||
match &twin.kind {
|
||||
NodeKind::Folder => is_dir,
|
||||
NodeKind::File { claimed_size, .. } => !is_dir && *claimed_size == Some(0),
|
||||
}
|
||||
}
|
||||
|
||||
/// What share of the cache budget an unpinned upload may take and still be kept
|
||||
/// as cached content once it lands: a sixteenth, 320 MiB of the default 5 GiB.
|
||||
/// A larger file would push out much of what the user read lately, for bytes
|
||||
/// they may never read back.
|
||||
const LANDED_KEEP_SHARE: u64 = 16;
|
||||
|
||||
/// Whether an upload of `len` bytes that just landed stays as the file's cached
|
||||
/// content. A pinned file always does, and so does any file when the budget is
|
||||
/// unlimited (`0`).
|
||||
fn keeps_landed_upload(pinned: bool, len: u64, budget: u64) -> bool {
|
||||
pinned || budget == 0 || len <= budget / LANDED_KEEP_SHARE
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::state::WriteHandle;
|
||||
use pdfs_core::Access;
|
||||
use pdfs_core::db::Db;
|
||||
use proton_drive_rs::NodeKind;
|
||||
@@ -1818,6 +2129,7 @@ mod tests {
|
||||
op.meta_json = Some(
|
||||
serde_json::to_string(&RenameMeta {
|
||||
original_parent_uid: source.uid.to_string(),
|
||||
original_name: Some(moved.name.clone()),
|
||||
})
|
||||
.unwrap(),
|
||||
);
|
||||
@@ -2083,6 +2395,60 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_a_fresh_empty_twin_of_the_same_kind_is_adopted() {
|
||||
// The op was queued at t=1000 s, after a create that got no answer.
|
||||
let queued = 1_000_000;
|
||||
let made = |mut node: Node, secs: i64| {
|
||||
node.creation_time = secs;
|
||||
node
|
||||
};
|
||||
assert!(adoptable(false, &made(file_node(0, 0, None), 990), queued));
|
||||
assert!(adoptable(true, &made(folder_node("d", None), 990), queued));
|
||||
// Made long before the op: someone else's file, not our lost create.
|
||||
assert!(!adoptable(false, &made(file_node(0, 0, None), 800), queued));
|
||||
// Our create leaves a file empty; one with content is someone else's.
|
||||
assert!(!adoptable(false, &made(file_node(0, 5, None), 990), queued));
|
||||
// The kind has to match what the op makes.
|
||||
assert!(!adoptable(true, &made(file_node(0, 0, None), 990), queued));
|
||||
assert!(!adoptable(
|
||||
false,
|
||||
&made(folder_node("d", None), 990),
|
||||
queued
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_landed_upload_stays_cached_unless_it_would_crowd_the_cache_out() {
|
||||
const GIB: u64 = 1 << 30;
|
||||
let budget = 5 * GIB;
|
||||
// An edited document or a source file: read back from disk, not Drive.
|
||||
assert!(keeps_landed_upload(false, 40_000, budget));
|
||||
assert!(keeps_landed_upload(false, budget / 16, budget));
|
||||
// A video the user copied in would push out what they read lately.
|
||||
assert!(!keeps_landed_upload(false, budget / 16 + 1, budget));
|
||||
// Pinned content is kept whatever its size, and so is everything when
|
||||
// the cache has no cap.
|
||||
assert!(keeps_landed_upload(true, 4 * GIB, budget));
|
||||
assert!(keeps_landed_upload(false, 4 * GIB, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_landing_upload_counts_until_its_guard_goes() {
|
||||
let count = Arc::new(AtomicU64::new(0));
|
||||
let first = LandingUpload::begin(&count);
|
||||
let landed = || -> Result<(), ()> {
|
||||
let _second = LandingUpload::begin(&count);
|
||||
assert_eq!(count.load(Ordering::SeqCst), 2);
|
||||
Err(())
|
||||
};
|
||||
// Uncounted on an early return too, or the queue never reads empty.
|
||||
assert!(landed().is_err());
|
||||
assert_eq!(count.load(Ordering::SeqCst), 1);
|
||||
drop(first);
|
||||
assert_eq!(count.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
||||
fn baseline(mtime: i64, size: u64, rev: Option<&str>) -> Baseline {
|
||||
Baseline {
|
||||
mtime,
|
||||
@@ -2110,6 +2476,26 @@ mod tests {
|
||||
assert!(revision_changed(&base, &remote).is_some());
|
||||
}
|
||||
|
||||
/// The reported shape (docs/BUGS.md B113): a downloader creates a file
|
||||
/// through the mount, and the file is moved before its write drains. The
|
||||
/// move re-stamps the node's mtime on the server (1790955809 -> 1790956593),
|
||||
/// but the revision is still the empty one the create minted.
|
||||
#[test]
|
||||
fn a_move_before_the_first_write_drains_is_not_a_conflict() {
|
||||
let minted = file_node(1_790_955_809, 0, Some("empty"));
|
||||
let path = std::env::temp_dir().join(format!("pdfs-drain-test-{}", std::process::id()));
|
||||
let file = File::create(&path).unwrap();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
let h = WriteHandle::created(2, &minted, file, path);
|
||||
let base = baseline(h.base_mtime, h.base_size, h.base_revision_id.as_deref());
|
||||
|
||||
let moved = file_node(1_790_956_593, 0, Some("empty"));
|
||||
assert_eq!(revision_changed(&base, &moved), None);
|
||||
// Another device writing it is still a conflict.
|
||||
let edited = file_node(1_790_956_593, 0, Some("theirs"));
|
||||
assert!(revision_changed(&base, &edited).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn without_ids_it_falls_back_to_mtime_and_size() {
|
||||
// Old sidecar (no revision id): the (mtime, size) tuple still governs.
|
||||
|
||||
@@ -0,0 +1,466 @@
|
||||
//! The calls the sync core makes to Drive, behind a trait.
|
||||
//!
|
||||
//! `Core` used to hold a concrete [`ProtonDriveClient`], so no test could make
|
||||
//! Drive answer late, answer stale, or not answer at all, and the interleavings
|
||||
//! behind most recent bugs could only be found on a real account over a slow
|
||||
//! link (`docs/MILESTONE-3.0.0.md` §2, §8.1). [`DriveApi`] is the part of the
|
||||
//! client the mount, the drain, the event feed and the mirror engine use; the
|
||||
//! real client implements it by forwarding, and the simulation tests implement
|
||||
//! it with an in-memory Drive that injects Drive's known faults.
|
||||
//!
|
||||
//! Sharing, Photos, devices, invitations and public links stay on the concrete
|
||||
//! client (`Core::client`) until a test needs them faked.
|
||||
//!
|
||||
//! The method names match the client's own, so a call site reads the same
|
||||
//! whichever it goes through. The client's generic methods (a reader or writer
|
||||
//! of any type) cannot be part of an object-safe trait, so the trait takes them
|
||||
//! as trait objects and `impl dyn DriveApi` puts the generic signatures back.
|
||||
|
||||
use std::io::{Read, Write};
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use futures::StreamExt as _;
|
||||
use futures::stream::BoxStream;
|
||||
use proton_drive_rs::proton_sdk::account::Quota;
|
||||
use proton_drive_rs::proton_sdk::error::Result;
|
||||
use proton_drive_rs::proton_sdk::ids::{DriveEventId, NodeUid};
|
||||
use proton_drive_rs::{
|
||||
DriveEvent, DriveEventScopeId, Node, NodeMoveItem, ProtonDriveClient, RevisionReader, Thumbnail,
|
||||
};
|
||||
|
||||
/// One outcome per node of a batch request; see [`ProtonDriveClient::trash_nodes`].
|
||||
pub(crate) type NodeOutcomes = Vec<(NodeUid, Result<()>)>;
|
||||
|
||||
/// The outcomes of a streaming batch request, one item per node as it lands.
|
||||
pub(crate) type OutcomeStream<'a> = BoxStream<'a, Result<(NodeUid, Result<()>)>>;
|
||||
|
||||
/// Drive as the sync core sees it. See the module documentation.
|
||||
#[async_trait]
|
||||
pub(crate) trait DriveApi: Send + Sync {
|
||||
async fn get_my_files_folder(&self) -> Result<Node>;
|
||||
|
||||
async fn get_node(&self, uid: &NodeUid) -> Result<Option<Node>>;
|
||||
|
||||
async fn enumerate_nodes(&self, uids: &[NodeUid]) -> Result<Vec<Node>>;
|
||||
|
||||
/// As [`DriveApi::enumerate_nodes`], but files carry no claimed size or
|
||||
/// modification time; see [`ProtonDriveClient::enumerate_nodes_light`].
|
||||
async fn enumerate_nodes_light(&self, uids: &[NodeUid]) -> Result<Vec<Node>>;
|
||||
|
||||
async fn enumerate_folder_children_node_uids(
|
||||
&self,
|
||||
folder_uid: &NodeUid,
|
||||
) -> Result<Vec<NodeUid>>;
|
||||
|
||||
async fn enumerate_events(
|
||||
&self,
|
||||
scope: &DriveEventScopeId,
|
||||
cursor: Option<&DriveEventId>,
|
||||
) -> Result<Vec<DriveEvent>>;
|
||||
|
||||
async fn invalidate_caches_for_event(&self, event: &DriveEvent) -> Result<()>;
|
||||
|
||||
async fn create_folder(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
last_modification_time: Option<i64>,
|
||||
) -> Result<NodeUid>;
|
||||
|
||||
async fn upload_file(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
media_type: &str,
|
||||
contents: &[u8],
|
||||
) -> Result<NodeUid>;
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn upload_file_from_dyn(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
media_type: &str,
|
||||
reader: &mut (dyn Read + Send),
|
||||
intended_size: i64,
|
||||
thumbnails: Vec<Thumbnail>,
|
||||
last_modification_time: Option<i64>,
|
||||
aead: bool,
|
||||
) -> Result<NodeUid>;
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn upload_file_replacing_draft_from_dyn(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
media_type: &str,
|
||||
reader: &mut (dyn Read + Send),
|
||||
intended_size: i64,
|
||||
thumbnails: Vec<Thumbnail>,
|
||||
last_modification_time: Option<i64>,
|
||||
aead: bool,
|
||||
) -> Result<NodeUid>;
|
||||
|
||||
async fn upload_new_revision_from_dyn(
|
||||
&self,
|
||||
file_uid: &NodeUid,
|
||||
reader: &mut (dyn Read + Send),
|
||||
intended_size: i64,
|
||||
thumbnails: Vec<Thumbnail>,
|
||||
last_modification_time: Option<i64>,
|
||||
) -> Result<()>;
|
||||
|
||||
async fn open_revision(&self, uid: &NodeUid) -> Result<Arc<dyn RevisionRead>>;
|
||||
|
||||
async fn download_file_to_dyn(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
output: &mut (dyn Write + Send),
|
||||
) -> Result<()>;
|
||||
|
||||
async fn download_revision_to_dyn(
|
||||
&self,
|
||||
file_uid: &NodeUid,
|
||||
revision_id: &str,
|
||||
writer: &mut (dyn Write + Send),
|
||||
) -> Result<()>;
|
||||
|
||||
async fn rename_node(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
new_name: &str,
|
||||
new_media_type: Option<&str>,
|
||||
) -> Result<()>;
|
||||
|
||||
async fn move_node(&self, uid: &NodeUid, new_parent: &NodeUid) -> Result<()>;
|
||||
|
||||
fn move_nodes_streaming<'a>(
|
||||
&'a self,
|
||||
items: Vec<NodeMoveItem>,
|
||||
new_parent: NodeUid,
|
||||
) -> OutcomeStream<'a>;
|
||||
|
||||
async fn trash_nodes(&self, uids: &[NodeUid]) -> Result<NodeOutcomes>;
|
||||
|
||||
async fn restore_nodes(&self, uids: &[NodeUid]) -> Result<NodeOutcomes>;
|
||||
|
||||
fn restore_nodes_streaming<'a>(&'a self, uids: &[NodeUid]) -> OutcomeStream<'a>;
|
||||
|
||||
fn delete_nodes_streaming<'a>(&'a self, uids: &[NodeUid]) -> OutcomeStream<'a>;
|
||||
|
||||
async fn enumerate_trash_node_uids(&self) -> Result<Vec<NodeUid>>;
|
||||
|
||||
/// The account's storage, across every Proton product.
|
||||
async fn quota(&self) -> Result<Quota>;
|
||||
}
|
||||
|
||||
/// The generic signatures of the client, over the object-safe ones above.
|
||||
impl dyn DriveApi {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) async fn upload_file_from<R: Read + Send>(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
media_type: &str,
|
||||
mut reader: R,
|
||||
intended_size: i64,
|
||||
thumbnails: Vec<Thumbnail>,
|
||||
last_modification_time: Option<i64>,
|
||||
aead: bool,
|
||||
) -> Result<NodeUid> {
|
||||
self.upload_file_from_dyn(
|
||||
parent_uid,
|
||||
name,
|
||||
media_type,
|
||||
&mut reader,
|
||||
intended_size,
|
||||
thumbnails,
|
||||
last_modification_time,
|
||||
aead,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) async fn upload_file_replacing_draft_from<R: Read + Send>(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
media_type: &str,
|
||||
mut reader: R,
|
||||
intended_size: i64,
|
||||
thumbnails: Vec<Thumbnail>,
|
||||
last_modification_time: Option<i64>,
|
||||
aead: bool,
|
||||
) -> Result<NodeUid> {
|
||||
self.upload_file_replacing_draft_from_dyn(
|
||||
parent_uid,
|
||||
name,
|
||||
media_type,
|
||||
&mut reader,
|
||||
intended_size,
|
||||
thumbnails,
|
||||
last_modification_time,
|
||||
aead,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn upload_new_revision_from<R: Read + Send>(
|
||||
&self,
|
||||
file_uid: &NodeUid,
|
||||
mut reader: R,
|
||||
intended_size: i64,
|
||||
thumbnails: Vec<Thumbnail>,
|
||||
last_modification_time: Option<i64>,
|
||||
) -> Result<()> {
|
||||
self.upload_new_revision_from_dyn(
|
||||
file_uid,
|
||||
&mut reader,
|
||||
intended_size,
|
||||
thumbnails,
|
||||
last_modification_time,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn download_file_to<W: Write + Send>(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
output: &mut W,
|
||||
) -> Result<()> {
|
||||
self.download_file_to_dyn(uid, output).await
|
||||
}
|
||||
|
||||
pub(crate) async fn download_revision_to<W: Write + Send>(
|
||||
&self,
|
||||
file_uid: &NodeUid,
|
||||
revision_id: &str,
|
||||
writer: &mut W,
|
||||
) -> Result<()> {
|
||||
self.download_revision_to_dyn(file_uid, revision_id, writer)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
/// An open revision that block reads are served from; see [`RevisionReader`].
|
||||
#[async_trait]
|
||||
pub(crate) trait RevisionRead: Send + Sync {
|
||||
/// Plaintext size of the revision.
|
||||
fn size(&self) -> u64;
|
||||
|
||||
/// Plaintext size of each block, in block order.
|
||||
fn block_sizes(&self) -> &[u64];
|
||||
|
||||
async fn read_at(&self, offset: u64, length: u64) -> Result<Vec<u8>>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl RevisionRead for RevisionReader {
|
||||
fn size(&self) -> u64 {
|
||||
RevisionReader::size(self)
|
||||
}
|
||||
|
||||
fn block_sizes(&self) -> &[u64] {
|
||||
RevisionReader::block_sizes(self)
|
||||
}
|
||||
|
||||
async fn read_at(&self, offset: u64, length: u64) -> Result<Vec<u8>> {
|
||||
RevisionReader::read_at(self, offset, length).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl DriveApi for ProtonDriveClient {
|
||||
async fn get_my_files_folder(&self) -> Result<Node> {
|
||||
ProtonDriveClient::get_my_files_folder(self).await
|
||||
}
|
||||
|
||||
async fn get_node(&self, uid: &NodeUid) -> Result<Option<Node>> {
|
||||
ProtonDriveClient::get_node(self, uid).await
|
||||
}
|
||||
|
||||
async fn enumerate_nodes(&self, uids: &[NodeUid]) -> Result<Vec<Node>> {
|
||||
ProtonDriveClient::enumerate_nodes(self, uids).await
|
||||
}
|
||||
|
||||
async fn enumerate_nodes_light(&self, uids: &[NodeUid]) -> Result<Vec<Node>> {
|
||||
ProtonDriveClient::enumerate_nodes_light(self, uids).await
|
||||
}
|
||||
|
||||
async fn enumerate_folder_children_node_uids(
|
||||
&self,
|
||||
folder_uid: &NodeUid,
|
||||
) -> Result<Vec<NodeUid>> {
|
||||
ProtonDriveClient::enumerate_folder_children_node_uids(self, folder_uid).await
|
||||
}
|
||||
|
||||
async fn enumerate_events(
|
||||
&self,
|
||||
scope: &DriveEventScopeId,
|
||||
cursor: Option<&DriveEventId>,
|
||||
) -> Result<Vec<DriveEvent>> {
|
||||
ProtonDriveClient::enumerate_events(self, scope, cursor).await
|
||||
}
|
||||
|
||||
async fn invalidate_caches_for_event(&self, event: &DriveEvent) -> Result<()> {
|
||||
ProtonDriveClient::invalidate_caches_for_event(self, event).await
|
||||
}
|
||||
|
||||
async fn create_folder(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
last_modification_time: Option<i64>,
|
||||
) -> Result<NodeUid> {
|
||||
ProtonDriveClient::create_folder(self, parent_uid, name, last_modification_time).await
|
||||
}
|
||||
|
||||
async fn upload_file(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
media_type: &str,
|
||||
contents: &[u8],
|
||||
) -> Result<NodeUid> {
|
||||
ProtonDriveClient::upload_file(self, parent_uid, name, media_type, contents).await
|
||||
}
|
||||
|
||||
async fn upload_file_from_dyn(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
media_type: &str,
|
||||
reader: &mut (dyn Read + Send),
|
||||
intended_size: i64,
|
||||
thumbnails: Vec<Thumbnail>,
|
||||
last_modification_time: Option<i64>,
|
||||
aead: bool,
|
||||
) -> Result<NodeUid> {
|
||||
ProtonDriveClient::upload_file_from(
|
||||
self,
|
||||
parent_uid,
|
||||
name,
|
||||
media_type,
|
||||
reader,
|
||||
intended_size,
|
||||
thumbnails,
|
||||
last_modification_time,
|
||||
aead,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn upload_file_replacing_draft_from_dyn(
|
||||
&self,
|
||||
parent_uid: &NodeUid,
|
||||
name: &str,
|
||||
media_type: &str,
|
||||
reader: &mut (dyn Read + Send),
|
||||
intended_size: i64,
|
||||
thumbnails: Vec<Thumbnail>,
|
||||
last_modification_time: Option<i64>,
|
||||
aead: bool,
|
||||
) -> Result<NodeUid> {
|
||||
ProtonDriveClient::upload_file_replacing_draft_from(
|
||||
self,
|
||||
parent_uid,
|
||||
name,
|
||||
media_type,
|
||||
reader,
|
||||
intended_size,
|
||||
thumbnails,
|
||||
last_modification_time,
|
||||
aead,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn upload_new_revision_from_dyn(
|
||||
&self,
|
||||
file_uid: &NodeUid,
|
||||
reader: &mut (dyn Read + Send),
|
||||
intended_size: i64,
|
||||
thumbnails: Vec<Thumbnail>,
|
||||
last_modification_time: Option<i64>,
|
||||
) -> Result<()> {
|
||||
ProtonDriveClient::upload_new_revision_from(
|
||||
self,
|
||||
file_uid,
|
||||
reader,
|
||||
intended_size,
|
||||
thumbnails,
|
||||
last_modification_time,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn open_revision(&self, uid: &NodeUid) -> Result<Arc<dyn RevisionRead>> {
|
||||
let reader = ProtonDriveClient::open_revision(self, uid).await?;
|
||||
Ok(Arc::new(reader))
|
||||
}
|
||||
|
||||
async fn download_file_to_dyn(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
output: &mut (dyn Write + Send),
|
||||
) -> Result<()> {
|
||||
ProtonDriveClient::download_file_to(self, uid, &mut &mut *output).await
|
||||
}
|
||||
|
||||
async fn download_revision_to_dyn(
|
||||
&self,
|
||||
file_uid: &NodeUid,
|
||||
revision_id: &str,
|
||||
writer: &mut (dyn Write + Send),
|
||||
) -> Result<()> {
|
||||
ProtonDriveClient::download_revision_to(self, file_uid, revision_id, &mut &mut *writer)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn rename_node(
|
||||
&self,
|
||||
uid: &NodeUid,
|
||||
new_name: &str,
|
||||
new_media_type: Option<&str>,
|
||||
) -> Result<()> {
|
||||
ProtonDriveClient::rename_node(self, uid, new_name, new_media_type).await
|
||||
}
|
||||
|
||||
async fn move_node(&self, uid: &NodeUid, new_parent: &NodeUid) -> Result<()> {
|
||||
ProtonDriveClient::move_node(self, uid, new_parent).await
|
||||
}
|
||||
|
||||
fn move_nodes_streaming<'a>(
|
||||
&'a self,
|
||||
items: Vec<NodeMoveItem>,
|
||||
new_parent: NodeUid,
|
||||
) -> OutcomeStream<'a> {
|
||||
ProtonDriveClient::move_nodes_streaming(self, items, new_parent).boxed()
|
||||
}
|
||||
|
||||
async fn trash_nodes(&self, uids: &[NodeUid]) -> Result<NodeOutcomes> {
|
||||
ProtonDriveClient::trash_nodes(self, uids).await
|
||||
}
|
||||
|
||||
async fn restore_nodes(&self, uids: &[NodeUid]) -> Result<NodeOutcomes> {
|
||||
ProtonDriveClient::restore_nodes(self, uids).await
|
||||
}
|
||||
|
||||
fn restore_nodes_streaming<'a>(&'a self, uids: &[NodeUid]) -> OutcomeStream<'a> {
|
||||
ProtonDriveClient::restore_nodes_streaming(self, uids).boxed()
|
||||
}
|
||||
|
||||
fn delete_nodes_streaming<'a>(&'a self, uids: &[NodeUid]) -> OutcomeStream<'a> {
|
||||
ProtonDriveClient::delete_nodes_streaming(self, uids).boxed()
|
||||
}
|
||||
|
||||
async fn enumerate_trash_node_uids(&self) -> Result<Vec<NodeUid>> {
|
||||
ProtonDriveClient::enumerate_trash_node_uids(self).await
|
||||
}
|
||||
|
||||
async fn quota(&self) -> Result<Quota> {
|
||||
ProtonDriveClient::quota(self).await
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
//! Push notifications for front-ends: the daemon's side of
|
||||
//! [`Request::Subscribe`](pdfs_core::control::Request::Subscribe).
|
||||
//!
|
||||
//! Front-ends used to poll. The app asked for the status every two seconds, the
|
||||
//! tray every three, and pages re-read themselves on a timer, whether or not
|
||||
//! anything had changed. Now each subscription owns a [`Mailbox`] in the
|
||||
//! [`EventHub`]. Code that changes something publishes a [`Topic`], and
|
||||
//! [`run_event_sampler`] watches the few things that change without passing
|
||||
//! through one place (the status, transfers, sync progress) and publishes them
|
||||
//! when they differ.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use parking_lot::{Condvar, Mutex, MutexGuard};
|
||||
use pdfs_core::control::{Event, JobItem, Response as CtlResponse, Topic, TransferItem};
|
||||
use tracing::warn;
|
||||
|
||||
use super::Core;
|
||||
|
||||
/// Most subscriptions served at once. Each holds a control handler for as long
|
||||
/// as it lasts; the app and the tray need one apiece, and the cap keeps a
|
||||
/// misbehaving client from spending the handlers that requests need.
|
||||
const MAX_SUBSCRIBERS: usize = 8;
|
||||
|
||||
/// How long a subscription waits after a change before it sends, so a burst
|
||||
/// (a folder of files trashed at once) goes out as one event.
|
||||
const COALESCE: Duration = Duration::from_millis(150);
|
||||
|
||||
/// How often [`run_event_sampler`] looks at transfers. Status and sync
|
||||
/// progress are looked at every other time.
|
||||
const SAMPLE_INTERVAL: Duration = Duration::from_millis(500);
|
||||
|
||||
/// Every open subscription's mailbox. Shared by every clone of [`Core`],
|
||||
/// on-demand forks included, so a change in any mount reaches every client.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct EventHub {
|
||||
mailboxes: Mutex<Vec<Arc<Mailbox>>>,
|
||||
}
|
||||
|
||||
/// What one subscription has yet to send.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Mailbox {
|
||||
inbox: Mutex<Inbox>,
|
||||
ready: Condvar,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Inbox {
|
||||
topics: BTreeSet<Topic>,
|
||||
/// The newest [`Event::Status`] line. A newer one replaces it unsent: a
|
||||
/// client wants the current status, not every status in between.
|
||||
status: Option<Arc<str>>,
|
||||
/// The newest [`Event::Transfers`] line, replaced the same way.
|
||||
transfers: Option<Arc<str>>,
|
||||
}
|
||||
|
||||
impl Inbox {
|
||||
fn is_empty(&self) -> bool {
|
||||
self.topics.is_empty() && self.status.is_none() && self.transfers.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
/// `event` as one line of the wire format, newline included.
|
||||
pub(crate) fn event_line(event: &Event) -> Option<Arc<str>> {
|
||||
match serde_json::to_string(event) {
|
||||
Ok(mut line) => {
|
||||
line.push('\n');
|
||||
Some(line.into())
|
||||
}
|
||||
Err(e) => {
|
||||
warn!(error = %e, "events: serialize event failed");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EventHub {
|
||||
/// Tell every subscriber that `topics` changed.
|
||||
pub(crate) fn publish(&self, topics: &[Topic]) {
|
||||
self.deliver(|inbox| inbox.topics.extend(topics.iter().copied()));
|
||||
}
|
||||
|
||||
/// Hand every subscriber a fresh status.
|
||||
fn publish_status(&self, line: Arc<str>) {
|
||||
self.deliver(|inbox| inbox.status = Some(line.clone()));
|
||||
}
|
||||
|
||||
/// Hand every subscriber a fresh transfers snapshot.
|
||||
fn publish_transfers(&self, line: Arc<str>) {
|
||||
self.deliver(|inbox| inbox.transfers = Some(line.clone()));
|
||||
}
|
||||
|
||||
fn deliver(&self, put: impl Fn(&mut Inbox)) {
|
||||
for mailbox in self.mailboxes.lock().iter() {
|
||||
put(&mut mailbox.inbox.lock());
|
||||
mailbox.ready.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
/// Open a mailbox, or `None` at [`MAX_SUBSCRIBERS`].
|
||||
pub(crate) fn subscribe(&self) -> Option<Arc<Mailbox>> {
|
||||
let mut mailboxes = self.mailboxes.lock();
|
||||
if mailboxes.len() >= MAX_SUBSCRIBERS {
|
||||
return None;
|
||||
}
|
||||
let mailbox = Arc::new(Mailbox::default());
|
||||
mailboxes.push(mailbox.clone());
|
||||
Some(mailbox)
|
||||
}
|
||||
|
||||
pub(crate) fn unsubscribe(&self, mailbox: &Arc<Mailbox>) {
|
||||
self.mailboxes
|
||||
.lock()
|
||||
.retain(|other| !Arc::ptr_eq(other, mailbox));
|
||||
}
|
||||
|
||||
fn has_subscribers(&self) -> bool {
|
||||
!self.mailboxes.lock().is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl Mailbox {
|
||||
/// Wait up to `timeout` for something to send, then take it as wire lines:
|
||||
/// the status first, then transfers, then the changed topics. Empty when
|
||||
/// nothing arrived in time.
|
||||
pub(crate) fn take(&self, timeout: Duration) -> Vec<Arc<str>> {
|
||||
let mut inbox = self.inbox.lock();
|
||||
if inbox.is_empty() {
|
||||
self.ready.wait_for(&mut inbox, timeout);
|
||||
if inbox.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
// Let the rest of a burst arrive before sending the first of it.
|
||||
MutexGuard::unlocked(&mut inbox, || std::thread::sleep(COALESCE));
|
||||
}
|
||||
let mut lines = Vec::new();
|
||||
lines.extend(inbox.status.take());
|
||||
lines.extend(inbox.transfers.take());
|
||||
if !inbox.topics.is_empty() {
|
||||
let topics = std::mem::take(&mut inbox.topics).into_iter().collect();
|
||||
lines.extend(event_line(&Event::Changed { topics }));
|
||||
}
|
||||
lines
|
||||
}
|
||||
}
|
||||
|
||||
/// The [`Event::Transfers`] line for a snapshot.
|
||||
pub(crate) fn transfers_line(items: Vec<TransferItem>, jobs: Vec<JobItem>) -> Option<Arc<str>> {
|
||||
event_line(&Event::Transfers { items, jobs })
|
||||
}
|
||||
|
||||
/// Watch what changes without a single place to publish it from, and publish it
|
||||
/// when it differs from the last look: the status (queue counts, cache use,
|
||||
/// online, pause), the transfers snapshot, and the synced folders with their
|
||||
/// live progress. Looks only while someone is subscribed.
|
||||
pub(crate) fn run_event_sampler(core: Core, username: String, mountpoint: std::path::PathBuf) {
|
||||
let mut transfers: Option<Arc<str>> = None;
|
||||
let mut status: Option<String> = None;
|
||||
let mut queue: Option<String> = None;
|
||||
let mut locations: Option<String> = None;
|
||||
let mut tick = 0u32;
|
||||
while core.shutdown.sleep(SAMPLE_INTERVAL) {
|
||||
if !core.events.has_subscribers() {
|
||||
// A new subscription starts from a snapshot of its own, so there is
|
||||
// nothing to compare against until then.
|
||||
transfers = None;
|
||||
status = None;
|
||||
queue = None;
|
||||
locations = None;
|
||||
continue;
|
||||
}
|
||||
let line = transfers_line(core.transfers.snapshot(), core.jobs_snapshot());
|
||||
if line.is_some() && line != transfers {
|
||||
transfers = line.clone();
|
||||
if let Some(line) = line {
|
||||
core.events.publish_transfers(line);
|
||||
}
|
||||
}
|
||||
|
||||
tick = tick.wrapping_add(1);
|
||||
if !tick.is_multiple_of(2) {
|
||||
continue;
|
||||
}
|
||||
let now = super::control::status_response(&core, &username, &mountpoint);
|
||||
let (key, queue_key) = status_keys(&now);
|
||||
if status.as_ref().is_some_and(|last| *last != key)
|
||||
&& let Some(line) = event_line(&Event::Status(Box::new(now)))
|
||||
{
|
||||
core.events.publish_status(line);
|
||||
}
|
||||
status = Some(key);
|
||||
if queue.as_ref().is_some_and(|last| *last != queue_key) {
|
||||
core.events.publish(&[Topic::Queue]);
|
||||
}
|
||||
queue = Some(queue_key);
|
||||
|
||||
if let Ok(now) = core.list_locations() {
|
||||
let key = serde_json::to_string(&now).unwrap_or_default();
|
||||
if locations.as_ref().is_some_and(|last| *last != key) {
|
||||
core.events.publish(&[Topic::Locations]);
|
||||
}
|
||||
locations = Some(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What makes two statuses different enough to send, and what makes their
|
||||
/// queues different. The age of the oldest staged write is left out of both:
|
||||
/// it grows every second while anything is staged, which is not news.
|
||||
fn status_keys(status: &CtlResponse) -> (String, String) {
|
||||
let CtlResponse::Status {
|
||||
parked_uploads,
|
||||
failing_ops,
|
||||
failing_error,
|
||||
staged_bytes,
|
||||
paused,
|
||||
paused_until,
|
||||
username,
|
||||
mountpoint,
|
||||
pinned,
|
||||
used,
|
||||
budget,
|
||||
online,
|
||||
pending_uploads,
|
||||
pending_changes,
|
||||
..
|
||||
} = status
|
||||
else {
|
||||
return (String::new(), String::new());
|
||||
};
|
||||
let queue = format!(
|
||||
"{pending_uploads}/{pending_changes}/{parked_uploads}/{failing_ops}/{failing_error:?}"
|
||||
);
|
||||
let key = format!(
|
||||
"{queue}/{staged_bytes}/{paused}/{paused_until:?}/{username}/{mountpoint}/{pinned}/\
|
||||
{used}/{budget}/{online}"
|
||||
);
|
||||
(key, queue)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn changed(line: &str) -> Vec<Topic> {
|
||||
match serde_json::from_str::<Event>(line.trim()) {
|
||||
Ok(Event::Changed { topics }) => topics,
|
||||
other => panic!("not a change event: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_burst_of_changes_arrives_as_one_event() {
|
||||
let hub = EventHub::default();
|
||||
let mailbox = hub.subscribe().expect("room for a subscriber");
|
||||
hub.publish(&[Topic::Trash, Topic::Files]);
|
||||
hub.publish(&[Topic::Files]);
|
||||
let lines = mailbox.take(Duration::from_secs(1));
|
||||
assert_eq!(lines.len(), 1);
|
||||
assert_eq!(changed(&lines[0]), vec![Topic::Files, Topic::Trash]);
|
||||
assert!(mailbox.take(Duration::from_millis(10)).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_newer_status_replaces_one_not_yet_sent() {
|
||||
let hub = EventHub::default();
|
||||
let mailbox = hub.subscribe().expect("room for a subscriber");
|
||||
hub.publish_status("first\n".into());
|
||||
hub.publish_status("second\n".into());
|
||||
assert_eq!(
|
||||
mailbox.take(Duration::from_secs(1)),
|
||||
vec![Arc::<str>::from("second\n")]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_closed_subscription_gets_nothing_more() {
|
||||
let hub = EventHub::default();
|
||||
let mailbox = hub.subscribe().expect("room for a subscriber");
|
||||
hub.unsubscribe(&mailbox);
|
||||
hub.publish(&[Topic::Files]);
|
||||
assert!(mailbox.take(Duration::from_millis(10)).is_empty());
|
||||
assert!(!hub.has_subscribers());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subscriptions_are_capped() {
|
||||
let hub = EventHub::default();
|
||||
let open: Vec<_> = (0..MAX_SUBSCRIBERS)
|
||||
.map(|_| hub.subscribe().expect("room for a subscriber"))
|
||||
.collect();
|
||||
assert!(hub.subscribe().is_none());
|
||||
hub.unsubscribe(&open[0]);
|
||||
assert!(hub.subscribe().is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_age_of_a_staged_write_is_not_a_change() {
|
||||
let status = |staged_oldest_secs| CtlResponse::Status {
|
||||
parked_uploads: 0,
|
||||
failing_ops: 0,
|
||||
failing_error: None,
|
||||
staged_bytes: 10,
|
||||
staged_oldest_secs,
|
||||
paused: false,
|
||||
paused_until: None,
|
||||
username: "u".into(),
|
||||
mountpoint: "/m".into(),
|
||||
pinned: 0,
|
||||
used: 0,
|
||||
budget: 0,
|
||||
pins: Vec::new(),
|
||||
online: true,
|
||||
pending_uploads: 1,
|
||||
pending_changes: 0,
|
||||
};
|
||||
assert_eq!(status_keys(&status(5)), status_keys(&status(6)));
|
||||
}
|
||||
}
|
||||
@@ -72,7 +72,7 @@ impl Filesystem for ProtonFs {
|
||||
}
|
||||
|
||||
fn lookup(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEntry) {
|
||||
let parent = parent.0;
|
||||
let parent = self.tree(parent).0;
|
||||
let name = match fuse_name(name) {
|
||||
Ok(name) => name,
|
||||
Err(error) => {
|
||||
@@ -95,11 +95,13 @@ impl Filesystem for ProtonFs {
|
||||
}
|
||||
|
||||
fn forget(&self, _req: &Request, ino: INodeNo, nlookup: u64) {
|
||||
let ino = self.tree(ino);
|
||||
let mut st = self.core.state();
|
||||
st.forget_lookup(ino.0, nlookup);
|
||||
}
|
||||
|
||||
fn getattr(&self, _req: &Request, ino: INodeNo, _fh: Option<FileHandle>, reply: ReplyAttr) {
|
||||
let ino = self.tree(ino);
|
||||
let (attr, provisional_in) = {
|
||||
let st = self.core.state();
|
||||
match st.entries.get(&ino.0) {
|
||||
@@ -175,7 +177,7 @@ impl Filesystem for ProtonFs {
|
||||
/// Enumerating a cold folder is a remote call, so it goes to a worker for
|
||||
/// the same reason `lookup` and `readdir` do.
|
||||
fn opendir(&self, _req: &Request, ino: INodeNo, _flags: OpenFlags, reply: ReplyOpen) {
|
||||
let ino = ino.0;
|
||||
let ino = self.tree(ino).0;
|
||||
if self.core.children_cached(ino) {
|
||||
self.serve_opendir(ino, reply);
|
||||
return;
|
||||
@@ -194,13 +196,13 @@ impl Filesystem for ProtonFs {
|
||||
offset: u64,
|
||||
reply: ReplyDirectory,
|
||||
) {
|
||||
let ino = ino.0;
|
||||
let ino = self.tree(ino).0;
|
||||
// The snapshot is the whole point: served from it, this is a pure
|
||||
// in-memory walk with no chance of a remote call, so it never needs a
|
||||
// worker. Only a handle whose `opendir` failed to snapshot (or a kernel
|
||||
// that issued `readdir` without one) falls back to the live path.
|
||||
if let Some(listing) = self.core.state().dir_snapshots.get(&fh.0).cloned() {
|
||||
reply_listing(&listing, offset, reply);
|
||||
reply_listing(&listing, offset, self.root, reply);
|
||||
return;
|
||||
}
|
||||
// Same split as `lookup`: only the cold, remote-enumerating path pays
|
||||
@@ -232,6 +234,7 @@ impl Filesystem for ProtonFs {
|
||||
/// a blob that is the whole file — so it is served from a worker rather than
|
||||
/// stalling every other request on the mount behind it.
|
||||
fn open(&self, _req: &Request, ino: INodeNo, flags: OpenFlags, reply: ReplyOpen) {
|
||||
let ino = self.tree(ino);
|
||||
if flags.acc_mode() == OpenAccMode::O_RDONLY {
|
||||
self.serve_open(ino, flags, reply);
|
||||
return;
|
||||
@@ -253,6 +256,7 @@ impl Filesystem for ProtonFs {
|
||||
_lock_owner: Option<LockOwner>,
|
||||
reply: ReplyData,
|
||||
) {
|
||||
let ino = self.tree(ino);
|
||||
// A file open for writing is served from its handle so reads see the
|
||||
// in-flight (possibly unsaved) content: authored bytes come from the
|
||||
// scratch file, untouched bytes from the base.
|
||||
@@ -290,7 +294,7 @@ impl Filesystem for ProtonFs {
|
||||
}
|
||||
return;
|
||||
}
|
||||
let (uid, mtime, fsize, is_video) = {
|
||||
let (uid, mtime, fsize, provisional, is_video) = {
|
||||
let st = self.core.state();
|
||||
match st.entries.get(&ino.0) {
|
||||
Some(e) if e.node.is_file() => {
|
||||
@@ -300,14 +304,24 @@ impl Filesystem for ProtonFs {
|
||||
};
|
||||
let is_video =
|
||||
PhotoKind::classify(Some(&e.node.name), media_type) == PhotoKind::Video;
|
||||
let fsize = self
|
||||
.core
|
||||
.pending
|
||||
.lock()
|
||||
.get(&e.uid)
|
||||
.map(|p| p.meta.len)
|
||||
.unwrap_or_else(|| node_size(&e.node));
|
||||
(e.uid.clone(), e.node.modification_time, fsize, is_video)
|
||||
let queued = self.core.pending.lock().get(&e.uid).map(|p| p.meta.len);
|
||||
// Still the cheap enumeration's ciphertext size (B12).
|
||||
let provisional = queued.is_none()
|
||||
&& matches!(
|
||||
&e.node.kind,
|
||||
NodeKind::File {
|
||||
claimed_size: None,
|
||||
..
|
||||
}
|
||||
);
|
||||
let fsize = queued.unwrap_or_else(|| node_size(&e.node));
|
||||
(
|
||||
e.uid.clone(),
|
||||
e.node.modification_time,
|
||||
fsize,
|
||||
provisional,
|
||||
is_video,
|
||||
)
|
||||
}
|
||||
Some(_) => {
|
||||
reply.error(Errno::EISDIR);
|
||||
@@ -330,6 +344,13 @@ impl Filesystem for ProtonFs {
|
||||
// races with nothing. FUSE does not require replies in request order.
|
||||
let core = self.core.clone();
|
||||
self.core.workers.run(Lane::Transfer, "read", move || {
|
||||
// The revision disagrees with a provisional size, and B84 fails
|
||||
// such a read outright, so settle the size first. On the network,
|
||||
// hence here and not above (bugs.md B100).
|
||||
let fsize = match provisional {
|
||||
true => core.resolve_provisional_size(ino.0, &uid).unwrap_or(fsize),
|
||||
false => fsize,
|
||||
};
|
||||
match core.read_range(&uid, mtime, fsize, offset, size as u64, cache_blocks) {
|
||||
Ok(bytes) => reply.data(&bytes),
|
||||
Err(e) => reply.error(e),
|
||||
@@ -347,7 +368,7 @@ impl Filesystem for ProtonFs {
|
||||
_flags: i32,
|
||||
reply: ReplyCreate,
|
||||
) {
|
||||
let parent = parent.0;
|
||||
let parent = self.tree(parent).0;
|
||||
if let Err(error) = self.core.require_writable(parent) {
|
||||
reply.error(error);
|
||||
return;
|
||||
@@ -383,6 +404,7 @@ impl Filesystem for ProtonFs {
|
||||
_lock_owner: Option<LockOwner>,
|
||||
reply: ReplyWrite,
|
||||
) {
|
||||
let ino = self.tree(ino);
|
||||
if let Err(error) = self.core.require_writable(ino.0) {
|
||||
reply.error(error);
|
||||
return;
|
||||
@@ -425,10 +447,7 @@ impl Filesystem for ProtonFs {
|
||||
reply.error(Errno::EBADF);
|
||||
return;
|
||||
};
|
||||
let end = offset + data.len() as u64;
|
||||
aw.written.add(offset, end);
|
||||
aw.len = aw.len.max(end);
|
||||
aw.dirty = true;
|
||||
aw.record_write(offset, offset + data.len() as u64);
|
||||
let len = aw.len;
|
||||
st.set_size(ino.0, len);
|
||||
len
|
||||
@@ -463,6 +482,7 @@ impl Filesystem for ProtonFs {
|
||||
_flags: Option<BsdFileFlags>,
|
||||
reply: ReplyAttr,
|
||||
) {
|
||||
let ino = self.tree(ino);
|
||||
if let Err(error) = self.core.require_writable(ino.0) {
|
||||
reply.error(error);
|
||||
return;
|
||||
@@ -530,6 +550,7 @@ impl Filesystem for ProtonFs {
|
||||
mode: i32,
|
||||
reply: ReplyEmpty,
|
||||
) {
|
||||
let ino = self.tree(ino);
|
||||
if let Err(error) = self.core.require_writable(ino.0) {
|
||||
reply.error(error);
|
||||
return;
|
||||
@@ -626,16 +647,17 @@ impl Filesystem for ProtonFs {
|
||||
fn release(
|
||||
&self,
|
||||
_req: &Request,
|
||||
_ino: INodeNo,
|
||||
ino: INodeNo,
|
||||
fh: FileHandle,
|
||||
_flags: OpenFlags,
|
||||
_lock_owner: Option<LockOwner>,
|
||||
_flush: bool,
|
||||
reply: ReplyEmpty,
|
||||
) {
|
||||
let ino = self.tree(ino);
|
||||
let (handle, unlinked_uid) = {
|
||||
let mut st = self.core.state();
|
||||
let unlinked_uid = release_unlinked_entry(&mut st, _ino.0);
|
||||
let unlinked_uid = release_unlinked_entry(&mut st, ino.0);
|
||||
let ino = st.handles.remove(&fh.0);
|
||||
let h = ino.and_then(|i| {
|
||||
let aw = st.active_writes.get_mut(&i)?;
|
||||
@@ -666,7 +688,7 @@ impl Filesystem for ProtonFs {
|
||||
}
|
||||
|
||||
fn unlink(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEmpty) {
|
||||
let parent = parent.0;
|
||||
let parent = self.tree(parent).0;
|
||||
if let Err(error) = self.core.require_writable(parent) {
|
||||
reply.error(error);
|
||||
return;
|
||||
@@ -686,7 +708,7 @@ impl Filesystem for ProtonFs {
|
||||
}
|
||||
|
||||
fn rmdir(&self, _req: &Request, parent: INodeNo, name: &OsStr, reply: ReplyEmpty) {
|
||||
let parent = parent.0;
|
||||
let parent = self.tree(parent).0;
|
||||
if let Err(error) = self.core.require_writable(parent) {
|
||||
reply.error(error);
|
||||
return;
|
||||
@@ -715,7 +737,7 @@ impl Filesystem for ProtonFs {
|
||||
_umask: u32,
|
||||
reply: ReplyEntry,
|
||||
) {
|
||||
let parent = parent.0;
|
||||
let parent = self.tree(parent).0;
|
||||
if let Err(error) = self.core.require_writable(parent) {
|
||||
reply.error(error);
|
||||
return;
|
||||
@@ -746,8 +768,8 @@ impl Filesystem for ProtonFs {
|
||||
flags: RenameFlags,
|
||||
reply: ReplyEmpty,
|
||||
) {
|
||||
let parent = parent.0;
|
||||
let newparent = newparent.0;
|
||||
let parent = self.tree(parent).0;
|
||||
let newparent = self.tree(newparent).0;
|
||||
if let Err(error) = self
|
||||
.core
|
||||
.require_writable(parent)
|
||||
@@ -780,6 +802,7 @@ impl Filesystem for ProtonFs {
|
||||
}
|
||||
|
||||
fn access(&self, _req: &Request, ino: INodeNo, mask: AccessFlags, reply: ReplyEmpty) {
|
||||
let ino = self.tree(ino);
|
||||
let state = self.core.state();
|
||||
let Some(entry) = state.entries.get(&ino.0) else {
|
||||
reply.error(Errno::ENOENT);
|
||||
@@ -793,6 +816,7 @@ impl Filesystem for ProtonFs {
|
||||
}
|
||||
|
||||
fn getxattr(&self, _req: &Request, ino: INodeNo, name: &OsStr, size: u32, reply: ReplyXattr) {
|
||||
let ino = self.tree(ino);
|
||||
// Always off the dispatch loop: a miss goes to the wire, and a lister
|
||||
// that stats a directory issues one of these per file per advertised
|
||||
// name — inline, that serialized the whole mount behind ~186 ms of
|
||||
@@ -805,6 +829,7 @@ impl Filesystem for ProtonFs {
|
||||
}
|
||||
|
||||
fn listxattr(&self, _req: &Request, ino: INodeNo, size: u32, reply: ReplyXattr) {
|
||||
let ino = self.tree(ino);
|
||||
self.serve_listxattr(ino, size, reply);
|
||||
}
|
||||
|
||||
@@ -954,6 +979,9 @@ pub struct ProtonFs {
|
||||
core: Core,
|
||||
uid: u32,
|
||||
gid: u32,
|
||||
/// The tree inode of this session's root, which the kernel knows as
|
||||
/// [`ROOT_INO`].
|
||||
root: u64,
|
||||
}
|
||||
|
||||
/// How many times a write `open` will re-read its base after a `release`
|
||||
@@ -994,59 +1022,55 @@ fn build_listing(st: &State, ino: u64) -> DirListing {
|
||||
/// The cookie handed back is the index of the *next* entry, so a resumed call
|
||||
/// continues exactly where the last one stopped — which is only true because
|
||||
/// the listing behind those indexes is a snapshot (bugs.md B43).
|
||||
fn reply_listing(listing: &DirListing, offset: u64, mut reply: ReplyDirectory) {
|
||||
fn reply_listing(listing: &DirListing, offset: u64, root: u64, mut reply: ReplyDirectory) {
|
||||
for (i, (ino, is_dir, name)) in listing.iter().enumerate().skip(offset as usize) {
|
||||
let ft = if *is_dir {
|
||||
FileType::Directory
|
||||
} else {
|
||||
FileType::RegularFile
|
||||
};
|
||||
if reply.add(INodeNo(*ino), (i + 1) as u64, ft, name) {
|
||||
if reply.add(kernel_ino(root, *ino), (i + 1) as u64, ft, name) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
reply.ok();
|
||||
}
|
||||
|
||||
/// The inode the kernel knows tree inode `ino` by, in a session rooted at
|
||||
/// `root`. The inverse of [`ProtonFs::tree`].
|
||||
pub(crate) fn kernel_ino(root: u64, ino: u64) -> INodeNo {
|
||||
INodeNo(if ino == root { ROOT_INO } else { ino })
|
||||
}
|
||||
|
||||
pub(crate) fn access_allowed(is_dir: bool, access: Access, mask: AccessFlags) -> bool {
|
||||
(!mask.contains(AccessFlags::W_OK) || access.writable())
|
||||
&& (!mask.contains(AccessFlags::X_OK) || is_dir)
|
||||
}
|
||||
|
||||
impl ProtonFs {
|
||||
/// Build the filesystem rooted at `root` (the user's My Files folder).
|
||||
/// Build the filesystem rooted at `root` (the user's My Files folder, or
|
||||
/// the folder an on-demand location shows).
|
||||
pub(super) fn new(core: Core, root: Node) -> Self {
|
||||
{
|
||||
let mut st = core.state();
|
||||
if let Err(e) = st.db.upsert_node(&root) {
|
||||
warn!(uid = %root.uid, error = %e, "db upsert root failed");
|
||||
}
|
||||
st.by_uid.insert(root.uid.clone(), ROOT_INO);
|
||||
// The filesystem root is always owned — it must never be
|
||||
// classified as a shared root, even when the API returns
|
||||
// membership data (which happens when the owner shares the
|
||||
// folder with someone else). Clean up any stale share_access
|
||||
// row that a previous downgrade_all_share_access may have
|
||||
// written for this uid, so hydrate_access cannot pick it up.
|
||||
let access = Access::Owner;
|
||||
st.share_access.remove(&root.uid);
|
||||
let _ = st.db.delete_share_access(&root.uid);
|
||||
st.entries.insert(
|
||||
ROOT_INO,
|
||||
Entry {
|
||||
uid: root.uid.clone(),
|
||||
parent: ROOT_INO,
|
||||
node: root,
|
||||
access,
|
||||
lookup_count: 1,
|
||||
open_count: 0,
|
||||
unlinked: false,
|
||||
},
|
||||
);
|
||||
}
|
||||
let root = core.state().install_root(root);
|
||||
let _ = core.root.set(root);
|
||||
// SAFETY: geteuid/getegid are infallible and have no preconditions.
|
||||
let (uid, gid) = unsafe { (libc::geteuid(), libc::getegid()) };
|
||||
Self { core, uid, gid }
|
||||
Self {
|
||||
core,
|
||||
uid,
|
||||
gid,
|
||||
root,
|
||||
}
|
||||
}
|
||||
|
||||
/// The tree inode the kernel means by `ino`: this session's root for
|
||||
/// [`ROOT_INO`], and every other inode as it is.
|
||||
fn tree(&self, ino: INodeNo) -> INodeNo {
|
||||
if ino.0 == ROOT_INO {
|
||||
INodeNo(self.root)
|
||||
} else {
|
||||
ino
|
||||
}
|
||||
}
|
||||
|
||||
/// The body of [`Filesystem::lookup`], on whichever thread ends up serving it.
|
||||
@@ -1155,7 +1179,7 @@ impl ProtonFs {
|
||||
let st = self.core.state();
|
||||
let listing = build_listing(&st, ino);
|
||||
drop(st);
|
||||
reply_listing(&listing, offset, reply);
|
||||
reply_listing(&listing, offset, self.root, reply);
|
||||
}
|
||||
|
||||
fn attr(&self, ino: u64, node: &Node, access: Access) -> FileAttr {
|
||||
@@ -1168,7 +1192,7 @@ impl ProtonFs {
|
||||
let mtime = unix_secs(node.modification_time);
|
||||
let crtime = unix_secs(node.creation_time);
|
||||
FileAttr {
|
||||
ino: INodeNo(ino),
|
||||
ino: kernel_ino(self.root, ino),
|
||||
size,
|
||||
blocks: size.div_ceil(512),
|
||||
atime: mtime,
|
||||
@@ -1193,7 +1217,7 @@ impl ProtonFs {
|
||||
/// open.
|
||||
fn serve_open(&self, ino: INodeNo, flags: OpenFlags, reply: ReplyOpen) {
|
||||
let write_requested = flags.acc_mode() != OpenAccMode::O_RDONLY;
|
||||
let (uid, base_mtime, base_size, base_revision_id) = {
|
||||
let (uid, base_mtime, base_size, provisional, base_revision_id) = {
|
||||
let mut st = self.core.state();
|
||||
match st.entries.get_mut(&ino.0) {
|
||||
Some(e) if e.node.is_file() => {
|
||||
@@ -1206,6 +1230,14 @@ impl ProtonFs {
|
||||
e.uid.clone(),
|
||||
e.node.modification_time,
|
||||
node_size(&e.node),
|
||||
// Still the cheap enumeration's ciphertext size (B12).
|
||||
matches!(
|
||||
&e.node.kind,
|
||||
NodeKind::File {
|
||||
claimed_size: None,
|
||||
..
|
||||
}
|
||||
),
|
||||
node_revision_id(&e.node),
|
||||
)
|
||||
}
|
||||
@@ -1251,6 +1283,21 @@ impl ProtonFs {
|
||||
// `reply` is consumed by whichever arm answers, so the loop yields an
|
||||
// outcome and the answer is sent once, after it.
|
||||
let outcome = 'install: {
|
||||
// The handle starts at the base's size. A provisional one is the
|
||||
// ciphertext size, so the file would grow by the encryption overhead
|
||||
// and reading the base would fail (bugs.md B100). A queued revision
|
||||
// stamps a real size, so this only goes to the network for a file
|
||||
// the listing has not sized yet.
|
||||
let base_size = match provisional && self.core.pending_blob(&uid).is_none() {
|
||||
false => base_size,
|
||||
true => match self.core.resolve_provisional_size(ino.0, &uid) {
|
||||
Some(size) => size,
|
||||
None => {
|
||||
error!(%uid, "could not settle the size of the base for a write open");
|
||||
break 'install Err(Errno::EIO);
|
||||
}
|
||||
},
|
||||
};
|
||||
for attempt in 0..OPEN_BASE_ATTEMPTS {
|
||||
let pending_base = self.core.pending.lock().get(&uid).cloned();
|
||||
if pending_base.as_ref().is_some_and(|p| !p.meta.complete) {
|
||||
@@ -1398,7 +1445,7 @@ impl ProtonFs {
|
||||
len,
|
||||
base_size,
|
||||
base_mtime,
|
||||
complete: authored == [(0, len)],
|
||||
complete: StagedWrite::covers(&authored, len),
|
||||
authored,
|
||||
based_on: self
|
||||
.core
|
||||
@@ -1427,6 +1474,12 @@ impl ProtonFs {
|
||||
return;
|
||||
}
|
||||
if let Err(e) = self.core.ensure_children(parent) {
|
||||
// The parent was looked up a moment ago, so ENOENT here means it
|
||||
// left the tree in between. Log it: the caller only sees "no such
|
||||
// file" on a create.
|
||||
if e == Errno::ENOENT {
|
||||
warn!(parent, name, "create: the parent folder is no longer known");
|
||||
}
|
||||
reply.error(e);
|
||||
return;
|
||||
}
|
||||
@@ -1435,6 +1488,10 @@ impl ProtonFs {
|
||||
match st.entries.get(&parent) {
|
||||
Some(e) => e.uid.clone(),
|
||||
None => {
|
||||
warn!(
|
||||
parent,
|
||||
name, "create: the parent folder left the tree during its listing"
|
||||
);
|
||||
reply.error(Errno::ENOENT);
|
||||
return;
|
||||
}
|
||||
@@ -1454,67 +1511,58 @@ impl ProtonFs {
|
||||
//
|
||||
// A parent that is itself still queued forces the same path even when we
|
||||
// are online: the API has no folder to put this in yet.
|
||||
let node =
|
||||
if !transient && self.core.online.load(Ordering::Relaxed) && !is_local_uid(&parent_uid)
|
||||
{
|
||||
if let Err(error) = self.core.require_uid_writable(&parent_uid) {
|
||||
reply.error(error);
|
||||
//
|
||||
// A create that set out online and lost the network on the way lands in
|
||||
// the queue too, so a Wi-Fi drop costs the caller a wait, never an EIO.
|
||||
let minted = if !transient && self.core.is_online() && !is_local_uid(&parent_uid) {
|
||||
if let Err(error) = self.core.require_uid_writable(&parent_uid) {
|
||||
reply.error(error);
|
||||
return;
|
||||
}
|
||||
// Create an empty file on the remote so it has a real uid immediately;
|
||||
// written bytes are buffered and sealed as a new revision on close.
|
||||
match self.core.block_on_bounded(self.core.drive.upload_file(
|
||||
&parent_uid,
|
||||
name,
|
||||
media_type_for(name),
|
||||
b"",
|
||||
)) {
|
||||
// A file that exists remotely but could not be read back is
|
||||
// queued, and the queued create finds it by name and adopts it.
|
||||
Ok(new_uid) => self.core.fetch_minted_node(&new_uid, name),
|
||||
Err(e) if self.core.lost_link(&e, "create") => None,
|
||||
Err(e) => {
|
||||
error!(%parent_uid, name, error = %e, "create file failed");
|
||||
reply.error(Errno::EIO);
|
||||
return;
|
||||
}
|
||||
// Create an empty file on the remote so it has a real uid immediately;
|
||||
// written bytes are buffered and sealed as a new revision on close.
|
||||
let new_uid = match self.core.rt.block_on(self.core.client.upload_file(
|
||||
&parent_uid,
|
||||
name,
|
||||
media_type_for(name),
|
||||
b"",
|
||||
)) {
|
||||
Ok(u) => u,
|
||||
Err(e) => {
|
||||
error!(%parent_uid, name, error = %e, "create file failed");
|
||||
reply.error(Errno::EIO);
|
||||
return;
|
||||
}
|
||||
};
|
||||
match self.core.fetch_node(&new_uid) {
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
reply.error(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let node = match minted {
|
||||
Some(node) => node,
|
||||
None => match self
|
||||
.core
|
||||
.queue_local_node(&parent_uid, name, false, transient)
|
||||
{
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
reply.error(e);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
match self
|
||||
.core
|
||||
.queue_local_node(&parent_uid, name, false, transient)
|
||||
{
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
reply.error(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
let new_uid = node.uid.clone();
|
||||
// The base this handle writes over is the node as it actually exists —
|
||||
// the empty file the server just minted — so its modification time comes
|
||||
// from the node, never from the local clock. `queue_revision` turns this
|
||||
// into `StagedWrite::based_on` and the drain compares that against the
|
||||
// remote: a `now_secs()` here differs from the server's stamp by however
|
||||
// long the create round-trip took, so the first write to a brand-new file
|
||||
// conflicts with its own create whenever the second happens to tick in
|
||||
// between.
|
||||
let base_mtime = node.modification_time;
|
||||
},
|
||||
};
|
||||
let (file, path) = match self.core.cache.create_scratch() {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
error!(%new_uid, error = %e, "create scratch file failed");
|
||||
error!(uid = %node.uid, error = %e, "create scratch file failed");
|
||||
reply.error(Errno::EIO);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let mut st = self.core.state();
|
||||
let ino = st.intern(parent, node);
|
||||
let ino = st.intern(parent, node.clone());
|
||||
if let Some(entry) = st.entries.get_mut(&ino) {
|
||||
entry.open_count = entry.open_count.saturating_add(1);
|
||||
}
|
||||
@@ -1525,23 +1573,10 @@ impl ProtonFs {
|
||||
}
|
||||
let fh = st.next_fh;
|
||||
st.next_fh += 1;
|
||||
let aw = st.active_writes.entry(ino).or_insert_with(|| {
|
||||
// A brand-new file: empty base, everything written is authored.
|
||||
WriteHandle {
|
||||
ino,
|
||||
uid: new_uid,
|
||||
file: Arc::new(file),
|
||||
path,
|
||||
written: Intervals::default(),
|
||||
len: 0,
|
||||
base_size: 0,
|
||||
base_mtime,
|
||||
// A brand-new file has no sealed remote revision to conflict with.
|
||||
base_revision_id: None,
|
||||
dirty: false,
|
||||
open_count: 0,
|
||||
}
|
||||
});
|
||||
let aw = st
|
||||
.active_writes
|
||||
.entry(ino)
|
||||
.or_insert_with(|| WriteHandle::created(ino, &node, file, path));
|
||||
aw.open_count += 1;
|
||||
st.handles.insert(fh, ino);
|
||||
let entry = st.entries.get(&ino).unwrap();
|
||||
@@ -1584,39 +1619,36 @@ impl ProtonFs {
|
||||
// As in `create`: offline — or under a parent that is itself still
|
||||
// queued — the folder becomes a placeholder that the drain turns into a
|
||||
// real one (offline.md Phase 3b).
|
||||
let node = if self.core.online.load(Ordering::Relaxed) && !is_local_uid(&parent_uid) {
|
||||
// Losing the network on the way queues it the same way.
|
||||
let minted = if self.core.is_online() && !is_local_uid(&parent_uid) {
|
||||
if let Err(error) = self.core.require_uid_writable(&parent_uid) {
|
||||
reply.error(error);
|
||||
return;
|
||||
}
|
||||
let new_uid =
|
||||
match self
|
||||
.core
|
||||
.rt
|
||||
.block_on(self.core.client.create_folder(&parent_uid, name, now))
|
||||
{
|
||||
Ok(u) => u,
|
||||
Err(e) => {
|
||||
error!(%parent_uid, name, error = %e, "create folder failed");
|
||||
reply.error(Errno::EIO);
|
||||
return;
|
||||
}
|
||||
};
|
||||
match self.core.fetch_node(&new_uid) {
|
||||
Ok(n) => n,
|
||||
match self
|
||||
.core
|
||||
.block_on_bounded(self.core.drive.create_folder(&parent_uid, name, now))
|
||||
{
|
||||
Ok(new_uid) => self.core.fetch_minted_node(&new_uid, name),
|
||||
Err(e) if self.core.lost_link(&e, "mkdir") => None,
|
||||
Err(e) => {
|
||||
reply.error(e);
|
||||
error!(%parent_uid, name, error = %e, "create folder failed");
|
||||
reply.error(Errno::EIO);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match self.core.queue_local_node(&parent_uid, name, true, false) {
|
||||
None
|
||||
};
|
||||
let node = match minted {
|
||||
Some(node) => node,
|
||||
None => match self.core.queue_local_node(&parent_uid, name, true, false) {
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
reply.error(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
let mut st = self.core.state();
|
||||
let local = is_local_uid(&node.uid);
|
||||
@@ -1706,12 +1738,18 @@ impl ProtonFs {
|
||||
if let Some(uid) = unlinked_uid
|
||||
&& release_can_discard_unlinked(&self.core.db, &uid)
|
||||
{
|
||||
// Ops withdrawn at unlink left the bytes this handle read behind,
|
||||
// owned by no op any more; they go with the last handle.
|
||||
let withdrawn = self.core.pending_blob(&uid);
|
||||
if let Err(error) = self.core.discard_queued_ops(&uid) {
|
||||
warn!(%uid, ?error, "queued-op cleanup failed; retaining unlinked state");
|
||||
} else {
|
||||
if let Err(error) = self.core.db.delete_node(&uid) {
|
||||
warn!(%uid, %error, "deleting released unlinked node state failed");
|
||||
}
|
||||
if let Some(blob) = withdrawn {
|
||||
self.core.cache.discard_staged(&blob);
|
||||
}
|
||||
self.core.cache.evict(&uid);
|
||||
self.core.evict_reader(&uid);
|
||||
}
|
||||
@@ -1916,12 +1954,14 @@ impl ProtonFs {
|
||||
// now and this handle's is stale. A retry resolves it.
|
||||
Ok(false) => {
|
||||
warn!(%uid, name, newname, "queued create vanished under a rename");
|
||||
self.core.restore_replaced(victim.as_ref(), newname);
|
||||
self.core
|
||||
.restore_replaced(victim.as_ref(), &new_parent_uid, newname);
|
||||
reply.error(Errno::EBUSY);
|
||||
}
|
||||
Err(e) => {
|
||||
error!(%uid, error = %e, "rewriting a queued create's target failed");
|
||||
self.core.restore_replaced(victim.as_ref(), newname);
|
||||
self.core
|
||||
.restore_replaced(victim.as_ref(), &new_parent_uid, newname);
|
||||
reply.error(Errno::EIO);
|
||||
}
|
||||
}
|
||||
@@ -1932,12 +1972,11 @@ impl ProtonFs {
|
||||
// take the synchronous path, so a genuine API refusal (permissions, a
|
||||
// name clash) surfaces to the caller instead of becoming a queued op
|
||||
// that can only ever conflict.
|
||||
if rename_needs_queue(
|
||||
self.core.online.load(Ordering::Relaxed),
|
||||
is_local_uid(&new_parent_uid),
|
||||
newparent != parent,
|
||||
newname != name,
|
||||
) {
|
||||
// The queued rename, as a closure: the network going away under the
|
||||
// synchronous path below lands here too, with whatever half already
|
||||
// reached Drive. The op records the end state, so the drain finishes
|
||||
// the rest.
|
||||
let queue_it = |reply: ReplyEmpty| {
|
||||
let queued = if replacement_authorized {
|
||||
self.core.queue_rename_authorized(
|
||||
ino,
|
||||
@@ -1965,23 +2004,30 @@ impl ProtonFs {
|
||||
// and retain an empty destination readdir page.
|
||||
reply.ok();
|
||||
if let Some(notifier) = self.core.notifier.get() {
|
||||
let _ = notifier.inval_entry(INodeNo(parent), OsStr::new(&name));
|
||||
let _ = notifier.inval_entry(INodeNo(newparent), OsStr::new(&newname));
|
||||
let _ = notifier.inval_inode(INodeNo(parent), 0, 0);
|
||||
let _ = notifier.inval_entry(parent, OsStr::new(&name));
|
||||
let _ = notifier.inval_entry(newparent, OsStr::new(&newname));
|
||||
let _ = notifier.inval_inode(parent);
|
||||
if newparent != parent {
|
||||
let _ = notifier.inval_inode(INodeNo(newparent), 0, 0);
|
||||
let _ = notifier.inval_inode(newparent);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
self.core.restore_replaced(victim.as_ref(), newname);
|
||||
self.core
|
||||
.restore_replaced(victim.as_ref(), &new_parent_uid, newname);
|
||||
reply.error(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
if rename_needs_queue(
|
||||
self.core.is_online(),
|
||||
is_local_uid(&new_parent_uid),
|
||||
newparent != parent,
|
||||
newname != name,
|
||||
) {
|
||||
queue_it(reply);
|
||||
return;
|
||||
}
|
||||
// Rename first if both halves change. Moving first makes the encrypted
|
||||
// name requirements stale and repeatedly failed with InvalidRequirements.
|
||||
// A failure past this point has already trashed any node the rename was
|
||||
// replacing, so put it back rather than leaving the caller with neither
|
||||
// the source moved nor the destination intact.
|
||||
@@ -1991,64 +2037,37 @@ impl ProtonFs {
|
||||
&old_parent_uid,
|
||||
&new_parent_uid,
|
||||
) {
|
||||
self.core.restore_replaced(victim.as_ref(), newname);
|
||||
self.core
|
||||
.restore_replaced(victim.as_ref(), &new_parent_uid, newname);
|
||||
reply.error(error);
|
||||
return;
|
||||
}
|
||||
if newname != name
|
||||
&& let Err(e) = self
|
||||
.core
|
||||
.rt
|
||||
.block_on(self.core.client.rename_node(&uid, newname, None))
|
||||
{
|
||||
// A move that also renames is one request, so the name only has to be
|
||||
// free in the destination, and the reply never stands for half of it.
|
||||
let renamed = if newparent != parent {
|
||||
let target = (newname != name).then_some(newname);
|
||||
self.core.move_rename_remote(&uid, &new_parent_uid, target)
|
||||
} else if newname != name {
|
||||
self.core.rename_remote(&uid, newname)
|
||||
} else {
|
||||
Ok(())
|
||||
};
|
||||
if let Err(e) = renamed {
|
||||
if self.core.lost_link(&e, "rename") {
|
||||
queue_it(reply);
|
||||
return;
|
||||
}
|
||||
error!(%uid, error = %e, "rename failed");
|
||||
self.core.restore_replaced(victim.as_ref(), newname);
|
||||
self.core
|
||||
.restore_replaced(victim.as_ref(), &new_parent_uid, newname);
|
||||
reply.error(Errno::EIO);
|
||||
return;
|
||||
}
|
||||
if newparent != parent {
|
||||
let mut attempts = 0u32;
|
||||
let moved = loop {
|
||||
match self
|
||||
.core
|
||||
.rt
|
||||
.block_on(self.core.client.move_node(&uid, &new_parent_uid))
|
||||
{
|
||||
Ok(()) => break Ok(()),
|
||||
Err(e)
|
||||
if newname != name
|
||||
&& api_code(&e) == Some(ResponseCode::InvalidRequirements)
|
||||
&& attempts < 20 =>
|
||||
{
|
||||
attempts += 1;
|
||||
// Requirement propagation routinely takes longer than
|
||||
// three seconds. Keep a bounded ten-second window; a
|
||||
// successful POSIX reply must mean both remote halves
|
||||
// landed, not merely that the second half was queued.
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
}
|
||||
Err(e) => break Err(e),
|
||||
}
|
||||
};
|
||||
if let Err(e) = moved {
|
||||
error!(%uid, attempts, error = %e, "move after rename failed");
|
||||
// The rename half landed in the source directory.
|
||||
let mut state = self.core.state();
|
||||
let old_parent_uid = state
|
||||
.entries
|
||||
.get(&parent)
|
||||
.map(|entry| entry.uid.clone())
|
||||
.unwrap_or_else(|| uid.clone());
|
||||
state.rename_in_place(ino, parent, &old_parent_uid, newname);
|
||||
drop(state);
|
||||
self.core.restore_replaced(victim.as_ref(), newname);
|
||||
reply.error(Errno::EIO);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Through the guard, so the database has both folders unlisted before
|
||||
// the reply. The next lookup rebuilds a folder still marked listed from
|
||||
// the database, which did not have the move yet (docs/BUGS.md B135).
|
||||
self.core
|
||||
.state
|
||||
.lock()
|
||||
.state()
|
||||
.relocate(ino, parent, newparent, &new_parent_uid, newname);
|
||||
reply.ok();
|
||||
}
|
||||
@@ -2092,7 +2111,12 @@ impl ProtonFs {
|
||||
// it just means its queued creation is no longer wanted. This works
|
||||
// offline, which the remote path below cannot (offline.md Phase 3b).
|
||||
if is_local_uid(&uid) {
|
||||
if !open_now && let Err(error) = self.core.discard_queued_ops(&uid) {
|
||||
let dropped = if open_now {
|
||||
self.core.withdraw_queued_ops(&uid)
|
||||
} else {
|
||||
self.core.discard_queued_ops(&uid)
|
||||
};
|
||||
if let Err(error) = dropped {
|
||||
reply.error(error);
|
||||
return;
|
||||
}
|
||||
@@ -2103,12 +2127,14 @@ impl ProtonFs {
|
||||
}
|
||||
// Offline: queue it. Trashing is the one mutation a user expects to work
|
||||
// regardless — the file is gone from their point of view the moment the
|
||||
// command returns (offline.md Phase 3b).
|
||||
if !self.core.online.load(Ordering::Relaxed) {
|
||||
match self.core.queue_trash(&uid, name) {
|
||||
Ok(()) => reply.ok(),
|
||||
Err(e) => reply.error(e),
|
||||
}
|
||||
// command returns (offline.md Phase 3b). The same when the network goes
|
||||
// away under the remote call below.
|
||||
let queue_it = |reply: ReplyEmpty| match self.core.queue_trash(&uid, name) {
|
||||
Ok(()) => reply.ok(),
|
||||
Err(e) => reply.error(e),
|
||||
};
|
||||
if !self.core.is_online() {
|
||||
queue_it(reply);
|
||||
return;
|
||||
}
|
||||
if let Err(e) = self.core.require_uid_writable(&uid) {
|
||||
@@ -2117,10 +2143,13 @@ impl ProtonFs {
|
||||
}
|
||||
if let Err(e) = self
|
||||
.core
|
||||
.rt
|
||||
.block_on(self.core.client.trash_nodes(std::slice::from_ref(&uid)))
|
||||
.block_on_bounded(self.core.drive.trash_nodes(std::slice::from_ref(&uid)))
|
||||
.and_then(batch::into_unit)
|
||||
{
|
||||
if self.core.lost_link(&e, "trash") {
|
||||
queue_it(reply);
|
||||
return;
|
||||
}
|
||||
error!(%uid, error = %e, "trash failed");
|
||||
self.core
|
||||
.log_activity(ActivityKind::Trash, name, e.to_string(), false);
|
||||
@@ -2131,7 +2160,13 @@ impl ProtonFs {
|
||||
// Otherwise an EIO response still made the path disappear locally.
|
||||
self.core.hidden.lock().insert(uid.clone());
|
||||
self.core.state().forget_or_unlink(&uid);
|
||||
if !open_now {
|
||||
if open_now {
|
||||
// The open handle keeps the staged bytes; the drain must not send
|
||||
// them to a node that is already in the trash (docs/BUGS.md B106).
|
||||
if let Err(error) = self.core.withdraw_queued_ops(&uid) {
|
||||
warn!(%uid, ?error, "remote trash landed but queued-op cleanup failed");
|
||||
}
|
||||
} else {
|
||||
if let Err(error) = self.core.discard_queued_ops(&uid) {
|
||||
warn!(%uid, ?error, "remote trash landed but queued-op cleanup failed");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,316 @@
|
||||
//! Noticing that the network went away mid-session, and that it came back.
|
||||
//!
|
||||
//! The mount used to learn it was offline only at startup: a daemon that had
|
||||
//! connected once kept `online` true for the rest of its life, so a Wi-Fi drop
|
||||
//! turned every create, mkdir, delete and rename into a two-minute wait for an
|
||||
//! `EIO`, while the queue that exists for exactly this sat unused. Now any
|
||||
//! remote call a handler waits on reports a failure to reach Proton through
|
||||
//! [`Core::lost_link`], which flips the mount offline; the handler then takes its
|
||||
//! queued path, and the probe thread here flips it back when Proton answers.
|
||||
|
||||
use std::future::Future;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use parking_lot::{Condvar, Mutex};
|
||||
use proton_drive_rs::proton_sdk::api::ResponseCode;
|
||||
use proton_drive_rs::proton_sdk::error::{ProtonApiError, ProtonError};
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
use crate::{Core, ONLINE_PROBE_MAX, ONLINE_PROBE_MIN};
|
||||
|
||||
/// How long a remote call a FUSE handler is waiting on may take before the link
|
||||
/// is treated as down.
|
||||
///
|
||||
/// The SDK's own budget for one request is four attempts of up to 30 seconds
|
||||
/// each, so a dead link held the caller for about two minutes. A metadata call
|
||||
/// that has had no answer in 20 seconds is not going to be one the user wants to
|
||||
/// keep waiting for; queueing the change is the better answer.
|
||||
pub(crate) const INTERACTIVE_CALL_TIMEOUT: Duration = Duration::from_secs(20);
|
||||
|
||||
/// How long one probe may wait for an answer. A probe is a single small request,
|
||||
/// and a probe stuck in the SDK's retry budget would delay noticing the link is
|
||||
/// back by as long as it waits.
|
||||
const PROBE_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
/// First delay between probes after a drop seen mid-session, and the ceiling it
|
||||
/// doubles to while the outage is young. A blip should end in seconds, not in
|
||||
/// the minutes [`ONLINE_PROBE_MAX`] allows.
|
||||
const PROBE_AFTER_DROP: Duration = Duration::from_secs(2);
|
||||
const PROBE_YOUNG_MAX: Duration = Duration::from_secs(30);
|
||||
/// How long an outage counts as young. Past it the probe backs off to
|
||||
/// [`ONLINE_PROBE_MAX`], because a laptop can sit offline for days and each
|
||||
/// probe is a real API round trip.
|
||||
const YOUNG_OUTAGE: Duration = Duration::from_secs(10 * 60);
|
||||
|
||||
/// How often the idle probe thread checks for teardown while the link is up.
|
||||
const IDLE_CHECK: Duration = Duration::from_secs(1);
|
||||
|
||||
/// The shared half of the online flag: when the link went down, and a condvar
|
||||
/// that fires on every change so waiters need not poll.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Link {
|
||||
lost_at: Mutex<Option<Instant>>,
|
||||
changed: Condvar,
|
||||
/// Folders listed from the DB alone while offline, to relist once back.
|
||||
pub(crate) stale_listings: Mutex<std::collections::HashSet<u64>>,
|
||||
}
|
||||
|
||||
/// Whether a failed call failed because Proton could not be reached, rather
|
||||
/// than because it answered and refused.
|
||||
///
|
||||
/// A transport error is the network. So is a request timeout: the SDK reports
|
||||
/// Proton's own "try again" codes that way, and [`no_answer`] reports our
|
||||
/// deadline that way. Anything else Proton actually said, and says again on a
|
||||
/// retry, so it must reach the caller rather than be queued.
|
||||
pub(crate) fn is_network_error(e: &ProtonError) -> bool {
|
||||
match e {
|
||||
ProtonError::Transport(_) => true,
|
||||
ProtonError::Api(api) => {
|
||||
matches!(
|
||||
api.code,
|
||||
ResponseCode::RequestTimeout | ResponseCode::Timeout
|
||||
)
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// The error a call reports when it runs past `after` without an answer.
|
||||
pub(crate) fn no_answer(after: Duration) -> ProtonError {
|
||||
ProtonError::Api(ProtonApiError {
|
||||
code: ResponseCode::RequestTimeout,
|
||||
http_status: 408,
|
||||
message: format!("no answer within {}s", after.as_secs()),
|
||||
details: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Run a remote call, giving up after [`INTERACTIVE_CALL_TIMEOUT`].
|
||||
pub(crate) async fn bounded<T>(
|
||||
call: impl Future<Output = Result<T, ProtonError>>,
|
||||
) -> Result<T, ProtonError> {
|
||||
match tokio::time::timeout(INTERACTIVE_CALL_TIMEOUT, call).await {
|
||||
Ok(result) => result,
|
||||
Err(_) => Err(no_answer(INTERACTIVE_CALL_TIMEOUT)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The next delay between probes, given the current one and how long the
|
||||
/// outage has lasted.
|
||||
fn next_probe_delay(delay: Duration, outage: Duration) -> Duration {
|
||||
let cap = if outage < YOUNG_OUTAGE {
|
||||
PROBE_YOUNG_MAX
|
||||
} else {
|
||||
ONLINE_PROBE_MAX
|
||||
};
|
||||
(delay * 2).min(cap)
|
||||
}
|
||||
|
||||
impl Core {
|
||||
/// Whether the mount currently believes Proton is reachable.
|
||||
pub(crate) fn is_online(&self) -> bool {
|
||||
self.online.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Run a remote call a caller is waiting on, giving up after
|
||||
/// [`INTERACTIVE_CALL_TIMEOUT`]. The timer is built inside the runtime: a
|
||||
/// FUSE worker has no reactor of its own.
|
||||
pub(crate) fn block_on_bounded<T>(
|
||||
&self,
|
||||
call: impl Future<Output = Result<T, ProtonError>>,
|
||||
) -> Result<T, ProtonError> {
|
||||
self.rt.block_on(bounded(call))
|
||||
}
|
||||
|
||||
/// Report a failed remote call. Returns whether it failed for want of a
|
||||
/// network, in which case the mount is now offline and the caller should
|
||||
/// take its queued path instead of failing.
|
||||
pub(crate) fn lost_link(&self, e: &ProtonError, what: &str) -> bool {
|
||||
if !is_network_error(e) {
|
||||
return false;
|
||||
}
|
||||
self.mark_offline(what, e);
|
||||
true
|
||||
}
|
||||
|
||||
/// Flip the mount offline and wake the probe. Only the first caller of an
|
||||
/// outage logs it; the rest are the same outage.
|
||||
pub(crate) fn mark_offline(&self, what: &str, error: &dyn std::fmt::Display) {
|
||||
let mut lost_at = self.link.lost_at.lock();
|
||||
if self.online.swap(false, Ordering::Relaxed) {
|
||||
*lost_at = Some(Instant::now());
|
||||
warn!(during = what, %error,
|
||||
"lost the connection to Proton; queueing changes until it returns");
|
||||
self.link.changed.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
/// Flip the mount back online, wake everything waiting for it, and let the
|
||||
/// queue drain.
|
||||
fn mark_online(&self) {
|
||||
{
|
||||
let mut lost_at = self.link.lost_at.lock();
|
||||
self.online.store(true, Ordering::Relaxed);
|
||||
match lost_at.take() {
|
||||
Some(since) => {
|
||||
info!(
|
||||
offline_secs = since.elapsed().as_secs(),
|
||||
"connection to Proton is back"
|
||||
)
|
||||
}
|
||||
None => info!("back online"),
|
||||
}
|
||||
self.link.changed.notify_all();
|
||||
}
|
||||
self.relist_offline_listings();
|
||||
// Anything written while offline is queued and waiting on exactly this,
|
||||
// including ops that failed on the way down and are sitting out a
|
||||
// backoff sized for a server that keeps refusing.
|
||||
if let Err(e) = self.db.retry_failed_ops_now(crate::now_millis()) {
|
||||
warn!(error = %e, "moving backed-off ops up after a reconnect failed");
|
||||
}
|
||||
self.wake_drain();
|
||||
}
|
||||
|
||||
/// Wait up to `budget` for the link to come back. Returns whether it is up.
|
||||
pub(crate) fn wait_online(&self, budget: Duration) -> bool {
|
||||
let deadline = Instant::now() + budget;
|
||||
let mut lost_at = self.link.lost_at.lock();
|
||||
while !self.is_online() {
|
||||
if self.shutdown.is_stopping()
|
||||
|| self
|
||||
.link
|
||||
.changed
|
||||
.wait_until(&mut lost_at, deadline)
|
||||
.timed_out()
|
||||
{
|
||||
return self.is_online();
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Wait out `interval` before a background poll of Proton, and while the
|
||||
/// link is down, until it is back. Returns as soon as it comes back, so the
|
||||
/// poll catches up on what changed during the outage at once instead of
|
||||
/// one interval later. A poll into a dead link would only wait out the
|
||||
/// SDK's retries and fail.
|
||||
pub(crate) async fn next_poll(&self, interval: Duration) {
|
||||
let due = Instant::now() + interval;
|
||||
let mut was_offline = false;
|
||||
loop {
|
||||
if !self.is_online() {
|
||||
was_offline = true;
|
||||
} else if was_offline || Instant::now() >= due {
|
||||
return;
|
||||
}
|
||||
tokio::time::sleep(IDLE_CHECK).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Watch the link for the life of the mount: sleep while it is up, and
|
||||
/// probe it back while it is down (offline.md Phase 1).
|
||||
///
|
||||
/// A mount that started from the cache is down from the first moment; one
|
||||
/// that started online goes down when [`Core::mark_offline`] says so.
|
||||
pub(crate) fn run_online_probe(&self) {
|
||||
// Only an offline start never had a chance to repair its share id.
|
||||
let mut repair_share_id = !self.is_online();
|
||||
loop {
|
||||
{
|
||||
let mut lost_at = self.link.lost_at.lock();
|
||||
while self.is_online() {
|
||||
if self.shutdown.is_stopping() {
|
||||
return;
|
||||
}
|
||||
self.link.changed.wait_for(&mut lost_at, IDLE_CHECK);
|
||||
}
|
||||
}
|
||||
if !self.probe_until_online() {
|
||||
return;
|
||||
}
|
||||
if std::mem::take(&mut repair_share_id) {
|
||||
self.repair_primary_share_id(&self.primary_root_uid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Probe until Proton answers, then flip the mount online. Returns `false`
|
||||
/// if the daemon is stopping.
|
||||
fn probe_until_online(&self) -> bool {
|
||||
let started = self.link.lost_at.lock().unwrap_or_else(Instant::now);
|
||||
let mut delay = if started.elapsed() < YOUNG_OUTAGE {
|
||||
PROBE_AFTER_DROP
|
||||
} else {
|
||||
ONLINE_PROBE_MIN
|
||||
};
|
||||
loop {
|
||||
if !self.shutdown.sleep(delay) {
|
||||
return false;
|
||||
}
|
||||
let probe = self.rt.block_on(async {
|
||||
match tokio::time::timeout(PROBE_TIMEOUT, self.drive.get_my_files_folder()).await {
|
||||
Ok(result) => result,
|
||||
Err(_) => Err(no_answer(PROBE_TIMEOUT)),
|
||||
}
|
||||
});
|
||||
match probe {
|
||||
Ok(root) => {
|
||||
{
|
||||
let root_ino = self.root_ino();
|
||||
let mut st = self.state();
|
||||
if let Some(e) = st.entries.get_mut(&root_ino) {
|
||||
e.node = root.clone();
|
||||
}
|
||||
}
|
||||
if let Err(e) = self.db.upsert_node(&root) {
|
||||
warn!(error = %e, "refresh root after reconnect failed");
|
||||
}
|
||||
self.mark_online();
|
||||
return true;
|
||||
}
|
||||
Err(e) => {
|
||||
debug!(error = %e, ?delay, "online probe failed; still offline");
|
||||
delay = next_probe_delay(delay, started.elapsed());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn api(code: ResponseCode) -> ProtonError {
|
||||
ProtonError::Api(ProtonApiError {
|
||||
code,
|
||||
http_status: 422,
|
||||
message: String::new(),
|
||||
details: None,
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_an_unanswered_call_counts_as_a_lost_link() {
|
||||
assert!(is_network_error(&no_answer(INTERACTIVE_CALL_TIMEOUT)));
|
||||
assert!(is_network_error(&api(ResponseCode::Timeout)));
|
||||
assert!(!is_network_error(&api(ResponseCode::AlreadyExists)));
|
||||
assert!(!is_network_error(&api(ResponseCode::InvalidRequirements)));
|
||||
assert!(!is_network_error(&ProtonError::invalid_operation("bug")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_young_outage_is_probed_often_and_an_old_one_rarely() {
|
||||
let young = Duration::from_secs(60);
|
||||
let old = YOUNG_OUTAGE + Duration::from_secs(1);
|
||||
assert_eq!(
|
||||
next_probe_delay(PROBE_AFTER_DROP, young),
|
||||
2 * PROBE_AFTER_DROP
|
||||
);
|
||||
assert_eq!(next_probe_delay(PROBE_YOUNG_MAX, young), PROBE_YOUNG_MAX);
|
||||
assert_eq!(next_probe_delay(PROBE_YOUNG_MAX, old), 2 * PROBE_YOUNG_MAX);
|
||||
assert_eq!(next_probe_delay(ONLINE_PROBE_MAX, old), ONLINE_PROBE_MAX);
|
||||
}
|
||||
}
|
||||
@@ -207,11 +207,11 @@ const ROOT_UID_KEY: &str = "root_uid";
|
||||
/// means an empty tree, and mounting that would show the user an empty Drive
|
||||
/// rather than an honest error.
|
||||
fn fetch_or_recall_root(
|
||||
client: &ProtonDriveClient,
|
||||
drive: &dyn DriveApi,
|
||||
rt: &tokio::runtime::Handle,
|
||||
db: &Db,
|
||||
) -> std::io::Result<(Node, bool)> {
|
||||
let err = match rt.block_on(client.get_my_files_folder()) {
|
||||
let err = match rt.block_on(drive.get_my_files_folder()) {
|
||||
Ok(root) => {
|
||||
if let Err(e) = db.set_state_str(ROOT_UID_KEY, &root.uid.to_string()) {
|
||||
warn!(error = %e, "persist root uid failed");
|
||||
@@ -276,13 +276,14 @@ pub(super) fn spawn_session(
|
||||
info!(mountpoint = %mountpoint.display(), "mounting Proton Drive location");
|
||||
let fs = ProtonFs::new(core.clone(), root);
|
||||
if core.primary {
|
||||
// Only My Files rehydrates the daemon-wide node cache. A secondary
|
||||
// inode space is rooted at one device subtree and must not import every
|
||||
// unrelated persisted node.
|
||||
// Only My Files rehydrates the daemon-wide node cache, once. A
|
||||
// secondary mount shares the tree it fills.
|
||||
core.hydrate();
|
||||
}
|
||||
let session = Session::new(fs, mountpoint, &config)?.spawn()?;
|
||||
let _ = core.notifier.set(session.notifier());
|
||||
let _ = core
|
||||
.notifier
|
||||
.set(KernelChannel::new(session.notifier(), core.root_ino()));
|
||||
core.session_live.store(true, Ordering::Release);
|
||||
Ok(session)
|
||||
}
|
||||
@@ -339,6 +340,45 @@ pub fn mount(
|
||||
control_socket: &Path,
|
||||
db: Arc<Db>,
|
||||
options: MountOptions,
|
||||
) -> std::io::Result<MountOutcome> {
|
||||
let drive = Arc::new(client.clone());
|
||||
mount_with(
|
||||
client,
|
||||
drive,
|
||||
rt,
|
||||
mountpoint,
|
||||
cache,
|
||||
control_socket,
|
||||
db,
|
||||
options,
|
||||
Host::Daemon,
|
||||
)
|
||||
}
|
||||
|
||||
/// What a mount runs in.
|
||||
pub(crate) enum Host {
|
||||
/// The daemon: stopped by SIGTERM or SIGINT, and indexing the user's home
|
||||
/// for the launcher prompt.
|
||||
Daemon,
|
||||
/// One of several simulated daemons in a test process (`sim`): stopped by
|
||||
/// a message on this channel, and with no home to index.
|
||||
#[cfg(test)]
|
||||
Simulation(std::sync::mpsc::Receiver<()>),
|
||||
}
|
||||
|
||||
/// [`mount`], reaching Drive through `drive` and hosted by `host`; `client`
|
||||
/// serves only what [`DriveApi`] does not cover.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn mount_with(
|
||||
client: ProtonDriveClient,
|
||||
drive: Arc<dyn DriveApi>,
|
||||
rt: tokio::runtime::Handle,
|
||||
mountpoint: &Path,
|
||||
cache: ContentCache,
|
||||
control_socket: &Path,
|
||||
db: Arc<Db>,
|
||||
options: MountOptions,
|
||||
host: Host,
|
||||
) -> std::io::Result<MountOutcome> {
|
||||
let MountOptions {
|
||||
username,
|
||||
@@ -349,7 +389,7 @@ pub fn mount(
|
||||
// Start the uptime clock here rather than at the first `pdfs diagnostics`,
|
||||
// so the age it reports is the daemon's own.
|
||||
super::diagnostics::start_clock();
|
||||
let (root, online) = fetch_or_recall_root(&client, &rt, &db)?;
|
||||
let (root, online) = fetch_or_recall_root(drive.as_ref(), &rt, &db)?;
|
||||
let scope = root.tree_event_scope_id();
|
||||
db.mount_upsert_my_files(&mountpoint.to_string_lossy(), &root.uid.to_string(), None)
|
||||
.map_err(|error| std::io::Error::other(format!("project My Files mount: {error}")))?;
|
||||
@@ -364,12 +404,14 @@ pub fn mount(
|
||||
|
||||
let paused_until = pause::load_paused_until(&db);
|
||||
let core = Core {
|
||||
client: client.clone(),
|
||||
drive,
|
||||
client,
|
||||
rt: rt.clone(),
|
||||
maintenance: Arc::new(Mutex::new(Default::default())),
|
||||
primary_root_uid: root.uid.clone(),
|
||||
primary: true,
|
||||
state: Arc::new(Mutex::new(State::new(db.clone(), share_access, 2))),
|
||||
state: Arc::new(Mutex::new(State::new(db.clone(), share_access))),
|
||||
root: Arc::new(OnceLock::new()),
|
||||
cache: Arc::new(cache),
|
||||
readers: Arc::new(Mutex::new(HashMap::new())),
|
||||
block_ring: Arc::new(Mutex::new(BlockRing::default())),
|
||||
@@ -383,6 +425,7 @@ pub fn mount(
|
||||
shared_generation: Arc::new(AtomicU64::new(0)),
|
||||
shared_refresh_deadlines: Arc::new(Mutex::new(SharedRefreshDeadlines::default())),
|
||||
online: Arc::new(AtomicBool::new(online)),
|
||||
link: Arc::new(Default::default()),
|
||||
sync_paused_until: Arc::new(AtomicI64::new(paused_until)),
|
||||
pending: Arc::new(Mutex::new(HashMap::new())),
|
||||
hidden: Arc::new(Mutex::new(HashSet::new())),
|
||||
@@ -390,6 +433,7 @@ pub fn mount(
|
||||
shutdown: Arc::new(crate::shutdown::Shutdown::default()),
|
||||
upload_times: Arc::new(Mutex::new(HashMap::new())),
|
||||
upload_cancel: Arc::new(Mutex::new(HashMap::new())),
|
||||
landing_uploads: Arc::new(AtomicU64::new(0)),
|
||||
timeline_refreshing: Arc::new(AtomicBool::new(false)),
|
||||
timeline_progress: Arc::new(Mutex::new(None)),
|
||||
similar_hashing: Arc::new(AtomicBool::new(false)),
|
||||
@@ -398,7 +442,6 @@ pub fn mount(
|
||||
trash_progress: Arc::new(tokio::sync::Notify::new()),
|
||||
conflict_notified: Arc::new(Mutex::new(HashSet::new())),
|
||||
sweep_mode,
|
||||
own_sealed_revs: Arc::new(Mutex::new(HashMap::new())),
|
||||
self_changes: Arc::new(Mutex::new(HashMap::new())),
|
||||
thumb_gen: Arc::new(Mutex::new(HashSet::new())),
|
||||
thumb_gen_budget: Arc::new(tokio::sync::Semaphore::new(
|
||||
@@ -411,11 +454,13 @@ pub fn mount(
|
||||
thumbnail_build_cancel: Arc::new(tokio::sync::Notify::new()),
|
||||
thumbnail_misses: Arc::new(Mutex::new(Default::default())),
|
||||
quota: Arc::new(Mutex::new(None)),
|
||||
quota_refreshing: Arc::new(AtomicBool::new(false)),
|
||||
size_upgrades: Arc::new(Mutex::new(HashMap::new())),
|
||||
size_waiters: Arc::new(Default::default()),
|
||||
notifier: Arc::new(OnceLock::new()),
|
||||
session_live: Arc::new(AtomicBool::new(false)),
|
||||
transfers: TransferRegistry::new(),
|
||||
events: Arc::new(Default::default()),
|
||||
indexing: Arc::new(AtomicBool::new(false)),
|
||||
sync_progress: Arc::new(Mutex::new(HashMap::new())),
|
||||
sync_tx,
|
||||
@@ -424,7 +469,7 @@ pub fn mount(
|
||||
states: Arc::new(StateRegistry::default()),
|
||||
};
|
||||
// Before anything can queue work against it: the drain thread below reaches
|
||||
// every mount's inode space through this registry, not through `core.state`.
|
||||
// every mount's session through this registry.
|
||||
core.register_state(mountpoint);
|
||||
core.transfers.set_limits(upload_limit, download_limit);
|
||||
|
||||
@@ -483,9 +528,10 @@ pub fn mount(
|
||||
);
|
||||
}
|
||||
|
||||
// Mounted from the cache: watch for the network coming back so the mount can
|
||||
// stop being read-only-ish without the user restarting the daemon.
|
||||
if !online {
|
||||
// Watch the link for the life of the mount: a mount from the cache waits for
|
||||
// the network to come back, and an online one can lose it at any moment and
|
||||
// must notice it coming back just the same.
|
||||
{
|
||||
let core = core.clone();
|
||||
workers.push(
|
||||
std::thread::Builder::new()
|
||||
@@ -496,7 +542,7 @@ pub fn mount(
|
||||
|
||||
// Keep the launcher prompt's "This computer" index fresh. Its own thread:
|
||||
// the walk is I/O-heavy and must never sit in front of a FUSE callback.
|
||||
{
|
||||
if matches!(host, Host::Daemon) {
|
||||
let db = core.db.clone();
|
||||
let indexing = core.indexing.clone();
|
||||
let transfers = core.transfers.clone();
|
||||
@@ -568,6 +614,22 @@ pub fn mount(
|
||||
}
|
||||
}
|
||||
}
|
||||
// Publishes what changes without one place to publish it from (status,
|
||||
// transfers, sync progress) to the front-ends subscribed on that socket.
|
||||
{
|
||||
let sampler_core = core.clone();
|
||||
let username = username.clone();
|
||||
let mountpoint = mountpoint.to_path_buf();
|
||||
match std::thread::Builder::new()
|
||||
.name("pdfs-events".into())
|
||||
.spawn(move || events::run_event_sampler(sampler_core, username, mountpoint))
|
||||
{
|
||||
Ok(handle) => workers.push(handle),
|
||||
// Not fatal: front-ends still get every published change, only
|
||||
// not the sampled ones.
|
||||
Err(error) => warn!(error = %error, "could not start the event sampler"),
|
||||
}
|
||||
}
|
||||
// The supervisor watches the pool and answers the systemd watchdog. Started
|
||||
// after the listener, because its liveness probe is a round trip over that
|
||||
// socket.
|
||||
@@ -604,7 +666,7 @@ pub fn mount(
|
||||
}
|
||||
}
|
||||
};
|
||||
rt.spawn(run_event_sync(client, scope, core.clone()));
|
||||
rt.spawn(run_event_sync(core.drive.clone(), scope, core.clone()));
|
||||
// The photos volume has its own event stream; without it the gallery only
|
||||
// learns about a phone-side deletion when the timeline goes stale.
|
||||
rt.spawn(run_photos_event_sync(core.clone()));
|
||||
@@ -623,30 +685,11 @@ pub fn mount(
|
||||
// are delivered onto the async runtime; bridge them onto a sync channel so
|
||||
// the loop below can react without blocking a worker thread. A bounded
|
||||
// channel of 1 is enough — we only need to know that *a* stop arrived.
|
||||
let (sig_tx, sig_rx) = std::sync::mpsc::sync_channel::<()>(1);
|
||||
rt.spawn(async move {
|
||||
let mut sigterm =
|
||||
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
warn!(error = %e, "install SIGTERM handler failed");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let mut sigint =
|
||||
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt()) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
warn!(error = %e, "install SIGINT handler failed");
|
||||
return;
|
||||
}
|
||||
};
|
||||
tokio::select! {
|
||||
_ = sigterm.recv() => info!("received SIGTERM"),
|
||||
_ = sigint.recv() => info!("received SIGINT"),
|
||||
}
|
||||
let _ = sig_tx.try_send(());
|
||||
});
|
||||
let sig_rx = match host {
|
||||
Host::Daemon => signal_stop(&rt),
|
||||
#[cfg(test)]
|
||||
Host::Simulation(rx) => rx,
|
||||
};
|
||||
|
||||
// Wait for whichever happens first: a stop signal (→ we unmount ourselves
|
||||
// via the lazy MNT_DETACH path, clean even mid-download), or the kernel
|
||||
@@ -703,6 +746,35 @@ pub fn mount(
|
||||
Ok(outcome)
|
||||
}
|
||||
|
||||
/// A channel that receives once SIGTERM or SIGINT arrives.
|
||||
fn signal_stop(rt: &tokio::runtime::Handle) -> std::sync::mpsc::Receiver<()> {
|
||||
let (sig_tx, sig_rx) = std::sync::mpsc::sync_channel::<()>(1);
|
||||
rt.spawn(async move {
|
||||
let mut sigterm =
|
||||
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
warn!(error = %e, "install SIGTERM handler failed");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let mut sigint =
|
||||
match tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt()) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
warn!(error = %e, "install SIGINT handler failed");
|
||||
return;
|
||||
}
|
||||
};
|
||||
tokio::select! {
|
||||
_ = sigterm.recv() => info!("received SIGTERM"),
|
||||
_ = sigint.recv() => info!("received SIGINT"),
|
||||
}
|
||||
let _ = sig_tx.try_send(());
|
||||
});
|
||||
sig_rx
|
||||
}
|
||||
|
||||
/// How long teardown waits for the FUSE worker pool before proceeding without
|
||||
/// it. Long enough for an in-flight block to finish, short enough that
|
||||
/// `systemctl --user stop` still returns inside `TimeoutStopSec`.
|
||||
|
||||
@@ -19,7 +19,7 @@ use std::time::{Duration, Instant};
|
||||
use base64::Engine as _;
|
||||
use pdfs_core::control::{
|
||||
FileThumbRequest, MappingProgress, PhotoItem, PhotoKind, PhotoLocation, PhotoThumb, PlaceInfo,
|
||||
ThumbnailBuildStatus, is_raw_image_name, is_thumbnail_image_name,
|
||||
ThumbnailBuildStatus, Topic, is_raw_image_name, is_thumbnail_image_name,
|
||||
};
|
||||
use pdfs_core::db::{self, StoredPhoto};
|
||||
use pdfs_core::{CoreError, CoreResult};
|
||||
@@ -1038,7 +1038,7 @@ impl Core {
|
||||
}
|
||||
let outcomes = self
|
||||
.rt
|
||||
.block_on(self.client.trash_nodes(uids))
|
||||
.block_on(self.drive.trash_nodes(uids))
|
||||
.map_err(|e| CoreError::from_api(&e, "trash"))?;
|
||||
let (trashed, failed) = pdfs_core::batch::split(outcomes);
|
||||
|
||||
@@ -2041,10 +2041,14 @@ impl Core {
|
||||
}
|
||||
let core = self.clone();
|
||||
self.rt.spawn(async move {
|
||||
if let Err(e) = core.refresh_timeline().await {
|
||||
warn!(error = %e, "background timeline refresh failed");
|
||||
}
|
||||
let refreshed = core.refresh_timeline().await;
|
||||
core.timeline_refreshing.store(false, Ordering::SeqCst);
|
||||
// Only a refresh that landed: a front-end re-reading after a failed
|
||||
// one would find the timeline still stale and start another.
|
||||
match refreshed {
|
||||
Ok(_) => core.events.publish(&[Topic::Photos]),
|
||||
Err(e) => warn!(error = %e, "background timeline refresh failed"),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -101,7 +101,7 @@ impl Core {
|
||||
match existing {
|
||||
Some(uid) => self
|
||||
.rt
|
||||
.block_on(self.client.upload_new_revision_from(
|
||||
.block_on(self.drive.upload_new_revision_from(
|
||||
&uid,
|
||||
Cursor::new(bytes),
|
||||
len,
|
||||
@@ -112,7 +112,7 @@ impl Core {
|
||||
.map_err(|e| CoreError::from_api(&e, "upload profile revision"))?,
|
||||
None => {
|
||||
self.rt
|
||||
.block_on(self.client.upload_file_replacing_draft_from(
|
||||
.block_on(self.drive.upload_file_replacing_draft_from(
|
||||
&dir_uid,
|
||||
PROFILE_FILE_NAME,
|
||||
"application/json",
|
||||
@@ -182,14 +182,14 @@ impl Core {
|
||||
) -> CoreResult<Option<NodeUid>> {
|
||||
let uids = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_folder_children_node_uids(parent_uid))
|
||||
.block_on(self.drive.enumerate_folder_children_node_uids(parent_uid))
|
||||
.map_err(|e| CoreError::from_api(&e, "list device root"))?;
|
||||
if uids.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let nodes = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_nodes(&uids))
|
||||
.block_on(self.drive.enumerate_nodes(&uids))
|
||||
.map_err(|e| CoreError::from_api(&e, "resolve device root children"))?;
|
||||
Ok(nodes
|
||||
.into_iter()
|
||||
@@ -210,7 +210,7 @@ impl Core {
|
||||
let uid = self
|
||||
.rt
|
||||
.block_on(
|
||||
self.client
|
||||
self.drive
|
||||
.create_folder(root_uid, PROFILE_DIR_NAME, Some(now_secs())),
|
||||
)
|
||||
.map_err(|e| CoreError::from_api(&e, "create profile folder"))?;
|
||||
@@ -241,7 +241,7 @@ impl Core {
|
||||
};
|
||||
let mut buf: Vec<u8> = Vec::new();
|
||||
self.rt
|
||||
.block_on(self.client.download_file_to(&uid, &mut buf))
|
||||
.block_on(self.drive.download_file_to(&uid, &mut buf))
|
||||
.map_err(|e| CoreError::from_api(&e, "download profile"))?;
|
||||
Profile::parse(&buf).map(Some).map_err(CoreError::invalid)
|
||||
}
|
||||
@@ -277,13 +277,13 @@ impl Core {
|
||||
|
||||
let uids = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_folder_children_node_uids(&root_uid))
|
||||
.block_on(self.drive.enumerate_folder_children_node_uids(&root_uid))
|
||||
.map_err(|e| CoreError::from_api(&e, "list device root"))?;
|
||||
let nodes = if uids.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
self.rt
|
||||
.block_on(self.client.enumerate_nodes(&uids))
|
||||
.block_on(self.drive.enumerate_nodes(&uids))
|
||||
.map_err(|e| CoreError::from_api(&e, "resolve device root children"))?
|
||||
};
|
||||
|
||||
|
||||
@@ -36,6 +36,21 @@ pub(super) const REPAIR_MAX: u64 = 512 * 1024 * 1024;
|
||||
/// demand read in the SDK's flat block semaphore.
|
||||
pub(super) const PREFETCH_BUDGET: usize = 8;
|
||||
|
||||
/// How long one read may wait on the network before it fails with `EIO`.
|
||||
///
|
||||
/// The SDK bounds each request, not a read: a block that times out is retried,
|
||||
/// and a read can need several blocks, a block table and a repair. On a link
|
||||
/// that stalled without dropping, one `read` held a FUSE worker for 34 minutes
|
||||
/// and the application behind it with it (`docs/BUGS.md` B107). An error the
|
||||
/// caller can retry beats a process stuck in `D` state.
|
||||
pub(super) const READ_FETCH_TIMEOUT: Duration = Duration::from_secs(120);
|
||||
|
||||
/// How long a read of blocks nothing has cached waits for a lost link to come
|
||||
/// back before failing. Long enough to ride out a Wi-Fi blip, short enough
|
||||
/// that a program reading while the laptop is really offline gets its `EIO`
|
||||
/// before the user gives up on it.
|
||||
const READ_OFFLINE_WAIT: Duration = Duration::from_secs(30);
|
||||
|
||||
/// Cap on tracked sequential streams. Only reads in progress matter, so the map
|
||||
/// is cleared wholesale rather than aged — losing the ramp costs one read.
|
||||
const MAX_PREFETCH_STREAMS: usize = 256;
|
||||
@@ -55,7 +70,7 @@ const MAX_OPEN_READERS: usize = 64;
|
||||
/// `(mtime, size)` — the same validity pair the content cache uses (a new
|
||||
/// revision bumps mtime). On a mismatch the reader is dropped and reopened.
|
||||
pub(super) struct CachedReader {
|
||||
reader: Arc<RevisionReader>,
|
||||
reader: Arc<dyn RevisionRead>,
|
||||
mtime: i64,
|
||||
size: u64,
|
||||
/// For LRU eviction.
|
||||
@@ -86,7 +101,7 @@ pub(super) struct PendingOpen {
|
||||
/// must not join this open — it would get a reader for the wrong bytes.
|
||||
mtime: i64,
|
||||
size: u64,
|
||||
rx: tokio::sync::watch::Receiver<Option<Result<Arc<RevisionReader>, Errno>>>,
|
||||
rx: tokio::sync::watch::Receiver<Option<Result<Arc<dyn RevisionRead>, Errno>>>,
|
||||
}
|
||||
|
||||
/// Distinguishes `PendingOpen`s. Only uniqueness matters, so a plain counter
|
||||
@@ -298,16 +313,16 @@ impl Core {
|
||||
uid: &NodeUid,
|
||||
mtime: i64,
|
||||
fsize: u64,
|
||||
) -> Result<Arc<RevisionReader>, Errno> {
|
||||
) -> Result<Arc<dyn RevisionRead>, Errno> {
|
||||
loop {
|
||||
// Decide under the lock, act outside it — network I/O must never run
|
||||
// under a std `Mutex`, and an await must never hold one.
|
||||
enum Act {
|
||||
Hit(Arc<RevisionReader>),
|
||||
Join(tokio::sync::watch::Receiver<Option<Result<Arc<RevisionReader>, Errno>>>),
|
||||
Hit(Arc<dyn RevisionRead>),
|
||||
Join(tokio::sync::watch::Receiver<Option<Result<Arc<dyn RevisionRead>, Errno>>>),
|
||||
Lead(
|
||||
u64,
|
||||
tokio::sync::watch::Sender<Option<Result<Arc<RevisionReader>, Errno>>>,
|
||||
tokio::sync::watch::Sender<Option<Result<Arc<dyn RevisionRead>, Errno>>>,
|
||||
),
|
||||
}
|
||||
let act = {
|
||||
@@ -361,9 +376,10 @@ impl Core {
|
||||
}
|
||||
Act::Lead(id, tx) => {
|
||||
debug!(%uid, mtime, fsize, "opening revision");
|
||||
let result = match self.client.open_revision(uid).await {
|
||||
Ok(reader) => Ok(Arc::new(reader)),
|
||||
let result = match self.drive.open_revision(uid).await {
|
||||
Ok(reader) => Ok(reader),
|
||||
Err(e) => {
|
||||
self.lost_link(&e, "open a revision");
|
||||
warn!(%uid, error = %e, "open_revision failed");
|
||||
Err(Errno::EIO)
|
||||
}
|
||||
@@ -552,7 +568,7 @@ impl Core {
|
||||
/// leader's claim is gone by then, so this cannot recurse.
|
||||
async fn fetch_block(
|
||||
&self,
|
||||
reader: &Arc<RevisionReader>,
|
||||
reader: &Arc<dyn RevisionRead>,
|
||||
uid: &NodeUid,
|
||||
mtime: i64,
|
||||
fsize: u64,
|
||||
@@ -618,7 +634,7 @@ impl Core {
|
||||
/// download, or the read fails. See [`Core::repair_block`] (bugs.md B84).
|
||||
async fn read_block(
|
||||
&self,
|
||||
reader: &Arc<RevisionReader>,
|
||||
reader: &Arc<dyn RevisionRead>,
|
||||
uid: &NodeUid,
|
||||
mtime: i64,
|
||||
fsize: u64,
|
||||
@@ -649,6 +665,7 @@ impl Core {
|
||||
.store_block_geometry(uid, mtime, fsize, reader.block_sizes());
|
||||
}
|
||||
let bytes = reader.read_at(span.start, span.len).await.map_err(|e| {
|
||||
self.lost_link(&e, "read a block");
|
||||
warn!(%uid, start = span.start, len = span.len, error = %e, "block read failed");
|
||||
Errno::EIO
|
||||
})?;
|
||||
@@ -761,7 +778,7 @@ impl Core {
|
||||
error!(%uid, %error, "creating a scratch file for a repair download failed");
|
||||
Errno::EIO
|
||||
})?;
|
||||
let outcome = self.client.download_file_to(uid, &mut file).await;
|
||||
let outcome = self.drive.download_file_to(uid, &mut file).await;
|
||||
let stored = (|| {
|
||||
outcome.map_err(|error| {
|
||||
warn!(%uid, %error, "repair download failed");
|
||||
@@ -939,28 +956,67 @@ impl Core {
|
||||
}
|
||||
|
||||
if !misses.is_empty() {
|
||||
let fetched = self.rt.block_on(async {
|
||||
// Resolve the file's keys and block table once, then read every
|
||||
// missing block through the shared reader. Previously each block
|
||||
// called `download_range`, which redid that resolution per block.
|
||||
let reader = self.revision_reader(uid, mtime, fsize).await?;
|
||||
// One deadline for the whole read, including any wait for a lost
|
||||
// link. A fetch that failed because the link went down is tried
|
||||
// once more when it is back, so a blip is a slow read, not an `EIO`.
|
||||
let deadline = Instant::now() + READ_FETCH_TIMEOUT;
|
||||
let mut retried = false;
|
||||
let fetched = loop {
|
||||
if !self.is_online() {
|
||||
let wait =
|
||||
READ_OFFLINE_WAIT.min(deadline.saturating_duration_since(Instant::now()));
|
||||
if !self.wait_online(wait) {
|
||||
debug!(%uid, offset, len, "read of uncached blocks while offline; failing it");
|
||||
return Err(Errno::EIO);
|
||||
}
|
||||
}
|
||||
let fetch = async {
|
||||
// Resolve the file's keys and block table once, then read every
|
||||
// missing block through the shared reader. Previously each block
|
||||
// called `download_range`, which redid that resolution per block.
|
||||
let reader = self.revision_reader(uid, mtime, fsize).await?;
|
||||
|
||||
let mut set = tokio::task::JoinSet::new();
|
||||
for &span in &misses {
|
||||
let (core, reader, uid) = (self.clone(), reader.clone(), uid.clone());
|
||||
set.spawn(async move {
|
||||
core.fetch_block(&reader, &uid, mtime, fsize, span, cache_blocks)
|
||||
.await
|
||||
.map(|bytes| (span.idx, bytes))
|
||||
});
|
||||
let mut set = tokio::task::JoinSet::new();
|
||||
for &span in &misses {
|
||||
let (core, reader, uid) = (self.clone(), reader.clone(), uid.clone());
|
||||
set.spawn(async move {
|
||||
core.fetch_block(&reader, &uid, mtime, fsize, span, cache_blocks)
|
||||
.await
|
||||
.map(|bytes| (span.idx, bytes))
|
||||
});
|
||||
}
|
||||
let mut out = Vec::with_capacity(misses.len());
|
||||
while let Some(joined) = set.join_next().await {
|
||||
// A join error means the task panicked; surface it as EIO.
|
||||
out.push(joined.map_err(|_| Errno::EIO)??);
|
||||
}
|
||||
Ok::<_, Errno>(out)
|
||||
};
|
||||
// Dropping the fetch on the deadline aborts the block tasks with
|
||||
// it; blocks that already landed stay cached for the retry. The
|
||||
// timer is built inside the runtime: a FUSE worker has no reactor
|
||||
// of its own, and building it out here panicked every remote read.
|
||||
let budget = deadline.saturating_duration_since(Instant::now());
|
||||
match self
|
||||
.rt
|
||||
.block_on(async { tokio::time::timeout(budget, fetch).await })
|
||||
{
|
||||
Ok(Ok(fetched)) => break fetched,
|
||||
// The fetch saw the link go down; the top of the loop waits
|
||||
// for it within what is left of the deadline.
|
||||
Ok(Err(_)) if !retried && !self.is_online() => retried = true,
|
||||
Ok(Err(e)) => return Err(e),
|
||||
Err(_) => {
|
||||
warn!(
|
||||
%uid, offset, len, secs = READ_FETCH_TIMEOUT.as_secs(),
|
||||
"read got no answer from the network in time; failing it"
|
||||
);
|
||||
self.evict_reader(uid);
|
||||
self.mark_offline("read", &crate::link::no_answer(READ_FETCH_TIMEOUT));
|
||||
return Err(Errno::EIO);
|
||||
}
|
||||
}
|
||||
let mut out = Vec::with_capacity(misses.len());
|
||||
while let Some(joined) = set.join_next().await {
|
||||
// A join error means the task panicked; surface it as EIO.
|
||||
out.push(joined.map_err(|_| Errno::EIO)??);
|
||||
}
|
||||
Ok::<_, Errno>(out)
|
||||
})?;
|
||||
};
|
||||
for (bidx, bytes) in fetched {
|
||||
blocks[(bidx - first) as usize] = Some(bytes);
|
||||
}
|
||||
|
||||
@@ -248,10 +248,7 @@ impl Core {
|
||||
|
||||
let outcomes = self
|
||||
.rt
|
||||
.block_on(
|
||||
self.client
|
||||
.trash_nodes(std::slice::from_ref(&candidate.uid)),
|
||||
)
|
||||
.block_on(self.drive.trash_nodes(std::slice::from_ref(&candidate.uid)))
|
||||
.map_err(|e| CoreError::from_api(&e, "trash the mis-dated original"))?;
|
||||
if let Some((_, Err(e))) = outcomes.into_iter().next() {
|
||||
return Err(CoreError::from_api(&e, "trash the mis-dated original"));
|
||||
|
||||
@@ -0,0 +1,611 @@
|
||||
//! Moving a file or folder between two locations without moving its content.
|
||||
//!
|
||||
//! `~/ProtonDrive` and every on-demand synced folder are separate FUSE
|
||||
//! sessions, and a mirror folder is a plain local directory. The kernel refuses
|
||||
//! `rename(2)` between two mounts with `EXDEV` before the daemon hears of it, so
|
||||
//! `mv` falls back to copy and delete: every file is downloaded, uploaded again,
|
||||
//! and the original is trashed.
|
||||
//!
|
||||
//! On Proton Drive the same move is one metadata change, because My files and
|
||||
//! this computer's device folders all live on the main volume. So a move
|
||||
//! request that names two absolute paths is resolved here, against every
|
||||
//! location this daemon knows, and done as a server-side move. The local side
|
||||
//! is then brought level: a mount forgets the node and re-lists, and a mirror
|
||||
//! folder either renames its copy along or drops it once Drive has it.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use parking_lot::Mutex;
|
||||
use pdfs_core::db::{StoredSyncEntry, StoredSyncFolder};
|
||||
use pdfs_core::{CoreError, CoreResult};
|
||||
use proton_drive_rs::NodeKind;
|
||||
use proton_drive_rs::proton_sdk::ids::NodeUid;
|
||||
use tracing::{info, warn};
|
||||
|
||||
use super::sync::mirror_subtree_unsynced;
|
||||
use super::{Core, is_local_uid, parse_uid};
|
||||
|
||||
/// How long a move waits for a mirror folder's sync pass to finish before it
|
||||
/// reports the folder busy. A pass can run for hours on a first sync, so the
|
||||
/// move does not wait it out.
|
||||
const MIRROR_LOCK_WAIT: Duration = Duration::from_secs(5);
|
||||
|
||||
/// Where one side of a move lives.
|
||||
enum Place {
|
||||
/// My files or an on-demand folder: a live FUSE mount. `core` is rooted at
|
||||
/// that mount and `rel` is relative to its mountpoint.
|
||||
Mounted { core: Box<Core>, rel: PathBuf },
|
||||
/// A mirror folder: a local directory kept in step by the sync engine.
|
||||
/// `rel` is `/`-separated, relative to the folder, as the baseline keys it.
|
||||
Mirror {
|
||||
folder: StoredSyncFolder,
|
||||
rel: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl Place {
|
||||
fn mirror_id(&self) -> Option<i64> {
|
||||
match self {
|
||||
Place::Mirror { folder, .. } => Some(folder.id),
|
||||
Place::Mounted { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_root(&self) -> bool {
|
||||
match self {
|
||||
Place::Mounted { rel, .. } => rel.as_os_str().is_empty(),
|
||||
Place::Mirror { rel, .. } => rel.is_empty(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The mirror folder that holds `abs`, and `abs` relative to it. The deepest
|
||||
/// folder wins, although folders are not expected to nest.
|
||||
fn mirror_covering(
|
||||
folders: Vec<StoredSyncFolder>,
|
||||
abs: &Path,
|
||||
) -> Option<(StoredSyncFolder, String)> {
|
||||
let folder = folders
|
||||
.into_iter()
|
||||
.filter(|f| f.mode == "mirror" && abs.starts_with(&f.local_path))
|
||||
.max_by_key(|f| Path::new(&f.local_path).components().count())?;
|
||||
let rel = abs.strip_prefix(&folder.local_path).ok()?;
|
||||
let parts = rel
|
||||
.components()
|
||||
.map(|c| match c {
|
||||
Component::Normal(part) => part.to_str(),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Option<Vec<_>>>()?;
|
||||
Some((folder, parts.join("/")))
|
||||
}
|
||||
|
||||
/// `name` inside the folder at `parent`, in baseline form.
|
||||
fn join_rel(parent: &str, name: &str) -> String {
|
||||
if parent.is_empty() {
|
||||
name.to_string()
|
||||
} else {
|
||||
format!("{parent}/{name}")
|
||||
}
|
||||
}
|
||||
|
||||
/// Rename `from` to `to`, failing with `EEXIST` rather than replacing
|
||||
/// whatever appeared at `to` since it was checked. A filesystem that cannot
|
||||
/// promise that gets a plain rename after one more look.
|
||||
fn rename_noreplace(from: &Path, to: &Path) -> std::io::Result<()> {
|
||||
use std::ffi::CString;
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
|
||||
let cstr = |p: &Path| {
|
||||
CString::new(p.as_os_str().as_bytes())
|
||||
.map_err(|_| std::io::Error::from_raw_os_error(libc::EINVAL))
|
||||
};
|
||||
let (c_from, c_to) = (cstr(from)?, cstr(to)?);
|
||||
// SAFETY: both are valid NUL-terminated paths that outlive the call.
|
||||
let rc = unsafe {
|
||||
libc::syscall(
|
||||
libc::SYS_renameat2,
|
||||
libc::AT_FDCWD,
|
||||
c_from.as_ptr(),
|
||||
libc::AT_FDCWD,
|
||||
c_to.as_ptr(),
|
||||
libc::RENAME_NOREPLACE,
|
||||
)
|
||||
};
|
||||
if rc == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let err = std::io::Error::last_os_error();
|
||||
match err.raw_os_error() {
|
||||
Some(libc::EINVAL | libc::ENOSYS) => {
|
||||
if std::fs::symlink_metadata(to).is_ok() {
|
||||
return Err(std::io::Error::from_raw_os_error(libc::EEXIST));
|
||||
}
|
||||
std::fs::rename(from, to)
|
||||
}
|
||||
_ => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `uid` is `root` or lies under it, as far as the nodes table knows.
|
||||
fn under(core: &Core, root: &NodeUid, uid: &str) -> bool {
|
||||
let root = root.to_string();
|
||||
uid == root || matches!(core.db.path_relative_to(&root, uid), Ok(Some(_)))
|
||||
}
|
||||
|
||||
impl Core {
|
||||
/// Move the file or folder at `src` into the folder at `dest_parent`. Both
|
||||
/// are absolute local paths and may be in different locations: My files,
|
||||
/// an on-demand folder, or a mirror folder. Returns the moved name.
|
||||
///
|
||||
/// Within one mount this is [`Core::move_to`]. Across locations it
|
||||
/// is a server-side move followed by local bookkeeping; see the module
|
||||
/// documentation. Anything the move could lose is refused up front.
|
||||
pub(crate) fn move_between(&self, src: &Path, dest_parent: &Path) -> CoreResult<String> {
|
||||
let source = self.locate(src)?;
|
||||
let dest = self.locate(dest_parent)?;
|
||||
if let (
|
||||
Place::Mounted { core, rel },
|
||||
Place::Mounted {
|
||||
core: dest_core,
|
||||
rel: dest_rel,
|
||||
},
|
||||
) = (&source, &dest)
|
||||
&& Arc::ptr_eq(&core.root, &dest_core.root)
|
||||
{
|
||||
return core.move_to(rel, dest_rel);
|
||||
}
|
||||
if source.is_root() {
|
||||
return Err(CoreError::invalid(format!(
|
||||
"{} is the root of a synced location and cannot be moved",
|
||||
src.display()
|
||||
)));
|
||||
}
|
||||
if dest_parent.starts_with(src) {
|
||||
return Err(CoreError::invalid("cannot move a folder into itself"));
|
||||
}
|
||||
let name = src
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.ok_or_else(|| CoreError::invalid(format!("{} has no usable name", src.display())))?
|
||||
.to_string();
|
||||
|
||||
// Hold every mirror folder involved, lowest id first, so no sync pass
|
||||
// sees the move half done and two moves cannot deadlock.
|
||||
let mut ids: Vec<i64> = [source.mirror_id(), dest.mirror_id()]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect();
|
||||
ids.sort_unstable();
|
||||
ids.dedup();
|
||||
let locks: Vec<Arc<Mutex<()>>> = ids.iter().map(|&id| self.sync_lock(id)).collect();
|
||||
let mut guards = Vec::with_capacity(locks.len());
|
||||
for lock in &locks {
|
||||
guards.push(lock.try_lock_for(MIRROR_LOCK_WAIT).ok_or_else(|| {
|
||||
CoreError::conflict("a synced folder is busy syncing; try again when it is done")
|
||||
})?);
|
||||
}
|
||||
|
||||
let mut baselines: HashMap<i64, HashMap<String, StoredSyncEntry>> = HashMap::new();
|
||||
for &id in &ids {
|
||||
let baseline = self
|
||||
.db
|
||||
.sync_entries(id)
|
||||
.map_err(|e| CoreError::internal(format!("db: {e}")))?;
|
||||
baselines.insert(id, baseline);
|
||||
}
|
||||
|
||||
let src_uid = self.source_uid(&source, &baselines)?;
|
||||
let dest_uid = self.dest_uid(&dest, &baselines, &name)?;
|
||||
if src_uid.volume_id != dest_uid.volume_id {
|
||||
return Err(CoreError::invalid(
|
||||
"the two locations are on different Proton Drive volumes; copy it instead",
|
||||
));
|
||||
}
|
||||
if under(self, &src_uid, &dest_uid.to_string()) {
|
||||
return Err(CoreError::invalid("cannot move a folder into itself"));
|
||||
}
|
||||
|
||||
// Mirror to mirror renames the local copy along, so it needs nothing
|
||||
// proven about it. Another filesystem makes that impossible, and the
|
||||
// pair then drops the source copy like any other move out of a mirror.
|
||||
let mut renamed = None;
|
||||
if let (
|
||||
Place::Mirror { folder, rel },
|
||||
Place::Mirror {
|
||||
folder: dest_folder,
|
||||
rel: dest_rel,
|
||||
},
|
||||
) = (&source, &dest)
|
||||
{
|
||||
let from = src.to_path_buf();
|
||||
let to = dest_parent.join(&name);
|
||||
match rename_noreplace(&from, &to) {
|
||||
Ok(()) => renamed = Some((from, to)),
|
||||
Err(e) if e.raw_os_error() == Some(libc::EXDEV) => {
|
||||
info!(from = %folder.local_path, to = %dest_folder.local_path,
|
||||
"move: mirror folders are on different filesystems");
|
||||
}
|
||||
Err(e) if e.raw_os_error() == Some(libc::EEXIST) => {
|
||||
return Err(CoreError::conflict(format!(
|
||||
"{name} already exists in {}",
|
||||
dest_parent.display()
|
||||
)));
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(CoreError::internal(format!(
|
||||
"could not move {} to {}: {e}",
|
||||
rel,
|
||||
join_rel(dest_rel, &name)
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if renamed.is_none()
|
||||
&& let Place::Mirror { folder, rel } = &source
|
||||
&& let Some(reason) =
|
||||
mirror_subtree_unsynced(Path::new(&folder.local_path), rel, &baselines[&folder.id])
|
||||
{
|
||||
return Err(CoreError::conflict(format!(
|
||||
"{name} is not fully synced yet ({reason}); wait for the sync to finish"
|
||||
)));
|
||||
}
|
||||
|
||||
if let Err(e) = self.move_remote(&src_uid, &dest_uid) {
|
||||
if let Some((from, to)) = &renamed
|
||||
&& let Err(back) = std::fs::rename(to, from)
|
||||
{
|
||||
warn!(from = %to.display(), to = %from.display(), error = %back,
|
||||
"move: could not put the local copy back after the move failed");
|
||||
}
|
||||
return Err(CoreError::from_api(&e, "move"));
|
||||
}
|
||||
|
||||
self.settle_source(
|
||||
&source,
|
||||
&src_uid,
|
||||
renamed.is_some(),
|
||||
&dest,
|
||||
&dest_uid,
|
||||
&name,
|
||||
src,
|
||||
);
|
||||
self.settle_dest(&dest, &dest_uid, &name);
|
||||
drop(guards);
|
||||
if renamed.is_none()
|
||||
&& let Place::Mirror { folder, .. } = &dest
|
||||
{
|
||||
// Nothing local exists yet; the folder's next pass downloads it.
|
||||
self.sync_now(Some(folder.id));
|
||||
}
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
/// Which location `abs` is in. A mount is looked for first: a mirror folder
|
||||
/// cannot live inside a FUSE mount, and a mount is what the kernel sees.
|
||||
fn locate(&self, abs: &Path) -> CoreResult<Place> {
|
||||
if let Some((core, rel)) = self.rooted_at(abs) {
|
||||
if !rel
|
||||
.components()
|
||||
.all(|c| matches!(c, Component::Normal(_) | Component::CurDir))
|
||||
{
|
||||
return Err(CoreError::invalid(format!(
|
||||
"{} escapes its location",
|
||||
abs.display()
|
||||
)));
|
||||
}
|
||||
return Ok(Place::Mounted {
|
||||
core: Box::new(core),
|
||||
rel,
|
||||
});
|
||||
}
|
||||
let folders = self
|
||||
.db
|
||||
.sync_folder_list()
|
||||
.map_err(|e| CoreError::internal(format!("db: {e}")))?;
|
||||
mirror_covering(folders, abs)
|
||||
.map(|(folder, rel)| Place::Mirror { folder, rel })
|
||||
.ok_or_else(|| {
|
||||
CoreError::invalid(format!(
|
||||
"{} is not in a Proton Drive location",
|
||||
abs.display()
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
/// The node being moved, once everything that must hold before it leaves
|
||||
/// its location has been checked.
|
||||
fn source_uid(
|
||||
&self,
|
||||
source: &Place,
|
||||
baselines: &HashMap<i64, HashMap<String, StoredSyncEntry>>,
|
||||
) -> CoreResult<NodeUid> {
|
||||
match source {
|
||||
Place::Mounted { core, rel } => {
|
||||
let (ino, uid) = core.resolve(rel)?;
|
||||
if is_local_uid(&uid) {
|
||||
return Err(CoreError::conflict(
|
||||
"it is not uploaded yet; wait for the upload to finish",
|
||||
));
|
||||
}
|
||||
let old_parent = core.source_parent_uid(ino, rel)?;
|
||||
for authority in [&uid, &old_parent] {
|
||||
core.require_uid_writable(authority)
|
||||
.map_err(|e| core.errno_error(e, "move access"))?;
|
||||
}
|
||||
if self.has_pending_work_under(&uid)? {
|
||||
return Err(CoreError::conflict(
|
||||
"it has changes that are not uploaded yet; wait for the upload to finish",
|
||||
));
|
||||
}
|
||||
Ok(uid)
|
||||
}
|
||||
Place::Mirror { folder, rel } => baselines[&folder.id]
|
||||
.get(rel)
|
||||
.and_then(|entry| entry.remote_uid.as_deref())
|
||||
.and_then(parse_uid)
|
||||
.ok_or_else(|| {
|
||||
CoreError::conflict(
|
||||
"it is not on Proton Drive yet; wait for the sync to finish",
|
||||
)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// The folder the node moves into. Refuses one that already holds `name`
|
||||
/// on this disk; Drive refuses a clash on its side by itself.
|
||||
fn dest_uid(
|
||||
&self,
|
||||
dest: &Place,
|
||||
baselines: &HashMap<i64, HashMap<String, StoredSyncEntry>>,
|
||||
name: &str,
|
||||
) -> CoreResult<NodeUid> {
|
||||
let uid = match dest {
|
||||
Place::Mounted { core, rel } => {
|
||||
let (ino, uid) = core.resolve(rel)?;
|
||||
let is_folder = core
|
||||
.state()
|
||||
.entries
|
||||
.get(&ino)
|
||||
.is_some_and(|entry| matches!(entry.node.kind, NodeKind::Folder));
|
||||
if !is_folder {
|
||||
return Err(CoreError::invalid("the destination is not a folder"));
|
||||
}
|
||||
core.require_uid_writable(&uid)
|
||||
.map_err(|e| core.errno_error(e, "move access"))?;
|
||||
uid
|
||||
}
|
||||
Place::Mirror { folder, rel } => {
|
||||
let local = Path::new(&folder.local_path).join(rel);
|
||||
if !local.is_dir() {
|
||||
return Err(CoreError::invalid("the destination is not a folder"));
|
||||
}
|
||||
if std::fs::symlink_metadata(local.join(name)).is_ok() {
|
||||
return Err(CoreError::conflict(format!(
|
||||
"{name} already exists in the destination"
|
||||
)));
|
||||
}
|
||||
let uid = if rel.is_empty() {
|
||||
Some(folder.remote_uid.as_str())
|
||||
} else {
|
||||
baselines[&folder.id]
|
||||
.get(rel)
|
||||
.and_then(|entry| entry.remote_uid.as_deref())
|
||||
};
|
||||
uid.and_then(parse_uid).ok_or_else(|| {
|
||||
CoreError::conflict(
|
||||
"the destination is not on Proton Drive yet; wait for the sync to finish",
|
||||
)
|
||||
})?
|
||||
}
|
||||
};
|
||||
if is_local_uid(&uid) {
|
||||
return Err(CoreError::conflict(
|
||||
"the destination is not uploaded yet; wait for the upload to finish",
|
||||
));
|
||||
}
|
||||
Ok(uid)
|
||||
}
|
||||
|
||||
/// Whether the queue owes Drive anything for `uid` or a node under it, or a
|
||||
/// file under it is open for writing. Moving such a node away would strand
|
||||
/// the pending change against a path that no longer leads to it.
|
||||
fn has_pending_work_under(&self, uid: &NodeUid) -> CoreResult<bool> {
|
||||
let ops = self
|
||||
.db
|
||||
.pending_ops()
|
||||
.map_err(|e| CoreError::internal(format!("db: {e}")))?;
|
||||
if ops.iter().any(|op| {
|
||||
under(self, uid, &op.uid)
|
||||
|| op
|
||||
.parent_uid
|
||||
.as_deref()
|
||||
.is_some_and(|p| under(self, uid, p))
|
||||
}) {
|
||||
return Ok(true);
|
||||
}
|
||||
// Collected first so no State lock is held across the nodes-table reads.
|
||||
let mut writing = Vec::new();
|
||||
self.for_each_state(|st| {
|
||||
writing.extend(st.active_writes.values().map(|w| w.uid.to_string()));
|
||||
});
|
||||
Ok(writing.iter().any(|w| under(self, uid, w)))
|
||||
}
|
||||
|
||||
/// Bring the source location level with a move Drive has made.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn settle_source(
|
||||
&self,
|
||||
source: &Place,
|
||||
uid: &NodeUid,
|
||||
renamed: bool,
|
||||
dest: &Place,
|
||||
dest_uid: &NodeUid,
|
||||
name: &str,
|
||||
src: &Path,
|
||||
) {
|
||||
match source {
|
||||
Place::Mounted { core, rel } => {
|
||||
// Moved everywhere, so no mount keeps serving it under the old
|
||||
// path and its row stays (`docs/BUGS.md` B117). The content
|
||||
// cache is keyed by uid and stays valid.
|
||||
self.relocate_everywhere(uid, dest_uid, name);
|
||||
core.invalidate_parent_listing(rel);
|
||||
}
|
||||
Place::Mirror { folder, rel } if renamed => {
|
||||
let Place::Mirror {
|
||||
folder: dest_folder,
|
||||
rel: dest_rel,
|
||||
} = dest
|
||||
else {
|
||||
unreachable!("a local rename needs a mirror destination");
|
||||
};
|
||||
if let Err(e) = self.db.sync_entries_move(
|
||||
folder.id,
|
||||
rel,
|
||||
dest_folder.id,
|
||||
&join_rel(dest_rel, name),
|
||||
) {
|
||||
// The next passes see a copy with no baseline on one side
|
||||
// and a baseline with no copy on the other. That uploads
|
||||
// the files again and trashes the moved node, but loses
|
||||
// nothing.
|
||||
warn!(error = ?e, "move: could not carry the sync baseline along");
|
||||
}
|
||||
}
|
||||
Place::Mirror { folder, rel } => {
|
||||
// Drive had all of it when the move was checked, so the local
|
||||
// copy is only a stale duplicate now. Something written into it
|
||||
// since would go with it, so look again first; if anything is
|
||||
// new, the copy stays and the next pass uploads it as new.
|
||||
let changed = match self.db.sync_entries(folder.id) {
|
||||
Ok(baseline) => {
|
||||
mirror_subtree_unsynced(Path::new(&folder.local_path), rel, &baseline)
|
||||
}
|
||||
Err(e) => Some(format!("db: {e}")),
|
||||
};
|
||||
if let Err(e) = self.db.sync_entries_remove_subtree(folder.id, rel) {
|
||||
warn!(error = ?e, "move: could not drop the sync baseline");
|
||||
}
|
||||
if let Some(reason) = changed {
|
||||
warn!(path = %src.display(), reason,
|
||||
"move: the local copy changed during the move; keeping it");
|
||||
return;
|
||||
}
|
||||
let removed = if src.is_dir() {
|
||||
std::fs::remove_dir_all(src)
|
||||
} else {
|
||||
std::fs::remove_file(src)
|
||||
};
|
||||
if let Err(e) = removed {
|
||||
warn!(path = %src.display(), error = %e,
|
||||
"move: could not remove the local copy after the move");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Bring the destination location level with a move Drive has made.
|
||||
fn settle_dest(&self, dest: &Place, uid: &NodeUid, name: &str) {
|
||||
if let Place::Mounted { .. } = dest {
|
||||
self.invalidate_children_of(uid);
|
||||
// A lookup that missed before the move may be cached as absent.
|
||||
self.for_each_mount(|st, notify| {
|
||||
if let Some(&ino) = st.by_uid.get(uid) {
|
||||
notify.inval_entry(ino, name.to_string());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn folder(id: i64, local_path: &str, mode: &str) -> StoredSyncFolder {
|
||||
StoredSyncFolder {
|
||||
id,
|
||||
local_path: local_path.to_string(),
|
||||
remote_uid: format!("vol~root{id}"),
|
||||
remote_share_id: String::new(),
|
||||
mode: mode.to_string(),
|
||||
pending_mode: None,
|
||||
state: "idle".to_string(),
|
||||
last_sync: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_covering_finds_the_mirror_folder_and_the_path_inside_it() {
|
||||
let folders = vec![folder(1, "/home/u/Docs", "mirror")];
|
||||
let (found, rel) = mirror_covering(folders, Path::new("/home/u/Docs/a/b.txt")).unwrap();
|
||||
assert_eq!(found.id, 1);
|
||||
assert_eq!(rel, "a/b.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_covering_gives_an_empty_path_for_the_folder_itself() {
|
||||
let folders = vec![folder(1, "/home/u/Docs", "mirror")];
|
||||
let (_, rel) = mirror_covering(folders, Path::new("/home/u/Docs")).unwrap();
|
||||
assert_eq!(rel, "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_covering_prefers_the_deepest_folder() {
|
||||
let folders = vec![
|
||||
folder(1, "/home/u/Docs", "mirror"),
|
||||
folder(2, "/home/u/Docs/Work", "mirror"),
|
||||
];
|
||||
let (found, rel) = mirror_covering(folders, Path::new("/home/u/Docs/Work/x")).unwrap();
|
||||
assert_eq!(found.id, 2);
|
||||
assert_eq!(rel, "x");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_covering_skips_on_demand_folders_and_name_prefixed_siblings() {
|
||||
let folders = || {
|
||||
vec![
|
||||
folder(1, "/home/u/Docs", "mirror"),
|
||||
folder(2, "/home/u/Media", "ondemand"),
|
||||
]
|
||||
};
|
||||
assert!(mirror_covering(folders(), Path::new("/home/u/Media/x")).is_none());
|
||||
assert!(mirror_covering(folders(), Path::new("/home/u/Docs2/x")).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mirror_covering_refuses_a_path_that_climbs_out() {
|
||||
let folders = vec![folder(1, "/home/u/Docs", "mirror")];
|
||||
assert!(mirror_covering(folders, Path::new("/home/u/Docs/../etc")).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn join_rel_names_a_child_of_the_folder_root_without_a_slash() {
|
||||
assert_eq!(join_rel("", "a"), "a");
|
||||
assert_eq!(join_rel("x/y", "a"), "x/y/a");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_local_rename_never_replaces_what_is_already_there() {
|
||||
let dir = std::env::temp_dir().join(format!("pdfs-relocate-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let (a, b, c) = (dir.join("a"), dir.join("b"), dir.join("c"));
|
||||
std::fs::write(&a, b"moved").unwrap();
|
||||
std::fs::write(&b, b"kept").unwrap();
|
||||
|
||||
let err = rename_noreplace(&a, &b).unwrap_err();
|
||||
assert_eq!(err.raw_os_error(), Some(libc::EEXIST));
|
||||
assert_eq!(std::fs::read(&b).unwrap(), b"kept");
|
||||
assert!(a.exists());
|
||||
|
||||
rename_noreplace(&a, &c).unwrap();
|
||||
assert!(!a.exists());
|
||||
assert_eq!(std::fs::read(&c).unwrap(), b"moved");
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -232,7 +232,7 @@ impl Core {
|
||||
let mut out = std::io::BufWriter::new(file);
|
||||
let written = match self
|
||||
.rt
|
||||
.block_on(self.client.download_revision_to(uid, revision_id, &mut out))
|
||||
.block_on(self.drive.download_revision_to(uid, revision_id, &mut out))
|
||||
.map_err(|e| CoreError::from_api(&e, "download revision"))
|
||||
.and_then(|()| {
|
||||
use std::io::Write as _;
|
||||
|
||||
@@ -21,7 +21,7 @@ use proton_drive_rs::{MemberRole, NodeKind};
|
||||
|
||||
use proton_drive_rs::Node;
|
||||
|
||||
use super::{Core, ROOT_INO, node_size, parse_uid, public_link_info, role_from_str, role_to_str};
|
||||
use super::{Core, node_size, parse_uid, public_link_info, role_from_str, role_to_str};
|
||||
|
||||
/// A node from a *shared* listing as a [`DirEntry`].
|
||||
///
|
||||
@@ -73,6 +73,8 @@ fn shared_entry_of(core: &Core, n: Node) -> DirEntry {
|
||||
shared_by,
|
||||
shared_at,
|
||||
shared_by_unverified,
|
||||
trashed_at: 0,
|
||||
trashed_from: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -461,7 +463,7 @@ impl Core {
|
||||
}
|
||||
let nodes = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_nodes(&uids))
|
||||
.block_on(self.drive.enumerate_nodes(&uids))
|
||||
.map_err(|e| CoreError::from_api(&e, "enumerate nodes"))?;
|
||||
Ok(nodes
|
||||
.into_iter()
|
||||
@@ -479,14 +481,14 @@ impl Core {
|
||||
parse_uid(uid).ok_or_else(|| CoreError::invalid(format!("invalid uid: {uid}")))?;
|
||||
let child_uids = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_folder_children_node_uids(&uid))
|
||||
.block_on(self.drive.enumerate_folder_children_node_uids(&uid))
|
||||
.map_err(|e| CoreError::from_api(&e, "enumerate shared children"))?;
|
||||
if child_uids.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let nodes = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_nodes(&child_uids))
|
||||
.block_on(self.drive.enumerate_nodes(&child_uids))
|
||||
.map_err(|e| CoreError::from_api(&e, "enumerate nodes"))?;
|
||||
Ok(nodes
|
||||
.into_iter()
|
||||
@@ -507,7 +509,7 @@ impl Core {
|
||||
parse_uid(uid).ok_or_else(|| CoreError::invalid(format!("invalid uid: {uid}")))?;
|
||||
let node = self
|
||||
.rt
|
||||
.block_on(self.client.get_node(&uid))
|
||||
.block_on(self.drive.get_node(&uid))
|
||||
.map_err(|e| CoreError::from_api(&e, "get shared node"))?
|
||||
.ok_or_else(|| CoreError::not_found("shared node not found"))?;
|
||||
if !node.is_file() {
|
||||
@@ -608,10 +610,10 @@ impl Core {
|
||||
/// Proton products (not Drive-only). A remote round-trip; nothing here is
|
||||
/// cached.
|
||||
pub(crate) fn account_quota(&self) -> CoreResult<(i64, i64)> {
|
||||
let q = self
|
||||
.rt
|
||||
.block_on(self.client.quota())
|
||||
.map_err(|e| CoreError::from_api(&e, "account quota"))?;
|
||||
let q = self.block_on_bounded(self.drive.quota()).map_err(|e| {
|
||||
self.lost_link(&e, "account quota");
|
||||
CoreError::from_api(&e, "account quota")
|
||||
})?;
|
||||
*self.quota.lock() = Some((std::time::Instant::now(), q.max_space, q.used_space));
|
||||
Ok((q.max_space, q.used_space))
|
||||
}
|
||||
@@ -630,11 +632,22 @@ impl Core {
|
||||
{
|
||||
return Some((max_space, used_space));
|
||||
}
|
||||
match self.account_quota() {
|
||||
let last = || self.quota.lock().map(|(_, max, used)| (max, used));
|
||||
if !self.is_online()
|
||||
|| self
|
||||
.quota_refreshing
|
||||
.swap(true, std::sync::atomic::Ordering::AcqRel)
|
||||
{
|
||||
return last();
|
||||
}
|
||||
let refreshed = self.account_quota();
|
||||
self.quota_refreshing
|
||||
.store(false, std::sync::atomic::Ordering::Release);
|
||||
match refreshed {
|
||||
Ok(q) => Some(q),
|
||||
Err(error) => {
|
||||
debug!(%error, "account quota refresh failed; serving the last known figures");
|
||||
self.quota.lock().map(|(_, max, used)| (max, used))
|
||||
last()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -656,7 +669,7 @@ impl Core {
|
||||
}
|
||||
let nodes = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_nodes(&uids))
|
||||
.block_on(self.drive.enumerate_nodes(&uids))
|
||||
.map_err(|e| CoreError::from_api(&e, "enumerate nodes"))?;
|
||||
let mut items = Vec::with_capacity(nodes.len());
|
||||
for n in nodes {
|
||||
@@ -701,19 +714,10 @@ impl Core {
|
||||
/// been seen through the mount (e.g. shared but not browsed to this session) —
|
||||
/// the caller then leaves the path empty.
|
||||
pub(crate) fn rel_path_for_uid(&self, uid: &NodeUid) -> Option<String> {
|
||||
// Searched across every mount: a node browsed through a sync folder is
|
||||
// interned in that fork's inode space, so the primary state alone had no
|
||||
// entry for it and the caller silently showed an empty path
|
||||
// (`docs/BUGS.md` B74). The path is relative to whichever mount holds
|
||||
// it, which is the one the user reached it through.
|
||||
let mut path = None;
|
||||
self.for_each_state(|st| {
|
||||
if path.is_some() {
|
||||
return;
|
||||
}
|
||||
path = walk_to_root(st, uid);
|
||||
});
|
||||
path
|
||||
// Relative to whichever root the node sits under: a node browsed
|
||||
// through a sync folder is under that fork's root, not My Files
|
||||
// (`docs/BUGS.md` B74).
|
||||
walk_to_root(&self.state(), uid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -758,14 +762,18 @@ impl StopSharingOutcome {
|
||||
}
|
||||
}
|
||||
|
||||
/// Walk one inode space from `uid` up to its root, building the path. `None` if
|
||||
/// that mount has never interned the node, or if the chain breaks partway —
|
||||
/// Walk the tree from `uid` up to the root it sits under, building the path.
|
||||
/// `None` if the node has never been interned, or if the chain breaks partway —
|
||||
/// a broken chain is a missing path, never a partial one.
|
||||
fn walk_to_root(st: &super::State, uid: &NodeUid) -> Option<String> {
|
||||
let mut ino = *st.by_uid.get(uid)?;
|
||||
let mut parts = Vec::new();
|
||||
while ino != ROOT_INO {
|
||||
while !st.roots.contains(&ino) {
|
||||
let entry = st.entries.get(&ino)?;
|
||||
// An orphan is its own parent without being a root.
|
||||
if entry.parent == ino {
|
||||
return None;
|
||||
}
|
||||
parts.push(entry.node.name.clone());
|
||||
ino = entry.parent;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,423 @@
|
||||
//! One simulated daemon: a real FUSE mount on its own state directory, whose
|
||||
//! Drive is a [`FakeClient`].
|
||||
//!
|
||||
//! Everything the daemon does runs as in production (drain workers, sync
|
||||
//! engine, event feed, online probe, control socket) except what the host
|
||||
//! changes (`mount::Host::Simulation`): a channel stops it instead of a signal,
|
||||
//! and it does not index the developer's home.
|
||||
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::sync::mpsc::SyncSender;
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use pdfs_core::cache::ContentCache;
|
||||
use pdfs_core::config::AppConfig;
|
||||
use pdfs_core::control::{self, PendingOpInfo, Request, Response};
|
||||
use pdfs_core::db::Db;
|
||||
use proton_drive_rs::ProtonDriveClient;
|
||||
use proton_drive_rs::proton_sdk::config::ProtonClientConfiguration;
|
||||
use proton_drive_rs::proton_sdk::session::{PasswordMode, ProtonApiSession, ResumeParameters};
|
||||
|
||||
use super::fake_drive::FakeClient;
|
||||
use crate::mount::{Host, MountOptions, MountOutcome, mount_with};
|
||||
|
||||
/// How long a mount may take to come up.
|
||||
const MOUNT_DEADLINE: Duration = Duration::from_secs(20);
|
||||
|
||||
/// How long a restart may wait for the last run to let go of its database.
|
||||
const LAST_RUN_DEADLINE: Duration = Duration::from_secs(30);
|
||||
|
||||
pub(crate) struct Daemon {
|
||||
pub(crate) client: FakeClient,
|
||||
pub(crate) mountpoint: PathBuf,
|
||||
dir: PathBuf,
|
||||
socket: PathBuf,
|
||||
stop: SyncSender<()>,
|
||||
mount: Option<JoinHandle<io::Result<MountOutcome>>>,
|
||||
rt: Option<tokio::runtime::Runtime>,
|
||||
}
|
||||
|
||||
impl Daemon {
|
||||
/// Mount a daemon on the state under `dir`, creating it on the first start.
|
||||
/// A later start on the same `dir` is a restart: it finds the queue, the
|
||||
/// cache and the database the last run left.
|
||||
pub(crate) fn start(dir: &Path, client: FakeClient) -> io::Result<Self> {
|
||||
log_to_test_output();
|
||||
let mountpoint = dir.join("mnt");
|
||||
let socket = dir.join("control.sock");
|
||||
std::fs::create_dir_all(&mountpoint)?;
|
||||
// A thread of the last run that is still in a request holds the
|
||||
// database, and with it the single-writer lock, until it returns. A
|
||||
// real restart waits for the old process to exit in the same way.
|
||||
let mut db = Db::open(&dir.join("pdfs.db"));
|
||||
let deadline = Instant::now() + LAST_RUN_DEADLINE;
|
||||
while db
|
||||
.as_ref()
|
||||
.is_err_and(|error| error.to_string().contains("already using"))
|
||||
&& Instant::now() < deadline
|
||||
{
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
db = Db::open(&dir.join("pdfs.db"));
|
||||
}
|
||||
let db = Arc::new(db.map_err(io::Error::other)?);
|
||||
let cache = ContentCache::open(dir.join("cache"), dir.join("pins.json"), 0, db.clone())
|
||||
.map_err(io::Error::other)?;
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(4)
|
||||
.enable_all()
|
||||
.build()?;
|
||||
let (stop, stopped) = std::sync::mpsc::sync_channel(1);
|
||||
let mount = {
|
||||
let drive = Arc::new(client.clone());
|
||||
let handle = rt.handle().clone();
|
||||
let mountpoint = mountpoint.clone();
|
||||
let socket = socket.clone();
|
||||
std::thread::Builder::new()
|
||||
.name("sim-mount".into())
|
||||
.spawn(move || {
|
||||
mount_with(
|
||||
offline_client(),
|
||||
drive,
|
||||
handle,
|
||||
&mountpoint,
|
||||
cache,
|
||||
&socket,
|
||||
db,
|
||||
MountOptions {
|
||||
username: "sim".into(),
|
||||
sweep_mode: AppConfig::default().resolved_conflict_sweep(),
|
||||
upload_limit: 0,
|
||||
download_limit: 0,
|
||||
},
|
||||
Host::Simulation(stopped),
|
||||
)
|
||||
})?
|
||||
};
|
||||
let deadline = Instant::now() + MOUNT_DEADLINE;
|
||||
while !is_mounted(&mountpoint) {
|
||||
if mount.is_finished() {
|
||||
return Err(match mount.join() {
|
||||
Ok(Err(error)) => error,
|
||||
_ => io::Error::other("mount ended before it came up"),
|
||||
});
|
||||
}
|
||||
if Instant::now() > deadline {
|
||||
return Err(io::Error::other("mount did not come up"));
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
}
|
||||
Ok(Self {
|
||||
client,
|
||||
mountpoint,
|
||||
dir: dir.to_path_buf(),
|
||||
socket,
|
||||
stop,
|
||||
mount: Some(mount),
|
||||
rt: Some(rt),
|
||||
})
|
||||
}
|
||||
|
||||
/// What the daemon still owes Drive, as `pdfs sync queue` shows it.
|
||||
pub(crate) fn pending(&self) -> io::Result<Vec<PendingOpInfo>> {
|
||||
match control::send(&self.socket, &Request::ListPendingOps).map_err(io::Error::other)? {
|
||||
Response::PendingOps { items } => Ok(items),
|
||||
other => Err(io::Error::other(format!("unexpected answer {other:?}"))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Stop the daemon and start it again on the same state and link.
|
||||
pub(crate) fn restart(mut self) -> io::Result<Self> {
|
||||
self.shut_down()?;
|
||||
Self::start(&self.dir.clone(), self.client.clone())
|
||||
}
|
||||
|
||||
/// Stop the daemon the way `systemctl --user stop` does.
|
||||
pub(crate) fn stop(mut self) -> io::Result<MountOutcome> {
|
||||
self.shut_down()
|
||||
}
|
||||
|
||||
fn shut_down(&mut self) -> io::Result<MountOutcome> {
|
||||
let Some(mount) = self.mount.take() else {
|
||||
return Ok(MountOutcome::Shutdown);
|
||||
};
|
||||
let _ = self.stop.try_send(());
|
||||
let outcome = mount
|
||||
.join()
|
||||
.unwrap_or_else(|_| Err(io::Error::other("mount thread panicked")));
|
||||
if let Some(rt) = self.rt.take() {
|
||||
rt.shutdown_timeout(Duration::from_secs(5));
|
||||
}
|
||||
outcome
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Daemon {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.shut_down();
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a file system is mounted at `path`: it sits on another device than
|
||||
/// its parent.
|
||||
fn is_mounted(path: &Path) -> bool {
|
||||
use std::os::unix::fs::MetadataExt;
|
||||
let device = |path: &Path| std::fs::metadata(path).map(|meta| meta.dev()).ok();
|
||||
let parent = path.parent().and_then(device);
|
||||
device(path).is_some_and(|dev| Some(dev) != parent)
|
||||
}
|
||||
|
||||
/// The daemons' logs, filtered by `RUST_LOG` and shown with `--nocapture`.
|
||||
/// What they log at `INFO` and above is also kept for [`take_logged`],
|
||||
/// whatever the filter.
|
||||
fn log_to_test_output() {
|
||||
use tracing_subscriber::prelude::*;
|
||||
let _ = tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::fmt::layer()
|
||||
.with_test_writer()
|
||||
.with_filter(tracing_subscriber::EnvFilter::from_default_env()),
|
||||
)
|
||||
.with(KeepLogged.with_filter(tracing_subscriber::filter::LevelFilter::INFO))
|
||||
.try_init();
|
||||
}
|
||||
|
||||
/// What the daemons logged at `INFO` and above since the last call, each as
|
||||
/// its message followed by its fields: `message key=value ...`.
|
||||
pub(crate) fn take_logged() -> Vec<String> {
|
||||
std::mem::take(&mut *LOGGED.lock())
|
||||
}
|
||||
|
||||
static LOGGED: parking_lot::Mutex<Vec<String>> = parking_lot::Mutex::new(Vec::new());
|
||||
|
||||
struct KeepLogged;
|
||||
|
||||
impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for KeepLogged {
|
||||
fn on_event(&self, event: &tracing::Event<'_>, _: tracing_subscriber::layer::Context<'_, S>) {
|
||||
let mut message = Message(String::new());
|
||||
event.record(&mut message);
|
||||
LOGGED.lock().push(message.0);
|
||||
}
|
||||
}
|
||||
|
||||
struct Message(String);
|
||||
|
||||
impl tracing::field::Visit for Message {
|
||||
fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
|
||||
if field.name() == "message" {
|
||||
self.0.insert_str(0, &format!("{value:?}"));
|
||||
} else {
|
||||
self.0.push_str(&format!(" {}={value:?}", field.name()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A client for what `DriveApi` does not cover (sharing, Photos, devices),
|
||||
/// pointed at a closed local port so none of it leaves the machine.
|
||||
fn offline_client() -> ProtonDriveClient {
|
||||
let config = ProtonClientConfiguration::new("external-drive-pdfs-sim@0.0.0-dev")
|
||||
.with_base_url("http://127.0.0.1:9");
|
||||
let session = ProtonApiSession::resume(
|
||||
config,
|
||||
ResumeParameters {
|
||||
session_id: "sim".to_owned().into(),
|
||||
username: "sim".into(),
|
||||
user_id: "sim".to_owned().into(),
|
||||
access_token: String::new(),
|
||||
refresh_token: String::new(),
|
||||
scopes: Vec::new(),
|
||||
is_waiting_for_second_factor_code: false,
|
||||
password_mode: PasswordMode::Single,
|
||||
},
|
||||
)
|
||||
.expect("an offline session needs no network");
|
||||
ProtonDriveClient::new(&session, Vec::new())
|
||||
}
|
||||
|
||||
/// A fresh directory for one simulation, under the system temp dir.
|
||||
pub(crate) fn scratch(label: &str) -> PathBuf {
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
static N: AtomicU64 = AtomicU64::new(0);
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"pdfs-sim-{label}-{}-{}",
|
||||
std::process::id(),
|
||||
N.fetch_add(1, Ordering::Relaxed)
|
||||
));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).expect("create a simulation directory");
|
||||
dir
|
||||
}
|
||||
|
||||
/// Wait up to `limit` for `done`, polling.
|
||||
pub(crate) fn wait_until(limit: Duration, mut done: impl FnMut() -> bool) -> bool {
|
||||
let deadline = Instant::now() + limit;
|
||||
loop {
|
||||
if done() {
|
||||
return true;
|
||||
}
|
||||
if Instant::now() > deadline {
|
||||
return false;
|
||||
}
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::sim::fake_drive::{Entry, FakeDrive, Faults};
|
||||
|
||||
#[test]
|
||||
#[ignore = "mounts FUSE: run with `cargo test -p pdfs-fuse sim:: -- --ignored`"]
|
||||
fn a_daemon_on_the_fake_drive_syncs_both_ways() {
|
||||
let drive = FakeDrive::new();
|
||||
let dir = scratch("smoke");
|
||||
let daemon = Daemon::start(&dir, drive.client(1, Faults::lan())).unwrap();
|
||||
|
||||
std::fs::write(daemon.mountpoint.join("hello.txt"), b"hi").unwrap();
|
||||
assert!(
|
||||
wait_until(Duration::from_secs(30), || {
|
||||
drive.tree().get("hello.txt") == Some(&Entry::File(Arc::new(b"hi".to_vec())))
|
||||
}),
|
||||
"a local write landed on Drive: {:?}",
|
||||
drive.tree()
|
||||
);
|
||||
|
||||
drive.device().write("phone.txt", b"from the phone");
|
||||
let remote = daemon.mountpoint.join("phone.txt");
|
||||
assert!(
|
||||
wait_until(Duration::from_secs(30), || {
|
||||
std::fs::read(&remote).ok().as_deref() == Some(b"from the phone".as_slice())
|
||||
}),
|
||||
"a remote write reached the mount"
|
||||
);
|
||||
|
||||
daemon.client.set_online(false);
|
||||
std::fs::write(daemon.mountpoint.join("offline.txt"), b"queued").unwrap();
|
||||
std::thread::sleep(Duration::from_secs(2));
|
||||
assert!(!drive.tree().contains_key("offline.txt"));
|
||||
daemon.client.set_online(true);
|
||||
assert!(
|
||||
wait_until(Duration::from_secs(60), || drive
|
||||
.tree()
|
||||
.contains_key("offline.txt")),
|
||||
"a write made offline landed once the link came back"
|
||||
);
|
||||
|
||||
assert!(matches!(daemon.stop(), Ok(MountOutcome::Shutdown)));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// Write `bytes` at `offset` into an existing file, without truncating it.
|
||||
fn write_at(path: &Path, offset: u64, bytes: &[u8]) -> io::Result<()> {
|
||||
use std::os::unix::fs::FileExt;
|
||||
std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.open(path)?
|
||||
.write_all_at(bytes, offset)
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "mounts FUSE: run with `cargo test -p pdfs-fuse sim:: -- --ignored`"]
|
||||
fn a_file_closed_empty_takes_a_second_write_at_once() {
|
||||
// The empty revision never counted as complete, so the next write
|
||||
// open before it drained was refused with EIO (B123).
|
||||
let drive = FakeDrive::new();
|
||||
let dir = scratch("empty-rewrite");
|
||||
let daemon = Daemon::start(&dir, drive.client(1, Faults::lan())).unwrap();
|
||||
|
||||
let path = daemon.mountpoint.join("empty.txt");
|
||||
std::fs::write(&path, b"old").unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
drive.tree().contains_key("empty.txt")
|
||||
}));
|
||||
std::fs::write(&path, b"").unwrap();
|
||||
write_at(&path, 0, b"filled").unwrap();
|
||||
assert!(
|
||||
wait_until(Duration::from_secs(30), || {
|
||||
drive.tree().get("empty.txt") == Some(&Entry::File(Arc::new(b"filled".to_vec())))
|
||||
}),
|
||||
"the second write landed: {:?}",
|
||||
drive.tree()
|
||||
);
|
||||
|
||||
assert!(matches!(daemon.stop(), Ok(MountOutcome::Shutdown)));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "mounts FUSE: run with `cargo test -p pdfs-fuse sim:: -- --ignored`"]
|
||||
fn the_echo_of_our_own_rename_keeps_the_cached_bytes() {
|
||||
// The echo read as someone else's change and evicted the bytes, so a
|
||||
// partial write could not gap-fill, and the next write open on the
|
||||
// file was refused with EIO (B123).
|
||||
let drive = FakeDrive::new();
|
||||
let dir = scratch("rename-echo");
|
||||
let daemon = Daemon::start(&dir, drive.client(1, Faults::lan())).unwrap();
|
||||
|
||||
let before = daemon.mountpoint.join("a.txt");
|
||||
std::fs::write(&before, b"0123456789").unwrap();
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
drive.tree().contains_key("a.txt")
|
||||
}));
|
||||
assert_eq!(std::fs::read(&before).unwrap(), b"0123456789");
|
||||
let after = daemon.mountpoint.join("b.txt");
|
||||
std::fs::rename(&before, &after).unwrap();
|
||||
// Long enough for the next event poll to bring the echo.
|
||||
std::thread::sleep(crate::POLL_INTERVAL + Duration::from_secs(3));
|
||||
|
||||
write_at(&after, 3, b"x").unwrap();
|
||||
write_at(&after, 5, b"y").unwrap();
|
||||
assert!(
|
||||
wait_until(Duration::from_secs(30), || {
|
||||
drive.tree().get("b.txt") == Some(&Entry::File(Arc::new(b"012x4y6789".to_vec())))
|
||||
}),
|
||||
"both writes landed: {:?}",
|
||||
drive.tree()
|
||||
);
|
||||
|
||||
assert!(matches!(daemon.stop(), Ok(MountOutcome::Shutdown)));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "mounts FUSE: run with `cargo test -p pdfs-fuse sim:: -- --ignored`"]
|
||||
fn a_rename_across_folders_ignores_the_new_name_in_the_old_folder() {
|
||||
// The rename went to Drive as a rename in the old folder and then a
|
||||
// move, so a sibling there with the new name failed it with EIO
|
||||
// (B122).
|
||||
let drive = FakeDrive::new();
|
||||
let dir = scratch("rename-across");
|
||||
let daemon = Daemon::start(&dir, drive.client(1, Faults::lan())).unwrap();
|
||||
|
||||
let mount = &daemon.mountpoint;
|
||||
std::fs::create_dir(mount.join("x")).unwrap();
|
||||
std::fs::create_dir(mount.join("z")).unwrap();
|
||||
std::fs::write(mount.join("x/f.txt"), b"moved").unwrap();
|
||||
std::fs::write(mount.join("x/d"), b"sibling").unwrap();
|
||||
std::fs::write(mount.join("z/d"), b"replaced").unwrap();
|
||||
let file = |bytes: &[u8]| Some(Entry::File(Arc::new(bytes.to_vec())));
|
||||
assert!(wait_until(Duration::from_secs(30), || {
|
||||
let tree = drive.tree();
|
||||
[
|
||||
("x/f.txt", b"moved".as_slice()),
|
||||
("x/d", b"sibling"),
|
||||
("z/d", b"replaced"),
|
||||
]
|
||||
.iter()
|
||||
.all(|(path, bytes)| tree.get(*path).cloned() == file(bytes))
|
||||
}));
|
||||
|
||||
std::fs::rename(mount.join("x/f.txt"), mount.join("z/d")).unwrap();
|
||||
assert_eq!(std::fs::read(mount.join("z/d")).unwrap(), b"moved");
|
||||
let tree = drive.tree();
|
||||
assert_eq!(tree.get("z/d").cloned(), file(b"moved"));
|
||||
assert_eq!(tree.get("x/d").cloned(), file(b"sibling"));
|
||||
assert!(!tree.contains_key("x/f.txt"));
|
||||
|
||||
assert!(matches!(daemon.stop(), Ok(MountOutcome::Shutdown)));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//! The simulation tests of `docs/MILESTONE-3.0.0.md` §8: a fake Drive with
|
||||
//! the faults the real one has, and the tools to drive daemons against it.
|
||||
|
||||
pub(crate) mod daemon;
|
||||
pub(crate) mod fake_drive;
|
||||
pub(crate) mod model;
|
||||
pub(crate) mod rng;
|
||||
pub(crate) mod run;
|
||||
pub(crate) mod watchdog;
|
||||
@@ -0,0 +1,445 @@
|
||||
//! The reference file system of the simulation (`docs/MILESTONE-3.0.0.md`
|
||||
//! §8.2, property 4): what each syscall a simulated client makes should
|
||||
//! answer, and the tree it should leave, on a POSIX file system.
|
||||
//!
|
||||
//! A [`Model`] covers one client's own folder; paths are relative to it. The
|
||||
//! same type is used for the trees read back from a mount and from Drive.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use super::rng::Rng;
|
||||
|
||||
/// A tree by path: `None` is a folder, `Some` a file's content.
|
||||
pub(crate) type Tree = BTreeMap<String, Option<Vec<u8>>>;
|
||||
|
||||
/// One file system call, or the few a program makes in a row.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) enum FsOp {
|
||||
/// `open(O_CREAT | O_TRUNC)`, write `data`, close.
|
||||
Create {
|
||||
path: String,
|
||||
data: Vec<u8>,
|
||||
},
|
||||
/// Open an existing file, write `data` at `offset` (past the end leaves
|
||||
/// a hole), close.
|
||||
Write {
|
||||
path: String,
|
||||
offset: u64,
|
||||
data: Vec<u8>,
|
||||
},
|
||||
/// Open an existing file and `ftruncate` it to `len`.
|
||||
Truncate {
|
||||
path: String,
|
||||
len: u64,
|
||||
},
|
||||
Mkdir {
|
||||
path: String,
|
||||
},
|
||||
Rename {
|
||||
from: String,
|
||||
to: String,
|
||||
},
|
||||
Unlink {
|
||||
path: String,
|
||||
},
|
||||
/// Open a file, unlink it, append `data`, read it all back, close.
|
||||
UnlinkOpen {
|
||||
path: String,
|
||||
data: Vec<u8>,
|
||||
},
|
||||
Rmdir {
|
||||
path: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl FsOp {
|
||||
/// The paths the op names, for the log.
|
||||
pub(crate) fn describe(&self) -> String {
|
||||
match self {
|
||||
FsOp::Create { path, data } => format!("create {path} ({} bytes)", data.len()),
|
||||
FsOp::Write { path, offset, data } => {
|
||||
format!("write {path} at {offset} ({} bytes)", data.len())
|
||||
}
|
||||
FsOp::Truncate { path, len } => format!("truncate {path} to {len}"),
|
||||
FsOp::Mkdir { path } => format!("mkdir {path}"),
|
||||
FsOp::Rename { from, to } => format!("rename {from} -> {to}"),
|
||||
FsOp::Unlink { path } => format!("unlink {path}"),
|
||||
FsOp::UnlinkOpen { path, data } => {
|
||||
format!("unlink {path} while open, then append {} bytes", data.len())
|
||||
}
|
||||
FsOp::Rmdir { path } => format!("rmdir {path}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The tree one client should have, and what its syscalls should answer.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub(crate) struct Model {
|
||||
tree: Tree,
|
||||
}
|
||||
|
||||
/// Names are drawn from small pools, so ops collide: a create over an existing
|
||||
/// file, a rename over another, a mkdir of a name that is taken.
|
||||
const FILE_NAMES: [&str; 6] = ["a.txt", "b.txt", "c.bin", "d", "e.md", "f.txt"];
|
||||
const DIR_NAMES: [&str; 3] = ["x", "y", "z"];
|
||||
|
||||
impl Model {
|
||||
pub(crate) fn tree(&self) -> &Tree {
|
||||
&self.tree
|
||||
}
|
||||
|
||||
pub(crate) fn content(&self, path: &str) -> Option<&Vec<u8>> {
|
||||
self.tree.get(path)?.as_ref()
|
||||
}
|
||||
|
||||
/// What `op` should answer on a POSIX file system, applying it on success.
|
||||
pub(crate) fn apply(&mut self, op: &FsOp) -> Result<(), i32> {
|
||||
match op {
|
||||
FsOp::Create { path, data } => {
|
||||
self.check_parent(path)?;
|
||||
match self.tree.get(path) {
|
||||
Some(None) => Err(libc::EISDIR),
|
||||
_ => {
|
||||
self.tree.insert(path.clone(), Some(data.clone()));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
FsOp::Write { path, offset, data } => {
|
||||
let content = self.file_mut(path)?;
|
||||
// Writing nothing changes nothing, even past the end.
|
||||
if data.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let end = *offset as usize + data.len();
|
||||
if content.len() < end {
|
||||
content.resize(end, 0);
|
||||
}
|
||||
content[*offset as usize..end].copy_from_slice(data);
|
||||
Ok(())
|
||||
}
|
||||
FsOp::Truncate { path, len } => {
|
||||
self.file_mut(path)?.resize(*len as usize, 0);
|
||||
Ok(())
|
||||
}
|
||||
FsOp::Mkdir { path } => {
|
||||
self.check_parent(path)?;
|
||||
if self.tree.contains_key(path) {
|
||||
return Err(libc::EEXIST);
|
||||
}
|
||||
self.tree.insert(path.clone(), None);
|
||||
Ok(())
|
||||
}
|
||||
FsOp::Rename { from, to } => self.rename(from, to),
|
||||
FsOp::Unlink { path } | FsOp::UnlinkOpen { path, .. } => {
|
||||
self.check_parent(path)?;
|
||||
match self.tree.get(path) {
|
||||
None => Err(libc::ENOENT),
|
||||
Some(None) => Err(libc::EISDIR),
|
||||
Some(Some(_)) => {
|
||||
self.tree.remove(path);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
FsOp::Rmdir { path } => {
|
||||
self.check_parent(path)?;
|
||||
match self.tree.get(path) {
|
||||
None => Err(libc::ENOENT),
|
||||
Some(Some(_)) => Err(libc::ENOTDIR),
|
||||
Some(None) if self.has_children(path) => Err(libc::ENOTEMPTY),
|
||||
Some(None) => {
|
||||
self.tree.remove(path);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rename(&mut self, from: &str, to: &str) -> Result<(), i32> {
|
||||
self.check_parent(from)?;
|
||||
let Some(moving) = self.tree.get(from).cloned() else {
|
||||
return Err(libc::ENOENT);
|
||||
};
|
||||
self.check_parent(to)?;
|
||||
if from == to {
|
||||
return Ok(());
|
||||
}
|
||||
if is_below(to, from) {
|
||||
return Err(libc::EINVAL);
|
||||
}
|
||||
match (&moving, self.tree.get(to)) {
|
||||
(None, Some(Some(_))) => return Err(libc::ENOTDIR),
|
||||
(Some(_), Some(None)) => return Err(libc::EISDIR),
|
||||
(None, Some(None)) if self.has_children(to) => return Err(libc::ENOTEMPTY),
|
||||
_ => {}
|
||||
}
|
||||
let below: Vec<(String, Option<Vec<u8>>)> = self
|
||||
.tree
|
||||
.iter()
|
||||
.filter(|(path, _)| is_below(path, from))
|
||||
.map(|(path, entry)| (path.clone(), entry.clone()))
|
||||
.collect();
|
||||
self.tree.remove(from);
|
||||
self.tree.insert(to.to_owned(), moving);
|
||||
for (path, entry) in below {
|
||||
self.tree.remove(&path);
|
||||
self.tree
|
||||
.insert(format!("{to}{}", &path[from.len()..]), entry);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn file_mut(&mut self, path: &str) -> Result<&mut Vec<u8>, i32> {
|
||||
self.check_parent(path)?;
|
||||
match self.tree.get_mut(path) {
|
||||
None => Err(libc::ENOENT),
|
||||
Some(None) => Err(libc::EISDIR),
|
||||
Some(Some(content)) => Ok(content),
|
||||
}
|
||||
}
|
||||
|
||||
fn check_parent(&self, path: &str) -> Result<(), i32> {
|
||||
let mut at = String::new();
|
||||
let mut names = path.split('/').peekable();
|
||||
while let Some(name) = names.next() {
|
||||
if names.peek().is_none() {
|
||||
break;
|
||||
}
|
||||
if !at.is_empty() {
|
||||
at.push('/');
|
||||
}
|
||||
at.push_str(name);
|
||||
match self.tree.get(&at) {
|
||||
None => return Err(libc::ENOENT),
|
||||
Some(Some(_)) => return Err(libc::ENOTDIR),
|
||||
Some(None) => {}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn has_children(&self, path: &str) -> bool {
|
||||
self.tree.keys().any(|other| is_below(other, path))
|
||||
}
|
||||
|
||||
pub(crate) fn files(&self) -> Vec<&String> {
|
||||
self.tree
|
||||
.iter()
|
||||
.filter(|(_, entry)| entry.is_some())
|
||||
.map(|(path, _)| path)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Folders, the client's own root (`""`) first.
|
||||
fn dirs(&self) -> Vec<String> {
|
||||
std::iter::once(String::new())
|
||||
.chain(
|
||||
self.tree
|
||||
.iter()
|
||||
.filter(|(_, entry)| entry.is_none())
|
||||
.map(|(path, _)| path.clone()),
|
||||
)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A random op, mostly one that should succeed.
|
||||
pub(crate) fn next_op(&self, rng: &mut Rng) -> FsOp {
|
||||
let dirs = self.dirs();
|
||||
let files = self.files();
|
||||
let in_dir = |rng: &mut Rng, names: &[&str]| {
|
||||
let parent = rng.pick(&dirs).expect("the root is always there");
|
||||
join(parent, rng.pick(names).expect("names"))
|
||||
};
|
||||
let a_file = |rng: &mut Rng| rng.pick(&files).map(|path| (*path).clone());
|
||||
let roll = rng.below(100);
|
||||
let op = match roll {
|
||||
0..30 => Some(FsOp::Create {
|
||||
path: in_dir(rng, &FILE_NAMES),
|
||||
data: data(rng),
|
||||
}),
|
||||
30..45 => a_file(rng).map(|path| {
|
||||
let len = self.content(&path).map_or(0, Vec::len) as u64;
|
||||
FsOp::Write {
|
||||
offset: rng.below(len + 8),
|
||||
path,
|
||||
data: data(rng),
|
||||
}
|
||||
}),
|
||||
45..50 => a_file(rng).map(|path| FsOp::Truncate {
|
||||
len: rng.below(32),
|
||||
path,
|
||||
}),
|
||||
50..60 => Some(FsOp::Mkdir {
|
||||
path: in_dir(rng, &DIR_NAMES),
|
||||
}),
|
||||
// A rename in place, over another file as often as not.
|
||||
60..72 => a_file(rng).map(|from| FsOp::Rename {
|
||||
to: join(parent_of(&from), rng.pick(&FILE_NAMES).expect("names")),
|
||||
from,
|
||||
}),
|
||||
// A move to another folder, renamed or not.
|
||||
72..82 => a_file(rng).map(|from| {
|
||||
let name = if rng.chance(0.5) {
|
||||
name_of(&from)
|
||||
} else {
|
||||
rng.pick(&FILE_NAMES).expect("names")
|
||||
};
|
||||
let parent = rng.pick(&dirs).expect("the root is always there");
|
||||
FsOp::Rename {
|
||||
to: join(parent, name),
|
||||
from,
|
||||
}
|
||||
}),
|
||||
// A folder renamed or moved, with what is in it.
|
||||
82..86 => {
|
||||
let from = rng.pick(&dirs[1..]).cloned();
|
||||
from.map(|from| FsOp::Rename {
|
||||
to: in_dir(rng, &DIR_NAMES),
|
||||
from,
|
||||
})
|
||||
}
|
||||
86..94 => a_file(rng).map(|path| FsOp::Unlink { path }),
|
||||
94..97 => a_file(rng).map(|path| FsOp::UnlinkOpen {
|
||||
path,
|
||||
data: data(rng),
|
||||
}),
|
||||
_ => rng
|
||||
.pick(&dirs[1..])
|
||||
.cloned()
|
||||
.map(|path| FsOp::Rmdir { path }),
|
||||
};
|
||||
op.unwrap_or_else(|| FsOp::Create {
|
||||
path: in_dir(rng, &FILE_NAMES),
|
||||
data: data(rng),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Content for a write: short and recognisable, now and then empty.
|
||||
pub(crate) fn data(rng: &mut Rng) -> Vec<u8> {
|
||||
let len = if rng.chance(0.1) {
|
||||
0
|
||||
} else {
|
||||
rng.between(1, 48)
|
||||
};
|
||||
let tag = rng.next_u64();
|
||||
(0..len).map(|i| b'a' + ((tag + i) % 26) as u8).collect()
|
||||
}
|
||||
|
||||
pub(crate) fn join(parent: &str, name: &str) -> String {
|
||||
if parent.is_empty() {
|
||||
name.to_owned()
|
||||
} else {
|
||||
format!("{parent}/{name}")
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn parent_of(path: &str) -> &str {
|
||||
path.rsplit_once('/').map_or("", |(parent, _)| parent)
|
||||
}
|
||||
|
||||
pub(crate) fn name_of(path: &str) -> &str {
|
||||
path.rsplit_once('/').map_or(path, |(_, name)| name)
|
||||
}
|
||||
|
||||
fn is_below(path: &str, ancestor: &str) -> bool {
|
||||
path.len() > ancestor.len() + 1
|
||||
&& path.starts_with(ancestor)
|
||||
&& path.as_bytes()[ancestor.len()] == b'/'
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn create(path: &str, data: &[u8]) -> FsOp {
|
||||
FsOp::Create {
|
||||
path: path.into(),
|
||||
data: data.to_vec(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_rename_over_a_file_replaces_it_and_a_folder_moves_with_its_contents() {
|
||||
let mut model = Model::default();
|
||||
model.apply(&FsOp::Mkdir { path: "x".into() }).unwrap();
|
||||
model.apply(&create("x/a", b"1")).unwrap();
|
||||
model.apply(&create("b", b"2")).unwrap();
|
||||
model
|
||||
.apply(&FsOp::Rename {
|
||||
from: "b".into(),
|
||||
to: "x/a".into(),
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(model.content("x/a"), Some(&b"2".to_vec()));
|
||||
model
|
||||
.apply(&FsOp::Rename {
|
||||
from: "x".into(),
|
||||
to: "y".into(),
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(model.tree().keys().collect::<Vec<_>>(), vec!["y", "y/a"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn calls_fail_the_way_posix_says() {
|
||||
let mut model = Model::default();
|
||||
model.apply(&FsOp::Mkdir { path: "x".into() }).unwrap();
|
||||
model.apply(&create("x/a", b"1")).unwrap();
|
||||
assert_eq!(
|
||||
model.apply(&FsOp::Mkdir { path: "x".into() }),
|
||||
Err(libc::EEXIST)
|
||||
);
|
||||
assert_eq!(model.apply(&create("x", b"")), Err(libc::EISDIR));
|
||||
assert_eq!(model.apply(&create("x/a/b", b"")), Err(libc::ENOTDIR));
|
||||
assert_eq!(model.apply(&create("q/a", b"")), Err(libc::ENOENT));
|
||||
assert_eq!(
|
||||
model.apply(&FsOp::Rmdir { path: "x".into() }),
|
||||
Err(libc::ENOTEMPTY)
|
||||
);
|
||||
assert_eq!(
|
||||
model.apply(&FsOp::Unlink { path: "x".into() }),
|
||||
Err(libc::EISDIR)
|
||||
);
|
||||
let into_itself = FsOp::Rename {
|
||||
from: "x".into(),
|
||||
to: "x/y".into(),
|
||||
};
|
||||
assert_eq!(model.apply(&into_itself), Err(libc::EINVAL));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_write_past_the_end_leaves_a_hole() {
|
||||
let mut model = Model::default();
|
||||
model.apply(&create("a", b"ab")).unwrap();
|
||||
let write = FsOp::Write {
|
||||
path: "a".into(),
|
||||
offset: 4,
|
||||
data: b"z".to_vec(),
|
||||
};
|
||||
model.apply(&write).unwrap();
|
||||
assert_eq!(model.content("a"), Some(&b"ab\0\0z".to_vec()));
|
||||
let nothing = FsOp::Write {
|
||||
path: "a".into(),
|
||||
offset: 9,
|
||||
data: Vec::new(),
|
||||
};
|
||||
model.apply(¬hing).unwrap();
|
||||
assert_eq!(model.content("a"), Some(&b"ab\0\0z".to_vec()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn generated_ops_mostly_succeed() {
|
||||
let mut rng = Rng::new(9);
|
||||
let mut model = Model::default();
|
||||
let ok = (0..500)
|
||||
.filter(|_| {
|
||||
let op = model.next_op(&mut rng);
|
||||
model.apply(&op).is_ok()
|
||||
})
|
||||
.count();
|
||||
assert!(ok > 350, "{ok} of 500 succeeded");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
//! A small seeded random number generator, so a failing simulation is a seed
|
||||
//! that can be replayed. SplitMix64: fast, good enough for choosing operations
|
||||
//! and faults, and no dependency.
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct Rng(u64);
|
||||
|
||||
impl Rng {
|
||||
pub(crate) fn new(seed: u64) -> Self {
|
||||
Self(seed)
|
||||
}
|
||||
|
||||
pub(crate) fn next_u64(&mut self) -> u64 {
|
||||
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
|
||||
let mut z = self.0;
|
||||
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
|
||||
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
|
||||
z ^ (z >> 31)
|
||||
}
|
||||
|
||||
/// A value in `0..n`; `0` when `n` is `0`.
|
||||
pub(crate) fn below(&mut self, n: u64) -> u64 {
|
||||
if n == 0 { 0 } else { self.next_u64() % n }
|
||||
}
|
||||
|
||||
/// A value in `lo..=hi`.
|
||||
pub(crate) fn between(&mut self, lo: u64, hi: u64) -> u64 {
|
||||
lo + self.below(hi.saturating_sub(lo) + 1)
|
||||
}
|
||||
|
||||
/// True with probability `p`.
|
||||
pub(crate) fn chance(&mut self, p: f64) -> bool {
|
||||
p > 0.0 && (self.next_u64() >> 11) as f64 / (1u64 << 53) as f64 <= p
|
||||
}
|
||||
|
||||
pub(crate) fn pick<'a, T>(&mut self, items: &'a [T]) -> Option<&'a T> {
|
||||
items.get(self.below(items.len() as u64) as usize)
|
||||
}
|
||||
|
||||
/// A child generator, so one consumer's draws do not shift another's.
|
||||
pub(crate) fn fork(&mut self) -> Self {
|
||||
Self::new(self.next_u64())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Rng;
|
||||
|
||||
#[test]
|
||||
fn a_seed_replays_the_same_sequence() {
|
||||
let (mut a, mut b) = (Rng::new(7), Rng::new(7));
|
||||
for _ in 0..100 {
|
||||
assert_eq!(a.next_u64(), b.next_u64());
|
||||
}
|
||||
assert_ne!(Rng::new(7).next_u64(), Rng::new(8).next_u64());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bounded_draws_stay_in_range() {
|
||||
let mut rng = Rng::new(1);
|
||||
for _ in 0..1000 {
|
||||
assert!(rng.below(5) < 5);
|
||||
assert!((3..=9).contains(&rng.between(3, 9)));
|
||||
}
|
||||
assert_eq!(rng.below(0), 0);
|
||||
assert!(!rng.chance(0.0));
|
||||
assert!(rng.chance(1.0));
|
||||
assert!([1, 2, 3].contains(rng.pick(&[1, 2, 3]).unwrap()));
|
||||
assert_eq!(rng.pick::<u8>(&[]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fork_draws_apart_from_its_parent() {
|
||||
let mut parent = Rng::new(3);
|
||||
let mut child = parent.fork();
|
||||
assert_ne!(parent.next_u64(), child.next_u64());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
//! Turns a call through a mount that never returns into a failed seed.
|
||||
//!
|
||||
//! A deadlock in a daemon leaves the run's own thread blocked in the kernel,
|
||||
//! where no budget check is reached, and a CI job then waits for its timeout.
|
||||
//! The watchdog runs beside the run. When what it was armed for outlives its
|
||||
//! limit, it aborts the FUSE connections of the run's mounts, so the blocked
|
||||
//! call fails and the run reports the hang, and writes where every thread was
|
||||
//! to `stacks.txt`.
|
||||
//!
|
||||
//! The stacks are taken in the process: no tracer can stop a thread that
|
||||
//! waits on a FUSE request, so `gdb` and `eu-stack` wait as long as the hang.
|
||||
//! Each thread is sent [`stack_signal`] and captures its own backtrace in the
|
||||
//! handler. A thread in the kernel takes the signal only once it leaves, so
|
||||
//! the mounts are aborted after the others have answered, and its backtrace
|
||||
//! is the call it was blocked in. Its kernel wait channel is recorded first.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::thread::JoinHandle;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use parking_lot::Mutex;
|
||||
|
||||
use super::daemon::wait_until;
|
||||
|
||||
/// How long the threads that are not blocked in the kernel have to answer,
|
||||
/// and the blocked ones once the mounts are aborted.
|
||||
const ANSWER: Duration = Duration::from_secs(5);
|
||||
|
||||
pub(crate) struct Watchdog {
|
||||
shared: Arc<Shared>,
|
||||
thread: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
struct Shared {
|
||||
/// The run's directory: its mounts are below it, and the stacks go in it.
|
||||
dir: PathBuf,
|
||||
armed: Mutex<Option<Armed>>,
|
||||
/// What hung, once something has.
|
||||
fired: Mutex<Option<Hung>>,
|
||||
stop: AtomicBool,
|
||||
}
|
||||
|
||||
struct Armed {
|
||||
what: String,
|
||||
limit: Duration,
|
||||
since: Instant,
|
||||
}
|
||||
|
||||
/// What hung, and where every thread was then.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) struct Hung {
|
||||
pub(crate) what: String,
|
||||
pub(crate) stacks: String,
|
||||
}
|
||||
|
||||
/// Disarms the watchdog when dropped.
|
||||
pub(crate) struct Watching(Arc<Shared>);
|
||||
|
||||
impl Watchdog {
|
||||
pub(crate) fn new(dir: &Path) -> Self {
|
||||
let shared = Arc::new(Shared {
|
||||
dir: dir.to_path_buf(),
|
||||
armed: Mutex::new(None),
|
||||
fired: Mutex::new(None),
|
||||
stop: AtomicBool::new(false),
|
||||
});
|
||||
let thread = {
|
||||
let shared = shared.clone();
|
||||
std::thread::Builder::new()
|
||||
.name("sim-watchdog".into())
|
||||
.spawn(move || watch(&shared))
|
||||
.expect("spawn the simulation watchdog")
|
||||
};
|
||||
Self {
|
||||
shared,
|
||||
thread: Some(thread),
|
||||
}
|
||||
}
|
||||
|
||||
/// Watch `what` until the guard is dropped, and abort the mounts if it is
|
||||
/// still going after `limit`.
|
||||
pub(crate) fn arm(&self, limit: Duration, what: impl Into<String>) -> Watching {
|
||||
*self.shared.armed.lock() = Some(Armed {
|
||||
what: what.into(),
|
||||
limit,
|
||||
since: Instant::now(),
|
||||
});
|
||||
Watching(self.shared.clone())
|
||||
}
|
||||
|
||||
/// What hung, if something did. Call [`Watchdog::stop`] first, or the
|
||||
/// stacks may still be on their way.
|
||||
pub(crate) fn fired(&self) -> Option<Hung> {
|
||||
self.shared.fired.lock().clone()
|
||||
}
|
||||
|
||||
/// Stop watching, once the stacks of a hang that was caught are taken.
|
||||
pub(crate) fn stop(&mut self) {
|
||||
self.shared.stop.store(true, Ordering::Relaxed);
|
||||
if let Some(thread) = self.thread.take() {
|
||||
let _ = thread.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Watching {
|
||||
fn drop(&mut self) {
|
||||
*self.0.armed.lock() = None;
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Watchdog {
|
||||
fn drop(&mut self) {
|
||||
self.stop();
|
||||
}
|
||||
}
|
||||
|
||||
fn watch(shared: &Shared) {
|
||||
while !shared.stop.load(Ordering::Relaxed) {
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
let hung = shared
|
||||
.armed
|
||||
.lock()
|
||||
.as_ref()
|
||||
.filter(|armed| armed.since.elapsed() > armed.limit)
|
||||
.map(|armed| format!("{} has not returned after {:?}", armed.what, armed.limit));
|
||||
let Some(hung) = hung else { continue };
|
||||
*shared.armed.lock() = None;
|
||||
let path = shared.dir.join("stacks.txt");
|
||||
// Before anything is let go: the run reports the hang, not whatever
|
||||
// the aborted call answers.
|
||||
*shared.fired.lock() = Some(Hung {
|
||||
what: format!("{hung}; the stacks are in {}", path.display()),
|
||||
stacks: String::new(),
|
||||
});
|
||||
let stacks = take_stacks(|| abort_mounts_below(&shared.dir));
|
||||
let what = match std::fs::write(&path, &stacks) {
|
||||
Ok(()) => format!("{hung}; the stacks are in {}", path.display()),
|
||||
Err(error) => format!("{hung}; the stacks could not be kept: {error}"),
|
||||
};
|
||||
*shared.fired.lock() = Some(Hung { what, stacks });
|
||||
}
|
||||
}
|
||||
|
||||
/// One thread of this process as the kernel sees it.
|
||||
struct Task {
|
||||
tid: i32,
|
||||
name: String,
|
||||
/// `D` while it waits in the kernel uninterruptibly.
|
||||
state: String,
|
||||
wchan: String,
|
||||
}
|
||||
|
||||
/// Where every other thread of this process is, with `release` called once
|
||||
/// those not blocked in the kernel have answered.
|
||||
fn take_stacks(release: impl FnOnce()) -> String {
|
||||
let own = gettid();
|
||||
let tasks = tasks();
|
||||
STACKS.lock().clear();
|
||||
install_stack_handler();
|
||||
for task in &tasks {
|
||||
if task.tid != own {
|
||||
// SAFETY: tgkill only sends a signal, whose handler is installed.
|
||||
unsafe {
|
||||
libc::syscall(
|
||||
libc::SYS_tgkill,
|
||||
std::process::id() as i32,
|
||||
task.tid,
|
||||
stack_signal(),
|
||||
)
|
||||
};
|
||||
}
|
||||
}
|
||||
let answered = |blocked: bool| {
|
||||
let stacks = STACKS.lock();
|
||||
tasks
|
||||
.iter()
|
||||
.filter(|task| task.tid != own && (blocked || task.state != "D"))
|
||||
.all(|task| stacks.contains_key(&task.tid))
|
||||
};
|
||||
wait_until(ANSWER, || answered(false));
|
||||
release();
|
||||
wait_until(ANSWER, || answered(true));
|
||||
let stacks = std::mem::take(&mut *STACKS.lock());
|
||||
let mut out = String::new();
|
||||
for task in tasks.iter().filter(|task| task.tid != own) {
|
||||
out.push_str(&format!(
|
||||
"thread {} {:?}, state {}, waiting in {}\n",
|
||||
task.tid, task.name, task.state, task.wchan
|
||||
));
|
||||
match stacks.get(&task.tid) {
|
||||
Some(stack) => out.push_str(&format!("{stack}\n")),
|
||||
None => out.push_str(" (no answer)\n\n"),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn tasks() -> Vec<Task> {
|
||||
let Ok(entries) = std::fs::read_dir("/proc/self/task") else {
|
||||
return Vec::new();
|
||||
};
|
||||
let read = |dir: &Path, file: &str| {
|
||||
std::fs::read_to_string(dir.join(file))
|
||||
.unwrap_or_default()
|
||||
.trim()
|
||||
.to_owned()
|
||||
};
|
||||
entries
|
||||
.flatten()
|
||||
.filter_map(|entry| {
|
||||
let tid = entry.file_name().to_str()?.parse().ok()?;
|
||||
let dir = entry.path();
|
||||
// The state follows the name in parentheses, which may hold spaces.
|
||||
let stat = read(&dir, "stat");
|
||||
let state = stat.rsplit_once(") ")?.1.split(' ').next()?.to_owned();
|
||||
Some(Task {
|
||||
tid,
|
||||
name: read(&dir, "comm"),
|
||||
state,
|
||||
wchan: read(&dir, "wchan"),
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The backtraces the threads captured, by thread id.
|
||||
static STACKS: Mutex<BTreeMap<i32, std::backtrace::Backtrace>> = Mutex::new(BTreeMap::new());
|
||||
|
||||
/// The signal that asks a thread for its stack: one of the real-time signals
|
||||
/// that nothing in the daemon uses.
|
||||
fn stack_signal() -> i32 {
|
||||
libc::SIGRTMIN() + 7
|
||||
}
|
||||
|
||||
fn install_stack_handler() {
|
||||
static INSTALLED: std::sync::Once = std::sync::Once::new();
|
||||
extern "C" fn handler(_: i32) {
|
||||
// Not async-signal-safe: the capture allocates. The process is being
|
||||
// taken down for a hang anyway, and a thread interrupted inside the
|
||||
// allocator only fails to answer.
|
||||
let stack = std::backtrace::Backtrace::force_capture();
|
||||
STACKS.lock().insert(gettid(), stack);
|
||||
}
|
||||
INSTALLED.call_once(|| {
|
||||
// SAFETY: a zeroed sigaction with a handler and SA_RESTART is valid,
|
||||
// and the handler only touches `STACKS`.
|
||||
unsafe {
|
||||
let mut action: libc::sigaction = std::mem::zeroed();
|
||||
action.sa_sigaction = handler as extern "C" fn(i32) as usize;
|
||||
action.sa_flags = libc::SA_RESTART;
|
||||
libc::sigemptyset(&mut action.sa_mask);
|
||||
libc::sigaction(stack_signal(), &action, std::ptr::null_mut());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn gettid() -> i32 {
|
||||
// SAFETY: gettid has no arguments and cannot fail.
|
||||
unsafe { libc::gettid() }
|
||||
}
|
||||
|
||||
/// Abort the FUSE connection of every mount below `dir`. Its connection is
|
||||
/// named by the mount's device minor, and the mount's owner may abort it.
|
||||
fn abort_mounts_below(dir: &Path) {
|
||||
let Ok(mountinfo) = std::fs::read_to_string("/proc/self/mountinfo") else {
|
||||
return;
|
||||
};
|
||||
for line in mountinfo.lines() {
|
||||
let fields: Vec<&str> = line.split(' ').collect();
|
||||
let (Some(device), Some(mountpoint)) = (fields.get(2), fields.get(4)) else {
|
||||
continue;
|
||||
};
|
||||
if !Path::new(mountpoint).starts_with(dir) {
|
||||
continue;
|
||||
}
|
||||
if let Some((_, minor)) = device.split_once(':') {
|
||||
let _ = std::fs::write(format!("/sys/fs/fuse/connections/{minor}/abort"), "1");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::sim::daemon::scratch;
|
||||
|
||||
#[test]
|
||||
#[ignore = "signals every thread of the test process: run with `cargo test -p pdfs-fuse sim:: -- --ignored`"]
|
||||
fn a_call_past_its_limit_is_reported_with_the_stacks() {
|
||||
let dir = scratch("watchdog");
|
||||
let mut watchdog = Watchdog::new(&dir);
|
||||
{
|
||||
let _watching = watchdog.arm(Duration::from_millis(100), "a nap");
|
||||
std::thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
watchdog.stop();
|
||||
let hung = watchdog.fired().expect("the watchdog fired");
|
||||
assert!(
|
||||
hung.what.starts_with("a nap has not returned after"),
|
||||
"{hung:?}"
|
||||
);
|
||||
assert!(hung.stacks.contains("a_call_past_its_limit"), "{hung:?}");
|
||||
let kept = std::fs::read_to_string(dir.join("stacks.txt")).unwrap_or_default();
|
||||
assert_eq!(kept, hung.stacks);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_call_within_its_limit_is_not() {
|
||||
let dir = scratch("watchdog-quiet");
|
||||
let watchdog = Watchdog::new(&dir);
|
||||
drop(watchdog.arm(Duration::from_millis(100), "a quick call"));
|
||||
std::thread::sleep(Duration::from_millis(500));
|
||||
assert_eq!(watchdog.fired(), None);
|
||||
drop(watchdog);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
@@ -128,6 +128,61 @@ pub(crate) struct WriteHandle {
|
||||
pub(crate) open_count: usize,
|
||||
}
|
||||
|
||||
impl WriteHandle {
|
||||
/// A handle on a file `create` just made: an empty base, so everything
|
||||
/// written through it is authored.
|
||||
///
|
||||
/// The base is `node` as it actually exists — the empty file the server just
|
||||
/// minted, or a queued create's placeholder — never the local clock or a
|
||||
/// blank. `queue_revision` turns it into `StagedWrite::based_on` and the
|
||||
/// drain compares the remote against that. A `now_secs()` mtime differs from
|
||||
/// the server's stamp by the create round-trip. A missing revision id drops
|
||||
/// the check to `(mtime, size)`, and moving or renaming the file advances
|
||||
/// its mtime on the server. Either way the first write to a brand-new file
|
||||
/// lands as a conflict copy of its own empty create (`docs/BUGS.md` B113).
|
||||
pub(crate) fn created(ino: u64, node: &Node, file: File, path: PathBuf) -> Self {
|
||||
Self {
|
||||
ino,
|
||||
uid: node.uid.clone(),
|
||||
file: Arc::new(file),
|
||||
path,
|
||||
written: Intervals::default(),
|
||||
len: 0,
|
||||
base_size: 0,
|
||||
base_mtime: node.modification_time,
|
||||
base_revision_id: crate::node_revision_id(node),
|
||||
dirty: false,
|
||||
open_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Write over `node` from now on: a revision this daemon has just put on the
|
||||
/// server under the handle, by sealing an upload or by landing the queued
|
||||
/// create the handle was opened on. The base the handle opened over is gone,
|
||||
/// and keeping it would make the drain read our own revision as another
|
||||
/// device's edit.
|
||||
pub(crate) fn rebase_onto(&mut self, node: &Node) {
|
||||
self.base_mtime = node.modification_time;
|
||||
self.base_size = crate::node_size(node);
|
||||
self.base_revision_id = crate::node_revision_id(node);
|
||||
}
|
||||
|
||||
/// Account for `[offset, end)` having been written through this handle.
|
||||
///
|
||||
/// A write past the end leaves a hole that reads as zeros, exactly like a
|
||||
/// grow through `setattr`, so the hole is authored too. Left unauthored, a
|
||||
/// hole below the base size was refilled from the base at commit: a file
|
||||
/// shrunk and then written past its new end got its old bytes back.
|
||||
pub(crate) fn record_write(&mut self, offset: u64, end: u64) {
|
||||
if offset > self.len {
|
||||
self.written.add(self.len, offset);
|
||||
}
|
||||
self.written.add(offset, end);
|
||||
self.len = self.len.max(end);
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// A released write whose upload has not happened yet (offline.md Phase 3).
|
||||
///
|
||||
/// The bytes live in the content cache's staging dir and the intent lives in the
|
||||
@@ -146,8 +201,23 @@ pub(crate) struct PendingRevision {
|
||||
/// time so this module stays free of `fuser`.
|
||||
pub(crate) type DirListing = Vec<(u64, bool, String)>;
|
||||
|
||||
/// Where [`State::next_ino`] starts: far above any local id a database hands
|
||||
/// out, so a fallback inode never names a node that has a row.
|
||||
pub(crate) const FALLBACK_INO: u64 = 1 << 48;
|
||||
|
||||
/// The inode of the node with local id `lid` ([`Db::lids_for`]). One above the
|
||||
/// lid, so no node takes [`crate::ROOT_INO`], which each session uses for its
|
||||
/// own root. The same in every mount and after a restart.
|
||||
pub(crate) fn lid_ino(lid: i64) -> u64 {
|
||||
lid.unsigned_abs() + crate::ROOT_INO
|
||||
}
|
||||
|
||||
/// Mutable inode bookkeeping, guarded by a mutex because fuser drives the
|
||||
/// `Filesystem` trait through `&self`.
|
||||
///
|
||||
/// One tree for every mount of the daemon. Each FUSE session sees it from its
|
||||
/// own root ([`State::roots`]) and calls that root inode 1; `ProtonFs`
|
||||
/// translates at the kernel boundary.
|
||||
pub(crate) struct State {
|
||||
/// inode -> node metadata.
|
||||
pub(crate) entries: HashMap<u64, Entry>,
|
||||
@@ -156,6 +226,12 @@ pub(crate) struct State {
|
||||
/// Cached directory listings: parent inode -> child inodes. Presence of a
|
||||
/// key means the directory has been enumerated.
|
||||
pub(crate) children: HashMap<u64, Vec<u64>>,
|
||||
/// The inodes some session has been rooted at ([`State::install_root`]).
|
||||
/// A root is its own parent. Kept after its session unmounts: the node is
|
||||
/// still one of ours.
|
||||
pub(crate) roots: HashSet<u64>,
|
||||
/// The next inode for a node the database could not give a local id, from
|
||||
/// a range lids never reach ([`FALLBACK_INO`]).
|
||||
pub(crate) next_ino: u64,
|
||||
/// Shared write state keyed by inode. Concurrent writers share the scratch file.
|
||||
pub(crate) active_writes: HashMap<u64, WriteHandle>,
|
||||
@@ -177,7 +253,7 @@ pub(crate) struct State {
|
||||
/// intern/root refresh. Core drains this set and notifies the matching
|
||||
/// kernel only after releasing the State lock.
|
||||
pub(crate) access_changes: HashSet<u64>,
|
||||
/// Persisted shared-root authority, loaded once per inode space and updated
|
||||
/// Persisted shared-root authority, loaded once and updated
|
||||
/// on every explicit role/tombstone change. Descendant inheritance reads
|
||||
/// this map instead of issuing one SQLite query per interned inode.
|
||||
pub(crate) share_access: HashMap<NodeUid, Access>,
|
||||
@@ -245,15 +321,6 @@ pub(crate) struct StateGuard<'a> {
|
||||
db: &'a Arc<Db>,
|
||||
}
|
||||
|
||||
/// Take a mount's inode lock through a [`StateGuard`], for the loops that walk
|
||||
/// every live mount rather than this `Core`'s own.
|
||||
pub(crate) fn lock_state<'a>(
|
||||
state: &'a parking_lot::Mutex<State>,
|
||||
db: &'a Arc<Db>,
|
||||
) -> StateGuard<'a> {
|
||||
StateGuard::new(state.lock(), db)
|
||||
}
|
||||
|
||||
impl<'a> StateGuard<'a> {
|
||||
pub(crate) fn new(guard: parking_lot::MutexGuard<'a, State>, db: &'a Arc<Db>) -> Self {
|
||||
Self {
|
||||
@@ -294,9 +361,6 @@ impl Drop for StateGuard<'_> {
|
||||
impl State {
|
||||
/// A fresh, empty inode space over `db`.
|
||||
///
|
||||
/// `next_ino` differs by caller: a real mount hands out [`crate::ROOT_INO`]
|
||||
/// to its root and starts allocating above it, while a bare state under test
|
||||
/// has no root and starts at 1.
|
||||
/// Apply the write-throughs this state owes SQLite, here and now.
|
||||
///
|
||||
/// Production code never calls this: [`StateGuard`] releases the inode lock
|
||||
@@ -323,12 +387,15 @@ impl State {
|
||||
self.db.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn new(db: Arc<Db>, share_access: HashMap<NodeUid, Access>, next_ino: u64) -> Self {
|
||||
/// A fresh, empty tree over `db`. A node's inode comes from its local id
|
||||
/// ([`lid_ino`]).
|
||||
pub(crate) fn new(db: Arc<Db>, share_access: HashMap<NodeUid, Access>) -> Self {
|
||||
Self {
|
||||
entries: HashMap::new(),
|
||||
by_uid: HashMap::new(),
|
||||
children: HashMap::new(),
|
||||
next_ino,
|
||||
roots: HashSet::new(),
|
||||
next_ino: FALLBACK_INO,
|
||||
active_writes: HashMap::new(),
|
||||
handles: HashMap::new(),
|
||||
next_fh: 1,
|
||||
@@ -339,18 +406,55 @@ impl State {
|
||||
outbox: Vec::new(),
|
||||
}
|
||||
}
|
||||
/// Whether `uid` names an owned node currently reachable from this mount's
|
||||
/// root. Resident entries can outlive their dentries for open-handle and
|
||||
/// access-revocation semantics, so `by_uid` membership alone is not proof
|
||||
/// that a control-socket operation may address the node.
|
||||
pub(crate) fn owns_visible_uid(&self, uid: &NodeUid) -> bool {
|
||||
let Some(root) = self.entries.get(&crate::ROOT_INO) else {
|
||||
return false;
|
||||
};
|
||||
if uid.volume_id != root.uid.volume_id {
|
||||
return false;
|
||||
/// Make `root` the root of a session and return its inode, which comes
|
||||
/// from its local id like any other node's.
|
||||
///
|
||||
/// The root is always owned. It must never be classified as a shared root,
|
||||
/// even when the API returns membership data (which happens when the owner
|
||||
/// shares the folder with someone else), so a stale share row a downgrade
|
||||
/// may have left for it is dropped. A node already in the tree keeps its
|
||||
/// inode and its lookups, and stops naming a parent: a session's `..` at
|
||||
/// its root is the root.
|
||||
pub(crate) fn install_root(&mut self, root: Node) -> u64 {
|
||||
if let Err(e) = self.db.upsert_node(&root) {
|
||||
warn!(uid = %root.uid, error = %e, "db upsert root failed");
|
||||
}
|
||||
self.share_access.remove(&root.uid);
|
||||
let _ = self.db.delete_share_access(&root.uid);
|
||||
let ino = match self.by_uid.get(&root.uid) {
|
||||
Some(&ino) => ino,
|
||||
None => self.new_inos(&[&root])[&root.uid],
|
||||
};
|
||||
if let Some(old) = self.entries.get(&ino).map(|e| e.parent)
|
||||
&& old != ino
|
||||
&& let Some(kids) = self.children.get_mut(&old)
|
||||
{
|
||||
kids.retain(|&k| k != ino);
|
||||
}
|
||||
self.by_uid.insert(root.uid.clone(), ino);
|
||||
let lookup_count = self.entries.get(&ino).map_or(1, |e| e.lookup_count.max(1));
|
||||
self.entries.insert(
|
||||
ino,
|
||||
Entry {
|
||||
uid: root.uid.clone(),
|
||||
parent: ino,
|
||||
node: root,
|
||||
access: Access::Owner,
|
||||
lookup_count,
|
||||
open_count: 0,
|
||||
unlinked: false,
|
||||
},
|
||||
);
|
||||
self.roots.insert(ino);
|
||||
ino
|
||||
}
|
||||
|
||||
/// Whether `uid` names an owned node currently reachable from one of
|
||||
/// `roots`, the roots of the sessions that are mounted. Resident entries
|
||||
/// can outlive their dentries for open-handle and access-revocation
|
||||
/// semantics, so `by_uid` membership alone is not proof that a
|
||||
/// control-socket operation may address the node.
|
||||
pub(crate) fn owns_visible_uid(&self, uid: &NodeUid, roots: &[u64]) -> bool {
|
||||
let Some(&target) = self.by_uid.get(uid) else {
|
||||
return false;
|
||||
};
|
||||
@@ -363,7 +467,9 @@ impl State {
|
||||
let Some(entry) = self.entries.get(¤t) else {
|
||||
return false;
|
||||
};
|
||||
if entry.uid.volume_id != root.uid.volume_id
|
||||
// The node's root scopes it to its own volume, excluding foreign
|
||||
// shared-with-me residents from uid-addressed sharing.
|
||||
if entry.uid.volume_id != uid.volume_id
|
||||
|| entry.unlinked
|
||||
|| entry.node.trashed
|
||||
|| entry.uid != entry.node.uid
|
||||
@@ -371,7 +477,7 @@ impl State {
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if current == crate::ROOT_INO {
|
||||
if roots.contains(¤t) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -413,15 +519,17 @@ impl State {
|
||||
self.access_changes.drain().collect()
|
||||
}
|
||||
|
||||
/// Whether `uid` lives on the volume this mount is rooted in.
|
||||
/// Whether `uid` lives on a volume some mount is rooted in.
|
||||
///
|
||||
/// Used to decide how a node with no resolvable parent is classified:
|
||||
/// our own volume fails open (it is owned content), a foreign volume
|
||||
/// fails closed (it can only be there because of a share).
|
||||
pub(crate) fn is_own_volume(&self, uid: &NodeUid) -> bool {
|
||||
self.entries
|
||||
.get(&crate::ROOT_INO)
|
||||
.is_some_and(|root| root.uid.volume_id == uid.volume_id)
|
||||
self.roots.iter().any(|ino| {
|
||||
self.entries
|
||||
.get(ino)
|
||||
.is_some_and(|root| root.uid.volume_id == uid.volume_id)
|
||||
})
|
||||
}
|
||||
|
||||
fn stored_share_access(&self, uid: &NodeUid) -> Option<Access> {
|
||||
@@ -770,11 +878,54 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn intern(&mut self, parent: u64, node: Node) -> u64 {
|
||||
pub(crate) fn intern(&mut self, parent: u64, mut node: Node) -> u64 {
|
||||
self.keep_revision_mtime(&mut node);
|
||||
self.outbox.push(DbWrite::Upsert(vec![node.clone()]));
|
||||
self.intern_mem(parent, node)
|
||||
}
|
||||
|
||||
/// Keep what is already known of a file whose content has not changed.
|
||||
///
|
||||
/// Drive stamps a link's `ModifyTime` on a rename or a move as well, while
|
||||
/// POSIX leaves a file's mtime alone there. The content cache is keyed by
|
||||
/// it, too: taking the new stamp made the cached bytes unreachable, so the
|
||||
/// next partial write could not fill its gaps (docs/BUGS.md B123). The
|
||||
/// active revision id says whether the content is still the same, and so
|
||||
/// whether the size and digest a listing left out are still the ones known.
|
||||
fn keep_revision_mtime(&self, node: &mut Node) {
|
||||
let Some(entry) = self
|
||||
.by_uid
|
||||
.get(&node.uid)
|
||||
.and_then(|ino| self.entries.get(ino))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
if let (
|
||||
NodeKind::File {
|
||||
active_revision_id: Some(now),
|
||||
claimed_size,
|
||||
content_sha1,
|
||||
..
|
||||
},
|
||||
NodeKind::File {
|
||||
active_revision_id: Some(was),
|
||||
claimed_size: size_was,
|
||||
content_sha1: sha1_was,
|
||||
..
|
||||
},
|
||||
) = (&mut node.kind, &entry.node.kind)
|
||||
&& now == was
|
||||
{
|
||||
node.modification_time = entry.node.modification_time;
|
||||
if claimed_size.is_none() {
|
||||
*claimed_size = *size_was;
|
||||
}
|
||||
if content_sha1.is_none() {
|
||||
content_sha1.clone_from(sha1_was);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocate (or reuse) a stable inode for a node, updating the hot-cache maps
|
||||
/// only. Every caller owes the DB a write-through — see the callers below;
|
||||
/// the split exists so a batch can pay for one transaction instead of `n`.
|
||||
@@ -784,11 +935,24 @@ impl State {
|
||||
}
|
||||
|
||||
fn intern_mem_with_access(&mut self, parent: u64, node: Node, access: Access) -> u64 {
|
||||
self.intern_mem_at(parent, node, access, None)
|
||||
}
|
||||
|
||||
/// [`State::intern_mem_with_access`], with the inode a new entry takes when
|
||||
/// the caller already has it ([`State::new_inos`]).
|
||||
fn intern_mem_at(
|
||||
&mut self,
|
||||
parent: u64,
|
||||
mut node: Node,
|
||||
access: Access,
|
||||
ino: Option<u64>,
|
||||
) -> u64 {
|
||||
if let Some(&ino) = self.by_uid.get(&node.uid) {
|
||||
let changed = self
|
||||
.entries
|
||||
.get(&ino)
|
||||
.is_some_and(|entry| entry.access != access);
|
||||
self.keep_open_write_size(ino, &mut node);
|
||||
if let Some(e) = self.entries.get_mut(&ino) {
|
||||
e.node = node;
|
||||
e.parent = parent;
|
||||
@@ -801,8 +965,13 @@ impl State {
|
||||
}
|
||||
return ino;
|
||||
}
|
||||
let ino = self.next_ino;
|
||||
self.next_ino += 1;
|
||||
let ino = match ino {
|
||||
Some(ino) => ino,
|
||||
None => self
|
||||
.new_inos(&[&node])
|
||||
.remove(&node.uid)
|
||||
.unwrap_or_default(),
|
||||
};
|
||||
self.by_uid.insert(node.uid.clone(), ino);
|
||||
self.entries.insert(
|
||||
ino,
|
||||
@@ -819,6 +988,41 @@ impl State {
|
||||
ino
|
||||
}
|
||||
|
||||
/// The inodes for nodes that have no entry yet, from their local ids, in
|
||||
/// one transaction. Should the database fail, they come from
|
||||
/// [`State::next_ino`] instead, so the mount keeps working, with inodes
|
||||
/// that last only this run. So do the inodes of virtual folders, which are
|
||||
/// never stored.
|
||||
fn new_inos(&mut self, nodes: &[&Node]) -> HashMap<NodeUid, u64> {
|
||||
let (virtual_nodes, stored): (Vec<&Node>, Vec<&Node>) = nodes
|
||||
.iter()
|
||||
.partition(|node| crate::r#virtual::is_virtual_uid(&node.uid));
|
||||
let mut inos = match self.db.lids_for(&stored) {
|
||||
Ok(lids) => stored
|
||||
.iter()
|
||||
.zip(lids)
|
||||
.map(|(node, lid)| (node.uid.clone(), lid_ino(lid)))
|
||||
.collect(),
|
||||
Err(error) => {
|
||||
warn!(%error, "no local ids for new inodes; numbering them for this run");
|
||||
self.run_inos(&stored)
|
||||
}
|
||||
};
|
||||
inos.extend(self.run_inos(&virtual_nodes));
|
||||
inos
|
||||
}
|
||||
|
||||
fn run_inos(&mut self, nodes: &[&Node]) -> HashMap<NodeUid, u64> {
|
||||
nodes
|
||||
.iter()
|
||||
.map(|node| {
|
||||
let ino = self.next_ino;
|
||||
self.next_ino += 1;
|
||||
(node.uid.clone(), ino)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Decrement lookup count for an inode and prune if lookup_count == 0 && open_count == 0 && unlinked.
|
||||
pub(crate) fn forget_lookup(&mut self, ino: u64, nlookup: u64) -> Option<(u64, String)> {
|
||||
if let Some(entry) = self.entries.get_mut(&ino) {
|
||||
@@ -833,6 +1037,23 @@ impl State {
|
||||
|
||||
/// Allocate (or reuse) a stable inode for a node that came *from* the
|
||||
/// database, which is why nothing is written back.
|
||||
/// Carry the size and mtime of a file open for writing over onto `node`,
|
||||
/// which is about to replace its entry.
|
||||
///
|
||||
/// The open handle is ahead of anything Drive can report until release
|
||||
/// queues it. Taking the remote's size shrank a SQLite database to its empty
|
||||
/// create while SQLite held it open, and the kernel then capped every read
|
||||
/// at that size (docs/BUGS.md B109).
|
||||
pub(crate) fn keep_open_write_size(&self, ino: u64, node: &mut Node) {
|
||||
let (Some(aw), Some(entry)) = (self.active_writes.get(&ino), self.entries.get(&ino)) else {
|
||||
return;
|
||||
};
|
||||
if let NodeKind::File { claimed_size, .. } = &mut node.kind {
|
||||
*claimed_size = Some(aw.len as i64);
|
||||
node.modification_time = entry.node.modification_time;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn intern_from_db(&mut self, parent: u64, node: Node) -> u64 {
|
||||
self.intern_mem(parent, node)
|
||||
}
|
||||
@@ -842,11 +1063,27 @@ impl State {
|
||||
///
|
||||
/// This is what keeps `ls` on a large folder quick: one commit for the
|
||||
/// listing, rather than one autocommit — and one fsync — per child.
|
||||
pub(crate) fn intern_batch(&mut self, parent: u64, nodes: Vec<Node>) -> Vec<u64> {
|
||||
pub(crate) fn intern_batch(&mut self, parent: u64, mut nodes: Vec<Node>) -> Vec<u64> {
|
||||
for node in &mut nodes {
|
||||
self.keep_revision_mtime(node);
|
||||
}
|
||||
self.outbox.push(DbWrite::Upsert(nodes.clone()));
|
||||
let fresh: Vec<&Node> = nodes
|
||||
.iter()
|
||||
.filter(|node| !self.by_uid.contains_key(&node.uid))
|
||||
.collect();
|
||||
let mut inos = if fresh.is_empty() {
|
||||
HashMap::new()
|
||||
} else {
|
||||
self.new_inos(&fresh)
|
||||
};
|
||||
nodes
|
||||
.into_iter()
|
||||
.map(|node| self.intern_mem(parent, node))
|
||||
.map(|node| {
|
||||
let access = self.access_for_node(parent, &node);
|
||||
let ino = inos.remove(&node.uid);
|
||||
self.intern_mem_at(parent, node, access, ino)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -1164,7 +1401,7 @@ mod tests {
|
||||
let dir = TempDir::new();
|
||||
let db = Db::open(&dir.0.join("cache.db")).unwrap();
|
||||
let share_access = db.all_share_access().unwrap();
|
||||
let st = State::new(Arc::new(db), share_access, 1);
|
||||
let st = State::new(Arc::new(db), share_access);
|
||||
(st, dir)
|
||||
}
|
||||
|
||||
@@ -1182,6 +1419,36 @@ mod tests {
|
||||
(st, dir, src, dst)
|
||||
}
|
||||
|
||||
/// A node's inode comes from its row's local id, so it is the same in
|
||||
/// every mount, after the hot cache drops it, and after a restart.
|
||||
#[test]
|
||||
fn a_node_keeps_its_inode_across_mounts_evictions_and_restarts() {
|
||||
let (mut st, _dir) = state();
|
||||
let inos = st.intern_batch(
|
||||
0,
|
||||
vec![
|
||||
node("a", "root", "a.txt", false),
|
||||
node("b", "root", "b.txt", false),
|
||||
],
|
||||
);
|
||||
st.flush_outbox();
|
||||
let db = st.db.clone();
|
||||
assert_ne!(inos[0], inos[1]);
|
||||
assert!(!inos.contains(&crate::ROOT_INO));
|
||||
|
||||
let mut other = State::new(db.clone(), HashMap::new());
|
||||
assert_eq!(other.intern(0, node("b", "root", "b.txt", false)), inos[1]);
|
||||
|
||||
st.forget_mem(&uid("a"));
|
||||
assert_eq!(st.intern(0, node("a", "root", "a.txt", false)), inos[0]);
|
||||
|
||||
let mut restarted = State::new(db, HashMap::new());
|
||||
assert_eq!(
|
||||
restarted.intern_batch(0, vec![node("a", "root", "a.txt", false)]),
|
||||
[inos[0]]
|
||||
);
|
||||
}
|
||||
|
||||
/// Audit A5. Moving a file out of a folder leaves that folder's DB row
|
||||
/// claiming a complete listing, so once the hot cache drops it, remote
|
||||
/// changes under it stop being seen. `relocate` has to clear both sides.
|
||||
@@ -1247,7 +1514,7 @@ mod tests {
|
||||
let root = uid("root").to_string();
|
||||
let state = parking_lot::Mutex::new(st);
|
||||
{
|
||||
let mut guard = lock_state(&state, &db);
|
||||
let mut guard = StateGuard::new(state.lock(), &db);
|
||||
guard.intern(0, node("root", "none", "My Files", true));
|
||||
assert!(
|
||||
db.node_by_uid(&root).unwrap().is_none(),
|
||||
@@ -1490,6 +1757,7 @@ mod tests {
|
||||
})
|
||||
.collect();
|
||||
st.next_ino = crate::ROOT_INO + nodes.len() as u64;
|
||||
st.roots.insert(crate::ROOT_INO);
|
||||
// Reverse order so entries are inserted before their parents, matching
|
||||
// the hydration path's non-topological materialization.
|
||||
for (i, node) in nodes.iter().enumerate().rev() {
|
||||
@@ -1863,4 +2131,137 @@ mod tests {
|
||||
let shared = st.intern(owned_root, shared);
|
||||
assert_eq!(st.entries[&shared].access, Access::Viewer);
|
||||
}
|
||||
|
||||
fn at_revision(mut node: Node, revision: &str, mtime: i64) -> Node {
|
||||
node.modification_time = mtime;
|
||||
if let NodeKind::File {
|
||||
active_revision_id, ..
|
||||
} = &mut node.kind
|
||||
{
|
||||
*active_revision_id = Some(revision.into());
|
||||
}
|
||||
node
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_renamed_file_keeps_its_mtime_until_its_content_changes() {
|
||||
let (mut st, _dir) = state();
|
||||
let root = st.intern(0, node("root", "none", "My Files", true));
|
||||
let mut file = node("file", "root", "a.txt", false);
|
||||
if let NodeKind::File {
|
||||
claimed_size,
|
||||
total_size_on_storage,
|
||||
..
|
||||
} = &mut file.kind
|
||||
{
|
||||
*claimed_size = Some(7);
|
||||
*total_size_on_storage = 519;
|
||||
}
|
||||
let ino = st.intern(root, at_revision(file.clone(), "r1", 100));
|
||||
// Drive stamps the link on a rename; the content is still r1.
|
||||
let mut renamed = at_revision(file.clone(), "r1", 160);
|
||||
renamed.name = "b.txt".into();
|
||||
st.intern_batch(root, vec![renamed]);
|
||||
assert_eq!(st.entries[&ino].node.modification_time, 100);
|
||||
assert_eq!(st.entries[&ino].node.name, "b.txt");
|
||||
// A listing that leaves the size out still describes revision r1.
|
||||
let mut bare = at_revision(file.clone(), "r1", 170);
|
||||
assert_eq!(crate::node_size(&st.entries[&ino].node), 7);
|
||||
if let NodeKind::File { claimed_size, .. } = &mut bare.kind {
|
||||
*claimed_size = None;
|
||||
}
|
||||
st.intern(root, bare);
|
||||
assert_eq!(st.entries[&ino].node.modification_time, 100);
|
||||
assert_eq!(crate::node_size(&st.entries[&ino].node), 7);
|
||||
st.intern(root, at_revision(file, "r2", 200));
|
||||
assert_eq!(st.entries[&ino].node.modification_time, 200);
|
||||
}
|
||||
|
||||
fn handle(len: u64, base_size: u64) -> WriteHandle {
|
||||
let path = std::env::temp_dir().join(format!("pdfs-handle-test-{}", std::process::id()));
|
||||
let file = File::create(&path).unwrap();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
WriteHandle {
|
||||
ino: 2,
|
||||
uid: uid("file"),
|
||||
file: Arc::new(file),
|
||||
path,
|
||||
written: Intervals::default(),
|
||||
len,
|
||||
base_size,
|
||||
base_mtime: 0,
|
||||
base_revision_id: None,
|
||||
dirty: false,
|
||||
open_count: 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// A file created offline is written while its queued create lands. The
|
||||
/// placeholder had no revision, so the handle takes the empty file the
|
||||
/// create made as its base (docs/BUGS.md B113).
|
||||
#[test]
|
||||
fn a_handle_on_a_placeholder_is_based_on_the_create_that_landed() {
|
||||
let mut h = handle(0, 0);
|
||||
assert_eq!(h.base_revision_id, None);
|
||||
let mut landed = node("real", "root", "f.mkv", false);
|
||||
landed.modification_time = 160;
|
||||
if let NodeKind::File {
|
||||
active_revision_id, ..
|
||||
} = &mut landed.kind
|
||||
{
|
||||
*active_revision_id = Some("empty".into());
|
||||
}
|
||||
h.rebase_onto(&landed);
|
||||
assert_eq!(h.base_mtime, 160);
|
||||
assert_eq!(h.base_size, 0);
|
||||
assert_eq!(h.base_revision_id.as_deref(), Some("empty"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_listing_does_not_shrink_a_file_open_for_writing() {
|
||||
let (mut st, _dir) = state();
|
||||
let ino = st.intern(0, node("file", "root", "workload.db", false));
|
||||
let mut h = handle(16384, 0);
|
||||
h.ino = ino;
|
||||
st.active_writes.insert(ino, h);
|
||||
st.set_size(ino, 16384);
|
||||
st.touch_mtime(ino, 500);
|
||||
|
||||
// The remote still holds the empty file the create sealed.
|
||||
st.intern(0, node("file", "root", "workload.db", false));
|
||||
assert_eq!(crate::node_size(&st.entries[&ino].node), 16384);
|
||||
assert_eq!(st.entries[&ino].node.modification_time, 500);
|
||||
|
||||
// Once the handle is gone the remote is authoritative again.
|
||||
st.active_writes.remove(&ino);
|
||||
st.intern(0, node("file", "root", "workload.db", false));
|
||||
assert_eq!(crate::node_size(&st.entries[&ino].node), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_write_past_the_end_authors_the_hole_it_leaves() {
|
||||
// Opened over a 100-byte base, shrunk to 10, then written at 50: bytes
|
||||
// 10..50 are zeros now, not the base's bytes 10..50.
|
||||
let mut h = handle(100, 100);
|
||||
h.written.clip(10);
|
||||
h.len = 10;
|
||||
h.record_write(50, 60);
|
||||
assert_eq!(h.len, 60);
|
||||
assert_eq!(
|
||||
h.written.segments(0, 60),
|
||||
vec![(0, 10, false), (10, 60, true)],
|
||||
"only the head still comes from the base"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_write_inside_the_file_leaves_the_rest_to_the_base() {
|
||||
let mut h = handle(100, 100);
|
||||
h.record_write(40, 50);
|
||||
assert_eq!(h.len, 100);
|
||||
assert_eq!(
|
||||
h.written.segments(0, 100),
|
||||
vec![(0, 40, false), (40, 50, true), (50, 100, false)]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -165,7 +165,7 @@ impl Core {
|
||||
}
|
||||
match self
|
||||
.rt
|
||||
.block_on(self.client.trash_nodes(std::slice::from_ref(uid)))
|
||||
.block_on(self.drive.trash_nodes(std::slice::from_ref(uid)))
|
||||
.and_then(batch::into_unit)
|
||||
{
|
||||
Ok(()) => {}
|
||||
@@ -175,10 +175,9 @@ impl Core {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Every mount, not just the primary: a conflict copy inside a sync
|
||||
// folder lives in that fork's inode space, and leaving it hooked up
|
||||
// there keeps a node we have just trashed remotely visible and
|
||||
// readable there (`docs/BUGS.md` B74).
|
||||
// Leaving a conflict copy hooked up in the tree keeps a node we have
|
||||
// just trashed remotely visible and readable in whichever mount shows
|
||||
// it (`docs/BUGS.md` B74).
|
||||
self.for_each_state(|st| {
|
||||
st.forget_or_unlink(uid);
|
||||
});
|
||||
|
||||
@@ -73,6 +73,9 @@ enum Applied {
|
||||
Downloaded,
|
||||
/// A conflict copy was kept.
|
||||
Conflict,
|
||||
/// The local file to upload was deleted after the scan planned it. There is
|
||||
/// nothing left to send, and the next pass sees the deletion (B108).
|
||||
Vanished,
|
||||
}
|
||||
|
||||
impl Core {
|
||||
@@ -367,7 +370,7 @@ impl Core {
|
||||
HashMap::new()
|
||||
} else {
|
||||
self.rt
|
||||
.block_on(self.client.enumerate_nodes_light(&check))
|
||||
.block_on(self.drive.enumerate_nodes_light(&check))
|
||||
.map_err(|e| format!("resolve changed nodes: {e}"))?
|
||||
.into_iter()
|
||||
.filter(|n| !n.trashed)
|
||||
@@ -522,7 +525,7 @@ impl Core {
|
||||
};
|
||||
if let Err(e) = self
|
||||
.rt
|
||||
.block_on(self.client.trash_nodes(&[uid]))
|
||||
.block_on(self.drive.trash_nodes(&[uid]))
|
||||
.and_then(batch::into_unit)
|
||||
{
|
||||
warn!(rel, error = %e, "sync: trash remote failed");
|
||||
@@ -773,7 +776,7 @@ impl Core {
|
||||
let remote = r.expect("remote deletion requires a remote file");
|
||||
if let Err(e) = self
|
||||
.rt
|
||||
.block_on(self.client.trash_nodes(std::slice::from_ref(&remote.uid)))
|
||||
.block_on(self.drive.trash_nodes(std::slice::from_ref(&remote.uid)))
|
||||
.and_then(batch::into_unit)
|
||||
{
|
||||
warn!(rel, error = %e, "sync: trash remote failed");
|
||||
@@ -836,7 +839,7 @@ impl Core {
|
||||
for (rel, uid) in delete_remote_dirs {
|
||||
if let Err(e) = self
|
||||
.rt
|
||||
.block_on(self.client.trash_nodes(&[uid]))
|
||||
.block_on(self.drive.trash_nodes(&[uid]))
|
||||
.and_then(batch::into_unit)
|
||||
{
|
||||
warn!(rel, error = %e, "sync: trash remote folder failed");
|
||||
@@ -891,14 +894,14 @@ impl Core {
|
||||
) -> Result<(), String> {
|
||||
let uids = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_folder_children_node_uids(folder))
|
||||
.block_on(self.drive.enumerate_folder_children_node_uids(folder))
|
||||
.map_err(|e| format!("enumerate {folder}: {e}"))?;
|
||||
if uids.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let nodes = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_nodes_light(&uids))
|
||||
.block_on(self.drive.enumerate_nodes_light(&uids))
|
||||
.map_err(|e| format!("resolve nodes: {e}"))?;
|
||||
for node in nodes {
|
||||
if node.trashed || !node.is_folder() {
|
||||
@@ -943,14 +946,14 @@ impl Core {
|
||||
}
|
||||
let uids = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_folder_children_node_uids(folder))
|
||||
.block_on(self.drive.enumerate_folder_children_node_uids(folder))
|
||||
.map_err(|e| format!("enumerate {folder}: {e}"))?;
|
||||
if uids.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let nodes = self
|
||||
.rt
|
||||
.block_on(self.client.enumerate_nodes_light(&uids))
|
||||
.block_on(self.drive.enumerate_nodes_light(&uids))
|
||||
.map_err(|e| format!("resolve nodes: {e}"))?;
|
||||
|
||||
// Files whose recorded mtime no longer matches: their size has to be
|
||||
@@ -968,7 +971,7 @@ impl Core {
|
||||
HashMap::new()
|
||||
} else {
|
||||
self.rt
|
||||
.block_on(self.client.enumerate_nodes(&stale))
|
||||
.block_on(self.drive.enumerate_nodes(&stale))
|
||||
.map_err(|e| format!("resolve nodes: {e}"))?
|
||||
.iter()
|
||||
.map(|n| (n.uid.to_string(), remote_file_sig(n).1))
|
||||
@@ -1136,7 +1139,7 @@ impl Core {
|
||||
match op {
|
||||
Pending::CreateDir { rel, parent } => {
|
||||
let uid = self
|
||||
.client
|
||||
.drive
|
||||
.create_folder(parent, base_name(rel), Some(now_secs()))
|
||||
.await
|
||||
.map_err(|e| format!("create remote folder {rel}: {e}"))?;
|
||||
@@ -1147,13 +1150,10 @@ impl Core {
|
||||
Ok(Applied::Dir(rel.clone(), uid))
|
||||
}
|
||||
Pending::UploadNew { rel, parent } => {
|
||||
self.upload_new(folder_id, local_root, rel, parent).await?;
|
||||
Ok(Applied::Uploaded)
|
||||
self.upload_new(folder_id, local_root, rel, parent).await
|
||||
}
|
||||
Pending::UploadRevision { rel, uid } => {
|
||||
self.upload_revision(folder_id, local_root, rel, uid)
|
||||
.await?;
|
||||
Ok(Applied::Uploaded)
|
||||
self.upload_revision(folder_id, local_root, rel, uid).await
|
||||
}
|
||||
Pending::Download {
|
||||
rel,
|
||||
@@ -1217,10 +1217,12 @@ impl Core {
|
||||
local_root: &Path,
|
||||
rel: &str,
|
||||
parent: &NodeUid,
|
||||
) -> Result<(), String> {
|
||||
) -> Result<Applied, String> {
|
||||
let path = local_root.join(rel_to_path(rel));
|
||||
let name = base_name(rel);
|
||||
let file = std::fs::File::open(&path).map_err(|e| format!("open {rel}: {e}"))?;
|
||||
let Some(file) = open_to_upload(&path, rel)? else {
|
||||
return Ok(Applied::Vanished);
|
||||
};
|
||||
let meta = file.metadata().map_err(|e| format!("stat {rel}: {e}"))?;
|
||||
let mtime = system_mtime(&meta);
|
||||
// Taken from the descriptor being streamed, so it names the content that
|
||||
@@ -1238,7 +1240,7 @@ impl Core {
|
||||
.begin(name, "", TransferDirection::Upload, meta.len()),
|
||||
);
|
||||
let uid = self
|
||||
.client
|
||||
.drive
|
||||
// Mirror push: the local file is authoritative. If a prior attempt was
|
||||
// interrupted mid-upload it left an unsealed draft of this name; recover
|
||||
// it even across a daemon restart (which rotates our client uid), so the
|
||||
@@ -1261,7 +1263,8 @@ impl Core {
|
||||
self.invalidate_children_of(parent);
|
||||
warn_if_torn(rel, &path, streamed);
|
||||
self.record_file_baseline(folder_id, rel, Some(streamed), &uid)
|
||||
.await
|
||||
.await?;
|
||||
Ok(Applied::Uploaded)
|
||||
}
|
||||
|
||||
/// Upload a changed local file as a new revision of an existing remote node.
|
||||
@@ -1271,9 +1274,11 @@ impl Core {
|
||||
local_root: &Path,
|
||||
rel: &str,
|
||||
uid: &NodeUid,
|
||||
) -> Result<(), String> {
|
||||
) -> Result<Applied, String> {
|
||||
let path = local_root.join(rel_to_path(rel));
|
||||
let file = std::fs::File::open(&path).map_err(|e| format!("open {rel}: {e}"))?;
|
||||
let Some(file) = open_to_upload(&path, rel)? else {
|
||||
return Ok(Applied::Vanished);
|
||||
};
|
||||
let meta = file.metadata().map_err(|e| format!("stat {rel}: {e}"))?;
|
||||
let mtime = system_mtime(&meta);
|
||||
let streamed = LocalSig::from(&meta);
|
||||
@@ -1289,13 +1294,14 @@ impl Core {
|
||||
meta.len(),
|
||||
),
|
||||
);
|
||||
self.client
|
||||
self.drive
|
||||
.upload_new_revision_from(uid, reader, meta.len() as i64, thumbnails, Some(mtime))
|
||||
.await
|
||||
.map_err(|e| format!("upload revision {rel}: {e}"))?;
|
||||
warn_if_torn(rel, &path, streamed);
|
||||
self.record_file_baseline(folder_id, rel, Some(streamed), uid)
|
||||
.await
|
||||
.await?;
|
||||
Ok(Applied::Uploaded)
|
||||
}
|
||||
|
||||
/// Download a remote file to its local path (atomically via a temp file),
|
||||
@@ -1328,7 +1334,21 @@ impl Core {
|
||||
.map(|d| d.as_nanos())
|
||||
.unwrap_or(0)
|
||||
));
|
||||
{
|
||||
// A file an on-demand mount already read, say one just moved here from
|
||||
// it, is in the content cache under the same revision key.
|
||||
let seeded = size > 0 && self.cache.copy_cached_to(uid, mtime, size as u64, &tmp);
|
||||
if seeded {
|
||||
std::fs::File::open(&tmp)
|
||||
.and_then(|f| f.sync_all())
|
||||
.map_err(|e| {
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
format!("sync tmp {rel}: {e}")
|
||||
})?;
|
||||
debug!(
|
||||
rel,
|
||||
"sync: placed from the content cache instead of downloading"
|
||||
);
|
||||
} else {
|
||||
let out = std::fs::File::create(&tmp).map_err(|e| format!("create tmp {rel}: {e}"))?;
|
||||
// Count the bytes as they land, so the download reports progress like
|
||||
// an on-demand hydration does.
|
||||
@@ -1339,7 +1359,7 @@ impl Core {
|
||||
size.max(0) as u64,
|
||||
);
|
||||
let mut out = CountingWriter::new(out, &guard);
|
||||
self.client
|
||||
self.drive
|
||||
.download_file_to(uid, &mut out)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
@@ -1404,7 +1424,7 @@ impl Core {
|
||||
let (lmtime, lsize) = (local.mtime, local.size);
|
||||
// Re-fetch the node so the stored remote signature is exactly what a walk
|
||||
// will report next time.
|
||||
let (rmtime, rsize) = match self.client.enumerate_nodes(std::slice::from_ref(uid)).await {
|
||||
let (rmtime, rsize) = match self.drive.enumerate_nodes(std::slice::from_ref(uid)).await {
|
||||
Ok(nodes) => nodes
|
||||
.first()
|
||||
.map(remote_file_sig)
|
||||
@@ -1431,6 +1451,23 @@ impl Core {
|
||||
}
|
||||
}
|
||||
|
||||
/// Open a local file for upload, or `None` when it is gone.
|
||||
///
|
||||
/// A scan plans uploads from a listing, and the file can be deleted before its
|
||||
/// upload starts — a build or a test tree cleaned up mid-pass. That is not a
|
||||
/// failure: the next pass sees the deletion. Counting it as one put the folder
|
||||
/// in `error` for a file nobody wanted any more (B108).
|
||||
fn open_to_upload(path: &Path, rel: &str) -> Result<Option<std::fs::File>, String> {
|
||||
match std::fs::File::open(path) {
|
||||
Ok(file) => Ok(Some(file)),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
|
||||
debug!(rel, "sync: file deleted before its upload; skipping");
|
||||
Ok(None)
|
||||
}
|
||||
Err(e) => Err(format!("open {rel}: {e}")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Say so when a file changed under an upload that was streaming it.
|
||||
///
|
||||
/// Purely diagnostic — the correction is already in place, because the baseline
|
||||
@@ -1602,6 +1639,76 @@ fn walk_local_tree(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Why the mirror copy at `rel` holds something Proton Drive does not, or
|
||||
/// `None` when everything under it is on Drive exactly as it is on disk.
|
||||
///
|
||||
/// A move out of a mirror folder removes the local copy once Drive has moved
|
||||
/// the node, so anything only this disk has would be lost with it: a file not
|
||||
/// uploaded yet, one edited since, a deletion not carried up yet, or anything
|
||||
/// the engine never uploads. That is why ignored names count here, and why the
|
||||
/// walk is its own rather than [`walk_local_tree`], which skips symlinks.
|
||||
pub(crate) fn mirror_subtree_unsynced(
|
||||
root: &Path,
|
||||
rel: &str,
|
||||
baseline: &HashMap<String, StoredSyncEntry>,
|
||||
) -> Option<String> {
|
||||
let mut seen = HashSet::new();
|
||||
if let Some(reason) = unsynced_local(root, rel, baseline, &mut seen) {
|
||||
return Some(reason);
|
||||
}
|
||||
let prefix = format!("{rel}/");
|
||||
baseline
|
||||
.keys()
|
||||
.filter(|r| *r == rel || r.starts_with(&prefix))
|
||||
.find(|r| !seen.contains(r.as_str()))
|
||||
.map(|r| format!("{r} was deleted here but not on Proton Drive yet"))
|
||||
}
|
||||
|
||||
fn unsynced_local(
|
||||
root: &Path,
|
||||
rel: &str,
|
||||
baseline: &HashMap<String, StoredSyncEntry>,
|
||||
seen: &mut HashSet<String>,
|
||||
) -> Option<String> {
|
||||
let path = root.join(rel_to_path(rel));
|
||||
let meta = match std::fs::symlink_metadata(&path) {
|
||||
Ok(meta) => meta,
|
||||
Err(e) => return Some(format!("cannot read {rel}: {e}")),
|
||||
};
|
||||
let Some(base) = baseline.get(rel) else {
|
||||
return Some(format!("{rel} is not on Proton Drive yet"));
|
||||
};
|
||||
seen.insert(rel.to_string());
|
||||
if meta.is_file() {
|
||||
return (!LocalSig::from(&meta).same_content(&LocalSig::from(base)))
|
||||
.then(|| format!("{rel} has changes that are not on Proton Drive yet"));
|
||||
}
|
||||
if !meta.is_dir() {
|
||||
return Some(format!("{rel} is not a regular file or folder"));
|
||||
}
|
||||
let entries = match std::fs::read_dir(&path) {
|
||||
Ok(entries) => entries,
|
||||
Err(e) => return Some(format!("cannot read {rel}: {e}")),
|
||||
};
|
||||
for entry in entries {
|
||||
let name = match entry {
|
||||
Ok(entry) => entry.file_name(),
|
||||
Err(e) => return Some(format!("cannot read {rel}: {e}")),
|
||||
};
|
||||
let Some(name) = name.to_str() else {
|
||||
return Some(format!("{rel} holds a name that is not valid UTF-8"));
|
||||
};
|
||||
// A download the engine left half done; it is the engine's to clean up.
|
||||
if name.contains(".pdfs-tmp-") {
|
||||
continue;
|
||||
}
|
||||
if let Some(reason) = unsynced_local(root, &format!("{rel}/{name}"), baseline, seen) {
|
||||
return Some(reason);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Publish a conflict copy without overwriting an earlier conflict bearing the
|
||||
/// same timestamp. The original is removed only after the new copy is durable.
|
||||
fn preserve_conflict_copy(path: &Path, stamp: i64) -> std::io::Result<PathBuf> {
|
||||
@@ -2155,6 +2262,27 @@ mod tests {
|
||||
assert_eq!(remote_sig(&e), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_deleted_before_its_upload_is_skipped_not_failed() {
|
||||
let root = std::env::temp_dir().join(format!("pdfs-b108-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
let gone = root.join("gone.txt");
|
||||
assert!(open_to_upload(&gone, "gone.txt").unwrap().is_none());
|
||||
|
||||
std::fs::write(&gone, b"x").unwrap();
|
||||
assert!(open_to_upload(&gone, "gone.txt").unwrap().is_some());
|
||||
|
||||
// Other open failures still count as errors.
|
||||
let err = open_to_upload(&gone.join("child"), "gone.txt/child");
|
||||
assert!(err.unwrap_err().starts_with("open gone.txt/child:"));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
|
||||
let mut o = Outcome::default();
|
||||
o.record(&Applied::Vanished);
|
||||
assert!(o.is_empty());
|
||||
assert_eq!(o.errors, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outcome_summarises_only_what_it_moved() {
|
||||
let mut o = Outcome::default();
|
||||
@@ -2346,6 +2474,94 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// A baseline row recording `path` as it is on disk right now.
|
||||
fn synced_row(root: &Path, rel: &str) -> (String, StoredSyncEntry) {
|
||||
let meta = std::fs::symlink_metadata(root.join(rel_to_path(rel))).unwrap();
|
||||
let sig = LocalSig::from(&meta);
|
||||
let entry = StoredSyncEntry {
|
||||
rel_path: rel.to_string(),
|
||||
remote_uid: Some(format!("vol~{rel}")),
|
||||
local_mtime: sig.mtime,
|
||||
local_mtime_ns: sig.mtime_ns,
|
||||
local_size: sig.size,
|
||||
remote_rev: None,
|
||||
remote_hash: None,
|
||||
};
|
||||
(rel.to_string(), entry)
|
||||
}
|
||||
|
||||
fn synced_subtree(name: &str) -> (PathBuf, HashMap<String, StoredSyncEntry>) {
|
||||
let root = sync_test_dir(name);
|
||||
std::fs::create_dir_all(root.join("dir/sub")).unwrap();
|
||||
std::fs::write(root.join("dir/a.txt"), b"a").unwrap();
|
||||
std::fs::write(root.join("dir/sub/b.txt"), b"b").unwrap();
|
||||
let baseline = ["dir", "dir/sub", "dir/a.txt", "dir/sub/b.txt"]
|
||||
.into_iter()
|
||||
.map(|rel| synced_row(&root, rel))
|
||||
.collect();
|
||||
(root, baseline)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fully_synced_mirror_subtree_may_leave() {
|
||||
let (root, baseline) = synced_subtree("move-synced");
|
||||
let reason = mirror_subtree_unsynced(&root, "dir", &baseline);
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
assert_eq!(reason, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mirror_subtree_with_a_file_not_uploaded_may_not_leave() {
|
||||
let (root, baseline) = synced_subtree("move-new-file");
|
||||
std::fs::write(root.join("dir/sub/new.txt"), b"n").unwrap();
|
||||
let reason = mirror_subtree_unsynced(&root, "dir", &baseline);
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
assert!(reason.unwrap().contains("dir/sub/new.txt"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mirror_subtree_with_an_edit_not_uploaded_may_not_leave() {
|
||||
let (root, baseline) = synced_subtree("move-edit");
|
||||
std::fs::write(root.join("dir/a.txt"), b"longer now").unwrap();
|
||||
let reason = mirror_subtree_unsynced(&root, "dir", &baseline);
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
assert!(reason.unwrap().contains("dir/a.txt"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mirror_subtree_with_a_deletion_not_carried_up_may_not_leave() {
|
||||
let (root, baseline) = synced_subtree("move-deleted");
|
||||
std::fs::remove_file(root.join("dir/sub/b.txt")).unwrap();
|
||||
let reason = mirror_subtree_unsynced(&root, "dir", &baseline);
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
assert!(reason.unwrap().contains("dir/sub/b.txt"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mirror_subtree_holding_a_symlink_may_not_leave() {
|
||||
let (root, baseline) = synced_subtree("move-symlink");
|
||||
std::os::unix::fs::symlink("a.txt", root.join("dir/link")).unwrap();
|
||||
let reason = mirror_subtree_unsynced(&root, "dir", &baseline);
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
assert!(reason.unwrap().contains("dir/link"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_name_prefixed_sibling_does_not_hold_a_mirror_subtree_back() {
|
||||
let (root, mut baseline) = synced_subtree("move-sibling");
|
||||
// Deleted here but not on Drive, yet outside the subtree being moved.
|
||||
baseline.insert(
|
||||
"dirt".to_string(),
|
||||
StoredSyncEntry {
|
||||
rel_path: "dirt".to_string(),
|
||||
..baseline["dir/a.txt"].clone()
|
||||
},
|
||||
);
|
||||
let reason = mirror_subtree_unsynced(&root, "dir", &baseline);
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
assert_eq!(reason, None);
|
||||
}
|
||||
|
||||
/// The same rule one level up: if the sync root itself cannot be read, the
|
||||
/// pass sees nothing, and "nothing" must never mean "delete everything".
|
||||
#[test]
|
||||
|
||||
@@ -108,6 +108,7 @@ impl Outcome {
|
||||
Applied::Uploaded => self.uploaded += 1,
|
||||
Applied::Downloaded => self.downloaded += 1,
|
||||
Applied::Conflict => self.conflicts += 1,
|
||||
Applied::Vanished => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ async fn run_uploads(
|
||||
.upload_thumbnails_async(t.path.clone(), t.name.clone())
|
||||
.await;
|
||||
match core
|
||||
.client
|
||||
.drive
|
||||
.upload_file_from(
|
||||
&t.parent_uid,
|
||||
&t.name,
|
||||
@@ -238,14 +238,11 @@ impl Core {
|
||||
.map_err(|error| self.errno_error(error, "upload destination"))?;
|
||||
let new_uid = self
|
||||
.rt
|
||||
.block_on(
|
||||
self.client
|
||||
.create_folder(parent_uid, name, Some(now_secs())),
|
||||
)
|
||||
.block_on(self.drive.create_folder(parent_uid, name, Some(now_secs())))
|
||||
.map_err(|e| CoreError::from_api(&e, &format!("create folder {name}")))?;
|
||||
let node = self
|
||||
.fetch_node(&new_uid)
|
||||
.map_err(|e| self.errno_error(e, "fetch node"))?;
|
||||
.fetch_minted_node(&new_uid, name)
|
||||
.ok_or_else(|| minted_but_unlisted(name))?;
|
||||
let mut st = self.state();
|
||||
let ino = st.intern(pino, node);
|
||||
if let Some(kids) = st.children.get_mut(&pino)
|
||||
|
||||
@@ -27,5 +27,6 @@ fn main() {
|
||||
println!("cargo:rerun-if-changed=resources/icons");
|
||||
println!("cargo:rerun-if-changed=resources/style.css");
|
||||
println!("cargo:rerun-if-changed=resources/proton-theme.css");
|
||||
println!("cargo:rerun-if-changed=resources/prompt.css");
|
||||
println!("cargo:rerun-if-changed=../../packaging/io.narl.proton-drive-linux.svg");
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- A copy is on this computer: a check in an outlined circle. Adwaita 50
|
||||
no longer ships emblem-ok-symbolic. -->
|
||||
<path fill="#222222" fill-rule="evenodd" d="M 8,1 A 7,7 0 1 0 8,15 A 7,7 0 1 0 8,1 Z M 8,2.5 A 5.5,5.5 0 1 1 8,13.5 A 5.5,5.5 0 1 1 8,2.5 Z"/>
|
||||
<path fill="#222222" d="M 4.1,8.6 L 5.2,7.5 L 7,9.3 L 10.8,5.4 L 11.9,6.5 L 7,11.5 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 450 B |
@@ -0,0 +1,6 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Show or hide the details pane: a window with its right-hand pane
|
||||
filled in. Some themes draw sidebar-show-right-symbolic as a solid block. -->
|
||||
<path fill="#222222" fill-rule="evenodd" d="M 3,2 H 13 A 2,2 0 0 1 15,4 V 12 A 2,2 0 0 1 13,14 H 3 A 2,2 0 0 1 1,12 V 4 A 2,2 0 0 1 3,2 Z M 3,3.5 A 0.5,0.5 0 0 0 2.5,4 V 12 A 0.5,0.5 0 0 0 3,12.5 H 13 A 0.5,0.5 0 0 0 13.5,12 V 4 A 0.5,0.5 0 0 0 13,3.5 Z"/>
|
||||
<path fill="#222222" d="M 9.5,5 H 12.5 V 11 H 9.5 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 564 B |
@@ -0,0 +1,8 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- A share link: two open chain ends and the bar that joins them, laid
|
||||
on the diagonal. Written out in plain coordinates: GTK's symbolic icon
|
||||
renderer drops a transformed group. -->
|
||||
<path fill="#222222" d="M 4.82,6.23 L 3.05,8 A 3.5,3.5 0 0 0 8,12.95 L 9.77,11.18 L 8.53,9.94 L 6.76,11.71 A 1.75,1.75 0 0 1 4.29,9.24 L 6.06,7.47 Z"/>
|
||||
<path fill="#222222" d="M 6.23,4.82 L 8,3.05 A 3.5,3.5 0 0 1 12.95,8 L 11.18,9.77 L 9.94,8.53 L 11.71,6.76 A 1.75,1.75 0 0 0 9.24,4.29 L 7.47,6.06 Z"/>
|
||||
<path fill="#222222" d="M 5.26,9.5 L 9.5,5.26 L 10.74,6.5 L 6.5,10.74 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 676 B |
@@ -0,0 +1,6 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Show on map: a folded map in three panels. No stock theme has a
|
||||
map-symbolic. -->
|
||||
<path fill="#222222" d="M 1,3.2 L 5,1.5 V 12.8 L 1,14.5 Z M 11,3.2 L 15,1.5 V 12.8 L 11,14.5 Z"/>
|
||||
<path fill="#222222" opacity="0.55" d="M 6,1.5 L 10,3.2 V 14.5 L 6,12.8 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 366 B |
@@ -0,0 +1,5 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Main menu: three bars. Bundled because themes such as Papirus draw
|
||||
open-menu-symbolic as three dots, the same as every row's "more" menu. -->
|
||||
<path fill="#222222" d="M 2,2.5 H 14 A 1,1 0 0 1 14,4.5 H 2 A 1,1 0 0 1 2,2.5 Z M 2,7 H 14 A 1,1 0 0 1 14,9 H 2 A 1,1 0 0 1 2,7 Z M 2,11.5 H 14 A 1,1 0 0 1 14,13.5 H 2 A 1,1 0 0 1 2,11.5 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 441 B |
@@ -0,0 +1,6 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Shared with me: two people. system-users-symbolic survives only in
|
||||
Adwaita's legacy folder. -->
|
||||
<path fill="#222222" d="M 6,2.5 A 2.5,2.5 0 1 0 6,7.5 A 2.5,2.5 0 1 0 6,2.5 Z M 1.5,14.5 C 1.5,11 3.5,9 6,9 C 8.5,9 10.5,11 10.5,14.5 Z"/>
|
||||
<path fill="#222222" d="M 11,2.4 A 2.1,2.1 0 1 0 11,6.6 A 2.1,2.1 0 1 0 11,2.4 Z M 9.8,8.4 C 10.2,8.3 10.6,8.25 11,8.25 C 13.3,8.25 15,10 15,13.5 H 11.8 C 11.7,11.5 11,9.7 9.8,8.4 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 529 B |
@@ -0,0 +1,8 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Shared by me: one node passing to two others. Adwaita 50 no longer
|
||||
ships emblem-shared-symbolic. -->
|
||||
<path fill="#222222" d="M 12,1.25 A 2.25,2.25 0 1 0 12,5.75 A 2.25,2.25 0 1 0 12,1.25 Z M 4,5.75 A 2.25,2.25 0 1 0 4,10.25 A 2.25,2.25 0 1 0 4,5.75 Z"/>
|
||||
<path fill="#222222" d="M 4.37,8.65 L 12.37,4.15 L 11.63,2.85 L 3.63,7.35 Z"/>
|
||||
<path fill="#222222" d="M 12,10.25 A 2.25,2.25 0 1 0 12,14.75 A 2.25,2.25 0 1 0 12,10.25 Z"/>
|
||||
<path fill="#222222" d="M 3.63,8.65 L 11.63,13.15 L 12.37,11.85 L 4.37,7.35 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 621 B |
@@ -0,0 +1,10 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Syncing: two arrows chasing each other round a circle. Bundled because
|
||||
Adwaita 50 no longer ships emblem-synchronizing-symbolic. The lower arrow
|
||||
is the upper one turned half a circle, written out in plain coordinates:
|
||||
GTK's symbolic icon renderer drops a transformed group. -->
|
||||
<path fill="#222222" d="M 2,8 A 6,6 0 0 1 12.91,4.56 L 11.48,5.56 A 4.25,4.25 0 0 0 3.75,8 Z"/>
|
||||
<path fill="#222222" d="M 14.23,3.64 L 14.03,7.68 L 10.17,6.48 Z"/>
|
||||
<path fill="#222222" d="M 14,8 A 6,6 0 0 1 3.09,11.44 L 4.52,10.44 A 4.25,4.25 0 0 0 12.25,8 Z"/>
|
||||
<path fill="#222222" d="M 1.77,12.36 L 1.97,8.32 L 5.83,9.52 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 733 B |
@@ -0,0 +1,4 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Up to date: a check knocked out of a filled circle. -->
|
||||
<path fill="#222222" fill-rule="evenodd" d="M 8,1 A 7,7 0 1 0 8,15 A 7,7 0 1 0 8,1 Z M 4.1,8.6 L 5.2,7.5 L 7,9.3 L 10.8,5.4 L 11.9,6.5 L 7,11.5 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 305 B |
@@ -0,0 +1,9 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
|
||||
<!-- Sharing stopped: the share glyph with its last node crossed out. Not
|
||||
a no-entry sign, which reads as an error. -->
|
||||
<path fill="#222222" d="M 12,1.25 A 2.25,2.25 0 1 0 12,5.75 A 2.25,2.25 0 1 0 12,1.25 Z M 4,5.75 A 2.25,2.25 0 1 0 4,10.25 A 2.25,2.25 0 1 0 4,5.75 Z"/>
|
||||
<path fill="#222222" d="M 4.37,8.65 L 12.37,4.15 L 11.63,2.85 L 3.63,7.35 Z"/>
|
||||
<path fill="#222222" d="M 3.63,8.65 L 8.23,11.25 L 8.97,9.95 L 4.37,7.35 Z"/>
|
||||
<path fill="#222222" d="M 9.28,11.02 L 13.48,15.22 L 14.72,13.98 L 10.52,9.78 Z"/>
|
||||
<path fill="#222222" d="M 13.48,9.78 L 9.28,13.98 L 10.52,15.22 L 14.72,11.02 Z"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 706 B |
@@ -10,6 +10,16 @@
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-filter-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-offline-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-online-only-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-cached-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-details-pane-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-link-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-map-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-menu-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-people-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-share-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-sync-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-synced-symbolic.svg</file>
|
||||
<file preprocess="xml-stripblanks">scalable/actions/pdfs-unshare-symbolic.svg</file>
|
||||
<!-- The Proton theme's folder: a full-colour icon from the icon theme
|
||||
can't be recoloured by CSS. -->
|
||||
<file preprocess="xml-stripblanks">scalable/places/pdfs-proton-folder.svg</file>
|
||||
@@ -23,5 +33,7 @@
|
||||
<gresource prefix="/de/nils/protondrivelinux">
|
||||
<file>style.css</file>
|
||||
<file>proton-theme.css</file>
|
||||
<!-- The search launcher's own sheet, over the same palette. -->
|
||||
<file>prompt.css</file>
|
||||
</gresource>
|
||||
</gresources>
|
||||
@@ -0,0 +1,224 @@
|
||||
/* The search launcher (pdfs-prompt). Colours come from the libadwaita
|
||||
palette, so the launcher follows the system accent, light/dark and high
|
||||
contrast like the app; the opt-in Proton theme recolours both alike. */
|
||||
|
||||
window.launcher-window {
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.launcher-card {
|
||||
background-color: var(--view-bg-color);
|
||||
color: var(--view-fg-color);
|
||||
border: 1px solid var(--border-color);
|
||||
border-radius: 14px;
|
||||
box-shadow: 0 16px 48px rgb(0 0 0 / 35%);
|
||||
}
|
||||
|
||||
.search-bar {
|
||||
padding: 14px 18px;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.search-icon {
|
||||
-gtk-icon-size: 20px;
|
||||
color: color-mix(in srgb, currentColor 55%, transparent);
|
||||
}
|
||||
|
||||
.search-entry {
|
||||
background: none;
|
||||
border: none;
|
||||
box-shadow: none;
|
||||
outline: none;
|
||||
padding: 2px 0;
|
||||
font-size: 1.15rem;
|
||||
}
|
||||
|
||||
.key-hint {
|
||||
padding: 2px 8px;
|
||||
border-radius: 6px;
|
||||
background-color: color-mix(in srgb, currentColor 8%, transparent);
|
||||
font-size: 0.75rem;
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
.chip-row {
|
||||
padding: 10px 14px;
|
||||
border-bottom: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.chip {
|
||||
padding: 4px 14px;
|
||||
min-height: 0;
|
||||
border-radius: 999px;
|
||||
background: none;
|
||||
border: 1px solid var(--border-color);
|
||||
box-shadow: none;
|
||||
font-size: 0.86rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.chip:hover {
|
||||
background-color: color-mix(in srgb, currentColor 6%, transparent);
|
||||
}
|
||||
|
||||
.chip:checked {
|
||||
background-color: var(--accent-bg-color);
|
||||
color: var(--accent-fg-color);
|
||||
border-color: var(--accent-bg-color);
|
||||
}
|
||||
|
||||
/* --- results ------------------------------------------------------------ */
|
||||
|
||||
.results {
|
||||
padding: 2px 8px 10px 8px;
|
||||
}
|
||||
|
||||
.section-header {
|
||||
padding: 12px 10px 6px 10px;
|
||||
}
|
||||
|
||||
.section-title {
|
||||
font-size: 0.78rem;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.06em;
|
||||
text-transform: uppercase;
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
.section-icon {
|
||||
-gtk-icon-size: 14px;
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
.result-list {
|
||||
background: none;
|
||||
}
|
||||
|
||||
.result-row {
|
||||
padding: 6px 10px;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.result-row:hover {
|
||||
background-color: color-mix(in srgb, currentColor 5%, transparent);
|
||||
}
|
||||
|
||||
/* A soft tint rather than a solid accent bar: the text keeps its colour, so
|
||||
the highlighted match stays readable on the selected row. */
|
||||
.result-row:selected,
|
||||
.result-row:selected:hover {
|
||||
background-color: color-mix(in srgb, var(--accent-bg-color) 18%, transparent);
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.result-name {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.result-location,
|
||||
.result-meta {
|
||||
font-size: 0.8rem;
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
.result-row:selected .result-location,
|
||||
.result-row:selected .result-meta {
|
||||
opacity: 0.7;
|
||||
}
|
||||
|
||||
.thumb {
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.result-badge,
|
||||
.preview-badge {
|
||||
-gtk-icon-size: 14px;
|
||||
}
|
||||
|
||||
.badge-pinned {
|
||||
color: var(--success-color);
|
||||
}
|
||||
|
||||
.badge-cached {
|
||||
color: var(--accent-color);
|
||||
}
|
||||
|
||||
.badge-cloud,
|
||||
.badge-local {
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
/* --- preview ------------------------------------------------------------ */
|
||||
|
||||
.preview {
|
||||
padding: 16px;
|
||||
background-color: color-mix(in srgb, currentColor 3%, transparent);
|
||||
}
|
||||
|
||||
.preview-thumb {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.preview-thumb .thumb {
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.preview-name {
|
||||
font-weight: 700;
|
||||
font-size: 1.05rem;
|
||||
}
|
||||
|
||||
.preview-kind {
|
||||
font-size: 0.85rem;
|
||||
opacity: 0.6;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.preview-key {
|
||||
font-size: 0.8rem;
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
.preview-value {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
.preview-actions {
|
||||
margin-top: 12px;
|
||||
}
|
||||
|
||||
.preview-action {
|
||||
padding: 6px 8px;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.preview-shortcut {
|
||||
font-size: 0.75rem;
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
.preview-feedback {
|
||||
margin-top: 6px;
|
||||
font-size: 0.8rem;
|
||||
color: var(--accent-color);
|
||||
}
|
||||
|
||||
.preview-empty {
|
||||
opacity: 0.2;
|
||||
}
|
||||
|
||||
/* --- footer ------------------------------------------------------------- */
|
||||
|
||||
.footer {
|
||||
padding: 8px 16px;
|
||||
border-top: 1px solid var(--border-color);
|
||||
}
|
||||
|
||||
.footer-text {
|
||||
font-size: 0.78rem;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
statuspage.compact > scrolledwindow > viewport > box {
|
||||
margin: 24px 12px;
|
||||
}
|
||||
@@ -19,6 +19,22 @@
|
||||
.sidebar-status image.error { color: var(--error-color); }
|
||||
.sidebar-quota progressbar trough, .sidebar-quota progressbar progress { min-height: 4px; }
|
||||
.sidebar-account { padding: 4px 4px 4px 8px; }
|
||||
/* A count of what waits on a page, at the end of its sidebar row */
|
||||
.sidebar-badge {
|
||||
min-width: 10px;
|
||||
padding: 0 7px;
|
||||
border-radius: 999px;
|
||||
background: alpha(currentColor, 0.15);
|
||||
font-size: 0.8em;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
/* Sync page status card: the state is the headline, the detail explains it */
|
||||
.sync-card { padding: 18px 24px; }
|
||||
.sync-card-icon { padding: 6px; }
|
||||
.sync-card-icon.success { color: var(--success-color); }
|
||||
.sync-card-icon.warning { color: var(--warning-color); }
|
||||
.sync-card-icon.error { color: var(--error-color); }
|
||||
|
||||
/* File browser */
|
||||
.file-grid { padding: 6px; }
|
||||
@@ -27,13 +43,11 @@
|
||||
.file-tile:selected, .file-tile:hover:selected { background: color-mix(in srgb, var(--accent-bg-color) 20%, transparent); }
|
||||
.file-thumbnail { border-radius: 7px; }
|
||||
.file-badge { -gtk-icon-shadow: 0 1px 2px rgba(0, 0, 0, 0.5); }
|
||||
.badge-pinned { color: var(--yellow-3); }
|
||||
.badge-pinned { color: var(--success-color); }
|
||||
.badge-cached { color: var(--success-color); }
|
||||
.badge-cloud { color: color-mix(in srgb, currentColor 55%, transparent); }
|
||||
.browser-statusbar { background-color: alpha(currentColor, 0.025); }
|
||||
scale.browser-status-meter trough, progressbar.browser-status-meter trough { min-width: 104px; }
|
||||
scale.browser-status-meter trough { min-height: 6px; }
|
||||
progressbar.browser-status-meter trough, progressbar.browser-status-meter progress { min-height: 6px; }
|
||||
scale.browser-status-meter trough { min-width: 104px; min-height: 6px; }
|
||||
.bulk-bar { padding: 6px 8px; border-radius: 12px; background: color-mix(in srgb, var(--accent-bg-color) 12%, transparent); }
|
||||
.dropzone { border: 2px dashed alpha(currentColor, 0.25); border-radius: 16px; padding: 28px 18px; transition: background 160ms ease, border-color 160ms ease; }
|
||||
.dropzone-active { border-color: var(--accent-bg-color); background: color-mix(in srgb, var(--accent-bg-color) 10%, transparent); }
|
||||
@@ -52,19 +66,19 @@ progressbar.browser-status-meter trough, progressbar.browser-status-meter progre
|
||||
.viewer-nav-btn { background-color: rgba(0, 0, 0, 0.45); color: white; margin: 20px; min-width: 44px; min-height: 44px; border-radius: 50%; opacity: 0.7; transition: opacity 150ms ease, background-color 150ms ease; }
|
||||
.viewer-nav-btn:hover { background-color: rgba(0, 0, 0, 0.85); color: white; opacity: 1; }
|
||||
.viewer-nav-btn:disabled { opacity: 0; }
|
||||
.photo-scrubber { min-width: 20px; padding: 0 2px; }
|
||||
.photo-scrubber value { font-weight: 600; background-color: var(--popover-bg-color); border-radius: 6px; padding: 2px 8px; }
|
||||
.photo-scrubber { background: linear-gradient(to left, alpha(var(--window-bg-color), 0.92) 40%, alpha(var(--window-bg-color), 0)); }
|
||||
.photo-scrubber-knob { background-color: var(--accent-bg-color); border-radius: 2px; }
|
||||
.photo-scrubber-bubble { font-weight: 600; background-color: var(--popover-bg-color); color: var(--popover-fg-color); border-radius: 999px; padding: 4px 12px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3); }
|
||||
.viewer-spinner { color: white; }
|
||||
.viewer-status { color: white; font-size: 1rem; background-color: rgba(0, 0, 0, 0.75); padding: 12px 24px; border-radius: 12px; }
|
||||
.viewer-info-panel { background-color: var(--window-bg-color); border-left: 1px solid alpha(currentColor, 0.12); }
|
||||
|
||||
/* Photos grid */
|
||||
.gallery-day { font-size: 0.82rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; color: alpha(currentColor, 0.65); padding: 2px 2px 6px 2px; }
|
||||
.photo-tile { padding: 0; margin: 0; min-height: 0; min-width: 0; border: none; border-radius: 4px; background: none; box-shadow: none; transition: box-shadow 180ms ease; }
|
||||
.photo-tile:hover { box-shadow: 0 8px 22px rgba(0, 0, 0, 0.45); }
|
||||
.photo-tile { padding: 0; margin: 0; min-height: 0; min-width: 0; border: none; border-radius: 4px; background: none; box-shadow: none; }
|
||||
.photo-tile:focus { outline: 2px solid var(--accent-color); outline-offset: -2px; }
|
||||
.photo-thumb { transition: transform 220ms ease; }
|
||||
.photo-tile:hover .photo-thumb { transform: scale(1.06); }
|
||||
.photo-tile:hover .photo-thumb { transform: scale(1.02); }
|
||||
.photo-check { color: white; background: rgba(0, 0, 0, 0.5); border-radius: 999px; padding: 3px; }
|
||||
.photo-check-on { background: var(--accent-bg-color); color: var(--accent-fg-color); }
|
||||
.photo-tile-selected { outline: 3px solid var(--accent-bg-color); outline-offset: -3px; }
|
||||
@@ -83,10 +97,18 @@ progressbar.browser-status-meter trough, progressbar.browser-status-meter progre
|
||||
.album-cover { border-radius: 16px; background: alpha(currentColor, 0.06); box-shadow: 0 4px 16px rgba(0, 0, 0, 0.32); transition: box-shadow 180ms ease; }
|
||||
.album-card:hover .album-cover { box-shadow: 0 10px 26px rgba(0, 0, 0, 0.5); }
|
||||
.album-card:hover .photo-thumb { transform: scale(1.05); }
|
||||
.view-switch button { padding-left: 18px; padding-right: 18px; }
|
||||
.view-switch button, .view-switch toggle { padding-left: 18px; padding-right: 18px; }
|
||||
|
||||
/* Shared with Me: what a share lets me do, as a tinted pill beside the name. */
|
||||
.role-pill { padding: 2px 10px; border-radius: 999px; font-weight: 600; color: var(--accent-color); background-color: color-mix(in srgb, var(--accent-bg-color) 16%, transparent); }
|
||||
.role-pill.warning-pill { color: var(--warning-color); background-color: color-mix(in srgb, var(--warning-bg-color) 20%, transparent); }
|
||||
|
||||
/* Activity: the feed sits on the window background as one card per row, under
|
||||
* day headings, like a boxed list with its rows set apart. */
|
||||
listview.activity-feed { background: none; color: inherit; padding: 0 12px 18px 12px; }
|
||||
listview.activity-feed > row { padding: 0; margin-bottom: 6px; background: none; }
|
||||
listview.activity-feed > header { padding: 18px 6px 6px 6px; background: none; }
|
||||
listview.activity-feed row.card { border-radius: 12px; }
|
||||
|
||||
/* Photos: how many timeline filters are on, as a small badge on the Filters button. */
|
||||
.filter-count { min-width: 18px; padding: 0 5px; border-radius: 999px; font-size: 0.8em; font-weight: 700; color: var(--accent-fg-color); background-color: var(--accent-bg-color); }
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
//! Dates and times as the app shows them, so every page words a moment the
|
||||
//! same way.
|
||||
//!
|
||||
//! - [`relative`] for "when did this happen" in lists: "Just now", "5 min
|
||||
//! ago", "Yesterday, 14:05", "Tuesday", then a date.
|
||||
//! - [`short_date`] for date columns: "Sep 23", with the year only when it is
|
||||
//! not this year.
|
||||
//! - [`full`] for tooltips: the weekday, date, year and time.
|
||||
//!
|
||||
//! The formats are `strftime` patterns that go through gettext, so they follow
|
||||
//! the language picked in Preferences, not only the system locale.
|
||||
|
||||
use gtk4::glib;
|
||||
|
||||
use crate::{gettext, gettext_f, ngettext_f};
|
||||
|
||||
/// Seconds in a day, for the "within the last week" checks.
|
||||
const DAY: i64 = 86_400;
|
||||
|
||||
/// A past moment in epoch seconds, relative to now. Empty when the time can't
|
||||
/// be read.
|
||||
pub(crate) fn relative(secs: i64) -> String {
|
||||
match (
|
||||
glib::DateTime::from_unix_local(secs),
|
||||
glib::DateTime::now_local(),
|
||||
) {
|
||||
(Ok(at), Ok(now)) => relative_to(&at, &now),
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// [`relative`] for a fixed `now`, so it can be tested.
|
||||
pub(crate) fn relative_to(at: &glib::DateTime, now: &glib::DateTime) -> String {
|
||||
let ago = now.to_unix() - at.to_unix();
|
||||
// A clock a little ahead of ours still reads as "just now".
|
||||
if (-60..60).contains(&ago) {
|
||||
return gettext("Just now");
|
||||
}
|
||||
if (60..3600).contains(&ago) {
|
||||
let minutes = (ago / 60) as u64;
|
||||
return ngettext_f("{n} min ago", "{n} min ago", minutes, &[]);
|
||||
}
|
||||
// Translators: strftime format for a time of day, such as "14:05".
|
||||
let time = || format(at, &gettext("%H:%M"));
|
||||
if at.ymd() == now.ymd() {
|
||||
return time();
|
||||
}
|
||||
if now.add_days(-1).is_ok_and(|y| y.ymd() == at.ymd()) {
|
||||
// Translators: {time} is a time of day, such as "14:05".
|
||||
return gettext_f("Yesterday, {time}", &[("time", &time())]);
|
||||
}
|
||||
if (0..6 * DAY).contains(&ago) {
|
||||
return format(at, "%A");
|
||||
}
|
||||
short_date_to(at, now)
|
||||
}
|
||||
|
||||
/// A date in epoch seconds, without the time. Empty when the time can't be
|
||||
/// read.
|
||||
pub(crate) fn short_date(secs: i64) -> String {
|
||||
match (
|
||||
glib::DateTime::from_unix_local(secs),
|
||||
glib::DateTime::now_local(),
|
||||
) {
|
||||
(Ok(at), Ok(now)) => short_date_to(&at, &now),
|
||||
_ => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// [`short_date`] for a fixed `now`, so it can be tested.
|
||||
pub(crate) fn short_date_to(at: &glib::DateTime, now: &glib::DateTime) -> String {
|
||||
let pattern = if at.year() == now.year() {
|
||||
// Translators: strftime format for a date in this year, such as "Sep 23".
|
||||
gettext("%b %-d")
|
||||
} else {
|
||||
// Translators: strftime format for a date in another year, such as "Sep 23, 2025".
|
||||
gettext("%b %-d, %Y")
|
||||
};
|
||||
format(at, &pattern)
|
||||
}
|
||||
|
||||
/// The whole moment in epoch seconds, for a tooltip. Empty when the time
|
||||
/// can't be read.
|
||||
pub(crate) fn full(secs: i64) -> String {
|
||||
glib::DateTime::from_unix_local(secs)
|
||||
.map(|at| {
|
||||
// Translators: strftime format for a full date and time, such as
|
||||
// "Wednesday, September 23, 2026, 14:05".
|
||||
format(&at, &gettext("%A, %B %-d, %Y, %H:%M"))
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn format(at: &glib::DateTime, pattern: &str) -> String {
|
||||
at.format(pattern)
|
||||
.map(|s| s.trim().to_string())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn at(y: i32, m: i32, d: i32, h: i32, min: i32) -> glib::DateTime {
|
||||
glib::DateTime::from_local(y, m, d, h, min, 0.0).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relative_times_get_coarser_with_age() {
|
||||
let now = at(2026, 9, 23, 15, 0);
|
||||
assert_eq!(relative_to(&at(2026, 9, 23, 14, 59), &now), "1 min ago");
|
||||
assert_eq!(relative_to(&at(2026, 9, 23, 14, 55), &now), "5 min ago");
|
||||
assert_eq!(relative_to(&at(2026, 9, 23, 9, 5), &now), "09:05");
|
||||
assert_eq!(
|
||||
relative_to(&at(2026, 9, 22, 14, 5), &now),
|
||||
"Yesterday, 14:05"
|
||||
);
|
||||
assert_eq!(relative_to(&at(2026, 9, 20, 10, 0), &now), "Sunday");
|
||||
assert_eq!(relative_to(&at(2026, 9, 1, 10, 0), &now), "Sep 1");
|
||||
assert_eq!(relative_to(&at(2024, 3, 5, 10, 0), &now), "Mar 5, 2024");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_moment_ago_or_slightly_ahead_is_just_now() {
|
||||
let now = at(2026, 9, 23, 15, 0);
|
||||
assert_eq!(relative_to(&now, &now), "Just now");
|
||||
assert_eq!(
|
||||
relative_to(&at(2026, 9, 23, 15, 0), &now.add_seconds(30.0).unwrap()),
|
||||
"Just now"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn short_dates_show_the_year_only_when_it_differs() {
|
||||
let now = at(2026, 9, 23, 15, 0);
|
||||
assert_eq!(short_date_to(&at(2026, 1, 2, 8, 0), &now), "Jan 2");
|
||||
assert_eq!(short_date_to(&at(2025, 12, 31, 8, 0), &now), "Dec 31, 2025");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
//! The window's side of the daemon's event feed ([`Request::Subscribe`]).
|
||||
//!
|
||||
//! The window used to ask the daemon for its status every two seconds and let
|
||||
//! pages re-read themselves on timers. Now one thread follows the feed and hands
|
||||
//! each event to the main loop: a status or transfers snapshot is painted as it
|
||||
//! comes, and a change reloads the page on screen if the page shows it. A
|
||||
//! window nobody can see keeps its changes for later and catches up when it is
|
||||
//! focused again.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use pdfs_core::control::{Event, Feed, follow};
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// How long the conflict list waits after a change before it is read again.
|
||||
/// Listing walks every node the daemon knows, and a sync pass can report
|
||||
/// changes many times a second.
|
||||
const CONFLICTS_DEBOUNCE: Duration = Duration::from_secs(3);
|
||||
|
||||
/// How long the import's state waits after its progress moved before it is
|
||||
/// read again.
|
||||
const TAKEOUT_DEBOUNCE: Duration = Duration::from_secs(2);
|
||||
|
||||
/// How old the invitations may be when the window is focused. Another person
|
||||
/// inviting this account sends the daemon no event, so focus is when they are
|
||||
/// looked for.
|
||||
const INVITATIONS_ON_FOCUS: Duration = Duration::from_secs(60);
|
||||
|
||||
/// What the feed keeps between events.
|
||||
#[derive(Default)]
|
||||
struct FeedState {
|
||||
/// What changed while the window was out of sight.
|
||||
deferred: RefCell<BTreeSet<Topic>>,
|
||||
/// Whether a conflict read is already waiting out [`CONFLICTS_DEBOUNCE`].
|
||||
conflicts_pending: Rc<Cell<bool>>,
|
||||
/// Whether an import read is already waiting out [`TAKEOUT_DEBOUNCE`].
|
||||
takeout_pending: Rc<Cell<bool>>,
|
||||
}
|
||||
|
||||
/// Follow the daemon for as long as `window` is open.
|
||||
pub(crate) fn follow_daemon(ui: &Rc<Ui>, window: &adw::ApplicationWindow) {
|
||||
let (tx, rx) = async_channel::bounded(64);
|
||||
let socket = ui.dirs.control_socket();
|
||||
let spawned = std::thread::Builder::new()
|
||||
.name("pdfs-feed".into())
|
||||
.spawn(move || follow(&socket, |feed| tx.send_blocking(feed).is_ok()));
|
||||
if let Err(e) = spawned {
|
||||
tracing::error!("cannot start the event feed: {e}");
|
||||
return;
|
||||
}
|
||||
|
||||
let state = Rc::new(FeedState::default());
|
||||
let ui_feed = ui.clone();
|
||||
let state_feed = state.clone();
|
||||
let window_feed = window.downgrade();
|
||||
let rx_feed = rx.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
while let Ok(feed) = rx_feed.recv().await {
|
||||
let hidden = window_feed.upgrade().is_none_or(|w| window_hidden(&w));
|
||||
on_feed(&ui_feed, &state_feed, feed, hidden);
|
||||
}
|
||||
});
|
||||
|
||||
// Catch up as soon as the user comes back to the window.
|
||||
let ui_focus = ui.clone();
|
||||
window.connect_is_active_notify(move |window| {
|
||||
if !window.is_active() {
|
||||
return;
|
||||
}
|
||||
let topics: Vec<Topic> = state.deferred.take().into_iter().collect();
|
||||
if !topics.is_empty() {
|
||||
follow_changes(&ui_focus, &state, &topics);
|
||||
}
|
||||
refresh_quota(&ui_focus);
|
||||
refresh_invitations_badge(&ui_focus, INVITATIONS_ON_FOCUS);
|
||||
});
|
||||
// Closing the channel ends the loop above, and the thread with the next
|
||||
// event it tries to hand over.
|
||||
window.connect_close_request(move |_| {
|
||||
rx.close();
|
||||
glib::Propagation::Proceed
|
||||
});
|
||||
}
|
||||
|
||||
fn on_feed(ui: &Rc<Ui>, state: &Rc<FeedState>, feed: Feed, hidden: bool) {
|
||||
match feed {
|
||||
// The daemon follows up with its whole state, so there is nothing to
|
||||
// ask for here.
|
||||
Feed::Connected | Feed::Event(Event::Heartbeat) => {}
|
||||
Feed::Event(Event::Status(status)) => {
|
||||
paint_status(ui, *status);
|
||||
// Uploads are what move the quota, and they move the status too.
|
||||
refresh_quota(ui);
|
||||
}
|
||||
// Painted even out of sight: the end of a batch is what sends the
|
||||
// "Sync complete" notification.
|
||||
Feed::Event(Event::Transfers { items, jobs }) => {
|
||||
repaint_transfers(ui, &items, &jobs);
|
||||
follow_takeout(ui, state, &jobs);
|
||||
}
|
||||
Feed::Event(Event::Changed { topics }) if hidden => {
|
||||
state.deferred.borrow_mut().extend(topics);
|
||||
}
|
||||
Feed::Event(Event::Changed { topics }) => follow_changes(ui, state, &topics),
|
||||
// Ask once, which paints "not connected" or "not responding", and clear
|
||||
// progress that can no longer move.
|
||||
Feed::Disconnected => {
|
||||
refresh_status(ui);
|
||||
repaint_transfers(ui, &[], &[]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reload what `topics` says changed: the sidebar badges wherever the user
|
||||
/// is, and the page on screen if it shows one of them.
|
||||
fn follow_changes(ui: &Rc<Ui>, state: &FeedState, topics: &[Topic]) {
|
||||
let changed = |topic| topics.contains(&topic);
|
||||
if changed(Topic::Conflicts) {
|
||||
let ui = ui.clone();
|
||||
debounce(&state.conflicts_pending, CONFLICTS_DEBOUNCE, move || {
|
||||
refresh_conflicts(&ui);
|
||||
if activity_visible(&ui) {
|
||||
refresh_activity_conflicts(&ui, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
if changed(Topic::Shares) {
|
||||
refresh_invitations_badge(ui, Duration::ZERO);
|
||||
}
|
||||
match ui.stack.visible_child_name().as_deref() {
|
||||
Some("browser") if changed(Topic::Files) => load_browser(ui),
|
||||
// The timeline is left alone: reloading it would throw away the place
|
||||
// the user scrolled to. F5 reloads it on request.
|
||||
Some("gallery")
|
||||
if changed(Topic::Photos)
|
||||
&& ui.gallery.content.visible_child_name().as_deref() == Some("albums") =>
|
||||
{
|
||||
load_albums(ui)
|
||||
}
|
||||
Some("trash") if changed(Topic::Trash) => load_trash(ui),
|
||||
Some("shared") if changed(Topic::Shares) => load_shared(ui),
|
||||
Some("sharedbyme") if changed(Topic::Shares) || changed(Topic::Files) => {
|
||||
load_shared_by_me(ui)
|
||||
}
|
||||
Some("devices") if changed(Topic::Devices) => load_devices(ui),
|
||||
Some("locations") => follow_sync_view(ui, topics),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the import's state again while its progress moves, and once more after
|
||||
/// it stops, which is how a finished import is noticed. Nothing is read while
|
||||
/// no import runs and the page is elsewhere.
|
||||
fn follow_takeout(ui: &Rc<Ui>, state: &FeedState, jobs: &[JobItem]) {
|
||||
let importing = jobs.iter().any(|job| job.title == IMPORT_JOB);
|
||||
if importing
|
||||
|| ui.takeout.running.get()
|
||||
|| ui.stack.visible_child_name().as_deref() == Some("takeout")
|
||||
{
|
||||
let ui = ui.clone();
|
||||
debounce(&state.takeout_pending, TAKEOUT_DEBOUNCE, move || {
|
||||
refresh_takeout(&ui)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Run `f` once `delay` has passed, unless a run is already waiting. The run
|
||||
/// sees everything that happened up to it, so nothing is missed by skipping.
|
||||
fn debounce(pending: &Rc<Cell<bool>>, delay: Duration, f: impl FnOnce() + 'static) {
|
||||
if pending.replace(true) {
|
||||
return;
|
||||
}
|
||||
let pending = pending.clone();
|
||||
glib::timeout_add_local_once(delay, move || {
|
||||
pending.set(false);
|
||||
f();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,609 @@
|
||||
//! Activity entries as the feed words them.
|
||||
//!
|
||||
//! The daemon logs each entry in plain English for its own log and the CLI:
|
||||
//! a target ("report.pdf", "3 item(s)", "sync conflict in Docs") and a detail
|
||||
//! ("to /Documents", "was notes.txt", "2 uploaded, 1 deleted"). This module
|
||||
//! reads the shapes the daemon writes and says them again in the user's
|
||||
//! language. A shape it does not know, which is mostly an error message,
|
||||
//! shows as the daemon wrote it.
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// What a feed row says about one entry.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) struct Described {
|
||||
pub(crate) title: String,
|
||||
/// Subtitle parts, shown joined by " · ".
|
||||
pub(crate) details: Vec<String>,
|
||||
}
|
||||
|
||||
/// The title and subtitle parts for `entry`.
|
||||
pub(crate) fn describe(entry: &ActivityEntry) -> Described {
|
||||
let title = entry_title(entry);
|
||||
let mut details = Vec::new();
|
||||
if entry.kind == ActivityKind::Conflict
|
||||
&& let Some(stamp) = conflict_stamp(&entry.target)
|
||||
{
|
||||
let when = dates::relative(stamp);
|
||||
if !when.is_empty() {
|
||||
// Translators: {time} is when a conflicting copy was made, such as
|
||||
// "Sep 21" or "5 min ago".
|
||||
details.push(gettext_f("Made {time}", &[("time", &when)]));
|
||||
}
|
||||
}
|
||||
if let Some(detail) = detail_label(entry) {
|
||||
details.insert(0, detail);
|
||||
}
|
||||
Described { title, details }
|
||||
}
|
||||
|
||||
/// The row title: what happened to what, one sentence per case so a
|
||||
/// translator can place the name where their language needs it.
|
||||
fn entry_title(entry: &ActivityEntry) -> String {
|
||||
use ActivityKind::*;
|
||||
let target = entry.target.as_str();
|
||||
let ok = entry.ok;
|
||||
|
||||
// Entries whose target is a count or a fixed phrase rather than a name.
|
||||
match (entry.kind, target) {
|
||||
(Upload, "Google Photos import") => {
|
||||
return if ok {
|
||||
gettext("Imported from Google Photos")
|
||||
} else {
|
||||
gettext("Couldn't import from Google Photos")
|
||||
};
|
||||
}
|
||||
(Upload, "bulk upload") => return gettext("Couldn't upload files"),
|
||||
(Upload, "add sync folder") => return gettext("Couldn't add a sync folder"),
|
||||
(Download, "restore folders") => return gettext("Couldn't restore folders"),
|
||||
(Unshare, "shared item") => return gettext("Left a shared item"),
|
||||
_ => {}
|
||||
}
|
||||
if matches!(entry.kind, Restore | DeleteForever | EmptyTrash)
|
||||
&& let Some(n) = leading_count(target, &["item(s)", "item", "items"])
|
||||
{
|
||||
return match entry.kind {
|
||||
Restore => ngettext_f("Restored {n} item", "Restored {n} items", n, &[]),
|
||||
DeleteForever => ngettext_f(
|
||||
"Deleted {n} item forever",
|
||||
"Deleted {n} items forever",
|
||||
n,
|
||||
&[],
|
||||
),
|
||||
_ => ngettext_f(
|
||||
"Emptied the Trash ({n} item)",
|
||||
"Emptied the Trash ({n} items)",
|
||||
n,
|
||||
&[],
|
||||
),
|
||||
};
|
||||
}
|
||||
if entry.kind == Download
|
||||
&& let Some(rest) = target.strip_prefix("restored ")
|
||||
&& let Some(n) = leading_count(rest, &["folder(s)"])
|
||||
{
|
||||
return ngettext_f("Restored {n} folder", "Restored {n} folders", n, &[]);
|
||||
}
|
||||
if entry.kind == Sync
|
||||
&& let Some(title) = sync_title(target)
|
||||
{
|
||||
return title;
|
||||
}
|
||||
|
||||
let Some(name) = target_name(target) else {
|
||||
return match entry.kind {
|
||||
Share => gettext("Shared an item"),
|
||||
PublicLink => gettext("Created a link to an item"),
|
||||
Unshare if entry.detail == "link removed" => gettext("Removed a link to an item"),
|
||||
Unshare => gettext("Stopped sharing an item"),
|
||||
_ => activity_title(entry.kind, ok, target),
|
||||
};
|
||||
};
|
||||
if entry.kind == Unshare && entry.detail == "link removed" {
|
||||
// Translators: {name} is a file or folder name.
|
||||
return gettext_f("Removed the link to {name}", &[("name", &name)]);
|
||||
}
|
||||
if entry.kind == Conflict
|
||||
&& let Some(base) = conflict_base(&name)
|
||||
{
|
||||
// Translators: {name} is the file the conflicting copy was made from.
|
||||
return gettext_f("Conflicting copy of {name}", &[("name", &base)]);
|
||||
}
|
||||
activity_title(entry.kind, ok, &name)
|
||||
}
|
||||
|
||||
/// The title of a sync pass or mode switch, whose target is a phrase.
|
||||
fn sync_title(target: &str) -> Option<String> {
|
||||
if let Some(name) = target.strip_prefix("sync conflict in ") {
|
||||
// Translators: {name} is a synced folder's name.
|
||||
return Some(gettext_f(
|
||||
"Conflicting changes in {name}",
|
||||
&[("name", name)],
|
||||
));
|
||||
}
|
||||
if let Some(name) = target.strip_prefix("sync incomplete for ") {
|
||||
// Translators: {name} is a synced folder's name.
|
||||
return Some(gettext_f(
|
||||
"Some items in {name} didn't sync",
|
||||
&[("name", name)],
|
||||
));
|
||||
}
|
||||
if let Some(name) = target.strip_prefix("sync failed for ") {
|
||||
// Translators: {name} is a synced folder's name.
|
||||
return Some(gettext_f("Couldn't sync {name}", &[("name", name)]));
|
||||
}
|
||||
if let Some(name) = target.strip_prefix("couldn't switch ") {
|
||||
// Translators: {name} is a synced folder's name.
|
||||
return Some(gettext_f(
|
||||
"Couldn't change how {name} syncs",
|
||||
&[("name", name)],
|
||||
));
|
||||
}
|
||||
if let Some(path) = target.strip_suffix(" is now on-demand") {
|
||||
let name = file_name(path);
|
||||
// Translators: {name} is a folder's name.
|
||||
return Some(gettext_f("{name} is now online only", &[("name", name)]));
|
||||
}
|
||||
if let Some(path) = target.strip_suffix(" is mirroring again; downloading") {
|
||||
let name = file_name(path);
|
||||
// Translators: {name} is a folder's name.
|
||||
return Some(gettext_f("{name} is synced again", &[("name", name)]));
|
||||
}
|
||||
if target.starts_with("already ") {
|
||||
return Some(gettext("Sync mode was already set"));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// The row title for an activity on a named item.
|
||||
pub(crate) fn activity_title(kind: ActivityKind, ok: bool, name: &str) -> String {
|
||||
let args = [("name", name)];
|
||||
match (kind, ok) {
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Upload, true) => gettext_f("Uploaded {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Upload, false) => gettext_f("Couldn't upload {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Download, true) => gettext_f("Downloaded {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Download, false) => gettext_f("Couldn't download {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Sync, true) => gettext_f("Synced {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Sync, false) => gettext_f("Couldn't sync {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Rename, true) => gettext_f("Renamed {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Rename, false) => gettext_f("Couldn't rename {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Move, true) => gettext_f("Moved {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Move, false) => gettext_f("Couldn't move {name}", &args),
|
||||
// Translators: {name} is a folder name.
|
||||
(ActivityKind::CreateFolder, true) => gettext_f("Created folder {name}", &args),
|
||||
// Translators: {name} is a folder name.
|
||||
(ActivityKind::CreateFolder, false) => gettext_f("Couldn't create folder {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Trash, true) => gettext_f("Moved {name} to the Trash", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Trash, false) => gettext_f("Couldn't move {name} to the Trash", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Restore, true) => gettext_f("Restored {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Restore, false) => gettext_f("Couldn't restore {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::DeleteForever, true) => gettext_f("Deleted {name} forever", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::DeleteForever, false) => gettext_f("Couldn't delete {name}", &args),
|
||||
(ActivityKind::EmptyTrash, true) => gettext("Emptied the Trash"),
|
||||
(ActivityKind::EmptyTrash, false) => gettext("Couldn't empty the Trash"),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Share, true) => gettext_f("Shared {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Share, false) => gettext_f("Couldn't share {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::PublicLink, true) => gettext_f("Created a link to {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::PublicLink, false) => gettext_f("Couldn't create a link to {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Unshare, true) => gettext_f("Stopped sharing {name}", &args),
|
||||
// Translators: {name} is a file or folder name.
|
||||
(ActivityKind::Unshare, false) => gettext_f("Couldn't stop sharing {name}", &args),
|
||||
// Translators: {name} is the name of the conflicting copy.
|
||||
(ActivityKind::Conflict, _) => gettext_f("Conflicting copy {name}", &args),
|
||||
}
|
||||
}
|
||||
|
||||
/// The subtitle's first part: the daemon's detail in the user's language, or
|
||||
/// `None` when it only repeats the title.
|
||||
fn detail_label(entry: &ActivityEntry) -> Option<String> {
|
||||
let detail = entry.detail.trim();
|
||||
let name = |s: &str| file_name(s).to_string();
|
||||
match detail {
|
||||
""
|
||||
| "uploaded"
|
||||
| "created"
|
||||
| "trashed"
|
||||
| "trashed from the mount"
|
||||
| "stopped sharing"
|
||||
| "left"
|
||||
| "link created"
|
||||
| "link removed"
|
||||
| "access removed"
|
||||
| "removed" => {
|
||||
return None;
|
||||
}
|
||||
"removed on Drive" => return Some(gettext("Removed on Proton Drive")),
|
||||
"removed locally" => return Some(gettext("Removed on this computer")),
|
||||
"replaced by a rename from the mount" => {
|
||||
return Some(gettext("Replaced by a renamed file"));
|
||||
}
|
||||
"destination folder no longer exists; the file was left in place" => {
|
||||
return Some(gettext(
|
||||
"The destination folder no longer exists, so the file stayed where it was",
|
||||
));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if entry.kind == ActivityKind::Upload
|
||||
&& entry.target == "Google Photos import"
|
||||
&& entry.ok
|
||||
&& let Some(summary) = import_summary(detail)
|
||||
{
|
||||
return Some(summary);
|
||||
}
|
||||
if entry.kind == ActivityKind::Sync
|
||||
&& let Some(summary) = sync_summary(detail)
|
||||
{
|
||||
return Some(summary);
|
||||
}
|
||||
if let Some(dest) = detail.strip_prefix("to ") {
|
||||
// Translators: {name} is the folder an item was moved into.
|
||||
return Some(gettext_f("To {name}", &[("name", &folder_name(dest))]));
|
||||
}
|
||||
if let Some(old) = detail.strip_prefix("was ") {
|
||||
// Translators: {name} is an item's previous name.
|
||||
return Some(gettext_f("Previously {name}", &[("name", &name(old))]));
|
||||
}
|
||||
if detail.starts_with("restored version ") {
|
||||
return Some(gettext("Restored an earlier version"));
|
||||
}
|
||||
if detail.starts_with("deleted version ") {
|
||||
return Some(gettext("Deleted an earlier version"));
|
||||
}
|
||||
if let Some(rest) = detail.strip_prefix("saved version ")
|
||||
&& let Some((_, path)) = rest.split_once(" to ")
|
||||
{
|
||||
// Translators: {path} is where an earlier version was saved.
|
||||
return Some(gettext_f(
|
||||
"Saved an earlier version to {path}",
|
||||
&[("path", &tilde_path(path))],
|
||||
));
|
||||
}
|
||||
if let Some(copy) = detail.strip_prefix("kept both; the copy is now ") {
|
||||
// Translators: {name} is the new name of the conflicting copy.
|
||||
return Some(gettext_f(
|
||||
"Kept both; the copy is now {name}",
|
||||
&[("name", copy)],
|
||||
));
|
||||
}
|
||||
if let Some(base) = detail
|
||||
.strip_prefix("kept ")
|
||||
.and_then(|rest| rest.strip_suffix("; the copy is in Trash"))
|
||||
{
|
||||
// Translators: {name} is the file that was kept.
|
||||
return Some(gettext_f(
|
||||
"Kept {name}; the copy is in the Trash",
|
||||
&[("name", base)],
|
||||
));
|
||||
}
|
||||
if detail.starts_with("kept the conflict copy ") {
|
||||
return Some(gettext("Kept the conflicting copy"));
|
||||
}
|
||||
if let Some(base) = detail.strip_prefix("kept ") {
|
||||
// Translators: {name} is the file that was kept.
|
||||
return Some(gettext_f("Kept {name}", &[("name", base)]));
|
||||
}
|
||||
if let Some(base) = detail.strip_prefix("auto-removed duplicate of ") {
|
||||
// Translators: {name} is the file the removed copy was identical to.
|
||||
return Some(gettext_f(
|
||||
"Same as {name}, so it was removed",
|
||||
&[("name", base)],
|
||||
));
|
||||
}
|
||||
if let Some(alt) = detail.strip_prefix("destination already had that name; moved as ") {
|
||||
// Translators: {name} is the name the item was given instead.
|
||||
return Some(gettext_f(
|
||||
"The name was taken, so it was moved as {name}",
|
||||
&[("name", alt)],
|
||||
));
|
||||
}
|
||||
if let Some(alt) = detail.strip_prefix("name was taken remotely; renamed to ") {
|
||||
// Translators: {name} is the name the item was given instead.
|
||||
return Some(gettext_f(
|
||||
"The name was taken on Proton Drive, so it was renamed to {name}",
|
||||
&[("name", alt)],
|
||||
));
|
||||
}
|
||||
if let Some(alt) = detail.strip_prefix("name was taken remotely; created as ") {
|
||||
// Translators: {name} is the name the item was given instead.
|
||||
return Some(gettext_f(
|
||||
"The name was taken on Proton Drive, so it was created as {name}",
|
||||
&[("name", alt)],
|
||||
));
|
||||
}
|
||||
if let Some(alt) =
|
||||
detail.strip_prefix("its folder was trashed remotely; created in the root as ")
|
||||
{
|
||||
// Translators: {name} is the name the item was given.
|
||||
return Some(gettext_f(
|
||||
"Its folder was moved to the Trash on Proton Drive, so it was created at the top as {name}",
|
||||
&[("name", alt)],
|
||||
));
|
||||
}
|
||||
if let Some(base) = detail.strip_prefix("differs from ") {
|
||||
// Translators: {name} is the file the conflicting copy was made from.
|
||||
return Some(gettext_f("Differs from {name}", &[("name", base)]));
|
||||
}
|
||||
if let Some(base) = detail
|
||||
.strip_prefix("no original ")
|
||||
.and_then(|rest| rest.strip_suffix(" to reconcile against"))
|
||||
{
|
||||
// Translators: {name} is the file the conflicting copy was made from.
|
||||
return Some(gettext_f("{name} no longer exists", &[("name", base)]));
|
||||
}
|
||||
if let Some(base) = detail
|
||||
.strip_prefix("identical to ")
|
||||
.and_then(|rest| rest.split_once(';').map(|(base, _)| base))
|
||||
{
|
||||
// Translators: {name} is the file the conflicting copy was made from.
|
||||
return Some(gettext_f("Same as {name}", &[("name", base)]));
|
||||
}
|
||||
if let Some((n, role)) = detail
|
||||
.split_once(" recipient(s) as ")
|
||||
.and_then(|(n, role)| Some((n.parse::<u64>().ok()?, role)))
|
||||
{
|
||||
return Some(match role {
|
||||
"viewer" => ngettext_f("{n} person can view", "{n} people can view", n, &[]),
|
||||
"editor" => ngettext_f("{n} person can edit", "{n} people can edit", n, &[]),
|
||||
_ => ngettext_f("{n} person invited", "{n} people invited", n, &[]),
|
||||
});
|
||||
}
|
||||
if let Some(n) = leading_count(detail, &["file(s) kept as conflict copies"]) {
|
||||
return Some(ngettext_f(
|
||||
"{n} file kept as a conflicting copy",
|
||||
"{n} files kept as conflicting copies",
|
||||
n,
|
||||
&[],
|
||||
));
|
||||
}
|
||||
if let Some(n) = leading_count(detail, &["item(s) failed; will retry"]) {
|
||||
return Some(ngettext_f(
|
||||
"{n} item failed and will be tried again",
|
||||
"{n} items failed and will be tried again",
|
||||
n,
|
||||
&[],
|
||||
));
|
||||
}
|
||||
Some(capitalize(detail))
|
||||
}
|
||||
|
||||
/// A sync pass summary such as "2 uploaded, 1 folder(s) created", as a
|
||||
/// list of counts. `None` when a part is not one the daemon writes.
|
||||
fn sync_summary(detail: &str) -> Option<String> {
|
||||
let mut parts = Vec::new();
|
||||
for part in detail.split(", ") {
|
||||
let (n, label) = part.split_once(' ')?;
|
||||
let n = n.parse::<u64>().ok()?;
|
||||
parts.push(match label {
|
||||
// Translators: part of a list summing up a sync pass.
|
||||
"uploaded" => ngettext_f("{n} uploaded", "{n} uploaded", n, &[]),
|
||||
// Translators: part of a list summing up a sync pass.
|
||||
"downloaded" => ngettext_f("{n} downloaded", "{n} downloaded", n, &[]),
|
||||
// Translators: part of a list summing up a sync pass.
|
||||
"folder(s) created" => ngettext_f("{n} folder created", "{n} folders created", n, &[]),
|
||||
// Translators: part of a list summing up a sync pass.
|
||||
"deleted" => ngettext_f("{n} deleted", "{n} deleted", n, &[]),
|
||||
// Translators: part of a list summing up a sync pass.
|
||||
"conflicted" => ngettext_f("{n} in conflict", "{n} in conflict", n, &[]),
|
||||
// Translators: part of a list summing up a sync pass; the files are
|
||||
// still open in another app.
|
||||
"deferred (open for write)" => ngettext_f("{n} still open", "{n} still open", n, &[]),
|
||||
// Translators: part of a list summing up a sync pass.
|
||||
"failed" => ngettext_f("{n} failed", "{n} failed", n, &[]),
|
||||
_ => return None,
|
||||
});
|
||||
}
|
||||
Some(parts.join(", "))
|
||||
}
|
||||
|
||||
/// A Google Photos import summary such as "40 uploaded · 2 already there".
|
||||
fn import_summary(detail: &str) -> Option<String> {
|
||||
let mut parts = Vec::new();
|
||||
for part in detail.split(" · ") {
|
||||
if part == "cancelled" {
|
||||
parts.push(gettext("Cancelled"));
|
||||
continue;
|
||||
}
|
||||
let (n, label) = part.split_once(' ')?;
|
||||
let n = n.parse::<u64>().ok()?;
|
||||
parts.push(match label {
|
||||
"uploaded" => ngettext_f("{n} uploaded", "{n} uploaded", n, &[]),
|
||||
// Translators: photos an import skipped because they were already
|
||||
// in Proton Drive.
|
||||
"already there" => ngettext_f("{n} already there", "{n} already there", n, &[]),
|
||||
"albums" => ngettext_f("{n} album", "{n} albums", n, &[]),
|
||||
"failed" => ngettext_f("{n} failed", "{n} failed", n, &[]),
|
||||
_ => return None,
|
||||
});
|
||||
}
|
||||
Some(parts.join(" · "))
|
||||
}
|
||||
|
||||
/// The number in front of `text` when the rest is one of `units`, as in
|
||||
/// "3 item(s)".
|
||||
fn leading_count(text: &str, units: &[&str]) -> Option<u64> {
|
||||
let (n, unit) = text.split_once(' ')?;
|
||||
units.contains(&unit).then_some(())?;
|
||||
n.parse().ok()
|
||||
}
|
||||
|
||||
/// The name to show for a target: the last part of a path, or `None` when
|
||||
/// the daemon logged a node id because it had no path.
|
||||
fn target_name(target: &str) -> Option<String> {
|
||||
let looks_like_uid =
|
||||
target.len() >= 40 && target.matches('~').count() == 1 && !target.contains(['/', ' ', '.']);
|
||||
if target.is_empty() || looks_like_uid {
|
||||
return None;
|
||||
}
|
||||
Some(file_name(target).to_string())
|
||||
}
|
||||
|
||||
/// The folder a destination path names, with the root read as "My files".
|
||||
fn folder_name(dest: &str) -> String {
|
||||
let name = file_name(dest.trim_end_matches('/'));
|
||||
if name.is_empty() {
|
||||
gettext("My files")
|
||||
} else {
|
||||
name.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
/// The file a `name (sync-conflict <stamp>).ext` copy was made from.
|
||||
pub(crate) fn conflict_base(name: &str) -> Option<String> {
|
||||
let (start, close) = conflict_marker(name)?;
|
||||
Some(format!("{}{}", &name[..start], &name[close + 1..]))
|
||||
}
|
||||
|
||||
/// When a conflicting copy was made, from the stamp in its name.
|
||||
fn conflict_stamp(name: &str) -> Option<i64> {
|
||||
let (start, close) = conflict_marker(name)?;
|
||||
let inner = &name[start + MARKER.len()..close];
|
||||
inner.split('-').next()?.parse().ok()
|
||||
}
|
||||
|
||||
const MARKER: &str = " (sync-conflict ";
|
||||
|
||||
/// Where the ` (sync-conflict <stamp>[-<n>])` marker starts and where its
|
||||
/// closing parenthesis is. The same shape the daemon reads back.
|
||||
fn conflict_marker(name: &str) -> Option<(usize, usize)> {
|
||||
let start = name.find(MARKER)?;
|
||||
let after = start + MARKER.len();
|
||||
let close = after + name[after..].find(')')?;
|
||||
let inner = &name[after..close];
|
||||
let mut parts = inner.splitn(2, '-');
|
||||
let digits = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit());
|
||||
if !digits(parts.next()?) || parts.next().is_some_and(|s| !digits(s)) {
|
||||
return None;
|
||||
}
|
||||
Some((start, close))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn entry(kind: ActivityKind, target: &str, detail: &str, ok: bool) -> ActivityEntry {
|
||||
ActivityEntry {
|
||||
time: 0,
|
||||
kind,
|
||||
target: target.into(),
|
||||
detail: detail.into(),
|
||||
ok,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn details_that_repeat_the_title_are_dropped() {
|
||||
let d = describe(&entry(ActivityKind::Upload, "a.txt", "uploaded", true));
|
||||
assert_eq!(d.title, "Uploaded a.txt");
|
||||
assert!(d.details.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paths_and_counts_read_as_names_and_numbers() {
|
||||
let d = describe(&entry(
|
||||
ActivityKind::Move,
|
||||
"/mnt/a.txt",
|
||||
"to /mnt/Docs",
|
||||
true,
|
||||
));
|
||||
assert_eq!(d.title, "Moved a.txt");
|
||||
assert_eq!(d.details, vec!["To Docs"]);
|
||||
let d = describe(&entry(ActivityKind::Restore, "3 item(s)", "", true));
|
||||
assert_eq!(d.title, "Restored 3 items");
|
||||
let d = describe(&entry(ActivityKind::EmptyTrash, "1 item", "", true));
|
||||
assert_eq!(d.title, "Emptied the Trash (1 item)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failures_say_what_could_not_happen() {
|
||||
let d = describe(&entry(
|
||||
ActivityKind::Upload,
|
||||
"a.txt",
|
||||
"quota exceeded",
|
||||
false,
|
||||
));
|
||||
assert_eq!(d.title, "Couldn't upload a.txt");
|
||||
assert_eq!(d.details, vec!["Quota exceeded"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sync_passes_are_summed_up() {
|
||||
let d = describe(&entry(
|
||||
ActivityKind::Sync,
|
||||
"Docs",
|
||||
"2 uploaded, 1 folder(s) created",
|
||||
true,
|
||||
));
|
||||
assert_eq!(d.title, "Synced Docs");
|
||||
assert_eq!(d.details, vec!["2 uploaded, 1 folder created"]);
|
||||
let d = describe(&entry(
|
||||
ActivityKind::Sync,
|
||||
"sync incomplete for Docs",
|
||||
"2 item(s) failed; will retry",
|
||||
false,
|
||||
));
|
||||
assert_eq!(d.title, "Some items in Docs didn't sync");
|
||||
assert_eq!(d.details, vec!["2 items failed and will be tried again"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_conflicting_copy_names_its_original() {
|
||||
let d = describe(&entry(
|
||||
ActivityKind::Conflict,
|
||||
"notes (sync-conflict 1700000000).txt",
|
||||
"differs from notes.txt",
|
||||
false,
|
||||
));
|
||||
assert_eq!(d.title, "Conflicting copy of notes.txt");
|
||||
assert_eq!(d.details[0], "Differs from notes.txt");
|
||||
assert_eq!(d.details.len(), 2);
|
||||
assert_eq!(conflict_base("a (sync-conflict 12-2)"), Some("a".into()));
|
||||
assert_eq!(conflict_base("a (sync-conflict x).txt"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn node_ids_are_not_shown_as_names() {
|
||||
let uid = format!("{}~{}", "A".repeat(30), "B".repeat(30));
|
||||
let d = describe(&entry(
|
||||
ActivityKind::Share,
|
||||
&uid,
|
||||
"1 recipient(s) as viewer",
|
||||
true,
|
||||
));
|
||||
assert_eq!(d.title, "Shared an item");
|
||||
assert_eq!(d.details, vec!["1 person can view"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_details_show_as_written() {
|
||||
let d = describe(&entry(
|
||||
ActivityKind::Download,
|
||||
"a.txt",
|
||||
"network timeout",
|
||||
false,
|
||||
));
|
||||
assert_eq!(d.details, vec!["Network timeout"]);
|
||||
}
|
||||
}
|
||||
@@ -83,7 +83,7 @@ pub(crate) fn load_albums(ui: &Rc<Ui>) {
|
||||
&ui,
|
||||
"network-offline-symbolic",
|
||||
&gettext("Not connected"),
|
||||
&gettext("The Proton Drive mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -286,7 +286,7 @@ pub(crate) fn prompt_new_album(ui: &Rc<Ui>, photos: Vec<String>) {
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&gettext("Couldn't create the album"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -351,7 +351,11 @@ fn run_album_request(ui: &Rc<Ui>, request: Request, done: String, failed: String
|
||||
Ok(Ok(Response::Error { message, kind })) => {
|
||||
toast_failure(&ui, &failed, &message, kind)
|
||||
}
|
||||
_ => toast_error(&ui, &failed, &gettext("The mount service didn't respond.")),
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&failed,
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -380,7 +384,7 @@ pub(crate) fn prompt_add_to_album(ui: &Rc<Ui>, photos: Vec<String>) {
|
||||
toast_error(
|
||||
&ui,
|
||||
&gettext("Couldn't load albums"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -487,7 +491,7 @@ fn add_to_album(ui: &Rc<Ui>, album: String, photos: Vec<String>) {
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&gettext("Couldn't add to the album"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -559,7 +563,7 @@ pub(crate) fn remove_from_album(ui: &Rc<Ui>, album: &AlbumInfo, photos: Vec<Stri
|
||||
toast_error(
|
||||
&ui,
|
||||
&gettext("Couldn't remove from the album"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -614,7 +618,7 @@ pub(crate) fn open_album(ui: &Rc<Ui>, album: AlbumInfo) {
|
||||
// would silently do something else. The Photos/Albums switcher goes too: an
|
||||
// album is a level below it, and back is the way out.
|
||||
ui.gallery.filters.set_visible(false);
|
||||
ui.gallery.view_switch.set_visible(false);
|
||||
ui.gallery.view_switch.widget().set_visible(false);
|
||||
ui.gallery.upload.set_visible(false);
|
||||
ui.gallery.back.set_visible(true);
|
||||
// A stale kind/date filter would otherwise be sent with the album request.
|
||||
@@ -632,7 +636,7 @@ pub(crate) fn close_album(ui: &Rc<Ui>) {
|
||||
return;
|
||||
}
|
||||
ui.gallery.title.set_title(&gettext("Photos"));
|
||||
ui.gallery.view_switch.set_visible(true);
|
||||
ui.gallery.view_switch.widget().set_visible(true);
|
||||
ui.gallery.upload.set_visible(true);
|
||||
ui.gallery.back.set_visible(false);
|
||||
}
|
||||
@@ -649,17 +653,15 @@ fn albums_status(ui: &Rc<Ui>, icon: &str, title: &str, description: &str) {
|
||||
/// whole content area; back leaves an open album for the grid it came from.
|
||||
pub(crate) fn wire_albums(ui: &Rc<Ui>) {
|
||||
let ui_albums = ui.clone();
|
||||
ui.gallery.albums_btn.clone().connect_toggled(move |btn| {
|
||||
if btn.is_active() {
|
||||
ui.gallery.view_switch.connect_changed(move |view| {
|
||||
if view == VIEW_ALBUMS {
|
||||
show_albums(&ui_albums);
|
||||
}
|
||||
});
|
||||
|
||||
// Only the button being switched *on* acts: the group fires `toggled` for
|
||||
// the one going off too, and acting on both would load twice.
|
||||
let ui_photos = ui.clone();
|
||||
ui.gallery.photos_btn.clone().connect_toggled(move |btn| {
|
||||
if !btn.is_active() {
|
||||
ui.gallery.view_switch.connect_changed(move |view| {
|
||||
if view != VIEW_TIMELINE {
|
||||
return;
|
||||
}
|
||||
close_album(&ui_photos);
|
||||
|
||||
@@ -1,85 +1,115 @@
|
||||
use crate::*;
|
||||
|
||||
pub(crate) struct DevicesState {
|
||||
// Devices page: two sections (synced folders + other devices), rebuilt
|
||||
// wholesale on each load.
|
||||
// Computers page: one list of the account's computers, this one first, and
|
||||
// a read-only browser over another computer's backup.
|
||||
/// "list", "browse", or "status" while the page loads or can't be read.
|
||||
pub(crate) content: gtk4::Stack,
|
||||
pub(crate) status: adw::StatusPage,
|
||||
pub(crate) retry: gtk4::Button,
|
||||
pub(crate) group: adw::PreferencesGroup,
|
||||
/// Rows added last time, removed before the next paint (adw groups have no
|
||||
/// clear-all).
|
||||
pub(crate) rows: RefCell<Vec<gtk4::Widget>>,
|
||||
pub(crate) sync_group: adw::PreferencesGroup,
|
||||
/// The single "This computer" summary row. The per-folder rows moved to the
|
||||
/// Locations page, which owns every local path.
|
||||
pub(crate) sync_rows: RefCell<Vec<gtk4::Widget>>,
|
||||
/// "Rename" action in the "This computer" header. Insensitive until the
|
||||
/// device list identifies this machine's own device.
|
||||
pub(crate) rename_this: gtk4::Button,
|
||||
/// This machine's own device `(uid, name)`, from the last device list. The
|
||||
/// current device is filtered out of the "Other computers" rows, so this is
|
||||
/// where its rename target lives. `None` until identified.
|
||||
pub(crate) this_device: RefCell<Option<(String, String)>>,
|
||||
pub(crate) inflight: Cell<bool>,
|
||||
pub(crate) loaded_at: Cell<Option<Instant>>,
|
||||
/// The browse view's files.
|
||||
pub(crate) files: FileList,
|
||||
/// The browse view: "list", or "empty".
|
||||
pub(crate) files_content: gtk4::Stack,
|
||||
/// The breadcrumb trail from "Computers" down to the current folder.
|
||||
pub(crate) crumb: gtk4::Box,
|
||||
/// Grid or list for the browse view; hidden on the device list.
|
||||
pub(crate) layout: gtk4::Button,
|
||||
/// Where the browse view is, as `(uid, name)`: the computer first, then each
|
||||
/// folder below it. Empty = the device list. A backup has no path in the
|
||||
/// mount, so descending is uid-addressed and the stack is the breadcrumb.
|
||||
pub(crate) nav: RefCell<Vec<(String, String)>>,
|
||||
/// The [`DevicesState::nav`] the page on screen was painted for, or `None`
|
||||
/// before the first paint.
|
||||
pub(crate) listed: RefCell<Option<Vec<(String, String)>>>,
|
||||
/// Runs the loads; the rows stay up while one runs.
|
||||
pub(crate) loader: Rc<Loader>,
|
||||
}
|
||||
|
||||
impl DevicesState {
|
||||
pub(crate) fn new(widgets: &DevicesWidgets) -> Self {
|
||||
DevicesState {
|
||||
content: widgets.content.clone(),
|
||||
status: widgets.status.clone(),
|
||||
retry: widgets.retry.clone(),
|
||||
group: widgets.group.clone(),
|
||||
rows: RefCell::new(Vec::new()),
|
||||
files: widgets.files.clone(),
|
||||
files_content: widgets.files_content.clone(),
|
||||
crumb: widgets.crumb.clone(),
|
||||
layout: widgets.layout.clone(),
|
||||
nav: RefCell::new(Vec::new()),
|
||||
listed: RefCell::new(None),
|
||||
loader: Loader::new(&widgets.content),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Widgets the Devices page's load/repaint touch.
|
||||
pub(crate) struct DevicesWidgets {
|
||||
pub(crate) content: gtk4::Stack,
|
||||
pub(crate) status: adw::StatusPage,
|
||||
/// "This computer" — this machine's device identity and a pointer to its
|
||||
/// folders on the Locations page.
|
||||
pub(crate) sync_group: adw::PreferencesGroup,
|
||||
/// "Rename" in the "This computer" header — renames this machine's device.
|
||||
pub(crate) rename_this: gtk4::Button,
|
||||
/// "Other computers" — the account's *other* registered devices. This
|
||||
/// machine's own device is deliberately not among them; see
|
||||
/// [`repaint_devices`].
|
||||
/// Every computer on the account, this one first.
|
||||
pub(crate) group: adw::PreferencesGroup,
|
||||
pub(crate) files: FileList,
|
||||
pub(crate) files_content: gtk4::Stack,
|
||||
/// Goes up one folder, and from a computer's top level back to the list.
|
||||
pub(crate) back: gtk4::Button,
|
||||
pub(crate) crumb: gtk4::Box,
|
||||
pub(crate) layout: gtk4::Button,
|
||||
pub(crate) retry: gtk4::Button,
|
||||
pub(crate) refresh: gtk4::Button,
|
||||
}
|
||||
|
||||
/// The Computers page: a "This computer" section naming the device this machine
|
||||
/// backs up to (renamable from its header), plus an "Other computers" section
|
||||
/// listing the account's other registered devices. The synced folders
|
||||
/// themselves live on the Sync page.
|
||||
/// The Computers page: every computer backing up to the account, with this one
|
||||
/// first. This computer's row leads to its folders on the Sync page; another
|
||||
/// computer's row opens its backup read-only, in the same list as My files.
|
||||
pub(crate) fn build_devices_page() -> (gtk4::Widget, DevicesWidgets) {
|
||||
let refresh = refresh_button();
|
||||
|
||||
// This page is about device *identity* — which computer this is, which other
|
||||
// computers back up to the account, and how to adopt one. The folders
|
||||
// themselves are local paths, so they live on the Sync page; keeping a
|
||||
// second copy of that list here would be two places to change the same mode.
|
||||
let sync_group = adw::PreferencesGroup::builder()
|
||||
.title(gettext("This computer"))
|
||||
.description(gettext("The computer this machine backs up as."))
|
||||
.build();
|
||||
// Rename control for *this* machine's device, in the section header. The
|
||||
// current device is filtered out of "Other computers", so this is the only
|
||||
// place it can be renamed; insensitive until the device list identifies it.
|
||||
let rename_this = gtk4::Button::builder()
|
||||
.icon_name("document-edit-symbolic")
|
||||
.tooltip_text(gettext("Rename this computer"))
|
||||
.valign(gtk4::Align::Center)
|
||||
.sensitive(false)
|
||||
.build();
|
||||
rename_this.add_css_class("flat");
|
||||
sync_group.set_header_suffix(Some(&rename_this));
|
||||
let group = adw::PreferencesGroup::builder()
|
||||
.title(gettext("Other computers"))
|
||||
.description(gettext("Other computers backing up to this account."))
|
||||
.build();
|
||||
|
||||
let groups = gtk4::Box::new(gtk4::Orientation::Vertical, 18);
|
||||
groups.append(&sync_group);
|
||||
groups.append(&group);
|
||||
let clamp = adw::Clamp::builder().child(&groups).build();
|
||||
let group = adw::PreferencesGroup::new();
|
||||
let clamp = adw::Clamp::builder().child(&group).build();
|
||||
let scroll = gtk4::ScrolledWindow::builder()
|
||||
.vexpand(true)
|
||||
.child(&clamp)
|
||||
.build();
|
||||
|
||||
// Browse: a path bar over the list, as on Shared with me.
|
||||
let files = FileList::new();
|
||||
files.show_list(true);
|
||||
let layout = layout_button(&files);
|
||||
layout.set_visible(false);
|
||||
let back = gtk4::Button::builder()
|
||||
.icon_name("go-previous-symbolic")
|
||||
.tooltip_text(gettext("Back"))
|
||||
.build();
|
||||
back.add_css_class("flat");
|
||||
let crumb = gtk4::Box::new(gtk4::Orientation::Horizontal, 2);
|
||||
crumb.set_valign(gtk4::Align::Center);
|
||||
let crumb_scroll = gtk4::ScrolledWindow::builder()
|
||||
.hscrollbar_policy(gtk4::PolicyType::External)
|
||||
.vscrollbar_policy(gtk4::PolicyType::Never)
|
||||
.hexpand(true)
|
||||
.child(&crumb)
|
||||
.build();
|
||||
let path_bar = gtk4::Box::new(gtk4::Orientation::Horizontal, 8);
|
||||
path_bar.append(&back);
|
||||
path_bar.append(&crumb_scroll);
|
||||
let files_empty = adw::StatusPage::builder()
|
||||
.icon_name("folder-open-symbolic")
|
||||
.title(gettext("This folder is empty"))
|
||||
.description(gettext("Nothing in this folder has been backed up."))
|
||||
.vexpand(true)
|
||||
.build();
|
||||
files_empty.add_css_class("compact");
|
||||
let files_content = gtk4::Stack::new();
|
||||
files_content.add_named(&files.views, Some("list"));
|
||||
files_content.add_named(&files_empty, Some("empty"));
|
||||
let browse = gtk4::Box::new(gtk4::Orientation::Vertical, 12);
|
||||
browse.append(&path_bar);
|
||||
browse.append(&files_content);
|
||||
|
||||
let retry = gtk4::Button::builder()
|
||||
.label(gettext("Retry"))
|
||||
.halign(gtk4::Align::Center)
|
||||
@@ -98,6 +128,7 @@ pub(crate) fn build_devices_page() -> (gtk4::Widget, DevicesWidgets) {
|
||||
content.set_vexpand(true);
|
||||
content.set_transition_type(gtk4::StackTransitionType::Crossfade);
|
||||
content.add_named(&scroll, Some("list"));
|
||||
content.add_named(&browse, Some("browse"));
|
||||
content.add_named(&status, Some("status"));
|
||||
|
||||
let inner = gtk4::Box::new(gtk4::Orientation::Vertical, 12);
|
||||
@@ -107,35 +138,66 @@ pub(crate) fn build_devices_page() -> (gtk4::Widget, DevicesWidgets) {
|
||||
inner.set_margin_end(18);
|
||||
inner.append(&content);
|
||||
let (frame, header, _) = page_frame(&gettext("Computers"), &inner);
|
||||
header.pack_end(&refresh);
|
||||
header.pack_end(&layout);
|
||||
|
||||
(
|
||||
frame.upcast(),
|
||||
DevicesWidgets {
|
||||
content,
|
||||
status,
|
||||
sync_group,
|
||||
rename_this,
|
||||
group,
|
||||
files,
|
||||
files_content,
|
||||
back,
|
||||
crumb,
|
||||
layout,
|
||||
retry,
|
||||
refresh,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Install the Devices page's retry button and the Rename action.
|
||||
pub(crate) fn wire_devices(ui: &Rc<Ui>, retry: >k4::Button) {
|
||||
/// Install the browse view's file list and the page's buttons.
|
||||
pub(crate) fn wire_devices(ui: &Rc<Ui>, widgets: &DevicesWidgets) {
|
||||
let files = &ui.devices.files;
|
||||
files.wire(
|
||||
ui,
|
||||
FileListBehavior {
|
||||
activate: open_device_entry,
|
||||
entry_menu: device_entry_menu,
|
||||
bulk_menu: device_bulk_menu,
|
||||
background_menu: device_background_menu,
|
||||
badges: false,
|
||||
drag_and_drop: false,
|
||||
},
|
||||
);
|
||||
files
|
||||
.column_view
|
||||
.append_column(&text_column(&pgettext("column", "Modified"), |e| {
|
||||
if e.modified > 0 {
|
||||
dates::short_date(e.modified)
|
||||
} else {
|
||||
"—".to_string()
|
||||
}
|
||||
}));
|
||||
files
|
||||
.column_view
|
||||
.append_column(&text_column(&pgettext("column", "Size"), |e| {
|
||||
if e.is_dir {
|
||||
"—".to_string()
|
||||
} else {
|
||||
human_bytes(e.size)
|
||||
}
|
||||
}));
|
||||
|
||||
let ui_back = ui.clone();
|
||||
widgets.back.connect_clicked(move |_| {
|
||||
ui_back.devices.nav.borrow_mut().pop();
|
||||
load_devices(&ui_back);
|
||||
});
|
||||
let ui_retry = ui.clone();
|
||||
retry.connect_clicked(move |_| {
|
||||
service::restart();
|
||||
load_devices(&ui_retry);
|
||||
});
|
||||
let ui_ren = ui.clone();
|
||||
ui.devices.rename_this.connect_clicked(move |_| {
|
||||
if let Some((uid, name)) = ui_ren.devices.this_device.borrow().clone() {
|
||||
prompt_rename_device(&ui_ren, &uid, &name);
|
||||
}
|
||||
});
|
||||
widgets
|
||||
.retry
|
||||
.connect_clicked(move |_| restart_service_then(&ui_retry, load_devices));
|
||||
}
|
||||
|
||||
/// Show a status page in place of the Devices list.
|
||||
@@ -147,21 +209,17 @@ pub(crate) fn devices_status(ui: &Rc<Ui>, icon: &str, title: &str, description:
|
||||
ui.devices.content.set_visible_child_name("status");
|
||||
}
|
||||
|
||||
/// Fetch this machine's synced folders and the account's other devices, then
|
||||
/// repaint both sections. The two requests are chained so a single unreachable
|
||||
/// daemon collapses the whole page to a status view.
|
||||
/// Load the view [`DevicesState::nav`] points at: the device list, or a folder
|
||||
/// of another computer's backup.
|
||||
pub(crate) fn load_devices(ui: &Rc<Ui>) {
|
||||
if ui.devices.inflight.get() {
|
||||
cancel_file_thumbnails(ui);
|
||||
let nav = ui.devices.nav.borrow().clone();
|
||||
ui.devices.layout.set_visible(!nav.is_empty());
|
||||
if !nav.is_empty() {
|
||||
load_device_folder(ui, &nav);
|
||||
return;
|
||||
}
|
||||
ui.devices.inflight.set(true);
|
||||
devices_status(
|
||||
ui,
|
||||
"computer-symbolic",
|
||||
&gettext("Loading…"),
|
||||
&gettext("Reading your computers."),
|
||||
false,
|
||||
);
|
||||
let ticket = begin_devices_load(ui, "computer-symbolic", gettext("Reading your computers."));
|
||||
ui.busy_begin();
|
||||
// The two lists are independent and the daemon serves requests concurrently,
|
||||
// so fire both up front and collect them, rather than paying two round trips
|
||||
@@ -172,213 +230,361 @@ pub(crate) fn load_devices(ui: &Rc<Ui>) {
|
||||
glib::spawn_future_local(async move {
|
||||
let sync = rx.recv().await;
|
||||
let devices_reply = rx2.recv().await;
|
||||
ui.busy_end();
|
||||
if !ticket.is_current() {
|
||||
return;
|
||||
}
|
||||
let folders = match sync {
|
||||
Ok(Ok(Response::SyncFolders { items })) => items,
|
||||
// The daemon answered but not as expected — treat as empty and carry
|
||||
// on to the devices list rather than failing the whole page.
|
||||
Ok(Ok(_)) => Vec::new(),
|
||||
Ok(Err(_)) | Err(_) => {
|
||||
ui.busy_end();
|
||||
ui.devices.inflight.set(false);
|
||||
ui.devices.loaded_at.set(None);
|
||||
devices_unreachable(&ui);
|
||||
return;
|
||||
}
|
||||
};
|
||||
// Daemon reachable: show the list and paint the "This computer" section.
|
||||
ui.devices.content.set_visible_child_name("list");
|
||||
repaint_this_computer(&ui, &folders);
|
||||
|
||||
let devices = match devices_reply {
|
||||
Ok(Ok(Response::Devices { items })) => items,
|
||||
_ => Vec::new(),
|
||||
};
|
||||
ui.busy_end();
|
||||
ui.devices.inflight.set(false);
|
||||
repaint_devices(&ui, &devices);
|
||||
ui.devices.loaded_at.set(Some(Instant::now()));
|
||||
ui.devices.content.set_visible_child_name("list");
|
||||
repaint_devices(&ui, &devices, &folders);
|
||||
repaint_device_crumb(&ui, &[]);
|
||||
*ui.devices.listed.borrow_mut() = Some(Vec::new());
|
||||
});
|
||||
}
|
||||
|
||||
/// List one folder of another computer's backup. The computer's own top level
|
||||
/// is its restorable folders; below that, any folder lists by uid.
|
||||
fn load_device_folder(ui: &Rc<Ui>, nav: &[(String, String)]) {
|
||||
let ticket = begin_devices_load(ui, "folder-symbolic", gettext("Reading this folder."));
|
||||
ui.busy_begin();
|
||||
let (uid, _) = nav.last().cloned().unwrap_or_default();
|
||||
let request = if nav.len() == 1 {
|
||||
Request::ListDeviceRestorableFolders { device: uid }
|
||||
} else {
|
||||
Request::ListSharedFolder { uid }
|
||||
};
|
||||
let rx = spawn_request(ui.dirs.control_socket(), request);
|
||||
let ui = ui.clone();
|
||||
let nav = nav.to_vec();
|
||||
glib::spawn_future_local(async move {
|
||||
let result = rx.recv().await;
|
||||
ui.busy_end();
|
||||
if !ticket.is_current() {
|
||||
return;
|
||||
}
|
||||
let entries = match result {
|
||||
Ok(Ok(Response::Entries { entries })) => entries,
|
||||
Ok(Ok(Response::RestorableFolders { items })) => {
|
||||
items.into_iter().map(restorable_entry).collect()
|
||||
}
|
||||
Ok(Ok(Response::Error { message, kind })) => {
|
||||
// The folder or the computer is gone: fall back a level rather
|
||||
// than stranding the page on a dead uid.
|
||||
ui.devices.nav.borrow_mut().pop();
|
||||
toast_failure(&ui, &gettext("Couldn't open folder"), &message, kind);
|
||||
load_devices(&ui);
|
||||
return;
|
||||
}
|
||||
_ => {
|
||||
devices_unreachable(&ui);
|
||||
return;
|
||||
}
|
||||
};
|
||||
ui.devices.content.set_visible_child_name("browse");
|
||||
repaint_device_crumb(&ui, &nav);
|
||||
*ui.devices.listed.borrow_mut() = Some(nav);
|
||||
if entries.is_empty() {
|
||||
ui.devices.files.model.remove_all();
|
||||
ui.devices.files_content.set_visible_child_name("empty");
|
||||
} else {
|
||||
ui.devices.files_content.set_visible_child_name("list");
|
||||
replace_items(&ui.devices.files.model, &entries);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// A computer's top-level folder as a browsable entry.
|
||||
fn restorable_entry(folder: RestorableFolder) -> DirEntry {
|
||||
DirEntry {
|
||||
name: folder.name,
|
||||
is_dir: true,
|
||||
uid: folder.remote_uid,
|
||||
..DirEntry::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Start a load of the view [`DevicesState::nav`] points at. A reload of the
|
||||
/// view on screen keeps it up and usable; moving elsewhere greys it out until
|
||||
/// the new view arrives.
|
||||
fn begin_devices_load(ui: &Rc<Ui>, icon: &'static str, description: String) -> LoadTicket {
|
||||
let ui_p = ui.clone();
|
||||
let placeholder = move || {
|
||||
devices_status(&ui_p, icon, &gettext("Loading…"), &description, false);
|
||||
};
|
||||
if *ui.devices.listed.borrow() == Some(ui.devices.nav.borrow().clone()) {
|
||||
ui.devices.loader.refresh(placeholder)
|
||||
} else {
|
||||
ui.devices.listed.borrow_mut().take();
|
||||
ui.devices.loader.replace(placeholder)
|
||||
}
|
||||
}
|
||||
|
||||
/// The daemon didn't answer the Devices page.
|
||||
pub(crate) fn devices_unreachable(ui: &Rc<Ui>) {
|
||||
if service::is_failed() || !service::is_active() {
|
||||
devices_status(
|
||||
ui,
|
||||
"network-offline-symbolic",
|
||||
&gettext("Not connected"),
|
||||
&gettext("The Proton Drive mount service isn't running."),
|
||||
true,
|
||||
);
|
||||
return;
|
||||
}
|
||||
devices_status(
|
||||
ui,
|
||||
"folder-remote-symbolic",
|
||||
&gettext("Connecting…"),
|
||||
&gettext("Waiting for the Proton Drive mount service to come up."),
|
||||
false,
|
||||
);
|
||||
let ui = ui.clone();
|
||||
glib::timeout_add_local_once(CONNECT_RETRY_INTERVAL, move || {
|
||||
if ui.stack.visible_child_name().as_deref() == Some("devices") {
|
||||
load_devices(&ui);
|
||||
}
|
||||
});
|
||||
service_unreachable(ui, "devices", devices_status, load_devices);
|
||||
}
|
||||
|
||||
/// Rebuild the "Other computers" section from a fresh listing. Empty is a normal
|
||||
/// state (this machine may be the only device), so it shows a placeholder row
|
||||
/// rather than collapsing the page.
|
||||
/// Rebuild the device list: this computer first, then the others.
|
||||
///
|
||||
/// This machine's own device is filtered out. Deleting a device deletes its root
|
||||
/// folder — every file backed up from it — and for *this* machine that would also
|
||||
/// pull the ground out from under the synced folders listed directly above, so it
|
||||
/// is not offered as a peer of "remove some other laptop". Removing this
|
||||
/// computer's backup means removing its folders, each of which asks about its
|
||||
/// cloud copy on its own terms.
|
||||
pub(crate) fn repaint_devices(ui: &Rc<Ui>, devices: &[DeviceInfo]) {
|
||||
/// This computer's row offers no Remove. Deleting a device deletes its root
|
||||
/// folder — every file backed up from it — and for this machine that would also
|
||||
/// pull the ground out from under its synced folders, so it is not offered as a
|
||||
/// peer of "remove some other laptop". Removing this computer's backup means
|
||||
/// removing its folders, each of which asks about its cloud copy on its own
|
||||
/// terms.
|
||||
pub(crate) fn repaint_devices(ui: &Rc<Ui>, devices: &[DeviceInfo], folders: &[SyncFolderInfo]) {
|
||||
for row in ui.devices.rows.borrow_mut().drain(..) {
|
||||
ui.devices.group.remove(&row);
|
||||
}
|
||||
// Identify this machine's own device so the "This computer" header's Rename
|
||||
// action has a target. It never appears in the rows below, so this is the
|
||||
// only place its name/uid is captured.
|
||||
match devices.iter().find(|d| d.this_device) {
|
||||
Some(me) => {
|
||||
*ui.devices.this_device.borrow_mut() = Some((me.uid.clone(), me.name.clone()));
|
||||
ui.devices
|
||||
.sync_group
|
||||
// Translators: {name} is the name of this computer's device.
|
||||
.set_description(Some(&gettext_f(
|
||||
"Backing up as “{name}”.",
|
||||
&[("name", &me.name)],
|
||||
)));
|
||||
ui.devices.rename_this.set_sensitive(true);
|
||||
ui.devices
|
||||
.rename_this
|
||||
// Translators: {name} is the name of this computer's device.
|
||||
.set_tooltip_text(Some(&gettext_f(
|
||||
"Rename this computer ({name})",
|
||||
&[("name", &me.name)],
|
||||
)));
|
||||
}
|
||||
None => {
|
||||
*ui.devices.this_device.borrow_mut() = None;
|
||||
ui.devices.rename_this.set_sensitive(false);
|
||||
}
|
||||
}
|
||||
let mut rows: Vec<gtk4::Widget> = Vec::new();
|
||||
let me = devices.iter().find(|d| d.this_device);
|
||||
rows.push(this_computer_row(ui, me, folders).upcast());
|
||||
let others: Vec<&DeviceInfo> = devices.iter().filter(|d| !d.this_device).collect();
|
||||
for dev in &others {
|
||||
rows.push(other_computer_row(ui, dev).upcast());
|
||||
}
|
||||
if others.is_empty() {
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(gettext("No other computers"))
|
||||
.subtitle(gettext("Desktop apps syncing to this account appear here."))
|
||||
.subtitle(gettext(
|
||||
"Install Proton Drive on another computer to see it here.",
|
||||
))
|
||||
.build();
|
||||
row.add_prefix(>k4::Image::from_icon_name("computer-symbolic"));
|
||||
ui.devices.group.add(&row);
|
||||
*ui.devices.rows.borrow_mut() = vec![row.upcast()];
|
||||
return;
|
||||
}
|
||||
let mut rows: Vec<gtk4::Widget> = Vec::new();
|
||||
for dev in others {
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(&dev.name)
|
||||
.subtitle(device_subtitle(dev))
|
||||
.build();
|
||||
row.add_prefix(>k4::Image::from_icon_name("computer-symbolic"));
|
||||
// Adoption is how a reinstalled or renamed machine re-attaches to the
|
||||
// device it used to be, instead of registering a duplicate. It only
|
||||
// makes sense on *another* computer's row, which is the only place this
|
||||
// loop paints.
|
||||
let (ui_ren, uid_ren, name_ren) = (ui.clone(), dev.uid.clone(), dev.name.clone());
|
||||
let (ui_ad, uid_ad, name_ad) = (ui.clone(), dev.uid.clone(), dev.name.clone());
|
||||
let (ui_rm, uid_rm, name_rm) = (ui.clone(), dev.uid.clone(), dev.name.clone());
|
||||
let (ui_rs, uid_rs, name_rs) = (ui.clone(), dev.uid.clone(), dev.name.clone());
|
||||
row.add_suffix(&more_menu_button(vec![
|
||||
(
|
||||
&gettext("Restore to This Computer…"),
|
||||
Box::new(move || {
|
||||
prompt_restore_folders(&ui_rs, Some((uid_rs.clone(), name_rs.clone())))
|
||||
}),
|
||||
),
|
||||
(
|
||||
&gettext("Rename…"),
|
||||
Box::new(move || prompt_rename_device(&ui_ren, &uid_ren, &name_ren)),
|
||||
),
|
||||
(
|
||||
&gettext("Use This Computer's Identity…"),
|
||||
Box::new(move || prompt_adopt_device(&ui_ad, &uid_ad, &name_ad)),
|
||||
),
|
||||
(
|
||||
&gettext("Remove Computer…"),
|
||||
Box::new(move || prompt_remove_device(&ui_rm, &uid_rm, &name_rm)),
|
||||
),
|
||||
]));
|
||||
ui.devices.group.add(&row);
|
||||
let get = gtk4::LinkButton::with_label(PROTON_DRIVE_DOWNLOAD, &gettext("Get Proton Drive"));
|
||||
get.set_valign(gtk4::Align::Center);
|
||||
row.add_suffix(&get);
|
||||
rows.push(row.upcast());
|
||||
}
|
||||
for row in &rows {
|
||||
ui.devices.group.add(row);
|
||||
}
|
||||
*ui.devices.rows.borrow_mut() = rows;
|
||||
}
|
||||
|
||||
/// A device row's subtitle: its platform, and when it last synced. A device that
|
||||
/// has never synced says so — an unexplained missing date reads as a bug, and
|
||||
/// "never" is the fact that tells the user this computer isn't backing anything up.
|
||||
pub(crate) fn device_subtitle(dev: &DeviceInfo) -> String {
|
||||
match dev.last_sync {
|
||||
// Translators: {platform} is the device's platform (such as "Linux"), {time} a date and time.
|
||||
Some(secs) if secs > 0 => gettext_f(
|
||||
"{platform} · last synced {time}",
|
||||
&[
|
||||
("platform", &dev.device_type),
|
||||
("time", &activity_time(secs)),
|
||||
],
|
||||
),
|
||||
// Translators: {platform} is the device's platform, such as "Linux".
|
||||
_ => gettext_f(
|
||||
"{platform} · never synced",
|
||||
&[("platform", &dev.device_type)],
|
||||
),
|
||||
/// Where the Proton Drive apps for other systems are.
|
||||
const PROTON_DRIVE_DOWNLOAD: &str = "https://proton.me/drive/download";
|
||||
|
||||
/// This computer's row: its device name with a "This computer" pill, and how
|
||||
/// many folders it backs up. It leads to those folders on the Sync page, which
|
||||
/// owns every local path. `me` is `None` until the daemon has registered this
|
||||
/// machine as a device.
|
||||
fn this_computer_row(
|
||||
ui: &Rc<Ui>,
|
||||
me: Option<&DeviceInfo>,
|
||||
folders: &[SyncFolderInfo],
|
||||
) -> adw::ActionRow {
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(me.map_or_else(|| gettext("This computer"), |d| d.name.clone()))
|
||||
.subtitle(this_computer_subtitle(folders))
|
||||
.activatable(true)
|
||||
.build();
|
||||
row.add_prefix(>k4::Image::from_icon_name("computer-symbolic"));
|
||||
if me.is_some() {
|
||||
let pill = gtk4::Label::new(Some(&gettext("This computer")));
|
||||
pill.add_css_class("role-pill");
|
||||
pill.add_css_class("caption");
|
||||
pill.set_valign(gtk4::Align::Center);
|
||||
row.add_suffix(&pill);
|
||||
}
|
||||
let mut menu = ActionMenu::new();
|
||||
if let Some(me) = me {
|
||||
let (ui_c, uid, name) = (ui.clone(), me.uid.clone(), me.name.clone());
|
||||
menu.item(&gettext("Rename…"), move || {
|
||||
prompt_rename_device(&ui_c, &uid, &name)
|
||||
});
|
||||
}
|
||||
let ui_c = ui.clone();
|
||||
menu.item(&gettext("Restore Folders…"), move || {
|
||||
prompt_restore_folders(&ui_c, None)
|
||||
});
|
||||
row.add_suffix(&menu.button());
|
||||
row.add_suffix(>k4::Image::from_icon_name("go-next-symbolic"));
|
||||
let ui_c = ui.clone();
|
||||
row.connect_activated(move |_| {
|
||||
ui_c.locations.views.set_visible_child_name("folders");
|
||||
ui_c.stack.set_visible_child_name("locations");
|
||||
});
|
||||
row
|
||||
}
|
||||
|
||||
/// Another computer's row. Opening it browses that computer's backup.
|
||||
fn other_computer_row(ui: &Rc<Ui>, dev: &DeviceInfo) -> adw::ActionRow {
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(&dev.name)
|
||||
.subtitle(device_subtitle(dev))
|
||||
.activatable(true)
|
||||
.build();
|
||||
row.add_prefix(>k4::Image::from_icon_name("computer-symbolic"));
|
||||
let (uid, name) = (dev.uid.clone(), dev.name.clone());
|
||||
let mut menu = ActionMenu::new();
|
||||
let (ui_c, uid_c, name_c) = (ui.clone(), uid.clone(), name.clone());
|
||||
menu.item(&gettext("Browse Files"), move || {
|
||||
browse_device(&ui_c, &uid_c, &name_c)
|
||||
});
|
||||
let (ui_c, uid_c, name_c) = (ui.clone(), uid.clone(), name.clone());
|
||||
menu.item(&gettext("Restore to This Computer…"), move || {
|
||||
prompt_restore_folders(&ui_c, Some((uid_c.clone(), name_c.clone())))
|
||||
});
|
||||
menu.section();
|
||||
let (ui_c, uid_c, name_c) = (ui.clone(), uid.clone(), name.clone());
|
||||
menu.item(&gettext("Rename…"), move || {
|
||||
prompt_rename_device(&ui_c, &uid_c, &name_c)
|
||||
});
|
||||
// Adoption is how a reinstalled or renamed machine re-attaches to the device
|
||||
// it used to be, instead of registering a duplicate. It only makes sense on
|
||||
// another computer's row.
|
||||
let (ui_c, uid_c, name_c) = (ui.clone(), uid.clone(), name.clone());
|
||||
menu.item(&gettext("Continue This Backup Here…"), move || {
|
||||
prompt_adopt_device(&ui_c, &uid_c, &name_c)
|
||||
});
|
||||
menu.section();
|
||||
let (ui_c, uid_c, name_c) = (ui.clone(), uid.clone(), name.clone());
|
||||
menu.item(&gettext("Remove…"), move || {
|
||||
prompt_remove_device(&ui_c, &uid_c, &name_c)
|
||||
});
|
||||
row.add_suffix(&menu.button());
|
||||
row.add_suffix(>k4::Image::from_icon_name("go-next-symbolic"));
|
||||
let ui_c = ui.clone();
|
||||
row.connect_activated(move |_| browse_device(&ui_c, &uid, &name));
|
||||
row
|
||||
}
|
||||
|
||||
/// Open another computer's backup at its top level.
|
||||
fn browse_device(ui: &Rc<Ui>, uid: &str, name: &str) {
|
||||
*ui.devices.nav.borrow_mut() = vec![(uid.to_string(), name.to_string())];
|
||||
load_devices(ui);
|
||||
}
|
||||
|
||||
/// Rebuild the breadcrumb trail for `nav`: "Computers", then the computer and
|
||||
/// each folder down to the current one, which is a plain heading.
|
||||
fn repaint_device_crumb(ui: &Rc<Ui>, nav: &[(String, String)]) {
|
||||
let crumb = &ui.devices.crumb;
|
||||
while let Some(child) = crumb.first_child() {
|
||||
crumb.remove(&child);
|
||||
}
|
||||
crumb.append(&device_crumb_node(
|
||||
ui,
|
||||
&gettext("Computers"),
|
||||
0,
|
||||
nav.is_empty(),
|
||||
));
|
||||
for (i, (_, name)) in nav.iter().enumerate() {
|
||||
let sep = gtk4::Label::new(Some("›"));
|
||||
sep.add_css_class("dim-label");
|
||||
crumb.append(&sep);
|
||||
crumb.append(&device_crumb_node(ui, name, i + 1, i + 1 == nav.len()));
|
||||
}
|
||||
}
|
||||
|
||||
/// Rebuild the "This computer" section: one row naming the device this machine
|
||||
/// backs up to, and how many folders it carries.
|
||||
///
|
||||
/// The per-folder rows (mode switch, sync now, remove) live on the Locations
|
||||
/// page. They are local paths, and Locations is the one page that owns those;
|
||||
/// two lists of the same folders would be two places to flip the same switch.
|
||||
pub(crate) fn repaint_this_computer(ui: &Rc<Ui>, folders: &[SyncFolderInfo]) {
|
||||
for row in ui.devices.sync_rows.borrow_mut().drain(..) {
|
||||
ui.devices.sync_group.remove(&row);
|
||||
/// One breadcrumb segment: a heading for the current folder, or a flat button
|
||||
/// that goes back up to `depth` levels below the device list.
|
||||
fn device_crumb_node(ui: &Rc<Ui>, label: &str, depth: usize, current: bool) -> gtk4::Widget {
|
||||
if current {
|
||||
let l = gtk4::Label::builder()
|
||||
.label(label)
|
||||
.ellipsize(gtk4::pango::EllipsizeMode::Start)
|
||||
.build();
|
||||
l.add_css_class("heading");
|
||||
return l.upcast();
|
||||
}
|
||||
let button = gtk4::Button::builder().label(label).build();
|
||||
button.add_css_class("flat");
|
||||
let ui = ui.clone();
|
||||
button.connect_clicked(move |_| {
|
||||
ui.devices.nav.borrow_mut().truncate(depth);
|
||||
load_devices(&ui);
|
||||
});
|
||||
button.upcast()
|
||||
}
|
||||
|
||||
/// Folders open in place; files download and open with the default app.
|
||||
fn open_device_entry(ui: &Rc<Ui>, entry: &DirEntry) {
|
||||
if entry.is_dir {
|
||||
ui.devices
|
||||
.nav
|
||||
.borrow_mut()
|
||||
.push((entry.uid.clone(), entry.name.clone()));
|
||||
load_devices(ui);
|
||||
} else {
|
||||
open_shared_file(ui, &entry.uid, &entry.name);
|
||||
}
|
||||
}
|
||||
|
||||
fn device_entry_menu(ui: &Rc<Ui>, entry: &DirEntry) -> ActionMenu {
|
||||
let mut menu = ActionMenu::new();
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
menu.item(&pgettext("verb", "Open"), move || {
|
||||
open_device_entry(&ui_c, &entry_c)
|
||||
});
|
||||
menu
|
||||
}
|
||||
|
||||
fn device_bulk_menu(_ui: &Rc<Ui>, entries: Vec<DirEntry>) -> ActionMenu {
|
||||
let mut menu = ActionMenu::new();
|
||||
menu.labelled_section(&ngettext_f(
|
||||
"{n} selected",
|
||||
"{n} selected",
|
||||
entries.len() as u64,
|
||||
&[],
|
||||
));
|
||||
menu
|
||||
}
|
||||
|
||||
fn device_background_menu(ui: &Rc<Ui>) -> ActionMenu {
|
||||
let mut menu = ActionMenu::new();
|
||||
let ui_c = ui.clone();
|
||||
menu.item(&gettext("Select All"), move || {
|
||||
ui_c.devices.files.selection.select_all();
|
||||
});
|
||||
let ui_c = ui.clone();
|
||||
menu.item(&gettext("Refresh"), move || reload_current_page(&ui_c));
|
||||
menu
|
||||
}
|
||||
|
||||
/// A device row's subtitle: its platform, and when it last backed up. A device
|
||||
/// that never did says so — an unexplained missing date reads as a bug, and
|
||||
/// "no backups yet" is the fact that tells the user this computer isn't backing
|
||||
/// anything up.
|
||||
pub(crate) fn device_subtitle(dev: &DeviceInfo) -> String {
|
||||
let platform = platform_label(&dev.device_type);
|
||||
match dev.last_sync {
|
||||
// Translators: {platform} is the device's platform (such as "Linux"), {time} a relative time such as "5 minutes ago".
|
||||
Some(secs) if secs > 0 => gettext_f(
|
||||
"{platform} · last backup {time}",
|
||||
&[("platform", platform), ("time", &dates::relative(secs))],
|
||||
),
|
||||
// Translators: {platform} is the device's platform, such as "Linux".
|
||||
_ => gettext_f("{platform} · no backups yet", &[("platform", platform)]),
|
||||
}
|
||||
}
|
||||
|
||||
/// The platform as its maker spells it. The API's names are enum variants.
|
||||
fn platform_label(device_type: &str) -> &str {
|
||||
match device_type {
|
||||
"MacOs" => "macOS",
|
||||
other => other,
|
||||
}
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(gettext("This computer"))
|
||||
.subtitle(this_computer_subtitle(folders))
|
||||
.build();
|
||||
row.add_prefix(>k4::Image::from_icon_name("computer-symbolic"));
|
||||
let manage = gtk4::Button::builder()
|
||||
.label(gettext("Open Sync"))
|
||||
.tooltip_text(gettext("Manage this computer's folders and mountpoint"))
|
||||
.valign(gtk4::Align::Center)
|
||||
.build();
|
||||
manage.add_css_class("flat");
|
||||
let ui_go = ui.clone();
|
||||
manage.connect_clicked(move |_| ui_go.stack.set_visible_child_name("locations"));
|
||||
row.add_suffix(&manage);
|
||||
let ui_rs = ui.clone();
|
||||
row.add_suffix(&more_menu_button(vec![(
|
||||
&gettext("Restore Folders…"),
|
||||
Box::new(move || prompt_restore_folders(&ui_rs, None)),
|
||||
)]));
|
||||
ui.devices.sync_group.add(&row);
|
||||
*ui.devices.sync_rows.borrow_mut() = vec![row.upcast()];
|
||||
}
|
||||
|
||||
/// How many folders this machine backs up, and whether any of them needs
|
||||
/// attention — the one fact worth surfacing away from the folder list itself.
|
||||
pub(crate) fn this_computer_subtitle(folders: &[SyncFolderInfo]) -> String {
|
||||
if folders.is_empty() {
|
||||
return gettext("No folders backed up yet — add one on the Sync page.");
|
||||
return gettext("No folders backed up yet");
|
||||
}
|
||||
let count = ngettext_f(
|
||||
"{n} folder backed up",
|
||||
@@ -391,11 +597,7 @@ pub(crate) fn this_computer_subtitle(folders: &[SyncFolderInfo]) -> String {
|
||||
.filter(|f| f.state == "error" || f.state == "conflict")
|
||||
.count();
|
||||
match attention {
|
||||
// Translators: {folders} is "N folders backed up".
|
||||
0 => gettext_f(
|
||||
"{folders} · manage them on the Sync page",
|
||||
&[("folders", &count)],
|
||||
),
|
||||
0 => count,
|
||||
// Translators: {folders} is "N folders backed up"; {n} counts the folders with a problem.
|
||||
n => ngettext_f(
|
||||
"{folders} · {n} needs attention",
|
||||
@@ -489,7 +691,7 @@ pub(crate) fn prompt_add_sync_folder(ui: &Rc<Ui>) {
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&gettext("Couldn't add folder"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -529,7 +731,7 @@ pub(crate) fn prompt_restore_folders(ui: &Rc<Ui>, device: Option<(String, String
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&gettext("Couldn't list folders to restore"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -577,7 +779,7 @@ fn show_restore_picker(
|
||||
.child(&group)
|
||||
.build();
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Restore folders"))
|
||||
.heading(gettext("Restore Folders"))
|
||||
.body(restore_picker_body(
|
||||
device.as_ref().map(|(_, n)| n.as_str()),
|
||||
))
|
||||
@@ -668,7 +870,7 @@ pub(crate) fn set_sync_folder_mode(ui: &Rc<Ui>, id: i64, mode: &'static str) {
|
||||
Ok(Ok(Response::Ok { .. })) => toast(
|
||||
&ui,
|
||||
&if mode == "ondemand" {
|
||||
gettext("Folder will switch to on-demand")
|
||||
gettext("Folder will switch to online only")
|
||||
} else {
|
||||
gettext("Folder will download and stay synced")
|
||||
},
|
||||
@@ -679,7 +881,7 @@ pub(crate) fn set_sync_folder_mode(ui: &Rc<Ui>, id: i64, mode: &'static str) {
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&gettext("Couldn't change mode"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
reload_sync_pages(&ui);
|
||||
@@ -710,7 +912,7 @@ pub(crate) fn prompt_remove_sync_folder(ui: &Rc<Ui>, id: i64, path: &str, ondema
|
||||
let body = if ondemand {
|
||||
// Translators: {path} is a local folder path.
|
||||
gettext_f(
|
||||
"Stop syncing “{path}”?\n\nThis folder is on-demand: its files live in Proton Drive, not on this disk, so the folder will be empty once it is unmounted. To keep a local copy, cancel, turn off On-demand, and wait for the download to finish before removing it.",
|
||||
"Stop syncing “{path}”?\n\nThis folder is online only: its files live in Proton Drive, not on this disk, so the folder will be empty once it stops syncing. To keep a local copy, cancel, switch the folder to Synced, and wait for the download to finish before removing it.",
|
||||
&[("path", path)],
|
||||
)
|
||||
} else {
|
||||
@@ -721,7 +923,7 @@ pub(crate) fn prompt_remove_sync_folder(ui: &Rc<Ui>, id: i64, path: &str, ondema
|
||||
)
|
||||
};
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Stop syncing folder"))
|
||||
.heading(gettext("Stop Syncing Folder?"))
|
||||
.body(body)
|
||||
.build();
|
||||
let group = adw::PreferencesGroup::new();
|
||||
@@ -818,7 +1020,7 @@ pub(crate) fn prompt_remove_device(ui: &Rc<Ui>, uid: &str, name: &str) {
|
||||
let group = adw::PreferencesGroup::new();
|
||||
group.add(&confirm);
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Remove Computer"))
|
||||
.heading(gettext("Remove Computer?"))
|
||||
// Translators: {name} is the name of the computer being removed.
|
||||
.body(gettext_f(
|
||||
"Remove “{name}” from this account?\n\nEverything it backed up to Proton Drive is deleted along with it. The files on that computer itself are not touched — but this cannot be undone from here.",
|
||||
@@ -852,7 +1054,8 @@ pub(crate) fn prompt_remove_device(ui: &Rc<Ui>, uid: &str, name: &str) {
|
||||
dialog.present(win.as_ref());
|
||||
}
|
||||
|
||||
/// Confirm, then adopt another device as this machine's identity.
|
||||
/// Confirm, then continue another computer's backup on this machine (adopt its
|
||||
/// device).
|
||||
///
|
||||
/// Worth a confirmation rather than a plain click: adoption re-points this
|
||||
/// machine's syncing at another computer's device folder, and the folders
|
||||
@@ -862,20 +1065,22 @@ pub(crate) fn prompt_remove_device(ui: &Rc<Ui>, uid: &str, name: &str) {
|
||||
pub(crate) fn prompt_adopt_device(ui: &Rc<Ui>, uid: &str, name: &str) {
|
||||
let win = ui_window(ui);
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Use this computer's identity"))
|
||||
// Translators: {name} is the name of the other computer whose identity is adopted.
|
||||
.heading(gettext("Continue This Backup Here?"))
|
||||
// Translators: {name} is the name of the other computer whose backup this one takes over.
|
||||
.body(gettext_f(
|
||||
"Treat this machine as “{name}”?\n\nNew synced folders are created under that computer in Proton Drive, and this machine keeps that identity even if its hostname changes. Folders already synced here are not moved.\n\nUse “Restore folders” afterwards to bring back what “{name}” was syncing.",
|
||||
"This computer takes over the backup of “{name}”, for example after a reinstall.\n\nNew synced folders go under “{name}” in Proton Drive, even if this computer's name changes. Folders already synced here are not moved. Use “Restore to This Computer” afterwards to bring back what “{name}” was syncing.",
|
||||
&[("name", name)],
|
||||
))
|
||||
.build();
|
||||
dialog.add_response("cancel", &gettext("Cancel"));
|
||||
dialog.add_response("adopt", &gettext("Use identity"));
|
||||
dialog.add_response("adopt", &gettext("Continue Here"));
|
||||
dialog.set_response_appearance("adopt", adw::ResponseAppearance::Suggested);
|
||||
dialog.set_default_response(Some("cancel"));
|
||||
dialog.set_close_response("cancel");
|
||||
let ui = ui.clone();
|
||||
let uid = uid.to_string();
|
||||
// Translators: {name} is the name of the computer whose backup this one took over.
|
||||
let done = gettext_f("Now backing up as “{name}”", &[("name", name)]);
|
||||
dialog.connect_response(None, move |_, resp| {
|
||||
if resp == "adopt" {
|
||||
run_devices_mutation(
|
||||
@@ -883,8 +1088,8 @@ pub(crate) fn prompt_adopt_device(ui: &Rc<Ui>, uid: &str, name: &str) {
|
||||
Request::AdoptDevice {
|
||||
uid: Some(uid.clone()),
|
||||
},
|
||||
&gettext("Identity adopted"),
|
||||
&gettext("Couldn't adopt that computer"),
|
||||
&done,
|
||||
&gettext("Couldn't continue that backup here"),
|
||||
);
|
||||
}
|
||||
});
|
||||
@@ -909,7 +1114,11 @@ pub(crate) fn run_devices_mutation(ui: &Rc<Ui>, req: Request, done: &str, failed
|
||||
Ok(Ok(Response::Error { message, kind })) => {
|
||||
toast_failure(&ui, &failed, &message, kind)
|
||||
}
|
||||
_ => toast_error(&ui, &failed, &gettext("The mount service didn't respond.")),
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&failed,
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -933,20 +1142,17 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_machine_with_no_folders_is_told_where_to_add_one() {
|
||||
assert_eq!(
|
||||
this_computer_subtitle(&[]),
|
||||
"No folders backed up yet — add one on the Sync page."
|
||||
);
|
||||
fn a_machine_with_no_folders_says_so() {
|
||||
assert_eq!(this_computer_subtitle(&[]), "No folders backed up yet");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn folders_needing_attention_are_counted_not_hidden() {
|
||||
// The per-folder rows moved to Locations, so this row is the only place
|
||||
// the Computers page can surface that something is wrong.
|
||||
// The per-folder rows live on the Sync page, so this row is the only
|
||||
// place the Computers page can surface that something is wrong.
|
||||
assert_eq!(
|
||||
this_computer_subtitle(&[folder("idle"), folder("idle")]),
|
||||
"2 folders backed up · manage them on the Sync page"
|
||||
"2 folders backed up"
|
||||
);
|
||||
assert_eq!(
|
||||
this_computer_subtitle(&[folder("idle"), folder("error")]),
|
||||
@@ -964,4 +1170,28 @@ mod tests {
|
||||
assert!(restore_picker_body(None).contains("this computer"));
|
||||
assert!(nothing_to_restore(Some("laptop")).contains("“laptop”"));
|
||||
}
|
||||
|
||||
fn device(device_type: &str, last_sync: Option<i64>) -> DeviceInfo {
|
||||
DeviceInfo {
|
||||
uid: "dev".into(),
|
||||
name: "laptop".into(),
|
||||
device_type: device_type.into(),
|
||||
last_sync,
|
||||
this_device: false,
|
||||
adopted: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_computer_that_never_backed_up_says_no_backups_yet() {
|
||||
assert_eq!(
|
||||
device_subtitle(&device("Linux", None)),
|
||||
"Linux · no backups yet"
|
||||
);
|
||||
assert_eq!(
|
||||
device_subtitle(&device("Linux", Some(0))),
|
||||
"Linux · no backups yet"
|
||||
);
|
||||
assert!(device_subtitle(&device("MacOs", Some(1))).starts_with("macOS · last backup "));
|
||||
}
|
||||
}
|
||||
@@ -75,22 +75,19 @@ pub(crate) fn show_duplicates(ui: &Rc<Ui>) {
|
||||
bottom.set_halign(gtk4::Align::End);
|
||||
bottom.append(&trash);
|
||||
|
||||
let exact = gtk4::ToggleButton::builder()
|
||||
.label(pgettext("duplicates", "Exact"))
|
||||
.tooltip_text(gettext("Photos stored more than once"))
|
||||
.active(true)
|
||||
.build();
|
||||
let similar = gtk4::ToggleButton::builder()
|
||||
.label(pgettext("duplicates", "Similar"))
|
||||
.tooltip_text(gettext("Photos that look alike"))
|
||||
.group(&exact)
|
||||
.build();
|
||||
let switch = gtk4::Box::new(gtk4::Orientation::Horizontal, 0);
|
||||
switch.add_css_class("linked");
|
||||
switch.append(&exact);
|
||||
switch.append(&similar);
|
||||
// Exact first, then Similar: the index is the finder.
|
||||
let switch = ToggleGroup::new(&[
|
||||
(
|
||||
&pgettext("duplicates", "Exact"),
|
||||
Some(&gettext("Photos stored more than once")),
|
||||
),
|
||||
(
|
||||
&pgettext("duplicates", "Similar"),
|
||||
Some(&gettext("Photos that look alike")),
|
||||
),
|
||||
]);
|
||||
let header = adw::HeaderBar::new();
|
||||
header.set_title_widget(Some(&switch));
|
||||
header.set_title_widget(Some(&switch.widget()));
|
||||
|
||||
let toolbar = adw::ToolbarView::new();
|
||||
toolbar.add_top_bar(&header);
|
||||
@@ -124,8 +121,8 @@ pub(crate) fn show_duplicates(ui: &Rc<Ui>) {
|
||||
.set(view_closed.generation.get().wrapping_add(1));
|
||||
});
|
||||
let view_switch = view.clone();
|
||||
similar.connect_toggled(move |similar| {
|
||||
let finder = if similar.is_active() {
|
||||
switch.connect_changed(move |index| {
|
||||
let finder = if index == 1 {
|
||||
Finder::Similar
|
||||
} else {
|
||||
Finder::Exact
|
||||
@@ -220,14 +217,14 @@ fn ask(view: &Rc<FinderView>, finder: Finder, generation: u32) {
|
||||
view.status.set_icon_name(Some("dialog-warning-symbolic"));
|
||||
view.status.set_title(&failed);
|
||||
view.status
|
||||
.set_description(Some(&gettext("The mount service didn't respond.")));
|
||||
.set_description(Some(&gettext("The Proton Drive service didn't respond.")));
|
||||
view.progress.set_visible(false);
|
||||
return;
|
||||
}
|
||||
};
|
||||
view.progress.set_visible(false);
|
||||
if sets.is_empty() {
|
||||
view.status.set_icon_name(Some("emblem-ok-symbolic"));
|
||||
view.status.set_icon_name(Some("pdfs-synced-symbolic"));
|
||||
match finder {
|
||||
Finder::Exact => {
|
||||
view.status.set_title(&gettext("No duplicates"));
|
||||
|
||||
@@ -7,7 +7,7 @@ pub(crate) struct LoginState {
|
||||
pub(crate) password: adw::PasswordEntryRow,
|
||||
pub(crate) login_button: gtk4::Button,
|
||||
/// Spins inside the Sign in button while a sign-in is running.
|
||||
pub(crate) login_spinner: gtk4::Spinner,
|
||||
pub(crate) login_spinner: Spinner,
|
||||
pub(crate) login_status: gtk4::Label,
|
||||
}
|
||||
|
||||
@@ -34,7 +34,8 @@ pub(crate) fn build_login_page() -> (gtk4::Widget, LoginState) {
|
||||
group.add(&email);
|
||||
group.add(&password);
|
||||
|
||||
let login_spinner = gtk4::Spinner::builder().visible(false).build();
|
||||
let login_spinner = spinner();
|
||||
login_spinner.set_visible(false);
|
||||
let button_content = gtk4::Box::new(gtk4::Orientation::Horizontal, 8);
|
||||
button_content.append(&login_spinner);
|
||||
button_content.append(>k4::Label::new(Some(&gettext("Sign in"))));
|
||||
@@ -170,7 +171,7 @@ pub(crate) fn wire_login(ui: &Rc<Ui>) {
|
||||
// Cache the new identity so `refresh` never hits the keyring.
|
||||
*ui.session.borrow_mut() = auth::load().ok();
|
||||
// Enable+start the mount service now that we have a session.
|
||||
service::enable_start();
|
||||
let _ = gio::spawn_blocking(service::enable_start).await;
|
||||
refresh(&ui);
|
||||
}
|
||||
Err(e) => ui.login.login_status.set_text(&e),
|
||||
@@ -185,7 +186,6 @@ fn set_signing_in(ui: &Rc<Ui>, running: bool) {
|
||||
ui.login.email.set_sensitive(!running);
|
||||
ui.login.password.set_sensitive(!running);
|
||||
ui.login.login_spinner.set_visible(running);
|
||||
ui.login.login_spinner.set_spinning(running);
|
||||
}
|
||||
|
||||
/// What to tell the person when sign-in fails. Proton's own API messages are
|
||||
@@ -295,7 +295,7 @@ pub(crate) fn spawn_login(
|
||||
/// the worker reads as a cancelled login.
|
||||
pub(crate) fn prompt_2fa(ui: &Rc<Ui>, code_tx: std::sync::mpsc::Sender<String>) {
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Two-factor authentication"))
|
||||
.heading(gettext("Two-Factor Authentication"))
|
||||
.body(gettext(
|
||||
"Enter the code from your authenticator app, or one of your recovery codes.",
|
||||
))
|
||||
@@ -344,12 +344,20 @@ pub(crate) fn sign_out(ui: &Rc<Ui>) {
|
||||
),
|
||||
&gettext("Sign Out"),
|
||||
move || {
|
||||
service::disable_stop();
|
||||
if let Err(e) = auth::logout() {
|
||||
tracing::error!("logout failed: {e}");
|
||||
}
|
||||
*ui.session.borrow_mut() = None;
|
||||
refresh(&ui);
|
||||
// systemctl and the keyring both block, so run them on a worker.
|
||||
let ui = ui.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
let result = gio::spawn_blocking(|| {
|
||||
service::disable_stop();
|
||||
auth::logout().map_err(|e| e.to_string())
|
||||
})
|
||||
.await;
|
||||
if let Ok(Err(e)) = result {
|
||||
tracing::error!("logout failed: {e}");
|
||||
}
|
||||
*ui.session.borrow_mut() = None;
|
||||
refresh(&ui);
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub(crate) mod activity;
|
||||
pub(crate) mod activity_text;
|
||||
pub(crate) mod albums;
|
||||
pub(crate) mod browser;
|
||||
pub(crate) mod devices;
|
||||
|
||||
@@ -31,7 +31,7 @@ pub(crate) struct Viewer {
|
||||
pub(crate) scroller: gtk4::ScrolledWindow,
|
||||
pub(crate) picture: gtk4::Picture,
|
||||
pub(crate) video: gtk4::Video,
|
||||
pub(crate) spinner: gtk4::Spinner,
|
||||
pub(crate) spinner: Spinner,
|
||||
pub(crate) status: gtk4::Label,
|
||||
pub(crate) title: gtk4::Label,
|
||||
pub(crate) counter: gtk4::Label,
|
||||
@@ -224,7 +224,6 @@ fn viewer_item(ui: &Rc<Ui>, viewer: &Rc<Viewer>, uid: &str) -> Option<PhotoItem>
|
||||
/// plays in place once its file is down.
|
||||
pub(crate) fn load_photo(ui: &Rc<Ui>, viewer: &Rc<Viewer>, uid: String) {
|
||||
viewer.spinner.set_visible(true);
|
||||
viewer.spinner.start();
|
||||
viewer.status.set_visible(false);
|
||||
viewer.loading.set(true);
|
||||
*viewer.path.borrow_mut() = None;
|
||||
@@ -266,7 +265,6 @@ pub(crate) fn load_photo(ui: &Rc<Ui>, viewer: &Rc<Viewer>, uid: String) {
|
||||
if *viewer.uid.borrow() != uid {
|
||||
return;
|
||||
}
|
||||
viewer.spinner.stop();
|
||||
viewer.spinner.set_visible(false);
|
||||
viewer.loading.set(false);
|
||||
// Armed whether or not the photo rendered: a slideshow steps past a
|
||||
@@ -307,7 +305,7 @@ pub(crate) fn load_photo(ui: &Rc<Ui>, viewer: &Rc<Viewer>, uid: String) {
|
||||
}
|
||||
},
|
||||
Ok(Ok(Response::Error { message, .. })) => fail(&message),
|
||||
Ok(Ok(_)) => fail(&gettext("Unexpected reply from the mount service.")),
|
||||
Ok(Ok(_)) => fail(&gettext("Unexpected reply from the Proton Drive service.")),
|
||||
Ok(Err(_)) | Err(_) => fail(&gettext("Couldn't reach Proton Drive.")),
|
||||
}
|
||||
});
|
||||
@@ -1136,7 +1134,7 @@ pub(crate) fn open_photo_viewer(ui: &Rc<Ui>, initial_uid: String) {
|
||||
.child(
|
||||
&adw::ButtonContent::builder()
|
||||
.label(gettext("Show on map"))
|
||||
.icon_name("map-symbolic")
|
||||
.icon_name("pdfs-map-symbolic")
|
||||
.build(),
|
||||
)
|
||||
.halign(gtk4::Align::Start)
|
||||
@@ -1192,12 +1190,10 @@ pub(crate) fn open_photo_viewer(ui: &Rc<Ui>, initial_uid: String) {
|
||||
.child(&info_panel)
|
||||
.build();
|
||||
|
||||
let spinner = gtk4::Spinner::builder()
|
||||
.halign(gtk4::Align::Center)
|
||||
.valign(gtk4::Align::Center)
|
||||
.width_request(48)
|
||||
.height_request(48)
|
||||
.build();
|
||||
let spinner = spinner();
|
||||
spinner.set_halign(gtk4::Align::Center);
|
||||
spinner.set_valign(gtk4::Align::Center);
|
||||
spinner.set_size_request(48, 48);
|
||||
spinner.add_css_class("viewer-spinner");
|
||||
overlay.add_overlay(&spinner);
|
||||
|
||||
@@ -1426,7 +1422,7 @@ pub(crate) fn open_photo_viewer(ui: &Rc<Ui>, initial_uid: String) {
|
||||
None
|
||||
}
|
||||
Ok(Ok(Response::Error { message, .. })) => Some(message),
|
||||
_ => Some(gettext("The mount service didn't respond.")),
|
||||
_ => Some(gettext("The Proton Drive service didn't respond.")),
|
||||
};
|
||||
if let Some(detail) = failed {
|
||||
toast_error(
|
||||
|
||||
@@ -102,7 +102,7 @@ pub(crate) fn load_places(ui: &Rc<Ui>) {
|
||||
&ui,
|
||||
"network-offline-symbolic",
|
||||
&gettext("Not connected"),
|
||||
&gettext("The Proton Drive mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -422,7 +422,7 @@ fn open_place(ui: &Rc<Ui>, place: PlaceInfo) {
|
||||
|
||||
// As in an album: no filters, no Upload, no view switch — back leads out.
|
||||
ui.gallery.filters.set_visible(false);
|
||||
ui.gallery.view_switch.set_visible(false);
|
||||
ui.gallery.view_switch.widget().set_visible(false);
|
||||
ui.gallery.upload.set_visible(false);
|
||||
ui.gallery.back.set_visible(true);
|
||||
ui.gallery.kind.set(None);
|
||||
@@ -439,7 +439,7 @@ pub(crate) fn close_place(ui: &Rc<Ui>) {
|
||||
return;
|
||||
}
|
||||
ui.gallery.title.set_title(&gettext("Photos"));
|
||||
ui.gallery.view_switch.set_visible(true);
|
||||
ui.gallery.view_switch.widget().set_visible(true);
|
||||
ui.gallery.upload.set_visible(true);
|
||||
ui.gallery.back.set_visible(false);
|
||||
}
|
||||
@@ -455,14 +455,14 @@ fn places_status(ui: &Rc<Ui>, icon: &str, title: &str, description: &str) {
|
||||
/// Wire the Places toggle of the view switcher.
|
||||
pub(crate) fn wire_places(ui: &Rc<Ui>) {
|
||||
let ui_places = ui.clone();
|
||||
ui.gallery.places_btn.clone().connect_toggled(move |btn| {
|
||||
if btn.is_active() {
|
||||
ui.gallery.view_switch.connect_changed(move |view| {
|
||||
if view == VIEW_PLACES {
|
||||
show_places(&ui_places);
|
||||
}
|
||||
});
|
||||
|
||||
let ui_layout = ui.clone();
|
||||
ui.gallery.places_map_btn.connect_toggled(move |_| {
|
||||
ui.gallery.places_layout.connect_changed(move |_| {
|
||||
// A status page (loading, empty, an error) stays until a listing
|
||||
// replaces it; only the grid and the map swap.
|
||||
if ui_layout
|
||||
@@ -493,19 +493,19 @@ pub(crate) fn wire_places(ui: &Rc<Ui>) {
|
||||
/// Switch to the Places map and zoom in on `(latitude, longitude)`, where a
|
||||
/// photo in the viewer was taken.
|
||||
pub(crate) fn show_on_map(ui: &Rc<Ui>, latitude: f64, longitude: f64) {
|
||||
ui.gallery.places_map_btn.set_active(true);
|
||||
if ui.gallery.places_btn.is_active() {
|
||||
ui.gallery.places_layout.set_active(PLACES_MAP);
|
||||
if ui.gallery.view_switch.active() == VIEW_PLACES {
|
||||
// Already in Places, perhaps inside a town: back out to the listing.
|
||||
show_places(ui);
|
||||
} else {
|
||||
ui.gallery.places_btn.set_active(true);
|
||||
ui.gallery.view_switch.set_active(VIEW_PLACES);
|
||||
}
|
||||
ui.gallery.places_map.show_spot(latitude, longitude);
|
||||
}
|
||||
|
||||
/// Show the places as cards or on the map, as the Grid/Map toggle says.
|
||||
fn show_places_layout(ui: &Rc<Ui>) {
|
||||
let layout = if ui.gallery.places_map_btn.is_active() {
|
||||
let layout = if ui.gallery.places_layout.active() == PLACES_MAP {
|
||||
"map"
|
||||
} else {
|
||||
"grid"
|
||||
|
||||
@@ -1,38 +1,94 @@
|
||||
use crate::*;
|
||||
|
||||
pub(crate) struct SharedByMeState {
|
||||
// Shared (by me) page: one section listing the items I've shared, each with a
|
||||
// copy-link / manage affordance. Rebuilt wholesale on each load.
|
||||
// Shared by me page: the items I have shared, as a file list whose columns
|
||||
// say where each lives, who has it and what its public link is doing.
|
||||
pub(crate) files: FileList,
|
||||
/// "list", or "status" while the page loads, is empty or can't be read.
|
||||
pub(crate) content: gtk4::Stack,
|
||||
pub(crate) status: adw::StatusPage,
|
||||
pub(crate) retry: gtk4::Button,
|
||||
pub(crate) group: adw::PreferencesGroup,
|
||||
pub(crate) rows: RefCell<Vec<gtk4::Widget>>,
|
||||
pub(crate) inflight: Cell<bool>,
|
||||
pub(crate) loaded_at: Cell<Option<Instant>>,
|
||||
/// "12 items" under the page title.
|
||||
pub(crate) subtitle: adw::WindowTitle,
|
||||
/// Which items to show, indexed like [`SHARE_FILTERS`].
|
||||
pub(crate) filter: gtk4::DropDown,
|
||||
/// The last listing, unfiltered. The list holds [`DirEntry`]s; the columns
|
||||
/// and menus look a row's people and link up here by uid.
|
||||
pub(crate) items: Rc<RefCell<Vec<SharedItem>>>,
|
||||
/// Runs the loads; the rows stay up while one runs.
|
||||
pub(crate) loader: Rc<Loader>,
|
||||
}
|
||||
|
||||
impl SharedByMeState {
|
||||
pub(crate) fn new(widgets: &SharedByMeWidgets) -> Self {
|
||||
SharedByMeState {
|
||||
files: widgets.files.clone(),
|
||||
content: widgets.content.clone(),
|
||||
status: widgets.status.clone(),
|
||||
retry: widgets.retry.clone(),
|
||||
subtitle: widgets.subtitle.clone(),
|
||||
filter: widgets.filter.clone(),
|
||||
items: Rc::new(RefCell::new(Vec::new())),
|
||||
loader: Loader::new(&widgets.content),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Widgets the Shared (by-me) page's load/repaint touch.
|
||||
pub(crate) struct SharedByMeWidgets {
|
||||
pub(crate) files: FileList,
|
||||
pub(crate) content: gtk4::Stack,
|
||||
pub(crate) status: adw::StatusPage,
|
||||
pub(crate) group: adw::PreferencesGroup,
|
||||
pub(crate) retry: gtk4::Button,
|
||||
pub(crate) refresh: gtk4::Button,
|
||||
pub(crate) subtitle: adw::WindowTitle,
|
||||
pub(crate) filter: gtk4::DropDown,
|
||||
}
|
||||
|
||||
/// The Shared page: one section listing the items I have shared with others —
|
||||
/// each row summarizing who has access, opening the item when activated, with
|
||||
/// Copy link and a menu for the link and the per-node Share dialog.
|
||||
pub(crate) fn build_shared_by_me_page() -> (gtk4::Widget, SharedByMeWidgets) {
|
||||
let refresh = refresh_button();
|
||||
/// Which shared items the page shows.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
pub(crate) enum ShareFilter {
|
||||
All,
|
||||
/// Items with a public link.
|
||||
Links,
|
||||
/// Items shared with people, invited or already in.
|
||||
People,
|
||||
/// Items whose public link has expired.
|
||||
Expired,
|
||||
}
|
||||
|
||||
let group = adw::PreferencesGroup::new();
|
||||
let clamp = adw::Clamp::builder().child(&group).build();
|
||||
let scroll = gtk4::ScrolledWindow::builder()
|
||||
.vexpand(true)
|
||||
.child(&clamp)
|
||||
.build();
|
||||
/// The filters in the order the header's drop-down lists them.
|
||||
const SHARE_FILTERS: [ShareFilter; 4] = [
|
||||
ShareFilter::All,
|
||||
ShareFilter::Links,
|
||||
ShareFilter::People,
|
||||
ShareFilter::Expired,
|
||||
];
|
||||
|
||||
fn share_filter_label(filter: ShareFilter) -> String {
|
||||
match filter {
|
||||
ShareFilter::All => gettext("All Shared Items"),
|
||||
ShareFilter::Links => gettext("Links Only"),
|
||||
ShareFilter::People => gettext("People Only"),
|
||||
ShareFilter::Expired => gettext("Expired Links"),
|
||||
}
|
||||
}
|
||||
|
||||
/// The Shared by me page: every item I have shared with people or by link.
|
||||
/// Activating one opens its Share dialog, since managing access is what this
|
||||
/// page is for; opening the item itself is in the menu.
|
||||
pub(crate) fn build_shared_by_me_page() -> (gtk4::Widget, SharedByMeWidgets) {
|
||||
let files = FileList::new();
|
||||
files.show_list(true);
|
||||
let layout = layout_button(&files);
|
||||
let labels: Vec<String> = SHARE_FILTERS
|
||||
.iter()
|
||||
.copied()
|
||||
.map(share_filter_label)
|
||||
.collect();
|
||||
let filter =
|
||||
gtk4::DropDown::from_strings(&labels.iter().map(String::as_str).collect::<Vec<_>>());
|
||||
filter.set_valign(gtk4::Align::Center);
|
||||
filter.set_tooltip_text(Some(&gettext("Show only some shared items")));
|
||||
|
||||
let retry = gtk4::Button::builder()
|
||||
.label(gettext("Retry"))
|
||||
@@ -42,7 +98,7 @@ pub(crate) fn build_shared_by_me_page() -> (gtk4::Widget, SharedByMeWidgets) {
|
||||
retry.add_css_class("suggested-action");
|
||||
retry.set_visible(false);
|
||||
let status = adw::StatusPage::builder()
|
||||
.icon_name("emblem-shared-symbolic")
|
||||
.icon_name("pdfs-share-symbolic")
|
||||
.vexpand(true)
|
||||
.child(&retry)
|
||||
.build();
|
||||
@@ -51,277 +107,154 @@ pub(crate) fn build_shared_by_me_page() -> (gtk4::Widget, SharedByMeWidgets) {
|
||||
let content = gtk4::Stack::new();
|
||||
content.set_vexpand(true);
|
||||
content.set_transition_type(gtk4::StackTransitionType::Crossfade);
|
||||
content.add_named(&scroll, Some("list"));
|
||||
content.add_named(&files.views, Some("list"));
|
||||
content.add_named(&status, Some("status"));
|
||||
|
||||
let inner = gtk4::Box::new(gtk4::Orientation::Vertical, 12);
|
||||
inner.set_margin_top(18);
|
||||
inner.set_margin_bottom(18);
|
||||
inner.set_margin_start(18);
|
||||
inner.set_margin_end(18);
|
||||
inner.set_margin_top(12);
|
||||
inner.set_margin_bottom(12);
|
||||
inner.set_margin_start(12);
|
||||
inner.set_margin_end(12);
|
||||
inner.append(&content);
|
||||
let (frame, header, _) = page_frame(&gettext("Shared by Me"), &inner);
|
||||
header.pack_end(&refresh);
|
||||
|
||||
let (frame, header, subtitle) = page_frame(&gettext("Shared by me"), &inner);
|
||||
header.pack_start(&filter);
|
||||
header.pack_end(&layout);
|
||||
|
||||
(
|
||||
frame.upcast(),
|
||||
SharedByMeWidgets {
|
||||
files,
|
||||
content,
|
||||
status,
|
||||
group,
|
||||
retry,
|
||||
refresh,
|
||||
subtitle,
|
||||
filter,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Install the Shared (by-me) page's retry button.
|
||||
pub(crate) fn wire_shared_by_me(ui: &Rc<Ui>, retry: >k4::Button) {
|
||||
/// Install the file list, its columns, the filter and the retry button.
|
||||
pub(crate) fn wire_shared_by_me(ui: &Rc<Ui>, widgets: &SharedByMeWidgets) {
|
||||
let files = &ui.shared_by_me.files;
|
||||
files.wire(
|
||||
ui,
|
||||
FileListBehavior {
|
||||
activate: open_share_dialog,
|
||||
entry_menu: shared_by_me_menu,
|
||||
bulk_menu: shared_by_me_bulk_menu,
|
||||
background_menu: shared_by_me_background_menu,
|
||||
badges: false,
|
||||
drag_and_drop: false,
|
||||
},
|
||||
);
|
||||
let items = &ui.shared_by_me.items;
|
||||
files
|
||||
.column_view
|
||||
.append_column(&text_column(&gettext("Location"), |e| {
|
||||
location_label(&e.path)
|
||||
}));
|
||||
let people = items.clone();
|
||||
files
|
||||
.column_view
|
||||
.append_column(&text_column(&pgettext("column", "People"), move |e| {
|
||||
find_item(&people, &e.uid)
|
||||
.map(|item| people_label(&item))
|
||||
.unwrap_or_else(|| "—".to_string())
|
||||
}));
|
||||
files.column_view.append_column(&link_column(ui));
|
||||
|
||||
let ui_retry = ui.clone();
|
||||
retry.connect_clicked(move |_| {
|
||||
service::restart();
|
||||
load_shared_by_me(&ui_retry);
|
||||
widgets
|
||||
.retry
|
||||
.connect_clicked(move |_| restart_service_then(&ui_retry, load_shared_by_me));
|
||||
let ui_filter = ui.clone();
|
||||
widgets.filter.connect_selected_notify(move |_| {
|
||||
let items = ui_filter.shared_by_me.items.borrow().clone();
|
||||
repaint_shared_by_me(&ui_filter, &items);
|
||||
});
|
||||
}
|
||||
|
||||
/// Show a status page in place of the Shared (by-me) list.
|
||||
pub(crate) fn shared_by_me_status(
|
||||
ui: &Rc<Ui>,
|
||||
icon: &str,
|
||||
title: &str,
|
||||
description: &str,
|
||||
retry: bool,
|
||||
) {
|
||||
ui.shared_by_me.status.set_icon_name(Some(icon));
|
||||
ui.shared_by_me.status.set_title(title);
|
||||
ui.shared_by_me.status.set_description(Some(description));
|
||||
ui.shared_by_me.retry.set_visible(retry);
|
||||
ui.shared_by_me.content.set_visible_child_name("status");
|
||||
/// The shared item a row stands for.
|
||||
fn find_item(items: &RefCell<Vec<SharedItem>>, uid: &str) -> Option<SharedItem> {
|
||||
items.borrow().iter().find(|item| item.uid == uid).cloned()
|
||||
}
|
||||
|
||||
/// Fetch the shared-by-me listing and repaint the page.
|
||||
pub(crate) fn load_shared_by_me(ui: &Rc<Ui>) {
|
||||
if ui.shared_by_me.inflight.get() {
|
||||
return;
|
||||
}
|
||||
cancel_file_thumbnails(ui);
|
||||
ui.shared_by_me.inflight.set(true);
|
||||
shared_by_me_status(
|
||||
ui,
|
||||
"emblem-shared-symbolic",
|
||||
&gettext("Loading…"),
|
||||
&gettext("Reading what you've shared."),
|
||||
false,
|
||||
);
|
||||
ui.busy_begin();
|
||||
let rx = spawn_request(ui.dirs.control_socket(), Request::ListSharedByMe);
|
||||
let ui = ui.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
let result = rx.recv().await;
|
||||
ui.busy_end();
|
||||
ui.shared_by_me.inflight.set(false);
|
||||
match result {
|
||||
Ok(Ok(Response::SharedByMe { items })) => {
|
||||
repaint_shared_by_me(&ui, &items);
|
||||
ui.shared_by_me.loaded_at.set(Some(Instant::now()));
|
||||
}
|
||||
Ok(Ok(Response::Error { message, .. })) => shared_by_me_status(
|
||||
&ui,
|
||||
"dialog-warning-symbolic",
|
||||
&gettext("Couldn't read your shares"),
|
||||
&message,
|
||||
false,
|
||||
),
|
||||
Ok(Ok(_)) => shared_by_me_status(
|
||||
&ui,
|
||||
"dialog-warning-symbolic",
|
||||
&gettext("Couldn't read your shares"),
|
||||
&gettext("Unexpected reply from the mount service."),
|
||||
false,
|
||||
),
|
||||
Ok(Err(_)) | Err(_) => {
|
||||
ui.shared_by_me.loaded_at.set(None);
|
||||
shared_by_me_unreachable(&ui);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The daemon didn't answer the Shared (by-me) page.
|
||||
pub(crate) fn shared_by_me_unreachable(ui: &Rc<Ui>) {
|
||||
if service::is_failed() || !service::is_active() {
|
||||
shared_by_me_status(
|
||||
ui,
|
||||
"network-offline-symbolic",
|
||||
&gettext("Not connected"),
|
||||
&gettext("The Proton Drive mount service isn't running."),
|
||||
true,
|
||||
);
|
||||
return;
|
||||
}
|
||||
shared_by_me_status(
|
||||
ui,
|
||||
"folder-remote-symbolic",
|
||||
&gettext("Connecting…"),
|
||||
&gettext("Waiting for the Proton Drive mount service to come up."),
|
||||
false,
|
||||
);
|
||||
let ui = ui.clone();
|
||||
glib::timeout_add_local_once(CONNECT_RETRY_INTERVAL, move || {
|
||||
if ui.stack.visible_child_name().as_deref() == Some("sharedbyme") {
|
||||
load_shared_by_me(&ui);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Rebuild the Shared (by-me) section from a fresh listing.
|
||||
pub(crate) fn repaint_shared_by_me(ui: &Rc<Ui>, items: &[SharedItem]) {
|
||||
for row in ui.shared_by_me.rows.borrow_mut().drain(..) {
|
||||
ui.shared_by_me.group.remove(&row);
|
||||
}
|
||||
if items.is_empty() {
|
||||
shared_by_me_status(
|
||||
ui,
|
||||
"emblem-shared-symbolic",
|
||||
&gettext("Nothing shared yet"),
|
||||
&gettext("Items you share with people or by link show up here."),
|
||||
false,
|
||||
);
|
||||
return;
|
||||
}
|
||||
ui.shared_by_me.content.set_visible_child_name("list");
|
||||
let mut rows: Vec<gtk4::Widget> = Vec::new();
|
||||
for item in items {
|
||||
let entry = shared_item_as_entry(item);
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(&item.name)
|
||||
.subtitle(shared_item_summary(item))
|
||||
/// The Link column: what the public link is doing, and a button that copies
|
||||
/// it while it works.
|
||||
fn link_column(ui: &Rc<Ui>) -> gtk4::ColumnViewColumn {
|
||||
let factory = gtk4::SignalListItemFactory::new();
|
||||
let ui_setup = ui.clone();
|
||||
factory.connect_setup(move |_, item| {
|
||||
let item = item.downcast_ref::<gtk4::ListItem>().unwrap();
|
||||
let label = gtk4::Label::builder()
|
||||
.halign(gtk4::Align::Start)
|
||||
.hexpand(true)
|
||||
.ellipsize(gtk4::pango::EllipsizeMode::End)
|
||||
.build();
|
||||
row.add_prefix(&file_thumbnail(ui, &entry, 40, 24, true));
|
||||
|
||||
// A node the daemon can place in my tree opens like it would in My Files.
|
||||
if !item.path.is_empty() {
|
||||
row.set_activatable(true);
|
||||
let ui_open = ui.clone();
|
||||
let entry_open = entry.clone();
|
||||
row.connect_activated(move |_| open_shared_by_me(&ui_open, &entry_open));
|
||||
}
|
||||
|
||||
let url = item.link.as_ref().and_then(|l| l.url.clone());
|
||||
// The one action people come here for most gets its own button.
|
||||
if let Some(url) = url.clone() {
|
||||
let copy = gtk4::Button::builder()
|
||||
.icon_name("edit-copy-symbolic")
|
||||
.tooltip_text(gettext("Copy link"))
|
||||
.valign(gtk4::Align::Center)
|
||||
.build();
|
||||
copy.add_css_class("flat");
|
||||
let ui_copy = ui.clone();
|
||||
copy.connect_clicked(move |btn| {
|
||||
label.add_css_class("dim-label");
|
||||
let copy = gtk4::Button::builder()
|
||||
.icon_name("pdfs-link-symbolic")
|
||||
.tooltip_text(gettext("Copy link"))
|
||||
.valign(gtk4::Align::Center)
|
||||
.build();
|
||||
copy.add_css_class("flat");
|
||||
// Read at click time: the view recycles this cell for other rows.
|
||||
let (ui, item_c) = (ui_setup.clone(), item.clone());
|
||||
copy.connect_clicked(move |btn| {
|
||||
let Some(obj) = item_c.item().and_downcast::<BoxedAnyObject>() else {
|
||||
return;
|
||||
};
|
||||
let uid = obj.borrow::<DirEntry>().uid.clone();
|
||||
if let Some(url) = find_item(&ui.shared_by_me.items, &uid)
|
||||
.and_then(|item| item.link)
|
||||
.and_then(|link| link.url)
|
||||
{
|
||||
btn.clipboard().set_text(&url);
|
||||
toast(&ui_copy, &gettext("Link copied"));
|
||||
});
|
||||
row.add_suffix(©);
|
||||
}
|
||||
row.add_suffix(&shared_by_me_menu(ui, &entry, url));
|
||||
|
||||
ui.shared_by_me.group.add(&row);
|
||||
rows.push(row.upcast());
|
||||
}
|
||||
*ui.shared_by_me.rows.borrow_mut() = rows;
|
||||
}
|
||||
|
||||
/// The ⋮ menu on a shared item: open it, the link, the Share dialog, and
|
||||
/// Stop Sharing.
|
||||
///
|
||||
/// Manage access opens the per-node Share dialog, which addresses a pathless
|
||||
/// node by uid, so every shared item can be managed from here.
|
||||
fn shared_by_me_menu(ui: &Rc<Ui>, entry: &DirEntry, url: Option<String>) -> gtk4::MenuButton {
|
||||
let mut items: Vec<(String, MenuAction)> = Vec::new();
|
||||
if !entry.path.is_empty() {
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
items.push((
|
||||
pgettext("verb", "Open"),
|
||||
Box::new(move || open_shared_by_me(&ui_c, &entry_c)),
|
||||
));
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
items.push((
|
||||
gettext("Show in My Files"),
|
||||
Box::new(move || show_in_my_files(&ui_c, &entry_c)),
|
||||
));
|
||||
}
|
||||
if let Some(url) = url {
|
||||
items.push((gettext("Open Link"), Box::new(move || open_uri(&url))));
|
||||
}
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
items.push((
|
||||
gettext("Manage Access…"),
|
||||
Box::new(move || open_share_dialog(&ui_c, &entry_c)),
|
||||
));
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
items.push((
|
||||
gettext("Stop Sharing…"),
|
||||
Box::new(move || prompt_stop_sharing(&ui_c, &entry_c)),
|
||||
));
|
||||
let (labels, actions): (Vec<String>, Vec<MenuAction>) = items.into_iter().unzip();
|
||||
more_menu_button(labels.iter().map(String::as_str).zip(actions).collect())
|
||||
}
|
||||
|
||||
/// Confirm, then remove every member, invitation and the public link.
|
||||
pub(crate) fn prompt_stop_sharing(ui: &Rc<Ui>, entry: &DirEntry) {
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Stop sharing?"))
|
||||
// Translators: {name} is the shared item's name.
|
||||
.body(gettext_f(
|
||||
"Everyone you invited loses access to “{name}”, pending invitations are withdrawn and its public link stops working.",
|
||||
&[("name", &entry.name)],
|
||||
))
|
||||
.build();
|
||||
dialog.add_response("cancel", &gettext("Cancel"));
|
||||
dialog.add_response("stop", &gettext("Stop Sharing"));
|
||||
dialog.set_response_appearance("stop", adw::ResponseAppearance::Destructive);
|
||||
dialog.set_default_response(Some("cancel"));
|
||||
dialog.set_close_response("cancel");
|
||||
let ui_c = ui.clone();
|
||||
let uid = entry.uid.clone();
|
||||
let name = entry.name.clone();
|
||||
dialog.connect_response(Some("stop"), move |_, _| {
|
||||
run_mutation(
|
||||
&ui_c,
|
||||
Request::StopSharingByUid { uid: uid.clone() },
|
||||
// Translators: {name} is the item's name.
|
||||
gettext_f("Stopped sharing {name}", &[("name", &name)]),
|
||||
gettext_noop("Couldn't stop sharing"),
|
||||
flash_copied(btn, "pdfs-link-symbolic");
|
||||
toast(&ui, &gettext("Link copied"));
|
||||
}
|
||||
});
|
||||
let cell = gtk4::Box::new(gtk4::Orientation::Horizontal, 6);
|
||||
cell.append(&label);
|
||||
cell.append(©);
|
||||
item.set_child(Some(&cell));
|
||||
});
|
||||
let items = ui.shared_by_me.items.clone();
|
||||
factory.connect_bind(move |_, item| {
|
||||
let item = item.downcast_ref::<gtk4::ListItem>().unwrap();
|
||||
let cell = item.child().and_downcast::<gtk4::Box>().unwrap();
|
||||
let label = cell.first_child().and_downcast::<gtk4::Label>().unwrap();
|
||||
let copy = cell.last_child().unwrap();
|
||||
let obj = item.item().and_downcast::<BoxedAnyObject>().unwrap();
|
||||
let uid = obj.borrow::<DirEntry>().uid.clone();
|
||||
let link = find_item(&items, &uid).and_then(|item| item.link);
|
||||
let now = glib::real_time() / 1_000_000;
|
||||
let text = link_label(link.as_ref(), now);
|
||||
label.set_label(&text);
|
||||
label.set_tooltip_text(Some(&text));
|
||||
copy.set_visible(
|
||||
link.as_ref()
|
||||
.is_some_and(|l| l.url.is_some() && !link_expired(l, now)),
|
||||
);
|
||||
});
|
||||
dialog.present(ui_window(ui).as_ref());
|
||||
let column = gtk4::ColumnViewColumn::new(Some(&pgettext("column", "Link")), Some(factory));
|
||||
column.set_resizable(true);
|
||||
column
|
||||
}
|
||||
|
||||
/// Open a shared item: a folder in My Files, a file the way My Files would.
|
||||
fn open_shared_by_me(ui: &Rc<Ui>, entry: &DirEntry) {
|
||||
if entry.is_dir {
|
||||
ui.stack.set_visible_child_name("browser");
|
||||
ui.browser.search.set_text("");
|
||||
browse_to(ui, entry.path.clone());
|
||||
} else {
|
||||
activate_entry(ui, entry);
|
||||
/// Where a shared item lives in My files: its folder, "My files" at the top,
|
||||
/// or a dash when the daemon can't place it.
|
||||
pub(crate) fn location_label(path: &str) -> String {
|
||||
match path.rsplit_once('/') {
|
||||
Some((parent, _)) => parent.to_string(),
|
||||
None if path.is_empty() => "—".to_string(),
|
||||
None => gettext("My files"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Go to the folder holding a shared item in My Files.
|
||||
pub(crate) fn show_in_my_files(ui: &Rc<Ui>, entry: &DirEntry) {
|
||||
let parent = entry
|
||||
.path
|
||||
.rsplit_once('/')
|
||||
.map(|(parent, _)| parent.to_string())
|
||||
.unwrap_or_default();
|
||||
ui.stack.set_visible_child_name("browser");
|
||||
ui.browser.search.set_text("");
|
||||
browse_to(ui, parent);
|
||||
}
|
||||
|
||||
/// A one-line summary of who can reach a shared item, for its row subtitle.
|
||||
pub(crate) fn shared_item_summary(item: &SharedItem) -> String {
|
||||
/// Who can reach a shared item through an invitation.
|
||||
pub(crate) fn people_label(item: &SharedItem) -> String {
|
||||
let mut parts = Vec::new();
|
||||
if item.member_count > 0 {
|
||||
// Translators: how many people have access to a shared item.
|
||||
@@ -341,19 +274,333 @@ pub(crate) fn shared_item_summary(item: &SharedItem) -> String {
|
||||
&[],
|
||||
));
|
||||
}
|
||||
if item.link.is_some() {
|
||||
parts.push(gettext("Public link"));
|
||||
}
|
||||
if parts.is_empty() {
|
||||
// Translators: a shared item's summary when no people or link can be counted.
|
||||
pgettext("state", "Shared")
|
||||
"—".to_string()
|
||||
} else {
|
||||
parts.join(" · ")
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a [`DirEntry`] from a [`SharedItem`] so the path-keyed Share dialog can
|
||||
/// open on it. Only used when the item's path is known.
|
||||
fn link_expired(link: &PublicLinkInfo, now: i64) -> bool {
|
||||
link.expires.is_some_and(|at| at <= now)
|
||||
}
|
||||
|
||||
/// What a public link is doing at `now` (epoch seconds): working, working
|
||||
/// until a date, or expired, and whether a password guards it.
|
||||
pub(crate) fn link_label(link: Option<&PublicLinkInfo>, now: i64) -> String {
|
||||
let Some(link) = link else {
|
||||
return "—".to_string();
|
||||
};
|
||||
let state = match link.expires {
|
||||
Some(at) if at <= now => pgettext("link", "Expired"),
|
||||
// Translators: {date} is when a public link stops working.
|
||||
Some(at) => gettext_f("Expires {date}", &[("date", &dates::short_date(at))]),
|
||||
None if link.created > 0 => gettext_f(
|
||||
// Translators: {date} is when the public link was created.
|
||||
"Active since {date}",
|
||||
&[("date", &dates::short_date(link.created))],
|
||||
),
|
||||
None => pgettext("link", "Active"),
|
||||
};
|
||||
if link.has_password {
|
||||
[state, gettext("Password protected")].join(" · ")
|
||||
} else {
|
||||
state
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `item` passes `filter` at `now` (epoch seconds).
|
||||
pub(crate) fn matches_filter(item: &SharedItem, filter: ShareFilter, now: i64) -> bool {
|
||||
match filter {
|
||||
ShareFilter::All => true,
|
||||
ShareFilter::Links => item.link.is_some(),
|
||||
ShareFilter::People => item.member_count + item.invite_count > 0,
|
||||
ShareFilter::Expired => item.link.as_ref().is_some_and(|l| link_expired(l, now)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Show a status page in place of the Shared (by-me) list.
|
||||
pub(crate) fn shared_by_me_status(
|
||||
ui: &Rc<Ui>,
|
||||
icon: &str,
|
||||
title: &str,
|
||||
description: &str,
|
||||
retry: bool,
|
||||
) {
|
||||
ui.shared_by_me.status.set_icon_name(Some(icon));
|
||||
ui.shared_by_me.status.set_title(title);
|
||||
ui.shared_by_me.status.set_description(Some(description));
|
||||
ui.shared_by_me.retry.set_visible(retry);
|
||||
ui.shared_by_me.content.set_visible_child_name("status");
|
||||
ui.shared_by_me.subtitle.set_subtitle("");
|
||||
}
|
||||
|
||||
/// Fetch the shared-by-me listing and repaint the page.
|
||||
pub(crate) fn load_shared_by_me(ui: &Rc<Ui>) {
|
||||
cancel_file_thumbnails(ui);
|
||||
let ui_p = ui.clone();
|
||||
let ticket = ui.shared_by_me.loader.refresh(move || {
|
||||
ui_p.shared_by_me.files.model.remove_all();
|
||||
shared_by_me_status(
|
||||
&ui_p,
|
||||
"pdfs-share-symbolic",
|
||||
&gettext("Loading…"),
|
||||
&gettext("Reading what you've shared."),
|
||||
false,
|
||||
);
|
||||
});
|
||||
ui.busy_begin();
|
||||
let rx = spawn_request(ui.dirs.control_socket(), Request::ListSharedByMe);
|
||||
let ui = ui.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
let result = rx.recv().await;
|
||||
ui.busy_end();
|
||||
if !ticket.is_current() {
|
||||
return;
|
||||
}
|
||||
if !matches!(result, Ok(Ok(Response::SharedByMe { .. }))) {
|
||||
ui.shared_by_me.files.model.remove_all();
|
||||
}
|
||||
match result {
|
||||
Ok(Ok(Response::SharedByMe { items })) => {
|
||||
let changed = changed_items(&ui.shared_by_me.items.borrow(), &items);
|
||||
*ui.shared_by_me.items.borrow_mut() = items.clone();
|
||||
repaint_shared_by_me(&ui, &items);
|
||||
rebind_rows(&ui, &changed);
|
||||
}
|
||||
Ok(Ok(Response::Error { message, .. })) => shared_by_me_status(
|
||||
&ui,
|
||||
"dialog-warning-symbolic",
|
||||
&gettext("Couldn't read your shares"),
|
||||
&message,
|
||||
false,
|
||||
),
|
||||
Ok(Ok(_)) => shared_by_me_status(
|
||||
&ui,
|
||||
"dialog-warning-symbolic",
|
||||
&gettext("Couldn't read your shares"),
|
||||
&gettext("Unexpected reply from the Proton Drive service."),
|
||||
false,
|
||||
),
|
||||
Ok(Err(_)) | Err(_) => {
|
||||
shared_by_me_unreachable(&ui);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The daemon didn't answer the Shared (by-me) page.
|
||||
pub(crate) fn shared_by_me_unreachable(ui: &Rc<Ui>) {
|
||||
service_unreachable(ui, "sharedbyme", shared_by_me_status, load_shared_by_me);
|
||||
}
|
||||
|
||||
/// Show the items that pass the header's filter.
|
||||
pub(crate) fn repaint_shared_by_me(ui: &Rc<Ui>, items: &[SharedItem]) {
|
||||
if items.is_empty() {
|
||||
shared_by_me_status(
|
||||
ui,
|
||||
"pdfs-share-symbolic",
|
||||
&gettext("Nothing shared yet"),
|
||||
&gettext("Items you share with people or by link show up here."),
|
||||
false,
|
||||
);
|
||||
ui.shared_by_me.files.model.remove_all();
|
||||
return;
|
||||
}
|
||||
let filter = SHARE_FILTERS
|
||||
.get(ui.shared_by_me.filter.selected() as usize)
|
||||
.copied()
|
||||
.unwrap_or(ShareFilter::All);
|
||||
let now = glib::real_time() / 1_000_000;
|
||||
let shown: Vec<DirEntry> = items
|
||||
.iter()
|
||||
.filter(|item| matches_filter(item, filter, now))
|
||||
.map(shared_item_as_entry)
|
||||
.collect();
|
||||
if shown.is_empty() {
|
||||
shared_by_me_status(
|
||||
ui,
|
||||
"pdfs-share-symbolic",
|
||||
&gettext("No Matching Items"),
|
||||
&gettext("None of your shared items match this filter."),
|
||||
false,
|
||||
);
|
||||
ui.shared_by_me.files.model.remove_all();
|
||||
return;
|
||||
}
|
||||
ui.shared_by_me.content.set_visible_child_name("list");
|
||||
ui.shared_by_me.subtitle.set_subtitle(&ngettext_f(
|
||||
"{n} item",
|
||||
"{n} items",
|
||||
shown.len() as u64,
|
||||
&[],
|
||||
));
|
||||
// Only the rows that changed are swapped, so a refresh keeps the selection
|
||||
// and the scroll position.
|
||||
replace_items(&ui.shared_by_me.files.model, &shown);
|
||||
}
|
||||
|
||||
/// The uids in `new` whose people or link differ from `old`. Their rows show
|
||||
/// the same [`DirEntry`] as before, so the list would not repaint them.
|
||||
fn changed_items(old: &[SharedItem], new: &[SharedItem]) -> HashSet<String> {
|
||||
new.iter()
|
||||
.filter(|item| old.iter().find(|o| o.uid == item.uid) != Some(*item))
|
||||
.map(|item| item.uid.clone())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Have the list bind the rows of `uids` again, to repaint their columns.
|
||||
fn rebind_rows(ui: &Rc<Ui>, uids: &HashSet<String>) {
|
||||
let model = &ui.shared_by_me.files.model;
|
||||
for pos in 0..model.n_items() {
|
||||
if entry_at(Some(model), pos).is_some_and(|e| uids.contains(&e.uid)) {
|
||||
model.items_changed(pos, 1, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A shared item's right-click menu: manage it, open it, its link, and Stop
|
||||
/// Sharing. Manage Access opens the Share dialog, which addresses a pathless
|
||||
/// node by uid, so every shared item can be managed from here.
|
||||
fn shared_by_me_menu(ui: &Rc<Ui>, entry: &DirEntry) -> ActionMenu {
|
||||
let mut menu = ActionMenu::new();
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
menu.item(&gettext("Manage Access…"), move || {
|
||||
open_share_dialog(&ui_c, &entry_c)
|
||||
});
|
||||
// A node the daemon can place in my tree opens like it would in My Files.
|
||||
if !entry.path.is_empty() {
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
menu.item(&pgettext("verb", "Open"), move || {
|
||||
open_shared_by_me(&ui_c, &entry_c)
|
||||
});
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
menu.item(&gettext("Show in My Files"), move || {
|
||||
show_in_my_files(&ui_c, &entry_c)
|
||||
});
|
||||
}
|
||||
let now = glib::real_time() / 1_000_000;
|
||||
let url = find_item(&ui.shared_by_me.items, &entry.uid)
|
||||
.and_then(|item| item.link)
|
||||
.filter(|link| !link_expired(link, now))
|
||||
.and_then(|link| link.url);
|
||||
if let Some(url) = url {
|
||||
menu.section();
|
||||
let (ui_c, url_c) = (ui.clone(), url.clone());
|
||||
menu.item(&gettext("Copy Link"), move || {
|
||||
ui_c.stack.clipboard().set_text(&url_c);
|
||||
toast(&ui_c, &gettext("Link copied"));
|
||||
});
|
||||
menu.item(&gettext("Open Link"), move || open_uri(&url));
|
||||
}
|
||||
menu.section();
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
menu.item(&gettext("Stop Sharing…"), move || {
|
||||
prompt_stop_sharing(&ui_c, std::slice::from_ref(&entry_c))
|
||||
});
|
||||
menu
|
||||
}
|
||||
|
||||
fn shared_by_me_bulk_menu(ui: &Rc<Ui>, entries: Vec<DirEntry>) -> ActionMenu {
|
||||
let mut menu = ActionMenu::new();
|
||||
menu.labelled_section(&ngettext_f(
|
||||
"{n} selected",
|
||||
"{n} selected",
|
||||
entries.len() as u64,
|
||||
&[],
|
||||
));
|
||||
let ui_c = ui.clone();
|
||||
menu.item(&gettext("Stop Sharing…"), move || {
|
||||
prompt_stop_sharing(&ui_c, &entries)
|
||||
});
|
||||
menu
|
||||
}
|
||||
|
||||
fn shared_by_me_background_menu(ui: &Rc<Ui>) -> ActionMenu {
|
||||
let mut menu = ActionMenu::new();
|
||||
let ui_c = ui.clone();
|
||||
menu.item(&gettext("Select All"), move || {
|
||||
ui_c.shared_by_me.files.selection.select_all();
|
||||
});
|
||||
let ui_c = ui.clone();
|
||||
menu.item(&gettext("Refresh"), move || reload_current_page(&ui_c));
|
||||
menu
|
||||
}
|
||||
|
||||
/// Confirm, then remove every member, invitation and the public link.
|
||||
pub(crate) fn prompt_stop_sharing(ui: &Rc<Ui>, entries: &[DirEntry]) {
|
||||
let body = match entries {
|
||||
// Translators: {name} is the shared item's name.
|
||||
[one] => gettext_f(
|
||||
"Everyone you invited loses access to “{name}”, pending invitations are withdrawn and its public link stops working.",
|
||||
&[("name", &one.name)],
|
||||
),
|
||||
_ => ngettext_f(
|
||||
"Everyone you invited loses access to {n} item, pending invitations are withdrawn and its public link stops working.",
|
||||
"Everyone you invited loses access to {n} items, pending invitations are withdrawn and their public links stop working.",
|
||||
entries.len() as u64,
|
||||
&[],
|
||||
),
|
||||
};
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Stop Sharing?"))
|
||||
.body(body)
|
||||
.build();
|
||||
dialog.add_response("cancel", &gettext("Cancel"));
|
||||
dialog.add_response("stop", &gettext("Stop Sharing"));
|
||||
dialog.set_response_appearance("stop", adw::ResponseAppearance::Destructive);
|
||||
dialog.set_default_response(Some("cancel"));
|
||||
dialog.set_close_response("cancel");
|
||||
let ui_c = ui.clone();
|
||||
let done = match entries {
|
||||
// Translators: {name} is the item's name.
|
||||
[one] => gettext_f("Stopped sharing {name}", &[("name", &one.name)]),
|
||||
_ => ngettext_f(
|
||||
"Stopped sharing {n} item",
|
||||
"Stopped sharing {n} items",
|
||||
entries.len() as u64,
|
||||
&[],
|
||||
),
|
||||
};
|
||||
let uids: Vec<String> = entries.iter().map(|e| e.uid.clone()).collect();
|
||||
dialog.connect_response(Some("stop"), move |_, _| {
|
||||
let done = done.clone();
|
||||
run_requests(
|
||||
&ui_c,
|
||||
uids.iter()
|
||||
.map(|uid| Request::StopSharingByUid { uid: uid.clone() })
|
||||
.collect(),
|
||||
gettext_noop("Couldn't stop sharing"),
|
||||
move |ui| {
|
||||
reload_listing(ui);
|
||||
toast(ui, &done);
|
||||
},
|
||||
);
|
||||
});
|
||||
dialog.present(ui_window(ui).as_ref());
|
||||
}
|
||||
|
||||
/// Open a shared item: a folder in My Files, a file the way My Files would.
|
||||
fn open_shared_by_me(ui: &Rc<Ui>, entry: &DirEntry) {
|
||||
if entry.is_dir {
|
||||
open_in_my_files(ui, entry.path.clone());
|
||||
} else {
|
||||
activate_entry(ui, entry);
|
||||
}
|
||||
}
|
||||
|
||||
/// Go to the folder holding a shared item in My Files.
|
||||
pub(crate) fn show_in_my_files(ui: &Rc<Ui>, entry: &DirEntry) {
|
||||
let parent = entry
|
||||
.path
|
||||
.rsplit_once('/')
|
||||
.map(|(parent, _)| parent.to_string())
|
||||
.unwrap_or_default();
|
||||
open_in_my_files(ui, parent);
|
||||
}
|
||||
|
||||
/// Build a [`DirEntry`] from a [`SharedItem`], for the file list and the
|
||||
/// Share dialog, which addresses a pathless node by uid.
|
||||
pub(crate) fn shared_item_as_entry(item: &SharedItem) -> DirEntry {
|
||||
DirEntry {
|
||||
name: item.name.clone(),
|
||||
@@ -369,5 +616,80 @@ pub(crate) fn shared_item_as_entry(item: &SharedItem) -> DirEntry {
|
||||
shared_by: String::new(),
|
||||
shared_at: 0,
|
||||
shared_by_unverified: false,
|
||||
trashed_at: 0,
|
||||
trashed_from: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn item(members: usize, invites: usize, link: Option<PublicLinkInfo>) -> SharedItem {
|
||||
SharedItem {
|
||||
uid: "vol~link".into(),
|
||||
name: "Budget.ods".into(),
|
||||
is_dir: false,
|
||||
modified: 0,
|
||||
path: "Work/Budget.ods".into(),
|
||||
member_count: members,
|
||||
invite_count: invites,
|
||||
link,
|
||||
}
|
||||
}
|
||||
|
||||
fn link(expires: Option<i64>, has_password: bool) -> PublicLinkInfo {
|
||||
PublicLinkInfo {
|
||||
id: "l".into(),
|
||||
url: Some("https://drive.proton.me/urls/X#y".into()),
|
||||
role: "viewer".into(),
|
||||
expires,
|
||||
has_password,
|
||||
created: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_link_says_whether_it_still_works() {
|
||||
assert_eq!(link_label(None, 100), "—");
|
||||
assert_eq!(link_label(Some(&link(None, false)), 100), "Active");
|
||||
assert_eq!(link_label(Some(&link(Some(100), false)), 100), "Expired");
|
||||
assert!(link_label(Some(&link(Some(200), false)), 100).starts_with("Expires "));
|
||||
assert_eq!(
|
||||
link_label(Some(&link(None, true)), 100),
|
||||
"Active · Password protected"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_filters_pick_links_people_and_expired_links() {
|
||||
let people = item(2, 0, None);
|
||||
let invited = item(0, 1, None);
|
||||
let live = item(0, 0, Some(link(None, false)));
|
||||
let expired = item(0, 0, Some(link(Some(50), false)));
|
||||
let pick = |filter| {
|
||||
[&people, &invited, &live, &expired]
|
||||
.iter()
|
||||
.map(|i| matches_filter(i, filter, 100))
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
assert_eq!(pick(ShareFilter::All), [true, true, true, true]);
|
||||
assert_eq!(pick(ShareFilter::Links), [false, false, true, true]);
|
||||
assert_eq!(pick(ShareFilter::People), [true, true, false, false]);
|
||||
assert_eq!(pick(ShareFilter::Expired), [false, false, false, true]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_location_is_the_folder_in_my_files() {
|
||||
assert_eq!(location_label("Work/Budget.ods"), "Work");
|
||||
assert_eq!(location_label("Budget.ods"), "My files");
|
||||
assert_eq!(location_label(""), "—");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn people_counts_members_and_pending_invitations() {
|
||||
assert_eq!(people_label(&item(0, 0, None)), "—");
|
||||
assert_eq!(people_label(&item(1, 0, None)), "1 person");
|
||||
assert_eq!(people_label(&item(2, 3, None)), "2 people · 3 pending");
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@ pub(crate) struct StatusState {
|
||||
/// Sidebar footer: the one-line sync state. The mount is driven by the
|
||||
/// systemd user service, not by the user — so this only reports.
|
||||
pub(crate) status_icon: gtk4::Image,
|
||||
/// Stands in for [`Self::status_icon`] until the first status arrives.
|
||||
pub(crate) status_spinner: Spinner,
|
||||
pub(crate) status_title: gtk4::Label,
|
||||
pub(crate) status_detail: gtk4::Label,
|
||||
pub(crate) cache_bar: gtk4::ProgressBar,
|
||||
@@ -82,12 +84,29 @@ pub(crate) struct StatusState {
|
||||
/// How many transfers were in flight on the previous poll. A drop to zero is
|
||||
/// what "sync complete" means; there's no completion event on the wire.
|
||||
pub(crate) active_transfers: Cell<usize>,
|
||||
/// The run of transfers since the count last left zero, for deciding
|
||||
/// whether its end is worth a "Sync complete" notification.
|
||||
pub(crate) transfer_batch: RefCell<Option<TransferBatch>>,
|
||||
}
|
||||
|
||||
/// One run of transfers, from the first file to the count falling back to 0.
|
||||
pub(crate) struct TransferBatch {
|
||||
started: Instant,
|
||||
/// Every file seen in flight during the run, so the notification can count
|
||||
/// files rather than the handful still moving on the last poll.
|
||||
names: HashSet<String>,
|
||||
}
|
||||
|
||||
/// A run of transfers earns a "Sync complete" notification only when it moved
|
||||
/// at least this many files or lasted at least [`SYNC_NOTIFY_MIN_DURATION`].
|
||||
/// Saving one document at a time would otherwise notify on every save.
|
||||
const SYNC_NOTIFY_MIN_FILES: usize = 5;
|
||||
const SYNC_NOTIFY_MIN_DURATION: Duration = Duration::from_secs(30);
|
||||
|
||||
/// One rendered pin row, retained so [`repaint_pins`] can flip the unpin button's
|
||||
/// `sensitive` in place (when the pin set is unchanged) instead of rebuilding.
|
||||
pub(crate) struct PinRow {
|
||||
pub(crate) row: adw::ActionRow,
|
||||
pub(crate) row: gtk4::ListBoxRow,
|
||||
/// The unpin button, absent on the placeholder row.
|
||||
pub(crate) unpin: Option<gtk4::Button>,
|
||||
}
|
||||
@@ -121,6 +140,7 @@ pub(crate) struct MainWidgets {
|
||||
pub(crate) account_name: gtk4::Label,
|
||||
pub(crate) avatar: adw::Avatar,
|
||||
pub(crate) status_icon: gtk4::Image,
|
||||
pub(crate) status_spinner: Spinner,
|
||||
pub(crate) status_title: gtk4::Label,
|
||||
pub(crate) status_detail: gtk4::Label,
|
||||
pub(crate) quota_box: gtk4::Box,
|
||||
@@ -178,9 +198,7 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
mountpoint_row.add_css_class("property");
|
||||
let mountpoint_button = gtk4::Button::builder()
|
||||
.label(gettext("Change…"))
|
||||
.tooltip_text(gettext(
|
||||
"Choose a different folder for the Proton Drive mount",
|
||||
))
|
||||
.tooltip_text(gettext("Choose where My files appears on this computer"))
|
||||
.valign(gtk4::Align::Center)
|
||||
.build();
|
||||
mountpoint_button.add_css_class("flat");
|
||||
@@ -310,7 +328,10 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
prefs.add(&storage);
|
||||
|
||||
// ---- Sidebar footer
|
||||
let status_icon = gtk4::Image::from_icon_name("content-loading-symbolic");
|
||||
// Until the first status arrives there is no state to show, only that the
|
||||
// app is still asking.
|
||||
let status_icon = gtk4::Image::builder().visible(false).build();
|
||||
let status_spinner = spinner();
|
||||
let status_title = gtk4::Label::builder()
|
||||
.label(gettext("Connecting…"))
|
||||
.halign(gtk4::Align::Start)
|
||||
@@ -329,6 +350,7 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
status_text.append(&status_title);
|
||||
status_text.append(&status_detail);
|
||||
let status_content = gtk4::Box::new(gtk4::Orientation::Horizontal, 10);
|
||||
status_content.append(&status_spinner);
|
||||
status_content.append(&status_icon);
|
||||
status_content.append(&status_text);
|
||||
let status_button = gtk4::Button::builder()
|
||||
@@ -390,6 +412,7 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
account_name,
|
||||
avatar,
|
||||
status_icon,
|
||||
status_spinner,
|
||||
status_title,
|
||||
status_detail,
|
||||
quota_box,
|
||||
@@ -491,7 +514,17 @@ pub(crate) fn wire_settings(
|
||||
ui.status
|
||||
.mountpoint_row
|
||||
.set_subtitle(&ui.dirs.resolved_mountpoint(&config).display().to_string());
|
||||
ui.status.autostart_row.set_active(service::is_enabled());
|
||||
// systemctl can take a while, so ask off the main thread and fill the
|
||||
// switch in when it answers, without firing its change handler.
|
||||
let ui_auto = ui.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
let enabled = gio::spawn_blocking(service::is_enabled)
|
||||
.await
|
||||
.unwrap_or(false);
|
||||
let suppress = ui_auto.status.settings_suppress.replace(true);
|
||||
ui_auto.status.autostart_row.set_active(enabled);
|
||||
ui_auto.status.settings_suppress.set(suppress);
|
||||
});
|
||||
ui.status
|
||||
.theme_row
|
||||
.set_active(config.proton_theme.unwrap_or(false));
|
||||
@@ -587,11 +620,14 @@ pub(crate) fn wire_settings(
|
||||
if ui_auto.status.settings_suppress.get() {
|
||||
return;
|
||||
}
|
||||
if row.is_active() {
|
||||
service::enable();
|
||||
} else {
|
||||
service::disable();
|
||||
}
|
||||
let enable = row.is_active();
|
||||
gio::spawn_blocking(move || {
|
||||
if enable {
|
||||
service::enable();
|
||||
} else {
|
||||
service::disable();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let ui_mp = ui.clone();
|
||||
@@ -606,8 +642,10 @@ pub(crate) fn wire_settings(
|
||||
let on = row.is_active();
|
||||
set_proton_theme(on);
|
||||
// Folders swap their icon with the theme; have every row bound again.
|
||||
let rows = ui_theme.browser.model.n_items();
|
||||
ui_theme.browser.model.items_changed(0, rows, rows);
|
||||
for list in file_lists(&ui_theme) {
|
||||
let rows = list.model.n_items();
|
||||
list.model.items_changed(0, rows, rows);
|
||||
}
|
||||
let mut config = ui_theme.dirs.load_config();
|
||||
config.proton_theme = Some(on);
|
||||
if let Err(e) = ui_theme.dirs.save_config(&config) {
|
||||
@@ -706,7 +744,11 @@ pub(crate) fn settings_request(ui: &Rc<Ui>, req: Request, done: String, failed:
|
||||
Ok(Ok(Response::Error { message, kind })) => {
|
||||
toast_failure(&ui, &failed, &message, kind)
|
||||
}
|
||||
_ => toast_error(&ui, &failed, &gettext("The mount service didn't respond.")),
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&failed,
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -716,7 +758,7 @@ pub(crate) fn settings_request(ui: &Rc<Ui>, req: Request, done: String, failed:
|
||||
pub(crate) fn prompt_mountpoint(ui: &Rc<Ui>) {
|
||||
let win = ui_window(ui);
|
||||
let dialog = gtk4::FileDialog::builder()
|
||||
.title(gettext("Choose mountpoint folder"))
|
||||
.title(gettext("Choose Proton Drive Folder"))
|
||||
.build();
|
||||
let ui = ui.clone();
|
||||
dialog.select_folder(win.as_ref(), gio::Cancellable::NONE, move |res| {
|
||||
@@ -728,34 +770,34 @@ pub(crate) fn prompt_mountpoint(ui: &Rc<Ui>) {
|
||||
let mut config = ui.dirs.load_config();
|
||||
config.mountpoint = Some(path_str.clone());
|
||||
if let Err(e) = ui.dirs.save_config(&config) {
|
||||
toast_error(&ui, &gettext("Couldn't save mountpoint"), &e.to_string());
|
||||
toast_error(&ui, &gettext("Couldn't save the folder"), &e.to_string());
|
||||
return;
|
||||
}
|
||||
ui.status.mountpoint_row.set_subtitle(&path_str);
|
||||
// The Locations page shows the same path as a row title; its cached rows
|
||||
// are now stale whether or not it is the visible page.
|
||||
ui.locations.loaded_at.set(None);
|
||||
// The Locations page shows the same path as a row title. Any other page
|
||||
// reads it again on the next visit.
|
||||
if ui.stack.visible_child_name().as_deref() == Some("locations") {
|
||||
load_locations(&ui);
|
||||
}
|
||||
|
||||
// The daemon only reads the mountpoint at mount time, so offer a restart.
|
||||
let confirm = adw::AlertDialog::builder()
|
||||
.heading(gettext("Restart to apply"))
|
||||
.heading(gettext("Restart Proton Drive?"))
|
||||
// Translators: {path} is the folder the drive is mounted at.
|
||||
.body(gettext_f(
|
||||
"The mountpoint is now “{path}”. Restart the Drive mount to use it?",
|
||||
"My files now appears in “{path}” after Proton Drive restarts.",
|
||||
&[("path", &path_str)],
|
||||
))
|
||||
.build();
|
||||
confirm.add_response("later", &gettext("Later"));
|
||||
confirm.add_response("restart", &gettext("Restart now"));
|
||||
confirm.add_response("restart", &gettext("Restart Now"));
|
||||
confirm.set_response_appearance("restart", adw::ResponseAppearance::Suggested);
|
||||
confirm.set_default_response(Some("restart"));
|
||||
confirm.set_close_response("later");
|
||||
confirm.connect_response(None, |_, resp| {
|
||||
let ui_restart = ui.clone();
|
||||
confirm.connect_response(None, move |_, resp| {
|
||||
if resp == "restart" {
|
||||
service::restart();
|
||||
restart_service_then(&ui_restart, |_| {});
|
||||
}
|
||||
});
|
||||
confirm.present(ui_window(&ui).as_ref());
|
||||
@@ -767,26 +809,27 @@ pub(crate) fn prompt_mountpoint(ui: &Rc<Ui>) {
|
||||
/// reload the page.
|
||||
pub(crate) fn wire_retry(ui: &Rc<Ui>) {
|
||||
let ui_browser = ui.clone();
|
||||
ui.browser.retry.clone().connect_clicked(move |_| {
|
||||
service::restart();
|
||||
load_browser(&ui_browser);
|
||||
});
|
||||
ui.browser
|
||||
.retry
|
||||
.clone()
|
||||
.connect_clicked(move |_| restart_service_then(&ui_browser, load_browser));
|
||||
let ui_gallery = ui.clone();
|
||||
ui.gallery.retry.clone().connect_clicked(move |_| {
|
||||
service::restart();
|
||||
load_gallery(&ui_gallery, false);
|
||||
});
|
||||
ui.gallery
|
||||
.retry
|
||||
.clone()
|
||||
.connect_clicked(move |_| restart_service_then(&ui_gallery, |ui| load_gallery(ui, false)));
|
||||
let ui_trash = ui.clone();
|
||||
ui.trash.retry.clone().connect_clicked(move |_| {
|
||||
service::restart();
|
||||
load_trash(&ui_trash);
|
||||
});
|
||||
ui.trash
|
||||
.retry
|
||||
.clone()
|
||||
.connect_clicked(move |_| restart_service_then(&ui_trash, load_trash));
|
||||
}
|
||||
|
||||
/// Repaint the window from the cached login identity, then kick an async mount-
|
||||
/// status fetch. Runs on the 2s tick: the identity check is instant (no keyring),
|
||||
/// and the status round-trip is offloaded to a worker so the main loop never
|
||||
/// blocks on a slow or wedged daemon.
|
||||
/// Repaint the window from the cached login identity, then ask the daemon for
|
||||
/// its status and transfers. Runs at startup, after a login and after a service
|
||||
/// restart; the daemon's event feed keeps both current in between. The identity
|
||||
/// check is instant (no keyring), and the round-trips run on workers so the
|
||||
/// main loop never blocks on a slow or wedged daemon.
|
||||
pub(crate) fn refresh(ui: &Rc<Ui>) {
|
||||
// Login identity decides which page is shown. Read the cached session — set
|
||||
// at startup and on login/logout — never the keyring.
|
||||
@@ -819,38 +862,14 @@ pub(crate) fn refresh(ui: &Rc<Ui>) {
|
||||
|
||||
refresh_status(ui);
|
||||
refresh_transfers(ui);
|
||||
// Both of these pages show work as it happens, so they follow the tick while
|
||||
// they are on screen. Every other page loads on navigation only.
|
||||
// A Takeout import outlives the page that started it, so it is polled
|
||||
// wherever the user has navigated to — otherwise leaving the page means
|
||||
// never being told it finished.
|
||||
ui.gallery
|
||||
.import_banner
|
||||
.set_revealed(ui.takeout.running.get());
|
||||
if ui.stack.visible_child_name().as_deref() == Some("takeout") || ui.takeout.running.get() {
|
||||
refresh_takeout(ui);
|
||||
}
|
||||
// The quota sits in the sidebar footer, visible on every page; its TTL
|
||||
// keeps this from asking more than once a minute.
|
||||
refresh_quota(ui);
|
||||
match ui.stack.visible_child_name().as_deref() {
|
||||
Some("locations") => {
|
||||
refresh_locations(ui);
|
||||
refresh_queue(ui);
|
||||
refresh_conflicts(ui, false);
|
||||
}
|
||||
Some("activity") => refresh_activity(ui),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// How long a quota reading stays fresh. Account storage barely moves, so the
|
||||
/// active-page tick refetches it only this often rather than every 2s.
|
||||
/// How long a quota reading stays fresh. Account storage barely moves, so a
|
||||
/// status change reads it again only this often.
|
||||
const QUOTA_TTL: Duration = Duration::from_secs(60);
|
||||
|
||||
/// Fetch the account quota (if the last reading is stale) and paint both the
|
||||
/// sidebar footer and Files status bar. A failed fetch leaves the last
|
||||
/// good reading in place.
|
||||
/// Fetch the account quota (if the last reading is stale) and paint the
|
||||
/// sidebar footer. A failed fetch leaves the last good reading in place.
|
||||
pub(crate) fn refresh_quota(ui: &Rc<Ui>) {
|
||||
if ui.status.quota_inflight.get() {
|
||||
return;
|
||||
@@ -874,10 +893,6 @@ pub(crate) fn refresh_quota(ui: &Rc<Ui>) {
|
||||
ui.status.quota_checked_at.set(Some(Instant::now()));
|
||||
paint_account_quota(&ui, max_space, used_space);
|
||||
ui.status.quota_box.set_visible(true);
|
||||
} else if ui.status.quota_checked_at.get().is_none() {
|
||||
// Match Dolphin: capacity information does not occupy the bar until
|
||||
// the backing observer has real figures.
|
||||
ui.browser.quota_box.set_visible(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -886,15 +901,6 @@ fn paint_account_quota(ui: &Rc<Ui>, max_space: i64, used_space: i64) {
|
||||
let (fraction, text) = quota_display(max_space, used_space);
|
||||
ui.status.quota_bar.set_fraction(fraction);
|
||||
ui.status.quota_label.set_text(&text);
|
||||
if let Some((fraction, free_text, tooltip)) = quota_status_display(max_space, used_space) {
|
||||
ui.browser.quota.set_fraction(fraction);
|
||||
ui.browser.quota.set_tooltip_text(Some(&tooltip));
|
||||
ui.browser.quota_text.set_label(&free_text);
|
||||
ui.browser.quota_text.set_tooltip_text(Some(&tooltip));
|
||||
ui.browser.quota_box.set_visible(true);
|
||||
} else {
|
||||
ui.browser.quota_box.set_visible(false);
|
||||
}
|
||||
}
|
||||
|
||||
fn quota_display(max_space: i64, used_space: i64) -> (f64, String) {
|
||||
@@ -924,34 +930,6 @@ fn quota_display(max_space: i64, used_space: i64) -> (f64, String) {
|
||||
)
|
||||
}
|
||||
|
||||
/// Dolphin's status bar shows a bare capacity bar followed by “X free”; the
|
||||
/// full free/total/percentage sentence is a tooltip rather than inline bar text.
|
||||
fn quota_status_display(max_space: i64, used_space: i64) -> Option<(f64, String, String)> {
|
||||
if max_space <= 0 {
|
||||
return None;
|
||||
}
|
||||
let total = max_space as u64;
|
||||
let used = (used_space.max(0) as u64).min(total);
|
||||
let free = total.saturating_sub(used);
|
||||
let fraction = used as f64 / total as f64;
|
||||
let pct = (fraction * 100.0).round() as u64;
|
||||
Some((
|
||||
fraction,
|
||||
// Translators: {size} is an amount of storage, such as "1.2 GiB".
|
||||
gettext_f("{size} free", &[("size", &human_bytes(free))]),
|
||||
// Translators: account storage. {free} and {total} are sizes such as
|
||||
// "1.2 GiB"; {percent} is a whole number, followed by the percent sign.
|
||||
gettext_f(
|
||||
"{free} free out of {total} ({percent}% used)",
|
||||
&[
|
||||
("free", &human_bytes(free)),
|
||||
("total", &human_bytes(total)),
|
||||
("percent", &pct.to_string()),
|
||||
],
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
/// Record the mount state seen by the last status poll: gate every control that
|
||||
/// needs a live daemon, and notify the desktop when the state actually flips.
|
||||
///
|
||||
@@ -981,20 +959,20 @@ pub(crate) fn set_mounted(ui: &Rc<Ui>, mounted: bool) {
|
||||
notify(
|
||||
"mount-state",
|
||||
&gettext("Proton Drive connected"),
|
||||
&gettext("Your Drive is mounted and available."),
|
||||
&gettext("Your files are available."),
|
||||
);
|
||||
} else {
|
||||
notify(
|
||||
"mount-state",
|
||||
&gettext("Proton Drive disconnected"),
|
||||
&gettext("The mount service stopped. Files aren't available until it restarts."),
|
||||
&gettext("The Proton Drive service stopped. Files aren't available until it restarts."),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Poll the daemon's in-flight transfers on a worker thread and repaint the
|
||||
/// Ask the daemon for its in-flight transfers on a worker thread and repaint the
|
||||
/// Activity group. Independently inflight-guarded from [`refresh_status`] so the
|
||||
/// two cheap polls on the 2s tick don't gate each other. The group hides itself
|
||||
/// two requests don't gate each other; the event feed pushes later snapshots. The group hides itself
|
||||
/// when nothing is moving, so an idle account shows no Activity section.
|
||||
pub(crate) fn refresh_transfers(ui: &Rc<Ui>) {
|
||||
if ui.status.transfers_inflight.get() {
|
||||
@@ -1036,17 +1014,35 @@ pub(crate) fn repaint_transfers(ui: &Rc<Ui>, items: &[TransferItem], jobs: &[Job
|
||||
takeout_progress(ui, jobs);
|
||||
|
||||
let previous = ui.status.active_transfers.replace(items.len());
|
||||
if !items.is_empty() {
|
||||
let mut batch = ui.status.transfer_batch.borrow_mut();
|
||||
let batch = batch.get_or_insert_with(|| TransferBatch {
|
||||
started: Instant::now(),
|
||||
names: HashSet::new(),
|
||||
});
|
||||
batch
|
||||
.names
|
||||
.extend(items.iter().map(|item| item.name.clone()));
|
||||
}
|
||||
if items.is_empty() && previous > 0 {
|
||||
notify(
|
||||
"sync-complete",
|
||||
&gettext("Sync complete"),
|
||||
&ngettext_f(
|
||||
"{n} file finished transferring.",
|
||||
"{n} files finished transferring.",
|
||||
previous as u64,
|
||||
&[],
|
||||
),
|
||||
);
|
||||
// Nobody needs a notification for what they are watching happen.
|
||||
let watching = ui_window(ui).is_some_and(|window| window.is_active());
|
||||
if let Some(batch) = ui.status.transfer_batch.take()
|
||||
&& !watching
|
||||
&& (batch.names.len() >= SYNC_NOTIFY_MIN_FILES
|
||||
|| batch.started.elapsed() >= SYNC_NOTIFY_MIN_DURATION)
|
||||
{
|
||||
notify(
|
||||
"sync-complete",
|
||||
&gettext("Sync complete"),
|
||||
&ngettext_f(
|
||||
"{n} file finished transferring.",
|
||||
"{n} files finished transferring.",
|
||||
batch.names.len() as u64,
|
||||
&[],
|
||||
),
|
||||
);
|
||||
}
|
||||
// A just-finished batch may have added files (bulk upload) the current
|
||||
// listing doesn't show yet; refresh whichever listing is on screen.
|
||||
reload_listing(ui);
|
||||
@@ -1073,7 +1069,11 @@ pub(crate) fn repaint_transfers(ui: &Rc<Ui>, items: &[TransferItem], jobs: &[Job
|
||||
let row_box = gtk4::Box::new(gtk4::Orientation::Vertical, 4);
|
||||
row_box.set_margin_top(8);
|
||||
row_box.set_margin_bottom(8);
|
||||
let label = gtk4::Label::builder().halign(gtk4::Align::Start).build();
|
||||
// Ellipsized so a long file name cannot widen the page.
|
||||
let label = gtk4::Label::builder()
|
||||
.halign(gtk4::Align::Start)
|
||||
.ellipsize(gtk4::pango::EllipsizeMode::Middle)
|
||||
.build();
|
||||
label.add_css_class("dim-label");
|
||||
let bar = gtk4::ProgressBar::new();
|
||||
row_box.append(&label);
|
||||
@@ -1169,7 +1169,10 @@ pub(crate) fn transfer_line(t: &TransferItem) -> ActivityLine {
|
||||
/// the mount line, cache bar and pin list on the reply. The daemon owns the cache
|
||||
/// stats now (`used`/`budget`/`pins` ride along on [`Response::Status`]), so the
|
||||
/// GUI never opens the on-disk cache itself. Skipped while a fetch is in flight so
|
||||
/// the tick can't stack threads on a stalled daemon.
|
||||
/// a stalled daemon can't pile up threads.
|
||||
///
|
||||
/// The event feed pushes every later change through [`paint_status`]; this asks
|
||||
/// only when the feed (re)connects or loses the daemon.
|
||||
pub(crate) fn refresh_status(ui: &Rc<Ui>) {
|
||||
if ui.status.status_inflight.get() {
|
||||
return;
|
||||
@@ -1181,75 +1184,102 @@ pub(crate) fn refresh_status(ui: &Rc<Ui>) {
|
||||
let result = rx.recv().await;
|
||||
ui.status.status_inflight.set(false);
|
||||
match result {
|
||||
Ok(Ok(Response::Status {
|
||||
mountpoint,
|
||||
used,
|
||||
budget,
|
||||
pins,
|
||||
online,
|
||||
pending_uploads,
|
||||
pending_changes,
|
||||
failing_ops,
|
||||
failing_error,
|
||||
paused,
|
||||
paused_until,
|
||||
..
|
||||
})) => {
|
||||
set_mounted(&ui, true);
|
||||
// The queue is the more useful thing to say when it has anything
|
||||
// in it: it is why a file that looks saved is not on the remote
|
||||
// yet, and offline is usually the reason it is still queued.
|
||||
let queued = i18n::pending_summary(pending_uploads, pending_changes);
|
||||
let state = if paused {
|
||||
SyncState::Paused {
|
||||
until: paused_until,
|
||||
queued,
|
||||
}
|
||||
} else if failing_ops > 0 {
|
||||
SyncState::Attention {
|
||||
count: failing_ops,
|
||||
error: failing_error,
|
||||
}
|
||||
} else if !online {
|
||||
SyncState::Offline { queued }
|
||||
} else if let Some(queued) = queued {
|
||||
SyncState::Syncing { queued }
|
||||
} else {
|
||||
SyncState::UpToDate { mountpoint }
|
||||
};
|
||||
paint_sync_status(&ui, state);
|
||||
let fraction = if budget == 0 {
|
||||
0.0
|
||||
} else {
|
||||
(used as f64 / budget as f64).min(1.0)
|
||||
};
|
||||
ui.status.cache_bar.set_fraction(fraction);
|
||||
// A 0 budget means *unlimited*, not a zero-byte cap — "of 0 B
|
||||
// used" reads as a broken read-out, and there is no fraction to
|
||||
// draw against no limit.
|
||||
ui.status.cache_label.set_text(&if budget == 0 {
|
||||
format!("{} cached — no limit set", human_bytes(used))
|
||||
} else {
|
||||
format!("{} of {} used", human_bytes(used), human_bytes(budget))
|
||||
});
|
||||
repaint_pins(&ui, &pins, true);
|
||||
}
|
||||
// Daemon unreachable (still starting, or down): report not-mounted and
|
||||
// grey out the unpin buttons in place, but leave the last-known pin
|
||||
// rows and cache read-out so the page doesn't flicker on a blip.
|
||||
_ => {
|
||||
set_mounted(&ui, false);
|
||||
paint_sync_status(&ui, SyncState::Disconnected);
|
||||
for r in ui.status.pin_rows.borrow().iter() {
|
||||
if let Some(b) = &r.unpin {
|
||||
b.set_sensitive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Ok(status @ Response::Status { .. })) => paint_status(&ui, status),
|
||||
// The daemon is up but stuck. Say so, and keep the mount state:
|
||||
// announcing "disconnected" for a slow reply would be wrong, and
|
||||
// the feed asks again when it reconnects.
|
||||
Ok(Err(e)) if e.is_timeout() => paint_sync_status(&ui, SyncState::NotResponding),
|
||||
_ => paint_disconnected(&ui),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Paint a [`Response::Status`], fetched or pushed: the mount state, the sync
|
||||
/// status, the cache bar and the pin list.
|
||||
pub(crate) fn paint_status(ui: &Rc<Ui>, status: Response) {
|
||||
let Response::Status {
|
||||
mountpoint,
|
||||
used,
|
||||
budget,
|
||||
pins,
|
||||
online,
|
||||
pending_uploads,
|
||||
pending_changes,
|
||||
failing_ops,
|
||||
failing_error,
|
||||
paused,
|
||||
paused_until,
|
||||
..
|
||||
} = status
|
||||
else {
|
||||
return;
|
||||
};
|
||||
set_mounted(ui, true);
|
||||
// The queue is the more useful thing to say when it has anything
|
||||
// in it: it is why a file that looks saved is not on the remote
|
||||
// yet, and offline is usually the reason it is still queued.
|
||||
let queued = i18n::pending_summary(pending_uploads, pending_changes);
|
||||
let state = if paused {
|
||||
SyncState::Paused {
|
||||
until: paused_until,
|
||||
queued,
|
||||
}
|
||||
} else if failing_ops > 0 {
|
||||
SyncState::Attention {
|
||||
count: failing_ops,
|
||||
error: failing_error,
|
||||
}
|
||||
} else if !online {
|
||||
SyncState::Offline { queued }
|
||||
} else if let Some(queued) = queued {
|
||||
SyncState::Syncing { queued }
|
||||
} else {
|
||||
SyncState::UpToDate { mountpoint }
|
||||
};
|
||||
paint_sync_status(ui, state);
|
||||
ui.locations.failing.set(failing_ops);
|
||||
paint_sync_banner(ui);
|
||||
let fraction = if budget == 0 {
|
||||
0.0
|
||||
} else {
|
||||
(used as f64 / budget as f64).min(1.0)
|
||||
};
|
||||
ui.status.cache_bar.set_fraction(fraction);
|
||||
// A 0 budget means *unlimited*, not a zero-byte cap — "of 0 B
|
||||
// used" reads as a broken read-out, and there is no fraction to
|
||||
// draw against no limit.
|
||||
ui.status.cache_label.set_text(&if budget == 0 {
|
||||
// Translators: {used} is a size such as "1.2 GiB".
|
||||
gettext_f(
|
||||
"{used} cached — no limit set",
|
||||
&[("used", &human_bytes(used))],
|
||||
)
|
||||
} else {
|
||||
// Translators: {used} and {limit} are sizes such as "1.2 GiB".
|
||||
gettext_f(
|
||||
"{used} of {limit} used",
|
||||
&[
|
||||
("used", &human_bytes(used)),
|
||||
("limit", &human_bytes(budget)),
|
||||
],
|
||||
)
|
||||
});
|
||||
repaint_pins(ui, &pins, true);
|
||||
}
|
||||
|
||||
/// Daemon unreachable (still starting, or down): report not-mounted and grey
|
||||
/// out the unpin buttons in place, but leave the last-known pin rows and cache
|
||||
/// read-out so the page doesn't flicker on a blip.
|
||||
pub(crate) fn paint_disconnected(ui: &Rc<Ui>) {
|
||||
set_mounted(ui, false);
|
||||
paint_sync_status(ui, SyncState::Disconnected);
|
||||
for r in ui.status.pin_rows.borrow().iter() {
|
||||
if let Some(b) = &r.unpin {
|
||||
b.set_sensitive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What the sidebar's status strip reports, most urgent first.
|
||||
pub(crate) enum SyncState {
|
||||
/// The user paused syncing; nothing goes up until it resumes. Outranks
|
||||
@@ -1277,13 +1307,15 @@ pub(crate) enum SyncState {
|
||||
},
|
||||
/// No mount daemon answered: still starting, or the service is down.
|
||||
Disconnected,
|
||||
/// The daemon is running but didn't answer in time.
|
||||
NotResponding,
|
||||
}
|
||||
|
||||
/// Paint the sidebar status strip and the Sync page's status card: icon,
|
||||
/// one-line state, and a detail line.
|
||||
fn paint_sync_status(ui: &Rc<Ui>, state: SyncState) {
|
||||
let paused = matches!(state, SyncState::Paused { .. });
|
||||
let connected = !matches!(state, SyncState::Disconnected);
|
||||
let connected = !matches!(state, SyncState::Disconnected | SyncState::NotResponding);
|
||||
let (icon, class, title, detail) = match state {
|
||||
SyncState::Paused { until, queued } => (
|
||||
"media-playback-pause-symbolic",
|
||||
@@ -1321,13 +1353,13 @@ fn paint_sync_status(ui: &Rc<Ui>, state: SyncState) {
|
||||
Some(queued.unwrap_or_else(|| gettext("Cached files only"))),
|
||||
),
|
||||
SyncState::Syncing { queued } => (
|
||||
"emblem-synchronizing-symbolic",
|
||||
"pdfs-sync-symbolic",
|
||||
None,
|
||||
pgettext("state", "Syncing"),
|
||||
Some(queued),
|
||||
),
|
||||
SyncState::UpToDate { mountpoint } => (
|
||||
"emblem-ok-symbolic",
|
||||
"pdfs-synced-symbolic",
|
||||
Some("success"),
|
||||
gettext("Up to date"),
|
||||
Some(mountpoint),
|
||||
@@ -1338,9 +1370,17 @@ fn paint_sync_status(ui: &Rc<Ui>, state: SyncState) {
|
||||
gettext("Not connected"),
|
||||
Some(gettext("Proton Drive isn't running")),
|
||||
),
|
||||
SyncState::NotResponding => (
|
||||
"dialog-warning-symbolic",
|
||||
Some("warning"),
|
||||
gettext("Not responding"),
|
||||
Some(gettext("Proton Drive is taking too long to answer")),
|
||||
),
|
||||
};
|
||||
let image = &ui.status.status_icon;
|
||||
image.set_icon_name(Some(icon));
|
||||
image.set_visible(true);
|
||||
ui.status.status_spinner.set_visible(false);
|
||||
for c in ["success", "warning", "error"] {
|
||||
image.remove_css_class(c);
|
||||
}
|
||||
@@ -1419,10 +1459,10 @@ pub(crate) fn repaint_pins(ui: &Rc<Ui>, pins: &[pdfs_core::cache::Pin], mounted:
|
||||
))
|
||||
.build();
|
||||
ui.status.pins_group.add(&row);
|
||||
ui.status
|
||||
.pin_rows
|
||||
.borrow_mut()
|
||||
.push(PinRow { row, unpin: None });
|
||||
ui.status.pin_rows.borrow_mut().push(PinRow {
|
||||
row: row.upcast(),
|
||||
unpin: None,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1474,7 +1514,7 @@ pub(crate) fn repaint_pins(ui: &Rc<Ui>, pins: &[pdfs_core::cache::Pin], mounted:
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&gettext("Couldn't make it online only"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -1483,32 +1523,29 @@ pub(crate) fn repaint_pins(ui: &Rc<Ui>, pins: &[pdfs_core::cache::Pin], mounted:
|
||||
|
||||
ui.status.pins_group.add(&row);
|
||||
ui.status.pin_rows.borrow_mut().push(PinRow {
|
||||
row,
|
||||
row: row.upcast(),
|
||||
unpin: Some(unpin),
|
||||
});
|
||||
}
|
||||
|
||||
if pins.len() > PINS_COLLAPSED {
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(if expanded {
|
||||
gettext("Show fewer")
|
||||
} else {
|
||||
ngettext_f(
|
||||
"Show all {n} file",
|
||||
"Show all {n} files",
|
||||
pins.len() as u64,
|
||||
&[],
|
||||
)
|
||||
})
|
||||
.activatable(true)
|
||||
.build();
|
||||
row.add_suffix(>k4::Image::from_icon_name(if expanded {
|
||||
let title = if expanded {
|
||||
gettext("Show fewer")
|
||||
} else {
|
||||
ngettext_f(
|
||||
"Show all {n} file",
|
||||
"Show all {n} files",
|
||||
pins.len() as u64,
|
||||
&[],
|
||||
)
|
||||
};
|
||||
let icon = if expanded {
|
||||
"go-up-symbolic"
|
||||
} else {
|
||||
"go-down-symbolic"
|
||||
}));
|
||||
};
|
||||
let ui_more = ui.clone();
|
||||
row.connect_activated(move |_| {
|
||||
let row = button_row(&title, icon, move || {
|
||||
ui_more
|
||||
.status
|
||||
.pins_expanded
|
||||
@@ -1529,7 +1566,7 @@ pub(crate) fn repaint_pins(ui: &Rc<Ui>, pins: &[pdfs_core::cache::Pin], mounted:
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{quota_display, quota_status_display};
|
||||
use super::quota_display;
|
||||
|
||||
#[test]
|
||||
fn quota_display_reports_used_total_and_percentage() {
|
||||
@@ -1544,18 +1581,4 @@ mod tests {
|
||||
assert_eq!(quota_display(0, -1), (0.0, "0 bytes used".to_string()));
|
||||
assert_eq!(quota_display(100, 150).0, 1.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quota_status_display_matches_dolphin_wording() {
|
||||
let gib = 1024_i64.pow(3);
|
||||
assert_eq!(
|
||||
quota_status_display(4 * gib, gib),
|
||||
Some((
|
||||
0.25,
|
||||
"3.0 GiB free".to_string(),
|
||||
"3.0 GiB free out of 4.0 GiB (25% used)".to_string()
|
||||
))
|
||||
);
|
||||
assert_eq!(quota_status_display(0, 0), None);
|
||||
}
|
||||
}
|
||||
@@ -15,16 +15,16 @@
|
||||
//! Import — staging is local, so a mis-drop costs nothing.
|
||||
//!
|
||||
//! The daemon owns the run itself ([`Request::ImportTakeout`]): this page only
|
||||
//! stages paths, launches, polls [`Request::ImportStatus`] for the final report,
|
||||
//! stages paths, launches, asks [`Request::ImportStatus`] for the final report,
|
||||
//! and reads live progress off the import's job in the transfer snapshot the
|
||||
//! status tick already fetches (see [`takeout_progress`]).
|
||||
//! daemon already pushes (see [`takeout_progress`]).
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// The title the daemon gives the import's job in [`Request::GetQueueStatus`].
|
||||
/// Matching on it is how this page borrows the progress the status tick is
|
||||
/// already polling instead of adding a second poll of its own.
|
||||
const IMPORT_JOB: &str = "Importing Google Photos";
|
||||
/// Matching on it is how this page borrows the progress the daemon already
|
||||
/// pushes instead of asking for it separately.
|
||||
pub(crate) const IMPORT_JOB: &str = "Importing Google Photos";
|
||||
|
||||
/// What the archive list says before anything is staged. `repaint_archives`
|
||||
/// swaps in the count and total once there is something to report, and swaps
|
||||
@@ -384,7 +384,7 @@ pub(crate) fn wire_takeout(ui: &Rc<Ui>, widgets: &TakeoutWidgets) {
|
||||
let count = ui_import.takeout.archives.borrow().len();
|
||||
let ui = ui_import.clone();
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Import this export?"))
|
||||
.heading(gettext("Import This Export?"))
|
||||
.body(ngettext_f(
|
||||
"{n} archive will be uploaded to Proton Photos, creating albums as they appear in the export. This can take hours; you can stop it at any point and what has been uploaded stays. Photos you already have are skipped.",
|
||||
"{n} archives will be uploaded to Proton Photos, creating albums as they appear in the export. This can take hours; you can stop it at any point and what has been uploaded stays. Photos you already have are skipped.",
|
||||
@@ -421,7 +421,7 @@ pub(crate) fn wire_takeout(ui: &Rc<Ui>, widgets: &TakeoutWidgets) {
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&gettext("Couldn't stop the import"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -589,6 +589,7 @@ fn start_import(ui: &Rc<Ui>, dry_run: bool) {
|
||||
match result {
|
||||
Ok(Ok(Response::Ok { .. })) => {
|
||||
ui.takeout.running.set(true);
|
||||
ui.gallery.import_banner.set_revealed(true);
|
||||
ui.takeout.summary_group.set_visible(false);
|
||||
ui.takeout.progress_group.set_visible(true);
|
||||
ui.takeout.progress_bar.set_fraction(0.0);
|
||||
@@ -613,15 +614,15 @@ fn start_import(ui: &Rc<Ui>, dry_run: bool) {
|
||||
_ => toast_error(
|
||||
&ui,
|
||||
&gettext("Couldn't start the import"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Poll the daemon for the import's state while the page is on screen, and paint
|
||||
/// the report once a run has finished. Cheap and inflight-guarded, like the other
|
||||
/// tick polls.
|
||||
/// Ask the daemon for the import's state, and paint the report once a run has
|
||||
/// finished. Asked on arrival and while import progress keeps arriving. Cheap
|
||||
/// and inflight-guarded.
|
||||
pub(crate) fn refresh_takeout(ui: &Rc<Ui>) {
|
||||
if ui.takeout.inflight.get() {
|
||||
return;
|
||||
@@ -636,6 +637,7 @@ pub(crate) fn refresh_takeout(ui: &Rc<Ui>) {
|
||||
return;
|
||||
};
|
||||
let was_running = ui.takeout.running.replace(running);
|
||||
ui.gallery.import_banner.set_revealed(running);
|
||||
if running {
|
||||
ui.takeout.progress_group.set_visible(true);
|
||||
} else {
|
||||
@@ -776,8 +778,8 @@ fn repaint_summary(ui: &Rc<Ui>, summary: &ImportSummary) {
|
||||
ui.takeout.summary_group.set_visible(true);
|
||||
}
|
||||
|
||||
/// Paint the import's progress from the transfer snapshot the status tick
|
||||
/// already fetched, so this page costs no extra poll. A job with no known total
|
||||
/// Paint the import's progress from the transfer snapshot the daemon pushed,
|
||||
/// so this page costs no extra request. A job with no known total
|
||||
/// pulses instead of showing a fraction, the same as the Activity list.
|
||||
pub(crate) fn takeout_progress(ui: &Rc<Ui>, jobs: &[JobItem]) {
|
||||
let Some(job) = jobs.iter().find(|job| job.title == IMPORT_JOB) else {
|
||||
|
||||
@@ -3,55 +3,51 @@ use crate::*;
|
||||
pub(crate) struct TrashState {
|
||||
// Trash page. Trashed nodes are addressed by uid, not by path, so this page
|
||||
// keeps no current-directory state — it re-lists from the daemon on show.
|
||||
pub(crate) model: gio::ListStore,
|
||||
pub(crate) files: FileList,
|
||||
pub(crate) content: gtk4::Stack,
|
||||
pub(crate) status: adw::StatusPage,
|
||||
pub(crate) retry: gtk4::Button,
|
||||
/// Empties the trash; insensitive while it is empty (or unread).
|
||||
/// Empties the trash; hidden while it is empty (or unread).
|
||||
pub(crate) empty: gtk4::Button,
|
||||
/// "12 items" under the page title.
|
||||
pub(crate) subtitle: adw::WindowTitle,
|
||||
pub(crate) selection: gtk4::MultiSelection,
|
||||
/// The bottom bar acting on the selection; revealed while anything is selected.
|
||||
pub(crate) selection_bar: gtk4::Revealer,
|
||||
pub(crate) selection_label: gtk4::Label,
|
||||
/// Runs the loads; the rows stay up while one runs.
|
||||
pub(crate) loader: Rc<Loader>,
|
||||
}
|
||||
|
||||
/// The widgets of the Trash page that a load repaints.
|
||||
pub(crate) struct TrashWidgets {
|
||||
pub(crate) model: gio::ListStore,
|
||||
pub(crate) list: gtk4::ListView,
|
||||
pub(crate) files: FileList,
|
||||
pub(crate) content: gtk4::Stack,
|
||||
pub(crate) status: adw::StatusPage,
|
||||
pub(crate) retry: gtk4::Button,
|
||||
pub(crate) empty: gtk4::Button,
|
||||
pub(crate) refresh: gtk4::Button,
|
||||
pub(crate) subtitle: adw::WindowTitle,
|
||||
pub(crate) selection: gtk4::MultiSelection,
|
||||
pub(crate) selection_bar: gtk4::Revealer,
|
||||
pub(crate) selection_label: gtk4::Label,
|
||||
pub(crate) restore_selected: gtk4::Button,
|
||||
pub(crate) delete_selected: gtk4::Button,
|
||||
}
|
||||
|
||||
/// The Trash page: a flat list of everything Drive is holding in the trash, each
|
||||
/// row offering Restore and Delete Forever, with Empty Trash in the header. A
|
||||
/// selection (click, Shift/Ctrl+click, Ctrl+A) gets the same two actions in a
|
||||
/// bottom bar.
|
||||
/// The Trash page: everything Drive is holding in the trash, as a list with
|
||||
/// the folder each item came from and when it was deleted, or as a grid, where
|
||||
/// photos are easier to recognise. Restore and Delete Permanently sit in the
|
||||
/// right-click menu and, for a selection, in a bottom bar; Empty Trash is in
|
||||
/// the header while there is something to empty.
|
||||
///
|
||||
/// A trashed node has no path inside the mount — the daemon forgets it when it is
|
||||
/// trashed — so unlike the Files page this one addresses entries by uid and always
|
||||
/// re-lists from the server rather than from a cached listing. Row rendering needs
|
||||
/// the [`Ui`] handle for its buttons, so the factory is installed in [`wire_trash`].
|
||||
/// A trashed node has no path inside the mount — the daemon forgets it when it
|
||||
/// is trashed — so unlike the Files page this one addresses entries by uid. The
|
||||
/// list's factories need the [`Ui`] handle, so they are installed in
|
||||
/// [`wire_trash`].
|
||||
pub(crate) fn build_trash_page() -> (gtk4::Widget, TrashWidgets) {
|
||||
let model = gio::ListStore::new::<BoxedAnyObject>();
|
||||
|
||||
let empty = gtk4::Button::builder()
|
||||
.label(gettext("Empty Trash…"))
|
||||
.tooltip_text(gettext("Permanently delete everything in the Trash"))
|
||||
.sensitive(false)
|
||||
.visible(false)
|
||||
.build();
|
||||
let refresh = refresh_button();
|
||||
|
||||
let retry = gtk4::Button::builder()
|
||||
.label(gettext("Retry"))
|
||||
@@ -67,28 +63,27 @@ pub(crate) fn build_trash_page() -> (gtk4::Widget, TrashWidgets) {
|
||||
.build();
|
||||
status.add_css_class("compact");
|
||||
|
||||
// Bulk Delete Forever always confirms with the count, so a stale selection
|
||||
// can't delete anything silently.
|
||||
let selection = gtk4::MultiSelection::new(Some(model.clone()));
|
||||
let list = gtk4::ListView::builder().model(&selection).build();
|
||||
let scroll = gtk4::ScrolledWindow::builder()
|
||||
.vexpand(true)
|
||||
.child(&list)
|
||||
.build();
|
||||
// The list first: where an item came from and when it went are what tell
|
||||
// two copies of a file apart here.
|
||||
let files = FileList::new();
|
||||
files.show_list(true);
|
||||
let layout = layout_button(&files);
|
||||
|
||||
let content = gtk4::Stack::new();
|
||||
content.set_vexpand(true);
|
||||
content.set_transition_type(gtk4::StackTransitionType::Crossfade);
|
||||
content.add_named(&scroll, Some("list"));
|
||||
content.add_named(&files.views, Some("list"));
|
||||
content.add_named(&status, Some("status"));
|
||||
|
||||
let inner = gtk4::Box::new(gtk4::Orientation::Vertical, 12);
|
||||
inner.set_margin_top(18);
|
||||
inner.set_margin_bottom(18);
|
||||
inner.set_margin_start(18);
|
||||
inner.set_margin_end(18);
|
||||
inner.set_margin_top(12);
|
||||
inner.set_margin_bottom(12);
|
||||
inner.set_margin_start(12);
|
||||
inner.set_margin_end(12);
|
||||
inner.append(&content);
|
||||
|
||||
// Bulk Delete Permanently always confirms with the count, so a stale
|
||||
// selection can't delete anything silently.
|
||||
let selection_label = gtk4::Label::new(None);
|
||||
let restore_selected = gtk4::Button::with_label(&pgettext("verb", "Restore"));
|
||||
let delete_selected = gtk4::Button::with_label(&gettext("Delete Permanently…"));
|
||||
@@ -105,20 +100,17 @@ pub(crate) fn build_trash_page() -> (gtk4::Widget, TrashWidgets) {
|
||||
|
||||
let (frame, header, subtitle) = page_frame(&gettext("Trash"), &inner);
|
||||
header.pack_start(&empty);
|
||||
header.pack_end(&refresh);
|
||||
header.pack_end(&layout);
|
||||
|
||||
(
|
||||
frame.upcast(),
|
||||
TrashWidgets {
|
||||
model,
|
||||
list,
|
||||
files,
|
||||
content,
|
||||
status,
|
||||
retry,
|
||||
empty,
|
||||
refresh,
|
||||
subtitle,
|
||||
selection,
|
||||
selection_bar,
|
||||
selection_label,
|
||||
restore_selected,
|
||||
@@ -127,142 +119,144 @@ pub(crate) fn build_trash_page() -> (gtk4::Widget, TrashWidgets) {
|
||||
)
|
||||
}
|
||||
|
||||
/// Install the row factory and the Empty Trash button. The row's two buttons read
|
||||
/// the entry off the [`gtk4::ListItem`] they were clicked on rather than a
|
||||
/// captured copy, so a recycled row always acts on the item it currently shows.
|
||||
/// Install the list's factories and columns, the Empty Trash button and the
|
||||
/// selection bar.
|
||||
pub(crate) fn wire_trash(ui: &Rc<Ui>, widgets: &TrashWidgets) {
|
||||
let list = &widgets.list;
|
||||
let empty = &widgets.empty;
|
||||
let factory = gtk4::SignalListItemFactory::new();
|
||||
let ui_setup = ui.clone();
|
||||
factory.connect_setup(move |_, item| {
|
||||
let item = item.downcast_ref::<gtk4::ListItem>().unwrap();
|
||||
|
||||
let thumbnail = file_thumbnail_widget(40, 32);
|
||||
let name = gtk4::Label::builder()
|
||||
.halign(gtk4::Align::Start)
|
||||
.ellipsize(gtk4::pango::EllipsizeMode::Middle)
|
||||
.build();
|
||||
let meta = gtk4::Label::builder().halign(gtk4::Align::Start).build();
|
||||
meta.add_css_class("dim-label");
|
||||
meta.add_css_class("caption");
|
||||
let text = gtk4::Box::new(gtk4::Orientation::Vertical, 2);
|
||||
text.set_hexpand(true);
|
||||
text.set_valign(gtk4::Align::Center);
|
||||
text.append(&name);
|
||||
text.append(&meta);
|
||||
|
||||
let restore = gtk4::Button::builder()
|
||||
.icon_name("edit-undo-symbolic")
|
||||
.tooltip_text(gettext("Restore to its original folder"))
|
||||
.valign(gtk4::Align::Center)
|
||||
.build();
|
||||
restore.add_css_class("flat");
|
||||
let purge = gtk4::Button::builder()
|
||||
.icon_name("edit-delete-symbolic")
|
||||
.tooltip_text(gettext("Delete permanently"))
|
||||
.valign(gtk4::Align::Center)
|
||||
.build();
|
||||
purge.add_css_class("flat");
|
||||
|
||||
let ui_restore = ui_setup.clone();
|
||||
let item_restore = item.clone();
|
||||
restore.connect_clicked(move |_| {
|
||||
if let Some(entry) = bound_entry(&item_restore) {
|
||||
restore_entry(&ui_restore, &entry);
|
||||
let files = &ui.trash.files;
|
||||
files.wire(
|
||||
ui,
|
||||
FileListBehavior {
|
||||
activate: activate_trashed,
|
||||
entry_menu: trash_entry_menu,
|
||||
bulk_menu: trash_bulk_menu,
|
||||
background_menu: trash_background_menu,
|
||||
badges: false,
|
||||
drag_and_drop: false,
|
||||
},
|
||||
);
|
||||
files
|
||||
.column_view
|
||||
.append_column(&text_column(&gettext("Original Location"), |e| {
|
||||
trashed_from_label(e)
|
||||
}));
|
||||
files
|
||||
.column_view
|
||||
.append_column(&text_column(&pgettext("column", "Deleted"), |e| {
|
||||
if e.trashed_at > 0 {
|
||||
dates::relative(e.trashed_at)
|
||||
} else {
|
||||
"—".to_string()
|
||||
}
|
||||
});
|
||||
let ui_purge = ui_setup.clone();
|
||||
let item_purge = item.clone();
|
||||
purge.connect_clicked(move |_| {
|
||||
if let Some(entry) = bound_entry(&item_purge) {
|
||||
prompt_delete_forever(&ui_purge, std::slice::from_ref(&entry));
|
||||
}));
|
||||
files
|
||||
.column_view
|
||||
.append_column(&text_column(&pgettext("column", "Size"), |e| {
|
||||
if e.is_dir {
|
||||
"—".to_string()
|
||||
} else {
|
||||
human_bytes(e.size)
|
||||
}
|
||||
});
|
||||
|
||||
let row = gtk4::Box::new(gtk4::Orientation::Horizontal, 12);
|
||||
row.set_margin_top(6);
|
||||
row.set_margin_bottom(6);
|
||||
row.set_margin_start(6);
|
||||
row.set_margin_end(6);
|
||||
row.append(&thumbnail);
|
||||
row.append(&text);
|
||||
row.append(&restore);
|
||||
row.append(&purge);
|
||||
item.set_child(Some(&row));
|
||||
});
|
||||
factory.connect_bind({
|
||||
let ui = ui.clone();
|
||||
move |_, item| {
|
||||
let item = item.downcast_ref::<gtk4::ListItem>().unwrap();
|
||||
let Some(entry) = bound_entry(item) else {
|
||||
return;
|
||||
};
|
||||
let row = item.child().and_downcast::<gtk4::Box>().unwrap();
|
||||
let Some(thumbnail) = row.first_child().and_downcast::<gtk4::Overlay>() else {
|
||||
return;
|
||||
};
|
||||
bind_file_thumbnail(&ui, &thumbnail, &entry, true);
|
||||
let Some(text) = thumbnail.next_sibling().and_downcast::<gtk4::Box>() else {
|
||||
return;
|
||||
};
|
||||
if let Some(name) = text.first_child().and_downcast::<gtk4::Label>() {
|
||||
name.set_label(&entry.name);
|
||||
}
|
||||
if let Some(meta) = text.last_child().and_downcast::<gtk4::Label>() {
|
||||
let kind = if entry.is_dir {
|
||||
gettext("Folder")
|
||||
} else {
|
||||
human_bytes(entry.size)
|
||||
};
|
||||
let modified = format_modified(entry.modified);
|
||||
// Translators: {kind} is "Folder" or a file size; {modified} is a date.
|
||||
let text = gettext_f(
|
||||
"{kind} · {modified}",
|
||||
&[("kind", &kind), ("modified", &modified)],
|
||||
);
|
||||
meta.set_label(&text);
|
||||
}
|
||||
}
|
||||
});
|
||||
list.set_factory(Some(&factory));
|
||||
}));
|
||||
|
||||
let ui_empty = ui.clone();
|
||||
empty.connect_clicked(move |_| prompt_empty_trash(&ui_empty));
|
||||
widgets
|
||||
.empty
|
||||
.connect_clicked(move |_| prompt_empty_trash(&ui_empty));
|
||||
|
||||
let ui_sel = ui.clone();
|
||||
widgets
|
||||
files
|
||||
.selection
|
||||
.connect_selection_changed(move |_, _, _| sync_trash_selection(&ui_sel));
|
||||
let ui_restore = ui.clone();
|
||||
widgets.restore_selected.connect_clicked(move |_| {
|
||||
let entries = selected_trash(&ui_restore);
|
||||
let entries = ui_restore.trash.files.selected();
|
||||
if !entries.is_empty() {
|
||||
restore_entries(&ui_restore, &entries);
|
||||
}
|
||||
});
|
||||
let ui_delete = ui.clone();
|
||||
widgets.delete_selected.connect_clicked(move |_| {
|
||||
let entries = selected_trash(&ui_delete);
|
||||
let entries = ui_delete.trash.files.selected();
|
||||
if !entries.is_empty() {
|
||||
prompt_delete_forever(&ui_delete, &entries);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The trashed entries currently selected, in list order.
|
||||
fn selected_trash(ui: &Rc<Ui>) -> Vec<DirEntry> {
|
||||
let selected = ui.trash.selection.selection();
|
||||
(0..selected.size())
|
||||
.filter_map(|i| ui.trash.model.item(selected.nth(i as u32)))
|
||||
.filter_map(|obj| obj.downcast::<BoxedAnyObject>().ok())
|
||||
.map(|obj| obj.borrow::<DirEntry>().clone())
|
||||
.collect()
|
||||
/// Where a trashed entry goes back to on restore, for the Original Location
|
||||
/// column.
|
||||
fn trashed_from_label(entry: &DirEntry) -> String {
|
||||
match entry.trashed_from.as_deref() {
|
||||
None => "—".to_string(),
|
||||
Some("") => gettext("My files"),
|
||||
Some(path) => path.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A trashed file can't be opened; say what would let it be.
|
||||
fn activate_trashed(ui: &Rc<Ui>, entry: &DirEntry) {
|
||||
toast(
|
||||
ui,
|
||||
// Translators: {name} is a file or folder name.
|
||||
&gettext_f("Restore “{name}” to open it", &[("name", &entry.name)]),
|
||||
);
|
||||
}
|
||||
|
||||
fn trash_entry_menu(ui: &Rc<Ui>, entry: &DirEntry) -> ActionMenu {
|
||||
let mut menu = ActionMenu::new();
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
menu.item(&pgettext("verb", "Restore"), move || {
|
||||
restore_entries(&ui_c, std::slice::from_ref(&entry_c))
|
||||
});
|
||||
menu.section();
|
||||
let (ui_c, entry_c) = (ui.clone(), entry.clone());
|
||||
menu.item(&gettext("Delete Permanently…"), move || {
|
||||
prompt_delete_forever(&ui_c, std::slice::from_ref(&entry_c))
|
||||
});
|
||||
menu
|
||||
}
|
||||
|
||||
fn trash_bulk_menu(ui: &Rc<Ui>, entries: Vec<DirEntry>) -> ActionMenu {
|
||||
let mut menu = ActionMenu::new();
|
||||
menu.labelled_section(&ngettext_f(
|
||||
"{n} selected",
|
||||
"{n} selected",
|
||||
entries.len() as u64,
|
||||
&[],
|
||||
));
|
||||
let (ui_c, batch) = (ui.clone(), entries.clone());
|
||||
menu.item(&pgettext("verb", "Restore"), move || {
|
||||
restore_entries(&ui_c, &batch)
|
||||
});
|
||||
menu.section();
|
||||
let ui_c = ui.clone();
|
||||
menu.item(&gettext("Delete Permanently…"), move || {
|
||||
prompt_delete_forever(&ui_c, &entries)
|
||||
});
|
||||
menu
|
||||
}
|
||||
|
||||
fn trash_background_menu(ui: &Rc<Ui>) -> ActionMenu {
|
||||
let mut menu = ActionMenu::new();
|
||||
let ui_c = ui.clone();
|
||||
menu.item(&gettext("Select All"), move || {
|
||||
ui_c.trash.files.selection.select_all();
|
||||
});
|
||||
let ui_c = ui.clone();
|
||||
menu.item(&gettext("Refresh"), move || reload_current_page(&ui_c));
|
||||
if ui.trash.files.model.n_items() > 0 {
|
||||
menu.section();
|
||||
let ui_c = ui.clone();
|
||||
menu.item(&gettext("Empty Trash…"), move || {
|
||||
prompt_empty_trash(&ui_c)
|
||||
});
|
||||
}
|
||||
menu
|
||||
}
|
||||
|
||||
/// Reveal the selection bar while anything is selected and count what is.
|
||||
fn sync_trash_selection(ui: &Rc<Ui>) {
|
||||
let count = ui.trash.selection.selection().size() as usize;
|
||||
let count = ui.trash.files.selection.selection().size() as usize;
|
||||
ui.trash.selection_bar.set_reveal_child(count > 0);
|
||||
if count > 0 {
|
||||
ui.trash.selection_label.set_label(&ngettext_f(
|
||||
@@ -274,29 +268,24 @@ fn sync_trash_selection(ui: &Rc<Ui>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The [`DirEntry`] a list item is currently bound to, or `None` for an unbound
|
||||
/// (recycled) row.
|
||||
pub(crate) fn bound_entry(item: >k4::ListItem) -> Option<DirEntry> {
|
||||
let obj = item.item().and_downcast::<BoxedAnyObject>()?;
|
||||
let entry = obj.borrow::<DirEntry>().clone();
|
||||
Some(entry)
|
||||
}
|
||||
|
||||
/// Fetch the trash listing and repaint the page.
|
||||
pub(crate) fn load_trash(ui: &Rc<Ui>) {
|
||||
cancel_file_thumbnails(ui);
|
||||
// Drop the old rows first: a stale row here would offer Restore on something
|
||||
// that may already be gone.
|
||||
ui.trash.model.remove_all();
|
||||
ui.trash.empty.set_sensitive(false);
|
||||
sync_trash_selection(ui);
|
||||
trash_status(
|
||||
ui,
|
||||
"user-trash-symbolic",
|
||||
&gettext("Loading…"),
|
||||
&gettext("Reading the trash."),
|
||||
false,
|
||||
);
|
||||
// The rows stay up while the trash is read again. Trashed items are
|
||||
// addressed by uid, so acting on one that has gone meanwhile fails
|
||||
// cleanly rather than hitting something else.
|
||||
let ui_p = ui.clone();
|
||||
let ticket = ui.trash.loader.refresh(move || {
|
||||
clear_trash(&ui_p);
|
||||
ui_p.trash.empty.set_sensitive(false);
|
||||
trash_status(
|
||||
&ui_p,
|
||||
"user-trash-symbolic",
|
||||
&gettext("Loading…"),
|
||||
&gettext("Reading the trash."),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
ui.busy_begin();
|
||||
let rx = spawn_request(ui.dirs.control_socket(), Request::ListTrash);
|
||||
@@ -304,6 +293,12 @@ pub(crate) fn load_trash(ui: &Rc<Ui>) {
|
||||
glib::spawn_future_local(async move {
|
||||
let result = rx.recv().await;
|
||||
ui.busy_end();
|
||||
if !ticket.is_current() {
|
||||
return;
|
||||
}
|
||||
if !matches!(result, Ok(Ok(Response::Entries { .. }))) {
|
||||
clear_trash(&ui);
|
||||
}
|
||||
match result {
|
||||
Ok(Ok(Response::Entries { entries })) => repaint_trash(&ui, &entries),
|
||||
Ok(Ok(Response::Error { message, .. })) => trash_status(
|
||||
@@ -317,7 +312,7 @@ pub(crate) fn load_trash(ui: &Rc<Ui>) {
|
||||
&ui,
|
||||
"dialog-warning-symbolic",
|
||||
&gettext("Couldn't read the trash"),
|
||||
&gettext("Unexpected reply from the mount service."),
|
||||
&gettext("Unexpected reply from the Proton Drive service."),
|
||||
false,
|
||||
),
|
||||
Ok(Err(_)) | Err(_) => trash_unreachable(&ui),
|
||||
@@ -328,29 +323,7 @@ pub(crate) fn load_trash(ui: &Rc<Ui>) {
|
||||
/// The daemon didn't answer. Same split as the Files page: still starting (poll
|
||||
/// again, no button) versus actually down (Retry, which restarts the service).
|
||||
pub(crate) fn trash_unreachable(ui: &Rc<Ui>) {
|
||||
if service::is_failed() || !service::is_active() {
|
||||
trash_status(
|
||||
ui,
|
||||
"network-offline-symbolic",
|
||||
&gettext("Not connected"),
|
||||
&gettext("The Proton Drive mount service isn't running."),
|
||||
true,
|
||||
);
|
||||
return;
|
||||
}
|
||||
trash_status(
|
||||
ui,
|
||||
"folder-remote-symbolic",
|
||||
&gettext("Connecting…"),
|
||||
&gettext("Waiting for the Proton Drive mount service to come up."),
|
||||
false,
|
||||
);
|
||||
let ui = ui.clone();
|
||||
glib::timeout_add_local_once(CONNECT_RETRY_INTERVAL, move || {
|
||||
if ui.stack.visible_child_name().as_deref() == Some("trash") {
|
||||
load_trash(&ui);
|
||||
}
|
||||
});
|
||||
service_unreachable(ui, "trash", trash_status, load_trash);
|
||||
}
|
||||
|
||||
/// Show a status page in place of the trash list.
|
||||
@@ -363,12 +336,13 @@ pub(crate) fn trash_status(ui: &Rc<Ui>, icon: &str, title: &str, description: &s
|
||||
ui.trash.subtitle.set_subtitle("");
|
||||
}
|
||||
|
||||
/// Repopulate the trash list, most recently modified first — the order in which a
|
||||
/// user looks for what they just deleted.
|
||||
/// Repopulate the trash, most recently deleted first: the order in which a user
|
||||
/// looks for what they just deleted.
|
||||
pub(crate) fn repaint_trash(ui: &Rc<Ui>, entries: &[DirEntry]) {
|
||||
ui.trash.model.remove_all();
|
||||
sync_trash_selection(ui);
|
||||
ui.trash.empty.set_sensitive(!entries.is_empty());
|
||||
// An empty Trash has nothing to empty, so the button goes away rather than
|
||||
// sitting there greyed out.
|
||||
ui.trash.empty.set_sensitive(true);
|
||||
ui.trash.empty.set_visible(!entries.is_empty());
|
||||
if entries.is_empty() {
|
||||
trash_status(
|
||||
ui,
|
||||
@@ -377,6 +351,7 @@ pub(crate) fn repaint_trash(ui: &Rc<Ui>, entries: &[DirEntry]) {
|
||||
&gettext("Items you delete from Proton Drive show up here."),
|
||||
false,
|
||||
);
|
||||
clear_trash(ui);
|
||||
return;
|
||||
}
|
||||
ui.trash.content.set_visible_child_name("list");
|
||||
@@ -388,38 +363,69 @@ pub(crate) fn repaint_trash(ui: &Rc<Ui>, entries: &[DirEntry]) {
|
||||
));
|
||||
|
||||
let mut sorted = entries.to_vec();
|
||||
sorted.sort_by_key(|e| std::cmp::Reverse(e.modified));
|
||||
for entry in sorted {
|
||||
ui.trash.model.append(&BoxedAnyObject::new(entry));
|
||||
}
|
||||
sort_trash(&mut sorted);
|
||||
// Only the rows that changed are swapped, so a refresh keeps the
|
||||
// selection and the scroll position.
|
||||
replace_items(&ui.trash.files.model, &sorted);
|
||||
sync_trash_selection(ui);
|
||||
}
|
||||
|
||||
/// Restore one trashed entry to the folder it was trashed from.
|
||||
pub(crate) fn restore_entry(ui: &Rc<Ui>, entry: &DirEntry) {
|
||||
restore_entries(ui, std::slice::from_ref(entry));
|
||||
/// Newest deletion first. Entries with no known deletion time go last, newest
|
||||
/// modification first among themselves.
|
||||
fn sort_trash(entries: &mut [DirEntry]) {
|
||||
entries.sort_by_key(|e| {
|
||||
(
|
||||
e.trashed_at == 0,
|
||||
std::cmp::Reverse(e.trashed_at),
|
||||
std::cmp::Reverse(e.modified),
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
/// Restore trashed entries to the folders they were trashed from.
|
||||
/// Drop every row, and with them the selection and its bar.
|
||||
fn clear_trash(ui: &Rc<Ui>) {
|
||||
ui.trash.files.model.remove_all();
|
||||
sync_trash_selection(ui);
|
||||
}
|
||||
|
||||
/// Restore trashed entries to the folders they were trashed from. When they
|
||||
/// all came from one known folder, the toast offers to show it.
|
||||
pub(crate) fn restore_entries(ui: &Rc<Ui>, entries: &[DirEntry]) {
|
||||
run_mutation(
|
||||
let message = match entries {
|
||||
// Translators: {name} is a file or folder name.
|
||||
[one] => gettext_f("Restored “{name}”", &[("name", &one.name)]),
|
||||
_ => ngettext_f(
|
||||
"Restored {n} item",
|
||||
"Restored {n} items",
|
||||
entries.len() as u64,
|
||||
&[],
|
||||
),
|
||||
};
|
||||
let folder = common_origin(entries);
|
||||
run_mutation_then(
|
||||
ui,
|
||||
Request::Restore {
|
||||
uids: entries.iter().map(|e| e.uid.clone()).collect(),
|
||||
},
|
||||
match entries {
|
||||
// Translators: {name} is a file or folder name.
|
||||
[one] => gettext_f("Restored “{name}”", &[("name", &one.name)]),
|
||||
_ => ngettext_f(
|
||||
"Restored {n} item",
|
||||
"Restored {n} items",
|
||||
entries.len() as u64,
|
||||
&[],
|
||||
),
|
||||
},
|
||||
gettext_noop("Couldn't restore"),
|
||||
move |ui| match folder {
|
||||
Some(folder) => toast_action(ui, &message, &gettext("Show"), move |ui| {
|
||||
open_in_my_files(ui, folder.clone())
|
||||
}),
|
||||
None => toast(ui, &message),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// The folder every entry goes back to, when that is one known folder.
|
||||
fn common_origin(entries: &[DirEntry]) -> Option<String> {
|
||||
let first = entries.first()?.trashed_from.clone()?;
|
||||
entries
|
||||
.iter()
|
||||
.all(|e| e.trashed_from.as_deref() == Some(first.as_str()))
|
||||
.then_some(first)
|
||||
}
|
||||
|
||||
/// Confirm, then permanently delete trashed entries. Irreversible, so it asks.
|
||||
pub(crate) fn prompt_delete_forever(ui: &Rc<Ui>, entries: &[DirEntry]) {
|
||||
let win = ui_window(ui);
|
||||
@@ -451,7 +457,7 @@ pub(crate) fn prompt_delete_forever(ui: &Rc<Ui>, entries: &[DirEntry]) {
|
||||
),
|
||||
};
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Delete Permanently"))
|
||||
.heading(gettext("Delete Permanently?"))
|
||||
.body(body)
|
||||
.build();
|
||||
dialog.add_response("cancel", &gettext("Cancel"));
|
||||
@@ -477,9 +483,9 @@ pub(crate) fn prompt_delete_forever(ui: &Rc<Ui>, entries: &[DirEntry]) {
|
||||
/// Confirm, then permanently delete everything in the trash.
|
||||
pub(crate) fn prompt_empty_trash(ui: &Rc<Ui>) {
|
||||
let win = ui_window(ui);
|
||||
let count = ui.trash.model.n_items();
|
||||
let count = ui.trash.files.model.n_items();
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Empty Trash"))
|
||||
.heading(gettext("Empty Trash?"))
|
||||
.body(ngettext_f(
|
||||
"Permanently delete all {n} item in the trash? This cannot be undone.",
|
||||
"Permanently delete all {n} items in the trash? This cannot be undone.",
|
||||
@@ -506,3 +512,59 @@ pub(crate) fn prompt_empty_trash(ui: &Rc<Ui>) {
|
||||
});
|
||||
dialog.present(win.as_ref());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn trashed(name: &str, trashed_at: i64, modified: i64, from: Option<&str>) -> DirEntry {
|
||||
DirEntry {
|
||||
name: name.into(),
|
||||
is_dir: false,
|
||||
size: 0,
|
||||
modified,
|
||||
pinned: false,
|
||||
cached: false,
|
||||
uid: name.into(),
|
||||
path: String::new(),
|
||||
role: String::new(),
|
||||
shared_by: String::new(),
|
||||
shared_at: 0,
|
||||
shared_by_unverified: false,
|
||||
trashed_at,
|
||||
trashed_from: from.map(str::to_string),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trash_lists_newest_deletion_first_and_undated_last() {
|
||||
let mut entries = vec![
|
||||
trashed("undated-old", 0, 1, None),
|
||||
trashed("older", 10, 50, None),
|
||||
trashed("undated-new", 0, 9, None),
|
||||
trashed("newest", 20, 5, None),
|
||||
];
|
||||
sort_trash(&mut entries);
|
||||
let names: Vec<_> = entries.iter().map(|e| e.name.as_str()).collect();
|
||||
assert_eq!(names, ["newest", "older", "undated-new", "undated-old"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn restore_offers_a_folder_only_when_all_share_one() {
|
||||
let docs = trashed("a", 1, 1, Some("Documents"));
|
||||
let docs2 = trashed("b", 1, 1, Some("Documents"));
|
||||
let root = trashed("c", 1, 1, Some(""));
|
||||
let unknown = trashed("d", 1, 1, None);
|
||||
assert_eq!(
|
||||
common_origin(&[docs.clone(), docs2]).as_deref(),
|
||||
Some("Documents")
|
||||
);
|
||||
assert_eq!(
|
||||
common_origin(std::slice::from_ref(&root)).as_deref(),
|
||||
Some("")
|
||||
);
|
||||
assert_eq!(common_origin(&[docs.clone(), root]), None);
|
||||
assert_eq!(common_origin(&[docs, unknown]), None);
|
||||
assert_eq!(common_origin(&[]), None);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
//! Reloading a page without flashing it.
|
||||
//!
|
||||
//! Pages used to swap their content for a "Loading…" status page on every
|
||||
//! load, so every sidebar click and every folder change blinked. A [`Loader`]
|
||||
//! keeps the last content on screen while the request runs and shows the
|
||||
//! loading state only if the reply is slow. [`replace_items`] then swaps in the
|
||||
//! new rows by changing only the ones that differ, so selection and scroll
|
||||
//! position survive a refresh.
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
use std::time::Duration;
|
||||
|
||||
use gtk4::gio;
|
||||
use gtk4::glib;
|
||||
use gtk4::glib::BoxedAnyObject;
|
||||
use gtk4::glib::object::Cast;
|
||||
use gtk4::prelude::*;
|
||||
|
||||
/// How long a load may run before the page gives up its old content for a
|
||||
/// loading state. Most replies arrive well within this, so the page updates in
|
||||
/// place.
|
||||
const LOADING_GRACE: Duration = Duration::from_millis(300);
|
||||
|
||||
/// One page's loads. Each [`Loader::refresh`] or [`Loader::replace`]
|
||||
/// supersedes the one before it.
|
||||
pub(crate) struct Loader {
|
||||
content: gtk4::Widget,
|
||||
generation: Cell<u64>,
|
||||
placeholder: RefCell<Option<glib::SourceId>>,
|
||||
}
|
||||
|
||||
impl Loader {
|
||||
/// A loader for the page whose content is `content`.
|
||||
pub(crate) fn new(content: &impl IsA<gtk4::Widget>) -> Rc<Self> {
|
||||
Rc::new(Loader {
|
||||
content: content.clone().upcast(),
|
||||
generation: Cell::new(0),
|
||||
placeholder: RefCell::new(None),
|
||||
})
|
||||
}
|
||||
|
||||
/// Start a load of what the page already shows. The content stays up and
|
||||
/// usable; `placeholder` runs if the load is still going after
|
||||
/// [`LOADING_GRACE`].
|
||||
pub(crate) fn refresh(self: &Rc<Self>, placeholder: impl FnOnce() + 'static) -> LoadTicket {
|
||||
self.begin(false, placeholder)
|
||||
}
|
||||
|
||||
/// Start a load of something else, such as another folder. The old
|
||||
/// content stays up but insensitive, since acting on it would act on the
|
||||
/// wrong thing; `placeholder` runs if the load is still going after
|
||||
/// [`LOADING_GRACE`].
|
||||
pub(crate) fn replace(self: &Rc<Self>, placeholder: impl FnOnce() + 'static) -> LoadTicket {
|
||||
self.begin(true, placeholder)
|
||||
}
|
||||
|
||||
fn begin(self: &Rc<Self>, stale: bool, placeholder: impl FnOnce() + 'static) -> LoadTicket {
|
||||
let generation = self.generation.get().wrapping_add(1);
|
||||
self.generation.set(generation);
|
||||
self.cancel_placeholder();
|
||||
self.content.set_sensitive(!stale);
|
||||
let loader = Rc::downgrade(self);
|
||||
let source = glib::timeout_add_local_once(LOADING_GRACE, move || {
|
||||
let Some(loader) = loader.upgrade() else {
|
||||
return;
|
||||
};
|
||||
loader.placeholder.borrow_mut().take();
|
||||
loader.content.set_sensitive(true);
|
||||
placeholder();
|
||||
});
|
||||
*self.placeholder.borrow_mut() = Some(source);
|
||||
LoadTicket {
|
||||
loader: self.clone(),
|
||||
generation,
|
||||
}
|
||||
}
|
||||
|
||||
fn cancel_placeholder(&self) {
|
||||
if let Some(source) = self.placeholder.borrow_mut().take() {
|
||||
source.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A load in flight. Dropping it ends the load: the pending loading state is
|
||||
/// cancelled and the content is usable again, unless a newer load has taken
|
||||
/// over in the meantime.
|
||||
pub(crate) struct LoadTicket {
|
||||
loader: Rc<Loader>,
|
||||
generation: u64,
|
||||
}
|
||||
|
||||
impl LoadTicket {
|
||||
/// Whether no newer load has started since this one. A superseded load's
|
||||
/// reply is stale and must not be painted.
|
||||
pub(crate) fn is_current(&self) -> bool {
|
||||
self.loader.generation.get() == self.generation
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LoadTicket {
|
||||
fn drop(&mut self) {
|
||||
if self.is_current() {
|
||||
self.loader.cancel_placeholder();
|
||||
self.loader.content.set_sensitive(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Make `model`, a list of [`BoxedAnyObject`]s holding `T`, hold `items`,
|
||||
/// replacing only the run of items that changed, in one `items-changed`
|
||||
/// signal. Rows before and after that run keep their objects, so the views
|
||||
/// keep their widgets, the selection and the scroll position.
|
||||
pub(crate) fn replace_items<T: Clone + PartialEq + 'static>(model: &gio::ListStore, items: &[T]) {
|
||||
let held = |i: usize, item: &T| {
|
||||
model
|
||||
.item(i as u32)
|
||||
.and_downcast::<BoxedAnyObject>()
|
||||
.is_some_and(|object| *object.borrow::<T>() == *item)
|
||||
};
|
||||
let Some((start, removed, added)) = changed_run(model.n_items() as usize, items, held) else {
|
||||
return;
|
||||
};
|
||||
let added: Vec<BoxedAnyObject> = items[added]
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(BoxedAnyObject::new)
|
||||
.collect();
|
||||
model.splice(start as u32, removed as u32, &added);
|
||||
}
|
||||
|
||||
/// The one run that differs between an old list of `old_len` items and `new`:
|
||||
/// where it starts, how many old items it covers, and which new items replace
|
||||
/// them. `held(i, item)` says whether old item `i` equals `item`. `None` when
|
||||
/// the lists are equal.
|
||||
fn changed_run<T>(
|
||||
old_len: usize,
|
||||
new: &[T],
|
||||
held: impl Fn(usize, &T) -> bool,
|
||||
) -> Option<(usize, usize, std::ops::Range<usize>)> {
|
||||
let shared = old_len.min(new.len());
|
||||
let prefix = (0..shared).take_while(|&i| held(i, &new[i])).count();
|
||||
let suffix = (0..shared - prefix)
|
||||
.take_while(|&k| held(old_len - 1 - k, &new[new.len() - 1 - k]))
|
||||
.count();
|
||||
let removed = old_len - prefix - suffix;
|
||||
let added = prefix..new.len() - suffix;
|
||||
(removed > 0 || !added.is_empty()).then_some((prefix, removed, added))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::changed_run;
|
||||
|
||||
fn run(old: &[u32], new: &[u32]) -> Option<(usize, usize, Vec<u32>)> {
|
||||
changed_run(old.len(), new, |i, item| old[i] == *item)
|
||||
.map(|(start, removed, added)| (start, removed, new[added].to_vec()))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn equal_lists_change_nothing() {
|
||||
assert_eq!(run(&[1, 2, 3], &[1, 2, 3]), None);
|
||||
assert_eq!(run(&[], &[]), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_the_differing_middle_is_replaced() {
|
||||
assert_eq!(run(&[1, 2, 3, 4], &[1, 9, 4]), Some((1, 2, vec![9])));
|
||||
assert_eq!(run(&[1, 2, 3], &[1, 2, 5, 3]), Some((2, 0, vec![5])));
|
||||
assert_eq!(run(&[1, 2, 3], &[1, 3]), Some((1, 1, vec![])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn growing_or_shrinking_at_either_end() {
|
||||
assert_eq!(run(&[1, 2], &[0, 1, 2]), Some((0, 0, vec![0])));
|
||||
assert_eq!(run(&[1, 2], &[1, 2, 3]), Some((2, 0, vec![3])));
|
||||
assert_eq!(run(&[1, 2, 3], &[]), Some((0, 3, vec![])));
|
||||
assert_eq!(run(&[], &[1]), Some((0, 0, vec![1])));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repeated_items_are_not_counted_twice() {
|
||||
// The prefix takes both 1s; the suffix must not reuse them.
|
||||
assert_eq!(run(&[1, 1], &[1, 1, 1]), Some((2, 0, vec![1])));
|
||||
assert_eq!(run(&[1, 1, 1], &[1, 1]), Some((2, 1, vec![])));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,311 @@
|
||||
//! The app's side of `src/compat.rs`: widgets libadwaita 1.6–1.8 brought, with
|
||||
//! a stand-in for 1.5. [`has_adw_1_8`] picks the real ones at run time.
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// A segmented control: a row of options of which exactly one is on, told
|
||||
/// apart by index in the order they were given. `AdwToggleGroup` on
|
||||
/// libadwaita 1.8, linked toggle buttons before.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ToggleGroup {
|
||||
widget: gtk4::Widget,
|
||||
options: Options,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum Options {
|
||||
/// The group's `AdwToggle`s, reached by property name.
|
||||
Toggles(Rc<[glib::Object]>),
|
||||
Buttons(Rc<[gtk4::ToggleButton]>),
|
||||
}
|
||||
|
||||
impl ToggleGroup {
|
||||
/// A group of `(label, tooltip)` options, with the first one on.
|
||||
pub(crate) fn new(options: &[(&str, Option<&str>)]) -> Self {
|
||||
if has_adw_1_8() {
|
||||
let children: String = (0..options.len())
|
||||
.map(|i| format!(r#"<child><object class="AdwToggle" id="toggle{i}"/></child>"#))
|
||||
.collect();
|
||||
let builder = build_ui(&format!(
|
||||
r#"<object class="AdwToggleGroup" id="group">{children}</object>"#
|
||||
));
|
||||
let toggles = options
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, (label, tooltip))| {
|
||||
let toggle: glib::Object = built(&builder, &format!("toggle{i}"));
|
||||
toggle.set_property("label", *label);
|
||||
if let Some(tooltip) = tooltip {
|
||||
toggle.set_property("tooltip", *tooltip);
|
||||
}
|
||||
toggle
|
||||
})
|
||||
.collect();
|
||||
let widget: gtk4::Widget = built(&builder, "group");
|
||||
// A toggle group starts with nothing on.
|
||||
widget.set_property("active", 0u32);
|
||||
Self {
|
||||
widget,
|
||||
options: Options::Toggles(toggles),
|
||||
}
|
||||
} else {
|
||||
let bar = gtk4::Box::new(gtk4::Orientation::Horizontal, 0);
|
||||
bar.add_css_class("linked");
|
||||
let buttons: Rc<[gtk4::ToggleButton]> = options
|
||||
.iter()
|
||||
.map(|(label, tooltip)| {
|
||||
let button = gtk4::ToggleButton::with_label(label);
|
||||
button.set_tooltip_text(*tooltip);
|
||||
bar.append(&button);
|
||||
button
|
||||
})
|
||||
.collect();
|
||||
if let Some((first, rest)) = buttons.split_first() {
|
||||
first.set_active(true);
|
||||
// Chaining each to the first is what GTK turns into mutual
|
||||
// exclusion.
|
||||
for button in rest {
|
||||
button.set_group(Some(first));
|
||||
}
|
||||
}
|
||||
Self {
|
||||
widget: bar.upcast(),
|
||||
options: Options::Buttons(buttons),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The widget to pack.
|
||||
pub(crate) fn widget(&self) -> gtk4::Widget {
|
||||
self.widget.clone()
|
||||
}
|
||||
|
||||
/// Give the options pill-shaped ends.
|
||||
pub(crate) fn set_round(&self) {
|
||||
match &self.options {
|
||||
Options::Toggles(_) => self.widget.add_css_class("round"),
|
||||
Options::Buttons(buttons) => {
|
||||
for button in buttons.iter() {
|
||||
button.add_css_class("pill");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The option that is on.
|
||||
pub(crate) fn active(&self) -> u32 {
|
||||
match &self.options {
|
||||
Options::Toggles(_) => self.widget.property("active"),
|
||||
Options::Buttons(buttons) => {
|
||||
buttons.iter().position(|b| b.is_active()).unwrap_or(0) as u32
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Turn option `index` on, which turns the one that was on off.
|
||||
pub(crate) fn set_active(&self, index: u32) {
|
||||
match &self.options {
|
||||
Options::Toggles(_) => self.widget.set_property("active", index),
|
||||
Options::Buttons(buttons) => {
|
||||
if let Some(button) = buttons.get(index as usize) {
|
||||
button.set_active(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_label(&self, index: u32, label: &str) {
|
||||
match &self.options {
|
||||
Options::Toggles(toggles) => {
|
||||
if let Some(toggle) = toggles.get(index as usize) {
|
||||
toggle.set_property("label", label);
|
||||
}
|
||||
}
|
||||
Options::Buttons(buttons) => {
|
||||
if let Some(button) = buttons.get(index as usize) {
|
||||
button.set_label(label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Let option `index` be picked, or not.
|
||||
pub(crate) fn set_enabled(&self, index: u32, enabled: bool) {
|
||||
match &self.options {
|
||||
Options::Toggles(toggles) => {
|
||||
if let Some(toggle) = toggles.get(index as usize) {
|
||||
toggle.set_property("enabled", enabled);
|
||||
}
|
||||
}
|
||||
Options::Buttons(buttons) => {
|
||||
if let Some(button) = buttons.get(index as usize) {
|
||||
button.set_sensitive(enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Call `f` with the option that has just come on, whether the user or
|
||||
/// [`Self::set_active`] turned it on.
|
||||
pub(crate) fn connect_changed(&self, f: impl Fn(u32) + 'static) {
|
||||
match &self.options {
|
||||
Options::Toggles(_) => {
|
||||
self.widget
|
||||
.connect_notify_local(Some("active"), move |group, _| {
|
||||
f(group.property("active"))
|
||||
});
|
||||
}
|
||||
Options::Buttons(buttons) => {
|
||||
let f = Rc::new(f);
|
||||
for (index, button) in buttons.iter().enumerate() {
|
||||
let f = f.clone();
|
||||
// The group also fires `toggled` for the button going off;
|
||||
// only the one coming on counts.
|
||||
button.connect_toggled(move |button| {
|
||||
if button.is_active() {
|
||||
f(index as u32);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A list row that is a button: a centred title and an icon after it.
|
||||
/// `AdwButtonRow` on libadwaita 1.8, an activatable `adw::ActionRow` before.
|
||||
pub(crate) fn button_row(
|
||||
title: &str,
|
||||
end_icon: &str,
|
||||
on_activated: impl Fn() + 'static,
|
||||
) -> gtk4::ListBoxRow {
|
||||
if has_adw_1_8() {
|
||||
let builder = build_ui(r#"<object class="AdwButtonRow" id="row"/>"#);
|
||||
let row: adw::PreferencesRow = built(&builder, "row");
|
||||
row.set_title(title);
|
||||
row.set_property("end-icon-name", end_icon);
|
||||
row.connect_local("activated", false, move |_| {
|
||||
on_activated();
|
||||
None
|
||||
});
|
||||
row.upcast()
|
||||
} else {
|
||||
let row = adw::ActionRow::builder()
|
||||
.title(title)
|
||||
.activatable(true)
|
||||
.build();
|
||||
row.add_suffix(>k4::Image::from_icon_name(end_icon));
|
||||
row.connect_activated(move |_| on_activated());
|
||||
row.upcast()
|
||||
}
|
||||
}
|
||||
|
||||
/// Show the keyboard shortcuts window over `window`. See [`shortcuts_dialog`].
|
||||
pub(crate) fn present_shortcuts(
|
||||
window: &impl IsA<gtk4::Widget>,
|
||||
groups: &[(String, Vec<(&str, String)>)],
|
||||
) {
|
||||
shortcuts_dialog(groups).present(Some(window));
|
||||
}
|
||||
|
||||
/// The keyboard shortcuts window: `groups` of `(accelerator, what it does)`
|
||||
/// under a heading each, all already translated. `AdwShortcutsDialog` on
|
||||
/// libadwaita 1.8, a preferences page of rows before.
|
||||
fn shortcuts_dialog(groups: &[(String, Vec<(&str, String)>)]) -> adw::Dialog {
|
||||
if has_adw_1_8() {
|
||||
let sections: String = groups
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(s, (_, keys))| {
|
||||
let items: String = (0..keys.len())
|
||||
.map(|k| {
|
||||
format!(r#"<child><object class="AdwShortcutsItem" id="item{s}_{k}"/></child>"#)
|
||||
})
|
||||
.collect();
|
||||
format!(
|
||||
r#"<child><object class="AdwShortcutsSection" id="section{s}">{items}</object></child>"#
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let builder = build_ui(&format!(
|
||||
r#"<object class="AdwShortcutsDialog" id="dialog">{sections}</object>"#
|
||||
));
|
||||
for (s, (title, keys)) in groups.iter().enumerate() {
|
||||
let section: glib::Object = built(&builder, &format!("section{s}"));
|
||||
section.set_property("title", title.as_str());
|
||||
for (k, (accel, action)) in keys.iter().enumerate() {
|
||||
let item: glib::Object = built(&builder, &format!("item{s}_{k}"));
|
||||
item.set_property("title", action.as_str());
|
||||
item.set_property("accelerator", *accel);
|
||||
}
|
||||
}
|
||||
return built(&builder, "dialog");
|
||||
}
|
||||
|
||||
let page = adw::PreferencesPage::new();
|
||||
for (title, keys) in groups {
|
||||
let group = adw::PreferencesGroup::builder().title(title).build();
|
||||
for (accel, action) in keys {
|
||||
let row = adw::ActionRow::builder().title(action).build();
|
||||
row.add_suffix(
|
||||
>k4::ShortcutLabel::builder()
|
||||
.accelerator(*accel)
|
||||
.valign(gtk4::Align::Center)
|
||||
.build(),
|
||||
);
|
||||
group.add(&row);
|
||||
}
|
||||
page.add(&group);
|
||||
}
|
||||
|
||||
adw::Dialog::builder()
|
||||
.title(gettext("Keyboard Shortcuts"))
|
||||
.content_width(460)
|
||||
.content_height(620)
|
||||
.child(&{
|
||||
let toolbar = adw::ToolbarView::new();
|
||||
toolbar.add_top_bar(&adw::HeaderBar::new());
|
||||
toolbar.set_content(Some(&page));
|
||||
toolbar
|
||||
})
|
||||
.build()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The newer widgets are made by class name and set up by property and
|
||||
/// signal name, so a typo in any of those only shows when they are built.
|
||||
/// Needs a display and libadwaita 1.8; passes trivially without either.
|
||||
#[test]
|
||||
fn newer_widgets_build_by_class_name() {
|
||||
if adw::init().is_err() || !has_adw_1_8() {
|
||||
return;
|
||||
}
|
||||
|
||||
let group = ToggleGroup::new(&[("One", None), ("Two", Some("The second"))]);
|
||||
group.set_round();
|
||||
group.set_label(1, "Deux");
|
||||
group.set_enabled(1, true);
|
||||
assert_eq!(group.widget().type_().name(), "AdwToggleGroup");
|
||||
assert_eq!(group.active(), 0);
|
||||
let changed = Rc::new(Cell::new(None));
|
||||
group.connect_changed({
|
||||
let changed = changed.clone();
|
||||
move |index| changed.set(Some(index))
|
||||
});
|
||||
group.set_active(1);
|
||||
assert_eq!((group.active(), changed.get()), (1, Some(1)));
|
||||
|
||||
let row = button_row("Row", "go-down-symbolic", || {});
|
||||
assert_eq!(row.type_().name(), "AdwButtonRow");
|
||||
assert_eq!(spinner().type_().name(), "AdwSpinner");
|
||||
|
||||
let dialog = shortcuts_dialog(&[(
|
||||
"General".to_owned(),
|
||||
vec![("<Control>q", "Quit".to_owned())],
|
||||
)]);
|
||||
assert_eq!(dialog.type_().name(), "AdwShortcutsDialog");
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,7 @@ pub(crate) struct DetailsWidgets {
|
||||
/// [`DirEntry`] and [`wire_details`] connects the buttons.
|
||||
pub(crate) fn build_details_pane() -> (gtk4::Widget, DetailsWidgets) {
|
||||
let toggle = gtk4::ToggleButton::builder()
|
||||
.icon_name("sidebar-show-right-symbolic")
|
||||
.icon_name("pdfs-details-pane-symbolic")
|
||||
.tooltip_text(gettext("Details (Alt+Enter)"))
|
||||
.build();
|
||||
|
||||
@@ -202,7 +202,7 @@ pub(crate) fn build_details_pane() -> (gtk4::Widget, DetailsWidgets) {
|
||||
let toggle_off = toggle.clone();
|
||||
empty_close.connect_clicked(move |_| toggle_off.set_active(false));
|
||||
let empty_status = adw::StatusPage::builder()
|
||||
.icon_name("sidebar-show-right-symbolic")
|
||||
.icon_name("pdfs-details-pane-symbolic")
|
||||
.title(gettext("No item selected"))
|
||||
.description(gettext("Select a file or folder to see its details."))
|
||||
.vexpand(true)
|
||||
@@ -373,7 +373,7 @@ pub(crate) fn show_details(ui: &Rc<Ui>, entry: &DirEntry) {
|
||||
});
|
||||
d.size_row.set_visible(!entry.is_dir);
|
||||
d.modified_row
|
||||
.set_subtitle(&format_modified(entry.modified));
|
||||
.set_subtitle(&dates::short_date(entry.modified));
|
||||
let rel = entry_rel(ui, entry);
|
||||
let parent = match rel.rfind('/') {
|
||||
Some(i) => &rel[..i],
|
||||
@@ -494,13 +494,13 @@ pub(crate) fn versions_summary(items: &[RevisionInfo]) -> String {
|
||||
match (items.len(), latest) {
|
||||
(0, _) | (_, None) => gettext("No earlier versions"),
|
||||
// Translators: {date} is the date of the only version.
|
||||
(1, Some(t)) => gettext_f("1 version · {date}", &[("date", &format_modified(t))]),
|
||||
(1, Some(t)) => gettext_f("1 version · {date}", &[("date", &dates::short_date(t))]),
|
||||
// Translators: {date} is the date of the newest version.
|
||||
(n, Some(t)) => ngettext_f(
|
||||
"{n} version · latest {date}",
|
||||
"{n} versions · latest {date}",
|
||||
n as u64,
|
||||
&[("date", &format_modified(t))],
|
||||
&[("date", &dates::short_date(t))],
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,522 @@
|
||||
//! The file list every file page shows: an icon grid and a column list over one
|
||||
//! model and one selection, with thumbnails, context menus, rubberband selection
|
||||
//! and click-to-deselect. My files, Trash, Shared with me and Shared by me build
|
||||
//! one each and differ only in their columns, menus and what activating an entry
|
||||
//! does (see [`FileListBehavior`]).
|
||||
|
||||
use crate::*;
|
||||
|
||||
/// Where a page's entries go when the user acts on them. Plain function pointers:
|
||||
/// each page has one of these, fixed for the life of the window.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct FileListBehavior {
|
||||
/// Double-click or Enter on an entry.
|
||||
pub(crate) activate: fn(&Rc<Ui>, &DirEntry),
|
||||
/// The right-click menu for one entry.
|
||||
pub(crate) entry_menu: fn(&Rc<Ui>, &DirEntry) -> ActionMenu,
|
||||
/// The right-click menu for a multi-selection the clicked entry is part of.
|
||||
pub(crate) bulk_menu: fn(&Rc<Ui>, Vec<DirEntry>) -> ActionMenu,
|
||||
/// The right-click menu on empty space.
|
||||
pub(crate) background_menu: fn(&Rc<Ui>) -> ActionMenu,
|
||||
/// Paint the offline/cached/online-only badge. Only entries in the mounted
|
||||
/// tree have a local state worth showing.
|
||||
pub(crate) badges: bool,
|
||||
/// Entries can be dragged onto folders to move them.
|
||||
pub(crate) drag_and_drop: bool,
|
||||
}
|
||||
|
||||
/// A realised grid cell's thumbnail and name label.
|
||||
type GridTile = (glib::WeakRef<gtk4::Overlay>, glib::WeakRef<gtk4::Label>);
|
||||
|
||||
/// One page's grid and list. Cheap to clone: every field is a GObject or an
|
||||
/// `Rc`, so a clone is the same list.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct FileList {
|
||||
/// The entries, as [`BoxedAnyObject`]-wrapped [`DirEntry`]s.
|
||||
pub(crate) model: gio::ListStore,
|
||||
/// One selection for both views: switching layout keeps what is selected.
|
||||
pub(crate) selection: gtk4::MultiSelection,
|
||||
pub(crate) grid: gtk4::GridView,
|
||||
pub(crate) column_view: gtk4::ColumnView,
|
||||
/// The two views, named "grid" and "list".
|
||||
pub(crate) views: gtk4::Stack,
|
||||
thumbnail_size: Rc<Cell<i32>>,
|
||||
/// Weak references to realised grid cells. A new thumbnail size resizes only
|
||||
/// these visible, recycled surfaces instead of invalidating the whole model.
|
||||
tiles: Rc<RefCell<Vec<GridTile>>>,
|
||||
behavior: Rc<Cell<Option<FileListBehavior>>>,
|
||||
}
|
||||
|
||||
impl FileList {
|
||||
/// Assemble the empty views. The factories need the [`Ui`] handle, so they
|
||||
/// are installed later by [`FileList::wire`].
|
||||
pub(crate) fn new() -> Self {
|
||||
let model = gio::ListStore::new::<BoxedAnyObject>();
|
||||
// Multi-select: acting on a batch is the common case, and doing it one
|
||||
// confirmation dialog at a time is not a workflow. Rubberband drags a
|
||||
// selection box from empty space, as a file manager does.
|
||||
let selection = gtk4::MultiSelection::new(Some(model.clone()));
|
||||
let grid = gtk4::GridView::builder()
|
||||
.model(&selection)
|
||||
.min_columns(2)
|
||||
.max_columns(32)
|
||||
.enable_rubberband(true)
|
||||
.build();
|
||||
grid.add_css_class("file-grid");
|
||||
let grid_scroll = gtk4::ScrolledWindow::builder()
|
||||
.vexpand(true)
|
||||
.child(&grid)
|
||||
.build();
|
||||
|
||||
let column_view = gtk4::ColumnView::builder()
|
||||
.model(&selection)
|
||||
.enable_rubberband(true)
|
||||
.build();
|
||||
column_view.add_css_class("data-table");
|
||||
let column_scroll = gtk4::ScrolledWindow::builder()
|
||||
.vexpand(true)
|
||||
.child(&column_view)
|
||||
.build();
|
||||
|
||||
let views = gtk4::Stack::new();
|
||||
views.set_vexpand(true);
|
||||
views.add_named(&grid_scroll, Some("grid"));
|
||||
views.add_named(&column_scroll, Some("list"));
|
||||
|
||||
FileList {
|
||||
model,
|
||||
selection,
|
||||
grid,
|
||||
column_view,
|
||||
views,
|
||||
thumbnail_size: Rc::new(Cell::new(GRID_THUMB_DEFAULT)),
|
||||
tiles: Rc::new(RefCell::new(Vec::new())),
|
||||
behavior: Rc::new(Cell::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Install the grid tiles, the Name column, activation, the context menus
|
||||
/// and click-to-deselect. The page appends its other columns afterwards.
|
||||
pub(crate) fn wire(&self, ui: &Rc<Ui>, behavior: FileListBehavior) {
|
||||
self.behavior.set(Some(behavior));
|
||||
for view in [
|
||||
self.grid.clone().upcast::<gtk4::Widget>(),
|
||||
self.column_view.clone().upcast(),
|
||||
] {
|
||||
self.attach_background_menu(ui, &view, behavior);
|
||||
self.attach_background_deselect(&view);
|
||||
}
|
||||
|
||||
let factory = gtk4::SignalListItemFactory::new();
|
||||
factory.connect_setup({
|
||||
let (ui, list) = (ui.clone(), self.clone());
|
||||
move |_, item| {
|
||||
let item = item.downcast_ref::<gtk4::ListItem>().unwrap();
|
||||
let size = list.thumbnail_size.get();
|
||||
let thumbnail = file_thumbnail_widget(size, grid_fallback_size(size));
|
||||
// Keep the sync-state badge inside the thumbnail surface.
|
||||
let badge = gtk4::Image::builder()
|
||||
.pixel_size(18)
|
||||
.halign(gtk4::Align::End)
|
||||
.valign(gtk4::Align::Start)
|
||||
.margin_top(2)
|
||||
.margin_end(2)
|
||||
.visible(false)
|
||||
.build();
|
||||
badge.add_css_class("file-badge");
|
||||
thumbnail.add_overlay(&badge);
|
||||
// `WordChar` rather than the default `Word`: a name with no
|
||||
// spaces offers no word-break opportunity, so word wrapping
|
||||
// cannot break it at all and the label asks for its full natural
|
||||
// width instead — one tile stretches to the width of the window
|
||||
// and the grid collapses to a single column. Allowing a mid-word
|
||||
// break is what keeps the two-line-then-ellipsis budget below
|
||||
// enforceable for *every* name rather than only the ones that
|
||||
// happen to have spaces.
|
||||
let label = gtk4::Label::builder()
|
||||
.ellipsize(gtk4::pango::EllipsizeMode::End)
|
||||
.justify(gtk4::Justification::Center)
|
||||
.max_width_chars(13)
|
||||
.width_chars(13)
|
||||
.wrap(true)
|
||||
.wrap_mode(gtk4::pango::WrapMode::WordChar)
|
||||
.lines(2)
|
||||
.build();
|
||||
list.tiles
|
||||
.borrow_mut()
|
||||
.push((thumbnail.downgrade(), label.downgrade()));
|
||||
let tile = gtk4::Box::new(gtk4::Orientation::Vertical, 4);
|
||||
tile.add_css_class("file-tile");
|
||||
tile.append(&thumbnail);
|
||||
tile.append(&label);
|
||||
list.attach_context_menu(&ui, item, &tile, behavior);
|
||||
if behavior.drag_and_drop {
|
||||
attach_drag(&ui, item, &tile);
|
||||
attach_drop(&ui, item, &tile);
|
||||
}
|
||||
item.set_child(Some(&tile));
|
||||
}
|
||||
});
|
||||
factory.connect_bind({
|
||||
let (ui, list) = (ui.clone(), self.clone());
|
||||
move |_, item| {
|
||||
let item = item.downcast_ref::<gtk4::ListItem>().unwrap();
|
||||
let tile = item.child().and_downcast::<gtk4::Box>().unwrap();
|
||||
let thumbnail = tile.first_child().and_downcast::<gtk4::Overlay>().unwrap();
|
||||
let badge = thumbnail
|
||||
.last_child()
|
||||
.and_downcast::<gtk4::Image>()
|
||||
.unwrap();
|
||||
let label = thumbnail
|
||||
.next_sibling()
|
||||
.and_downcast::<gtk4::Label>()
|
||||
.unwrap();
|
||||
let obj = item.item().and_downcast::<BoxedAnyObject>().unwrap();
|
||||
let entry = obj.borrow::<DirEntry>();
|
||||
resize_grid_tile(&thumbnail, &label, list.thumbnail_size.get());
|
||||
bind_file_thumbnail(&ui, &thumbnail, &entry, false);
|
||||
label.set_label(&entry.name);
|
||||
label.set_tooltip_text(Some(&entry.name));
|
||||
if behavior.badges {
|
||||
apply_badge(&badge, &entry);
|
||||
}
|
||||
}
|
||||
});
|
||||
self.grid.set_factory(Some(&factory));
|
||||
|
||||
let ui_grid = ui.clone();
|
||||
self.grid.connect_activate(move |grid, pos| {
|
||||
if let Some(entry) = entry_at(grid.model().as_ref(), pos) {
|
||||
(behavior.activate)(&ui_grid, &entry);
|
||||
}
|
||||
});
|
||||
|
||||
self.column_view
|
||||
.append_column(&self.name_column(ui, behavior));
|
||||
let ui_col = ui.clone();
|
||||
self.column_view.connect_activate(move |view, pos| {
|
||||
if let Some(entry) = entry_at(view.model().as_ref(), pos) {
|
||||
(behavior.activate)(&ui_col, &entry);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Every highlighted entry, in model order.
|
||||
///
|
||||
/// Walks the model rather than the selection bitset: a listing is at most a
|
||||
/// few thousand rows, and asking each position whether it is selected keeps
|
||||
/// this free of bitset-iterator lifetimes for no measurable cost.
|
||||
pub(crate) fn selected(&self) -> Vec<DirEntry> {
|
||||
(0..self.selection.n_items())
|
||||
.filter(|i| self.selection.is_selected(*i))
|
||||
.filter_map(|i| entry_at(Some(&self.selection), i))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Show the grid, or the list.
|
||||
pub(crate) fn show_list(&self, list: bool) {
|
||||
self.views
|
||||
.set_visible_child_name(if list { "list" } else { "grid" });
|
||||
}
|
||||
|
||||
/// Whether the list, rather than the grid, is on screen.
|
||||
pub(crate) fn shows_list(&self) -> bool {
|
||||
self.views.visible_child_name().as_deref() == Some("list")
|
||||
}
|
||||
|
||||
/// Resize the grid's thumbnails. Only realised cells are touched; the rest
|
||||
/// pick the size up when they are bound.
|
||||
pub(crate) fn set_thumbnail_size(&self, size: i32) {
|
||||
if self.thumbnail_size.replace(size) == size {
|
||||
return;
|
||||
}
|
||||
self.tiles
|
||||
.borrow_mut()
|
||||
.retain(|(thumbnail_ref, label_ref)| {
|
||||
let (Some(thumbnail), Some(label)) = (thumbnail_ref.upgrade(), label_ref.upgrade())
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
resize_grid_tile(&thumbnail, &label, size);
|
||||
true
|
||||
});
|
||||
self.grid.queue_resize();
|
||||
}
|
||||
|
||||
/// The Menu key or Shift+F10: the menu for the selection, or the background
|
||||
/// menu when nothing is selected, opened at the focused item.
|
||||
pub(crate) fn popup_keyboard_menu(&self, ui: &Rc<Ui>) {
|
||||
let Some(behavior) = self.behavior.get() else {
|
||||
return;
|
||||
};
|
||||
let view = self
|
||||
.views
|
||||
.visible_child()
|
||||
.unwrap_or_else(|| self.views.clone().upcast());
|
||||
// The focused cell is where the eye is; without one, the middle of the view.
|
||||
let (x, y) = view
|
||||
.root()
|
||||
.and_then(|root| root.focus())
|
||||
.filter(|focus| focus.is_ancestor(&view))
|
||||
.and_then(|focus| focus.compute_bounds(&view))
|
||||
.map(|b| {
|
||||
(
|
||||
(b.x() + b.width() / 2.0) as f64,
|
||||
(b.y() + b.height() / 2.0) as f64,
|
||||
)
|
||||
})
|
||||
.unwrap_or((view.width() as f64 / 2.0, view.height() as f64 / 2.0));
|
||||
let selected = self.selected();
|
||||
let menu = match selected.len() {
|
||||
0 => (behavior.background_menu)(ui),
|
||||
1 => (behavior.entry_menu)(ui, &selected[0]),
|
||||
_ => (behavior.bulk_menu)(ui, selected),
|
||||
};
|
||||
menu.popup_at(&view, x, y);
|
||||
}
|
||||
|
||||
/// The Name column: a small thumbnail with its local-state badge overlaid,
|
||||
/// followed by the name and the same right-click menu the grid tiles carry.
|
||||
fn name_column(&self, ui: &Rc<Ui>, behavior: FileListBehavior) -> gtk4::ColumnViewColumn {
|
||||
let factory = gtk4::SignalListItemFactory::new();
|
||||
factory.connect_setup({
|
||||
let (ui, list) = (ui.clone(), self.clone());
|
||||
move |_, item| {
|
||||
let item = item.downcast_ref::<gtk4::ListItem>().unwrap();
|
||||
let thumbnail = file_thumbnail_widget(28, 16);
|
||||
let badge = gtk4::Image::builder()
|
||||
.pixel_size(14)
|
||||
.halign(gtk4::Align::End)
|
||||
.valign(gtk4::Align::Start)
|
||||
.visible(false)
|
||||
.build();
|
||||
badge.add_css_class("file-badge");
|
||||
thumbnail.add_overlay(&badge);
|
||||
// Ellipsized so the Name column can be *narrower* than its
|
||||
// longest name. Without it the label's minimum width is the whole
|
||||
// string, the column inherits that minimum, and one long name
|
||||
// pushes the other columns off the right edge of the window for
|
||||
// every row.
|
||||
let label = gtk4::Label::builder()
|
||||
.halign(gtk4::Align::Start)
|
||||
.ellipsize(gtk4::pango::EllipsizeMode::End)
|
||||
.build();
|
||||
let cell = gtk4::Box::new(gtk4::Orientation::Horizontal, 8);
|
||||
cell.append(&thumbnail);
|
||||
cell.append(&label);
|
||||
list.attach_context_menu(&ui, item, &cell, behavior);
|
||||
if behavior.drag_and_drop {
|
||||
attach_drag(&ui, item, &cell);
|
||||
attach_drop(&ui, item, &cell);
|
||||
}
|
||||
item.set_child(Some(&cell));
|
||||
}
|
||||
});
|
||||
factory.connect_bind({
|
||||
let ui = ui.clone();
|
||||
move |_, item| {
|
||||
let item = item.downcast_ref::<gtk4::ListItem>().unwrap();
|
||||
let cell = item.child().and_downcast::<gtk4::Box>().unwrap();
|
||||
let thumbnail = cell.first_child().and_downcast::<gtk4::Overlay>().unwrap();
|
||||
let badge = thumbnail
|
||||
.last_child()
|
||||
.and_downcast::<gtk4::Image>()
|
||||
.unwrap();
|
||||
let label = thumbnail
|
||||
.next_sibling()
|
||||
.and_downcast::<gtk4::Label>()
|
||||
.unwrap();
|
||||
let obj = item.item().and_downcast::<BoxedAnyObject>().unwrap();
|
||||
let entry = obj.borrow::<DirEntry>();
|
||||
bind_file_thumbnail(&ui, &thumbnail, &entry, true);
|
||||
label.set_label(&entry.name);
|
||||
label.set_tooltip_text(Some(&entry.name));
|
||||
if behavior.badges {
|
||||
apply_badge(&badge, &entry);
|
||||
}
|
||||
}
|
||||
});
|
||||
let column = gtk4::ColumnViewColumn::new(Some(&pgettext("column", "Name")), Some(factory));
|
||||
column.set_expand(true);
|
||||
column
|
||||
}
|
||||
|
||||
/// Pop the menu for whatever entry `item` is bound to when a cell is
|
||||
/// right-clicked. Capturing the [`gtk4::ListItem`] rather than a snapshot of
|
||||
/// the entry keeps the menu right as the view recycles cells while
|
||||
/// scrolling. The click is claimed, so the background menu does not open on
|
||||
/// top of it.
|
||||
fn attach_context_menu(
|
||||
&self,
|
||||
ui: &Rc<Ui>,
|
||||
item: >k4::ListItem,
|
||||
anchor: >k4::Box,
|
||||
behavior: FileListBehavior,
|
||||
) {
|
||||
let gesture = gtk4::GestureClick::new();
|
||||
gesture.set_button(gtk4::gdk::BUTTON_SECONDARY);
|
||||
let (ui, list, item, target) = (ui.clone(), self.clone(), item.clone(), anchor.clone());
|
||||
gesture.connect_pressed(move |gesture, _, x, y| {
|
||||
gesture.set_state(gtk4::EventSequenceState::Claimed);
|
||||
if let Some(obj) = item.item().and_downcast::<BoxedAnyObject>() {
|
||||
let entry = obj.borrow::<DirEntry>().clone();
|
||||
// Right-clicking a row that is part of a multi-selection acts on
|
||||
// the batch; right-clicking outside one acts on the row.
|
||||
let selected = list.selected();
|
||||
if selected.len() > 1 && selected.iter().any(|e| e.uid == entry.uid) {
|
||||
(behavior.bulk_menu)(&ui, selected).popup_at(&target, x, y);
|
||||
} else {
|
||||
(behavior.entry_menu)(&ui, &entry).popup_at(&target, x, y);
|
||||
}
|
||||
}
|
||||
});
|
||||
anchor.add_controller(gesture);
|
||||
}
|
||||
|
||||
/// Right-clicking the empty space of a view offers the page's own actions.
|
||||
fn attach_background_menu(&self, ui: &Rc<Ui>, view: >k4::Widget, behavior: FileListBehavior) {
|
||||
let gesture = gtk4::GestureClick::new();
|
||||
gesture.set_button(gtk4::gdk::BUTTON_SECONDARY);
|
||||
let (ui, target) = (ui.clone(), view.clone());
|
||||
gesture.connect_pressed(move |_, _, x, y| {
|
||||
(behavior.background_menu)(&ui).popup_at(&target, x, y);
|
||||
});
|
||||
view.add_controller(gesture);
|
||||
}
|
||||
|
||||
/// Clicking the empty space of a view drops the selection, as in a file
|
||||
/// manager. GTK's list views leave it alone, so a stray highlight otherwise
|
||||
/// sticks around until another item is clicked.
|
||||
fn attach_background_deselect(&self, view: >k4::Widget) {
|
||||
let gesture = gtk4::GestureClick::new();
|
||||
gesture.set_button(gtk4::gdk::BUTTON_PRIMARY);
|
||||
// Capture sees the press before an item's own gesture claims it; this
|
||||
// only looks, so the item still gets its click.
|
||||
gesture.set_propagation_phase(gtk4::PropagationPhase::Capture);
|
||||
let (selection, target) = (self.selection.clone(), view.clone());
|
||||
gesture.connect_pressed(move |gesture, _, x, y| {
|
||||
let modifiers = gesture.current_event_state();
|
||||
if modifiers.intersects(
|
||||
gtk4::gdk::ModifierType::CONTROL_MASK | gtk4::gdk::ModifierType::SHIFT_MASK,
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if !on_item(&target, x, y) {
|
||||
selection.unselect_all();
|
||||
}
|
||||
});
|
||||
view.add_controller(gesture);
|
||||
}
|
||||
}
|
||||
|
||||
/// A header button that switches `list` between grid and list. It offers the
|
||||
/// other layout, the way a toggle reads.
|
||||
pub(crate) fn layout_button(list: &FileList) -> gtk4::Button {
|
||||
let button = gtk4::Button::builder().valign(gtk4::Align::Center).build();
|
||||
let sync = |button: >k4::Button, list: bool| {
|
||||
button.set_icon_name(if list {
|
||||
"view-grid-symbolic"
|
||||
} else {
|
||||
"view-list-symbolic"
|
||||
});
|
||||
button.set_tooltip_text(Some(&if list {
|
||||
gettext("Show as grid (Ctrl+1)")
|
||||
} else {
|
||||
gettext("Show as list (Ctrl+2)")
|
||||
}));
|
||||
};
|
||||
sync(&button, list.shows_list());
|
||||
let weak = button.downgrade();
|
||||
list.views.connect_visible_child_name_notify(move |views| {
|
||||
if let Some(button) = weak.upgrade() {
|
||||
sync(
|
||||
&button,
|
||||
views.visible_child_name().as_deref() == Some("list"),
|
||||
);
|
||||
}
|
||||
});
|
||||
let views = list.views.clone();
|
||||
button.connect_clicked(move |_| {
|
||||
let to_list = views.visible_child_name().as_deref() != Some("list");
|
||||
views.set_visible_child_name(if to_list { "list" } else { "grid" });
|
||||
});
|
||||
button
|
||||
}
|
||||
|
||||
/// Build a trailing text column whose cell text is derived from each [`DirEntry`]
|
||||
/// by `render`.
|
||||
pub(crate) fn text_column(
|
||||
title: &str,
|
||||
render: impl Fn(&DirEntry) -> String + 'static,
|
||||
) -> gtk4::ColumnViewColumn {
|
||||
let factory = gtk4::SignalListItemFactory::new();
|
||||
factory.connect_setup(|_, item| {
|
||||
let item = item.downcast_ref::<gtk4::ListItem>().unwrap();
|
||||
let label = gtk4::Label::builder()
|
||||
.halign(gtk4::Align::Start)
|
||||
.ellipsize(gtk4::pango::EllipsizeMode::End)
|
||||
.build();
|
||||
label.add_css_class("dim-label");
|
||||
item.set_child(Some(&label));
|
||||
});
|
||||
factory.connect_bind(move |_, item| {
|
||||
let item = item.downcast_ref::<gtk4::ListItem>().unwrap();
|
||||
let label = item.child().and_downcast::<gtk4::Label>().unwrap();
|
||||
let obj = item.item().and_downcast::<BoxedAnyObject>().unwrap();
|
||||
let entry = obj.borrow::<DirEntry>();
|
||||
let text = render(&entry);
|
||||
label.set_tooltip_text(Some(&text));
|
||||
label.set_label(&text);
|
||||
});
|
||||
gtk4::ColumnViewColumn::new(Some(title), Some(factory))
|
||||
}
|
||||
|
||||
fn resize_grid_tile(thumbnail: >k4::Overlay, label: >k4::Label, size: i32) {
|
||||
resize_file_thumbnail(thumbnail, size, grid_fallback_size(size));
|
||||
let name_width = (size / 6 + 1).clamp(8, 24);
|
||||
label.set_width_chars(name_width);
|
||||
label.set_max_width_chars(name_width);
|
||||
}
|
||||
|
||||
fn grid_fallback_size(thumbnail_size: i32) -> i32 {
|
||||
(thumbnail_size * 8 / 9).clamp(24, thumbnail_size)
|
||||
}
|
||||
|
||||
/// Whether `(x, y)` in `view` lands on an item: a grid tile ("child"), a list
|
||||
/// row ("row"), or a column header ("header"), which has clicks of its own.
|
||||
fn on_item(view: >k4::Widget, x: f64, y: f64) -> bool {
|
||||
let mut widget = view.pick(x, y, gtk4::PickFlags::DEFAULT);
|
||||
while let Some(w) = widget {
|
||||
if &w == view {
|
||||
return false;
|
||||
}
|
||||
if matches!(w.css_name().as_str(), "child" | "row" | "header") {
|
||||
return true;
|
||||
}
|
||||
widget = w.parent();
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Every page's file list.
|
||||
pub(crate) fn file_lists(ui: &Ui) -> [&FileList; 4] {
|
||||
[
|
||||
&ui.browser.files,
|
||||
&ui.trash.files,
|
||||
&ui.shared.files,
|
||||
&ui.shared_by_me.files,
|
||||
]
|
||||
}
|
||||
|
||||
/// The file list of the page on screen, if it has one.
|
||||
pub(crate) fn visible_file_list(ui: &Ui) -> Option<&FileList> {
|
||||
match ui.stack.visible_child_name()?.as_str() {
|
||||
"browser" => Some(&ui.browser.files),
|
||||
"trash" => Some(&ui.trash.files),
|
||||
"sharedbyme" => Some(&ui.shared_by_me.files),
|
||||
"shared" if ui.shared.views.visible_child_name().as_deref() == Some("files") => {
|
||||
Some(&ui.shared.files)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
use crate::*;
|
||||
|
||||
/// An action run by an [`ActionMenu`] item.
|
||||
pub(crate) type MenuAction = Box<dyn Fn()>;
|
||||
|
||||
/// A `gio::Menu` whose items run closures, for menus whose contents depend on
|
||||
/// what they were opened for (a row, a selection, a photo). Each item gets its
|
||||
/// own action in a private `menu` group, installed on whatever hosts the
|
||||
@@ -148,12 +145,3 @@ fn popover_host(anchor: >k4::Widget) -> gtk4::Widget {
|
||||
}
|
||||
host
|
||||
}
|
||||
|
||||
/// A flat ⋮ button whose menu lists `items` as (label, action).
|
||||
pub(crate) fn more_menu_button(items: Vec<(&str, MenuAction)>) -> gtk4::MenuButton {
|
||||
let mut menu = ActionMenu::new();
|
||||
for (label, run) in items {
|
||||
menu.item(label, run);
|
||||
}
|
||||
menu.button()
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
pub(crate) mod compat;
|
||||
pub(crate) mod details;
|
||||
pub(crate) mod file_list;
|
||||
pub(crate) mod menu;
|
||||
pub(crate) mod scrubber;
|
||||
pub(crate) mod share_dialog;
|
||||
pub(crate) mod thumbnails;
|
||||
pub(crate) mod versions_dialog;
|
||||
@@ -0,0 +1,403 @@
|
||||
//! The Photos timeline's month scrubber, laid over the grid's right edge as in
|
||||
//! Google Photos: a thin tick where each year starts, a knob at the month on
|
||||
//! screen, and a bubble naming the month under the pointer. It stays out of
|
||||
//! the way until the pointer nears the edge or the timeline scrolls.
|
||||
|
||||
use crate::*;
|
||||
use gtk4::cairo;
|
||||
|
||||
/// Width of the strip the scrubber draws in, in px.
|
||||
const STRIP_WIDTH: i32 = 72;
|
||||
/// How close to the right edge the pointer has to come to show the scrubber.
|
||||
const EDGE: f64 = 96.0;
|
||||
/// Space kept free above the first and below the last month, in px.
|
||||
const PAD: f64 = 16.0;
|
||||
/// How long the scrubber stays up after the timeline stops scrolling.
|
||||
const LINGER: Duration = Duration::from_millis(1500);
|
||||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
/// How many months the strip spans, newest at the top.
|
||||
months: usize,
|
||||
/// Where each year starts, as `(month index, label)`.
|
||||
years: Vec<(usize, String)>,
|
||||
/// The month the knob marks.
|
||||
value: usize,
|
||||
/// Whether the timeline has months to move between at all.
|
||||
available: bool,
|
||||
/// The pointer is near the right edge.
|
||||
near: bool,
|
||||
/// The month under the pointer while it is on the strip.
|
||||
hover: Option<usize>,
|
||||
/// Where the running drag started, in the strip's coordinates.
|
||||
drag_from: Option<f64>,
|
||||
/// Keeps the scrubber up for a moment after a scroll.
|
||||
linger: Option<glib::SourceId>,
|
||||
label: Option<Rc<dyn Fn(usize) -> String>>,
|
||||
on_pick: Option<Rc<dyn Fn(usize)>>,
|
||||
}
|
||||
|
||||
/// The scrubber's widgets and state. Cheap to clone: every clone is the same
|
||||
/// scrubber.
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct Scrubber {
|
||||
revealer: gtk4::Revealer,
|
||||
area: gtk4::DrawingArea,
|
||||
knob: gtk4::Box,
|
||||
bubble: gtk4::Label,
|
||||
/// The timeline the scrubber lies over.
|
||||
scroll: Option<gtk4::ScrolledWindow>,
|
||||
state: Rc<RefCell<State>>,
|
||||
}
|
||||
|
||||
impl Scrubber {
|
||||
/// Build the scrubber over `host`, whose child is the scrolled timeline.
|
||||
pub(crate) fn new(host: >k4::Overlay) -> Self {
|
||||
let scroll = host.child().and_downcast::<gtk4::ScrolledWindow>();
|
||||
let area = gtk4::DrawingArea::builder()
|
||||
.width_request(STRIP_WIDTH)
|
||||
.vexpand(true)
|
||||
.build();
|
||||
area.add_css_class("photo-scrubber");
|
||||
let knob = gtk4::Box::builder()
|
||||
.width_request(24)
|
||||
.height_request(4)
|
||||
.halign(gtk4::Align::End)
|
||||
.valign(gtk4::Align::Start)
|
||||
.margin_end(4)
|
||||
.can_target(false)
|
||||
.build();
|
||||
knob.add_css_class("photo-scrubber-knob");
|
||||
let strip = gtk4::Overlay::new();
|
||||
strip.set_child(Some(&area));
|
||||
strip.add_overlay(&knob);
|
||||
let revealer = gtk4::Revealer::builder()
|
||||
.transition_type(gtk4::RevealerTransitionType::Crossfade)
|
||||
.transition_duration(150)
|
||||
.halign(gtk4::Align::End)
|
||||
.valign(gtk4::Align::Fill)
|
||||
.can_target(false)
|
||||
.child(&strip)
|
||||
.build();
|
||||
let bubble = gtk4::Label::builder()
|
||||
.halign(gtk4::Align::End)
|
||||
.valign(gtk4::Align::Start)
|
||||
.margin_end(STRIP_WIDTH)
|
||||
.can_target(false)
|
||||
.visible(false)
|
||||
.build();
|
||||
bubble.add_css_class("photo-scrubber-bubble");
|
||||
host.add_overlay(&revealer);
|
||||
host.add_overlay(&bubble);
|
||||
|
||||
let scrubber = Self {
|
||||
revealer,
|
||||
area,
|
||||
knob,
|
||||
bubble,
|
||||
scroll,
|
||||
state: Rc::default(),
|
||||
};
|
||||
scrubber.wire(host);
|
||||
scrubber
|
||||
}
|
||||
|
||||
fn wire(&self, host: >k4::Overlay) {
|
||||
let this = self.clone();
|
||||
self.area.set_draw_func(move |area, cr, width, height| {
|
||||
this.draw(area, cr, width, height);
|
||||
});
|
||||
let this = self.clone();
|
||||
self.area.connect_resize(move |_, _, _| this.place_knob());
|
||||
|
||||
// Near the edge is judged on the whole timeline, not the strip: the
|
||||
// strip takes no input while it is hidden.
|
||||
let edge = gtk4::EventControllerMotion::new();
|
||||
let this = self.clone();
|
||||
let host_motion = host.clone();
|
||||
edge.connect_motion(move |_, x, _| {
|
||||
let near = x >= f64::from(host_motion.width()) - EDGE;
|
||||
if this.state.borrow().near != near {
|
||||
this.state.borrow_mut().near = near;
|
||||
this.sync_reveal();
|
||||
}
|
||||
});
|
||||
let this = self.clone();
|
||||
edge.connect_leave(move |_| {
|
||||
this.state.borrow_mut().near = false;
|
||||
this.sync_reveal();
|
||||
});
|
||||
host.add_controller(edge);
|
||||
|
||||
let hover = gtk4::EventControllerMotion::new();
|
||||
let this = self.clone();
|
||||
hover.connect_motion(move |_, _, y| {
|
||||
let index = this.index_at(y);
|
||||
this.state.borrow_mut().hover = index;
|
||||
this.sync_bubble();
|
||||
});
|
||||
let this = self.clone();
|
||||
hover.connect_leave(move |_| {
|
||||
this.state.borrow_mut().hover = None;
|
||||
this.sync_bubble();
|
||||
});
|
||||
self.area.add_controller(hover);
|
||||
|
||||
// A click is a drag that goes nowhere, so this covers both.
|
||||
let drag = gtk4::GestureDrag::new();
|
||||
let this = self.clone();
|
||||
drag.connect_drag_begin(move |_, _, y| {
|
||||
this.state.borrow_mut().drag_from = Some(y);
|
||||
this.pick(y, true);
|
||||
});
|
||||
let this = self.clone();
|
||||
drag.connect_drag_update(move |_, _, dy| {
|
||||
let from = this.state.borrow().drag_from;
|
||||
if let Some(from) = from {
|
||||
this.pick(from + dy, false);
|
||||
}
|
||||
});
|
||||
let this = self.clone();
|
||||
drag.connect_drag_end(move |_, _, _| {
|
||||
this.state.borrow_mut().drag_from = None;
|
||||
this.sync_reveal();
|
||||
this.sync_bubble();
|
||||
});
|
||||
self.area.add_controller(drag);
|
||||
}
|
||||
|
||||
/// Span `months` months, newest first, with a tick and a label where each
|
||||
/// of `years` starts. Moves the knob back to the top.
|
||||
pub(crate) fn set_months(&self, months: usize, years: Vec<(usize, String)>) {
|
||||
{
|
||||
let mut state = self.state.borrow_mut();
|
||||
state.months = months;
|
||||
state.years = years;
|
||||
state.value = 0;
|
||||
}
|
||||
self.area.queue_draw();
|
||||
self.place_knob();
|
||||
}
|
||||
|
||||
/// Whether there is anything to scrub through. The scrubber never shows
|
||||
/// while there is not.
|
||||
pub(crate) fn set_available(&self, available: bool) {
|
||||
self.state.borrow_mut().available = available;
|
||||
// The scrubber takes the scrollbar's place at the edge; both at once
|
||||
// would fight over the same strip.
|
||||
if let Some(scroll) = &self.scroll {
|
||||
scroll.set_vscrollbar_policy(if available {
|
||||
gtk4::PolicyType::External
|
||||
} else {
|
||||
gtk4::PolicyType::Automatic
|
||||
});
|
||||
}
|
||||
self.sync_reveal();
|
||||
}
|
||||
|
||||
pub(crate) fn is_available(&self) -> bool {
|
||||
self.state.borrow().available
|
||||
}
|
||||
|
||||
/// Mark month `index`, unless the user has the scrubber in hand: the
|
||||
/// timeline's scroll position must not move the knob out from under them.
|
||||
pub(crate) fn set_value(&self, index: usize) {
|
||||
{
|
||||
let mut state = self.state.borrow_mut();
|
||||
if state.drag_from.is_some() || state.hover.is_some() || state.value == index {
|
||||
return;
|
||||
}
|
||||
state.value = index;
|
||||
}
|
||||
self.place_knob();
|
||||
}
|
||||
|
||||
/// How a month index reads in the bubble, such as "March 2024".
|
||||
pub(crate) fn set_label_func(&self, label: impl Fn(usize) -> String + 'static) {
|
||||
self.state.borrow_mut().label = Some(Rc::new(label));
|
||||
}
|
||||
|
||||
/// Call `f` with the month the user points the scrubber at.
|
||||
pub(crate) fn connect_pick(&self, f: impl Fn(usize) + 'static) {
|
||||
self.state.borrow_mut().on_pick = Some(Rc::new(f));
|
||||
}
|
||||
|
||||
/// Show the scrubber for a moment, as the timeline scrolls, so it can be
|
||||
/// found without hunting for the edge — and on a touch screen at all.
|
||||
pub(crate) fn flash(&self) {
|
||||
if !self.is_available() {
|
||||
return;
|
||||
}
|
||||
if let Some(source) = self.state.borrow_mut().linger.take() {
|
||||
source.remove();
|
||||
}
|
||||
let this = self.clone();
|
||||
let source = glib::timeout_add_local_once(LINGER, move || {
|
||||
this.state.borrow_mut().linger = None;
|
||||
this.sync_reveal();
|
||||
});
|
||||
self.state.borrow_mut().linger = Some(source);
|
||||
self.sync_reveal();
|
||||
}
|
||||
|
||||
fn sync_reveal(&self) {
|
||||
let show = {
|
||||
let state = self.state.borrow();
|
||||
state.available && (state.near || state.drag_from.is_some() || state.linger.is_some())
|
||||
};
|
||||
self.revealer.set_reveal_child(show);
|
||||
// A hidden revealer still claims its strip of the grid; let the clicks
|
||||
// through to the tiles underneath.
|
||||
self.revealer.set_can_target(show);
|
||||
if !show {
|
||||
self.state.borrow_mut().hover = None;
|
||||
self.bubble.set_visible(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// The bubble names the month being dragged to, or else the one under
|
||||
/// the pointer.
|
||||
fn sync_bubble(&self) {
|
||||
let (index, label) = {
|
||||
let state = self.state.borrow();
|
||||
let index = if state.drag_from.is_some() {
|
||||
Some(state.value)
|
||||
} else {
|
||||
state.hover
|
||||
};
|
||||
(index, state.label.clone())
|
||||
};
|
||||
let (Some(index), Some(label)) = (index, label) else {
|
||||
self.bubble.set_visible(false);
|
||||
return;
|
||||
};
|
||||
self.bubble.set_label(&label(index));
|
||||
let (_, height, _, _) = self.bubble.measure(gtk4::Orientation::Vertical, -1);
|
||||
let y = self.y_of(index) - f64::from(height) / 2.0;
|
||||
self.bubble.set_margin_top(y.max(0.0) as i32);
|
||||
self.bubble.set_visible(true);
|
||||
}
|
||||
|
||||
/// Move the knob to month `y` points at, and tell the page when that is a
|
||||
/// different month — or when a new drag starts, so a click jumps even to
|
||||
/// the month already marked.
|
||||
fn pick(&self, y: f64, starting: bool) {
|
||||
let Some(index) = self.index_at(y) else {
|
||||
return;
|
||||
};
|
||||
let (changed, on_pick) = {
|
||||
let mut state = self.state.borrow_mut();
|
||||
let changed = state.value != index;
|
||||
state.value = index;
|
||||
(changed, state.on_pick.clone())
|
||||
};
|
||||
self.place_knob();
|
||||
self.sync_bubble();
|
||||
if let (true, Some(on_pick)) = (changed || starting, on_pick) {
|
||||
on_pick(index);
|
||||
}
|
||||
}
|
||||
|
||||
fn place_knob(&self) {
|
||||
let value = self.state.borrow().value;
|
||||
let y = self.y_of(value) - 2.0;
|
||||
self.knob.set_margin_top(y.max(0.0) as i32);
|
||||
}
|
||||
|
||||
/// Where month `index` sits on the strip.
|
||||
fn y_of(&self, index: usize) -> f64 {
|
||||
let months = self.state.borrow().months;
|
||||
month_y(index, months, f64::from(self.area.height()))
|
||||
}
|
||||
|
||||
/// The month at height `y` on the strip, if it spans any.
|
||||
fn index_at(&self, y: f64) -> Option<usize> {
|
||||
let months = self.state.borrow().months;
|
||||
month_at(y, months, f64::from(self.area.height()))
|
||||
}
|
||||
|
||||
fn draw(&self, area: >k4::DrawingArea, cr: &cairo::Context, width: i32, height: i32) {
|
||||
let state = self.state.borrow();
|
||||
let color = area.color();
|
||||
let (width, height) = (f64::from(width), f64::from(height));
|
||||
let attrs = gtk4::pango::AttrList::new();
|
||||
attrs.insert(gtk4::pango::AttrFloat::new_scale(0.8));
|
||||
attrs.insert(gtk4::pango::AttrInt::new_weight(gtk4::pango::Weight::Bold));
|
||||
// The bottom of the last label drawn: a year crowded against the one
|
||||
// above it keeps its tick but not its label.
|
||||
let mut clear_below = f64::NEG_INFINITY;
|
||||
for (index, year) in &state.years {
|
||||
let y = month_y(*index, state.months, height);
|
||||
cr.set_source_rgba(
|
||||
f64::from(color.red()),
|
||||
f64::from(color.green()),
|
||||
f64::from(color.blue()),
|
||||
0.5,
|
||||
);
|
||||
cr.rectangle(width - 12.0, y.round() - 0.5, 8.0, 1.0);
|
||||
let _ = cr.fill();
|
||||
|
||||
let layout = area.create_pango_layout(Some(year));
|
||||
layout.set_attributes(Some(&attrs));
|
||||
let (_, logical) = layout.pixel_extents();
|
||||
let (w, h) = (f64::from(logical.width()), f64::from(logical.height()));
|
||||
let top = y - h / 2.0;
|
||||
if top < clear_below {
|
||||
continue;
|
||||
}
|
||||
clear_below = top + h + 2.0;
|
||||
cr.set_source_rgba(
|
||||
f64::from(color.red()),
|
||||
f64::from(color.green()),
|
||||
f64::from(color.blue()),
|
||||
0.8,
|
||||
);
|
||||
cr.move_to(width - 16.0 - w, top);
|
||||
pangocairo::functions::show_layout(cr, &layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Where month `index` of `months` sits on a strip `height` px tall: evenly
|
||||
/// spaced, newest at the top.
|
||||
fn month_y(index: usize, months: usize, height: f64) -> f64 {
|
||||
let span = (height - 2.0 * PAD).max(0.0);
|
||||
if months < 2 {
|
||||
return PAD;
|
||||
}
|
||||
PAD + span * index as f64 / (months - 1) as f64
|
||||
}
|
||||
|
||||
/// The month nearest height `y` on a strip `height` px tall.
|
||||
fn month_at(y: f64, months: usize, height: f64) -> Option<usize> {
|
||||
let last = months.checked_sub(1)?;
|
||||
let span = height - 2.0 * PAD;
|
||||
if span <= 0.0 {
|
||||
return Some(0);
|
||||
}
|
||||
let fraction = ((y - PAD) / span).clamp(0.0, 1.0);
|
||||
Some((fraction * last as f64).round() as usize)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_scrubber_maps_heights_back_to_their_months() {
|
||||
let height = 432.0;
|
||||
for index in 0..12 {
|
||||
assert_eq!(
|
||||
month_at(month_y(index, 12, height), 12, height),
|
||||
Some(index)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_scrubber_clamps_past_either_end() {
|
||||
assert_eq!(month_at(-50.0, 12, 432.0), Some(0));
|
||||
assert_eq!(month_at(900.0, 12, 432.0), Some(11));
|
||||
assert_eq!(month_at(10.0, 0, 432.0), None);
|
||||
}
|
||||
}
|
||||
@@ -97,7 +97,7 @@ impl Busy {
|
||||
pub(crate) fn start(button: >k4::Button) -> Self {
|
||||
let label = button.label();
|
||||
let icon = button.icon_name();
|
||||
let spinner = gtk4::Spinner::builder().spinning(true).build();
|
||||
let spinner = spinner();
|
||||
match &label {
|
||||
Some(text) => {
|
||||
let content = gtk4::Box::new(gtk4::Orientation::Horizontal, 6);
|
||||
@@ -407,6 +407,14 @@ pub(crate) fn open_share_dialog(ui: &Rc<Ui>, entry: &DirEntry) {
|
||||
);
|
||||
});
|
||||
|
||||
// Shared by me lists what this dialog changes: reload it when it is the
|
||||
// page underneath. Any other page reads it again on the next visit.
|
||||
let ui_closed = ui.clone();
|
||||
dialog.connect_closed(move |_| {
|
||||
if ui_closed.stack.visible_child_name().as_deref() == Some("sharedbyme") {
|
||||
load_shared_by_me(&ui_closed);
|
||||
}
|
||||
});
|
||||
share_dialog_reload(&state);
|
||||
dialog.present(ui_window(ui).as_ref());
|
||||
}
|
||||
@@ -427,7 +435,7 @@ pub(crate) fn share_dialog_reload(state: &Rc<ShareDialog>) {
|
||||
_ => toast_error(
|
||||
&state.ui,
|
||||
&gettext("Couldn't load sharing"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -582,6 +590,7 @@ pub(crate) fn repaint_share_link(state: &Rc<ShareDialog>, link: Option<&PublicLi
|
||||
let url_copy = url.clone();
|
||||
copy.connect_clicked(move |btn| {
|
||||
btn.clipboard().set_text(&url_copy);
|
||||
flash_copied(btn, "edit-copy-symbolic");
|
||||
toast(&state_copy.ui, &gettext("Link copied"));
|
||||
});
|
||||
row.add_suffix(©);
|
||||
@@ -718,7 +727,7 @@ pub(crate) fn share_dialog_create_link(
|
||||
_ => toast_error(
|
||||
&state.ui,
|
||||
&gettext("Couldn't create the link"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -756,7 +765,7 @@ pub(crate) fn share_dialog_op(
|
||||
toast_error(
|
||||
&state.ui,
|
||||
&failed,
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
);
|
||||
share_dialog_reload(&state);
|
||||
}
|
||||
|
||||
@@ -214,19 +214,6 @@ pub(crate) fn bind_file_thumbnail(
|
||||
schedule_file_thumbs(ui);
|
||||
}
|
||||
|
||||
/// Create and immediately bind a thumbnail for a non-recycled row.
|
||||
pub(crate) fn file_thumbnail(
|
||||
ui: &Rc<Ui>,
|
||||
entry: &DirEntry,
|
||||
size: i32,
|
||||
fallback_size: i32,
|
||||
symbolic_fallback: bool,
|
||||
) -> gtk4::Overlay {
|
||||
let widget = file_thumbnail_widget(size, fallback_size);
|
||||
bind_file_thumbnail(ui, &widget, entry, symbolic_fallback);
|
||||
widget
|
||||
}
|
||||
|
||||
/// Whether the file name denotes an image format for which Proton or the local
|
||||
/// decoder can reasonably provide a thumbnail.
|
||||
pub(crate) fn is_image_name(name: &str) -> bool {
|
||||
|
||||
@@ -167,7 +167,7 @@ pub(crate) fn versions_dialog_reload(state: &Rc<VersionsDialog>) {
|
||||
_ => toast_error(
|
||||
&state.ui,
|
||||
&gettext("Couldn't load versions"),
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
@@ -204,13 +204,13 @@ fn repaint_versions(state: &Rc<VersionsDialog>, items: &[RevisionInfo]) {
|
||||
gettext_f(
|
||||
"{time} · {size}",
|
||||
&[
|
||||
("time", &activity_time(item.created)),
|
||||
("time", &dates::relative(item.created)),
|
||||
("size", &human_bytes(size)),
|
||||
],
|
||||
),
|
||||
)
|
||||
} else {
|
||||
(activity_time(item.created), human_bytes(size))
|
||||
(dates::relative(item.created), human_bytes(size))
|
||||
};
|
||||
if let Some(email) = &item.signed_by {
|
||||
subtitle.push_str(" · ");
|
||||
@@ -266,7 +266,7 @@ fn repaint_versions(state: &Rc<VersionsDialog>, items: &[RevisionInfo]) {
|
||||
/// every device on the account, not only this one.
|
||||
fn prompt_restore_version(state: &Rc<VersionsDialog>, revision_id: &str) {
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Restore version"))
|
||||
.heading(gettext("Restore Version?"))
|
||||
// Translators: {name} is a file name.
|
||||
.body(gettext_f(
|
||||
"Make this version the current content of “{name}”? The version it replaces stays in the history.",
|
||||
@@ -301,7 +301,7 @@ fn prompt_restore_version(state: &Rc<VersionsDialog>, revision_id: &str) {
|
||||
/// Confirm, then permanently delete one revision.
|
||||
fn prompt_delete_version(state: &Rc<VersionsDialog>, revision_id: &str) {
|
||||
let dialog = adw::AlertDialog::builder()
|
||||
.heading(gettext("Delete version"))
|
||||
.heading(gettext("Delete Version?"))
|
||||
.body(gettext(
|
||||
"Delete this version permanently? Its content can't be recovered.",
|
||||
))
|
||||
@@ -377,7 +377,7 @@ fn versions_dialog_op(state: &Rc<VersionsDialog>, request: Request, done: &str,
|
||||
_ => toast_error(
|
||||
&state.ui,
|
||||
&failed,
|
||||
&gettext("The mount service didn't respond."),
|
||||
&gettext("The Proton Drive service didn't respond."),
|
||||
),
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
//! Widgets that newer libadwaita releases bring, with a stand-in built from
|
||||
//! what the oldest supported release (1.5, as on Ubuntu 24.04) has.
|
||||
//!
|
||||
//! The app builds against 1.5 and picks at run time: when the libadwaita it
|
||||
//! runs with is 1.8 or newer, the real widgets are made by class name through
|
||||
//! a `gtk4::Builder`, since the 1.5 bindings have no types for them. Their
|
||||
//! properties and signals are then reached by name. Call sites never care
|
||||
//! which one they got.
|
||||
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use gtk4::prelude::*;
|
||||
|
||||
/// Set to anything to use the stand-ins even on a new libadwaita, to see the
|
||||
/// app as it looks on the oldest supported release.
|
||||
const STAND_INS_ENV: &str = "PDFS_STAND_IN_WIDGETS";
|
||||
|
||||
/// Whether the libadwaita the app runs with has the 1.8 widgets.
|
||||
pub(crate) fn has_adw_1_8() -> bool {
|
||||
static HAS: OnceLock<bool> = OnceLock::new();
|
||||
*HAS.get_or_init(|| {
|
||||
std::env::var_os(STAND_INS_ENV).is_none()
|
||||
&& (adw::major_version(), adw::minor_version()) >= (1, 8)
|
||||
})
|
||||
}
|
||||
|
||||
/// Build `xml`, a GtkBuilder interface naming classes the bindings lack. It
|
||||
/// is only ever one this module wrote, so a failure is a bug here.
|
||||
pub(crate) fn build_ui(xml: &str) -> gtk4::Builder {
|
||||
gtk4::Builder::from_string(&format!("<interface>{xml}</interface>"))
|
||||
}
|
||||
|
||||
/// The object `id` from a builder [`build_ui`] made.
|
||||
pub(crate) fn built<T: IsA<gtk4::glib::Object>>(builder: >k4::Builder, id: &str) -> T {
|
||||
builder
|
||||
.object(id)
|
||||
.unwrap_or_else(|| panic!("the compat UI has no object {id}"))
|
||||
}
|
||||
|
||||
/// A busy spinner. It spins whenever it is shown: show and hide it with
|
||||
/// `set_visible` rather than starting and stopping it.
|
||||
pub(crate) type Spinner = gtk4::Widget;
|
||||
|
||||
/// A new [`Spinner`], shown: `AdwSpinner` on libadwaita 1.8, a spinning
|
||||
/// `gtk4::Spinner` before.
|
||||
pub(crate) fn spinner() -> Spinner {
|
||||
if has_adw_1_8() {
|
||||
let builder = build_ui(r#"<object class="AdwSpinner" id="spinner"/>"#);
|
||||
built(&builder, "spinner")
|
||||
} else {
|
||||
// Spinning costs nothing while hidden: an unmapped widget draws no
|
||||
// frames.
|
||||
gtk4::Spinner::builder().spinning(true).build().upcast()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
//! Match highlighting: which characters of a result name the query matched,
|
||||
//! as Pango markup with those characters in bold.
|
||||
//!
|
||||
//! Each query term is looked for as a substring of the name first, and as a
|
||||
//! subsequence ("rpt" in "report") when that fails, which is roughly how the
|
||||
//! daemon's ranking treats a term too. Case is folded one character at a
|
||||
//! time, so every range stays on a character boundary of the original name,
|
||||
//! whatever the script.
|
||||
|
||||
use gtk4::glib;
|
||||
|
||||
/// One character of a name: its byte range and its case-folded form.
|
||||
struct Folded {
|
||||
start: usize,
|
||||
end: usize,
|
||||
folded: String,
|
||||
}
|
||||
|
||||
fn fold(text: &str) -> Vec<Folded> {
|
||||
text.char_indices()
|
||||
.map(|(start, c)| Folded {
|
||||
start,
|
||||
end: start + c.len_utf8(),
|
||||
folded: c.to_lowercase().collect(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The byte ranges of `name` that `query` matched, sorted and merged.
|
||||
pub(crate) fn ranges(name: &str, query: &str) -> Vec<(usize, usize)> {
|
||||
let chars = fold(name);
|
||||
let mut found: Vec<(usize, usize)> = Vec::new();
|
||||
for term in query.split_whitespace() {
|
||||
let term: Vec<String> = fold(term).into_iter().map(|c| c.folded).collect();
|
||||
if let Some(at) = substring(&chars, &term) {
|
||||
found.push((chars[at].start, chars[at + term.len() - 1].end));
|
||||
} else if let Some(hits) = subsequence(&chars, &term) {
|
||||
found.extend(hits.into_iter().map(|at| (chars[at].start, chars[at].end)));
|
||||
}
|
||||
}
|
||||
found.sort_unstable();
|
||||
let mut merged: Vec<(usize, usize)> = Vec::with_capacity(found.len());
|
||||
for (start, end) in found {
|
||||
match merged.last_mut() {
|
||||
Some(last) if start <= last.1 => last.1 = last.1.max(end),
|
||||
_ => merged.push((start, end)),
|
||||
}
|
||||
}
|
||||
merged
|
||||
}
|
||||
|
||||
/// Where `term` first occurs in `chars`, as a character index.
|
||||
fn substring(chars: &[Folded], term: &[String]) -> Option<usize> {
|
||||
if term.is_empty() || term.len() > chars.len() {
|
||||
return None;
|
||||
}
|
||||
(0..=chars.len() - term.len()).find(|&at| {
|
||||
chars[at..at + term.len()]
|
||||
.iter()
|
||||
.zip(term)
|
||||
.all(|(c, t)| c.folded == *t)
|
||||
})
|
||||
}
|
||||
|
||||
/// The character indices of the leftmost match of `term` as a subsequence,
|
||||
/// or `None` when not every character of it can be found in order.
|
||||
fn subsequence(chars: &[Folded], term: &[String]) -> Option<Vec<usize>> {
|
||||
if term.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut hits = Vec::with_capacity(term.len());
|
||||
let mut next = 0;
|
||||
for wanted in term {
|
||||
let at = (next..chars.len()).find(|&i| chars[i].folded == *wanted)?;
|
||||
hits.push(at);
|
||||
next = at + 1;
|
||||
}
|
||||
Some(hits)
|
||||
}
|
||||
|
||||
/// `name` as Pango markup, escaped, with the matched characters in bold.
|
||||
pub(crate) fn markup(name: &str, query: &str) -> String {
|
||||
let mut out = String::with_capacity(name.len() + 16);
|
||||
let mut done = 0;
|
||||
for (start, end) in ranges(name, query) {
|
||||
out.push_str(&glib::markup_escape_text(&name[done..start]));
|
||||
out.push_str("<b>");
|
||||
out.push_str(&glib::markup_escape_text(&name[start..end]));
|
||||
out.push_str("</b>");
|
||||
done = end;
|
||||
}
|
||||
out.push_str(&glib::markup_escape_text(&name[done..]));
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_substring_match_is_one_range_in_any_case() {
|
||||
assert_eq!(ranges("Tax Report.pdf", "REP"), vec![(4, 7)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_term_that_is_no_substring_falls_back_to_a_subsequence() {
|
||||
// r, p and t of "report", not the "rt" pair at the end of a substring.
|
||||
assert_eq!(ranges("report.txt", "rpt"), vec![(0, 1), (2, 3), (5, 6)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_term_with_no_match_highlights_nothing() {
|
||||
assert!(ranges("report.txt", "xyz").is_empty());
|
||||
assert!(ranges("report.txt", "").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn several_terms_merge_into_sorted_ranges() {
|
||||
assert_eq!(
|
||||
ranges("tax return 2024", "2024 tax ret"),
|
||||
vec![(0, 3), (4, 7), (11, 15)]
|
||||
);
|
||||
assert_eq!(ranges("abc", "ab bc"), vec![(0, 3)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_ascii_names_keep_ranges_on_character_boundaries() {
|
||||
assert_eq!(ranges("Über Größe.odt", "über"), vec![(0, 5)]);
|
||||
assert_eq!(ranges("Über Größe.odt", "GRÖ"), vec![(6, 10)]);
|
||||
assert_eq!(markup("Über Größe.odt", "größe"), "Über <b>Größe</b>.odt");
|
||||
assert_eq!(
|
||||
markup("日本語のメモ.txt", "メモ"),
|
||||
"日本語の<b>メモ</b>.txt"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn markup_escapes_the_name_inside_and_outside_matches() {
|
||||
assert_eq!(markup("a<b>&c", "b"), "a<<b>b</b>>&c");
|
||||
assert_eq!(markup("R&D.pdf", "r&d"), "<b>R&D</b>.pdf");
|
||||
assert_eq!(markup("<none>", "zzz"), "<none>");
|
||||
}
|
||||
}
|
||||