diff --git a/README.md b/README.md index bbd313e..f806262 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,11 @@ A deleted file usually stays on the drive until something new is written on top | Your situation | Can Reclaim help? | | --- | --- | | SD card, camera card or USB stick | **Usually yes.** These usually use FAT32 or exFAT, which Reclaim reads. | -| External hard drive | **Yes**, if it's formatted as NTFS, FAT32 or exFAT. | +| External hard drive | **Yes**, if it's formatted as NTFS, FAT32, exFAT, ext2, ext3, ext4 or HFS+ (Mac OS Extended). | | Windows computer's internal drive | **Yes** for hard drives. For SSDs, chances are low (see [SSDs](#why-are-ssds-different)). | -| Your Mac's built-in drive | **Not yet.** Macs use APFS, which Reclaim can't read yet. Memory cards and USB drives used on a Mac are fine. | -| Linux system drive | **Not yet.** It's usually ext4 or Btrfs, which Reclaim can't read yet. | +| Your Mac's built-in drive | **Not yet** on current Macs, which use APFS. Older Macs and external drives formatted as **Mac OS Extended (HFS+)** work. Memory cards and USB drives used on a Mac are fine. | +| Linux drive | **Yes** for ext2, ext3 and ext4. **Not yet** for Btrfs or XFS. On an SSD, chances are low (see [SSDs](#why-are-ssds-different)). | +| Windows Server or Dev Drive using ReFS | **Not yet.** Reclaim recognizes ReFS but can't read it yet. | | Files that were overwritten | **No.** No software can bring back data that new files have replaced. | Reclaim is an **early preview**, and no recovery tool can promise every file will come back. @@ -62,7 +63,7 @@ Pick the file for your computer from the **[latest release](https://github.com/s ## How to recover your files 1. **Choose the drive** where the files were. Connected drives appear on their own; click **Refresh** after plugging one in. If the drive is encrypted (BitLocker, FileVault, LUKS), unlock it first. -2. **Click Start scanning.** Files appear while the scan runs. You can stop as soon as you see what you need. +2. **Click Start scanning.** Files appear while the scan runs. You can recover them right away, without waiting for the scan to finish, or stop the scan as soon as you see what you need. 3. **Look through the results.** Search by name or type, filter by category (photos, documents…), and click a file to see its details and a preview for pictures. 4. **Tick the files you want and click Recover selected.** Pick a folder on a **different drive**. Then open the recovered files to check they are complete. @@ -121,7 +122,7 @@ No. Everything stays on your computer. Reclaim only contacts GitHub to check for | **Windows: access denied** | Close Reclaim, right-click it and choose **Run as administrator**. | | **Mac: access denied** | Reading a whole drive needs an administrator password. Also check **System Settings → Privacy & Security** for a request to allow access to removable volumes or Full Disk Access. | | **Linux: access denied** | Accept the password prompt that appears when you open a drive. If no prompt appears, install your desktop's polkit authentication agent and UDisks2. | -| **"Unused-space scanning needs a supported formatted volume"** | Choose the partition or volume (for example `sdb1`), not the whole disk (`sdb`). The drive must be NTFS, FAT or exFAT. | +| **"Unused-space scanning needs a supported formatted volume"** | Choose the partition or volume (for example `sdb1`), not the whole disk (`sdb`). The drive must be NTFS, FAT, exFAT, ext2/3/4 or HFS+. ReFS, APFS, Btrfs and XFS can't be scanned yet. | | **Scan finished with unreadable areas, or the drive stopped responding** | Check the cable and try once more. If it happens again, the drive may be failing: recover the files already found and stop using it. | | **A recovered file won't open** | It may be incomplete or overwritten. Try other results of the same type. | | **Windows: Reclaim won't start** | An error message explains why. A log is saved at `%LOCALAPPDATA%\Reclaim\logs\reclaim.log`; attach it when you [report a problem](https://github.com/sudo-py-dev/reclaim/issues). | @@ -144,7 +145,7 @@ cd reclaim cargo run --release --locked ``` -The [developer guide](docs/DEVELOPMENT.md) covers tests, drive permissions, architecture and detailed limits. See also the [format research](docs/FILE_FORMAT_RESEARCH.md), [translation notes](locales/README.md), [screenshot instructions](docs/screenshots/README.md), [code and UI audit](docs/CODE_UI_AUDIT.md) and the [website](website/README.md). +The [developer guide](docs/DEVELOPMENT.md) covers building, drive permissions, architecture and detailed limits, and [TESTING.md](docs/TESTING.md) every test and its results. See also the [format research](docs/FILE_FORMAT_RESEARCH.md), [translation notes](locales/README.md) and the [website](website/README.md).
How scanning works diff --git a/docs/CODE_UI_AUDIT.md b/docs/CODE_UI_AUDIT.md deleted file mode 100644 index 56bcc35..0000000 --- a/docs/CODE_UI_AUDIT.md +++ /dev/null @@ -1,90 +0,0 @@ -# Code and interface audit - -Reviewed 2026-11-02. Scope: scanner and ZIP classification, result filtering, -thumbnail loading/caching, desktop event handling, and existing UI regression tests. - -## Changes - -| Finding | Correction | -| --- | --- | -| Empty searches constructed filenames and searchable strings for every result | Skip text construction for an empty query | -| Nonempty searches lowercased individual fields separately | Lowercase the combined search text once, preserving cross-field matching | -| Thumbnail cache eviction repeated inside the visible-row loop | Evict once per rendered row range; binary search sorted visible indices | -| Result widget IDs used source offsets, which need not be unique | Scope each row by its stable result index within the current scan | -| Resident thumbnail loading copied the complete compressed image | Borrow existing immutable bytes; own only bytes read from disk | -| ZIP package detection retained every archive entry name | Track only the fixed set of recognized package markers | -| ZIP classification searched arbitrary amounts of payload for the footer | Search only the final 65,557 bytes permitted for a classic ZIP footer and comment | -| ZIP classification repeated directory checks already completed by `valid_zip` | Reuse validated directory bounds | - -These reduce work and allocations. No end-to-end throughput percentage has been -measured; disk access and decoding can still dominate performance. - -## Repeatable checks - -Everything runs with the normal Rust toolchain; there are no script dependencies. - -```sh -cargo test --locked # all tests, including the project audit -cargo test --locked --test audit # the audit on its own -cargo clippy --locked --all-targets -- -D warnings -``` - -The project audit (`tests/audit`) parses the source and checks, with each rule -tagged by CWE: `unsafe` confined to the platform layer and documented; no -`static mut`, `transmute`, leaks or unreviewed uninitialised memory; programs -started only by the two native launchers; no plain HTTP, TLS bypasses, weak -hashes or committed secrets; private temporary files; explicit bounds on reads -and allocations sized by scanned data; a ratchet on unchecked indexing; no debug -leftovers, silenced warnings, orphan files or em dashes in user-facing text; -every interface message translated, no dead catalog keys, matching placeholders, -and `ui/app.slint` defaults in step with their translations; exact dependency -versions, current advisory exceptions and a fuzz lockfile that matches -production. A finding can be accepted only next to its code with -`// audit-allow(RULE): reason`, and unused acceptances fail too. - -`Cargo.toml` sets the compiler-enforced half (`[lints]`: no `unwrap`, `expect`, -`panic`, stray prints or undocumented `unsafe` in shipped code), and CI runs -`cargo-deny` against the RustSec advisory database. `RECLAIM_PREVIEW_DIR` with -`cargo test --bin reclaim slint_workspace` still writes headless UI renderings. - -## Rules for future changes - -- Keep disk reads, decoding, downloads, and authentication off the render thread. -- Keep scan queues bounded and cancellation checks inside long-running loops. -- Preserve read-only source access and export without overwriting existing files. -- Use unique, stable widget IDs; preserve virtualization of long result lists. -- Avoid allocating per-result search strings when the search is empty. -- Preserve English/Hebrew catalogs, placeholders, keyboard controls, and RTL data. -- Check minimum and default window sizes in light and dark appearances. -- Keep image decoding and candidate parsing bounded; validate untrusted sizes. -- Prefer existing components and dependencies; simplify only with equivalent behavior. - -The Rust project audit enforces the file/catalog rules. Tests and compiler checks -cover selected behavior. The architectural rules above still require code review; -text matching cannot prove UI responsiveness or memory safety. - -## Verification and limits - -Headless previews cover minimum/default sizes, both appearances, Hebrew layouts, -settings, scan progress, and results. - -Automatic layout checks (`src/layout_checks.rs`, run by -`layout_is_clear_and_steady_on_every_page_language_and_size`) go further than -previews: on every page, with no source, during a scan with long file names, and -with the update notice open, in English, Hebrew, Arabic and German, at -900x640 and 1160x780, every button, checkbox, dropdown and text field must lie -inside the window and must not overlap another control. Controls inside a -scroll view are judged by the part that is visible. The scan status headline -must keep the same size and position whether it says "scanning" or names a -slow check. Representative source selection, results, -Hebrew, and active scan previews were visually inspected. Automated interaction -coverage includes keyboard controls, modal behavior, filtering/export, and window -commands. Hardware/device tests and the live HTTPS test remain opt-in. - -A headless render does not verify the actual compositor, native dialogs, screen -readers, authentication, or Windows/macOS hardware behavior. The file list -updates incrementally: a refresh appends new results and patches rows whose -selection changed, and rebuilds only when the search, category or result list -changes; a test streams 2,000 results and checks the list always equals a full -rebuild. Image -previews remain sequential background work with bounded decoding limits. diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index 5a007be..9b1fcfa 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -7,7 +7,7 @@ A Rust desktop app for recovering file candidates from drives and raw disk image ## Features - **Native drive discovery:** Linux sysfs, Windows storage APIs, and macOS IOKit/Disk Arbitration. No `lsblk`, PowerShell, `diskutil`, or `plutil` subprocesses. -- **Unused-space default:** NTFS, FAT12/16/32, and exFAT volume maps; occupied clusters and filesystem metadata are excluded from signature carving. No full-source fallback. +- **Unused-space default:** NTFS, FAT12/16/32, exFAT, ext2/3/4, and HFS+/HFSX volume maps (ReFS is detected and refused); occupied clusters and filesystem metadata are excluded from signature carving. No full-source fallback. - **Live scan results:** unused-byte progress, cancellation, and recoverable partial results. - **Filtered recovery:** search by filename, extension, or source offset; filter by file category. Recovery saves only selected files matching the active filters. - **Safe output naming:** original NTFS leaf names when available, generated names otherwise, and numeric suffixes to avoid replacing existing files. @@ -85,70 +85,30 @@ Reclaim is not a complete filesystem recovery suite. A signature match does not ## Development ```sh -cargo fmt --check +cargo fmt --all -- --check cargo test --locked cargo clippy --locked --all-targets -- -D warnings ``` -`cargo test` includes the project audit in `tests/audit` (security rules tagged -by CWE, translation coverage and dead keys, supply-chain policy). Run it alone -with `cargo test --locked --test audit`. See [the code and UI audit](CODE_UI_AUDIT.md) -for what it checks and how to accept a reviewed finding. +`cargo test` includes the project audit and the headless UI renders. +[TESTING.md](TESTING.md) lists every suite, the audit rules, the layout checks, +fuzzing, the opt-in hardware tests, CI and the real USB drive results. -Tests cover signature boundaries, image validation, NTFS records, filtered exports, collision protection, sector-aligned reads, native Linux metadata fixtures, cancellation, and Slint page rendering in both themes. +### Data preservation -### Data preservation and USB checks - -Recovery blocks a destination known to share the scanned physical drive, even -after a confirmation dialog. Unknown topology still requires explicit review; -choose a destination you know is on another physical drive. Linux refuses to +Recovery blocks a destination known to share the scanned physical drive. The +in-app destination dialog names the folder and offers only "Choose another +folder" or Cancel; the drive is checked again just before writing. When the +drive cannot be determined, the dialog also offers "Save here anyway", for a +destination you know is on another physical drive. The dialog is drawn by +Reclaim itself, not a native message box, so it starts no helper program +(such as zenity on Linux) and cannot fail silently when one is missing. +The folder picker opens where the last recovery saved. Linux refuses to certify separation for anonymous filesystem device numbers (including Btrfs) or incomplete device-mapper/RAID topology. A single mountinfo source does not describe every disk in a multi-device filesystem. -The security regression tests simulate a USB disconnect after a partial read, -competing exports with the same filename, invalid candidate ranges, existing -files and symlinks. Failed exports must leave no published partial file or -temporary staging file, and successful exports must preserve existing data: - -```sh -cargo test --locked --test security -``` - -Two opt-in Linux hardware tests only read the selected device or verify that -same-drive export is rejected. They do not format, delete, write test files, or -export recovery candidates. Set the device and its existing mount directory -explicitly, checking their identity first: - -```sh -RECLAIM_TEST_DEVICE=/dev/sdX1 cargo test --locked --bin reclaim live_named_volume_can_read_data -- --ignored --nocapture -RECLAIM_TEST_DEVICE=/dev/sdX1 RECLAIM_TEST_DESTINATION=/media/user/USB cargo test --locked --bin reclaim live_usb_export_to_source_drive_is_blocked_before_any_write -- --ignored -``` - -The read test scans only the first 16 MiB. Normal read permissions or the -desktop's read-only UDisks authorization are required. Disconnect/error behavior -is simulated in automated tests; do not unplug a device during an actual export -or use a failing drive for repeated hardware tests. Mounted source contents and -mount topology can still change externally; these checks are not a guarantee -against every hardware failure or mount race. - -Optional headless UI previews: - -```sh -mkdir -p /tmp/reclaim-previews -RECLAIM_PREVIEW_DIR=/tmp/reclaim-previews cargo test --locked --bin reclaim slint_workspace -``` - -The same test run also checks layout automatically: no control leaves the window or overlaps another on any page, state, language or window size, and the scan status line does not move between states (see [the code and UI audit](CODE_UI_AUDIT.md)). - -The previews are PNG images rendered from the actual Slint components using its software renderer. They do not test native window management, authentication prompts, or real-device recovery. - -[Desktop CI](../.github/workflows/build.yml) runs locked tests (including the UI -and project audit) and Clippy with warnings as errors on Linux, Windows, and -macOS. Formatting runs once on Linux. Separate jobs check dependency advisories, -bans and sources, and install the website's locked dependencies, type-check it, -and build it. Checks run on pull requests, main, and weekly; newer commits cancel -older PR runs. Third-party actions are pinned to immutable revisions. +### Builds and releases Tests compile the app and UI in the test profile; PR checks do not perform a second optimized build, package installers, or upload executable artifacts. @@ -157,8 +117,17 @@ workflow before building optimized binaries and native installers for all four shipping targets. Only releases upload artifacts, sign updates, and publish GitHub downloads. Release runs are serialized per tag and artifacts expire after seven days. macOS binaries use ad-hoc signing; Developer ID signing and -notarization are not configured. Native desktop and hardware testing remain -necessary; headless tests do not replace them. +notarization are not configured. + +### Rules for changes + +- Keep disk reads, decoding, downloads, and authentication off the render thread. +- Keep scan queues bounded and cancellation checks inside long-running loops. +- Preserve read-only source access and export without overwriting existing files. +- Use unique, stable widget IDs; preserve virtualization of long result lists. +- Keep image decoding and candidate parsing bounded; validate untrusted sizes. +- Check minimum and default window sizes in light, dark and right-to-left layouts. +- Prefer existing components and dependencies; simplify only with equivalent behavior. ## License and credits diff --git a/docs/DISTRIBUTION.md b/docs/DISTRIBUTION.md index 48f2c30..de5b9e2 100644 --- a/docs/DISTRIBUTION.md +++ b/docs/DISTRIBUTION.md @@ -86,6 +86,28 @@ Build committed source without copying local credentials, caches or disk images: packaging/snap/build.sh ``` +If a local LXD build cannot obtain network access, use Canonical's remote builders +from a full Git checkout of the committed public source (shallow clones are not +supported). Authorize Snapcraft in Launchpad when prompted. Remote builds publish +the source snapshot to Launchpad. Keep build logs private: + +```sh +umask 077 +snapcraft remote-build --build-for=amd64 --launchpad-accept-public-upload +``` + +A remote build downloads the snap into the current directory. Move it where the +upload steps below expect it, replacing any older local build: + +```sh +mkdir -p dist +mv -f reclaim_*_amd64.snap dist/ +``` + +The recipe installs Ubuntu's Rustup package and invokes Rust 1.99.0 explicitly. +Toolchain files are installed only in the build environment; they are not shipped +in the application snap. + The result is `dist/reclaim__amd64.snap`. Upload and request release with: ```sh diff --git a/docs/TESTING.md b/docs/TESTING.md new file mode 100644 index 0000000..26ec689 --- /dev/null +++ b/docs/TESTING.md @@ -0,0 +1,208 @@ +# Testing + +Every check Reclaim has, how to run it, and what each one proves. The last +part records the tests on a real USB drive and their results. + +## Before every change + +```sh +cargo fmt --all -- --check +cargo test --locked +cargo clippy --locked --all-targets -- -D warnings +cargo clippy --locked --target x86_64-pc-windows-gnu --all-targets -- -D warnings +``` + +`cargo test` runs every suite below, including the project audit and the +headless UI renders. On 2026-11-12 it ran 183 tests with 4 opt-in tests +ignored; all passed on Linux, and both Clippy runs passed. CI runs the same +tests and Clippy on Linux, Windows and macOS (see [CI](#ci)). + +## Automated tests + +| Suite | What it checks | +| --- | --- | +| `src/` unit tests (library) | Signature boundaries, image and archive validation, NTFS records, sector-aligned and resilient reads, unreadable-sector skipping, allocation maps for FAT12/16/32, exFAT, NTFS, ext2/3/4 and HFS+/HFSX, BLOCK_UNINIT, unknown ext features, ReFS refusal, ext2 block-map parsing against hostile data, cancellation and progress | +| `src/` unit tests (app) | Drive discovery parsing on Linux, Windows and macOS, drive hot-plug refresh, recovery and export rules, update metadata and signatures, translations, native process hardening, diagnostics | +| `src/slint_ui_tests.rs` | Renders every page headlessly and runs the layout checks below; keyboard controls, modal dialogs, filtering, export and window commands | +| `tests/security.rs` | USB disconnect after a partial read, competing exports with the same name, invalid candidate ranges, existing files and symbolic links. A failed export must leave no partial or staging file; a successful one must not touch existing data | +| `tests/image_detection.rs`, `container_detection.rs`, `new_formats.rs`, `expanded_formats.rs` | Each supported file type is found, sized and named correctly, and damaged copies are rejected | +| `tests/large_zip.rs`, `zip_scan_work.rs` | Large archives are classified with bounded work | +| `tests/ntfs_resident.rs` | Small files stored inside NTFS records | +| `tests/ext_journal.rs` | A split, deleted file comes back whole and with its name from the ext3 and ext4 journal (kernel-written fixtures) | +| `tests/ext_block_maps.rs` | A split, deleted 322 KB file comes back whole on ext2 from the freed block maps, including the second map level (kernel-written fixture) | +| `tests/audit` | The project audit (next section) | + +The fixtures are described in [tests/fixtures/README.md](../tests/fixtures/README.md). + +## Project audit + +`tests/audit` parses the source and fails the build on any of these, each rule +tagged by CWE: + +- `unsafe` outside the listed platform files or without a `SAFETY` comment; no + `static mut`, `transmute`, leaks or unreviewed uninitialised memory. +- Programs started by anything other than the two native launchers. +- Plain HTTP, TLS bypasses, weak hashes, committed secrets, non-private + temporary files. +- Reads and allocations sized by scanned data without an explicit bound, and a + ratchet (`tests/audit/baseline.toml`) on unchecked indexing. +- Debug leftovers, silenced warnings, orphan files, em dashes in user text. +- An interface message missing from a catalog, dead catalog keys, mismatched + placeholders, or `ui/app.slint` defaults out of step with their translations. +- An informal "you" in the German, French or Russian catalog (all three address + the user formally), or a Latin term ending in `+` or `#` ("HFS+") in Hebrew + or Arabic without the left-to-right mark that keeps the symbol in place. +- Inexact dependency versions, stale advisory exceptions, or a fuzz lockfile + that differs from production. + +A finding can be accepted only next to its code with +`// audit-allow(RULE): reason`; an acceptance that no longer matches fails too. +Run it alone with `cargo test --locked --test audit`. `Cargo.toml` adds the +compiler half: no `unwrap`, `expect`, `panic`, stray prints or undocumented +`unsafe` in shipped code. CI also runs `cargo-deny` against the RustSec +advisory database. + +## Interface + +`layout_is_clear_and_steady_on_every_page_language_and_size` checks every page, +with no source, during a scan with long file names, and with the update notice +open, in English, Hebrew, Arabic and German, at 900x640 and 1160x780. Every +button, checkbox, dropdown and text field must lie inside the window and must +not overlap another control; controls in a scroll view are judged by their +visible part. The "Select all" caption and the found and selected counts never +elide; with counts of 250,000 both must still lie inside the list column. The scan headline must keep its size and position whether it says +"scanning" or names a slow check. A streaming test adds 2,000 results and checks +the list always equals a full rebuild. + +Headless previews (PNG files from the real Slint components, software +rendered) cover all three pages in all seven languages, light and dark, and +English, Hebrew and Arabic at the compact size: + +```sh +mkdir -p /tmp/reclaim-previews +RECLAIM_PREVIEW_DIR=/tmp/reclaim-previews cargo test --locked --bin reclaim slint_workspace +``` + +The screenshots in `docs/screenshots/` come from these renders. They do not +test the real compositor, native dialogs, screen readers, authentication +prompts or Windows and macOS hardware. + +## Fuzzing + +Three cargo-fuzz targets cover the scanner, the source reader and the +unused-space maps. Setup and commands are in [fuzz/README.md](../fuzz/README.md). + +## Opt-in hardware tests + +Two Linux tests only read the selected device or check that exporting to the +same drive is refused. They never format, delete or write test files. Check the +device name first: + +```sh +RECLAIM_TEST_DEVICE=/dev/sdX1 cargo test --locked --bin reclaim live_named_volume_can_read_data -- --ignored --nocapture +RECLAIM_TEST_DEVICE=/dev/sdX1 RECLAIM_TEST_DESTINATION=/media/user/USB cargo test --locked --bin reclaim live_usb_export_to_source_drive_is_blocked_before_any_write -- --ignored +``` + +The read test scans only the first 16 MiB and needs read permission or the +desktop's read-only UDisks prompt. Do not unplug a drive during a real export +or test on a failing drive; disconnects are covered by the simulated tests. + +## Filesystem images made with real tools + +Read-only scans were run on images made with `mkfs.fat` (FAT12, FAT16, FAT32), +`mkfs.exfat` and `mkntfs`, and on `mkfs.ext2` (1 KiB blocks), `mkfs.ext3` +(2 KiB) and `mkfs.ext4` (4 KiB, 64bit, flex_bg, metadata_csum) with a file +written and deleted through `debugfs`. The deleted file was found and the kept +file was not, and free bytes matched `dumpe2fs` exactly on ext2 and ext3. HFS+ +was only tested with images built by the unit tests. + +## Real USB drive + +### Setup + +A 57 GB SanDisk USB 3.2 stick, used only for testing with its owner's +permission, was split into one partition per filesystem: FAT12 (4 MB), FAT16 +(256 MB), and 1 GB each of FAT32, exFAT, NTFS, ext2, ext3 and ext4. Linux 7.0 +formatted and wrote them through udisks (FAT and exFAT by the kernel drivers, +NTFS by `ntfs3`, ext by the ext4 driver). + +The test set was 48 real files, 49 MB in total: JPEG, PNG, WebP, PDF, Ogg, +WAV, ZIP and GZIP, from 40 KB to 12 MB. FAT12 got 30 small files (2.7 MB). The +files went into nested folders (`Photos/2024 Trip`, `Documents` and the root) +and were then deleted. + +Each partition was copied once, read-only, to an image file, and every scan ran +on the copy. A file counts only if it came back byte for byte (SHA-256 match); +"damaged" means the start matches but not the whole file. + +### Rounds + +1. **Empty drive:** the test files written to a fresh partition and deleted. +2. **Used drive:** 300 other files kept on the drive around the deleted ones. +3. **Full drive:** the partition filled with alternating gap files and kept + files (192 KiB and 256 KiB; 48 KiB and 16 KiB on FAT12), the rest filled, + then the gaps deleted. The test files were written into the gaps, so + anything larger than a gap was split, and then deleted. This is the hardest + case: the drive has to place files in the leftover holes. + +### Results + +Files recovered whole, out of 48 (FAT12 out of 30): + +| Round | FAT12 | FAT16 | FAT32 | exFAT | NTFS | ext2 | ext3 | ext4 | +| --- | --- | --- | --- | --- | --- | --- | --- | --- | +| Empty drive | 30 | 48 | 48 | 48 | 48 | 46 | 47 | 48 | +| Used drive | not run | 48 | 48 | 48 | 48 | 46 | 47 | 47 | +| Full drive | 30 | 48 | 47 | 48 | 48 | 35 | 47 | 47 | + +FAT12 also returned every original file name. The used round was not run on +FAT12 because 4 MB does not hold the test files and 300 others. + +### What the rounds changed + +The first runs found real gaps, and each was fixed and measured again: + +| Filesystem, round | Before | After | Fix | +| --- | --- | --- | --- | +| FAT32, full | 17 | 47 | FAT32 erases a deleted file's chain, but fills gaps in order. Free space is now carved as one stream with used space skipped, so a split file joins up; formats with internal checks reject wrong joins. A bug where skipping a bad sector could also skip the next healthy free area was fixed on the way | +| ext4, full | 30 | 47 | The journal often keeps a copy of a deleted file's inode and directory entry; files are rebuilt from it, with names, only when every block is still free | +| ext3, used | 8 | 47 | Same journal reader | +| ext2, empty and used | 8 | 46 | Freed indirect blocks keep the list of where a file's pieces are; carving now reads each file's pieces in order | + +### Remaining misses + +- **One WAV, everywhere:** `cp` stored its silent stretches as holes, which + real camera and phone files do not have. A test artifact. +- **One PDF on FAT32 and ext2:** it has text after its final `%%EOF`, which + carving cannot see. +- **ext2, full drive (11 more):** when a file's first 12 blocks were split + themselves, their locations are erased with the file. Hundreds of stale maps + from the filler files fit the same gaps, so guessing the start would join + the wrong data; it is left to carving. + +### Repeating it + +The scoring harness was a temporary ignored test, not kept in the repository. +To repeat: format a stick you can erase, write the test files into each +partition in the three layouts above, copy each partition to an image with the +desktop's read-only authorization, run `scan_unused_reader` on the image, and +compare SHA-256 hashes of what `FoundReader` returns against the originals. + +## CI + +[Desktop CI](../.github/workflows/build.yml) runs the locked tests (including the +audit and UI) and Clippy with warnings as errors on Linux, Windows and macOS; +formatting once on Linux; `cargo-deny` for advisories, bans and sources; and a +type-check and build of the website. It runs on pull requests, on `main` and +weekly, with actions pinned to commit hashes. The +[release workflow](../.github/workflows/release.yml) runs the same checks before +building installers. Snap packages are built and smoke-tested in their own +workflow, which fails if the window does not appear. + +## Not covered + +Native window management, file dialogs, screen readers and authentication +prompts are not tested automatically. Windows and macOS hardware were not +tested from this Linux host; CI builds and tests them but does not scan real +drives. HFS+ was not tested on a real drive. Mounted volumes can change during +a scan; scanning an unchanged image is the reliable path. diff --git a/docs/UNUSED_SPACE_SCANNING.md b/docs/UNUSED_SPACE_SCANNING.md index c464a4f..fc9fb11 100644 --- a/docs/UNUSED_SPACE_SCANNING.md +++ b/docs/UNUSED_SPACE_SCANNING.md @@ -19,13 +19,83 @@ Supported maps: Fragmented runlists within a base record are supported, including MFT-relative reads across runs. Compressed/sparse/encrypted/split allocation streams requiring ATTRIBUTE_LIST extensions are rejected. Metadata clusters must be allocated. +- **ext2/ext3/ext4:** superblock geometry and feature flags, the group descriptor + table (32-byte or 64-bit descriptors, flex_bg placement) and each group's block + bitmap. Groups flagged BLOCK_UNINIT under group checksums never held data and + are skipped. Compression, external journal devices, META_BG and bigalloc are + rejected. The superblock, descriptor table and bitmap blocks must be allocated. + Descriptor checksums are not verified. A mounted journaled volume may have + allocations still in its journal, so its bitmap can lag by a few blocks. +- **HFS+ and HFSX:** volume header geometry and the allocation file (one bit per + block, most significant bit first) from the eight extents in the header. An + allocation file that needs the extents overflow file is rejected. The volume + header, its backup and the allocation file must be allocated. HFS wrapped in a + classic HFS volume is not recognized. +- **ReFS:** recognized by its boot sector and refused with its own message. Its + free-space structures are not publicly documented, so no map is guessed. -Every free extent has its own bounded reader. Candidate seeks and lookahead cannot -cross an occupied gap. Found offsets map back to original volume offsets for -export. The scanner's buffer shrinks for small extents to avoid allocating 1 MiB -for every isolated free cluster. Aggregate progress counts **unused bytes**, not -the physical offset currently being examined; 100% means all mapped free extents -were searched. A volume with no free clusters finishes without carving data. +## Deleted files from filesystem records + +Before carving, the map step also reads the records a filesystem keeps for +deleted files, and reports those files first: + +- **NTFS:** deleted MFT records with an unnamed, uncompressed, fully + initialized `$DATA` stream. The record's cluster runs give the exact size and + every fragment, so fragmented files come back whole, with their original name + when the record still has one. Linux's `ntfs3` driver writes records in the + older NTFS 3.0 layout (update array at 42) and removes the name on delete; + those records are accepted and their type is taken from the file's first + bytes. +- **FAT12/16/32:** deleted directory entries, with the long name when its + checksum matches the short name (the short name's lost first letter is + restored from the long name). The FAT chain of a deleted file is cleared, so + the data is assumed to be stored in one piece. +- **exFAT:** deleted entry sets. Files flagged as stored in one piece come back + exactly; others follow their FAT chain when it is intact and unused. + +A record is used only when every cluster it names is still free, so its data +cannot belong to a newer file. Its space is then removed from carving only if +the file's first bytes match its type (MP4, MOV, HEIC and Matroska headers are +recognised here too); otherwise the file is listed as unverified and its space +is still carved. Walks are bounded: 32 directory levels, 20,000 directories, +128 MiB of directory data and 50,000 files per volume. + +- **ext3/ext4 journal:** Linux erases a deleted file's block list and name. + The journal often still holds older copies of the inode-table and directory + blocks from when the file existed. The newest copy of each inode that is free + now, with an extent tree (ext4) or block map (ext3) whose blocks are all + still unused, gives the file's exact size and every piece; names come from + journaled directory blocks. Block maps with holes (sparse files) are skipped. + +- **ext2/ext3 block maps:** past its first 12 blocks, an ext2 or ext3 file + lists its blocks in separate indirect blocks. Linux clears the list in the + inode on delete, but the freed indirect blocks keep their contents. Free + space is read once for blocks that hold a list of free block numbers; the 12 + free blocks before the first listed block are taken as the file's start when + they sit together and open with a known file signature, and a second map + level (double indirect) continues files past the first map. Nothing is + listed from a map directly: carving reads each such file's pieces in order + and still checks the result, so a wrong guess only changes the order of + unrelated free space. Where two maps claim the same space, the longer one is + used, since the kernel can leave a shorter, older copy. Bounded at 200,000 + maps and two million block runs; this pass is half of the scan's progress. + +**ext2 limit:** when a file's first 12 blocks were themselves split (a nearly +full drive), their locations are erased with the inode and the file is only +found if carving joins the pieces. Files above 4 GiB (triple indirect) are not +rebuilt from maps. + +**Carving across used space.** All free ranges are carved as one stream in disk +order, leaving out the space still in use. When a drive fills its gaps in +order (as FAT32 does), a fragmented file continues in the next free range and +comes back whole; its pieces are recorded so recovery reads the same bytes. +Formats with an internal check (image decode, checksums, ZIP directory) reject +a wrong join. Occupied space is never read for carving. After a read error, +the skipped area never reaches past the free range it started in. + +Found offsets map back to original volume offsets for export. Aggregate +progress counts **unused bytes**; 100% means all mapped free space was searched. +A volume with no free clusters finishes without carving data. The filesystem map is read at scan start, not an operating-system snapshot. Other programs can change a mounted volume afterward. The existing advice to stop using @@ -33,7 +103,7 @@ the drive, or scan an unchanged volume image, still applies. Sector-aligned sour caching can read adjacent bytes, but signatures are never searched in occupied clusters. Allocation metadata itself must be read even though it is allocated. -Whole-disk MBR/GPT images, unsupported filesystems (including ext4/APFS), and +Whole-disk MBR/GPT images, unsupported filesystems (including APFS, Btrfs, XFS and ReFS), and unreadable/damaged maps produce an actionable error. Select a supported formatted volume/partition. Deleted NTFS resident records inside allocated MFT storage are not searched in this mode. The explicit library `scan_reader` remains available @@ -49,12 +119,11 @@ budgets. Cancellation is checked during mapping as well as signature scanning. - [Microsoft FAT on-disk structures](https://github.com/Microsoft/Windows-driver-samples/blob/main/filesys/fastfat/fat.h): BPB geometry and FAT12/16/32 interpretation, cross-checked with [libfsfat's format documentation](https://github.com/libyal/libfsfat/blob/main/documentation/File%20Allocation%20Table%20%28FAT%29%20format.asciidoc). - [Microsoft MFT documentation](https://learn.microsoft.com/en-us/windows/win32/devnotes/master-file-table): MFT DATA and record-six volume bitmap, distinct from the MFT record-allocation BITMAP attribute. - [libfsntfs format documentation](https://github.com/libyal/libfsntfs/blob/main/documentation/New%20Technologies%20File%20System%20%28NTFS%29.asciidoc): NTFS boot layout, record fixups, nonresident attributes and signed relative data runs. +- [Linux kernel ext4 disk layout](https://docs.kernel.org/filesystems/ext4/globals.html): superblock fields, feature flags, group descriptors and block-group flags. +- [Apple Technical Note TN1150](https://developer.apple.com/library/archive/technotes/tn/tn1150.html): HFS+ volume header, fork data and allocation file bit order. ## Validation -Portable tests cover occupied-file exclusion, exact free-file export offsets, -occupied-gap isolation, FAT disagreement, NTFS resident/nonresident bitmaps and -fragmented MFT streams, exFAT metadata protection, malformed geometry, fail-closed -errors, cancellation, and unused-byte progress. Manual read-only smoke scans also -completed on regular-file images created with `mkfs.fat` (FAT12, FAT16, FAT32), -`mkfs.exfat`, and `mkntfs`. No user drive was formatted or modified. +Unit tests, kernel-written fixtures, images made with real tools and a real +USB drive with every supported filesystem are described, with results, in +[TESTING.md](TESTING.md#real-usb-drive). diff --git a/docs/runtime-dependencies.md b/docs/runtime-dependencies.md deleted file mode 100644 index 4f43c26..0000000 --- a/docs/runtime-dependencies.md +++ /dev/null @@ -1,24 +0,0 @@ -# Runtime dependency policy - -File detection, carving, image validation, and export run in Rust. No FFmpeg, -SQLite executable, shell, PowerShell, diskutil, or lsblk is required by the app. -Checked-in media/database fixtures need no external decoder during tests. - -Drive discovery uses Linux kernel files and UDisks D-Bus APIs, Windows Win32 -APIs, or macOS IOKit/Disk Arbitration APIs. Native dialogs and accessibility -use OS libraries and desktop services. Those services are allowed API providers; -this policy does not require the operating system to run in one process. - -The updater uses HTTPS through Rust libraries and SHA-256 verification. Unix -executable replacement uses Rust filesystem operations. On Windows, Rust code -starts the verified Inno Setup package directly through the Win32 process API; -the app never launches cmd.exe, PowerShell, or a cleanup executable. Native OS -APIs open browser links and installer files without command helpers. - -On Linux a native D-Bus session address is required. Process-backed transports, -autolaunch, and fallback address lists are rejected before starting the GUI. - -Clippy rejects first-party process-command creation, and cargo-deny bans -self-replace, webbrowser, and open helper dependencies. Development and release -packaging use the separate Rust tooling crate (`cargo xtask`), Rust security tools, and native platform packaging -tools. They are not invoked by the installed application. diff --git a/docs/screenshots/README.md b/docs/screenshots/README.md deleted file mode 100644 index 8b06e79..0000000 --- a/docs/screenshots/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Interface previews - -The screenshots show the actual Slint components with sample source and result data. Generate software-rendered previews with: - -```sh -RECLAIM_PREVIEW_DIR=/tmp/reclaim-previews cargo test --locked --bin reclaim slint_workspace -``` - -This renders Sources, Recovered files, and Settings & help across all seven languages in light and dark themes. English, Hebrew, and Arabic are also rendered at the compact window size. Filenames include locale, width, page, and theme. Native dialogs and physical device access need desktop testing. diff --git a/docs/security-review.md b/docs/security-review.md index 3227d96..6146e39 100644 --- a/docs/security-review.md +++ b/docs/security-review.md @@ -198,3 +198,28 @@ audit and headless UI renders) and `ntfs.rs`, `detection.rs`, `source.rs`, `cfb.rs`, Windows and macOS drive discovery) still contain unchecked indexing and arithmetic, mostly after explicit bounds checks. The audit ratchet keeps their counts from growing. + +## Runtime dependency policy + +File detection, carving, image validation, and export run in Rust. No FFmpeg, +SQLite executable, shell, PowerShell, diskutil, or lsblk is required by the app. +Checked-in media/database fixtures need no external decoder during tests. + +Drive discovery uses Linux kernel files and UDisks D-Bus APIs, Windows Win32 +APIs, or macOS IOKit/Disk Arbitration APIs. Native dialogs and accessibility +use OS libraries and desktop services. Those services are allowed API providers; +this policy does not require the operating system to run in one process. + +The updater uses HTTPS through Rust libraries and SHA-256 verification. Unix +executable replacement uses Rust filesystem operations. On Windows, Rust code +starts the verified Inno Setup package directly through the Win32 process API; +the app never launches cmd.exe, PowerShell, or a cleanup executable. Native OS +APIs open browser links and installer files without command helpers. + +On Linux a native D-Bus session address is required. Process-backed transports, +autolaunch, and fallback address lists are rejected before starting the GUI. + +Clippy rejects first-party process-command creation, and cargo-deny bans +self-replace, webbrowser, and open helper dependencies. Development and release +packaging use the separate Rust tooling crate (`cargo xtask`), Rust security tools, and native platform packaging +tools. They are not invoked by the installed application. diff --git a/locales/ar.json b/locales/ar.json index 844b819..956d544 100644 --- a/locales/ar.json +++ b/locales/ar.json @@ -62,6 +62,9 @@ "System": "النظام", "Too many files with the same recovered name": "ملفات كثيرة جدًا بنفس الاسم المستعاد", "USB drive": "محرك أقراص USB", + "Phone or camera": "هاتف أو كاميرا", + "File transfer only": "لنقل الملفات فقط", + "Phones and cameras connect for file transfer only, so Reclaim can’t search them for deleted files. If yours has a memory card, put the card in a card reader and scan it here. Also check its Trash or Recently deleted folder.": "تتصل الهواتف والكاميرات لنقل الملفات فقط، لذلك لا يستطيع Reclaim البحث فيها عن الملفات المحذوفة. إذا كان في جهازك بطاقة ذاكرة، فضعها في قارئ بطاقات وافحصها هنا. تحقق أيضًا من مجلد سلة المهملات أو المحذوفة مؤخرًا في الجهاز.", "Unlocked volume": "وحدة تخزين مفتوحة القفل", "Unverified candidate": "ملف محتمل غير متحقق منه", "Video": "الفيديو", @@ -77,6 +80,7 @@ "Choose a drive": "اختر محرك أقراص", "Scan stopped": "توقف الفحص", "Stop": "إيقاف", + "Stop recovery": "إيقاف الاستعادة", "File type": "نوع الملف", "Size": "الحجم", "bytes": "بايت", @@ -112,7 +116,10 @@ "{0} of {1}": "{0} من {1}", "Reading unused-space map…": "جارٍ قراءة خريطة المساحة غير المستخدمة…", "No unused space is available on this volume.": "لا تتوفر مساحة غير مستخدمة على وحدة التخزين هذه.", - "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, or exFAT). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "يتطلب فحص المساحة غير المستخدمة وحدة تخزين مهيأة بتنسيق مدعوم (NTFS أو FAT12/16/32 أو exFAT). اختر وحدة التخزين أو القسم بدلًا من صورة القرص الكامل. لم تُفحص المساحة المشغولة.", + "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, exFAT, ext2/3/4, or HFS+). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "يتطلب فحص المساحة غير المستخدمة وحدة تخزين مهيأة بتنسيق مدعوم (NTFS أو FAT12/16/32 أو exFAT أو ext2/3/4 أو HFS+‎). اختر وحدة التخزين أو القسم بدلًا من صورة القرص الكامل. لم تُفحص المساحة المشغولة.", + "This is a ReFS volume. Reclaim cannot read the ReFS free-space map yet, so this volume was not scanned.": "هذه وحدة تخزين ReFS. لا يستطيع Reclaim قراءة خريطة المساحة الحرة في ReFS بعد، لذلك لم تُفحص وحدة التخزين هذه.", + "This ext volume uses a feature Reclaim cannot map yet (compression, an external journal, META_BG, or bigalloc), so it was not scanned.": "تستخدم وحدة التخزين ext هذه ميزة لا يستطيع Reclaim التعامل معها بعد (الضغط أو سجل يومي خارجي أو META_BG أو bigalloc)، لذلك لم تُفحص.", + "This HFS+ volume's free-space map is too fragmented for Reclaim to read yet, so it was not scanned.": "خريطة المساحة الحرة في وحدة التخزين HFS+‎ هذه مجزأة أكثر مما يستطيع Reclaim قراءته حاليًا، لذلك لم تُفحص.", "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive.": "قد يؤدي حفظ الملفات المستعادة على محرك الأقراص الفعلي نفسه الذي فحصته إلى الكتابة فوق البيانات التي تحاول استعادتها وإتلافها نهائيًا. اختر مجلدًا على محرك أقراص فعلي آخر. تابع فقط إذا كانت الوجهة على محرك أقراص آخر.", "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.": "قد يؤدي الحفظ هنا إلى الكتابة فوق الملفات المحذوفة التي تحاول استعادتها وإتلافها نهائيًا. اختر مجلدًا على محرك أقراص فعلي آخر، مثل محرك أقراص USB.", "Could not connect to the update server. Check your internet connection and try again.": "تعذر الاتصال بخادم التحديثات. تحقق من اتصالك بالإنترنت وحاول مجددًا.", @@ -147,7 +154,7 @@ "Recover in three steps": "الاستعادة في ثلاث خطوات", "1. Stop using the drive you lost files from. Don't save, download or install anything to it.\n\n2. Choose that drive or a raw disk image, then scan its unused space. Stop any time and keep partial results.\n\n3. Inspect and select files, then save them to a different physical drive.": "1. توقف عن استخدام القرص الذي فقدت منه الملفات. لا تحفظ عليه أو تنزّل إليه أو تثبّت عليه أي شيء.\n\n2. اختر ذلك القرص أو صورة قرص خام، ثم افحص مساحته غير المستخدمة. يمكنك التوقف في أي وقت والاحتفاظ بالنتائج الجزئية.\n\n3. افحص الملفات وحددها ثم احفظها على قرص فعلي آخر.", "Supported formats": "الصيغ المدعومة", - "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, and NTFS volumes.": "صور ومستندات وأرشيفات وصوت وفيديو وتطبيقات وملفات NTFS صغيرة مقيمة. فحص المساحة غير المستخدمة في وحدات تخزين FAT وexFAT وNTFS.", + "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, NTFS, ext2/3/4, and HFS+ volumes.": "صور ومستندات وأرشيفات وصوت وفيديو وتطبيقات وملفات NTFS صغيرة مقيمة. فحص المساحة غير المستخدمة في وحدات تخزين FAT وexFAT وNTFS وext2/3/4 وHFS+‎.", "Built with Rust and Slint · MIT licensed application": "مبني باستخدام Rust وSlint · تطبيق بترخيص MIT", "Locked": "مقفل", "Stopping after the current step…": "جارٍ التوقف بعد الخطوة الحالية…", @@ -217,5 +224,8 @@ "Could not restart as administrator: {0}": "تعذرت إعادة التشغيل كمسؤول: {0}", "The source stopped returning data before its reported end. It may be truncated, disconnected, or restricted by the operating system. Files already found can still be recovered.": "توقف المصدر عن إرجاع البيانات قبل نهايته المعلنة. قد يكون مبتورًا أو غير متصل أو مقيدًا بواسطة نظام التشغيل. لا يزال بإمكانك استعادة الملفات التي تم العثور عليها.", "Could not prepare read-only access to this volume: {0}": "تعذر إعداد الوصول للقراءة فقط إلى وحدة التخزين هذه: {0}", - "Updates are managed by the Snap Store.": "تتم إدارة التحديثات بواسطة متجر Snap." + "Updates are managed by the Snap Store.": "تتم إدارة التحديثات بواسطة متجر Snap.", + "Choose another folder": "اختيار مجلد آخر", + "Save here anyway": "الحفظ هنا على أي حال", + "Cancel": "إلغاء" } diff --git a/locales/de.json b/locales/de.json index e9fc1e9..3bdfcf2 100644 --- a/locales/de.json +++ b/locales/de.json @@ -50,18 +50,21 @@ "Refresh": "Aktualisieren", "Save to a different physical drive": "Auf einem anderen physischen Laufwerk speichern", "Saved {n} file. Open the destination folder and verify their contents.": "{n} Datei gespeichert. Öffnen Sie den Zielordner und prüfen Sie den Inhalt.", - "Saved {n} files. Open the destination folder and verify their contents.": "{n} Dateien gespeichert. Öffne den Zielordner und prüfe ihren Inhalt.", + "Saved {n} files. Open the destination folder and verify their contents.": "{n} Dateien gespeichert. Öffnen Sie den Zielordner und prüfen Sie den Inhalt.", "Scan complete. {} file candidate found.": "Scan abgeschlossen. {} mögliche Datei gefunden.", "Scan complete. {} file candidates found.": "Scan abgeschlossen. {} mögliche Dateien gefunden.", "Scan failed. See the message above.": "Scan fehlgeschlagen. Siehe Meldung oben.", "Scan stopped. You can recover partial results.": "Scan angehalten. Sie können Teilergebnisse wiederherstellen.", - "Scan the selected source before recovering files.": "Durchsuche die ausgewählte Quelle vor der Wiederherstellung.", + "Scan the selected source before recovering files.": "Durchsuchen Sie die ausgewählte Quelle vor der Wiederherstellung.", "Scanning for recoverable files…": "Wiederherstellbare Dateien werden gesucht…", "Source changed or candidate is truncated": "Quelle geändert oder mögliche Datei abgeschnitten", "Source changed since scanning. Scan it again before recovering.": "Quelle seit dem Scan geändert. Scannen Sie sie vor der Wiederherstellung erneut.", "System": "System", "Too many files with the same recovered name": "Zu viele Dateien mit gleichem wiederhergestellten Namen", "USB drive": "USB-Laufwerk", + "Phone or camera": "Smartphone oder Kamera", + "File transfer only": "Nur Dateiübertragung", + "Phones and cameras connect for file transfer only, so Reclaim can’t search them for deleted files. If yours has a memory card, put the card in a card reader and scan it here. Also check its Trash or Recently deleted folder.": "Smartphones und Kameras verbinden sich nur zur Dateiübertragung, daher kann Reclaim sie nicht nach gelöschten Dateien durchsuchen. Hat Ihr Gerät eine Speicherkarte, stecken Sie sie in ein Kartenlesegerät und scannen Sie sie hier. Prüfen Sie außerdem den Papierkorb oder den Ordner „Zuletzt gelöscht“ auf dem Gerät.", "Unlocked volume": "Entsperrtes Volume", "Unverified candidate": "Ungeprüfte mögliche Datei", "Video": "Video", @@ -69,7 +72,7 @@ "Window closed": "Fenster geschlossen", "Windows denied access. For physical drives, restart Reclaim with Run as administrator, or open a disk image. BitLocker volumes must be unlocked first.": "Windows hat den Zugriff verweigert. Starten Sie Reclaim für physische Laufwerke als Administrator oder öffnen Sie ein Datenträgerabbild. BitLocker-Volumes müssen zuerst entsperrt werden.", "macOS could not inspect drives. Open a raw disk image instead.": "macOS konnte Laufwerke nicht prüfen. Öffnen Sie ein unverarbeitetes Datenträgerabbild.", - "macOS denied access. Reading a whole drive requires administrator rights, and macOS may ask you to allow access to removable volumes or Full Disk Access in System Settings > Privacy & Security. You can open a disk image instead.": "macOS hat den Zugriff verweigert. Das Lesen eines ganzen Laufwerks erfordert Administratorrechte, und macOS fragt eventuell nach Zugriff auf Wechselmedien oder Festplattenvollzugriff unter Systemeinstellungen > Datenschutz & Sicherheit. Du kannst stattdessen ein Datenträgerabbild öffnen.", + "macOS denied access. Reading a whole drive requires administrator rights, and macOS may ask you to allow access to removable volumes or Full Disk Access in System Settings > Privacy & Security. You can open a disk image instead.": "macOS hat den Zugriff verweigert. Das Lesen eines ganzen Laufwerks erfordert Administratorrechte, und macOS fragt eventuell nach Zugriff auf Wechselmedien oder Festplattenvollzugriff unter Systemeinstellungen > Datenschutz & Sicherheit. Sie können stattdessen ein Datenträgerabbild öffnen.", "{} file": "{} Datei", "{} files": "{} Dateien", "{} selected": "{} ausgewählt", @@ -77,6 +80,7 @@ "Choose a drive": "Laufwerk auswählen", "Scan stopped": "Scan angehalten", "Stop": "Anhalten", + "Stop recovery": "Wiederherstellung stoppen", "File type": "Dateityp", "Size": "Größe", "bytes": "Bytes", @@ -112,55 +116,58 @@ "{0} of {1}": "{0} von {1}", "Reading unused-space map…": "Karte des ungenutzten Speicherplatzes wird gelesen…", "No unused space is available on this volume.": "Auf diesem Volume ist kein ungenutzter Speicherplatz verfügbar.", - "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, or exFAT). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "Das Scannen ungenutzten Speicherplatzes erfordert ein unterstütztes formatiertes Volume (NTFS, FAT12/16/32 oder exFAT). Wählen Sie das Volume bzw. die Partition statt eines Abbilds des gesamten Datenträgers. Belegter Speicherplatz wurde nicht gescannt.", + "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, exFAT, ext2/3/4, or HFS+). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "Das Scannen ungenutzten Speicherplatzes erfordert ein unterstütztes formatiertes Volume (NTFS, FAT12/16/32, exFAT, ext2/3/4 oder HFS+). Wählen Sie das Volume bzw. die Partition statt eines Abbilds des gesamten Datenträgers. Belegter Speicherplatz wurde nicht gescannt.", + "This is a ReFS volume. Reclaim cannot read the ReFS free-space map yet, so this volume was not scanned.": "Dies ist ein ReFS-Volume. Reclaim kann die Freiraumtabelle von ReFS noch nicht lesen, daher wurde dieses Volume nicht gescannt.", + "This ext volume uses a feature Reclaim cannot map yet (compression, an external journal, META_BG, or bigalloc), so it was not scanned.": "Dieses ext-Volume nutzt eine Funktion, die Reclaim noch nicht auswerten kann (Komprimierung, externes Journal, META_BG oder Bigalloc), daher wurde es nicht gescannt.", + "This HFS+ volume's free-space map is too fragmented for Reclaim to read yet, so it was not scanned.": "Die Freiraumtabelle dieses HFS+-Volumes ist zu stark fragmentiert, als dass Reclaim sie schon lesen könnte, daher wurde es nicht gescannt.", "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive.": "Das Speichern wiederhergestellter Dateien auf demselben gescannten physischen Laufwerk kann die zu rettenden Daten überschreiben und dauerhaft beschädigen. Wählen Sie einen Ordner auf einem anderen physischen Laufwerk. Fahren Sie nur fort, wenn das Ziel auf einem anderen Laufwerk liegt.", "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.": "Das Speichern hier kann die zu rettenden gelöschten Dateien überschreiben und dauerhaft beschädigen. Wählen Sie einen Ordner auf einem anderen physischen Laufwerk, etwa einem USB-Laufwerk.", "Could not connect to the update server. Check your internet connection and try again.": "Keine Verbindung zum Update-Server. Prüfen Sie Ihre Internetverbindung und versuchen Sie es erneut.", "Could not check for updates. Please try again later.": "Updates konnten nicht geprüft werden. Versuchen Sie es später erneut.", "The scan ended unexpectedly. You can recover the files already found or scan again.": "Der Scan wurde unerwartet beendet. Sie können die bereits gefundenen Dateien wiederherstellen oder erneut scannen.", "Recovery ended unexpectedly. Check the destination folder before trying again.": "Die Wiederherstellung wurde unerwartet beendet. Prüfen Sie den Zielordner, bevor Sie es erneut versuchen.", - "Scan finished with unreadable areas. Some files could not be checked. The drive may be failing: recover the files you need now and avoid repeated scans.": "Der Scan wurde mit unlesbaren Bereichen beendet. Einige Dateien konnten nicht geprüft werden. Das Laufwerk ist möglicherweise defekt: Stelle die wichtigen Dateien jetzt wieder her und vermeide wiederholte Scans.", + "Scan finished with unreadable areas. Some files could not be checked. The drive may be failing: recover the files you need now and avoid repeated scans.": "Der Scan wurde mit unlesbaren Bereichen beendet. Einige Dateien konnten nicht geprüft werden. Das Laufwerk ist möglicherweise defekt: Stellen Sie die wichtigen Dateien jetzt wieder her und vermeiden Sie wiederholte Scans.", "No source selected": "Keine Quelle ausgewählt", - "Select a file to inspect its details.": "Wähle eine Datei aus, um ihre Details zu sehen.", - "A second chance for your files": "Eine zweite Chance für deine Dateien", + "Select a file to inspect its details.": "Wählen Sie eine Datei aus, um ihre Details zu sehen.", + "A second chance for your files": "Eine zweite Chance für Ihre Dateien", "Sources": "Quellen", "Recovered files": "Dateien zur Wiederherstellung", "Settings & help": "Einstellungen und Hilfe", "Read-only by design": "Nur Lesezugriff", - "Your source stays untouched. Save recovered files to another drive.": "Die Quelle bleibt unverändert. Speichere wiederhergestellte Dateien auf einem anderen Laufwerk.", - "Choose a drive or disk image to search for deleted files.": "Wähle ein Laufwerk oder Datenträgerabbild für die Suche nach gelöschten Dateien.", - "Inspect candidates, choose your files, and recover them safely.": "Prüfe gefundene Dateien, wähle sie aus und stelle sie sicher wieder her.", + "Your source stays untouched. Save recovered files to another drive.": "Die Quelle bleibt unverändert. Speichern Sie wiederhergestellte Dateien auf einem anderen Laufwerk.", + "Choose a drive or disk image to search for deleted files.": "Wählen Sie ein Laufwerk oder Datenträgerabbild für die Suche nach gelöschten Dateien.", + "Inspect candidates, choose your files, and recover them safely.": "Prüfen Sie gefundene Dateien, wählen Sie sie aus und stellen Sie sie sicher wieder her.", "Appearance, updates, and a little guidance.": "Darstellung, Updates und eine kurze Anleitung.", "AVAILABLE SOURCES": "VERFÜGBARE QUELLEN", "Open disk image": "Datenträgerabbild öffnen", - "No drives found. Open a disk image or connect a drive and refresh.": "Keine Laufwerke gefunden. Öffne ein Abbild oder verbinde ein Laufwerk und aktualisiere.", + "No drives found. Open a disk image or connect a drive and refresh.": "Keine Laufwerke gefunden. Öffnen Sie ein Abbild oder schließen Sie ein Laufwerk an und aktualisieren Sie.", "SELECTED SOURCE": "AUSGEWÄHLTE QUELLE", "Start scanning": "Suche starten", "Search name or offset…": "Name oder Offset suchen…", "Select all": "Alle auswählen", "Searching… candidates will appear here.": "Suche läuft… gefundene Dateien erscheinen hier.", - "No matching files yet. Start a scan from Sources.": "Noch keine passenden Dateien. Starte eine Suche unter Quellen.", + "No matching files yet. Start a scan from Sources.": "Noch keine passenden Dateien. Starten Sie eine Suche unter Quellen.", "FILE INSPECTOR": "DATEIDETAILS", - "Candidates may be incomplete. Verify recovered files before relying on them.": "Gefundene Dateien können unvollständig sein. Prüfe sie nach der Wiederherstellung.", + "Candidates may be incomplete. Verify recovered files before relying on them.": "Gefundene Dateien können unvollständig sein. Prüfen Sie sie nach der Wiederherstellung.", "Recover selected": "Auswahl wiederherstellen", "System language": "Systemsprache", "Recover in three steps": "In drei Schritten wiederherstellen", - "1. Stop using the drive you lost files from. Don't save, download or install anything to it.\n\n2. Choose that drive or a raw disk image, then scan its unused space. Stop any time and keep partial results.\n\n3. Inspect and select files, then save them to a different physical drive.": "1. Benutze das Laufwerk, auf dem die Dateien verloren gingen, nicht mehr. Speichere, lade und installiere nichts darauf.\n\n2. Wähle dieses Laufwerk oder ein Rohabbild und durchsuche den ungenutzten Speicher. Du kannst jederzeit stoppen und Teilergebnisse behalten.\n\n3. Prüfe und wähle Dateien aus und speichere sie auf einem anderen physischen Laufwerk.", + "1. Stop using the drive you lost files from. Don't save, download or install anything to it.\n\n2. Choose that drive or a raw disk image, then scan its unused space. Stop any time and keep partial results.\n\n3. Inspect and select files, then save them to a different physical drive.": "1. Verwenden Sie das Laufwerk, auf dem die Dateien verloren gingen, nicht mehr. Speichern, laden und installieren Sie nichts darauf.\n\n2. Wählen Sie dieses Laufwerk oder ein Rohabbild und durchsuchen Sie den ungenutzten Speicher. Sie können jederzeit stoppen und Teilergebnisse behalten.\n\n3. Prüfen Sie die Dateien, wählen Sie sie aus und speichern Sie sie auf einem anderen physischen Laufwerk.", "Supported formats": "Unterstützte Formate", - "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, and NTFS volumes.": "Fotos, Dokumente, Archive, Audio, Video, Anwendungen und kleine residente NTFS-Dateien. Suche im ungenutzten Speicher von FAT-, exFAT- und NTFS-Volumes.", + "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, NTFS, ext2/3/4, and HFS+ volumes.": "Fotos, Dokumente, Archive, Audio, Video, Anwendungen und kleine residente NTFS-Dateien. Suche im ungenutzten Speicher von FAT-, exFAT-, NTFS-, ext2/3/4- und HFS+-Volumes.", "Built with Rust and Slint · MIT licensed application": "Mit Rust und Slint entwickelt · Anwendung unter MIT-Lizenz", "Locked": "Gesperrt", "Stopping after the current step…": "Wird nach dem aktuellen Schritt gestoppt…", "The volume size is unknown; unused-space scanning cannot start.": "Die Volumegröße ist unbekannt; die Suche im ungenutzten Speicher kann nicht starten.", - "Update worker stopped. Try again.": "Der Updateprozess wurde beendet. Versuche es erneut.", - "Update verified. Open the installer to finish.": "Update geprüft. Öffne das Installationsprogramm zum Abschluss.", - "Update finished. Close and reopen Reclaim if needed.": "Update abgeschlossen. Schließe und öffne Reclaim bei Bedarf erneut.", + "Update worker stopped. Try again.": "Der Updateprozess wurde beendet. Versuchen Sie es erneut.", + "Update verified. Open the installer to finish.": "Update geprüft. Öffnen Sie das Installationsprogramm zum Abschluss.", + "Update finished. Close and reopen Reclaim if needed.": "Update abgeschlossen. Schließen und öffnen Sie Reclaim bei Bedarf erneut.", "Version {0} is available.": "Version {0} ist verfügbar.", "Review recovery destination": "Wiederherstellungsziel prüfen", "reclaim": "reclaim", " · ": " · ", "Invalid preview position": "Ungültige Vorschauposition", - "This image is too large to preview. Recover it to view the full image.": "Das Bild ist zu groß für die Vorschau. Stelle es wieder her, um es vollständig anzuzeigen.", + "This image is too large to preview. Recover it to view the full image.": "Das Bild ist zu groß für die Vorschau. Stellen Sie es wieder her, um es vollständig anzuzeigen.", "The source is no longer available.": "Die Quelle ist nicht mehr verfügbar.", "This image could not be previewed. Its contents may be incomplete or damaged.": "Keine Vorschau möglich. Der Bildinhalt ist möglicherweise unvollständig oder beschädigt.", "Could not open Reclaim": "Reclaim konnte nicht geöffnet werden", @@ -209,7 +216,7 @@ "What’s new": "Was ist neu", "Later": "Später", "Hide details": "Details ausblenden", - "You can keep working and update later.": "Du kannst weiterarbeiten und später aktualisieren.", + "You can keep working and update later.": "Sie können weiterarbeiten und später aktualisieren.", "Updates": "Updates", "Updates are managed by Flatpak.": "Updates werden von Flatpak verwaltet.", "Restart as administrator": "Als Administrator neu starten", @@ -217,5 +224,8 @@ "Could not restart as administrator: {0}": "Neustart als Administrator fehlgeschlagen: {0}", "The source stopped returning data before its reported end. It may be truncated, disconnected, or restricted by the operating system. Files already found can still be recovered.": "Die Quelle lieferte vor ihrem gemeldeten Ende keine Daten mehr. Sie ist möglicherweise abgeschnitten, getrennt oder durch das Betriebssystem eingeschränkt. Bereits gefundene Dateien können weiterhin wiederhergestellt werden.", "Could not prepare read-only access to this volume: {0}": "Der schreibgeschützte Zugriff auf dieses Volume konnte nicht vorbereitet werden: {0}", - "Updates are managed by the Snap Store.": "Updates werden vom Snap Store verwaltet." + "Updates are managed by the Snap Store.": "Updates werden vom Snap Store verwaltet.", + "Choose another folder": "Anderen Ordner wählen", + "Save here anyway": "Trotzdem hier speichern", + "Cancel": "Abbrechen" } diff --git a/locales/en.json b/locales/en.json index 011c36e..4cc079c 100644 --- a/locales/en.json +++ b/locales/en.json @@ -62,6 +62,9 @@ "System": "System", "Too many files with the same recovered name": "Too many files with the same recovered name", "USB drive": "USB drive", + "Phone or camera": "Phone or camera", + "File transfer only": "File transfer only", + "Phones and cameras connect for file transfer only, so Reclaim can’t search them for deleted files. If yours has a memory card, put the card in a card reader and scan it here. Also check its Trash or Recently deleted folder.": "Phones and cameras connect for file transfer only, so Reclaim can’t search them for deleted files. If yours has a memory card, put the card in a card reader and scan it here. Also check its Trash or Recently deleted folder.", "Unlocked volume": "Unlocked volume", "Unverified candidate": "Unverified candidate", "Video": "Video", @@ -77,6 +80,7 @@ "Choose a drive": "Choose a drive", "Scan stopped": "Scan stopped", "Stop": "Stop", + "Stop recovery": "Stop recovery", "File type": "File type", "Size": "Size", "bytes": "bytes", @@ -112,7 +116,10 @@ "{0} of {1}": "{0} of {1}", "Reading unused-space map…": "Reading unused-space map…", "No unused space is available on this volume.": "No unused space is available on this volume.", - "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, or exFAT). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, or exFAT). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.", + "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, exFAT, ext2/3/4, or HFS+). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, exFAT, ext2/3/4, or HFS+). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.", + "This is a ReFS volume. Reclaim cannot read the ReFS free-space map yet, so this volume was not scanned.": "This is a ReFS volume. Reclaim cannot read the ReFS free-space map yet, so this volume was not scanned.", + "This ext volume uses a feature Reclaim cannot map yet (compression, an external journal, META_BG, or bigalloc), so it was not scanned.": "This ext volume uses a feature Reclaim cannot map yet (compression, an external journal, META_BG, or bigalloc), so it was not scanned.", + "This HFS+ volume's free-space map is too fragmented for Reclaim to read yet, so it was not scanned.": "This HFS+ volume's free-space map is too fragmented for Reclaim to read yet, so it was not scanned.", "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive.": "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive.", "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.": "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.", "Could not connect to the update server. Check your internet connection and try again.": "Could not connect to the update server. Check your internet connection and try again.", @@ -147,7 +154,7 @@ "Recover in three steps": "Recover in three steps", "1. Stop using the drive you lost files from. Don't save, download or install anything to it.\n\n2. Choose that drive or a raw disk image, then scan its unused space. Stop any time and keep partial results.\n\n3. Inspect and select files, then save them to a different physical drive.": "1. Stop using the drive you lost files from. Don't save, download or install anything to it.\n\n2. Choose that drive or a raw disk image, then scan its unused space. Stop any time and keep partial results.\n\n3. Inspect and select files, then save them to a different physical drive.", "Supported formats": "Supported formats", - "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, and NTFS volumes.": "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, and NTFS volumes.", + "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, NTFS, ext2/3/4, and HFS+ volumes.": "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, NTFS, ext2/3/4, and HFS+ volumes.", "Built with Rust and Slint · MIT licensed application": "Built with Rust and Slint · MIT licensed application", "Locked": "Locked", "Stopping after the current step…": "Stopping after the current step…", @@ -217,5 +224,8 @@ "Could not restart as administrator: {0}": "Could not restart as administrator: {0}", "The source stopped returning data before its reported end. It may be truncated, disconnected, or restricted by the operating system. Files already found can still be recovered.": "The source stopped returning data before its reported end. It may be truncated, disconnected, or restricted by the operating system. Files already found can still be recovered.", "Could not prepare read-only access to this volume: {0}": "Could not prepare read-only access to this volume: {0}", - "Updates are managed by the Snap Store.": "Updates are managed by the Snap Store." + "Updates are managed by the Snap Store.": "Updates are managed by the Snap Store.", + "Choose another folder": "Choose another folder", + "Save here anyway": "Save here anyway", + "Cancel": "Cancel" } diff --git a/locales/es.json b/locales/es.json index f82d4ed..21f13a8 100644 --- a/locales/es.json +++ b/locales/es.json @@ -62,6 +62,9 @@ "System": "Sistema", "Too many files with the same recovered name": "Demasiados archivos con el mismo nombre recuperado", "USB drive": "Unidad USB", + "Phone or camera": "Teléfono o cámara", + "File transfer only": "Solo transferencia de archivos", + "Phones and cameras connect for file transfer only, so Reclaim can’t search them for deleted files. If yours has a memory card, put the card in a card reader and scan it here. Also check its Trash or Recently deleted folder.": "Los teléfonos y las cámaras solo se conectan para transferir archivos, así que Reclaim no puede buscar archivos eliminados en ellos. Si el tuyo tiene tarjeta de memoria, ponla en un lector de tarjetas y analízala aquí. Revisa también su papelera o la carpeta «Eliminados recientemente».", "Unlocked volume": "Volumen desbloqueado", "Unverified candidate": "Candidato sin verificar", "Video": "Vídeo", @@ -77,6 +80,7 @@ "Choose a drive": "Elegir una unidad", "Scan stopped": "Análisis detenido", "Stop": "Detener", + "Stop recovery": "Detener recuperación", "File type": "Tipo de archivo", "Size": "Tamaño", "bytes": "bytes", @@ -112,7 +116,10 @@ "{0} of {1}": "{0} de {1}", "Reading unused-space map…": "Leyendo mapa de espacio libre…", "No unused space is available on this volume.": "No hay espacio libre disponible en este volumen.", - "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, or exFAT). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "El análisis de espacio libre necesita un volumen formateado compatible (NTFS, FAT12/16/32 o exFAT). Selecciona el volumen o partición en lugar de una imagen de disco completo. No se analizó el espacio ocupado.", + "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, exFAT, ext2/3/4, or HFS+). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "El análisis de espacio libre necesita un volumen formateado compatible (NTFS, FAT12/16/32, exFAT, ext2/3/4 o HFS+). Selecciona el volumen o partición en lugar de una imagen de disco completo. No se analizó el espacio ocupado.", + "This is a ReFS volume. Reclaim cannot read the ReFS free-space map yet, so this volume was not scanned.": "Este es un volumen ReFS. Reclaim aún no puede leer el mapa de espacio libre de ReFS, así que no se analizó este volumen.", + "This ext volume uses a feature Reclaim cannot map yet (compression, an external journal, META_BG, or bigalloc), so it was not scanned.": "Este volumen ext usa una función que Reclaim aún no puede interpretar (compresión, diario externo, META_BG o bigalloc), así que no se analizó.", + "This HFS+ volume's free-space map is too fragmented for Reclaim to read yet, so it was not scanned.": "El mapa de espacio libre de este volumen HFS+ está demasiado fragmentado para que Reclaim pueda leerlo todavía, así que no se analizó.", "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive.": "Guardar los archivos recuperados en la misma unidad física analizada puede sobrescribir y dañar permanentemente los datos que intentas recuperar. Elige una carpeta en otra unidad física. Continúa solo si el destino está en otra unidad.", "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.": "Guardar aquí puede sobrescribir los archivos eliminados que intentas recuperar y dañarlos permanentemente. Elige una carpeta en otra unidad física, como una unidad USB.", "Could not connect to the update server. Check your internet connection and try again.": "No se pudo conectar con el servidor de actualizaciones. Comprueba tu conexión a internet e inténtalo de nuevo.", @@ -147,7 +154,7 @@ "Recover in three steps": "Recupera en tres pasos", "1. Stop using the drive you lost files from. Don't save, download or install anything to it.\n\n2. Choose that drive or a raw disk image, then scan its unused space. Stop any time and keep partial results.\n\n3. Inspect and select files, then save them to a different physical drive.": "1. Deja de usar la unidad en la que perdiste los archivos. No guardes, descargues ni instales nada en ella.\n\n2. Elige esa unidad o una imagen de disco sin procesar y analiza su espacio sin usar. Puedes detenerte y conservar resultados parciales.\n\n3. Revisa y selecciona los archivos y guárdalos en otra unidad física.", "Supported formats": "Formatos compatibles", - "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, and NTFS volumes.": "Fotos, documentos, archivos comprimidos, audio, vídeo, aplicaciones y pequeños archivos residentes NTFS. Análisis del espacio sin usar en volúmenes FAT, exFAT y NTFS.", + "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, NTFS, ext2/3/4, and HFS+ volumes.": "Fotos, documentos, archivos comprimidos, audio, vídeo, aplicaciones y pequeños archivos residentes NTFS. Análisis del espacio sin usar en volúmenes FAT, exFAT, NTFS, ext2/3/4 y HFS+.", "Built with Rust and Slint · MIT licensed application": "Creado con Rust y Slint · Aplicación con licencia MIT", "Locked": "Bloqueado", "Stopping after the current step…": "Deteniendo después del paso actual…", @@ -217,5 +224,8 @@ "Could not restart as administrator: {0}": "No se pudo reiniciar como administrador: {0}", "The source stopped returning data before its reported end. It may be truncated, disconnected, or restricted by the operating system. Files already found can still be recovered.": "El origen dejó de devolver datos antes de su final declarado. Puede estar truncado, desconectado o restringido por el sistema operativo. Los archivos ya encontrados aún se pueden recuperar.", "Could not prepare read-only access to this volume: {0}": "No se pudo preparar el acceso de solo lectura a este volumen: {0}", - "Updates are managed by the Snap Store.": "Snap Store gestiona las actualizaciones." + "Updates are managed by the Snap Store.": "Snap Store gestiona las actualizaciones.", + "Choose another folder": "Elegir otra carpeta", + "Save here anyway": "Guardar aquí de todos modos", + "Cancel": "Cancelar" } diff --git a/locales/fr.json b/locales/fr.json index a7ef413..bd8984f 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -62,6 +62,9 @@ "System": "Système", "Too many files with the same recovered name": "Trop de fichiers avec le même nom récupéré", "USB drive": "Disque USB", + "Phone or camera": "Téléphone ou appareil photo", + "File transfer only": "Transfert de fichiers uniquement", + "Phones and cameras connect for file transfer only, so Reclaim can’t search them for deleted files. If yours has a memory card, put the card in a card reader and scan it here. Also check its Trash or Recently deleted folder.": "Les téléphones et les appareils photo se connectent uniquement pour le transfert de fichiers, Reclaim ne peut donc pas y rechercher de fichiers supprimés. Si le vôtre possède une carte mémoire, placez-la dans un lecteur de cartes et analysez-la ici. Vérifiez aussi sa corbeille ou le dossier « Supprimés récemment ».", "Unlocked volume": "Volume déverrouillé", "Unverified candidate": "Fichier potentiel non vérifié", "Video": "Vidéo", @@ -77,6 +80,7 @@ "Choose a drive": "Choisir un disque", "Scan stopped": "Analyse arrêtée", "Stop": "Arrêter", + "Stop recovery": "Arrêter la récupération", "File type": "Type de fichier", "Size": "Taille", "bytes": "octets", @@ -112,7 +116,10 @@ "{0} of {1}": "{0} sur {1}", "Reading unused-space map…": "Lecture de la carte de l’espace libre…", "No unused space is available on this volume.": "Aucun espace libre disponible sur ce volume.", - "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, or exFAT). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "L’analyse de l’espace libre nécessite un volume formaté pris en charge (NTFS, FAT12/16/32 ou exFAT). Sélectionnez le volume ou la partition plutôt qu’une image de disque entier. L’espace occupé n’a pas été analysé.", + "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, exFAT, ext2/3/4, or HFS+). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "L’analyse de l’espace libre nécessite un volume formaté pris en charge (NTFS, FAT12/16/32, exFAT, ext2/3/4 ou HFS+). Sélectionnez le volume ou la partition plutôt qu’une image de disque entier. L’espace occupé n’a pas été analysé.", + "This is a ReFS volume. Reclaim cannot read the ReFS free-space map yet, so this volume was not scanned.": "Ce volume est au format ReFS. Reclaim ne sait pas encore lire la carte de l’espace libre de ReFS, ce volume n’a donc pas été analysé.", + "This ext volume uses a feature Reclaim cannot map yet (compression, an external journal, META_BG, or bigalloc), so it was not scanned.": "Ce volume ext utilise une fonction que Reclaim ne sait pas encore interpréter (compression, journal externe, META_BG ou bigalloc), il n’a donc pas été analysé.", + "This HFS+ volume's free-space map is too fragmented for Reclaim to read yet, so it was not scanned.": "La carte de l’espace libre de ce volume HFS+ est trop fragmentée pour que Reclaim puisse la lire pour l’instant, il n’a donc pas été analysé.", "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive.": "Enregistrer les fichiers récupérés sur le disque physique analysé peut écraser et endommager définitivement les données à récupérer. Choisissez un dossier sur un autre disque physique. Continuez uniquement si la destination est sur un autre disque.", "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.": "Enregistrer ici peut écraser les fichiers supprimés à récupérer et les endommager définitivement. Choisissez un dossier sur un autre disque physique, comme un disque USB.", "Could not connect to the update server. Check your internet connection and try again.": "Impossible de se connecter au serveur de mises à jour. Vérifiez votre connexion Internet et réessayez.", @@ -147,7 +154,7 @@ "Recover in three steps": "Récupérer en trois étapes", "1. Stop using the drive you lost files from. Don't save, download or install anything to it.\n\n2. Choose that drive or a raw disk image, then scan its unused space. Stop any time and keep partial results.\n\n3. Inspect and select files, then save them to a different physical drive.": "1. Cessez d’utiliser le disque sur lequel les fichiers ont été perdus. N’y enregistrez, téléchargez ni installez rien.\n\n2. Choisissez ce disque ou une image disque brute, puis analysez son espace inutilisé. Vous pouvez arrêter et conserver les résultats partiels.\n\n3. Examinez et sélectionnez les fichiers, puis enregistrez-les sur un autre disque physique.", "Supported formats": "Formats pris en charge", - "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, and NTFS volumes.": "Photos, documents, archives, audio, vidéo, applications et petits fichiers résidents NTFS. Analyse de l’espace inutilisé des volumes FAT, exFAT et NTFS.", + "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, NTFS, ext2/3/4, and HFS+ volumes.": "Photos, documents, archives, audio, vidéo, applications et petits fichiers résidents NTFS. Analyse de l’espace inutilisé des volumes FAT, exFAT, NTFS, ext2/3/4 et HFS+.", "Built with Rust and Slint · MIT licensed application": "Créé avec Rust et Slint · Application sous licence MIT", "Locked": "Verrouillé", "Stopping after the current step…": "Arrêt après l’étape en cours…", @@ -217,5 +224,8 @@ "Could not restart as administrator: {0}": "Impossible de redémarrer en tant qu’administrateur : {0}", "The source stopped returning data before its reported end. It may be truncated, disconnected, or restricted by the operating system. Files already found can still be recovered.": "La source a cessé de fournir des données avant sa fin annoncée. Elle peut être tronquée, déconnectée ou limitée par le système d’exploitation. Les fichiers déjà trouvés peuvent encore être récupérés.", "Could not prepare read-only access to this volume: {0}": "Impossible de préparer l’accès en lecture seule à ce volume : {0}", - "Updates are managed by the Snap Store.": "Les mises à jour sont gérées par le Snap Store." + "Updates are managed by the Snap Store.": "Les mises à jour sont gérées par le Snap Store.", + "Choose another folder": "Choisir un autre dossier", + "Save here anyway": "Enregistrer ici quand même", + "Cancel": "Annuler" } diff --git a/locales/he.json b/locales/he.json index 2b6720f..e2ba20d 100644 --- a/locales/he.json +++ b/locales/he.json @@ -62,6 +62,9 @@ "System": "מערכת", "Too many files with the same recovered name": "קיימים יותר מדי קבצים בעלי אותו שם משוחזר", "USB drive": "כונן USB", + "Phone or camera": "טלפון או מצלמה", + "File transfer only": "העברת קבצים בלבד", + "Phones and cameras connect for file transfer only, so Reclaim can’t search them for deleted files. If yours has a memory card, put the card in a card reader and scan it here. Also check its Trash or Recently deleted folder.": "טלפונים ומצלמות מתחברים להעברת קבצים בלבד, ולכן Reclaim לא יכול לחפש בהם קבצים שנמחקו. אם יש במכשיר כרטיס זיכרון, הכניסו אותו לקורא כרטיסים וסרקו אותו כאן. כדאי גם לבדוק את תיקיית האשפה או ״נמחקו לאחרונה״ במכשיר.", "Unlocked volume": "כונן שנעילתו בוטלה", "Unverified candidate": "מועמד שלא אומת", "Video": "וידאו", @@ -77,6 +80,7 @@ "Choose a drive": "בחירת כונן", "Scan stopped": "הסריקה הופסקה", "Stop": "עצירה", + "Stop recovery": "עצירת השחזור", "File type": "סוג קובץ", "Size": "גודל", "bytes": "בתים", @@ -112,7 +116,10 @@ "{0} of {1}": "{0} מתוך {1}", "Reading unused-space map…": "קורא את מפת השטח הפנוי…", "No unused space is available on this volume.": "אין שטח פנוי לסריקה באמצעי אחסון זה.", - "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, or exFAT). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "סריקת שטח שאינו בשימוש דורשת אמצעי אחסון מפורמט ונתמך (NTFS, FAT12/16/32 או exFAT). יש לבחור אמצעי אחסון או מחיצה במקום דמות של כונן שלם. שטח שנמצא בשימוש לא נסרק.", + "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, exFAT, ext2/3/4, or HFS+). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "סריקת שטח שאינו בשימוש דורשת אמצעי אחסון מפורמט ונתמך (NTFS, FAT12/16/32, exFAT, ext2/3/4 או HFS+‎). יש לבחור אמצעי אחסון או מחיצה במקום דמות של כונן שלם. שטח שנמצא בשימוש לא נסרק.", + "This is a ReFS volume. Reclaim cannot read the ReFS free-space map yet, so this volume was not scanned.": "זהו אמצעי אחסון ReFS. עדיין אין ל־Reclaim אפשרות לקרוא את מפת השטח הפנוי של ReFS, ולכן אמצעי האחסון הזה לא נסרק.", + "This ext volume uses a feature Reclaim cannot map yet (compression, an external journal, META_BG, or bigalloc), so it was not scanned.": "אמצעי האחסון ext הזה משתמש בתכונה ש־Reclaim עדיין לא יודע לפענח (דחיסה, יומן חיצוני, META_BG או bigalloc), ולכן הוא לא נסרק.", + "This HFS+ volume's free-space map is too fragmented for Reclaim to read yet, so it was not scanned.": "מפת השטח הפנוי של אמצעי האחסון HFS+‎ הזה מקוטעת מדי מכדי ש־Reclaim יוכל לקרוא אותה כרגע, ולכן הוא לא נסרק.", "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive.": "שמירת קבצים משוחזרים באותו כונן פיזי שנסרק עלולה לדרוס נתונים שמנסים לשחזר ולפגוע בהם לצמיתות. יש לבחור תיקייה בכונן פיזי אחר. יש להמשיך רק אם היעד נמצא בכונן אחר.", "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.": "שמירה כאן עלולה לדרוס את הקבצים המחוקים שמנסים לשחזר ולפגוע בהם לצמיתות. יש לבחור תיקייה בכונן פיזי אחר, כמו כונן USB.", "Could not connect to the update server. Check your internet connection and try again.": "לא ניתן להתחבר לשרת העדכונים. יש לבדוק את החיבור לאינטרנט ולנסות שוב.", @@ -147,7 +154,7 @@ "Recover in three steps": "שחזור בשלושה שלבים", "1. Stop using the drive you lost files from. Don't save, download or install anything to it.\n\n2. Choose that drive or a raw disk image, then scan its unused space. Stop any time and keep partial results.\n\n3. Inspect and select files, then save them to a different physical drive.": "1. הפסיקו להשתמש בכונן שממנו נמחקו הקבצים. אל תשמרו, תורידו או תתקינו עליו דבר.\n\n2. בחרו את הכונן הזה או קובץ תמונת דיסק גולמית, וסרקו את השטח הלא מנוצל. אפשר לעצור בכל עת ולשמור תוצאות חלקיות.\n\n3. בדקו ובחרו קבצים, ושמרו אותם בכונן פיזי אחר.", "Supported formats": "פורמטים נתמכים", - "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, and NTFS volumes.": "תמונות, מסמכים, ארכיונים, שמע, וידאו, יישומים וקובצי NTFS קטנים המאוחסנים ברשומה. סריקת שטח לא מנוצל במחיצות FAT,‏ exFAT ו־NTFS.", + "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, NTFS, ext2/3/4, and HFS+ volumes.": "תמונות, מסמכים, ארכיונים, שמע, וידאו, יישומים וקובצי NTFS קטנים המאוחסנים ברשומה. סריקת שטח לא מנוצל במחיצות FAT,‏ exFAT,‏ NTFS,‏ ext2/3/4 ו־HFS+‎.", "Built with Rust and Slint · MIT licensed application": "נבנה עם Rust ו־Slint · יישום ברישיון MIT", "Locked": "נעול", "Stopping after the current step…": "הפעולה תיעצר לאחר השלב הנוכחי…", @@ -217,5 +224,8 @@ "Could not restart as administrator: {0}": "לא ניתן להפעיל מחדש כמנהל: {0}", "The source stopped returning data before its reported end. It may be truncated, disconnected, or restricted by the operating system. Files already found can still be recovered.": "המקור הפסיק להחזיר נתונים לפני סופו המדווח. ייתכן שהוא נקטע, נותק או שהגישה אליו מוגבלת על ידי מערכת ההפעלה. עדיין ניתן לשחזר את הקבצים שכבר נמצאו.", "Could not prepare read-only access to this volume: {0}": "לא ניתן להכין גישה לקריאה בלבד לכונן הזה: {0}", - "Updates are managed by the Snap Store.": "העדכונים מנוהלים באמצעות חנות Snap." + "Updates are managed by the Snap Store.": "העדכונים מנוהלים באמצעות חנות Snap.", + "Choose another folder": "בחירת תיקייה אחרת", + "Save here anyway": "שמירה כאן בכל זאת", + "Cancel": "ביטול" } diff --git a/locales/ru.json b/locales/ru.json index 6a48cb6..763137c 100644 --- a/locales/ru.json +++ b/locales/ru.json @@ -62,6 +62,9 @@ "System": "Системный", "Too many files with the same recovered name": "Слишком много файлов с одним восстановленным именем", "USB drive": "USB-диск", + "Phone or camera": "Телефон или камера", + "File transfer only": "Только передача файлов", + "Phones and cameras connect for file transfer only, so Reclaim can’t search them for deleted files. If yours has a memory card, put the card in a card reader and scan it here. Also check its Trash or Recently deleted folder.": "Телефоны и камеры подключаются только для передачи файлов, поэтому Reclaim не может искать на них удалённые файлы. Если в устройстве есть карта памяти, вставьте её в кардридер и просканируйте здесь. Проверьте также корзину или папку «Недавно удалённые» на устройстве.", "Unlocked volume": "Разблокированный том", "Unverified candidate": "Непроверенный файл", "Video": "Видео", @@ -77,6 +80,7 @@ "Choose a drive": "Выбрать диск", "Scan stopped": "Сканирование остановлено", "Stop": "Остановить", + "Stop recovery": "Остановить восстановление", "File type": "Тип файла", "Size": "Размер", "bytes": "байт", @@ -112,7 +116,10 @@ "{0} of {1}": "{0} из {1}", "Reading unused-space map…": "Чтение карты свободного места…", "No unused space is available on this volume.": "На этом томе нет свободного места.", - "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, or exFAT). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "Для сканирования свободного места нужен том с поддерживаемой файловой системой (NTFS, FAT12/16/32 или exFAT). Выберите том или раздел вместо образа всего диска. Занятое место не сканировалось.", + "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, exFAT, ext2/3/4, or HFS+). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.": "Для сканирования свободного места нужен том с поддерживаемой файловой системой (NTFS, FAT12/16/32, exFAT, ext2/3/4 или HFS+). Выберите том или раздел вместо образа всего диска. Занятое место не сканировалось.", + "This is a ReFS volume. Reclaim cannot read the ReFS free-space map yet, so this volume was not scanned.": "Это том ReFS. Reclaim пока не умеет читать карту свободного места ReFS, поэтому этот том не сканировался.", + "This ext volume uses a feature Reclaim cannot map yet (compression, an external journal, META_BG, or bigalloc), so it was not scanned.": "Этот том ext использует функцию, которую Reclaim пока не поддерживает (сжатие, внешний журнал, META_BG или bigalloc), поэтому он не сканировался.", + "This HFS+ volume's free-space map is too fragmented for Reclaim to read yet, so it was not scanned.": "Карта свободного места этого тома HFS+ слишком фрагментирована, и Reclaim пока не может её прочитать, поэтому он не сканировался.", "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive.": "Сохранение восстановленных файлов на тот же физический диск может перезаписать и безвозвратно повредить данные, которые вы пытаетесь восстановить. Выберите папку на другом физическом диске. Продолжайте только если папка назначения находится на другом диске.", "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.": "Сохранение здесь может перезаписать удалённые файлы и безвозвратно повредить их. Выберите папку на другом физическом диске, например USB-диске.", "Could not connect to the update server. Check your internet connection and try again.": "Не удалось подключиться к серверу обновлений. Проверьте подключение к интернету и повторите попытку.", @@ -147,7 +154,7 @@ "Recover in three steps": "Восстановление за три шага", "1. Stop using the drive you lost files from. Don't save, download or install anything to it.\n\n2. Choose that drive or a raw disk image, then scan its unused space. Stop any time and keep partial results.\n\n3. Inspect and select files, then save them to a different physical drive.": "1. Перестаньте пользоваться диском, с которого пропали файлы. Ничего не сохраняйте, не скачивайте и не устанавливайте на него.\n\n2. Выберите этот диск или необработанный образ диска и просканируйте его неиспользуемое пространство. Можно остановиться и сохранить частичные результаты.\n\n3. Проверьте и выберите файлы, затем сохраните их на другом физическом диске.", "Supported formats": "Поддерживаемые форматы", - "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, and NTFS volumes.": "Фото, документы, архивы, аудио, видео, приложения и небольшие резидентные файлы NTFS. Сканирование свободного пространства томов FAT, exFAT и NTFS.", + "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, NTFS, ext2/3/4, and HFS+ volumes.": "Фото, документы, архивы, аудио, видео, приложения и небольшие резидентные файлы NTFS. Сканирование свободного пространства томов FAT, exFAT, NTFS, ext2/3/4 и HFS+.", "Built with Rust and Slint · MIT licensed application": "Создано на Rust и Slint · Приложение под лицензией MIT", "Locked": "Заблокирован", "Stopping after the current step…": "Остановка после текущего этапа…", @@ -217,5 +224,8 @@ "Could not restart as administrator: {0}": "Не удалось перезапустить от имени администратора: {0}", "The source stopped returning data before its reported end. It may be truncated, disconnected, or restricted by the operating system. Files already found can still be recovered.": "Источник перестал возвращать данные до заявленного конца. Возможно, он усечён, отключён или доступ ограничен операционной системой. Уже найденные файлы по-прежнему можно восстановить.", "Could not prepare read-only access to this volume: {0}": "Не удалось подготовить доступ к этому тому только для чтения: {0}", - "Updates are managed by the Snap Store.": "Обновления управляются через Snap Store." + "Updates are managed by the Snap Store.": "Обновления управляются через Snap Store.", + "Choose another folder": "Выбрать другую папку", + "Save here anyway": "Всё равно сохранить здесь", + "Cancel": "Отмена" } diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 2bb1cf3..321659b 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -72,7 +72,7 @@ parts: craftctl default craftctl set version="$(python3 -c 'import tomllib; print(tomllib.load(open("Cargo.toml", "rb"))["package"]["version"])')" override-build: | - /usr/bin/rustup toolchain install 1.99.0 --profile minimal + /usr/bin/rustup toolchain install 1.99.0 --profile minimal --no-self-update /usr/bin/rustup run 1.99.0 cargo build --locked --release --bin reclaim install -Dm755 target/release/reclaim "$CRAFT_PART_INSTALL/bin/reclaim" install -Dm644 snap/gui/reclaim.desktop "$CRAFT_PART_INSTALL/usr/share/applications/reclaim.desktop" diff --git a/src/allocation.rs b/src/allocation.rs index ee5c8d6..530d91c 100644 --- a/src/allocation.rs +++ b/src/allocation.rs @@ -4,7 +4,12 @@ use std::{ io::{self, Read, Seek, SeekFrom}, sync::atomic::{AtomicBool, Ordering}, }; +mod directory; +mod ext; mod fat; +mod hfs; +mod indirect; +mod journal; mod ntfs; const MAX_MAP: usize = 64 * 1024 * 1024; const MAX_RANGES: usize = 1_000_000; @@ -13,6 +18,10 @@ pub(crate) struct Plan { pub ranges: Vec<(u64, u64)>, pub unused: u64, pub filesystem: &'static str, + /// Deleted files rebuilt from filesystem records, reported before carving. + pub files: Vec, + /// Set on ext2/ext3, whose freed block maps can put split files back in order. + pub indirect: Option, } fn invalid(message: &str) -> io::Error { io::Error::new(io::ErrorKind::InvalidData, message) @@ -20,9 +29,41 @@ fn invalid(message: &str) -> io::Error { fn unsupported() -> io::Error { io::Error::new( io::ErrorKind::Unsupported, - "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, or exFAT). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.", + "Unused-space scanning needs a supported formatted volume (NTFS, FAT12/16/32, exFAT, ext2/3/4, or HFS+). Select the volume/partition rather than a whole-disk image. Occupied space was not scanned.", ) } +fn refs_unsupported() -> io::Error { + io::Error::new( + io::ErrorKind::Unsupported, + "This is a ReFS volume. Reclaim cannot read the ReFS free-space map yet, so this volume was not scanned.", + ) +} +/// Bit order inside each byte of an allocation bitmap. +#[derive(Clone, Copy)] +enum BitOrder { + /// NTFS, exFAT and ext: bit 0 of byte 0 is the first cluster. + LeastSignificantFirst, + /// HFS+: bit 7 of byte 0 is the first block. + MostSignificantFirst, +} +/// Named files found in filesystem metadata, or none when that metadata could +/// not be read. Names are extra on top of the free-space map: a bad sector in a +/// directory, the MFT or the journal must not throw the map away and stop the +/// whole unused-space scan. A stop request still ends the scan. +fn files_or_none( + result: io::Result>, + source: &str, + cancel: &AtomicBool, +) -> io::Result> { + match result { + Err(e) if cancel.load(Ordering::Relaxed) => Err(e), + Err(e) => { + log::warn!("{source} metadata could not be read, named files skipped: {e}"); + Ok(Vec::new()) + } + files => files, + } +} fn check(cancel: &AtomicBool) -> io::Result<()> { if cancel.load(Ordering::Relaxed) { Err(io::Error::new(io::ErrorKind::Interrupted, "Scan stopped")) @@ -70,8 +111,20 @@ impl Plan { ranges: Vec::new(), unused: 0, filesystem, + files: Vec::new(), + indirect: None, } } + /// Whether the byte range lies wholly inside one unused range. + fn is_unused(&self, at: u64, length: u64) -> bool { + let next = self.ranges.partition_point(|(start, _)| *start <= at); + let Some(end) = at.checked_add(length) else { + return false; + }; + next.checked_sub(1) + .and_then(|index| self.ranges.get(index)) + .is_some_and(|(start, size)| end <= start.saturating_add(*size)) + } fn add(&mut self, start: u64, len: u64) -> io::Result<()> { if len == 0 { return Ok(()); @@ -98,6 +151,7 @@ fn from_bitmap( clusters: u64, start: u64, cluster_size: u64, + order: BitOrder, filesystem: &'static str, cancel: &AtomicBool, ) -> io::Result { @@ -109,7 +163,11 @@ fn from_bitmap( if cluster.is_multiple_of(8192) { check(cancel)?; } - if bitmap[(cluster / 8) as usize] & (1 << (cluster % 8)) == 0 { + let mask = match order { + BitOrder::LeastSignificantFirst => 1 << (cluster % 8), + BitOrder::MostSignificantFirst => 0x80 >> (cluster % 8), + }; + if bitmap[(cluster / 8) as usize] & mask == 0 { plan.add(start + cluster * cluster_size, cluster_size)?; } } @@ -120,40 +178,116 @@ pub(crate) fn plan( total: u64, cancel: &AtomicBool, ) -> io::Result { - let boot = read_at(input, 0, 512, total, cancel)?; - if &boot[510..512] != b"\x55\xaa" { - return Err(unsupported()); - } - if &boot[3..11] == b"NTFS " { - ntfs::plan(input, &boot, total, cancel) - } else if &boot[3..11] == b"EXFAT " { - fat::exfat(input, &boot, total, cancel) - } else if matches!(u16at(&boot, 11), 512 | 1024 | 2048 | 4096) && boot[13].is_power_of_two() { - fat::classic(input, &boot, total, cancel) + // Boot sectors (NTFS, FAT, exFAT, ReFS) sit at offset 0; the ext superblock + // and HFS+ volume header sit at 1024, so read enough for both. + let head = read_at( + input, + 0, + crate::bytes::to_usize(total.min(2048)), + total, + cancel, + )?; + let boot = head.get(..512).ok_or_else(unsupported)?; + let oem = boot.get(3..11); + let signed = boot.get(510..512) == Some(b"\x55\xaa".as_slice()); + let at_header = head.get(hfs::HEADER..hfs::HEADER + 2); + if oem == Some(b"ReFS\0\0\0\0".as_slice()) { + Err(refs_unsupported()) + } else if signed && oem == Some(b"NTFS ".as_slice()) { + ntfs::plan(input, boot, total, cancel) + } else if signed && oem == Some(b"EXFAT ".as_slice()) { + fat::exfat(input, boot, total, cancel) + } else if crate::bytes::u16_le(&head, ext::SUPERBLOCK + ext::MAGIC_OFFSET) == ext::MAGIC { + ext::plan(input, &head, total, cancel) + } else if at_header == Some(hfs::SIGNATURE_PLUS.as_slice()) + || at_header == Some(hfs::SIGNATURE_X.as_slice()) + { + hfs::plan(input, &head, total, cancel) + } else if signed + && matches!(u16at(boot, 11), 512 | 1024 | 2048 | 4096) + && boot.get(13).is_some_and(|n| n.is_power_of_two()) + { + fat::classic(input, boot, total, cancel) } else { Err(unsupported()) } } -/// A free extent is an independent address space. Reads/seeks cannot cross an -/// allocated gap, including candidate lookahead by the ZIP/executable probes. -struct Extent<'a, R> { +/// All unused space as one stream: the free ranges, in disk order, with the +/// space still in use left out. +/// +/// When a drive fills its gaps in order, a fragmented file continues in the +/// next free range, so carving across the joins brings it back whole. Formats +/// with an internal check (image decode, checksums, ZIP directory) reject a +/// wrong join; a file found across a join records its pieces as `extents`. +struct FreeSpace<'a, R> { input: &'a mut R, - start: u64, + /// Physical `(start, length)` pieces and the stream offset each begins at. + pieces: Vec<(u64, u64, u64)>, length: u64, position: u64, } -impl Read for Extent<'_, R> { +impl<'a, R> FreeSpace<'a, R> { + fn new(input: &'a mut R, ranges: &[(u64, u64)]) -> Self { + let mut stream_start = 0u64; + let pieces = ranges + .iter() + .map(|&(start, length)| { + let piece = (start, length, stream_start); + stream_start = stream_start.saturating_add(length); + piece + }) + .collect(); + Self { + input, + pieces, + length: stream_start, + position: 0, + } + } +} +/// The piece holding stream offset `at`, as (physical start, length, stream start). +fn piece(pieces: &[(u64, u64, u64)], at: u64) -> Option<(u64, u64, u64)> { + let index = pieces + .partition_point(|(_, _, stream_start)| *stream_start <= at) + .checked_sub(1)?; + pieces + .get(index) + .copied() + .filter(|(_, length, stream_start)| at < stream_start.saturating_add(*length)) +} +/// The physical offset of stream offset `at`. +fn physical(pieces: &[(u64, u64, u64)], at: u64) -> u64 { + piece(pieces, at).map_or(at, |(start, _, stream_start)| start + (at - stream_start)) +} +/// Physical ranges holding stream bytes `at..at + size`. +fn physical_ranges(pieces: &[(u64, u64, u64)], at: u64, size: u64) -> Vec<(u64, u64)> { + let mut ranges = Vec::new(); + let mut cursor = at; + let end = at.saturating_add(size); + while cursor < end { + let Some((start, length, stream_start)) = piece(pieces, cursor) else { + break; + }; + let take = (stream_start + length).min(end) - cursor; + ranges.push((start + (cursor - stream_start), take)); + cursor += take; + } + ranges +} +impl Read for FreeSpace<'_, R> { fn read(&mut self, b: &mut [u8]) -> io::Result { - let length = b.len().min(crate::bytes::to_usize( - self.length.saturating_sub(self.position), - )); - if length == 0 { + let Some((start, length, stream_start)) = piece(&self.pieces, self.position) else { + return Ok(0); + }; + let in_piece = stream_start + length - self.position; + let count = b.len().min(crate::bytes::to_usize(in_piece)); + if count == 0 { return Ok(0); } self.input - .seek(SeekFrom::Start(self.start + self.position))?; - let count = self.input.read(&mut b[..length])?; + .seek(SeekFrom::Start(start + (self.position - stream_start)))?; + let count = self.input.read(&mut b[..count])?; if count == 0 { return Err(io::Error::new( io::ErrorKind::UnexpectedEof, @@ -164,7 +298,7 @@ impl Read for Extent<'_, R> { Ok(count) } } -impl Seek for Extent<'_, R> { +impl Seek for FreeSpace<'_, R> { fn seek(&mut self, from: SeekFrom) -> io::Result { let next = match from { SeekFrom::Start(n) => i128::from(n), @@ -175,84 +309,203 @@ impl Seek for Extent<'_, R> { Ok(self.position) } } +/// Checks files rebuilt from records against their contents and removes the +/// space of the ones that match from the carving plan. +/// +/// A record can be older than the data now in its clusters. Only a file whose +/// first bytes match its type is trusted to own that space; the others stay in +/// the list as unverified and their space is still carved, so a newer deleted +/// file stored there is not hidden. +fn exclude_known_files(input: &mut (impl Read + Seek), plan: &mut Plan) -> io::Result<()> { + let mut owned: Vec<(u64, u64)> = Vec::new(); + for file in &mut plan.files { + let Some(extents) = file.extents.clone() else { + continue; + }; + let mut head = Vec::with_capacity(64); + let read = crate::FoundReader::new(input, file) + .and_then(|reader| reader.take(64).read_to_end(&mut head)); + if read.is_err() { + // Unreadable start (a bad sector): keep the record, but it cannot + // prove it owns its space, so that space is still carved. + file.validation = crate::Validation::Unverified; + continue; + } + if file.original_name.is_none() { + // A nameless record: the bytes are the only clue to the type. + match crate::sniff_kind(&head) { + Some(kind) => { + file.kind = kind; + owned.extend(extents.iter().copied()); + } + None => file.validation = crate::Validation::Unverified, + } + } else if crate::starts_like(file.kind, &head) { + owned.extend(extents.iter().copied()); + } else { + file.validation = crate::Validation::Unverified; + } + } + if owned.is_empty() { + return Ok(()); + } + owned.sort_unstable(); + plan.ranges = subtract(&plan.ranges, &owned); + plan.unused = plan.ranges.iter().map(|(_, length)| length).sum(); + Ok(()) +} +/// `ranges` with the sorted `owned` ranges cut out. +fn subtract(ranges: &[(u64, u64)], owned: &[(u64, u64)]) -> Vec<(u64, u64)> { + let mut kept = Vec::with_capacity(ranges.len()); + let mut next = 0; + for &(start, length) in ranges { + let end = start.saturating_add(length); + let mut cursor = start; + while let Some(&(at, size)) = owned.get(next) { + let owned_end = at.saturating_add(size); + if owned_end <= cursor { + next += 1; + continue; + } + if at >= end { + break; + } + if at > cursor { + kept.push((cursor, at - cursor)); + } + cursor = cursor.max(owned_end); + if owned_end > end { + break; + } + next += 1; + } + if cursor < end { + kept.push((cursor, end - cursor)); + } + } + kept +} +/// A read buffer no bigger than the space to scan, at most 1 MiB. +fn to_usize_min(length: u64) -> usize { + crate::bytes::to_usize(length.min(1024 * 1024)) +} pub(crate) fn scan( input: &mut (impl Read + Seek), total: u64, cancel: &AtomicBool, mut event: impl FnMut(crate::Event), ) -> io::Result<()> { - let plan = match plan(input, total, cancel) { + let mut plan = match plan(input, total, cancel) { Err(e) if e.kind() == io::ErrorKind::Interrupted && cancel.load(Ordering::Relaxed) => { return Ok(()); } result => result?, }; + exclude_known_files(input, &mut plan)?; event(crate::Event::UnusedSpace { bytes: plan.unused, filesystem: plan.filesystem, }); - let mut completed = 0; let mut found_count = 0; let mut resident_bytes = 0usize; - for (start, length) in plan.ranges { - if cancel.load(Ordering::Relaxed) { - return Ok(()); + // Files rebuilt from records come first; carving must not list them again. + let mut known = std::collections::HashSet::new(); + for file in std::mem::take(&mut plan.files) { + // An unverified record's space is still carved; a newer file found + // there by carving is real and must not be hidden behind the record. + if file.validation != crate::Validation::Unverified { + known.insert(file.offset); } - let mut extent = Extent { - input, - start, - length, - position: 0, - }; - let mut limited = false; - crate::resilient::scan( - &mut extent, - cancel, - |e| match e { - crate::Event::Found(mut file) => { - found_count += 1; - resident_bytes += file.resident_data.as_ref().map_or(0, |data| data.len()); - if found_count > 100_000 || resident_bytes > MAX_MAP { - limited = true; - return; - } - file.offset += start; - event(crate::Event::Found(file)); + found_count += 1; + resident_bytes += file.resident_data.as_ref().map_or(0, |data| data.len()); + event(crate::Event::Found(file)); + } + if cancel.load(Ordering::Relaxed) || plan.ranges.is_empty() { + return Ok(()); + } + // ext2/ext3: read free space once for leftover block maps, then carve + // with each rebuilt file's pieces in order. Each pass is half the progress. + let half = plan.unused / 2; + let order = match plan.indirect { + Some(geometry) => { + let files = match indirect::chains(input, &plan, geometry, cancel, |done| { + event(crate::Event::Progress((done / 2).min(half))); + }) { + Err(e) + if e.kind() == io::ErrorKind::Interrupted && cancel.load(Ordering::Relaxed) => + { + return Ok(()); } - crate::Event::Progress(bytes) => { - event(crate::Event::Progress(completed + bytes.min(length))) + result => result?, + }; + Some(indirect::ordered(&plan.ranges, &files)) + } + None => None, + }; + let base = if order.is_some() { half } else { 0 }; + let scale = |at: u64| if order.is_some() { base + at / 2 } else { at }; + let mut stream = FreeSpace::new(input, order.as_deref().unwrap_or(&plan.ranges)); + let pieces = stream.pieces.clone(); + let length = stream.length; + let mut limited = false; + crate::resilient::scan( + &mut stream, + cancel, + |e| match e { + crate::Event::Found(mut file) => { + found_count += 1; + resident_bytes += file.resident_data.as_ref().map_or(0, |data| data.len()); + if found_count > 100_000 || resident_bytes > MAX_MAP { + limited = true; + return; } - crate::Event::Checking { - offset, - kind, - bytes, - } => { - event(crate::Event::Progress(completed + offset.min(length))); - event(crate::Event::Checking { - offset: start + offset, - kind, - bytes, - }); + let ranges = physical_ranges(&pieces, file.offset, file.size); + file.offset = physical(&pieces, file.offset); + if known.contains(&file.offset) { + return; } - crate::Event::Unreadable { offset, bytes } => { - event(crate::Event::Unreadable { - offset: start + offset, - bytes, - }); + if ranges.len() > 1 && file.resident_data.is_none() { + file.extents = Some(ranges.into()); } - // The full-source scan of a free extent never reports unused space. - crate::Event::UnusedSpace { .. } => {} - }, - length.min(1024 * 1024) as usize, - )?; - if limited { - return Err(invalid( - "Candidate limit reached (100,000). Recover available results.", - )); - } - completed += length; - event(crate::Event::Progress(completed)); + event(crate::Event::Found(file)); + } + crate::Event::Progress(bytes) => { + event(crate::Event::Progress(scale(bytes.min(length)))) + } + crate::Event::Checking { + offset, + kind, + bytes, + } => { + event(crate::Event::Progress(scale(offset.min(length)))); + event(crate::Event::Checking { + offset: physical(&pieces, offset), + kind, + bytes, + }); + } + crate::Event::Unreadable { offset, bytes } => { + event(crate::Event::Unreadable { + offset: physical(&pieces, offset), + bytes, + }); + } + // The carving pass over free space never reports unused space. + crate::Event::UnusedSpace { .. } => {} + }, + to_usize_min(length), + &|at| { + piece(&pieces, at).map_or(u64::MAX, |(_, length, stream_start)| { + stream_start.saturating_add(length) + }) + }, + )?; + if limited { + return Err(invalid( + "Candidate limit reached (100,000). Recover available results.", + )); } + event(crate::Event::Progress(length)); Ok(()) } diff --git a/src/allocation/directory.rs b/src/allocation/directory.rs new file mode 100644 index 0000000..3a15b26 --- /dev/null +++ b/src/allocation/directory.rs @@ -0,0 +1,467 @@ +//! Deleted files named by FAT12/16/32 and exFAT directory entries. +//! +//! A deleted entry keeps the file's name (FAT loses the first letter of the +//! short name), its exact size and its first cluster. Files are rebuilt only +//! when every cluster they need is still marked free, so their data cannot +//! belong to a newer file. Directories are walked with bounded depth, entries +//! and bytes, and each directory cluster is visited once. +// Hardened module: no unchecked indexing or arithmetic in shipped code (CWE-125, CWE-190). +#![cfg_attr( + not(test), + deny(clippy::indexing_slicing, clippy::arithmetic_side_effects) +)] + +use std::collections::HashSet; +use std::sync::Arc; + +use crate::bytes; +use crate::{Found, Validation}; + +/// Deepest directory nesting followed. +pub(super) const MAX_DEPTH: usize = 32; +/// Most directories read on one volume. +pub(super) const MAX_DIRECTORIES: usize = 20_000; +/// Most bytes of directory data read on one volume. +pub(super) const MAX_DIRECTORY_BYTES: u64 = 128 * 1024 * 1024; +/// Most deleted files reported from one volume. +pub(super) const MAX_FILES: usize = 50_000; +/// Longest cluster chain followed for one file or directory. +pub(super) const MAX_CHAIN: usize = 1 << 20; + +/// Where the data heap starts and how big a cluster is. +#[derive(Clone, Copy)] +pub(super) struct Heap { + pub start: u64, + pub cluster_size: u64, + /// Highest valid cluster number (clusters are numbered from 2). + pub last: u64, +} +impl Heap { + pub(super) fn offset(self, cluster: u64) -> Option { + if cluster < 2 || cluster > self.last { + return None; + } + cluster + .checked_sub(2)? + .checked_mul(self.cluster_size)? + .checked_add(self.start) + } + /// Contiguous byte extents for a cluster list, merged where adjacent. + pub(super) fn extents(self, clusters: &[u64]) -> Option> { + let mut extents: Vec<(u64, u64)> = Vec::new(); + for cluster in clusters { + let at = self.offset(*cluster)?; + match extents.last_mut() { + Some((start, length)) if start.checked_add(*length) == Some(at) => { + *length = length.checked_add(self.cluster_size)?; + } + _ => extents.push((at, self.cluster_size)), + } + } + Some(extents) + } + pub(super) fn cluster_count(self, size: u64) -> Option { + Some(size.div_ceil(self.cluster_size).max(1)) + } +} + +/// A deleted file found in a directory: name, size and clusters. +pub(super) fn found(name: String, size: u64, extents: Vec<(u64, u64)>) -> Option { + let offset = extents.first()?.0; + Some(Found { + offset, + size, + kind: crate::ntfs::extension_kind(&name), + selected: true, + // The entry describes the data; its bytes are checked before carving trusts it. + validation: Validation::MetadataChecked, + original_name: Some(name), + resident_data: None, + ntfs: None, + extents: Some(Arc::from(extents)), + }) +} + +/// A name from directory data is shown and used as a file name, so it must +/// be printable and must not climb or split paths. +pub(super) fn clean_name(name: String) -> Option { + let name = name.trim_end_matches([' ', '.']).to_owned(); + (!name.is_empty() + && name.chars().count() <= 255 + && !name + .chars() + .any(|c| c.is_control() || matches!(c, '/' | '\\' | '\0')) + && !matches!(name.as_str(), "." | "..")) + .then_some(name) +} + +/// Tracks the work budget of one directory walk. +pub(super) struct Budget { + pub directories: usize, + pub bytes: u64, + pub visited: HashSet, +} +impl Budget { + pub(super) fn new() -> Self { + Self { + directories: 0, + bytes: 0, + visited: HashSet::new(), + } + } + /// Takes `bytes` of directory data from the budget; false when spent. + pub(super) fn take(&mut self, bytes: u64) -> bool { + let Some(total) = self.bytes.checked_add(bytes) else { + return false; + }; + if total > MAX_DIRECTORY_BYTES || self.directories >= MAX_DIRECTORIES { + return false; + } + self.bytes = total; + self.directories = self.directories.saturating_add(1); + true + } +} + +// ---------------------------------------------------------------- FAT12/16/32 + +/// The checksum a long-name entry stores for its 8.3 short name. +fn short_name_checksum(short: &[u8; 11]) -> u8 { + short + .iter() + .fold(0u8, |sum, byte| sum.rotate_right(1).wrapping_add(*byte)) +} + +/// Up to 13 UTF-16 characters of one long-name entry, without padding. +fn long_name_part(entry: &[u8]) -> Vec { + let ranges = [(1usize, 11usize), (14, 26), (28, 32)]; + let mut units = Vec::with_capacity(13); + for (from, to) in ranges { + for pair in entry.get(from..to).unwrap_or_default().as_chunks::<2>().0 { + let unit = u16::from_le_bytes(*pair); + if unit == 0 || unit == 0xffff { + return units; + } + units.push(unit); + } + } + units +} + +/// The short 8.3 name of a deleted entry, whose first byte was overwritten. +fn short_name(entry: &[u8], first: u8) -> Option { + let base = entry.get(1..8)?; + let extension = entry.get(8..11)?; + let mut name = String::new(); + name.push(char::from(first)); + name.extend(base.iter().map(|b| char::from(*b))); + let mut name = name.trim_end().to_owned(); + let extension = extension.iter().map(|b| char::from(*b)).collect::(); + let extension = extension.trim_end(); + if !extension.is_empty() { + name.push('.'); + name.push_str(extension); + } + (name.is_ascii()).then_some(name) +} + +/// One parsed FAT directory record. +pub(super) enum FatEntry { + /// A live subdirectory starting at this cluster. + Directory(u64), + /// A deleted file: name, size and first cluster. + Deleted(String, u64, u64), +} + +/// Parses the 32-byte records of one FAT directory. +/// +/// `fat32` selects whether the high cluster word is used. +pub(super) fn fat_entries(data: &[u8], fat32: bool) -> Vec { + let mut entries = Vec::new(); + // Long-name parts in on-disk order (last part first) and their checksum. + let mut long_parts: Vec<(u8, Vec)> = Vec::new(); + for entry in data.as_chunks::<32>().0 { + let Some(&marker) = entry.first() else { + break; + }; + if marker == 0 { + break; + } + let attributes = entry.get(11).copied().unwrap_or(0); + if attributes == 0x0f { + long_parts.push((entry.get(13).copied().unwrap_or(0), long_name_part(entry))); + continue; + } + let parts = std::mem::take(&mut long_parts); + if attributes & 0x08 != 0 { + continue; + } + let high = if fat32 { + u64::from(bytes::u16_le(entry, 20)) + } else { + 0 + }; + let cluster = (high << 16) | u64::from(bytes::u16_le(entry, 26)); + let size = u64::from(bytes::u32_le(entry, 28)); + if marker != 0xe5 { + if attributes & 0x10 != 0 && marker != b'.' && cluster >= 2 { + entries.push(FatEntry::Directory(cluster)); + } + continue; + } + if attributes & 0x10 != 0 || size == 0 || cluster < 2 { + continue; + } + let Some(short) = entry.get(..11).and_then(|s| <[u8; 11]>::try_from(s).ok()) else { + continue; + }; + // The long name survives deletion; its checksum also recovers the + // short name's lost first letter. + let long = parts.first().and_then(|(checksum, _)| { + if parts.iter().any(|(sum, _)| sum != checksum) { + return None; + } + let units: Vec = parts + .iter() + .rev() + .flat_map(|(_, part)| part.clone()) + .collect(); + let long = String::from_utf16(&units).ok()?; + // The short name is derived from the long one, so only its first + // letter (or `_` for one that has no 8.3 form) can have been lost. + // Trying every letter would accept a stranger's name 1 time in 3. + let derived = long + .chars() + .find(|c| !matches!(c, ' ' | '.')) + .filter(char::is_ascii_alphanumeric) + .map(|c| c.to_ascii_uppercase() as u8); + let matches = |first: u8| { + let mut original = short; + if let Some(byte) = original.first_mut() { + *byte = first; + } + short_name_checksum(&original) == *checksum + }; + (derived.is_some_and(matches) || matches(b'_')).then_some(long) + }); + let name = long.or_else(|| short_name(entry, b'_')); + if let Some(name) = name.and_then(clean_name) { + entries.push(FatEntry::Deleted(name, size, cluster)); + } + } + entries +} + +// ---------------------------------------------------------------- exFAT + +/// One parsed exFAT directory entry set. +pub(super) enum ExfatEntry { + /// A live subdirectory: first cluster, length, and whether it is contiguous. + Directory(u64, u64, bool), + /// A deleted file: name, size, first cluster, and whether it is contiguous. + Deleted(String, u64, u64, bool), +} + +/// Parses the entry sets of one exFAT directory. +pub(super) fn exfat_entries(data: &[u8]) -> Vec { + let mut entries = Vec::new(); + let records: Vec<&[u8]> = data + .as_chunks::<32>() + .0 + .iter() + .map(|r| r.as_slice()) + .collect(); + let mut index = 0usize; + while let Some(record) = records.get(index) { + let kind = record.first().copied().unwrap_or(0); + if kind == 0 { + break; + } + index = index.saturating_add(1); + // File entry: 0x85 in use, 0x05 deleted. + if kind & 0x7f != 0x05 { + continue; + } + let in_use = kind & 0x80 != 0; + let secondary = usize::from(record.get(1).copied().unwrap_or(0)); + let attributes = bytes::u16_le(record, 4); + if !(2..=18).contains(&secondary) { + continue; + } + let Some(set) = records.get(index..index.saturating_add(secondary)) else { + break; + }; + let Some(stream) = set.first() else { + continue; + }; + // Stream extension: 0xc0 in use, 0x40 deleted, matching the file entry. + let stream_kind = stream.first().copied().unwrap_or(0); + if stream_kind & 0x7f != 0x40 || (stream_kind & 0x80 != 0) != in_use { + continue; + } + let flags = stream.get(1).copied().unwrap_or(0); + let name_length = usize::from(stream.get(3).copied().unwrap_or(0)); + let cluster = u64::from(bytes::u32_le(stream, 20)); + let length = bytes::u64_le(stream, 24); + let contiguous = flags & 0x02 != 0; + // name_length comes from one byte, so it is at most 255. + let mut units = Vec::with_capacity(name_length.min(255)); + for name in set.iter().skip(1) { + if name.first().copied().unwrap_or(0) & 0x7f != 0x41 { + break; + } + for pair in name.get(2..32).unwrap_or_default().as_chunks::<2>().0 { + if units.len() < name_length { + units.push(u16::from_le_bytes(*pair)); + } + } + } + index = index.saturating_add(secondary); + if cluster < 2 { + continue; + } + if attributes & 0x10 != 0 { + if in_use { + entries.push(ExfatEntry::Directory(cluster, length, contiguous)); + } + continue; + } + if in_use || length == 0 || units.len() != name_length { + continue; + } + if let Some(name) = String::from_utf16(&units).ok().and_then(clean_name) { + entries.push(ExfatEntry::Deleted(name, length, cluster, contiguous)); + } + } + entries +} + +#[cfg(test)] +mod tests { + use super::*; + + fn short_entry(name: &[u8; 11], first: u8, attributes: u8, cluster: u16, size: u32) -> Vec { + let mut entry = vec![0; 32]; + entry[..11].copy_from_slice(name); + entry[0] = first; + entry[11] = attributes; + entry[26..28].copy_from_slice(&cluster.to_le_bytes()); + entry[28..32].copy_from_slice(&size.to_le_bytes()); + entry + } + + /// Deleted long-name entries for `text`, in on-disk order (last part first). + fn long_entry(checksum: u8, text: &str) -> Vec { + let mut units: Vec = text.encode_utf16().collect(); + units.push(0); + let slots = [1, 3, 5, 7, 9, 14, 16, 18, 20, 22, 24, 28, 30]; + let mut entries = Vec::new(); + for part in units.chunks(13).rev() { + let mut entry = vec![0xff; 32]; + entry[0] = 0xe5; + entry[11] = 0x0f; + entry[12] = 0; + entry[13] = checksum; + entry[26] = 0; + entry[27] = 0; + for (slot, unit) in slots.iter().zip(part) { + entry[*slot..*slot + 2].copy_from_slice(&unit.to_le_bytes()); + } + entries.extend(entry); + } + entries + } + + #[test] + fn deleted_fat_entries_keep_long_names_and_restore_short_names() { + let short = *b"HOLIDA~1JPG"; + let checksum = short_name_checksum(&short); + let mut data = Vec::new(); + data.extend(long_entry(checksum, "Holiday photo.jpg")); + data.extend(short_entry(&short, 0xe5, 0x20, 7, 5000)); + data.extend(short_entry(b"NOTES TXT", 0xe5, 0x20, 9, 10)); + data.extend(short_entry(b"PICTURES ", b'P', 0x10, 12, 0)); + data.extend(short_entry(b"EMPTY TXT", 0xe5, 0x20, 13, 0)); + let entries = fat_entries(&data, false); + assert!( + matches!(&entries[0], FatEntry::Deleted(name, 5000, 7) if name == "Holiday photo.jpg") + ); + assert!(matches!(&entries[1], FatEntry::Deleted(name, 10, 9) if name == "_OTES.TXT")); + assert!(matches!(entries[2], FatEntry::Directory(12))); + assert_eq!(entries.len(), 3); + } + + #[test] + fn a_long_name_with_the_wrong_checksum_falls_back_to_the_short_name() { + let mut data = long_entry(0x12, "Somebody else.txt"); + data.extend(short_entry(b"REPORT PDF", 0xe5, 0x20, 5, 99)); + let entries = fat_entries(&data, true); + assert!(matches!(&entries[0], FatEntry::Deleted(name, 99, 5) if name == "_EPORT.PDF")); + } + + #[test] + fn deleted_exfat_sets_keep_names_sizes_and_contiguity() { + let mut data = vec![0u8; 32 * 6]; + let set = |data: &mut Vec, + at: usize, + kind: u8, + attributes: u16, + name: &str, + cluster: u32, + length: u64, + flags: u8| { + let units: Vec = name.encode_utf16().collect(); + let names = units.len().div_ceil(15); + data[at * 32] = kind; + data[at * 32 + 1] = u8::try_from(1 + names).unwrap(); + data[at * 32 + 4..at * 32 + 6].copy_from_slice(&attributes.to_le_bytes()); + let stream = (at + 1) * 32; + data[stream] = (kind & 0x80) | 0x40; + data[stream + 1] = flags; + data[stream + 3] = u8::try_from(units.len()).unwrap(); + data[stream + 20..stream + 24].copy_from_slice(&cluster.to_le_bytes()); + data[stream + 24..stream + 32].copy_from_slice(&length.to_le_bytes()); + for (i, chunk) in units.chunks(15).enumerate() { + let entry = (at + 2 + i) * 32; + data[entry] = (kind & 0x80) | 0x41; + for (j, unit) in chunk.iter().enumerate() { + data[entry + 2 + j * 2..entry + 4 + j * 2].copy_from_slice(&unit.to_le_bytes()); + } + } + }; + set( + &mut data, + 0, + 0x05, + 0x20, + "Video 2024.mp4", + 40, + 123_456_789, + 0x03, + ); + set(&mut data, 3, 0x85, 0x10, "DCIM", 8, 4096, 0x03); + let entries = exfat_entries(&data); + assert!( + matches!(&entries[0], ExfatEntry::Deleted(name, 123_456_789, 40, true) if name == "Video 2024.mp4") + ); + assert!(matches!(entries[1], ExfatEntry::Directory(8, 4096, true))); + } + + #[test] + fn hostile_directory_bytes_never_panic() { + let mut data = vec![0u8; 4096]; + for seed in 0u32..2000 { + for (i, byte) in data.iter_mut().enumerate() { + let mixed = seed + .wrapping_mul(2_654_435_761) + .wrapping_add(u32::try_from(i).unwrap().wrapping_mul(40_503)); + *byte = mixed.to_le_bytes()[1]; + } + let _ = fat_entries(&data, seed % 2 == 0); + let _ = exfat_entries(&data); + } + assert!(clean_name("../x".into()).is_none()); + assert!(clean_name("a\u{0}b".into()).is_none()); + assert_eq!(clean_name("name. ".into()).as_deref(), Some("name")); + } +} diff --git a/src/allocation/ext.rs b/src/allocation/ext.rs new file mode 100644 index 0000000..5875327 --- /dev/null +++ b/src/allocation/ext.rs @@ -0,0 +1,328 @@ +//! ext2, ext3 and ext4 free-block maps, read from each block group's bitmap. +//! +//! The map comes from the superblock, the group descriptor table and the block +//! bitmaps. Deleted files lose their block pointers, so they are rebuilt from +//! journal copies (`journal`, ext3/ext4) or leftover block maps (`indirect`, +//! ext2/ext3) before free blocks are carved. +// Hardened module: no unchecked indexing or arithmetic in shipped code (CWE-125, CWE-190). +#![cfg_attr( + not(test), + deny(clippy::indexing_slicing, clippy::arithmetic_side_effects) +)] + +use std::io::{self, Read, Seek}; +use std::sync::atomic::AtomicBool; + +use crate::bytes; + +use super::{MAX_MAP, Plan, check, invalid, product, read_at}; + +/// Byte offset of the superblock from the start of the volume. +pub(super) const SUPERBLOCK: usize = 1024; +/// `s_magic`, relative to the superblock. +pub(super) const MAGIC_OFFSET: usize = 0x38; +pub(super) const MAGIC: u16 = 0xef53; + +const COMPAT_HAS_JOURNAL: u32 = 0x4; + +const INCOMPAT_FILETYPE: u32 = 0x2; +// Set while a journaled volume is mounted or after an unclean shutdown. +const INCOMPAT_RECOVER: u32 = 0x4; +const INCOMPAT_EXTENTS: u32 = 0x40; +const INCOMPAT_64BIT: u32 = 0x80; +const INCOMPAT_MMP: u32 = 0x100; +const INCOMPAT_FLEX_BG: u32 = 0x200; +const INCOMPAT_EA_INODE: u32 = 0x400; +const INCOMPAT_DIRDATA: u32 = 0x1000; +const INCOMPAT_CSUM_SEED: u32 = 0x2000; +const INCOMPAT_LARGEDIR: u32 = 0x4000; +const INCOMPAT_INLINE_DATA: u32 = 0x8000; +const INCOMPAT_ENCRYPT: u32 = 0x10000; +const INCOMPAT_CASEFOLD: u32 = 0x20000; +/// Features that do not change where block bitmaps live or what a bit means. +/// Compression, external journal devices and META_BG descriptor placement are +/// not in this list, so those volumes are rejected instead of misread. +const INCOMPAT_KNOWN: u32 = INCOMPAT_FILETYPE + | INCOMPAT_RECOVER + | INCOMPAT_EXTENTS + | INCOMPAT_64BIT + | INCOMPAT_MMP + | INCOMPAT_FLEX_BG + | INCOMPAT_EA_INODE + | INCOMPAT_DIRDATA + | INCOMPAT_CSUM_SEED + | INCOMPAT_LARGEDIR + | INCOMPAT_INLINE_DATA + | INCOMPAT_ENCRYPT + | INCOMPAT_CASEFOLD; +/// Features that only ext4 sets; without them a journaled volume is ext3. +const INCOMPAT_EXT4: u32 = INCOMPAT_EXTENTS + | INCOMPAT_64BIT + | INCOMPAT_FLEX_BG + | INCOMPAT_EA_INODE + | INCOMPAT_INLINE_DATA + | INCOMPAT_ENCRYPT + | INCOMPAT_CASEFOLD + | INCOMPAT_CSUM_SEED + | INCOMPAT_LARGEDIR; + +const RO_COMPAT_GDT_CSUM: u32 = 0x10; +// One bitmap bit per cluster of blocks instead of per block. +const RO_COMPAT_BIGALLOC: u32 = 0x200; +const RO_COMPAT_METADATA_CSUM: u32 = 0x400; + +/// `bg_flags`: the group's block bitmap was never written because no block in +/// the group was ever allocated. Only trusted when group checksums are on. +const BLOCK_UNINIT: u16 = 0x2; + +/// Superblock geometry checked before any group is read. +struct Geometry { + block_size: u64, + blocks: u64, + first_data_block: u64, + blocks_per_group: u64, + descriptor_size: u64, + is_64bit: bool, + uninit_trusted: bool, + /// Files are mapped by extent trees, not block maps (ext4). + extents: bool, + name: &'static str, +} + +fn unsupported_feature() -> io::Error { + io::Error::new( + io::ErrorKind::Unsupported, + "This ext volume uses a feature Reclaim cannot map yet (compression, an external journal, META_BG, or bigalloc), so it was not scanned.", + ) +} + +fn u32_field(superblock: &[u8], at: usize) -> u32 { + bytes::u32_le(superblock, at) +} + +fn geometry(superblock: &[u8], total: u64) -> io::Result { + let log_block_size = u32_field(superblock, 0x18); + let first_data_block = u64::from(u32_field(superblock, 0x14)); + let blocks_per_group = u64::from(u32_field(superblock, 0x20)); + let revision = u32_field(superblock, 0x4c); + let (compat, incompat, ro_compat) = if revision == 0 { + (0, 0, 0) + } else { + ( + u32_field(superblock, 0x5c), + u32_field(superblock, 0x60), + u32_field(superblock, 0x64), + ) + }; + if incompat & !INCOMPAT_KNOWN != 0 || ro_compat & RO_COMPAT_BIGALLOC != 0 { + return Err(unsupported_feature()); + } + if log_block_size > 6 { + return Err(invalid("Invalid ext block size")); + } + let block_size = 1024u64 << log_block_size; + let is_64bit = incompat & INCOMPAT_64BIT != 0; + let high = if is_64bit { + u64::from(u32_field(superblock, 0x150)) + } else { + 0 + }; + let blocks = high + .checked_shl(32) + .map(|high| high | u64::from(u32_field(superblock, 0x04))) + .ok_or_else(|| invalid("Invalid ext block count"))?; + let descriptor_size = if is_64bit { + u64::from(bytes::u16_le(superblock, 0xfe)) + } else { + 32 + }; + let expected_first = u64::from(block_size == 1024); + if first_data_block != expected_first + || blocks_per_group == 0 + || !blocks_per_group.is_multiple_of(8) + || blocks_per_group > block_size.saturating_mul(8) + || blocks <= first_data_block + || product(blocks, block_size)? > total + || !descriptor_size.is_power_of_two() + || !(32..=1024).contains(&descriptor_size) + || (is_64bit && descriptor_size < 64) + { + return Err(invalid("Invalid ext volume geometry")); + } + let name = if compat & COMPAT_HAS_JOURNAL == 0 { + "ext2" + } else if incompat & INCOMPAT_EXT4 == 0 { + "ext3" + } else { + "ext4" + }; + Ok(Geometry { + block_size, + blocks, + first_data_block, + blocks_per_group, + descriptor_size, + is_64bit, + uninit_trusted: ro_compat & (RO_COMPAT_GDT_CSUM | RO_COMPAT_METADATA_CSUM) != 0, + extents: incompat & INCOMPAT_EXTENTS != 0, + name, + }) +} + +/// Adds every clear bit of one group's bitmap to `plan` as a free block. +fn add_free_blocks( + plan: &mut Plan, + bitmap: &[u8], + first_block: u64, + count: u64, + block_size: u64, +) -> io::Result<()> { + let mut index = 0u64; + for byte in bitmap { + if index >= count { + break; + } + if *byte == 0xff { + index = index.saturating_add(8); + continue; + } + for bit in 0..8u32 { + if index >= count { + break; + } + if byte & (1 << bit) == 0 { + let block = first_block + .checked_add(index) + .ok_or_else(|| invalid("Invalid ext block number"))?; + plan.add(product(block, block_size)?, block_size)?; + } + index = index.saturating_add(1); + } + } + Ok(()) +} + +/// True when `block` lies inside one of the sorted free ranges of `plan`. +fn is_free(plan: &Plan, block: u64, block_size: u64) -> bool { + let Some(at) = block.checked_mul(block_size) else { + return false; + }; + let next = plan.ranges.partition_point(|(start, _)| *start <= at); + next.checked_sub(1) + .and_then(|index| plan.ranges.get(index)) + .is_some_and(|(start, length)| at < start.saturating_add(*length)) +} + +/// Builds the free-block map of an ext2, ext3 or ext4 volume. +/// +/// `head` holds at least the first 2048 bytes of the volume. +pub(super) fn plan( + input: &mut (impl Read + Seek), + head: &[u8], + total: u64, + cancel: &AtomicBool, +) -> io::Result { + let superblock = head + .get(SUPERBLOCK..SUPERBLOCK.saturating_add(1024)) + .ok_or_else(|| invalid("ext superblock is truncated"))?; + let geometry = geometry(superblock, total)?; + let data_blocks = geometry.blocks.saturating_sub(geometry.first_data_block); + let groups = data_blocks.div_ceil(geometry.blocks_per_group); + let table_length = product(groups, geometry.descriptor_size)?; + if table_length > MAX_MAP as u64 { + return Err(invalid( + "ext group descriptor table is too large to map safely", + )); + } + let table_block = geometry + .first_data_block + .checked_add(1) + .ok_or_else(|| invalid("Invalid ext volume geometry"))?; + let table = read_at( + input, + product(table_block, geometry.block_size)?, + bytes::to_usize(table_length), + total, + cancel, + )?; + let mut plan = Plan::new(geometry.name); + if !geometry.extents { + plan.indirect = Some(super::indirect::Geometry { + block_size: geometry.block_size, + blocks: geometry.blocks, + }); + } + // Blocks the bitmaps themselves must mark as used. + let mut metadata = vec![geometry.first_data_block]; + let table_blocks = table_length.div_ceil(geometry.block_size); + metadata.extend((0..table_blocks).filter_map(|n| table_block.checked_add(n))); + let descriptor_size = bytes::to_usize(geometry.descriptor_size); + for (group, descriptor) in (0u64..).zip(table.chunks_exact(descriptor_size)) { + check(cancel)?; + let flags = bytes::u16_le(descriptor, 0x12); + if geometry.uninit_trusted && flags & BLOCK_UNINIT != 0 { + // Nothing was ever stored in this group, so there is nothing to recover. + continue; + } + let high = if geometry.is_64bit { + u64::from(bytes::u32_le(descriptor, 0x20)) + } else { + 0 + }; + let bitmap_block = (high << 32) | u64::from(bytes::u32_le(descriptor, 0)); + if bitmap_block < geometry.first_data_block || bitmap_block >= geometry.blocks { + return Err(invalid("Invalid ext block bitmap location")); + } + metadata.push(bitmap_block); + let first_block = product(group, geometry.blocks_per_group)? + .checked_add(geometry.first_data_block) + .ok_or_else(|| invalid("Invalid ext volume geometry"))?; + let count = geometry + .blocks + .saturating_sub(first_block) + .min(geometry.blocks_per_group); + let bitmap = read_at( + input, + product(bitmap_block, geometry.block_size)?, + bytes::to_usize(count.div_ceil(8)), + total, + cancel, + )?; + add_free_blocks(&mut plan, &bitmap, first_block, count, geometry.block_size)?; + } + for block in metadata { + if is_free(&plan, block, geometry.block_size) { + return Err(invalid("ext metadata is incorrectly marked unused")); + } + } + if u32_field(superblock, 0x5c) & COMPAT_HAS_JOURNAL != 0 { + let revision = u32_field(superblock, 0x4c); + let inode_size = if revision == 0 { + 128 + } else { + u64::from(bytes::u16_le(superblock, 0x58)) + }; + let inodes_per_group = u64::from(u32_field(superblock, 0x28)); + if inode_size.is_power_of_two() + && (128..=geometry.block_size).contains(&inode_size) + && inodes_per_group > 0 + { + let volume = super::journal::Volume { + block_size: geometry.block_size, + blocks: geometry.blocks, + inodes_per_group, + inode_size, + journal_inode: u64::from(u32_field(superblock, 0xe0)), + descriptors: &table, + descriptor_size, + is_64bit: geometry.is_64bit, + }; + plan.files = super::files_or_none( + super::journal::deleted_files(input, &volume, &plan, total, cancel), + "journal", + cancel, + )?; + } + } + Ok(plan) +} diff --git a/src/allocation/fat.rs b/src/allocation/fat.rs index dba78b6..1547e91 100644 --- a/src/allocation/fat.rs +++ b/src/allocation/fat.rs @@ -1,3 +1,4 @@ +use super::directory; use super::*; use crate::bytes::to_usize; use std::collections::HashSet; @@ -158,9 +159,188 @@ pub(super) fn classic( plan.add(first * sector + (cluster - 2) * cluster_size, cluster_size)?; } } + let heap = directory::Heap { + start: first * sector, + cluster_size, + last: clusters + 1, + }; + let root_directory = if bits == 32 { + Root::Chain(root) + } else { + Root::Fixed((reserved + fats * fat_sectors) * sector, roots * 32) + }; + let Some(table) = tables.first_mut() else { + return Ok(plan); + }; + plan.files = super::files_or_none( + classic_files( + input, + table, + bits, + root_directory, + heap, + &plan, + total, + cancel, + ), + "FAT", + cancel, + )?; Ok(plan) } +/// Where a FAT volume keeps its root directory. +enum Root { + /// FAT12/16: a fixed region (byte offset, length) before the data heap. + Fixed(u64, u64), + /// FAT32: an ordinary cluster chain. + Chain(u64), +} + +/// Follows a cluster chain from `start`, bounded and loop-safe. Returns the +/// clusters in order, or `None` for a broken chain. +fn chain( + input: &mut (impl Read + Seek), + table: &mut Table, + start: u64, + bits: u32, + heap: directory::Heap, + total: u64, + cancel: &AtomicBool, +) -> io::Result>> { + let end_of_chain = match bits { + 12 => 0xff8, + 16 => 0xfff8, + 32 => 0x0fff_fff8, + _ => 0xffff_fff8, + }; + let mut clusters = Vec::new(); + let mut seen = HashSet::new(); + let mut current = start; + loop { + if current < 2 || current > heap.last || !seen.insert(current) { + return Ok(None); + } + clusters.push(current); + if clusters.len() >= directory::MAX_CHAIN { + return Ok(None); + } + if clusters.len().is_multiple_of(4096) { + check(cancel)?; + } + let next = table.entry(input, current, bits, total, cancel)?; + if next >= end_of_chain { + return Ok(Some(clusters)); + } + current = next; + } +} + +/// Reads whole clusters of directory data within the walk budget. +fn read_clusters( + input: &mut (impl Read + Seek), + clusters: &[u64], + heap: directory::Heap, + budget: &mut directory::Budget, + total: u64, + cancel: &AtomicBool, +) -> io::Result>> { + let Some(length) = (clusters.len() as u64).checked_mul(heap.cluster_size) else { + return Ok(None); + }; + if !budget.take(length) || clusters.iter().any(|c| !budget.visited.insert(*c)) { + return Ok(None); + } + let mut data = Vec::new(); + for (start, length) in heap.extents(clusters).unwrap_or_default() { + data.extend(read_at(input, start, to_usize(length), total, cancel)?); + } + Ok(Some(data)) +} + +/// The clusters a deleted file stored in one piece from `start` would use, +/// if all of them are still unused. +fn free_run(plan: &Plan, heap: directory::Heap, start: u64, size: u64) -> Option> { + let count = heap.cluster_count(size)?; + let end = start.checked_add(count)?; + if end.checked_sub(1)? > heap.last { + return None; + } + let clusters: Vec = (start..end).collect(); + let extents = heap.extents(&clusters)?; + extents + .iter() + .all(|(at, length)| plan.is_unused(*at, *length)) + .then_some(clusters) +} + +/// Deleted files named in FAT12/16/32 directories, assumed stored in one +/// piece: the FAT chain of a deleted file is cleared, so its order is lost. +#[expect( + clippy::too_many_arguments, + reason = "volume geometry passed once, read-only" +)] +fn classic_files( + input: &mut (impl Read + Seek), + table: &mut Table, + bits: u32, + root: Root, + heap: directory::Heap, + plan: &Plan, + total: u64, + cancel: &AtomicBool, +) -> io::Result> { + let mut budget = directory::Budget::new(); + let mut files = Vec::new(); + let mut pending: Vec<(Option, usize)> = vec![(None, 0)]; + while let Some((start, depth)) = pending.pop() { + check(cancel)?; + let data = match (start.as_ref(), &root) { + (None, Root::Fixed(at, length)) => { + if !budget.take(*length) { + break; + } + read_at(input, *at, to_usize(*length), total, cancel)? + } + (None, Root::Chain(cluster)) | (Some(cluster), _) => { + let cluster = *cluster; + let Some(clusters) = chain(input, table, cluster, bits, heap, total, cancel)? + else { + continue; + }; + match read_clusters(input, &clusters, heap, &mut budget, total, cancel)? { + Some(data) => data, + None => continue, + } + } + }; + for entry in directory::fat_entries(&data, bits == 32) { + match entry { + directory::FatEntry::Directory(cluster) => { + if depth < directory::MAX_DEPTH { + pending.push((Some(cluster), depth + 1)); + } + } + directory::FatEntry::Deleted(name, size, cluster) => { + let Some(clusters) = free_run(plan, heap, cluster, size) else { + continue; + }; + if let Some(found) = heap + .extents(&clusters) + .and_then(|extents| directory::found(name, size, extents)) + { + files.push(found); + } + if files.len() >= directory::MAX_FILES { + return Ok(files); + } + } + } + } + } + Ok(files) +} + pub(super) fn exfat( input: &mut (impl Read + Seek), b: &[u8], @@ -307,12 +487,100 @@ pub(super) fn exfat( return Err(invalid("exFAT metadata is incorrectly marked unused")); } } - from_bitmap( + let mut plan = from_bitmap( &bytes, clusters, heap * sector, cluster_size, + BitOrder::LeastSignificantFirst, "exFAT", cancel, - ) + )?; + let heap = directory::Heap { + start: heap * sector, + cluster_size, + last: clusters + 1, + }; + plan.files = super::files_or_none( + exfat_files(input, &mut table, root, heap, &plan, total, cancel), + "exFAT", + cancel, + )?; + Ok(plan) +} + +/// Deleted files named in exFAT directories. Files flagged as stored in one +/// piece come back exactly; others follow their FAT chain when it is intact +/// and unused, and otherwise are assumed contiguous. +fn exfat_files( + input: &mut (impl Read + Seek), + table: &mut Table, + root: u64, + heap: directory::Heap, + plan: &Plan, + total: u64, + cancel: &AtomicBool, +) -> io::Result> { + let mut budget = directory::Budget::new(); + let mut files = Vec::new(); + // (first cluster, length or None for a chained root, contiguous, depth) + let mut pending: Vec<(u64, Option, bool, usize)> = vec![(root, None, false, 0)]; + while let Some((start, length, contiguous, depth)) = pending.pop() { + check(cancel)?; + let clusters = if contiguous { + let Some(count) = length.and_then(|length| heap.cluster_count(length)) else { + continue; + }; + let Some(end) = start.checked_add(count) else { + continue; + }; + if end.saturating_sub(1) > heap.last || count > directory::MAX_CHAIN as u64 { + continue; + } + (start..end).collect() + } else { + match chain(input, table, start, 64, heap, total, cancel)? { + Some(clusters) => clusters, + None => continue, + } + }; + let Some(data) = read_clusters(input, &clusters, heap, &mut budget, total, cancel)? else { + continue; + }; + for entry in directory::exfat_entries(&data) { + match entry { + directory::ExfatEntry::Directory(cluster, length, contiguous) => { + if depth < directory::MAX_DEPTH { + pending.push((cluster, Some(length), contiguous, depth + 1)); + } + } + directory::ExfatEntry::Deleted(name, size, cluster, contiguous) => { + let mut clusters = None; + if !contiguous + && let Some(found) = chain(input, table, cluster, 64, heap, total, cancel)? + && heap.cluster_count(size) == Some(found.len() as u64) + && heap.extents(&found).is_some_and(|extents| { + extents.iter().all(|(at, len)| plan.is_unused(*at, *len)) + }) + { + clusters = Some(found); + } + let Some(clusters) = clusters.or_else(|| free_run(plan, heap, cluster, size)) + else { + continue; + }; + if let Some(found) = heap + .extents(&clusters) + .and_then(|extents| directory::found(name, size, extents)) + { + files.push(found); + } + if files.len() >= directory::MAX_FILES { + return Ok(files); + } + } + } + } + } + Ok(files) } diff --git a/src/allocation/hfs.rs b/src/allocation/hfs.rs new file mode 100644 index 0000000..539e1da --- /dev/null +++ b/src/allocation/hfs.rs @@ -0,0 +1,155 @@ +//! HFS+ and HFSX free-block maps, read from the volume's allocation file. +//! +//! The allocation file is a bitmap with one bit per allocation block, most +//! significant bit first. Only the eight extents in the volume header are +//! followed; an allocation file that spills into the extents overflow file is +//! rejected rather than read partially. +// Hardened module: no unchecked indexing or arithmetic in shipped code (CWE-125, CWE-190). +#![cfg_attr( + not(test), + deny(clippy::indexing_slicing, clippy::arithmetic_side_effects) +)] + +use std::io::{self, Read, Seek}; +use std::sync::atomic::AtomicBool; + +use crate::bytes; + +use super::{BitOrder, MAX_MAP, Plan, check, from_bitmap, invalid, product, read_at}; + +/// Byte offset of the volume header from the start of the volume. +pub(super) const HEADER: usize = 1024; +/// Last byte of the volume header. +const HEADER_END: u64 = HEADER as u64 + 511; +pub(super) const SIGNATURE_PLUS: &[u8; 2] = b"H+"; +pub(super) const SIGNATURE_X: &[u8; 2] = b"HX"; +/// `HFSPlusVolumeHeader.allocationFile`, relative to the header. +const ALLOCATION_FORK: usize = 112; +const EXTENT_COUNT: usize = 8; +/// Largest allocation file read, with room for clump rounding past the bitmap. +const MAX_FORK: u64 = 2 * MAX_MAP as u64; + +fn overflow_extents() -> io::Error { + io::Error::new( + io::ErrorKind::Unsupported, + "This HFS+ volume's free-space map is too fragmented for Reclaim to read yet, so it was not scanned.", + ) +} + +fn u32_field(header: &[u8], at: usize) -> u64 { + u64::from(bytes::u32_be(header, at)) +} + +/// Reads bit `block` of an HFS+ bitmap; a block past the end counts as used. +fn is_used(bitmap: &[u8], block: u64) -> bool { + let Ok(index) = usize::try_from(block / 8) else { + return true; + }; + bitmap + .get(index) + .is_none_or(|byte| byte & (0x80 >> (block % 8)) != 0) +} + +/// Builds the free-block map of an HFS+ or HFSX volume. +/// +/// `head` holds at least the first 2048 bytes of the volume. +pub(super) fn plan( + input: &mut (impl Read + Seek), + head: &[u8], + total: u64, + cancel: &AtomicBool, +) -> io::Result { + let header = head + .get(HEADER..HEADER.saturating_add(512)) + .ok_or_else(|| invalid("HFS+ volume header is truncated"))?; + let (name, version) = match header.get(..2) { + Some(signature) if signature == SIGNATURE_PLUS => ("HFS+", 4), + Some(signature) if signature == SIGNATURE_X => ("HFSX", 5), + _ => return Err(invalid("Invalid HFS+ volume header")), + }; + let block_size = u32_field(header, 40); + let blocks = u32_field(header, 44); + if bytes::u16_be(header, 2) != version + || !block_size.is_power_of_two() + || block_size < 512 + || blocks == 0 + || product(blocks, block_size)? > total + || blocks.div_ceil(8) > MAX_MAP as u64 + { + return Err(invalid("Invalid HFS+ volume geometry")); + } + let fork = header + .get(ALLOCATION_FORK..ALLOCATION_FORK.saturating_add(80)) + .ok_or_else(|| invalid("HFS+ volume header is truncated"))?; + let logical_size = bytes::u64_be(fork, 0); + let fork_blocks = u32_field(fork, 12); + let needed = blocks.div_ceil(8); + if logical_size < needed || product(fork_blocks, block_size)? < needed { + return Err(invalid("Invalid HFS+ allocation file size")); + } + let mut extents = Vec::with_capacity(EXTENT_COUNT); + let mut listed = 0u64; + for extent in fork.get(16..80).unwrap_or_default().as_chunks::<8>().0 { + let start = u32_field(extent, 0); + let count = u32_field(extent, 4); + if count == 0 { + break; + } + if start.checked_add(count).is_none_or(|end| end > blocks) { + return Err(invalid("Invalid HFS+ allocation file extent")); + } + listed = listed.saturating_add(count); + extents.push((start, count)); + } + if listed < fork_blocks { + return Err(overflow_extents()); + } + // Bounds the per-block metadata check below (CWE-400). + if listed != fork_blocks || product(fork_blocks, block_size)? > MAX_FORK { + return Err(invalid("Invalid HFS+ allocation file size")); + } + // audit-allow(DOS-ALLOC): needed is checked against MAX_MAP (64 MiB) above. + let mut bitmap = Vec::with_capacity(bytes::to_usize(needed)); + for (start, count) in &extents { + check(cancel)?; + let remaining = needed.saturating_sub(bitmap.len() as u64); + if remaining == 0 { + break; + } + let length = product(*count, block_size)?.min(remaining); + bitmap.extend(read_at( + input, + product(*start, block_size)?, + bytes::to_usize(length), + total, + cancel, + )?); + } + if (bitmap.len() as u64) < needed { + return Err(invalid("HFS+ allocation file is truncated")); + } + // The volume header, its backup near the end, and the allocation file + // itself are always allocated on a healthy volume. + let volume_end = product(blocks, block_size)?; + let header_blocks = [HEADER as u64, HEADER_END, volume_end.saturating_sub(1024)] + .map(|at| at.checked_div(block_size).unwrap_or(u64::MAX)); + let fork_blocks = extents + .iter() + .flat_map(|(start, count)| *start..start.saturating_add(*count)); + if header_blocks + .into_iter() + .chain(fork_blocks) + .any(|block| !is_used(&bitmap, block)) + { + return Err(invalid("HFS+ metadata is incorrectly marked unused")); + } + from_bitmap( + &bitmap, + blocks, + 0, + block_size, + BitOrder::MostSignificantFirst, + name, + cancel, + ) +} diff --git a/src/allocation/indirect.rs b/src/allocation/indirect.rs new file mode 100644 index 0000000..71ac279 --- /dev/null +++ b/src/allocation/indirect.rs @@ -0,0 +1,415 @@ +//! Deleted ext2/ext3 files put back in order from their leftover block maps. +//! +//! ext2 and ext3 list a file's blocks past the first 12 in separate indirect +//! blocks. On delete Linux clears the list in the inode, but the freed +//! indirect blocks usually keep their contents. A free block holding a list of +//! free block numbers is taken as such a map, and the 12 free blocks before +//! the first listed block as the file's start when they open with a known +//! file signature. Maps one level up (double indirect) continue a file past +//! the first map. +//! +//! Nothing is reported from the maps directly: the carving stream is +//! reordered so each file's pieces follow one another, and carving still +//! checks every file. A wrong guess only changes the order in which unrelated +//! free space is read. +// Hardened module: no unchecked indexing or arithmetic in shipped code (CWE-125, CWE-190). +#![cfg_attr( + not(test), + deny(clippy::indexing_slicing, clippy::arithmetic_side_effects) +)] + +use std::collections::{BTreeMap, HashMap, HashSet}; +use std::io::{self, Read, Seek, SeekFrom}; +use std::sync::atomic::AtomicBool; + +use crate::bytes; + +use super::journal::push_block; +use super::{Plan, check}; + +/// Blocks an ext2/ext3 inode addresses directly, before the first map. +const DIRECT: u64 = 12; +/// Most block maps kept from one volume. +const MAX_MAPS: usize = 200_000; +/// Most block runs kept across all maps (16 bytes each). +const MAX_RUNS: usize = 2_000_000; +/// Free space read per request while looking for maps. +const CHUNK: u64 = 1 << 20; + +/// Where an ext2/ext3 volume's blocks are, for reading block maps. +#[derive(Clone, Copy, Debug)] +pub(crate) struct Geometry { + pub block_size: u64, + pub blocks: u64, +} + +/// The blocks a free block lists, as `(first block, count)` runs, when it +/// reads as a block map: a run of nonzero entries, then only zeros, every +/// entry a free block of the volume. +fn parse_map(data: &[u8], geometry: Geometry, plan: &Plan) -> Option> { + let entries = data.as_chunks::<4>().0; + let used = entries + .iter() + .position(|entry| *entry == [0; 4]) + .unwrap_or(entries.len()); + let (list, rest) = entries.split_at_checked(used)?; + if list.is_empty() || rest.iter().any(|entry| *entry != [0; 4]) { + return None; + } + let mut runs = Vec::new(); + for entry in list { + let block = u64::from(u32::from_le_bytes(*entry)); + let at = block.checked_mul(geometry.block_size)?; + if block >= geometry.blocks || !plan.is_unused(at, geometry.block_size) { + return None; + } + push_block(&mut runs, block); + } + Some(runs) +} + +/// Every block map in the free space of `plan`, by the block it was found in. +fn find_maps( + input: &mut (impl Read + Seek), + plan: &Plan, + geometry: Geometry, + cancel: &AtomicBool, + progress: &mut impl FnMut(u64), +) -> io::Result>> { + let block_size = geometry.block_size; + let chunk = CHUNK + .checked_div(block_size) + .unwrap_or(1) + .max(1) + .saturating_mul(block_size); + let mut maps = HashMap::new(); + let mut runs = 0usize; + let mut done = 0u64; + let mut buffer = Vec::new(); + for &(start, length) in &plan.ranges { + let first = start.div_ceil(block_size); + let end = start + .saturating_add(length) + .checked_div(block_size) + .unwrap_or(0); + let mut block = first; + while block < end { + check(cancel)?; + let count = end + .saturating_sub(block) + .min(chunk.checked_div(block_size).unwrap_or(1)); + let size = count.saturating_mul(block_size); + buffer.resize(bytes::to_usize(size), 0); + let read = input + .seek(SeekFrom::Start(block.saturating_mul(block_size))) + .and_then(|_| input.read_exact(&mut buffer)); + // An unreadable stretch is reported by carving later; skip it here. + if read.is_ok() { + for (number, data) in + (block..).zip(buffer.chunks_exact(bytes::to_usize(block_size))) + { + if maps.len() >= MAX_MAPS || runs >= MAX_RUNS { + break; + } + if let Some(list) = parse_map(data, geometry, plan) { + runs = runs.saturating_add(list.len()); + maps.insert(number, list); + } + } + } + block = block.saturating_add(count); + done = done.saturating_add(size); + progress(done); + } + } + Ok(maps) +} + +fn blocks_of(runs: &[(u64, u64)]) -> impl Iterator + '_ { + runs.iter() + .flat_map(|(start, count)| *start..start.saturating_add(*count)) +} + +/// Deleted files rebuilt from leftover block maps, each as physical +/// `(offset, length)` ranges in file order. Every range lies in the free +/// space of `plan`, and no two files share a range. +pub(super) fn chains( + input: &mut (impl Read + Seek), + plan: &Plan, + geometry: Geometry, + cancel: &AtomicBool, + mut progress: impl FnMut(u64), +) -> io::Result>> { + let maps = find_maps(input, plan, geometry, cancel, &mut progress)?; + let block_size = geometry.block_size; + let per_map = block_size.checked_div(4).unwrap_or(0); + // A map that lists only other maps is one level up (double indirect). + let mut parents: HashMap = HashMap::new(); + let mut children: HashSet = HashSet::new(); + for (&block, runs) in &maps { + if blocks_of(runs).all(|child| maps.contains_key(&child)) { + let first_data = blocks_of(runs) + .next() + .and_then(|child| maps.get(&child)) + .and_then(|child| child.first()) + .map(|(start, _)| *start); + if let Some(first_data) = first_data { + parents.insert(first_data, block); + children.extend(blocks_of(runs)); + } + } + } + let parent_blocks: HashSet = parents.values().copied().collect(); + let mut numbers: Vec = maps + .keys() + .copied() + .filter(|block| !parent_blocks.contains(block) && !children.contains(block)) + .collect(); + numbers.sort_unstable(); + let mut files: Vec> = Vec::new(); + let mut signature = vec![0u8; 64.min(bytes::to_usize(block_size))]; + for number in numbers { + check(cancel)?; + let Some(runs) = maps.get(&number) else { + continue; + }; + let Some(&(first, _)) = runs.first() else { + continue; + }; + let Some(head) = first.checked_sub(DIRECT).filter(|head| *head > 0) else { + continue; + }; + let (Some(head_at), Some(head_length)) = + (head.checked_mul(block_size), DIRECT.checked_mul(block_size)) + else { + continue; + }; + if !plan.is_unused(head_at, head_length) { + continue; + } + let opened = input + .seek(SeekFrom::Start(head_at)) + .and_then(|_| input.read_exact(&mut signature)); + if opened.is_err() || crate::sniff_kind(&signature).is_none() { + continue; + } + let mut file = vec![(head, DIRECT)]; + let mut listed = 0u64; + for block in blocks_of(runs) { + push_block(&mut file, block); + listed = listed.saturating_add(1); + } + if listed == per_map { + let last = blocks_of(runs).last().unwrap_or(0); + // The double-indirect map, if written in line, sits between the pieces. + let parent = (1..=3u64) + .filter_map(|gap| parents.get(&last.saturating_add(gap))) + .next(); + if let Some(child_runs) = parent.and_then(|parent| maps.get(parent)) { + for child in blocks_of(child_runs) { + for block in maps + .get(&child) + .map(Vec::as_slice) + .map(blocks_of) + .into_iter() + .flatten() + { + push_block(&mut file, block); + } + } + } + } + let ranges: Option> = file + .iter() + .map(|(start, count)| { + Some(( + start.checked_mul(block_size)?, + count.checked_mul(block_size)?, + )) + }) + .collect(); + if let Some(ranges) = ranges + && ranges + .iter() + .all(|(at, length)| plan.is_unused(*at, *length)) + { + files.push(ranges); + } + } + Ok(without_overlaps(files)) +} + +/// Whether two of a file's own ranges share any byte. +fn overlaps_itself(file: &[(u64, u64)]) -> bool { + let mut ranges = file.to_vec(); + ranges.sort_unstable(); + ranges + .windows(2) + .any(|pair| matches!(pair, [(at, size), (next, _)] if at.saturating_add(*size) > *next)) +} + +/// Keeps files that share no space. Where maps disagree, the one listing +/// more blocks wins: the kernel can leave an older, shorter copy of a map +/// that lists only the start of the same file. +fn without_overlaps(mut files: Vec>) -> Vec> { + let length = |file: &Vec<(u64, u64)>| -> u64 { file.iter().map(|(_, size)| *size).sum() }; + files.sort_by_key(|file| std::cmp::Reverse(length(file))); + // Accepted space: range start to range end. + let mut claimed: BTreeMap = BTreeMap::new(); + let mut kept = Vec::new(); + for file in files { + // A damaged or hostile map can list the same block twice; that file + // would read it twice, so it is not a file. + if overlaps_itself(&file) { + continue; + } + let clear = file.iter().all(|&(at, size)| { + let end = at.saturating_add(size); + claimed + .range(..end) + .next_back() + .is_none_or(|(_, &claimed_end)| claimed_end <= at) + }); + if clear { + for &(at, size) in &file { + claimed.insert(at, at.saturating_add(size)); + } + kept.push(file); + } + } + kept +} + +/// The carving order: free space in disk order, except that each rebuilt +/// file is read whole where its first piece lies. `ranges` is sorted; every +/// file range lies inside it and no two files overlap. +pub(super) fn ordered(ranges: &[(u64, u64)], files: &[Vec<(u64, u64)>]) -> Vec<(u64, u64)> { + let mut claimed: Vec<(u64, u64)> = files.iter().flatten().copied().collect(); + claimed.sort_unstable(); + let rest = super::subtract(ranges, &claimed); + let mut items: Vec<(u64, &[(u64, u64)])> = rest + .iter() + .map(|piece| (piece.0, std::slice::from_ref(piece))) + .chain( + files + .iter() + .filter_map(|file| Some((file.first()?.0, file.as_slice()))), + ) + .collect(); + items.sort_unstable_by_key(|(key, _)| *key); + let mut order: Vec<(u64, u64)> = Vec::with_capacity(rest.len().saturating_add(claimed.len())); + for (at, length) in items + .into_iter() + .flat_map(|(_, pieces)| pieces.iter().copied()) + { + match order.last_mut() { + Some((start, size)) if start.checked_add(*size) == Some(at) => { + *size = size.saturating_add(length); + } + _ => order.push((at, length)), + } + } + order +} + +#[cfg(test)] +mod tests { + use super::{Geometry, Plan, chains, ordered, parse_map, without_overlaps}; + use std::io::Cursor; + use std::sync::atomic::AtomicBool; + + const GEOMETRY: Geometry = Geometry { + block_size: 1024, + blocks: 64, + }; + + fn plan(free: &[(u64, u64)]) -> Plan { + let mut plan = Plan::new("ext2"); + for &(block, count) in free { + plan.add(block * 1024, count * 1024).unwrap(); + } + plan + } + + fn map(entries: &[u32]) -> Vec { + let mut block = vec![0u8; 1024]; + for (slot, entry) in block.as_chunks_mut::<4>().0.iter_mut().zip(entries) { + slot.copy_from_slice(&entry.to_le_bytes()); + } + block + } + + #[test] + fn a_map_lists_free_blocks_then_zeros() { + let free = plan(&[(10, 40)]); + assert_eq!( + parse_map(&map(&[20, 21, 22, 30]), GEOMETRY, &free), + Some(vec![(20, 3), (30, 1)]) + ); + // Empty, a gap of zeros inside the list, used or out-of-range blocks. + assert_eq!(parse_map(&map(&[]), GEOMETRY, &free), None); + let mut gap = map(&[20, 21]); + gap[12..16].copy_from_slice(&22u32.to_le_bytes()); + assert_eq!(parse_map(&gap, GEOMETRY, &free), None); + assert_eq!(parse_map(&map(&[20, 5]), GEOMETRY, &free), None); + assert_eq!(parse_map(&map(&[20, 64]), GEOMETRY, &free), None); + assert_eq!(parse_map(&map(&[u32::MAX]), GEOMETRY, &free), None); + assert_eq!(parse_map(&[0xff; 7], GEOMETRY, &free), None); + } + + #[test] + fn hostile_free_space_never_panics() { + // Every free block holds a map pointing at every other free block. + let mut image = vec![0u8; 64 * 1024]; + let entries: Vec = (1..64).collect(); + for block in image.as_chunks_mut::<1024>().0 { + block.copy_from_slice(&map(&entries)); + } + let free = plan(&[(1, 63)]); + let files = chains( + &mut Cursor::new(image), + &free, + GEOMETRY, + &AtomicBool::new(false), + |_| {}, + ) + .unwrap(); + assert!(files.is_empty()); + } + + #[test] + fn the_longer_of_two_overlapping_files_is_kept() { + let short = vec![(0, 10)]; + let long = vec![(0, 10), (50, 10)]; + let apart = vec![(100, 5)]; + let kept = without_overlaps(vec![short, long.clone(), apart.clone()]); + assert_eq!(kept, vec![long, apart]); + } + + #[test] + fn a_map_listing_the_same_blocks_twice_is_not_a_file() { + // [20, 8] with a signature at block 8: (8, 13) then (8, 1). + let twice = vec![(8, 13), (8, 1)]; + let fine = vec![(100, 5), (90, 5)]; + let kept = without_overlaps(vec![twice, fine.clone()]); + assert_eq!(kept, vec![fine]); + } + + #[test] + fn each_rebuilt_file_is_read_whole_where_it_starts() { + let ranges = [(0, 100), (200, 100)]; + let files = [vec![(10, 10), (250, 10), (40, 10)]]; + assert_eq!( + ordered(&ranges, &files), + vec![ + (0, 20), + (250, 10), + (40, 10), + (20, 20), + (50, 50), + (200, 50), + (260, 40) + ] + ); + } +} diff --git a/src/allocation/journal.rs b/src/allocation/journal.rs new file mode 100644 index 0000000..729af24 --- /dev/null +++ b/src/allocation/journal.rs @@ -0,0 +1,541 @@ +//! Deleted ext3/ext4 files rebuilt from inode copies in the journal. +//! +//! On delete, Linux clears a file's block list from its inode and erases its +//! directory entry. The journal (JBD2) often still holds an older copy of the +//! inode-table block, from when the file existed, with the full block list and +//! size, and older copies of directory blocks with its name. A copy is used +//! only for an inode that is now free and whose blocks are all still unused, +//! so the data cannot belong to a newer file. +//! +//! Every read is bounded: the journal length, the number of logged blocks, +//! block-map depth and the number of files reported. +// Hardened module: no unchecked indexing or arithmetic in shipped code (CWE-125, CWE-190). +#![cfg_attr( + not(test), + deny(clippy::indexing_slicing, clippy::arithmetic_side_effects) +)] + +use std::collections::HashMap; +use std::io::{self, Read, Seek}; +use std::sync::atomic::AtomicBool; + +use crate::bytes; + +use super::{Plan, check, directory, read_at}; + +const JOURNAL_MAGIC: u32 = 0xc03b_3998; +const DESCRIPTOR: u32 = 1; +const SUPERBLOCK_V1: u32 = 3; +const SUPERBLOCK_V2: u32 = 4; +const FEATURE_64BIT: u32 = 0x2; +const FEATURE_CSUM_V2: u32 = 0x8; +const FEATURE_CSUM_V3: u32 = 0x10; +const TAG_ESCAPED: u32 = 0x1; +const TAG_SAME_UUID: u32 = 0x2; +const TAG_LAST: u32 = 0x8; +const EXTENT_MAGIC: u16 = 0xf30a; +const INODE_FLAG_EXTENTS: u32 = 0x8_0000; +const MODE_TYPE: u16 = 0xf000; +const MODE_FILE: u16 = 0x8000; +/// Most journal blocks walked (a 1 GiB journal at 4 KiB blocks). +const MAX_JOURNAL_BLOCKS: u64 = 1 << 18; +/// Deepest extent tree or indirect map followed. +const MAX_DEPTH: u32 = 5; +/// Most block ranges accepted for one file. +const MAX_EXTENTS: usize = 4096; +/// Most inode bytes kept from journal copies while collecting (64 MiB). +const MAX_COPY_BYTES: u64 = 64 << 20; +/// Most file names kept from journal directory copies. +const MAX_NAMES: usize = 100_000; + +/// What the journal reader needs from the ext superblock and group table. +pub(super) struct Volume<'a> { + pub block_size: u64, + pub blocks: u64, + pub inodes_per_group: u64, + pub inode_size: u64, + pub journal_inode: u64, + pub descriptors: &'a [u8], + pub descriptor_size: usize, + pub is_64bit: bool, +} +impl Volume<'_> { + fn offset(&self, block: u64) -> Option { + block.checked_mul(self.block_size) + } + /// Block number holding inode `number`, and the inode's byte offset in it. + fn inode_place(&self, number: u64) -> Option<(u64, u64)> { + let index = number.checked_sub(1)?; + let group = index.checked_div(self.inodes_per_group)?; + let within = index.checked_rem(self.inodes_per_group)?; + let descriptor = self.descriptors.get( + usize::try_from(group) + .ok()? + .checked_mul(self.descriptor_size)?.., + )?; + let high = if self.is_64bit { + u64::from(bytes::u32_le(descriptor, 0x28)) + } else { + 0 + }; + let table = (high << 32) | u64::from(bytes::u32_le(descriptor, 8)); + let byte = within.checked_mul(self.inode_size)?; + let block = table.checked_add(byte.checked_div(self.block_size)?)?; + Some((block, byte.checked_rem(self.block_size)?)) + } + /// Inode numbers stored in block `block`, if it is part of an inode table. + fn inodes_in(&self, block: u64) -> Option { + let groups = self.descriptors.len().checked_div(self.descriptor_size)?; + let per_block = self.block_size.checked_div(self.inode_size)?; + let table_blocks = self + .inodes_per_group + .checked_mul(self.inode_size)? + .div_ceil(self.block_size); + for group in 0..groups { + let descriptor = self + .descriptors + .get(group.checked_mul(self.descriptor_size)?..)?; + let high = if self.is_64bit { + u64::from(bytes::u32_le(descriptor, 0x28)) + } else { + 0 + }; + let table = (high << 32) | u64::from(bytes::u32_le(descriptor, 8)); + if block >= table && block < table.saturating_add(table_blocks) { + let first = u64::try_from(group) + .ok()? + .checked_mul(self.inodes_per_group)? + .checked_add(block.checked_sub(table)?.checked_mul(per_block)?)? + .checked_add(1)?; + return Some(first); + } + } + None + } +} + +fn read_block( + input: &mut (impl Read + Seek), + volume: &Volume<'_>, + block: u64, + total: u64, + cancel: &AtomicBool, +) -> io::Result>> { + if block == 0 || block >= volume.blocks { + return Ok(None); + } + let Some(at) = volume.offset(block) else { + return Ok(None); + }; + read_at(input, at, bytes::to_usize(volume.block_size), total, cancel).map(Some) +} + +/// The data blocks of an inode as `(first block, count)` runs in file order, +/// from its extent tree (ext4) or block map (ext2/ext3). Child blocks are read +/// from the disk; `None` when any part is missing or implausible. +fn block_runs( + input: &mut (impl Read + Seek), + volume: &Volume<'_>, + inode: &[u8], + total: u64, + cancel: &AtomicBool, +) -> io::Result>> { + let map = inode.get(0x28..0x28 + 60).unwrap_or_default(); + let mut runs = Vec::new(); + let complete = if bytes::u32_le(inode, 0x20) & INODE_FLAG_EXTENTS != 0 { + extent_runs(input, volume, map, MAX_DEPTH, &mut runs, total, cancel)? + } else { + let mut ok = true; + for entry in map.get(..48).unwrap_or_default().as_chunks::<4>().0 { + push_block(&mut runs, u64::from(u32::from_le_bytes(*entry))); + } + for (slot, depth) in [(48usize, 1u32), (52, 2), (56, 3)] { + let block = u64::from(bytes::u32_le(map, slot)); + if block != 0 { + ok &= indirect_runs(input, volume, block, depth, &mut runs, total, cancel)?; + } + } + ok + }; + // Block 0 never holds file data; a run at 0 means a hole or a cleared map. + let usable = complete + && !runs.is_empty() + && runs.len() <= MAX_EXTENTS + && runs.iter().all(|(start, count)| { + *start != 0 + && start + .checked_add(*count) + .is_some_and(|end| end <= volume.blocks) + }); + Ok(usable.then_some(runs)) +} + +pub(super) fn push_block(runs: &mut Vec<(u64, u64)>, block: u64) { + if block == 0 { + return; + } + match runs.last_mut() { + Some((start, count)) if start.checked_add(*count) == Some(block) => { + *count = count.saturating_add(1); + } + _ => runs.push((block, 1)), + } +} + +fn extent_runs( + input: &mut (impl Read + Seek), + volume: &Volume<'_>, + node: &[u8], + depth_left: u32, + runs: &mut Vec<(u64, u64)>, + total: u64, + cancel: &AtomicBool, +) -> io::Result { + if bytes::u16_le(node, 0) != EXTENT_MAGIC || depth_left == 0 || runs.len() > MAX_EXTENTS { + return Ok(false); + } + let entries = usize::from(bytes::u16_le(node, 2)); + let depth = bytes::u16_le(node, 6); + if entries == 0 { + return Ok(false); + } + for index in 0..entries { + let Some(entry) = index + .checked_mul(12) + .and_then(|at| at.checked_add(12)) + .and_then(|at| node.get(at..at.checked_add(12)?)) + else { + return Ok(false); + }; + if depth == 0 { + let length = u64::from(bytes::u16_le(entry, 4)); + // Above 32768 the extent is allocated but unwritten: no data to recover. + if length == 0 || length > 32768 { + return Ok(false); + } + let start = + (u64::from(bytes::u16_le(entry, 6)) << 32) | u64::from(bytes::u32_le(entry, 8)); + match runs.last_mut() { + Some((previous, count)) if previous.checked_add(*count) == Some(start) => { + *count = count.saturating_add(length); + } + _ => runs.push((start, length)), + } + } else { + let child = + (u64::from(bytes::u16_le(entry, 8)) << 32) | u64::from(bytes::u32_le(entry, 4)); + let Some(block) = read_block(input, volume, child, total, cancel)? else { + return Ok(false); + }; + if !extent_runs( + input, + volume, + &block, + depth_left.saturating_sub(1), + runs, + total, + cancel, + )? { + return Ok(false); + } + } + } + Ok(true) +} + +fn indirect_runs( + input: &mut (impl Read + Seek), + volume: &Volume<'_>, + block: u64, + depth: u32, + runs: &mut Vec<(u64, u64)>, + total: u64, + cancel: &AtomicBool, +) -> io::Result { + if depth == 0 || runs.len() > MAX_EXTENTS { + return Ok(false); + } + let Some(data) = read_block(input, volume, block, total, cancel)? else { + return Ok(false); + }; + for entry in data.as_chunks::<4>().0 { + let child = u64::from(u32::from_le_bytes(*entry)); + if child == 0 { + break; + } + if child >= volume.blocks { + return Ok(false); + } + if depth == 1 { + push_block(runs, child); + } else if !indirect_runs( + input, + volume, + child, + depth.saturating_sub(1), + runs, + total, + cancel, + )? { + return Ok(false); + } + } + Ok(true) +} + +/// File names from a directory block copy: inode number to name. +fn directory_names(block: &[u8], names: &mut HashMap) { + let mut at = 0usize; + while let Some(entry) = block.get(at..) { + let inode = u64::from(bytes::u32_le(entry, 0)); + let record = usize::from(bytes::u16_le(entry, 4)); + let length = usize::from(entry.get(6).copied().unwrap_or(0)); + let kind = entry.get(7).copied().unwrap_or(0); + if record < 12 || !record.is_multiple_of(4) || length.saturating_add(8) > record { + return; + } + // file_type 1 is a regular file. + if inode != 0 + && kind == 1 + && (names.len() < MAX_NAMES || names.contains_key(&inode)) + && let Some(name) = entry + .get(8..length.saturating_add(8)) + .and_then(|raw| std::str::from_utf8(raw).ok()) + .and_then(|name| directory::clean_name(name.to_owned())) + { + names.insert(inode, name); + } + at = at.saturating_add(record); + } +} + +/// Size in bytes of one descriptor tag. +fn tag_size(incompat: u32) -> usize { + if incompat & FEATURE_CSUM_V3 != 0 { + 16 + } else if incompat & FEATURE_64BIT != 0 { + 12 + } else { + 8 + } +} + +/// Deleted regular files whose inode survives in the journal. +pub(super) fn deleted_files( + input: &mut (impl Read + Seek), + volume: &Volume<'_>, + plan: &Plan, + total: u64, + cancel: &AtomicBool, +) -> io::Result> { + let Some((block, offset)) = volume.inode_place(volume.journal_inode) else { + return Ok(Vec::new()); + }; + let Some(table) = read_block(input, volume, block, total, cancel)? else { + return Ok(Vec::new()); + }; + let journal_inode = table + .get(bytes::to_usize(offset)..) + .unwrap_or_default() + .get(..bytes::to_usize(volume.inode_size)) + .unwrap_or_default() + .to_vec(); + let Some(journal) = block_runs(input, volume, &journal_inode, total, cancel)? else { + return Ok(Vec::new()); + }; + let journal_blocks: Vec = journal + .iter() + .flat_map(|(start, count)| *start..start.saturating_add(*count)) + .take(bytes::to_usize(MAX_JOURNAL_BLOCKS)) + .collect(); + let Some(first) = journal_blocks.first().copied() else { + return Ok(Vec::new()); + }; + let Some(header) = read_block(input, volume, first, total, cancel)? else { + return Ok(Vec::new()); + }; + let kind = bytes::u32_be(&header, 4); + if bytes::u32_be(&header, 0) != JOURNAL_MAGIC + || !matches!(kind, SUPERBLOCK_V1 | SUPERBLOCK_V2) + || u64::from(bytes::u32_be(&header, 12)) != volume.block_size + { + return Ok(Vec::new()); + } + let incompat = if kind == SUPERBLOCK_V2 { + bytes::u32_be(&header, 0x28) + } else { + 0 + }; + let tag_size = tag_size(incompat); + let checksummed = incompat & (FEATURE_CSUM_V2 | FEATURE_CSUM_V3) != 0; + // The log is a ring from s_first to s_maxlen: a descriptor near its end + // logs its data blocks from s_first again. Block 0 is this superblock. + let end = journal_blocks + .len() + .min(bytes::to_usize(u64::from(bytes::u32_be(&header, 0x10)))); + let first_log = bytes::to_usize(u64::from(bytes::u32_be(&header, 0x14))); + if first_log == 0 || first_log >= end { + return Ok(Vec::new()); + } + let log_block = |index: usize| -> Option { + let wrapped = if index < end { + index + } else { + first_log + .checked_add(index.checked_sub(end)?) + .filter(|at| *at < end)? + }; + journal_blocks.get(wrapped).copied() + }; + let max_copies = bytes::to_usize(MAX_COPY_BYTES.checked_div(volume.inode_size).unwrap_or(0)); + // Latest journal copy of each inode that still had a block list, and names. + let mut copies: HashMap)> = HashMap::new(); + let mut names: HashMap = HashMap::new(); + let mut index = 1usize; + while index < end + && let Some(&physical) = journal_blocks.get(index) + { + if index.is_multiple_of(1024) { + check(cancel)?; + } + index = index.saturating_add(1); + let Some(descriptor) = read_block(input, volume, physical, total, cancel)? else { + continue; + }; + if bytes::u32_be(&descriptor, 0) != JOURNAL_MAGIC + || bytes::u32_be(&descriptor, 4) != DESCRIPTOR + { + continue; + } + let sequence = bytes::u32_be(&descriptor, 8); + let tail = if checksummed { 4 } else { 0 }; + let mut at = 12usize; + while let Some(tag) = descriptor.get(at..at.saturating_add(tag_size)) { + let (target, flags) = if incompat & FEATURE_CSUM_V3 != 0 { + let high = u64::from(bytes::u32_be(tag, 8)); + ( + (high << 32) | u64::from(bytes::u32_be(tag, 0)), + bytes::u32_be(tag, 4), + ) + } else { + let high = if incompat & FEATURE_64BIT != 0 { + u64::from(bytes::u32_be(tag, 8)) + } else { + 0 + }; + ( + (high << 32) | u64::from(bytes::u32_be(tag, 0)), + u32::from(bytes::u16_be(tag, 6)), + ) + }; + at = at.saturating_add(tag_size); + if flags & TAG_SAME_UUID == 0 { + at = at.saturating_add(16); + } + // The logged copy is the next journal block. + let Some(copy_at) = log_block(index) else { + break; + }; + index = index.saturating_add(1); + if let Some(mut copy) = read_block(input, volume, copy_at, total, cancel)? { + if flags & TAG_ESCAPED != 0 + && let Some(magic) = copy.get_mut(..4) + { + magic.copy_from_slice(&JOURNAL_MAGIC.to_be_bytes()); + } + if let Some(first_inode) = volume.inodes_in(target) { + let size = bytes::to_usize(volume.inode_size); + for (number, inode) in (first_inode..).zip(copy.chunks_exact(size.max(1))) { + let mode = bytes::u16_le(inode, 0); + let has_map = if bytes::u32_le(inode, 0x20) & INODE_FLAG_EXTENTS != 0 { + bytes::u16_le(inode, 0x28) == EXTENT_MAGIC + && bytes::u16_le(inode, 0x2a) != 0 + } else { + bytes::u32_le(inode, 0x28) != 0 + }; + if mode & MODE_TYPE == MODE_FILE && has_map { + let newer = copies + .get(&number) + .is_none_or(|(seen, _)| sequence.wrapping_sub(*seen) < 1 << 31); + // Bounded: new inodes stop being kept at the budget. + if newer && (copies.len() < max_copies || copies.contains_key(&number)) + { + copies.insert(number, (sequence, inode.to_vec())); + } + } + } + } else { + directory_names(©, &mut names); + } + } + if flags & TAG_LAST != 0 + || at.saturating_add(tag_size) > descriptor.len().saturating_sub(tail) + { + break; + } + } + } + let mut numbers: Vec = copies.keys().copied().collect(); + numbers.sort_unstable(); + let mut files = Vec::new(); + for number in numbers { + check(cancel)?; + let Some((_, inode)) = copies.get(&number) else { + continue; + }; + // The live inode must be free now: the file was deleted. + let Some((block, offset)) = volume.inode_place(number) else { + continue; + }; + let Some(table) = read_block(input, volume, block, total, cancel)? else { + continue; + }; + let live = table.get(bytes::to_usize(offset)..).unwrap_or_default(); + if bytes::u16_le(live, 26) != 0 { + continue; + } + let size = + u64::from(bytes::u32_le(inode, 4)) | (u64::from(bytes::u32_le(inode, 0x6c)) << 32); + if size == 0 { + continue; + } + let Some(runs) = block_runs(input, volume, inode, total, cancel)? else { + continue; + }; + let mut extents = Vec::with_capacity(runs.len()); + let mut covered = 0u64; + for (start, count) in runs { + let (Some(at), Some(length)) = + (volume.offset(start), count.checked_mul(volume.block_size)) + else { + continue; + }; + extents.push((at, length)); + covered = covered.saturating_add(length); + } + if covered < size + || !extents + .iter() + .all(|(at, length)| plan.is_unused(*at, *length)) + { + continue; + } + let name = names.get(&number).cloned(); + let Some(mut found) = directory::found( + name.clone().unwrap_or_else(|| format!("inode {number}")), + size, + extents, + ) else { + continue; + }; + if name.is_none() { + found.original_name = None; + found.kind = "bin"; + } + files.push(found); + if files.len() >= directory::MAX_FILES { + break; + } + } + Ok(files) +} diff --git a/src/allocation/ntfs.rs b/src/allocation/ntfs.rs index e8dc8dc..f98786c 100644 --- a/src/allocation/ntfs.rs +++ b/src/allocation/ntfs.rs @@ -307,5 +307,124 @@ pub(super) fn plan( if bitmap[0] & 1 == 0 { return Err(invalid("NTFS boot cluster is incorrectly marked unused")); } - from_bitmap(&bitmap, clusters, 0, cluster_size, "NTFS", cancel) + let mut plan = from_bitmap( + &bitmap, + clusters, + 0, + cluster_size, + BitOrder::LeastSignificantFirst, + "NTFS", + cancel, + )?; + let geometry = crate::ntfs::Geometry { + cluster_size, + clusters, + }; + plan.files = super::files_or_none( + deleted_files(input, &mft, record_size, geometry, &bitmap, total, cancel), + "MFT", + cancel, + )?; + Ok(plan) +} + +/// Most deleted files taken from one MFT; the scan's own limit is 100,000. +const MAX_DELETED: usize = 50_000; + +/// Physical byte position of `logical` within a nonresident stream. +fn physical(stream: &Stream, logical: u64, cluster_size: u64) -> Option { + let mut start = 0u64; + for (lcn, count) in &stream.runs { + let length = count.checked_mul(cluster_size)?; + if logical < start.checked_add(length)? { + return lcn.checked_mul(cluster_size)?.checked_add(logical - start); + } + start = start.checked_add(length)?; + } + None +} + +/// Whether every cluster of a byte range is marked free in the volume bitmap. +fn all_free(bitmap: &[u8], at: u64, length: u64, cluster_size: u64) -> bool { + let first = at / cluster_size; + let Some(last) = at.checked_add(length).map(|end| end.div_ceil(cluster_size)) else { + return false; + }; + (first..last).all(|cluster| { + usize::try_from(cluster / 8) + .ok() + .and_then(|index| bitmap.get(index)) + .is_some_and(|byte| byte & (1 << (cluster % 8)) == 0) + }) +} + +/// Deleted files whose MFT records survive and whose data clusters are all +/// still unused, so their bytes cannot have been overwritten by another file. +/// Fragmented files come back whole, with their original names. +fn deleted_files( + input: &mut (impl Read + Seek), + mft: &Stream, + record_size: u64, + geometry: crate::ntfs::Geometry, + bitmap: &[u8], + total: u64, + cancel: &AtomicBool, +) -> io::Result> { + let records = mft.initialized.min(mft.size) / record_size; + let batch = (1024 * 1024 / record_size).max(1); + let mut files = Vec::new(); + let mut index = 16; + while index < records && files.len() < MAX_DELETED { + check(cancel)?; + let count = batch.min(records - index); + let bytes = match stream_read( + input, + mft, + index * record_size, + to_usize(count * record_size), + geometry.cluster_size, + total, + cancel, + ) { + Ok(bytes) => bytes, + Err(e) if cancel.load(Ordering::Relaxed) => return Err(e), + // A bad sector in the MFT loses only the records in this batch. + Err(_) => { + index += count; + continue; + } + }; + for (number, raw) in (index..).zip(bytes.chunks_exact(to_usize(record_size))) { + // Only deleted (flags 0) file records are candidates. + if raw.get(..4) != Some(b"FILE".as_slice()) || u16at(raw, 22) != 0 { + continue; + } + let Some(mut file) = crate::ntfs::deleted_file(raw.to_vec(), geometry, cancel) else { + continue; + }; + match &file.extents { + Some(extents) => { + if !extents + .iter() + .all(|(at, length)| all_free(bitmap, *at, *length, geometry.cluster_size)) + { + continue; + } + } + None => { + let Some(at) = physical(mft, number * record_size, geometry.cluster_size) + else { + continue; + }; + file.offset = at; + } + } + files.push(file); + if files.len() >= MAX_DELETED { + break; + } + } + index += count; + } + Ok(files) } diff --git a/src/allocation/tests.rs b/src/allocation/tests.rs index b721748..4cc376d 100644 --- a/src/allocation/tests.rs +++ b/src/allocation/tests.rs @@ -229,11 +229,28 @@ fn occupied_files_are_not_read_or_found_and_free_offsets_export_correctly() { assert_eq!(done, unused); } #[test] -fn a_candidate_cannot_bridge_an_occupied_cluster() { +fn a_file_split_around_an_occupied_cluster_comes_back_without_it() { + // Free clusters 3 and 4, occupied 5, free 6: a file filled the gap in order. let (mut b, heap) = fat12(); b[heap + 512..heap + 512 + 9].copy_from_slice(b"%PDF-1.7\n"); + b[heap + 2 * 512..heap + 3 * 512].fill(b'x'); + b[heap + 3 * 512..heap + 3 * 512 + 9].copy_from_slice(b"OCCUPIED\n"); b[heap + 4 * 512..heap + 4 * 512 + 5].copy_from_slice(b"%%EOF"); - assert!(run(&b).0.is_empty()); + let (files, _, _) = run(&b); + assert_eq!(files.len(), 1); + let file = &files[0]; + assert_eq!(file.offset, (heap + 512) as u64); + assert_eq!( + file.extents.as_deref(), + Some([((heap + 512) as u64, 1024), ((heap + 4 * 512) as u64, 5)].as_slice()) + ); + let dir = tempfile::tempdir().unwrap(); + let output = dir.path().join("split.pdf"); + crate::recover_reader(&mut Cursor::new(&b), &output, file).unwrap(); + let recovered = std::fs::read(output).unwrap(); + assert!(recovered.starts_with(b"%PDF-1.7\n") && recovered.ends_with(b"%%EOF")); + assert!(!recovered.windows(8).any(|w| w == b"OCCUPIED")); + assert_eq!(recovered.len(), 1024 + 5); } #[test] fn mirrored_fat_disagreement_is_treated_as_occupied() { @@ -402,3 +419,220 @@ fn media_faults_skip_free_data_but_never_fabricate_an_allocation_map() { ); assert!(!mapped); } + +/// A 1 MiB ext2 volume with 1 KiB blocks, one group, and blocks 100..104 free. +fn ext2() -> Vec { + let mut b = vec![0; 1024 * 1024]; + let s = 1024; + put32(&mut b, s + 0x04, 1024); + put32(&mut b, s + 0x14, 1); + put32(&mut b, s + 0x20, 8192); + put16(&mut b, s + 0x38, 0xef53); + put32(&mut b, s + 0x4c, 1); + put32(&mut b, s + 0x60, 0x2); + // Group descriptor table in block 2, block bitmap in block 3. + put32(&mut b, 2 * 1024, 3); + let bitmap = 3 * 1024; + b[bitmap..bitmap + 1024].fill(0xff); + for block in 100..104 { + let bit = block - 1; + b[bitmap + bit / 8] &= !(1 << (bit % 8)); + } + b +} +/// A 1 MiB HFS+ volume with 512-byte blocks and blocks 100..104 free. +fn hfs_plus() -> Vec { + let mut b = vec![0; 1024 * 1024]; + let h = 1024; + b[h..h + 2].copy_from_slice(b"H+"); + b[h + 2..h + 4].copy_from_slice(&4u16.to_be_bytes()); + b[h + 40..h + 44].copy_from_slice(&512u32.to_be_bytes()); + b[h + 44..h + 48].copy_from_slice(&2048u32.to_be_bytes()); + b[h + 112..h + 120].copy_from_slice(&256u64.to_be_bytes()); + b[h + 124..h + 128].copy_from_slice(&1u32.to_be_bytes()); + b[h + 128..h + 132].copy_from_slice(&10u32.to_be_bytes()); + b[h + 132..h + 136].copy_from_slice(&1u32.to_be_bytes()); + let bitmap = 10 * 512; + b[bitmap..bitmap + 256].fill(0xff); + for block in 100..104 { + b[bitmap + block / 8] &= !(0x80 >> (block % 8)); + } + b +} +fn map(b: &[u8]) -> io::Result { + plan(&mut Cursor::new(b), b.len() as u64, &AtomicBool::new(false)) +} +#[test] +fn ext_block_bitmaps_limit_the_scan_to_free_blocks() { + let mut b = ext2(); + let pdf = b"%PDF-1.7\nfree ext\n%%EOF"; + b[100 * 1024..100 * 1024 + pdf.len()].copy_from_slice(pdf); + b[120 * 1024..120 * 1024 + pdf.len()].copy_from_slice(pdf); + let (files, unused, done) = run(&b); + assert_eq!((unused, done), (4096, 4096)); + assert_eq!(files.len(), 1); + assert_eq!(files[0].offset, 100 * 1024); + assert_eq!(map(&b).unwrap().filesystem, "ext2"); + put32(&mut b, 1024 + 0x5c, 0x4); + assert_eq!(map(&b).unwrap().filesystem, "ext3"); + put32(&mut b, 1024 + 0x60, 0x2 | 0x40); + assert_eq!(map(&b).unwrap().filesystem, "ext4"); +} +#[test] +fn ext_metadata_marked_free_or_unknown_features_fail_closed() { + let mut b = ext2(); + // Clear the bit for the block bitmap itself (block 3). + b[3 * 1024] &= !(1 << 2); + assert_eq!(map(&b).unwrap_err().kind(), io::ErrorKind::InvalidData); + let mut b = ext2(); + put32(&mut b, 1024 + 0x60, 0x2 | 0x10); + assert_eq!(map(&b).unwrap_err().kind(), io::ErrorKind::Unsupported); + let mut b = ext2(); + put32(&mut b, 1024 + 0x64, 0x200); + assert_eq!(map(&b).unwrap_err().kind(), io::ErrorKind::Unsupported); +} +#[test] +fn ext_uninitialized_groups_are_skipped_only_with_group_checksums() { + let mut b = ext2(); + put16(&mut b, 2 * 1024 + 0x12, 0x2); + assert_eq!(map(&b).unwrap().unused, 4096); + put32(&mut b, 1024 + 0x64, 0x10); + assert_eq!(map(&b).unwrap().unused, 0); +} +#[test] +fn hfs_plus_allocation_file_limits_the_scan_to_free_blocks() { + let mut b = hfs_plus(); + let pdf = b"%PDF-1.7\nfree HFS+\n%%EOF"; + b[100 * 512..100 * 512 + pdf.len()].copy_from_slice(pdf); + b[120 * 512..120 * 512 + pdf.len()].copy_from_slice(pdf); + let (files, unused, done) = run(&b); + assert_eq!((unused, done), (2048, 2048)); + assert_eq!(files.len(), 1); + assert_eq!(files[0].offset, 100 * 512); + b[1024..1026].copy_from_slice(b"HX"); + b[1026..1028].copy_from_slice(&5u16.to_be_bytes()); + assert_eq!(map(&b).unwrap().filesystem, "HFSX"); +} +#[test] +fn hfs_plus_damaged_or_overflowing_maps_fail_closed() { + let mut b = hfs_plus(); + // Block 10 holds the allocation file and must be marked used. + b[10 * 512 + 1] &= !0x20; + assert_eq!(map(&b).unwrap_err().kind(), io::ErrorKind::InvalidData); + let mut b = hfs_plus(); + b[1024 + 124..1024 + 128].copy_from_slice(&2u32.to_be_bytes()); + assert_eq!(map(&b).unwrap_err().kind(), io::ErrorKind::Unsupported); + let mut b = hfs_plus(); + b[1024 + 132..1024 + 136].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(map(&b).is_err()); +} +#[test] +fn refs_volumes_are_named_and_never_scanned() { + let mut b = vec![0; 64 * 1024]; + b[3..11].copy_from_slice(b"ReFS\0\0\0\0"); + b[510..512].copy_from_slice(b"\x55\xaa"); + let error = map(&b).unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::Unsupported); + assert!(error.to_string().contains("ReFS")); +} +#[test] +fn malformed_ext_and_hfs_headers_do_not_panic() { + for mut b in [ext2(), hfs_plus()] { + for at in (1024..1024 + 512).chain(2048..2048 + 64) { + for value in [0, 1, 0x80, 255] { + let old = b[at]; + b[at] = value; + let _outcome = map(&b); + b[at] = old; + } + } + } +} + +/// A deleted MFT record in the NTFS 3.0 layout Linux's ntfs3 driver writes: +/// update array at 42, attributes from 48, and no file name left behind. +fn ntfs3_deleted_record(runs: &[u8], size: u64) -> Vec { + let mut b = vec![0; 1024]; + b[..4].copy_from_slice(b"FILE"); + put16(&mut b, 4, 42); + put16(&mut b, 6, 3); + put16(&mut b, 16, 1); + put16(&mut b, 20, 48); + put16(&mut b, 22, 0); + put32(&mut b, 28, 1024); + let mut at = 48; + // $STANDARD_INFORMATION, resident, 48-byte value. + put32(&mut b, at, 0x10); + put32(&mut b, at + 4, 72); + put32(&mut b, at + 16, 48); + put16(&mut b, at + 20, 24); + at += 72; + // Unnamed nonresident $DATA. + let length = (64 + runs.len()).next_multiple_of(8); + put32(&mut b, at, 0x80); + put32(&mut b, at + 4, u32::try_from(length).unwrap()); + b[at + 8] = 1; + let clusters = size.div_ceil(4096); + put64(&mut b, at + 24, clusters - 1); + put16(&mut b, at + 32, 64); + put64(&mut b, at + 40, clusters * 4096); + put64(&mut b, at + 48, size); + put64(&mut b, at + 56, size); + b[at + 64..at + 64 + runs.len()].copy_from_slice(runs); + at += length; + put32(&mut b, at, u32::MAX); + put32(&mut b, 24, u32::try_from(at + 8).unwrap()); + put16(&mut b, 42, 0xbeef); + for (i, tail) in [510, 1022].into_iter().enumerate() { + let pair = [b[tail], b[tail + 1]]; + b[44 + i * 2..46 + i * 2].copy_from_slice(&pair); + put16(&mut b, tail, 0xbeef); + } + b +} + +#[test] +fn ntfs3_records_without_names_keep_their_fragments() { + let geometry = crate::ntfs::Geometry { + cluster_size: 4096, + clusters: 1000, + }; + // Two fragments: 2 clusters at 200, then 1 cluster at 500. + let record = ntfs3_deleted_record(&[0x21, 2, 200, 0, 0x21, 1, 44, 1, 0], 9000); + let file = crate::ntfs::deleted_file(record, geometry, &AtomicBool::new(false)).unwrap(); + assert_eq!(file.original_name, None); + assert_eq!(file.kind, "bin"); + assert_eq!(file.size, 9000); + assert_eq!( + file.extents.as_deref(), + Some([(200 * 4096, 2 * 4096), (500 * 4096, 4096)].as_slice()) + ); + // A run past the end of the volume, or a sparse run, is never guessed at. + for runs in [&[0x21, 2, 0xe8, 0x03, 0][..], &[0x01, 2, 0][..]] { + let record = ntfs3_deleted_record(runs, 8000); + assert!(crate::ntfs::deleted_file(record, geometry, &AtomicBool::new(false)).is_none()); + } +} + +#[test] +fn file_types_are_recognised_from_their_first_bytes() { + let mut mp4 = b"\0\0\0\x18ftypisom".to_vec(); + assert_eq!(crate::sniff_kind(&mp4), Some("mp4")); + mp4[8..12].copy_from_slice(b"qt "); + assert_eq!(crate::sniff_kind(&mp4), Some("mov")); + mp4[8..12].copy_from_slice(b"heic"); + assert_eq!(crate::sniff_kind(&mp4), Some("heic")); + assert_eq!( + crate::sniff_kind(b"\x1a\x45\xdf\xa3\x01\0\0\0\x42\x82\x84webm"), + Some("webm") + ); + assert_eq!( + crate::sniff_kind(b"\xff\xd8\xff\xe0\0\x10JFIF"), + Some("jpg") + ); + assert_eq!(crate::sniff_kind(b"RIFF\0\0\0\0WAVEfmt "), Some("wav")); + assert_eq!(crate::sniff_kind(b"plain text"), None); + assert!(crate::starts_like("docx", b"PK\x03\x04\x14\0")); + assert!(crate::starts_like("mov", b"\0\0\0\x18ftypisom")); + assert!(!crate::starts_like("png", b"\xff\xd8\xff\xe0")); +} diff --git a/src/drives.rs b/src/drives.rs index b706b7a..4169b3e 100644 --- a/src/drives.rs +++ b/src/drives.rs @@ -21,9 +21,16 @@ pub struct Drive { pub mounted: bool, pub encrypted: bool, pub sector_size: usize, + /// A phone or camera connected for file transfer (MTP/PTP). It is listed + /// so the user can see why it cannot be scanned; it is never opened. + pub phone: bool, } impl Drive { + /// Whether this entry can be selected and scanned. + pub fn scannable(&self) -> bool { + !self.encrypted && !self.phone + } pub fn display_name(&self) -> String { if self.name_is_message { reclaim::i18n::t(&self.name) diff --git a/src/drives/linux.rs b/src/drives/linux.rs index 59f6c27..5573934 100644 --- a/src/drives/linux.rs +++ b/src/drives/linux.rs @@ -18,13 +18,80 @@ pub fn discover() -> Result, String> { if Path::new("/.flatpak-info").exists() { return Ok(Vec::new()); } - discover_at( + let mut drives = discover_at( Path::new("/sys/class/block"), Path::new("/dev"), Path::new("/proc/self/mountinfo"), Path::new("/run/udev/data"), ) - .map_err(|error| format!("Could not list drives: {error}. You can still open a disk image.")) + .map_err(|error| format!("Could not list drives: {error}. You can still open a disk image."))?; + drives.extend(phones_at(Path::new("/sys/bus/usb/devices"))); + Ok(drives) +} + +/// Longest device-supplied name shown for a phone. +const MAX_PHONE_NAME: usize = 64; +/// Most USB entries examined, so a strange sysfs cannot stall discovery. +const MAX_USB_ENTRIES: usize = 512; + +/// Phones and cameras that offer only file transfer (MTP or PTP). They have +/// no block device, so without this they would silently be missing from the +/// list. USB storage never uses the still-image interface class (6). +fn phones_at(usb: &Path) -> Vec { + let Ok(entries) = fs::read_dir(usb) else { + return Vec::new(); + }; + let mut names: Vec = entries + .take(MAX_USB_ENTRIES) + .filter_map(Result::ok) + .map(|entry| entry.file_name().to_string_lossy().into_owned()) + .collect(); + names.sort(); + let mut phones = Vec::new(); + for device in names.iter().filter(|name| !name.contains(':')) { + let interface_prefix = format!("{device}:"); + let transfers_files = names + .iter() + .filter(|name| name.starts_with(&interface_prefix)) + .any(|interface| { + let at = usb.join(interface); + read(at.join("bInterfaceClass")) == "06" + || read(at.join("interface")).eq_ignore_ascii_case("MTP") + }); + if !transfers_files { + continue; + } + let at = usb.join(device); + let name = [read(at.join("manufacturer")), read(at.join("product"))] + .iter() + .filter(|part| !part.is_empty()) + .cloned() + .collect::>() + .join(" "); + // Device strings come from the device itself: keep them printable and short. + let name: String = name + .chars() + .filter(|c| !c.is_control()) + .take(MAX_PHONE_NAME) + .collect(); + phones.push(Drive { + path: at, + name_is_message: name.is_empty(), + name: if name.is_empty() { + "Phone or camera".into() + } else { + name + }, + size: 0, + kind: "Phone or camera".into(), + location: "File transfer only".into(), + mounted: false, + encrypted: false, + sector_size: 512, + phone: true, + }); + } + phones } /// Kernel and udev text files are small; the cap keeps a misbehaving @@ -268,6 +335,7 @@ fn discover_at(sys: &Path, dev: &Path, mounts: &Path, udev: &Path) -> std::io::R mounted: in_use, encrypted, sector_size, + phone: false, }); } // Prefer named volumes over their parent disk. The volume retains its @@ -394,6 +462,44 @@ mod tests { } } + #[test] + fn phones_and_cameras_are_listed_but_storage_and_other_devices_are_not() { + let root = tempfile::tempdir().unwrap(); + let write = |path: &str, text: &str| { + let path = root.path().join(path); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, text).unwrap(); + }; + // An Android phone (MTP over the still-image class). + write("1-2/manufacturer", "Unihertz\n"); + write("1-2/product", "F21 Pro\n"); + write("1-2:1.0/bInterfaceClass", "06\n"); + write("1-2:1.0/interface", "MTP\n"); + // A phone exposing MTP on a vendor class, with a hostile name. + write( + "1-5/product", + "Phone\u{1b}[2J with a very long name that keeps going and going and going past the limit", + ); + write("1-5:1.0/bInterfaceClass", "ff\n"); + write("1-5:1.0/interface", "mtp\n"); + // A USB stick (mass storage) and a Wi-Fi adapter. + write("1-3/product", "USB Flash Disk\n"); + write("1-3:1.0/bInterfaceClass", "08\n"); + write("1-4/product", "Wireless_Device\n"); + write("1-4:1.0/bInterfaceClass", "e0\n"); + let phones = phones_at(root.path()); + assert_eq!(phones.len(), 2); + assert_eq!(phones[0].name, "Unihertz F21 Pro"); + assert!( + phones + .iter() + .all(|p| p.phone && !p.scannable() && p.size == 0) + ); + assert!(!phones[1].name.contains('\u{1b}')); + assert!(phones[1].name.chars().count() <= MAX_PHONE_NAME); + assert!(phones_at(&root.path().join("missing")).is_empty()); + } + #[test] fn recovery_folders_on_the_scanned_physical_disk_are_detected() { let f = Fixture::new(); diff --git a/src/drives/macos.rs b/src/drives/macos.rs index 3551db3..a269a66 100644 --- a/src/drives/macos.rs +++ b/src/drives/macos.rs @@ -333,6 +333,7 @@ pub fn discover() -> Result, String> { mounted: in_use, encrypted: locked, sector_size: 1, + phone: false, }); } let active_parents: HashSet<_> = rows diff --git a/src/drives/monitor.rs b/src/drives/monitor.rs index 748f496..94be216 100644 --- a/src/drives/monitor.rs +++ b/src/drives/monitor.rs @@ -90,6 +90,7 @@ mod tests { mounted: false, encrypted: false, sector_size: 512, + phone: false, } } #[test] diff --git a/src/drives/windows.rs b/src/drives/windows.rs index 6003016..f255a78 100644 --- a/src/drives/windows.rs +++ b/src/drives/windows.rs @@ -186,6 +186,7 @@ pub fn discover() -> Result, String> { mounted: false, encrypted: false, sector_size: sector, + phone: false, }); } // SAFETY: GetLogicalDrives has no pointer arguments. @@ -275,6 +276,7 @@ pub fn discover() -> Result, String> { mounted: true, encrypted, sector_size, + phone: false, }); } if unknown_backing_disk { diff --git a/src/lib.rs b/src/lib.rs index 5cc1071..3477862 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,13 +34,13 @@ pub const FILE_CATEGORIES: &[&str] = &[ ]; pub fn category_index(kind: &str) -> usize { match kind { - "jpg" | "png" | "gif" | "bmp" | "webp" | "qoi" => 1, + "jpg" | "png" | "gif" | "bmp" | "webp" | "qoi" | "heic" => 1, "ico" => 7, "pdf" | "docx" | "xlsx" | "pptx" | "docm" | "xlsm" | "pptm" | "odt" | "ods" | "odp" | "epub" | "ole" | "txt" | "csv" | "json" | "xml" | "md" | "html" | "log" | "sqlite" => 2, "zip" | "7z" | "cab" | "rar" | "tar" | "gz" | "xar" => 3, - "wav" | "aiff" | "aifc" | "au" | "mp3" | "ogg" | "opus" | "mid" => 4, - "avi" | "mp4" | "ogv" => 5, + "wav" | "aiff" | "aifc" | "au" | "mp3" | "ogg" | "opus" | "mid" | "m4a" => 4, + "avi" | "mp4" | "ogv" | "mov" | "3gp" | "mkv" | "webm" => 5, "jar" | "apk" | "exe" | "dll" | "elf" | "msi" | "msp" | "mst" | "deb" | "rpm" | "dex" | "pkg" | "swf" => 6, _ => 0, @@ -85,6 +85,10 @@ pub struct Found { pub original_name: Option, pub resident_data: Option>, pub ntfs: Option, + /// The file's data as `(offset, length)` byte ranges of the source, in + /// order, when a filesystem record says it was stored in pieces. `offset` + /// is then the first range and `size` the exact file length. + pub extents: Option>, } impl Found { /// A portable original filename when available, otherwise a generated name. @@ -439,6 +443,13 @@ fn candidate_end(fmt: &Format, bytes: &[u8], from: usize, cancel: &AtomicBool) - continue; } let end = from + index + fmt.tail.len(); + if fmt.ext == "pdf" { + match pdf_after_eof(bytes, end, false) { + PdfAfterEof::Ends(end) => return CandidateEnd::Complete(end), + PdfAfterEof::Continues => continue, + PdfAfterEof::Undecided => return CandidateEnd::More, + } + } if fmt.ext != "zip" { return CandidateEnd::Complete(end); } @@ -458,6 +469,119 @@ fn candidate_end(fmt: &Format, bytes: &[u8], from: usize, cancel: &AtomicBool) - CandidateEnd::More } +/// What follows a PDF `%%EOF`. +#[derive(Debug, PartialEq, Eq)] +enum PdfAfterEof { + /// The file ends at this byte: nothing of the PDF follows. + Ends(usize), + /// Another revision follows; the search goes on to a later `%%EOF`. + Continues, + /// The bytes read so far stop before this can be decided. + Undecided, +} + +/// Most whitespace looked past after `%%EOF` for another revision. +const MAX_PDF_GAP: usize = 1024; + +/// PDF whitespace (ISO 32000-1, 7.2.2): NUL, tab, line feed, form feed, +/// carriage return and space. +fn pdf_space(byte: u8) -> bool { + matches!(byte, 0 | b'\t' | b'\n' | 0x0c | b'\r' | b' ') +} + +/// What follows the `%%EOF` that finishes at `eof`. +/// +/// The line break after `%%EOF` belongs to the file. Linearized ("fast web +/// view") PDFs and files saved with incremental updates hold several `%%EOF` +/// markers; stopping at the first one would cut off most of the document. +/// When `bytes` ends before the next token is whole, the answer waits for +/// more data, unless `at_end` says the source has no more. +fn pdf_after_eof(bytes: &[u8], eof: usize, at_end: bool) -> PdfAfterEof { + let mut end = eof; + for expected in *b"\r\n" { + if bytes.get(end) == Some(&expected) { + end = end.saturating_add(1); + } + } + let next = bytes.get(end..).unwrap_or_default(); + // Line whitespace only: free space after a file is often zero-filled, and + // NUL there is padding, not a gap before another revision. + let gap = next + .iter() + .take_while(|b| **b != 0 && pdf_space(**b)) + .count(); + if gap > MAX_PDF_GAP { + return PdfAfterEof::Ends(end); + } + let next = next.get(gap..).unwrap_or_default(); + let keyword = |word: &[u8]| { + if next.starts_with(word) { + Some(true) + } else if word.starts_with(next) { + None + } else { + Some(false) + } + }; + let answers = [ + keyword(b"xref"), + keyword(b"trailer"), + keyword(b"startxref"), + pdf_object_header(next), + ]; + if answers.contains(&Some(true)) { + PdfAfterEof::Continues + } else if answers.contains(&None) && !at_end { + PdfAfterEof::Undecided + } else { + PdfAfterEof::Ends(end) + } +} + +/// Whether `bytes` starts with an indirect object header such as `12 0 obj`, +/// or `None` when `bytes` ends before that is known. +fn pdf_object_header(bytes: &[u8]) -> Option { + let mut rest = bytes; + for _ in 0..2 { + let digits = rest.iter().take_while(|b| b.is_ascii_digit()).count(); + if digits == rest.len() && digits <= 10 { + return None; + } + if digits == 0 || digits > 10 { + return Some(false); + } + rest = rest.get(digits..).unwrap_or_default(); + let spaces = rest.iter().take_while(|b| pdf_space(**b)).count(); + if spaces == rest.len() { + return None; + } + if spaces == 0 { + return Some(false); + } + rest = rest.get(spaces..).unwrap_or_default(); + } + if rest.starts_with(b"obj") { + Some(true) + } else if b"obj".starts_with(rest) { + None + } else { + Some(false) + } +} + +/// Where a PDF read to the end of the source ends: after its last `%%EOF`. +fn pdf_end_at_source_end(bytes: &[u8]) -> CandidateEnd { + let mut last = None; + for (index, window) in bytes.windows(5).enumerate() { + if window == b"%%EOF" + && let PdfAfterEof::Ends(end) = pdf_after_eof(bytes, index + 5, true) + { + last = Some(end); + } + } + last.map_or(CandidateEnd::Invalid, CandidateEnd::Complete) +} + fn valid_zip(bytes: &[u8], eocd: usize) -> bool { if eocd + 22 > bytes.len() { return false; @@ -513,6 +637,58 @@ fn valid_zip(bytes: &[u8], eocd: usize) -> bool { cursor == eocd } +/// The file type a file's first bytes announce, from the carving signatures +/// plus the ISO media (MP4, MOV, HEIC) and Matroska headers. `None` when the +/// bytes carry no known signature. +pub(crate) fn sniff_kind(head: &[u8]) -> Option<&'static str> { + if head.get(4..8) == Some(b"ftyp".as_slice()) { + return Some(match head.get(8..12)? { + b"qt " => "mov", + b"heic" | b"heix" | b"heim" | b"heis" | b"mif1" | b"msf1" => "heic", + b"M4A " | b"M4B " => "m4a", + b"3gp4" | b"3gp5" | b"3gp6" | b"3g2a" => "3gp", + _ => "mp4", + }); + } + if head.starts_with(b"\x1a\x45\xdf\xa3") { + let webm = head.windows(4).any(|window| window == b"webm"); + return Some(if webm { "webm" } else { "mkv" }); + } + let fmt = FORMATS + .iter() + .find(|fmt| !fmt.head.is_empty() && head.starts_with(fmt.head))?; + Some(match fmt.ext { + "riff" => match head.get(8..12)? { + b"WEBP" => "webp", + b"AVI " => "avi", + b"WAVE" => "wav", + _ => return None, + }, + "aiff" if head.get(8..12) == Some(b"AIFC".as_slice()) => "aifc", + "zip" => "zip", + other => formats::kind(other, head), + }) +} + +/// Whether `head`, the first bytes of a file, carries a signature of the file +/// type `kind`. Unknown types and plain text never match. +pub(crate) fn starts_like(kind: &str, head: &[u8]) -> bool { + fn family(kind: &str) -> &str { + match kind { + "docx" | "xlsx" | "pptx" | "docm" | "xlsm" | "pptm" | "odt" | "ods" | "odp" + | "epub" | "jar" | "apk" => "zip", + "opus" | "ogv" => "ogg", + "dll" => "exe", + "mov" | "3gp" | "m4a" => "mp4", + "webm" => "mkv", + "aifc" => "aiff", + "msi" | "msp" | "mst" | "ole" => "cfb", + other => other, + } + } + sniff_kind(head).is_some_and(|found| family(found) == family(kind)) +} + fn candidate_kind(fmt: &Format, bytes: &[u8]) -> &'static str { match fmt.ext { "riff" => match &bytes[8..12] { @@ -792,12 +968,17 @@ pub(crate) fn scan_reader_with_filter( } let remaining = (candidate_limit - candidate.len()).min(chunk.len()); let count = read_chunk(file, &mut chunk[..remaining], cancel)?; - if count == 0 { + // A PDF whose end was waiting on more data ends at the + // source's end after its last `%%EOF`. + let at_source_end = count == 0; + if at_source_end && fmt.ext != "pdf" { break; } let from = candidate.len().saturating_sub(fmt.tail.len()); candidate.extend_from_slice(&chunk[..count]); - let boundary = if fmt.ext == "ogg" { + let boundary = if at_source_end { + pdf_end_at_source_end(&candidate) + } else if fmt.ext == "ogg" { ogg.push(&candidate, cancel) } else if fmt.ext == "gz" { gzip.push(&candidate, cancel) @@ -861,6 +1042,7 @@ pub(crate) fn scan_reader_with_filter( original_name: None, resident_data: None, ntfs: None, + extents: None, })); } } @@ -876,6 +1058,78 @@ pub(crate) fn scan_reader_with_filter( Ok(()) } +/// Reads a found file's bytes: resident data, one range, or its extents in order. +pub struct FoundReader<'a, R> { + input: &'a mut R, + resident: Option<&'a [u8]>, + extents: Vec<(u64, u64)>, + /// Index of the current extent and bytes already read from it. + current: usize, + used: u64, + remaining: u64, +} +impl<'a, R: Read + Seek> FoundReader<'a, R> { + pub fn new(input: &'a mut R, found: &'a Found) -> io::Result { + let extents = match &found.extents { + Some(extents) => { + let stored = extents + .iter() + .try_fold(0u64, |sum, (_, length)| sum.checked_add(*length)); + if stored.is_none_or(|stored| stored < found.size) { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "Candidate range is invalid", + )); + } + extents.to_vec() + } + None => vec![(found.offset, found.size)], + }; + Ok(Self { + input, + resident: found.resident_data.as_deref(), + extents, + current: 0, + used: 0, + remaining: found.size, + }) + } +} +impl Read for FoundReader<'_, R> { + fn read(&mut self, buffer: &mut [u8]) -> io::Result { + if let Some(resident) = &mut self.resident { + return resident.read(buffer); + } + while self.remaining > 0 { + let Some(&(start, length)) = self.extents.get(self.current) else { + return Ok(0); + }; + if self.used >= length { + self.current = self.current.saturating_add(1); + self.used = 0; + continue; + } + let wanted = (length - self.used).min(self.remaining); + let count = buffer.len().min(to_usize(wanted)); + let at = start + .checked_add(self.used) + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid extent"))?; + self.input.seek(SeekFrom::Start(at))?; + let Some(buffer) = buffer.get_mut(..count) else { + return Ok(0); + }; + let read = self.input.read(buffer)?; + if read == 0 { + return Ok(0); + } + self.used += read as u64; + self.remaining -= read as u64; + return Ok(read); + } + Ok(0) + } +} + /// Never overwrite an existing output; open the source only for reading. pub fn recover(source: &Path, destination: &Path, found: &Found) -> io::Result<()> { recover_reader(&mut File::open(source)?, destination, found) @@ -925,6 +1179,7 @@ pub fn recover_reader( ) -> io::Result<()> { if (found.size == 0 && found.resident_data.is_none()) || (found.size > LIMIT as u64 + && found.extents.is_none() && !(found.kind == "zip" && found.size <= u32::MAX as u64 + 65557)) || found .resident_data @@ -937,9 +1192,7 @@ pub fn recover_reader( "Candidate range is invalid", )); } - if found.resident_data.is_none() { - input.seek(SeekFrom::Start(found.offset))?; - } + // Keep incomplete or unvalidated data private. Publish without replacing an // existing entry, including a symlink, only after all checks succeed. let parent = destination @@ -947,18 +1200,24 @@ pub fn recover_reader( .filter(|p| !p.as_os_str().is_empty()) .unwrap_or_else(|| Path::new(".")); let mut output = tempfile::NamedTempFile::new_in(parent)?; - let copied = if let Some(bytes) = &found.resident_data { - io::copy(&mut bytes.as_ref(), &mut output)? - } else { - io::copy(&mut input.take(found.size), &mut output)? - }; + let copied = io::copy(&mut FoundReader::new(input, found)?, &mut output)?; if copied != found.size { return Err(io::Error::new( io::ErrorKind::UnexpectedEof, "Source changed or candidate is truncated", )); } - if found.kind == "zip" && found.size > LIMIT as u64 { + if found.extents.is_some() + && !matches!( + found.validation, + Validation::ImageDecoded | Validation::StructureChecked + ) + { + // The filesystem record gave the exact pieces and length; the bytes + // are copied as stored, without second-guessing them by content. + // Carved files that cross free-space pieces also carry extents, but + // were checked by content when found, so they are checked again below. + } else if found.kind == "zip" && found.size > LIMIT as u64 { if zip::large_end(output.as_file_mut(), 0, &AtomicBool::new(false))? != Some(found.size) { return Err(io::Error::new( io::ErrorKind::InvalidData, @@ -999,6 +1258,83 @@ pub fn recover_reader( #[cfg(test)] mod tests { use super::*; + #[test] + fn carved_files_across_pieces_are_checked_again_on_recovery() { + let png = include_bytes!("../tests/fixtures/real.png"); + let split = png.len() / 2; + // The image in two pieces with a used gap between them. + let mut source = png[..split].to_vec(); + source.extend_from_slice(&[0xAA; 512]); + source.extend_from_slice(&png[split..]); + let gap_end = (split + 512) as u64; + let found = Found { + offset: 0, + size: png.len() as u64, + kind: "png", + selected: true, + validation: Validation::ImageDecoded, + original_name: None, + resident_data: None, + ntfs: None, + extents: Some(vec![(0, split as u64), (gap_end, (png.len() - split) as u64)].into()), + }; + let dir = tempfile::tempdir().unwrap(); + recover_reader( + &mut io::Cursor::new(source.clone()), + &dir.path().join("ok.png"), + &found, + ) + .unwrap(); + assert_eq!(std::fs::read(dir.path().join("ok.png")).unwrap(), png); + // The second piece was overwritten after the scan. + source[split + 512 + 8..].fill(0); + let changed = dir.path().join("changed.png"); + assert!(recover_reader(&mut io::Cursor::new(source), &changed, &found).is_err()); + assert!(!changed.exists()); + } + + #[test] + fn pdf_end_waits_for_the_next_token_and_accepts_pdf_whitespace() { + let eof = b"%PDF-1.7\n1 0 obj\n<<>>\nendobj\n%%EOF".len(); + let pdf = |after: &[u8]| { + let mut bytes = b"%PDF-1.7\n1 0 obj\n<<>>\nendobj\n%%EOF".to_vec(); + bytes.extend_from_slice(after); + bytes + }; + // A revision with tab-separated header tokens follows. + assert_eq!( + pdf_after_eof(&pdf(b"\n2\t0\tobj"), eof, false), + PdfAfterEof::Continues + ); + // The read stops right after %%EOF or inside the next token: wait. + for cut in [&b"\n"[..], b"\n2 0 o", b"\nxr", b"\nstartx", b"\n12"] { + assert_eq!( + pdf_after_eof(&pdf(cut), eof, false), + PdfAfterEof::Undecided, + "{cut:?}" + ); + } + // At the source's end, the same bytes end the file. + assert_eq!( + pdf_after_eof(&pdf(b"\n2 0 o"), eof, true), + PdfAfterEof::Ends(eof + 1) + ); + // Anything else after %%EOF ends the file. + assert_eq!( + pdf_after_eof(&pdf(b"\nGIF89a"), eof, false), + PdfAfterEof::Ends(eof + 1) + ); + // Zero padding after the file, as in free space, ends it at once. + assert_eq!( + pdf_after_eof(&pdf(b"\n\0\0\0"), eof, false), + PdfAfterEof::Ends(eof + 1) + ); + assert!(matches!( + pdf_end_at_source_end(&pdf(b"\n")), + CandidateEnd::Complete(end) if end == eof + 1 + )); + } + #[test] fn zip_classification_handles_maximum_comment_and_false_footer() { let mut bytes = include_bytes!("../tests/fixtures/package.docx").to_vec(); @@ -1057,6 +1393,34 @@ mod tests { assert_eq!(std::fs::read(&source).unwrap(), bytes); } #[test] + fn pdf_keeps_its_final_line_break_and_every_revision() { + let carve = |pdf: &[u8]| { + let mut bytes = pdf.to_vec(); + bytes.extend_from_slice(&[0; 64]); + let mut results = vec![]; + scan_reader( + &mut std::io::Cursor::new(&bytes), + &AtomicBool::new(false), + |e| { + if let Event::Found(f) = e { + results.push(f) + } + }, + ) + .unwrap(); + assert_eq!(results.len(), 1); + let found = &results[0]; + bytes[to_usize(found.offset)..to_usize(found.offset + found.size)].to_vec() + }; + let simple = b"%PDF-1.7\n1 0 obj\n<<>>\nendobj\n%%EOF\r\n"; + assert_eq!(carve(simple), simple); + // Linearized and incrementally saved files continue after a %%EOF. + let revised = b"%PDF-1.7\n1 0 obj\n<<>>\nendobj\n%%EOF\n2 0 obj\n<<>>\nendobj\nxref\n0 1\ntrailer\n<<>>\n%%EOF\n"; + assert_eq!(carve(revised), revised); + let updated = b"%PDF-1.4\n%%EOF\nxref\n0 1\ntrailer\n<<>>\nstartxref\n9\n%%EOF\n"; + assert_eq!(carve(updated), updated); + } + #[test] fn all_supported_signatures() { let path_temp = tempfile::tempdir().unwrap(); let path = path_temp.path().join("formats"); diff --git a/src/main.rs b/src/main.rs index 05170cb..a585e0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,6 @@ enum Message { SourceAccessDenied, Opened(Arc), Done(Result<(), String>), - Export(Result), } struct Reclaim { inspected: Option, @@ -77,10 +76,20 @@ struct Reclaim { status: String, filter: String, category: usize, + /// Recovery has its own worker, channel and stop flag, so it can run while + /// a scan continues and stopping one never stops the other. exporting: bool, + export_receiver: Option>>, + export_cancel: Arc, + /// The latest recovery message, shown beside Recover; empty when none. + recovery_status: String, started: Option, source_stamp: Option<(u64, Option)>, - recovery_folder: Option<(PathBuf, drives::Destination)>, + /// A chosen folder that may share a drive with the source, waiting for the + /// user in the destination dialog. + destination_review: Option<(PathBuf, drives::Destination)>, + /// The folder the last recovery saved to; the folder picker opens there next. + last_destination: Option, shown: ShownFiles, /// A slow candidate check named in the status line, and since when. long_check: Option<(&'static str, std::time::Instant)>, @@ -90,6 +99,25 @@ struct Reclaim { const LONG_CHECK_AFTER: Duration = Duration::from_millis(1500); /// Once named, a check stays on screen at least this long, so it can be read. const LONG_CHECK_SHOWN: Duration = Duration::from_millis(1500); +/// A source reader that fails once `stop` is set, so a copy in progress ends +/// within one read. `io::copy` retries `Interrupted`, so this uses `Other`. +struct StoppableReader { + inner: R, + stop: Arc, +} +impl std::io::Read for StoppableReader { + fn read(&mut self, buffer: &mut [u8]) -> std::io::Result { + if self.stop.load(Ordering::Relaxed) { + return Err(std::io::Error::other("Recovery stopped")); + } + self.inner.read(buffer) + } +} +impl std::io::Seek for StoppableReader { + fn seek(&mut self, position: std::io::SeekFrom) -> std::io::Result { + self.inner.seek(position) + } +} /// What the file list model currently holds, so a sync appends new results and /// patches changed selections instead of rebuilding every row. A scan tick then /// costs the same with 30 results or 30,000. @@ -137,9 +165,13 @@ impl Default for Reclaim { filter: String::new(), category: 0, exporting: false, + export_receiver: None, + export_cancel: Arc::new(AtomicBool::new(false)), + recovery_status: String::new(), started: None, source_stamp: None, - recovery_folder: None, + destination_review: None, + last_destination: None, shown: ShownFiles::default(), long_check: None, } @@ -152,6 +184,14 @@ impl Reclaim { self.cancel.store(true, Ordering::Relaxed); self.status = "Stopping after the current step…".into(); } + fn stop_recovery(&mut self) { + self.export_cancel.store(true, Ordering::Relaxed); + self.recovery_status = "Stopping after the current step…".into(); + } + /// A scan or a recovery is running, so the source must not change. + fn working(&self) -> bool { + self.busy || self.exporting + } fn start(&mut self) { let Some(path) = self.source.clone() else { return; @@ -164,7 +204,7 @@ impl Reclaim { self.scan_error = None; self.needs_administrator = false; self.unreadable.clear(); - self.exporting = false; + self.recovery_status.clear(); self.started = Some(std::time::Instant::now()); self.source_stamp = path.metadata().ok().map(|m| (m.len(), m.modified().ok())); self.scan_activity = None; @@ -330,14 +370,26 @@ impl Reclaim { }) .collect() } + /// Asks for a recovery folder. The picker opens where the last recovery + /// saved, unless that folder is gone or is the one just turned down. fn choose_recovery_folder(&mut self) { - let Some(directory) = rfd::FileDialog::new() + let rejected = self + .destination_review + .take() + .map(|(directory, _)| directory); + let mut picker = rfd::FileDialog::new() .set_title(i18n::t("Save to a different physical drive")) - .pick_folder() - else { - return; - }; - self.review_recovery_folder(directory); + .set_can_create_directories(true); + if let Some(folder) = self + .last_destination + .as_ref() + .filter(|folder| Some(*folder) != rejected.as_ref() && folder.is_dir()) + { + picker = picker.set_directory(folder); + } + if let Some(directory) = picker.pick_folder() { + self.review_recovery_folder(directory); + } } /// Saves straight away only when the folder provably cannot overwrite the /// scanned data; otherwise the destination dialog explains the risk. @@ -347,17 +399,28 @@ impl Reclaim { }; match drives::destination(source, &directory) { drives::Destination::Separate => self.export(directory), - destination => self.recovery_folder = Some((directory, destination)), + destination => self.destination_review = Some((directory, destination)), + } + } + /// Saves to a folder whose drive could not be determined, after the user + /// confirmed it is on another drive. A same-drive folder is never accepted, + /// and `export` checks the drive again before writing. + fn save_to_unconfirmed_destination(&mut self) { + if let Some((directory, drives::Destination::Unknown)) = self.destination_review.take() { + self.export(directory); } } fn export(&mut self, directory: PathBuf) { let Some(source) = self.scanned_source.clone() else { return; }; + if self.exporting { + return; + } // Recheck after the dialog: a mount or drive may have changed while it // was open. Never allow an affirmative click to write onto the source. if drives::destination(&source, &directory) == drives::Destination::SameDrive { - self.status = "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.".into(); + self.recovery_status = "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.".into(); return; } let files = self.recovery_files(); @@ -368,43 +431,90 @@ impl Reclaim { if needs_source_bytes && source.metadata().ok().map(|m| (m.len(), m.modified().ok())) != self.source_stamp { - self.status = "Source changed since scanning. Scan it again before recovering.".into(); + self.recovery_status = + "Source changed since scanning. Scan it again before recovering.".into(); return; } let Some(handle) = self.source_handle.clone() else { - self.status = "Scan the selected source before recovering files.".into(); + self.recovery_status = "Scan the selected source before recovering files.".into(); return; }; - let (tx, rx) = mpsc::sync_channel(256); - self.receiver = Some(rx); - self.busy = true; - self.status = "Recovering selected files…".into(); + self.last_destination = Some(directory.clone()); + let (tx, rx) = mpsc::sync_channel(1); + self.export_receiver = Some(rx); + self.recovery_status = "Recovering selected files…".into(); self.exporting = true; - self.cancel = Arc::new(AtomicBool::new(false)); - let cancel = self.cancel.clone(); + self.export_cancel = Arc::new(AtomicBool::new(false)); + let cancel = self.export_cancel.clone(); let alignment = self.source_alignment; let source_size = self.source_size; std::thread::spawn(move || { let result = (|| { - let input = handle - .try_clone() + // Never a cloned handle: it would share the running scan's cursor. + let input = preview::Input::new(handle) .map_err(|e| format!("Could not read the scanned source: {e}"))?; - let mut input = reclaim::SourceReader::new(input, alignment, source_size) + let input = reclaim::SourceReader::new(input, alignment, source_size) .map_err(|e| e.to_string())?; + // Stop recovery interrupts a large file mid-copy; its private + // temporary file is discarded, never published. + let mut input = StoppableReader { + inner: input, + stop: cancel.clone(), + }; let mut count = 0usize; + let stopped = |count: usize| { + if count == 1 { + format!("Recovery stopped. {count} file saved.") + } else { + format!("Recovery stopped. {count} files saved.") + } + }; for (f, number) in files { if cancel.load(Ordering::Relaxed) { - return Err(format!("Recovery stopped. {count} files saved.")); + return Err(stopped(count)); } - reclaim::recover_unique(&mut input, &directory, &f, number) - .map_err(|e| format!("Recovered {count} files before error: {e}"))?; + reclaim::recover_unique(&mut input, &directory, &f, number).map_err(|e| { + if cancel.load(Ordering::Relaxed) { + stopped(count) + } else if count == 1 { + format!("Recovered {count} file before error: {e}") + } else { + format!("Recovered {count} files before error: {e}") + } + })?; count = count.saturating_add(1); } Ok(count) })(); - tx.send(Message::Export(result)).ok(); + tx.send(result).ok(); }); } + /// Collects a finished recovery; returns whether anything changed. + fn poll_recovery(&mut self) -> bool { + let Some(rx) = &self.export_receiver else { + return false; + }; + let result = match rx.try_recv() { + Ok(result) => result, + Err(mpsc::TryRecvError::Empty) => return false, + Err(mpsc::TryRecvError::Disconnected) => Err( + "Recovery ended unexpectedly. Check the destination folder before trying again." + .into(), + ), + }; + self.export_receiver = None; + self.exporting = false; + self.recovery_status = match result { + Ok(n @ 1) => { + format!("Saved {n} file. Open the destination folder and verify their contents.") + } + Ok(n) => { + format!("Saved {n} files. Open the destination folder and verify their contents.") + } + Err(e) => e, + }; + true + } } impl Reclaim { fn refresh_drives(&mut self) { @@ -472,13 +582,8 @@ impl Reclaim { changed = true; self.busy = false; self.scan_activity = None; - if self.exporting { - self.exporting = false; - self.status = "Recovery ended unexpectedly. Check the destination folder before trying again.".into(); - } else { - self.scan_error = Some("The scan ended unexpectedly. You can recover the files already found or scan again.".into()); - self.status = "Scan failed. See the message above.".into(); - } + self.scan_error = Some("The scan ended unexpectedly. You can recover the files already found or scan again.".into()); + self.status = "Scan failed. See the message above.".into(); } break; } @@ -572,22 +677,13 @@ impl Reclaim { } }; } - Message::Export(result) => { - self.busy = false; - self.exporting = false; - self.status = match result { - Ok(n) => format!( - "Saved {n} files. Open the destination folder and verify their contents." - ), - Err(e) => e, - }; - } } } } if !self.busy { self.receiver = None; } + changed |= self.poll_recovery(); changed } fn choose_source(&mut self) { @@ -668,8 +764,20 @@ impl Reclaim { ); ui.set_busy(self.busy); ui.set_drives_refreshing(self.drives_loading); - ui.set_can_scan(!self.busy && self.source.is_some()); - ui.set_can_recover(!self.busy && selected > 0); + ui.set_can_scan(!self.working() && self.source.is_some()); + // While recovering, the same button stops the recovery. + ui.set_can_recover(self.exporting || selected > 0); + ui.set_recovering(self.exporting); + ui.set_recovery_status(i18n::t(&self.recovery_status).into()); + let review = self.destination_review.as_ref(); + ui.set_destination_review(review.is_some()); + ui.set_destination_same_drive(matches!(review, Some((_, drives::Destination::SameDrive)))); + ui.set_destination_path( + review + .map(|(folder, _)| i18n::isolate(&folder.display().to_string())) + .unwrap_or_default() + .into(), + ); ui.set_all_selected(visible > 0 && selected == visible); ui.set_has_results(!self.results.is_empty()); ui.set_show_status( @@ -681,7 +789,7 @@ impl Reclaim { let (status, detail) = self.scan_status(std::time::Instant::now()); ui.set_status(status.into()); ui.set_scan_detail(detail.into()); - ui.set_needs_administrator(self.needs_administrator && !self.busy); + ui.set_needs_administrator(self.needs_administrator && !self.working()); ui.set_error( self.scan_error .as_ref() @@ -735,7 +843,13 @@ impl Reclaim { .iter() .map(|d| DriveItem { name: i18n::isolate(&d.display_name()).into(), - device: device_tag(&d.path).into(), + // A phone's path is a USB port in sysfs, which means nothing to people. + device: if d.phone { + String::new() + } else { + device_tag(&d.path) + } + .into(), icon: drive_icon(d), detail: format!( "{} · {}{}", @@ -751,11 +865,17 @@ impl Reclaim { } ) .into(), - size: format_bytes(d.size).into(), - available: !d.encrypted, + size: if d.phone { + String::new() + } else { + format_bytes(d.size) + } + .into(), + available: d.scannable(), }) .collect(); replace_model(ui.get_drives(), drives, |m| ui.set_drives(m)); + ui.set_phone_connected(self.drives.iter().any(|d| d.phone)); } } /// One row of the file list for result `index`. @@ -784,7 +904,9 @@ fn device_tag(path: &std::path::Path) -> String { } /// `kind` holds the fixed English labels set by the platform drive discovery. fn drive_icon(drive: &drives::Drive) -> DriveIcon { - if drive.encrypted { + if drive.phone { + DriveIcon::Phone + } else if drive.encrypted { DriveIcon::Locked } else if drive.kind == "USB drive" { DriveIcon::Usb @@ -936,7 +1058,7 @@ fn run() -> Result<(), slint::PlatformError> { let state = state.clone(); let weak = ui.as_weak(); ui.on_restart_as_administrator(move || { - if state.borrow().busy || !state.borrow().needs_administrator { + if state.borrow().working() || !state.borrow().needs_administrator { return; } let error = match native_process::restart_elevated() { @@ -985,7 +1107,7 @@ fn run() -> Result<(), slint::PlatformError> { }}; } action!(on_open_image, |s: &mut Reclaim, _: &AppWindow| { - if !s.busy { + if !s.working() { s.choose_source(); } }); @@ -993,7 +1115,7 @@ fn run() -> Result<(), slint::PlatformError> { s.refresh_drives(); }); action!(on_scan, |s: &mut Reclaim, ui: &AppWindow| { - if !s.busy { + if !s.working() { s.start(); ui.set_page(1); ui.set_query("".into()); @@ -1009,40 +1131,31 @@ fn run() -> Result<(), slint::PlatformError> { s.category = usize::try_from(ui.get_category()).unwrap_or(0); }); action!(on_recover, |s: &mut Reclaim, _: &AppWindow| { - if s.busy { + // Recovery may start while a scan runs; a second press stops it. + if s.exporting { + s.stop_recovery(); return; } s.choose_recovery_folder(); - if let Some((directory, destination)) = s.recovery_folder.take() { - let explanation = match destination { - drives::Destination::SameDrive => { - "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive." - } - _ => { - "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive." - } - }; - if rfd::MessageDialog::new() - .set_title(i18n::t("Review recovery destination")) - .set_description(i18n::t(explanation)) - .set_buttons(if destination == drives::Destination::SameDrive { - rfd::MessageButtons::Ok - } else { - rfd::MessageButtons::OkCancel - }) - .set_level(rfd::MessageLevel::Warning) - .show() - == rfd::MessageDialogResult::Ok - && destination != drives::Destination::SameDrive - { - s.export(directory); - } + }); + // The destination dialog: pick again, accept a folder on an undetermined + // drive, or drop it. + action!( + on_choose_another_destination, + |s: &mut Reclaim, _: &AppWindow| { + s.choose_recovery_folder(); } + ); + action!(on_save_here_anyway, |s: &mut Reclaim, _: &AppWindow| { + s.save_to_unconfirmed_destination(); + }); + action!(on_cancel_destination, |s: &mut Reclaim, _: &AppWindow| { + s.destination_review = None; }); action_with!(on_select_drive, |s, i: i32| { - if !s.busy + if !s.working() && let Ok(i) = usize::try_from(i) - && let Some(d) = s.drives.get(i).filter(|d| !d.encrypted) + && let Some(d) = s.drives.get(i).filter(|d| d.scannable()) { s.source = Some(d.path.clone()); s.needs_administrator = false; @@ -1127,7 +1240,7 @@ fn run() -> Result<(), slint::PlatformError> { let state = state.clone(); let weak = ui.as_weak(); ui.on_apply_update(move || { - if !state.borrow().busy { + if !state.borrow().working() { updates.borrow_mut().apply(); if updates.borrow().installing() && let Some(ui) = weak.upgrade() @@ -1156,7 +1269,7 @@ fn run() -> Result<(), slint::PlatformError> { s.sync_ui(&ui); } let mut updates = updates.borrow_mut(); - if !s.busy && started.elapsed() >= Duration::from_secs(30) { + if !s.working() && started.elapsed() >= Duration::from_secs(30) { updates.check_automatically(); } let was_updating = updates.installing(); @@ -1249,6 +1362,7 @@ mod slint_ui_tests; impl Drop for Reclaim { fn drop(&mut self) { self.cancel.store(true, Ordering::Relaxed); + self.export_cancel.store(true, Ordering::Relaxed); } } // Prevent process-backed D-Bus addresses (including unixexec) from reaching diff --git a/src/ntfs.rs b/src/ntfs.rs index ec06372..51a1ad2 100644 --- a/src/ntfs.rs +++ b/src/ntfs.rs @@ -1,5 +1,9 @@ -//! Limited NTFS 3-style record recovery. Only deleted base records with one -//! unnamed resident $DATA value are accepted. No cluster runs are guessed. +//! NTFS 3-style deleted-record recovery. +//! +//! A deleted base record keeps its name, size and, for larger files, the list +//! of cluster runs that held the data. Resident data is taken from the record +//! itself. Cluster runs are only used when the caller knows the volume +//! geometry (an unused-space scan); they are read from the record, never guessed. use crate::{Found, NtfsMetadata, Validation}; use std::{ io::{self, Read}, @@ -57,10 +61,106 @@ pub fn read_record( Err(e) }; } - Ok(parse_record(record, offset, cancel).map(|found| (found, size))) + Ok(parse_record(record, offset, None, cancel).map(|found| (found, size))) } -fn parse_record(mut record: Vec, offset: u64, cancel: &AtomicBool) -> Option { +/// Volume geometry needed to turn a deleted record's cluster runs into bytes. +#[derive(Clone, Copy)] +pub(crate) struct Geometry { + pub cluster_size: u64, + pub clusters: u64, +} + +/// Most cluster runs accepted for one deleted file. +const MAX_RUNS: usize = 4096; + +/// A deleted file from a raw MFT record of a mapped volume: resident data, or +/// nonresident data as byte extents of that volume. +pub(crate) fn deleted_file( + record: Vec, + geometry: Geometry, + cancel: &AtomicBool, +) -> Option { + parse_record(record, 0, Some(geometry), cancel) +} + +fn little(bytes: &[u8]) -> u64 { + bytes + .iter() + .rev() + .fold(0, |value, byte| (value << 8) | u64::from(*byte)) +} + +/// Byte extents of an unnamed, uncompressed, fully initialized nonresident +/// $DATA attribute, or `None` when it cannot be read exactly. +fn extents(attr: &[u8], geometry: Geometry) -> Option<(u64, Vec<(u64, u64)>)> { + // Flags: compressed, encrypted or sparse data cannot be copied as stored. + if attr.len() < 64 || short(attr, 12) != 0 || crate::bytes::u64_le(attr, 16) != 0 { + return None; + } + let last = crate::bytes::u64_le(attr, 24); + let size = crate::bytes::u64_le(attr, 48); + let initialized = crate::bytes::u64_le(attr, 56); + if size == 0 || initialized != size { + return None; + } + let mut p = usize::from(short(attr, 32)); + let mut runs: Vec<(u64, u64)> = Vec::new(); + let mut lcn = 0i128; + let mut covered = 0u64; + loop { + let tag = *attr.get(p)?; + p = p.checked_add(1)?; + if tag == 0 { + break; + } + let n = usize::from(tag & 15); + let o = usize::from(tag >> 4); + // o == 0 is a sparse run: there is no stored data to recover. + if n == 0 || n > 8 || o == 0 || o > 8 || runs.len() >= MAX_RUNS { + return None; + } + let length = little(attr.get(p..p.checked_add(n)?)?); + p = p.checked_add(n)?; + let raw = attr.get(p..p.checked_add(o)?)?; + let negative = raw.last().is_some_and(|b| b & 128 != 0); + let value = i128::from(little(raw)); + let delta = if negative { + value.checked_sub(1i128.checked_shl(u32::try_from(o.checked_mul(8)?).ok()?)?)? + } else { + value + }; + p = p.checked_add(o)?; + lcn = lcn.checked_add(delta)?; + let start = u64::try_from(lcn).ok()?; + if length == 0 || start.checked_add(length)? > geometry.clusters { + return None; + } + covered = covered.checked_add(length)?; + let bytes = length.checked_mul(geometry.cluster_size)?; + let at = start.checked_mul(geometry.cluster_size)?; + match runs.last_mut() { + Some((previous, previous_len)) if previous.checked_add(*previous_len) == Some(at) => { + *previous_len = previous_len.checked_add(bytes)?; + } + _ => runs.push((at, bytes)), + } + } + if runs.is_empty() + || last.checked_add(1) != Some(covered) + || covered.checked_mul(geometry.cluster_size)? < size + { + return None; + } + Some((size, runs)) +} + +fn parse_record( + mut record: Vec, + offset: u64, + geometry: Option, + cancel: &AtomicBool, +) -> Option { if cancel.load(Ordering::Relaxed) || short(&record, 16) == 0 || short(&record, 22) != 0 @@ -76,8 +176,11 @@ fn parse_record(mut record: Vec, offset: u64, cancel: &AtomicBool) -> Option return None; } let sector = record.len() / (count - 1); + // NTFS 3.1 records keep their update array at 48; the older 3.0 layout, + // still written by Linux's ntfs3 driver, keeps it at 42 and has no + // record-number field. if !matches!(sector, 512 | 4096) - || usa < 48 + || usa < 42 || usa + count * 2 > first || first < 48 || !first.is_multiple_of(8) @@ -102,10 +205,11 @@ fn parse_record(mut record: Vec, offset: u64, cancel: &AtomicBool) -> Option let mut at = first; let mut name: Option<(u8, String)> = None; let mut data = None; + let mut nonresident = None; let mut terminated = false; let mut standard_info = false; let mut metadata = NtfsMetadata { - record_number: long(&record, 44), + record_number: if usa >= 48 { long(&record, 44) } else { 0 }, sequence_number: short(&record, 16), ..Default::default() }; @@ -207,7 +311,7 @@ fn parse_record(mut record: Vec, offset: u64, cancel: &AtomicBool) -> Option } } 0x80 if attr[9] == 0 => { - if data.is_some() { + if data.is_some() || nonresident.is_some() { return None; } data = Some(Arc::<[u8]>::from(value)); @@ -215,14 +319,36 @@ fn parse_record(mut record: Vec, offset: u64, cancel: &AtomicBool) -> Option _ => {} } } else if kind == 0x80 && attr[9] == 0 { - return None; + let geometry = geometry?; + if data.is_some() || nonresident.is_some() { + return None; + } + nonresident = Some(extents(attr, geometry)?); } at += length; } if !terminated || !standard_info { return None; } - let (_, name) = name?; + let name = name.map(|(_, name)| name); + if let Some((size, runs)) = nonresident { + let (offset, _) = *runs.first()?; + return Some(Found { + offset, + size, + // Linux's ntfs3 driver removes the name from a deleted record but + // keeps its data runs; the type then comes from the file's bytes. + kind: name.as_deref().map_or("bin", extension_kind), + selected: true, + // The record describes the data exactly; its bytes are not checked here. + validation: Validation::MetadataChecked, + original_name: name, + resident_data: None, + ntfs: Some(metadata), + extents: Some(Arc::from(runs)), + }); + } + let name = name?; let data = data?; let kind = if data.starts_with(b"SQLite format 3\0") { "sqlite" @@ -243,10 +369,11 @@ fn parse_record(mut record: Vec, offset: u64, cancel: &AtomicBool) -> Option original_name: Some(name), resident_data: Some(data), ntfs: Some(metadata), + extents: None, }) } -fn extension_kind(name: &str) -> &'static str { +pub(crate) fn extension_kind(name: &str) -> &'static str { match name .rsplit_once('.') .map(|(_, ext)| ext.to_ascii_lowercase()) @@ -286,7 +413,13 @@ fn extension_kind(name: &str) -> &'static str { Some("au") => "au", Some("avi") => "avi", Some("mp3") => "mp3", - Some("mp4") => "mp4", + Some("mp4" | "m4v") => "mp4", + Some("mov") => "mov", + Some("3gp" | "3g2") => "3gp", + Some("mkv") => "mkv", + Some("webm") => "webm", + Some("heic" | "heif") => "heic", + Some("m4a") => "m4a", Some("jar") => "jar", Some("apk") => "apk", Some("exe") => "exe", diff --git a/src/preview.rs b/src/preview.rs index 4aa1f73..724e018 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -14,13 +14,17 @@ pub fn supported(kind: &str) -> bool { ) } -// Independent position: preview reads must not move the scanner's file cursor. -struct Input { +/// A reader with its own position on the shared source handle. +/// +/// A cloned handle shares one file cursor with the running scan on Linux and +/// Windows, so a seek here would silently move the scanner and corrupt both +/// reads. Previews and recovery, which run while a scan is active, use this. +pub(crate) struct Input { file: Arc, position: u64, } impl Input { - fn new(file: Arc) -> std::io::Result { + pub(crate) fn new(file: Arc) -> std::io::Result { #[cfg(windows)] let file = { use std::os::windows::io::{AsRawHandle, FromRawHandle}; @@ -47,7 +51,28 @@ impl Input { return Err(std::io::Error::last_os_error()); } // SAFETY: ReOpenFile returned a new owned handle; transfer it once. - Arc::new(unsafe { std::fs::File::from_raw_handle(handle) }) + let file = unsafe { std::fs::File::from_raw_handle(handle) }; + // Extended volume reads are a per-handle setting: the scan enabled + // it on its own handle, and without it reads near the end of a + // volume can stop early. A plain file (a disk image) refuses this + // control, which changes nothing, so the result is not needed. + let mut returned = 0u32; + // SAFETY: file is a live read-only handle. The control has no input + // or output buffers, never writes to or dismounts the volume, and + // returned is a valid writable u32. + unsafe { + windows_sys::Win32::System::IO::DeviceIoControl( + file.as_raw_handle(), + windows_sys::Win32::System::Ioctl::FSCTL_ALLOW_EXTENDED_DASD_IO, + std::ptr::null(), + 0, + std::ptr::null_mut(), + 0, + &mut returned, + std::ptr::null_mut(), + ); + } + Arc::new(file) }; Ok(Self { file, position: 0 }) } @@ -106,12 +131,11 @@ pub fn load( None, ) .map_err(|e| e.to_string())?; - input - .seek(SeekFrom::Start(found.offset)) - .map_err(|e| e.to_string())?; // audit-allow(DOS-ALLOC): previews are refused above 32 MiB at the top of this function. let mut bytes = vec![0; usize::try_from(found.size).map_err(|e| e.to_string())?]; - input.read_exact(&mut bytes).map_err(|e| e.to_string())?; + reclaim::FoundReader::new(&mut input, found) + .and_then(|mut reader| reader.read_exact(&mut bytes)) + .map_err(|e| e.to_string())?; std::borrow::Cow::Owned(bytes) }; let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)) @@ -149,6 +173,7 @@ mod tests { original_name: None, resident_data: Some(Arc::from(bytes)), ntfs: None, + extents: None, } } diff --git a/src/recovery_tests.rs b/src/recovery_tests.rs index a92d5af..6277002 100644 --- a/src/recovery_tests.rs +++ b/src/recovery_tests.rs @@ -1,6 +1,19 @@ use super::*; use std::io::Write; +/// A 4100-byte source ending in "data", opened by path as the app opens a +/// source. An anonymous temporary file is opened delete-on-close on Windows, +/// and the independent reader recovery uses cannot reopen such a handle. +/// The returned path deletes the file when dropped. +fn source_file() -> (tempfile::TempPath, std::fs::File) { + let mut named = tempfile::NamedTempFile::new().unwrap(); + named.write_all(&[0; 4096]).unwrap(); + named.write_all(b"data").unwrap(); + let path = named.into_temp_path(); + let file = std::fs::File::open(&path).unwrap(); + (path, file) +} + #[cfg(target_os = "linux")] #[test] #[ignore = "requires RECLAIM_TEST_DEVICE and RECLAIM_TEST_DESTINATION on that USB volume"] @@ -23,11 +36,12 @@ fn live_usb_export_to_source_drive_is_blocked_before_any_write() { original_name: Some("must-not-be-written.txt".into()), resident_data: Some(Arc::from(&b"data"[..])), ntfs: None, + extents: None, }]; app.export(directory.clone()); - assert!(!app.busy); - assert!(app.receiver.is_none()); - assert!(app.status.contains("different physical drive")); + assert!(!app.exporting); + assert!(app.export_receiver.is_none()); + assert!(app.recovery_status.contains("different physical drive")); assert_eq!(std::fs::read_dir(directory).unwrap().count(), before); } @@ -66,20 +80,21 @@ fn recovery_exports_only_selected_files_matching_current_filters() { original_name: Some(name.into()), resident_data: Some(Arc::from(&b"data"[..])), ntfs: None, + extents: None, }) .collect(); app.export(directory.clone()); if expected.is_empty() { - assert!(!app.busy); - assert!(app.receiver.is_none()); + assert!(!app.exporting); + assert!(app.export_receiver.is_none()); } else { let result = app - .receiver + .export_receiver .as_ref() .unwrap() .recv_timeout(Duration::from_secs(5)) .unwrap(); - assert!(matches!(result, Message::Export(Ok(count)) if count == expected.len())); + assert_eq!(result, Ok(expected.len())); } let mut actual: Vec<_> = std::fs::read_dir(&directory) .unwrap() @@ -109,6 +124,7 @@ fn stopping_scan_preserves_selected_partial_results() { original_name: Some("photo.png".into()), resident_data: Some(Arc::from(&b"data"[..])), ntfs: None, + extents: None, }))) .unwrap(); tx.send(Message::Done(Ok(()))).unwrap(); @@ -131,6 +147,7 @@ fn automatic_drive_updates_preserve_selection_and_scan_results() { mounted: false, encrypted: false, sector_size: 512, + phone: false, }; let mut app = Reclaim::default(); app.drives = vec![drive("usb1")]; @@ -250,9 +267,7 @@ fn scan_total_stays_unknown_until_unused_space_is_mapped() { #[test] fn recovery_reads_offsets_beyond_the_unused_byte_total() { - let mut source = tempfile::tempfile().unwrap(); - source.write_all(&[0; 4096]).unwrap(); - source.write_all(b"data").unwrap(); + let (_path, source) = source_file(); let directory = tempfile::tempdir().unwrap(); let mut app = Reclaim::default(); app.scanned_source = Some("test-volume".into()); @@ -268,14 +283,20 @@ fn recovery_reads_offsets_beyond_the_unused_byte_total() { original_name: Some("recovered.txt".into()), resident_data: None, ntfs: None, + extents: None, }]; app.export(directory.path().to_owned()); let deadline = std::time::Instant::now() + Duration::from_secs(5); - while app.busy && std::time::Instant::now() < deadline { + while app.exporting && std::time::Instant::now() < deadline { app.poll(); std::thread::yield_now(); } - assert!(!app.busy, "export did not finish"); + assert!(!app.exporting, "export did not finish"); + assert!( + app.recovery_status.starts_with("Saved"), + "{}", + app.recovery_status + ); assert_eq!( std::fs::read(directory.path().join("recovered.txt")).unwrap(), b"data" @@ -284,22 +305,36 @@ fn recovery_reads_offsets_beyond_the_unused_byte_total() { #[test] fn disconnected_workers_stop_busy_state_without_overwriting_completed_results() { - for exporting in [false, true] { - let mut app = Reclaim::default(); - app.busy = true; - app.exporting = exporting; - let (tx, rx) = mpsc::channel(); - app.receiver = Some(rx); - drop(tx); - app.poll(); - assert!(!app.busy); - assert!(!app.exporting); - assert!(app.receiver.is_none()); - assert_eq!(app.scan_error.is_some(), !exporting); - let status = app.status.clone(); - app.poll(); - assert_eq!(app.status, status); - } + let mut app = Reclaim::default(); + app.busy = true; + let (tx, rx) = mpsc::sync_channel(1); + app.receiver = Some(rx); + drop(tx); + app.poll(); + assert!(!app.busy); + assert!(app.receiver.is_none()); + assert!(app.scan_error.is_some()); + let status = app.status.clone(); + app.poll(); + assert_eq!(app.status, status); + + // A lost recovery worker reports beside Recover and leaves a scan alone. + let mut app = Reclaim::default(); + app.busy = true; + let (_scan, rx) = mpsc::sync_channel(1); + app.receiver = Some(rx); + app.status = "Scanning for recoverable files…".into(); + app.exporting = true; + let (tx, rx) = mpsc::sync_channel::>(1); + app.export_receiver = Some(rx); + drop(tx); + app.poll(); + assert!(!app.exporting); + assert!(app.export_receiver.is_none()); + assert!(app.recovery_status.contains("Recovery ended unexpectedly")); + assert!(app.busy); + assert!(app.scan_error.is_none()); + assert_eq!(app.status, "Scanning for recoverable files…"); } #[test] @@ -343,3 +378,98 @@ fn source_access_failure_offers_elevation_only_when_requested_by_the_worker() { assert!(app.results.is_empty()); } } + +/// A source with a file at 4096, held open the way a running scan holds it. +fn scanned_source(app: &mut Reclaim) -> (tempfile::TempPath, Arc) { + let (path, source) = source_file(); + let handle = Arc::new(source); + app.scanned_source = Some("test-volume".into()); + app.source_handle = Some(handle.clone()); + app.source_size = Some(4100); + app.results = vec![Found { + offset: 4096, + size: 4, + kind: "txt", + selected: true, + validation: reclaim::Validation::Unverified, + original_name: Some("recovered.txt".into()), + resident_data: None, + ntfs: None, + extents: None, + }]; + (path, handle) +} + +fn finish_recovery(app: &mut Reclaim) { + let deadline = std::time::Instant::now() + Duration::from_secs(5); + while app.exporting && std::time::Instant::now() < deadline { + app.poll(); + std::thread::yield_now(); + } + assert!(!app.exporting, "recovery did not finish"); + assert!( + app.recovery_status.starts_with("Saved"), + "{}", + app.recovery_status + ); +} + +#[test] +fn recovery_while_scanning_keeps_the_scan_running_and_its_cursor_in_place() { + use std::io::{Seek, SeekFrom}; + let mut app = Reclaim::default(); + let (_path, handle) = scanned_source(&mut app); + // The scan is mid-way: it owns the receiver, the stop flag and a cursor. + app.busy = true; + let (scan, rx) = mpsc::sync_channel(4); + app.receiver = Some(rx); + app.status = "Scanning for recoverable files…".into(); + let scan_cancel = app.cancel.clone(); + (&*handle).seek(SeekFrom::Start(1234)).unwrap(); + let directory = tempfile::tempdir().unwrap(); + app.export(directory.path().to_owned()); + assert!(app.exporting); + assert!(app.busy); + finish_recovery(&mut app); + assert_eq!( + std::fs::read(directory.path().join("recovered.txt")).unwrap(), + b"data" + ); + // A cloned handle shares this cursor; recovery must not have moved it. + assert_eq!((&*handle).stream_position().unwrap(), 1234); + assert!(app.busy); + assert!(!scan_cancel.load(Ordering::Relaxed)); + assert_eq!(app.status, "Scanning for recoverable files…"); + assert!(app.recovery_status.starts_with("Saved 1 file.")); + // The scan keeps reporting into the same state afterwards. + scan.send(Message::Scan(Event::Progress(10))).unwrap(); + app.poll(); + assert_eq!(app.scanned, 10); +} + +#[test] +fn stopping_recovery_and_stopping_the_scan_are_independent() { + let mut app = Reclaim::default(); + app.busy = true; + app.exporting = true; + app.stop_recovery(); + assert!(app.export_cancel.load(Ordering::Relaxed)); + assert!(!app.cancel.load(Ordering::Relaxed)); + let mut app = Reclaim::default(); + app.busy = true; + app.exporting = true; + app.stop_operation(); + assert!(app.cancel.load(Ordering::Relaxed)); + assert!(!app.export_cancel.load(Ordering::Relaxed)); +} + +#[test] +fn a_second_recovery_cannot_start_while_one_runs() { + let mut app = Reclaim::default(); + let _source = scanned_source(&mut app); + app.exporting = true; + let directory = tempfile::tempdir().unwrap(); + app.export(directory.path().to_owned()); + assert!(app.export_receiver.is_none()); + assert_eq!(std::fs::read_dir(directory.path()).unwrap().count(), 0); +} diff --git a/src/resilient.rs b/src/resilient.rs index c42e7eb..cbfc978 100644 --- a/src/resilient.rs +++ b/src/resilient.rs @@ -23,6 +23,10 @@ struct Reader<'a, R> { input: &'a mut R, damage: Rc>, cancel: &'a AtomicBool, + /// End of the physically contiguous area holding an offset. A retry, and + /// so a damage mark, never crosses it: in joined free space the next area + /// is elsewhere on the disk and may be healthy. + area_end: &'a dyn Fn(u64) -> u64, } impl Read for Reader<'_, R> { fn read(&mut self, bytes: &mut [u8]) -> io::Result { @@ -62,7 +66,10 @@ impl Read for Reader<'_, R> { Err(error) if is_media_error(&error) => { self.input.seek(SeekFrom::Start(at))?; // Retry one bounded block. Preserve healthy data surrounding the fault. - let count = bytes.len().min(to_usize(4096u64.saturating_sub(at % 4096))); + let count = bytes + .len() + .min(to_usize(4096u64.saturating_sub(at % 4096))) + .min(to_usize((self.area_end)(at).saturating_sub(at)).max(1)); let Some(block) = bytes.get_mut(..count) else { return Err(io::Error::from(io::ErrorKind::InvalidInput)); }; @@ -115,12 +122,14 @@ pub(crate) fn scan( cancel: &AtomicBool, mut event: impl FnMut(Event), buffer_size: usize, + area_end: &dyn Fn(u64) -> u64, ) -> io::Result<()> { let damage = Rc::new(RefCell::new(Damage::default())); let mut reader = Reader { input, damage: damage.clone(), cancel, + area_end, }; let result = crate::scan_reader_with_filter( &mut reader, @@ -204,6 +213,7 @@ mod tests { _ => {} }, 32768, + &|_| u64::MAX, ) .unwrap(); assert_eq!(found, vec![0, 12288, 28672], "alignment {alignment}"); @@ -214,9 +224,17 @@ mod tests { fn permission_errors_remain_fatal_and_cancellation_does_not_probe() { let mut source = device(); source.error = if cfg!(windows) { 5 } else { 13 }; - assert!(scan(&mut source, &AtomicBool::new(false), |_| {}, 32768).is_err()); + assert!( + scan(&mut source, &AtomicBool::new(false), |_| {}, 32768, &|_| { + u64::MAX + }) + .is_err() + ); let mut source = device(); - scan(&mut source, &AtomicBool::new(true), |_| {}, 32768).unwrap(); + scan(&mut source, &AtomicBool::new(true), |_| {}, 32768, &|_| { + u64::MAX + }) + .unwrap(); assert_eq!(source.reads, 0); } #[test] @@ -228,6 +246,7 @@ mod tests { input: &mut input, damage, cancel: &cancel, + area_end: &|_| u64::MAX, }; reader.seek(SeekFrom::Start(8192)).unwrap(); assert_eq!(reader.read(&mut [0; 4096]).unwrap(), 4096); @@ -289,6 +308,7 @@ mod overlap_tests { } }, 16384, + &|_| u64::MAX, ) .unwrap(); assert_eq!(found, vec![(12288, "pdf")]); diff --git a/src/slint_ui_tests.rs b/src/slint_ui_tests.rs index cbb9508..0277378 100644 --- a/src/slint_ui_tests.rs +++ b/src/slint_ui_tests.rs @@ -1,5 +1,45 @@ use super::*; +/// A phone connected for file transfer, as Linux discovery reports it. +fn phone() -> drives::Drive { + drives::Drive { + path: "/sys/bus/usb/devices/1-2".into(), + name: "Unihertz F21 Pro".into(), + name_is_message: false, + size: 0, + kind: "Phone or camera".into(), + location: "File transfer only".into(), + mounted: false, + encrypted: false, + sector_size: 512, + phone: true, + } +} + +#[test] +fn phones_are_listed_unavailable_with_an_explanation() { + slint::platform::set_platform(Box::new(i_slint_backend_testing::TestingBackend::new( + i_slint_backend_testing::TestingBackendOptions { + renderer_name: Some("software".into()), + ..Default::default() + }, + ))) + .ok(); + let ui = AppWindow::new().unwrap(); + let mut state = Reclaim::default(); + state.sync_ui(&ui); + assert!(!ui.get_phone_connected()); + state.drives.push(phone()); + state.sync_ui(&ui); + assert!(ui.get_phone_connected()); + let row = ui.get_drives().row_data(0).unwrap(); + assert!(!row.available); + assert_eq!(row.icon, DriveIcon::Phone); + assert_eq!(row.size, ""); + assert_eq!(row.device, ""); + assert!(row.detail.contains("File transfer only")); +} + #[test] fn slint_workspace_renders_all_pages_and_filters_recovery_selection() { slint::platform::set_platform(Box::new(i_slint_backend_testing::TestingBackend::new( @@ -21,6 +61,7 @@ fn slint_workspace_renders_all_pages_and_filters_recovery_selection() { mounted: false, encrypted: false, sector_size: 512, + phone: false, }); // Other kinds render their own icon and device tag in the previews. for (path, name, name_is_message, kind, location, encrypted) in [ @@ -59,8 +100,10 @@ fn slint_workspace_renders_all_pages_and_filters_recovery_selection() { mounted: false, encrypted, sector_size: 512, + phone: false, }); } + state.drives.push(phone()); state.source = Some(state.drives[0].path.clone()); state.results = vec![ Found { @@ -71,6 +114,7 @@ fn slint_workspace_renders_all_pages_and_filters_recovery_selection() { original_name: Some("Summer.png".into()), resident_data: None, ntfs: None, + extents: None, validation: reclaim::Validation::StructureChecked, }, Found { @@ -81,6 +125,7 @@ fn slint_workspace_renders_all_pages_and_filters_recovery_selection() { original_name: Some("Notes.pdf".into()), resident_data: None, ntfs: None, + extents: None, validation: reclaim::Validation::StructureChecked, }, ]; @@ -590,6 +635,7 @@ fn file_list_stays_exact_while_a_large_scan_streams_results() { original_name: n.is_multiple_of(7).then(|| format!("Holiday {n}.{kind}")), resident_data: None, ntfs: None, + extents: None, validation: reclaim::Validation::StructureChecked, }; let assert_exact = |state: &Reclaim, ui: &AppWindow, step: &str| { @@ -801,6 +847,11 @@ fn layout_is_clear_and_steady_on_every_page_language_and_size() { let ui = AppWindow::new().unwrap(); ui.show().unwrap(); let first_run = Reclaim::default; + let with_phone = || { + let mut state = Reclaim::default(); + state.drives.push(phone()); + state + }; let scanning = || { let mut state = Reclaim::default(); state.source = Some("/dev/disk-with-a-long-descriptive-name".into()); @@ -815,6 +866,7 @@ fn layout_is_clear_and_steady_on_every_page_language_and_size() { )), resident_data: None, ntfs: None, + extents: None, validation: reclaim::Validation::StructureChecked, }); } @@ -824,6 +876,12 @@ fn layout_is_clear_and_steady_on_every_page_language_and_size() { state.scanned = 2_250_000_000; state }; + let recovering = || { + let mut state = scanning(); + state.exporting = true; + state.recovery_status = "Recovering selected files…".into(); + state + }; for (code, language) in [ ("en", i18n::Language::English), ("he", i18n::Language::Hebrew), @@ -842,7 +900,9 @@ fn layout_is_clear_and_steady_on_every_page_language_and_size() { ui.set_page(page); for (name, make, notice) in [ ("first run", &first_run as &dyn Fn() -> Reclaim, false), + ("phone connected", &with_phone, false), ("scanning", &scanning, false), + ("recovering while scanning", &recovering, false), ("update notice", &first_run, true), ] { let mut state = make(); @@ -858,6 +918,39 @@ fn layout_is_clear_and_steady_on_every_page_language_and_size() { } ui.set_update_notice(false); ui.set_page(1); + // The list header never elides, so counts in the hundreds of + // thousands must still fit the narrow list column. + scanning().sync_ui(&ui); + let count = "250000"; + ui.set_summary(i18n::message("Found: {0} · Selected: {1}", &[count, count]).into()); + ui.window().take_snapshot().unwrap(); + assert_controls_clear( + &ui, + window, + &format!("{code} {width}x{height} page 1 large counts"), + ); + // Both labels are at least as wide as their text, so text that + // does not fit pokes out of the list column instead of eliding. + let element = |id: &str| { + i_slint_backend_testing::ElementHandle::find_by_element_id(&ui, id) + .next() + .unwrap_or_else(|| panic!("{id}")) + }; + let list = element("AppWindow::files-view"); + let (left, right) = ( + list.absolute_position().x, + list.absolute_position().x + list.size().width, + ); + for id in ["AppWindow::select-all-caption", "AppWindow::list-summary"] { + let label = element(id); + let start = label.absolute_position().x; + let end = start + label.size().width; + assert!( + start >= left - 0.5 && end <= right + 0.5, + "{code} {width}x{height}: {id} ({start}..{end}) is cut off by the list \ + column ({left}..{right})" + ); + } let mut state = scanning(); let mut steady = |ui: &AppWindow| { state.sync_ui(ui); @@ -867,12 +960,85 @@ fn layout_is_clear_and_steady_on_every_page_language_and_size() { ui.set_status(i18n::message("Checking a {0} file…", &["ZIP"]).into()); ui.window().take_snapshot().unwrap(); }; + let mut recovering_now = |ui: &AppWindow| { + recovering().sync_ui(ui); + ui.window().take_snapshot().unwrap(); + }; assert_steady( &ui, "ScanStatus::headline", &format!("{code} {width}x{height}"), - &mut [("scanning", &mut steady), ("slow check", &mut slow_check)], + &mut [ + ("scanning", &mut steady), + ("slow check", &mut slow_check), + ("recovering", &mut recovering_now), + ], ); + // Recovery messages replace the hint without moving anything. + let message = |text: &'static str| { + move |ui: &AppWindow| { + let mut state = recovering(); + state.recovery_status = text.into(); + state.sync_ui(ui); + ui.window().take_snapshot().unwrap(); + } + }; + let mut hint = |ui: &AppWindow| { + scanning().sync_ui(ui); + ui.window().take_snapshot().unwrap(); + }; + assert_steady( + &ui, + "AppWindow::recovery-message", + &format!("{code} {width}x{height}"), + &mut [ + ("hint", &mut hint), + ("recovering", &mut message("Recovering selected files…")), + ( + "saved", + &mut message( + "Saved 1 file. Open the destination folder and verify their contents.", + ), + ), + ( + "warning", + &mut message( + "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.", + ), + ), + ], + ); + // Selected files can be recovered mid-scan; the button then stops it. + scanning().sync_ui(&ui); + assert!(ui.get_can_recover(), "{code}: recover during a scan"); + recovering().sync_ui(&ui); + ui.window().take_snapshot().unwrap(); + assert!(ui.get_can_recover() && ui.get_busy()); + assert!( + i_slint_backend_testing::ElementHandle::find_by_accessible_label( + &ui, + &i18n::t("Stop recovery"), + ) + .next() + .is_some(), + "{code}: the recover button offers to stop" + ); + if let Ok(directory) = std::env::var("RECLAIM_PREVIEW_DIR") { + let mut state = recovering(); + state.recovery_status = + "Saved 1 file. Open the destination folder and verify their contents." + .into(); + state.sync_ui(&ui); + let snapshot = ui.window().take_snapshot().unwrap(); + image::save_buffer( + format!("{directory}/recovering-{code}-{width}.png"), + snapshot.as_bytes(), + snapshot.width(), + snapshot.height(), + image::ColorType::Rgba8, + ) + .unwrap(); + } } }); } @@ -974,3 +1140,226 @@ fn windows_drive_access_failure_has_an_actionable_aligned_restart_button() { }); } } + +#[test] +fn destination_review_offers_a_safe_choice_and_never_accepts_the_source_drive() { + use crate::layout_checks::assert_controls_clear; + slint::platform::set_platform(Box::new(i_slint_backend_testing::TestingBackend::new( + i_slint_backend_testing::TestingBackendOptions { + renderer_name: Some("software".into()), + ..Default::default() + }, + ))) + .ok(); + let ui = AppWindow::new().unwrap(); + ui.show().unwrap(); + ui.set_page(1); + let folder = PathBuf::from("/media/user/Backup drive with a long name/Recovered photos"); + let button = |ui: &AppWindow, label: &str| { + i_slint_backend_testing::ElementHandle::find_by_accessible_label(ui, &i18n::t(label)) + .next() + .is_some() + }; + for (code, language) in [ + ("en", i18n::Language::English), + ("he", i18n::Language::Hebrew), + ("ar", i18n::Language::Arabic), + ("de", i18n::Language::German), + ("es", i18n::Language::Spanish), + ("fr", i18n::Language::French), + ("ru", i18n::Language::Russian), + ] { + i18n::with_language(language, || { + ui_text::apply(&ui); + for (width, height) in [(900u32, 640u32), (1160, 780)] { + ui.window() + .set_size(slint::PhysicalSize::new(width, height)); + for (kind, destination) in [ + ("same-drive", drives::Destination::SameDrive), + ("unknown", drives::Destination::Unknown), + ] { + let mut state = Reclaim::default(); + state.destination_review = Some((folder.clone(), destination)); + state.sync_ui(&ui); + let snapshot = ui.window().take_snapshot().unwrap(); + let context = format!("{code} {width}x{height} {kind} destination"); + assert!(ui.get_destination_review(), "{context}"); + assert!(ui.get_destination_path().contains("Recovered photos")); + assert!(button(&ui, "Choose another folder"), "{context}"); + assert!(button(&ui, "Cancel"), "{context}"); + assert_eq!( + button(&ui, "Save here anyway"), + destination == drives::Destination::Unknown, + "{context}: only an undetermined drive can be accepted" + ); + assert_controls_clear(&ui, (width as f32, height as f32), &context); + // Dialog buttons never elide, so each must fit inside the card. + let card = i_slint_backend_testing::ElementHandle::find_by_element_id( + &ui, + "AppWindow::destination-card", + ) + .next() + .expect("destination card"); + let (left, right) = ( + card.absolute_position().x, + card.absolute_position().x + card.size().width, + ); + for label in ["Cancel", "Save here anyway", "Choose another folder"] { + for action in + i_slint_backend_testing::ElementHandle::find_by_accessible_label( + &ui, + &i18n::t(label), + ) + { + let start = action.absolute_position().x; + let end = start + action.size().width; + assert!( + start >= left && end <= right, + "{context}: {label} ({start}..{end}) is outside the dialog ({left}..{right})" + ); + } + } + if let Ok(directory) = std::env::var("RECLAIM_PREVIEW_DIR") { + image::save_buffer( + format!("{directory}/destination-{kind}-{code}-{width}.png"), + snapshot.as_bytes(), + snapshot.width(), + snapshot.height(), + image::ColorType::Rgba8, + ) + .unwrap(); + } + } + } + }); + } + // Escape cancels. + let cancelled = std::rc::Rc::new(std::cell::Cell::new(false)); + ui.on_cancel_destination({ + let cancelled = cancelled.clone(); + move || cancelled.set(true) + }); + ui.set_destination_review(false); + ui.window().take_snapshot().unwrap(); + ui.set_destination_review(true); + ui.window().take_snapshot().unwrap(); + let escape: slint::SharedString = slint::platform::Key::Escape.into(); + ui.window() + .dispatch_event(slint::platform::WindowEvent::KeyPressed { + text: escape.clone(), + }); + ui.window() + .dispatch_event(slint::platform::WindowEvent::KeyReleased { text: escape }); + assert!(cancelled.get(), "Escape closes the destination dialog"); + // "Save here anyway" never writes onto the scanned drive, even if invoked + // for a same-drive folder; the dialog just closes. + let mut state = Reclaim::default(); + state.destination_review = Some((folder.clone(), drives::Destination::SameDrive)); + state.save_to_unconfirmed_destination(); + assert!(!state.exporting && state.destination_review.is_none()); +} + +#[test] +fn finished_scan_message_wraps_at_the_full_width() { + slint::platform::set_platform(Box::new(i_slint_backend_testing::TestingBackend::new( + i_slint_backend_testing::TestingBackendOptions { + renderer_name: Some("software".into()), + ..Default::default() + }, + ))) + .ok(); + let ui = AppWindow::new().unwrap(); + ui.show().unwrap(); + ui.window().set_size(slint::PhysicalSize::new(900, 640)); + ui.set_page(1); + i18n::with_language(i18n::Language::German, || { + ui_text::apply(&ui); + let mut state = Reclaim::default(); + state.status = "Scan finished with unreadable areas. Some files could not be checked. The drive may be failing: recover the files you need now and avoid repeated scans.".into(); + state.sync_ui(&ui); + let snapshot = ui.window().take_snapshot().unwrap(); + let headline = + i_slint_backend_testing::ElementHandle::find_by_element_id(&ui, "ScanStatus::headline") + .next() + .expect("scan headline"); + // Wrapped at the row's width this is two or three lines, not a word per line. + assert!(headline.size().width > 300.0, "{:?}", headline.size()); + assert!(headline.size().height < 70.0, "{:?}", headline.size()); + if let Ok(directory) = std::env::var("RECLAIM_PREVIEW_DIR") { + image::save_buffer( + format!("{directory}/status-de-finished.png"), + snapshot.as_bytes(), + snapshot.width(), + snapshot.height(), + image::ColorType::Rgba8, + ) + .unwrap(); + } + }); +} + +#[test] +fn stop_buttons_are_filled_red_in_light_and_dark() { + slint::platform::set_platform(Box::new(i_slint_backend_testing::TestingBackend::new( + i_slint_backend_testing::TestingBackendOptions { + renderer_name: Some("software".into()), + mock_time: true, + ..Default::default() + }, + ))) + .ok(); + let ui = AppWindow::new().unwrap(); + ui.show().unwrap(); + ui.window().set_size(slint::PhysicalSize::new(900, 640)); + ui_text::apply(&ui); + ui.set_page(1); + let mut state = Reclaim::default(); + state.results.push(Found { + offset: 0, + size: 2048, + kind: "jpg", + selected: true, + original_name: None, + resident_data: None, + ntfs: None, + extents: None, + validation: reclaim::Validation::StructureChecked, + }); + state.busy = true; + state.exporting = true; + state.status = "Scanning for recoverable files…".into(); + for dark in [false, true] { + ui.global::().set_theme(if dark { 2 } else { 1 }); + state.sync_ui(&ui); + ui.window().take_snapshot().unwrap(); + // Let the button's background animation finish. + i_slint_backend_testing::mock_elapsed_time(Duration::from_millis(300)); + slint::platform::update_timers_and_animations(); + let snapshot = ui.window().take_snapshot().unwrap(); + for label in ["Stop", "Stop recovery"] { + let button = + i_slint_backend_testing::ElementHandle::find_by_accessible_label(&ui, label) + .next() + .unwrap_or_else(|| panic!("{label} button")); + // A background pixel inside the button, clear of its icon and caption. + let position = button.absolute_position(); + #[expect( + clippy::cast_possible_truncation, + clippy::cast_sign_loss, + reason = "an on-screen position in a 900x640 snapshot is small and positive" + )] + let (x, y) = ((position.x + 5.0) as u32, (position.y + 5.0) as u32); + let offset = ((y * snapshot.width() + x) * 4) as usize; + let pixel = &snapshot.as_bytes()[offset..offset + 3]; + let (red, green, blue) = ( + i32::from(pixel[0]), + i32::from(pixel[1]), + i32::from(pixel[2]), + ); + assert!( + red > green + 80 && red > blue + 80, + "{label} is not filled red (dark: {dark}): {pixel:?}" + ); + } + } +} diff --git a/src/ui_text.rs b/src/ui_text.rs index f702932..3174fb3 100644 --- a/src/ui_text.rs +++ b/src/ui_text.rs @@ -73,7 +73,7 @@ ui_strings! { set_version => "Version", set_recover_in_three_steps => "Recover in three steps", set_supported_formats => "Supported formats", - set_photos_documents_archives_audio_video => "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, and NTFS volumes.", + set_photos_documents_archives_audio_video => "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, NTFS, ext2/3/4, and HFS+ volumes.", set_check_for_updates => "Check for updates", set_restart_as_administrator => "Restart as administrator", set_whats_new => "What’s new", @@ -85,6 +85,14 @@ ui_strings! { set_project_website => "Project website", set_built_with_rust_and_slint => "Built with Rust and Slint · MIT licensed application", set_stop => "Stop", + set_stop_recovery => "Stop recovery", + set_review_recovery_destination => "Review recovery destination", + set_saving_here_can_overwrite => "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive.", + set_saving_to_the_same_drive => "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive.", + set_choose_another_folder => "Choose another folder", + set_save_here_anyway => "Save here anyway", + set_cancel => "Cancel", + set_phones_and_cameras_connect_for => "Phones and cameras connect for file transfer only, so Reclaim can’t search them for deleted files. If yours has a memory card, put the card in a card reader and scan it here. Also check its Trash or Recently deleted folder.", set_language => "Language", set_appearance => "Appearance", } diff --git a/tests/audit/baseline.toml b/tests/audit/baseline.toml index 73a8fec..6f6037e 100644 --- a/tests/audit/baseline.toml +++ b/tests/audit/baseline.toml @@ -5,7 +5,7 @@ [index] "src/allocation/fat.rs" = 19 "src/allocation/ntfs.rs" = 17 -"src/allocation.rs" = 6 +"src/allocation.rs" = 2 "src/detection.rs" = 19 "src/drives/linux.rs" = 0 "src/drives/macos.rs" = 0 diff --git a/tests/audit/main.rs b/tests/audit/main.rs index 4867f2a..9d40a6b 100644 --- a/tests/audit/main.rs +++ b/tests/audit/main.rs @@ -16,20 +16,22 @@ //! Suppressions that no longer match a finding are reported too, so they cannot //! outlive the code they excused. -#![expect( - clippy::expect_used, - clippy::panic, - clippy::string_slice, - reason = "a test harness reports by panicking, and the rules slice ASCII-delimited text" -)] +#![expect(clippy::string_slice, reason = "the rules slice ASCII-delimited text")] +// Test-only code, so Clippy treats the harness helpers as tests on every +// toolchain: they report findings by panicking. +#[cfg(test)] mod rules; +#[cfg(test)] mod supply_chain; +#[cfg(test)] mod translations; +#[cfg(test)] mod workspace; use workspace::{Workspace, report}; +#[cfg(test)] fn workspace() -> &'static Workspace { use std::sync::OnceLock; static WORKSPACE: OnceLock = OnceLock::new(); diff --git a/tests/audit/translations.rs b/tests/audit/translations.rs index c2effe4..ff64e65 100644 --- a/tests/audit/translations.rs +++ b/tests/audit/translations.rs @@ -147,6 +147,33 @@ pub fn catalogs(ws: &Workspace) -> Vec { format!("bidi override character in {key:?}; use isolates"), )); } + if RIGHT_TO_LEFT + .iter() + .any(|code| catalog.path.ends_with(&format!("/{code}.json"))) + && has_unanchored_trailing_symbol(value) + { + findings.push(Finding::new( + "I18N-BIDI", + Some(451), + &catalog.path, + line, + format!( + "a Latin term ending in + or # in {key:?} shows the symbol on the wrong \ + side; add U+200E after it" + ), + )); + } + if let Some(word) = informal_word(&catalog.path, value) { + findings.push(Finding::new( + "I18N-FORMALITY", + None, + &catalog.path, + line, + format!( + "informal {word:?} in {key:?}; this catalog addresses the user formally" + ), + )); + } } let keys: BTreeSet<&str> = catalog.pairs.iter().map(|(k, _)| k.as_str()).collect(); for missing in english.difference(&keys) { @@ -172,6 +199,95 @@ pub fn catalogs(ws: &Workspace) -> Vec { findings } +/// Catalogs laid out right to left. +const RIGHT_TO_LEFT: &[&str] = &["he", "ar"]; + +/// Informal forms of address for catalogs that use the formal one. The German +/// list adds the informal imperatives this app's messages use, since German +/// imperatives often carry no pronoun ("Wähle" against "Wählen Sie"). +const INFORMAL: &[(&str, &[&str])] = &[ + ( + "de", + &[ + "du", + "dich", + "dir", + "dein", + "deine", + "deinen", + "deinem", + "deiner", + "deines", + "kannst", + "hast", + "möchtest", + "willst", + "musst", + "benutze", + "durchsuche", + "lade", + "öffne", + "prüfe", + "schließe", + "speichere", + "starte", + "stelle", + "verbinde", + "vermeide", + "versuche", + "verwende", + "wähle", + ], + ), + ("fr", &["tu", "toi", "ton", "ta", "tes"]), + ( + "ru", + &[ + "ты", + "тебя", + "тебе", + "тобой", + "твой", + "твоя", + "твоё", + "твои", + "твоих", + ], + ), +]; + +/// The first informal word in `value` when `path` is a catalog that must +/// address the user formally. +fn informal_word(path: &str, value: &str) -> Option { + let (_, words) = INFORMAL + .iter() + .find(|(code, _)| path.ends_with(&format!("/{code}.json")))?; + value + .split(|c: char| !c.is_alphabetic()) + .map(str::to_lowercase) + .find(|word| words.contains(&word.as_str())) +} + +/// Whether a Latin term ends in `+` or `#` ("HFS+", "C#") without a +/// left-to-right mark after it. In a right-to-left paragraph the symbol then +/// takes the paragraph's direction and is drawn on the other side of the term. +fn has_unanchored_trailing_symbol(value: &str) -> bool { + let mut chars = value.chars().peekable(); + while let Some(c) = chars.next() { + if !c.is_ascii_alphanumeric() || !chars.peek().is_some_and(|n| matches!(n, '+' | '#')) { + continue; + } + while chars.next_if(|n| matches!(n, '+' | '#')).is_some() {} + if chars + .peek() + .is_none_or(|n| *n != '\u{200e}' && !n.is_ascii_alphanumeric()) + { + return true; + } + } + false +} + /// Whether a literal reads like a sentence meant for people rather than an /// identifier, format, path or file signature. fn looks_like_message(text: &str) -> bool { diff --git a/tests/container_detection.rs b/tests/container_detection.rs index 566be42..cae1ada 100644 --- a/tests/container_detection.rs +++ b/tests/container_detection.rs @@ -1,11 +1,8 @@ -#![expect( - clippy::unwrap_used, - reason = "fixture helpers fail the test by panicking" -)] use reclaim::{Event, Found, Validation, recover_reader, scan_reader}; use std::io::{self, Cursor, Read, Seek, SeekFrom}; use std::sync::atomic::AtomicBool; +#[cfg(test)] fn collect(input: &mut (impl Read + Seek)) -> Vec { let mut found = Vec::new(); scan_reader(input, &AtomicBool::new(false), |e| { @@ -129,6 +126,7 @@ fn fix_crc(page: &mut [u8]) { } page[22..26].copy_from_slice(&crc.to_le_bytes()); } +#[cfg(test)] fn page(serial: u32, sequence: u32, flags: u8, lacing: &[u8], body: &[u8]) -> Vec { assert_eq!( lacing.iter().map(|&n| usize::from(n)).sum::(), diff --git a/tests/expanded_formats.rs b/tests/expanded_formats.rs index e02ae65..b4eebff 100644 --- a/tests/expanded_formats.rs +++ b/tests/expanded_formats.rs @@ -1,10 +1,7 @@ -#![expect( - clippy::unwrap_used, - reason = "fixture helpers fail the test by panicking" -)] use reclaim::{Event, Found, recover, scan}; use std::sync::atomic::AtomicBool; +#[cfg(test)] fn carve_and_check(fixtures: &[(&str, Vec)]) { let dir_temp = tempfile::tempdir().unwrap(); let dir = dir_temp.path().join("work"); @@ -90,6 +87,7 @@ fn zip_packages_have_correct_extensions_and_no_duplicate_local_entries() { ); } +#[cfg(test)] fn chunk_container(head: &[u8], kind: &[u8], little: bool) -> Vec { let mut bytes = head.to_vec(); let size = 16u32; @@ -197,6 +195,7 @@ fn generated_names_are_readable_and_keep_the_correct_extension() { original_name: None, resident_data: None, ntfs: None, + extents: None, }; assert_eq!(found.recovery_name(3), expected); assert_eq!( diff --git a/tests/ext_block_maps.rs b/tests/ext_block_maps.rs new file mode 100644 index 0000000..a953c94 --- /dev/null +++ b/tests/ext_block_maps.rs @@ -0,0 +1,71 @@ +//! Deleted files on ext2 come back whole from the block maps the kernel +//! leaves behind, even when they were stored in many pieces. +use reclaim::{Event, Found, FoundReader, scan_unused_reader}; +use std::{ + io::{Cursor, Read}, + sync::atomic::AtomicBool, +}; + +/// The document the fixture stored across freed gaps, then deleted. It is +/// longer than one block map covers, so the second map level is needed too. +#[cfg(test)] +fn document() -> Vec { + let mut bytes = b"%PDF-1.4\n".to_vec(); + for line in 0..7000 { + bytes.extend(format!("line {line:05} of the deleted ext2 block map test\n").bytes()); + } + bytes.extend(b"%%EOF\n"); + bytes +} + +#[cfg(test)] +fn fixture() -> Vec { + let path = format!( + "{}/tests/fixtures/deleted-blockmap-ext2.img.gz", + env!("CARGO_MANIFEST_DIR") + ); + let mut bytes = Vec::new(); + flate2::read::GzDecoder::new(std::fs::File::open(path).unwrap()) + .read_to_end(&mut bytes) + .unwrap(); + bytes +} + +#[cfg(test)] +fn scan(image: &[u8]) -> Vec { + let mut found = Vec::new(); + scan_unused_reader( + &mut Cursor::new(image), + image.len() as u64, + &AtomicBool::new(false), + |event| { + if let Event::Found(file) = event { + found.push(file); + } + }, + ) + .unwrap(); + found +} + +#[test] +fn fragmented_deleted_file_returns_whole_from_ext2_block_maps() { + let image = fixture(); + let expected = document(); + let mut input = Cursor::new(&image); + let whole = scan(&image).into_iter().find(|file| { + let mut recovered = Vec::new(); + FoundReader::new(&mut input, file) + .unwrap() + .read_to_end(&mut recovered) + .unwrap(); + recovered == expected + }); + let file = whole.expect("the deleted document did not come back whole"); + assert!( + file.extents + .as_ref() + .is_some_and(|extents| extents.len() > 2), + "the fixture document should be stored in many pieces" + ); +} diff --git a/tests/ext_journal.rs b/tests/ext_journal.rs new file mode 100644 index 0000000..efdea6a --- /dev/null +++ b/tests/ext_journal.rs @@ -0,0 +1,69 @@ +//! Deleted files on ext3/ext4 come back from the inode copies in the journal, +//! whole and with their names, even when they were stored in pieces. +use reclaim::{Event, Found, FoundReader, scan_unused_reader}; +use std::{ + io::{Cursor, Read}, + sync::atomic::AtomicBool, +}; + +/// The document the fixtures stored, split across freed gaps, then deleted. +fn document() -> Vec { + let mut bytes = b"%PDF-1.4\n".to_vec(); + for line in 0..2000 { + bytes.extend(format!("line {line:05} of the deleted journal test document\n").bytes()); + } + bytes.extend(b"%%EOF\n"); + bytes +} + +#[cfg(test)] +fn fixture(name: &str) -> Vec { + let path = format!("{}/tests/fixtures/{name}", env!("CARGO_MANIFEST_DIR")); + let mut bytes = Vec::new(); + flate2::read::GzDecoder::new(std::fs::File::open(path).unwrap()) + .read_to_end(&mut bytes) + .unwrap(); + bytes +} + +#[cfg(test)] +fn scan(image: &[u8]) -> Vec { + let mut found = Vec::new(); + scan_unused_reader( + &mut Cursor::new(image), + image.len() as u64, + &AtomicBool::new(false), + |event| { + if let Event::Found(file) = event { + found.push(file); + } + }, + ) + .unwrap(); + found +} + +#[test] +fn fragmented_deleted_files_return_whole_from_the_ext_journal() { + for name in ["deleted-journal-ext4.img.gz", "deleted-journal-ext3.img.gz"] { + let image = fixture(name); + let found = scan(&image); + let file = found + .iter() + .find(|file| file.original_name.as_deref() == Some("Holiday plan.pdf")) + .unwrap_or_else(|| panic!("{name}: deleted document not found by name")); + assert!( + file.extents + .as_ref() + .is_some_and(|extents| extents.len() > 1), + "{name}: the fixture document should be stored in pieces" + ); + let mut input = Cursor::new(&image); + let mut recovered = Vec::new(); + FoundReader::new(&mut input, file) + .unwrap() + .read_to_end(&mut recovered) + .unwrap(); + assert!(recovered == document(), "{name}: recovered bytes differ"); + } +} diff --git a/tests/fixtures/README.md b/tests/fixtures/README.md index fa5126c..81901b5 100644 --- a/tests/fixtures/README.md +++ b/tests/fixtures/README.md @@ -33,3 +33,25 @@ CRC bit by bit to exercise invalid sequence numbers and packet continuations. while a damaged archive still exposes its independently recoverable image. The `formats/` fixtures are generated by `cargo xtask fixtures` using Rust encoders and explicit on-disk records. They are project-owned, inert parser fixtures under MIT. MSI/MSP/MST contain minimal CFB root metadata, and RPM/DEX contain minimal framing; these are not working installers or applications. PE certificate records are structural examples, not valid signatures. See `docs/FILE_FORMAT_RESEARCH.md` for primary sources and coverage limits. + +`deleted-journal-ext4.img.gz` and `deleted-journal-ext3.img.gz` are 3 MiB +volumes made with `mkfs.ext4`/`mkfs.ext3 -b 1024`, mounted through udisks and +written by the Linux 7.0 kernel driver: the disk was filled with alternating +16 KiB files, every other one was deleted, then a 96 KB generated document +(`Docs/Holiday plan.pdf`, rebuilt by `tests/ext_journal.rs`) was saved into +the gaps and deleted. The driver erased its block list and name; only the +journal still holds them. + +`deleted-blockmap-ext2.img.gz` is a 3 MiB volume made with `mkfs.ext2 -b 1024` +and written by the Linux 7.0 kernel driver the same way, with a 322 KB +generated document (rebuilt by `tests/ext_block_maps.rs`) saved into the gaps +and deleted. Its inode is cleared; only the freed indirect and double-indirect +blocks still list where its pieces are. + +Mounting records the mount point, including the local user name, in each +superblock's "last mounted" field and in superblock copies in the journal. That +field is zeroed in all three images (`debugfs -w -R 'ssv last_mounted ""'` +after zeroing the 64 bytes, which also refreshes the ext4 superblock checksum), +and they are packed with `gzip -9 -n` so the archive holds no file name or +time. The ext4 journal's copy of that block keeps its original journal +checksum; the volume is clean, so the journal is never replayed. diff --git a/tests/fixtures/deleted-blockmap-ext2.img.gz b/tests/fixtures/deleted-blockmap-ext2.img.gz new file mode 100644 index 0000000..3b4febd Binary files /dev/null and b/tests/fixtures/deleted-blockmap-ext2.img.gz differ diff --git a/tests/fixtures/deleted-journal-ext3.img.gz b/tests/fixtures/deleted-journal-ext3.img.gz new file mode 100644 index 0000000..a96fe82 Binary files /dev/null and b/tests/fixtures/deleted-journal-ext3.img.gz differ diff --git a/tests/fixtures/deleted-journal-ext4.img.gz b/tests/fixtures/deleted-journal-ext4.img.gz new file mode 100644 index 0000000..cd5d8ad Binary files /dev/null and b/tests/fixtures/deleted-journal-ext4.img.gz differ diff --git a/tests/image_detection.rs b/tests/image_detection.rs index 04c09ae..32bdad7 100644 --- a/tests/image_detection.rs +++ b/tests/image_detection.rs @@ -1,10 +1,7 @@ -#![expect( - clippy::unwrap_used, - reason = "fixture helpers fail the test by panicking" -)] use reclaim::{Event, Found, Validation, recover_reader, scan_reader}; use std::{io::Cursor, sync::atomic::AtomicBool}; +#[cfg(test)] fn scan(bytes: &[u8]) -> Vec { let mut found = vec![]; scan_reader(&mut Cursor::new(bytes), &AtomicBool::new(false), |event| { diff --git a/tests/large_zip.rs b/tests/large_zip.rs index f609d12..08ffa33 100644 --- a/tests/large_zip.rs +++ b/tests/large_zip.rs @@ -1,7 +1,3 @@ -#![expect( - clippy::unwrap_used, - reason = "fixture helpers fail the test by panicking" -)] use reclaim::{Event, Validation, recover_reader, scan_reader}; use std::{ io::{Read, Seek, SeekFrom, Write}, @@ -9,6 +5,7 @@ use std::{ }; // A real, stored ZIP entry containing 65 MiB of zeros, written sparsely. +#[cfg(test)] fn archive(file: &mut std::fs::File, prefix: u64) -> u64 { let size = 65 * 1024 * 1024u32; let mut crc = crc32fast::Hasher::new(); diff --git a/tests/new_formats.rs b/tests/new_formats.rs index 585cc9e..e8b1b76 100644 --- a/tests/new_formats.rs +++ b/tests/new_formats.rs @@ -1,7 +1,3 @@ -#![expect( - clippy::unwrap_used, - reason = "fixture helpers fail the test by panicking" -)] use reclaim::{Event, Found, Validation, recover_reader, scan_reader}; use std::{ io::{self, Cursor, Read, Seek, SeekFrom}, @@ -37,6 +33,7 @@ const FIXTURES: &[(&str, &[u8])] = &[ ("rar", include_bytes!("fixtures/formats/sample.rar")), ("tar", include_bytes!("fixtures/formats/sample.tar")), ]; +#[cfg(test)] fn collect(input: &mut (impl Read + Seek)) -> Vec { let mut found = Vec::new(); scan_reader(input, &AtomicBool::new(false), |e| { diff --git a/tests/ntfs_resident.rs b/tests/ntfs_resident.rs index 276c295..615b6b8 100644 --- a/tests/ntfs_resident.rs +++ b/tests/ntfs_resident.rs @@ -1,19 +1,17 @@ -#![expect( - clippy::unwrap_used, - clippy::panic, - reason = "fixture helpers fail the test by panicking" -)] use reclaim::{Event, Found, Validation, recover_reader, recover_unique, scan_reader}; use std::{ io::{self, Cursor, Read, Seek, SeekFrom}, sync::atomic::AtomicBool, }; +#[cfg(test)] fn put16(bytes: &mut [u8], at: usize, n: u16) { bytes[at..at + 2].copy_from_slice(&n.to_le_bytes()); } +#[cfg(test)] fn put32(bytes: &mut [u8], at: usize, n: u32) { bytes[at..at + 4].copy_from_slice(&n.to_le_bytes()); } +#[cfg(test)] fn attribute(record: &mut [u8], at: &mut usize, kind: u32, value: &[u8]) -> usize { let start = *at; let length = (24 + value.len()).next_multiple_of(8); @@ -25,6 +23,7 @@ fn attribute(record: &mut [u8], at: &mut usize, kind: u32, value: &[u8]) -> usiz *at += length; start } +#[cfg(test)] fn record(name: &str, contents: &[u8], flags: u16, external: bool) -> Vec { let mut bytes = vec![0; 1024]; bytes[..4].copy_from_slice(b"FILE"); @@ -59,6 +58,7 @@ fn record(name: &str, contents: &[u8], flags: u16, external: bool) -> Vec { } bytes } +#[cfg(test)] fn scan(bytes: &[u8]) -> Vec { let mut found = vec![]; scan_reader(&mut Cursor::new(bytes), &AtomicBool::new(false), |e| { @@ -69,7 +69,9 @@ fn scan(bytes: &[u8]) -> Vec { .unwrap(); found } +#[cfg(test)] struct Unreadable; +#[cfg(test)] impl Read for Unreadable { #[expect( clippy::panic_in_result_fn, @@ -79,6 +81,7 @@ impl Read for Unreadable { panic!("Resident exports must not read the raw source"); } } +#[cfg(test)] impl Seek for Unreadable { #[expect( clippy::panic_in_result_fn, diff --git a/tests/security.rs b/tests/security.rs index 1f88a19..37e5afb 100644 --- a/tests/security.rs +++ b/tests/security.rs @@ -11,6 +11,7 @@ fn candidate() -> Found { original_name: None, resident_data: None, ntfs: None, + extents: None, } } diff --git a/ui/app.slint b/ui/app.slint index 3da82e2..5347031 100644 --- a/ui/app.slint +++ b/ui/app.slint @@ -56,7 +56,7 @@ export global Strings { in-out property recover-in-three-steps: "Recover in three steps"; in-out property <[string]> recovery-step-list; in-out property supported-formats: "Supported formats"; - in-out property photos-documents-archives-audio-video: "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, and NTFS volumes."; + in-out property photos-documents-archives-audio-video: "Photos, documents, archives, audio, video, applications, and small NTFS resident files. Unused-space scanning on FAT, exFAT, NTFS, ext2/3/4, and HFS+ volumes."; in-out property check-for-updates: "Check for updates"; in-out property restart-as-administrator: "Restart as administrator"; in-out property updates: "Updates"; @@ -68,9 +68,17 @@ export global Strings { in-out property project-website: "Project website"; in-out property built-with-rust-and-slint: "Built with Rust and Slint · MIT licensed application"; in-out property stop: "Stop"; + in-out property stop-recovery: "Stop recovery"; + in-out property review-recovery-destination: "Review recovery destination"; + in-out property saving-here-can-overwrite: "Saving here can overwrite the deleted files you are trying to recover and damage them permanently. Choose a folder on a different physical drive, such as a USB drive."; + in-out property saving-to-the-same-drive: "Saving recovered files to the same physical drive you scanned can overwrite and permanently damage data you are trying to recover. Choose a folder on a different physical drive. Continue only if this destination is on another drive."; + in-out property choose-another-folder: "Choose another folder"; + in-out property save-here-anyway: "Save here anyway"; + in-out property cancel: "Cancel"; + in-out property phones-and-cameras-connect-for: "Phones and cameras connect for file transfer only, so Reclaim can’t search them for deleted files. If yours has a memory card, put the card in a card reader and scan it here. Also check its Trash or Recently deleted folder."; } import { CheckBox, LineEdit, ScrollView, ListView as NativeListView, Palette as NativePalette } from "std-widgets.slint"; -export enum DriveIcon { disk, usb, volume, locked } +export enum DriveIcon { disk, usb, volume, locked, phone } export struct DriveItem { name: string, device: string, icon: DriveIcon, detail: string, size: string, available: bool } export struct FileItem { name: string, kind: string, size: string, selected: bool, index: int } export struct ReleaseNoteBlock { text: string, kind: int } @@ -99,6 +107,11 @@ export global Palette { out property primary-hover: dark ? #5c89ea : #3567d3; out property primary-pressed: dark ? #416cc9 : #244ea8; out property danger: dark ? #ffaaa4 : #b42318; + // Filled Stop buttons. White text keeps at least 4.5:1 on each state, and + // the dark fill keeps 3:1 against the card it sits on. + out property stop: dark ? #d13c30 : #c42b1c; + out property stop-hover: dark ? #c0392b : #b42318; + out property stop-pressed: dark ? #b8322a : #a3261a; } // Shared metrics: one control height, one radius scale, spacing in multiples of 4. global Metrics { @@ -193,8 +206,8 @@ component Body inherits Label { color: Palette.muted; font-size: 13px; wrap: wor component Divider inherits Rectangle { height: 1px; background: Palette.line; vertical-stretch: 0; } component Card inherits Rectangle { clip: true; background: Palette.surface; border-radius: 12px; border-width: 1px; border-color: Palette.line; } -// One button component: filled for the single primary action of a page, outlined otherwise, -// borderless (`quiet`) for low-emphasis actions. +// One button component: filled for the single primary action of a page, filled red (`stop`) +// for stopping work in progress, outlined otherwise, borderless (`quiet`) for low-emphasis actions. // Rotate the path coordinates: the software renderer does not implement item rotation. component RefreshSpin inherits Path { private property phase: mod(animation-tick() / 1ms, 700) / 700 * 360deg; @@ -212,11 +225,15 @@ component Action inherits Rectangle { in property text; in property icon; in property primary; + in property stop; in property quiet; in property enabled: true; in property spinning; + // Never narrower than its caption: for dialogs, where a cut-off choice is unreadable. + in property fit; + private property filled: root.primary || root.stop; callback clicked(); - height: Metrics.control; min-width: 64px; horizontal-stretch: 0; + height: Metrics.control; min-width: root.fit ? max(64px, root.preferred-width) : 64px; horizontal-stretch: 0; preferred-width: caption.preferred-width + (root.icon.width > 0 ? 26px : 0px) + 30px; forward-focus: focus; accessible-role: button; @@ -224,9 +241,10 @@ component Action inherits Rectangle { accessible-enabled: root.enabled; accessible-action-default => { if root.enabled { root.clicked(); } } border-radius: Metrics.radius; - border-width: focus.has-focus ? 2px : primary || quiet ? 0px : 1px; + border-width: focus.has-focus ? 2px : root.filled || quiet ? 0px : 1px; border-color: focus.has-focus ? Palette.accent : Palette.line; - background: primary ? (touch.pressed ? Palette.primary-pressed : touch.has-hover ? Palette.primary-hover : Palette.primary) + background: stop ? (touch.pressed ? Palette.stop-pressed : touch.has-hover ? Palette.stop-hover : Palette.stop) + : primary ? (touch.pressed ? Palette.primary-pressed : touch.has-hover ? Palette.primary-hover : Palette.primary) : touch.pressed ? Palette.line : touch.has-hover ? Palette.subtle : quiet ? transparent : Palette.surface; animate background { duration: 120ms; } opacity: enabled || spinning ? 1 : 0.45; @@ -238,10 +256,10 @@ component Action inherits Rectangle { } Row { padding-left: 14px; padding-right: 14px; spacing: 8px; if root.icon.width > 0 : Rectangle { width: 18px; height: 18px; - if root.spinning : RefreshSpin { width: parent.width; height: parent.height; stroke: root.primary ? white : Palette.accent; } - if !root.spinning : Image { width: parent.width; height: parent.height; source: root.icon; colorize: root.primary ? white : Palette.accent; } + if root.spinning : RefreshSpin { width: parent.width; height: parent.height; stroke: root.filled ? white : Palette.accent; } + if !root.spinning : Image { width: parent.width; height: parent.height; source: root.icon; colorize: root.filled ? white : Palette.accent; } } - caption := Label { font-size: 13px; font-weight: root.primary ? 600 : 500; min-width: 0px; horizontal-stretch: 1; overflow: elide; text: root.text; color: root.primary ? white : Palette.ink; vertical-alignment: center; } + caption := Label { font-size: 13px; font-weight: root.filled ? 600 : 500; min-width: 0px; horizontal-stretch: 1; overflow: elide; text: root.text; color: root.filled ? white : Palette.ink; vertical-alignment: center; } } touch := TouchArea { enabled: root.enabled; mouse-cursor: pointer; clicked => { focus.focus(); root.clicked(); } } } @@ -443,10 +461,11 @@ component ScanStatus inherits VerticalLayout { spacing: 10px; Row { spacing: 12px; min-height: 28px; Rectangle { width: 8px; height: 8px; border-radius: 4px; background: root.error != "" ? Palette.danger : root.busy ? Palette.primary : Palette.line; } - // One steady line while busy; longer results after a scan may wrap. - headline := Label { text: root.status; color: Palette.ink; font-size: 13px; wrap: root.busy ? TextWrap.no-wrap : TextWrap.word-wrap; overflow: elide; vertical-alignment: center; min-width: 0px; horizontal-stretch: 1; } + // One steady line while busy; longer results after a scan may wrap. A zero preferred + // width makes it take only the leftover space, so its width never follows the text. + headline := Label { text: root.status; color: Palette.ink; font-size: 13px; wrap: root.busy ? TextWrap.no-wrap : TextWrap.word-wrap; overflow: elide; vertical-alignment: center; min-width: 0px; preferred-width: 0px; horizontal-stretch: 1; } if root.busy && root.detail != "" : Label { text: root.detail; color: Palette.secondary; font-size: 13px; vertical-alignment: center; horizontal-stretch: 0; } - if root.busy : Action { height: 28px; text: Strings.stop; clicked => { root.stop(); } } + if root.busy : Action { height: 28px; text: Strings.stop; icon: @image-url("icons/stop.svg"); stop: true; clicked => { root.stop(); } } } if root.busy || root.progress > 0 : Rectangle { height: 4px; background: Palette.line; border-radius: 2px; clip: true; if root.busy && root.progress == 0 : Rectangle { x: (parent.width + self.width) * mod(animation-tick() / 1ms, 1800) / 1800 - self.width; width: parent.width / 4; border-radius: 2px; background: Palette.primary; } @@ -528,12 +547,18 @@ export component AppWindow inherits Window { in-out property preview-token; in property busy; in property drives-refreshing; + // A phone or camera is connected; the Sources page explains why it cannot be scanned. + in property phone-connected; in-out property refresh-feedback; Timer { interval: 700ms; running: root.refresh-feedback; triggered => { root.refresh-feedback = false; } } in-out property update-check-feedback; Timer { interval: 700ms; running: root.update-check-feedback; triggered => { root.update-check-feedback = false; } } in property can-scan; in property can-recover; + // Recovery runs on its own, also while a scan continues. + in property recovering; + // The latest recovery message; empty shows the destination hint instead. + in property recovery-status; // Every visible result is ticked (drives the list's "Select all" box). in-out property all-selected; // The scan produced results, so an empty list means the filters hide them. @@ -565,6 +590,12 @@ export component AppWindow inherits Window { callback apply-update(); callback dismiss-update(); callback open-image(); callback refresh(); callback select-drive(int); callback scan(); callback stop(); + // A recovery folder that may share a drive with the scanned source waits here + // for the user's decision. A same-drive folder can only be replaced, never accepted. + in property destination-review; + in property destination-same-drive; + in property destination-path; + callback choose-another-destination(); callback save-here-anyway(); callback cancel-destination(); callback recover(); callback toggle-file(int); callback inspect(int); callback select-all(bool); callback filter(); callback check-update(); Row { cross-axis-alignment: stretch; // ── Sidebar ───────────────────────────────────────────── @@ -633,10 +664,11 @@ export component AppWindow inherits Window { animate background { duration: 120ms; } Row { padding-left: 8px; padding-right: 12px; spacing: 10px; Rectangle { width: 32px; height: 32px; border-radius: Metrics.radius; background: root.selected-drive == i ? Palette.surface : Palette.subtle; - Image { width: 18px; height: 18px; colorize: drive.icon == DriveIcon.locked ? Palette.muted : Palette.accent; + Image { width: 18px; height: 18px; colorize: drive.icon == DriveIcon.locked || drive.icon == DriveIcon.phone ? Palette.muted : Palette.accent; source: drive.icon == DriveIcon.usb ? @image-url("icons/usb.svg") : drive.icon == DriveIcon.volume ? @image-url("icons/volume.svg") : drive.icon == DriveIcon.locked ? @image-url("icons/lock.svg") + : drive.icon == DriveIcon.phone ? @image-url("icons/phone.svg") : @image-url("icons/drive.svg"); } } @@ -666,6 +698,11 @@ export component AppWindow inherits Window { } } } } + // Phones are listed but cannot be scanned; say why and what works instead. + if root.phone-connected : Row { spacing: 8px; padding-left: 4px; padding-right: 4px; + VerticalLayout { padding-top: 1px; alignment: start; Image { source: @image-url("icons/phone.svg"); width: 16px; height: 16px; colorize: Palette.muted; } } + phone-note := Label { text: Strings.phones-and-cameras-connect-for; color: Palette.secondary; font-size: 12px; wrap: word-wrap; min-width: 0px; horizontal-stretch: 1; } + } Rectangle { vertical-stretch: 1; } // One action bar: what will be scanned, the button that scans it, and, // only while something runs or failed, the scan status. @@ -691,11 +728,16 @@ export component AppWindow inherits Window { } Row { cross-axis-alignment: stretch; vertical-stretch: 1; spacing: 12px; Card { horizontal-stretch: 1; min-width: 0px; VerticalLayout { - // List header: one "select all" box above the row boxes, and the counts. + // List header: one "select all" box above the row boxes, with the counts + // under its caption. Stacked, each line gets the full width of the narrow + // list column, so neither is cut off in German at the compact size. They + // never elide; the layout tests check both stay inside the list column. Row { height: 44px; vertical-stretch: 0; padding-left: 18px; padding-right: 18px; spacing: 10px; - CheckBox { horizontal-stretch: 0; enabled: root.files.length > 0 && !root.busy; checked <=> root.all-selected; accessible-label: Strings.select-all; toggled => { root.select-all(self.checked); } } - Label { text: Strings.select-all; color: root.files.length > 0 ? Palette.ink : Palette.muted; font-size: 13px; font-weight: 500; vertical-alignment: center; min-width: 0px; horizontal-stretch: 1; overflow: elide; } - Label { text: root.summary; color: Palette.muted; font-size: 12px; vertical-alignment: center; horizontal-stretch: 0; } + CheckBox { horizontal-stretch: 0; enabled: root.files.length > 0; checked <=> root.all-selected; accessible-label: Strings.select-all; toggled => { root.select-all(self.checked); } } + VerticalLayout { horizontal-stretch: 1; alignment: center; spacing: 1px; + select-all-caption := Label { text: Strings.select-all; color: root.files.length > 0 ? Palette.ink : Palette.muted; font-size: 13px; font-weight: 500; min-width: self.preferred-width; } + list-summary := Label { text: root.summary; color: Palette.muted; font-size: 12px; min-width: self.preferred-width; } + } } Divider { } Rectangle { vertical-stretch: 1; @@ -747,9 +789,11 @@ export component AppWindow inherits Window { Row { height: 40px; cross-axis-alignment: center; spacing: 12px; Row { horizontal-stretch: 1; min-width: 0px; spacing: 8px; padding-left: 4px; padding-right: 4px; Image { source: @image-url("icons/shield.svg"); width: 16px; height: 16px; colorize: Palette.muted; } - Label { text: Strings.save-to-a-different-physical-drive; color: Palette.muted; font-size: 12px; vertical-alignment: center; min-width: 0px; horizontal-stretch: 1; overflow: elide; } + // Recovery messages may take two lines; the row keeps its height. + recovery-message := Label { text: root.recovery-status != "" ? root.recovery-status : Strings.save-to-a-different-physical-drive; color: root.recovery-status != "" ? Palette.ink : Palette.muted; font-size: 12px; vertical-alignment: center; min-width: 0px; height: 30px; horizontal-stretch: 1; wrap: word-wrap; overflow: elide; } } - Action { width: 264px; height: 40px; text: Strings.recover-selected; icon: @image-url("icons/reclaim.svg"); primary: true; enabled: root.can-recover; clicked => { root.recover(); } } + // While recovering, the same button turns red and stops the recovery. + Action { width: 264px; height: 40px; text: root.recovering ? Strings.stop-recovery : Strings.recover-selected; icon: root.recovering ? @image-url("icons/stop.svg") : @image-url("icons/reclaim.svg"); primary: !root.recovering; stop: root.recovering; enabled: root.can-recover; clicked => { root.recover(); } } } } // Settings & help: preferences, then guidance, then version and project details. @@ -852,6 +896,41 @@ export component AppWindow inherits Window { } } } + // Recovery destination review: same backdrop and card as the update notice. It names + // the chosen folder, explains the risk and offers the safe choice first. Escape cancels. + if root.destination-review : destination-keys := FocusScope { + x: 0px; y: 0px; width: root.width; height: root.height; + init => { self.focus(); } + key-pressed(event) => { + if event.text == Key.Escape { root.cancel-destination(); return accept; } + return reject; + } + Rectangle { background: #00000066; TouchArea { } } + destination-card := Rectangle { + x: (parent.width - self.width) / 2; + y: (parent.height - self.height) / 2; + width: min(620px, parent.width - 40px); height: min(destination-content.preferred-height, parent.height - 40px); + clip: true; + background: Palette.surface; border-radius: 12px; border-width: 1px; border-color: Palette.line; + drop-shadow-blur: 24px; drop-shadow-offset-y: 6px; drop-shadow-color: #00000050; + destination-content := VerticalLayout { padding: 20px; spacing: 14px; + Row { spacing: 12px; + IconTile { icon: @image-url("icons/drive.svg"); } + VerticalLayout { horizontal-stretch: 1; min-width: 0px; spacing: 2px; alignment: center; + Label { text: Strings.review-recovery-destination; color: Palette.ink; font-size: 16px; font-weight: 700; wrap: word-wrap; } + destination-folder := Label { text: root.destination-path; color: Palette.muted; font-size: 12px; overflow: elide; min-width: 0px; } + } + } + Body { text: root.destination-same-drive ? Strings.saving-here-can-overwrite : Strings.saving-to-the-same-drive; color: Palette.secondary; } + Row { spacing: 8px; + Rectangle { horizontal-stretch: 1; } + Action { text: Strings.cancel; quiet: true; fit: true; clicked => { root.cancel-destination(); } } + if !root.destination-same-drive : Action { text: Strings.save-here-anyway; fit: true; clicked => { root.save-here-anyway(); } } + Action { text: Strings.choose-another-folder; icon: @image-url("icons/files.svg"); primary: true; fit: true; clicked => { root.choose-another-destination(); } } + } + } + } + } // The page column's title bar, drawn over its top padding. if root.custom-frame : TitleBar { x: Locale.rtl ? 0px : 233px; diff --git a/ui/icons/phone.svg b/ui/icons/phone.svg new file mode 100644 index 0000000..b9ca335 --- /dev/null +++ b/ui/icons/phone.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/ui/icons/stop.svg b/ui/icons/stop.svg new file mode 100644 index 0000000..b96766c --- /dev/null +++ b/ui/icons/stop.svg @@ -0,0 +1 @@ + diff --git a/website/vercel.json b/website/vercel.json index c58a302..63d7daf 100644 --- a/website/vercel.json +++ b/website/vercel.json @@ -2,7 +2,7 @@ "$schema": "https://openapi.vercel.sh/vercel.json", "framework": "astro", "buildCommand": "npm run build", - "ignoreCommand": "git diff --quiet \"${VERCEL_GIT_PREVIOUS_SHA:-HEAD^}\" HEAD -- .", + "ignoreCommand": "base=\"$VERCEL_GIT_PREVIOUS_SHA\"; if [ -n \"$base\" ] && git cat-file -e \"$base^{commit}\" 2>/dev/null && git diff --quiet \"$base\" HEAD -- .; then exit 0; else exit 1; fi", "outputDirectory": "dist", "trailingSlash": true, "redirects": [