24 Commits
main ... v0.3.0

Author SHA1 Message Date
sreedevk
e41e5d35fb typo fix 2025-07-13 23:02:06 +00:00
Sreedev Kodichath
884f88d749 Update README.md 2025-07-13 19:00:58 -04:00
sreedevk
ab15cdfcd7 added benchmarks 2025-07-13 22:53:26 +00:00
sreedevk
7c5b106784 add more goals on readme 2025-07-13 21:42:22 +00:00
sreedevk
add53a3be1 updates documentation 2025-07-13 21:40:18 +00:00
sreedevk
d99d783327 add more tests 2025-07-13 21:24:31 +00:00
sreedevk
ed55aaa005 speeeeeeeeeeed 2025-07-13 20:21:17 +00:00
sreedevk
fe79a07b43 changed hash type to u128 2025-07-13 18:11:07 +00:00
sreedevk
47b2fc4a87 move max path size update to a later stage in the pipeline for accurate results 2025-07-13 14:24:24 +00:00
sreedevk
184efdcbbc simplify scanner 2025-07-13 14:02:19 +00:00
sreedevk
81e13a14a2 reduce memuse: fileinfo uses boxed path instead of pathbuf
reduce memuse: fileinfo does not store full metadata
2025-07-13 13:45:30 +00:00
sreedevk
5daec2f531 multi progress box also hidden if --progress not set 2025-07-13 13:37:03 +00:00
sreedevk
fcf69ae1cc progress bar issues fixed with multiprogress 2025-07-13 13:21:20 +00:00
sreedevk
3a03208f47 parallelization progress representation on readme updated 2025-07-13 13:05:34 +00:00
sreedevk
95ecfa0ccc disable progress sprinners by default 2025-07-13 12:59:22 +00:00
sreedevk
ce115fb8dc better organization of interactive module 2025-07-13 12:47:17 +00:00
sreedevk
3785a87cee add more goals to readme 2025-07-13 12:44:49 +00:00
sreedevk
d42166a4fd added first test 2025-07-12 22:23:37 +00:00
sreedevk
4d85f0bbc9 memory usage improvements 2025-07-12 21:30:05 +00:00
sreedevk
8826a87f32 added strict mode with partial hashing default 2025-07-12 21:17:00 +00:00
sreedevk
4aa536e89b hashing restored 2025-07-12 21:04:43 +00:00
sreedevk
9d67a91423 formatter speed improved (new bug: does not output hash) 2025-07-12 20:50:04 +00:00
sreedevk
4a6aadb78e parallelization improvements 2025-07-12 20:21:44 +00:00
sreedevk
1b06eb8e85 multithreading improvements 2025-07-12 18:26:10 +00:00
13 changed files with 559 additions and 989 deletions

View File

@@ -1,121 +1,137 @@
name: Deduplicator Release Build CI Pipeline # CI that:
#
# * checks for a Git Tag that looks like a release
# * creates a Github Release™ and fills in its text
# * builds artifacts with cargo-dist (executable-zips, installers)
# * uploads those artifacts to the Github Release™
#
# Note that the Github Release™ will be created before the artifacts,
# so there will be a few minutes where the release has no artifacts
# and then they will slowly trickle in, possibly failing. To make
# this more pleasant we mark the release as a "draft" until all
# artifacts have been successfully uploaded. This allows you to
# choose what to do with partial successes and avoids spamming
# anyone with notifications before the release is actually ready.
name: Release
permissions:
contents: write
# This task will run whenever you push a git tag that looks like a version
# like "v1", "v1.2.0", "v0.1.0-prerelease01", "my-app-v1.0.0", etc.
# The version will be roughly parsed as ({PACKAGE_NAME}-)?v{VERSION}, where
# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION
# must be a Cargo-style SemVer Version.
#
# If PACKAGE_NAME is specified, then we will create a Github Release™ for that
# package (erroring out if it doesn't have the given version or isn't cargo-dist-able).
#
# If PACKAGE_NAME isn't specified, then we will create a Github Release™ for all
# (cargo-dist-able) packages in the workspace with that version (this is mode is
# intended for workspaces with only one dist-able package, or with all dist-able
# packages versioned/released in lockstep).
#
# If you push multiple tags at once, separate instances of this workflow will
# spin up, creating an independent Github Release™ for each one.
#
# If there's a prerelease-style suffix to the version then the Github Release™
# will be marked as a prerelease.
on: on:
push: push:
tags: tags:
- 'v*.*.*' - '*-?v[0-9]+*'
jobs: jobs:
lint_test: # Create the Github Release™ so the packages have something to be uploaded to
create-release:
runs-on: ubuntu-latest runs-on: ubuntu-latest
outputs:
has-releases: ${{ steps.create-release.outputs.has-releases }}
env: env:
RUSTFLAGS: "-C target-feature=+aes,+sse2" GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps: steps:
- name: Checkout repository - uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust - name: Install Rust
uses: dtolnay/rust-toolchain@stable run: rustup update 1.71.0 --no-self-update && rustup default 1.71.0
with: - name: Install cargo-dist
components: clippy run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh
- id: create-release
run: |
cargo dist plan --tag=${{ github.ref_name }} --output-format=json > dist-manifest.json
echo "dist plan ran successfully"
cat dist-manifest.json
- name: Run cargo check # Create the Github Release™ based on what cargo-dist thinks it should be
run: cargo check ANNOUNCEMENT_TITLE=$(jq --raw-output ".announcement_title" dist-manifest.json)
IS_PRERELEASE=$(jq --raw-output ".announcement_is_prerelease" dist-manifest.json)
jq --raw-output ".announcement_github_body" dist-manifest.json > new_dist_announcement.md
gh release create ${{ github.ref_name }} --draft --prerelease="$IS_PRERELEASE" --title="$ANNOUNCEMENT_TITLE" --notes-file=new_dist_announcement.md
echo "created announcement!"
- name: Run cargo clippy # Upload the manifest to the Github Release™
run: cargo clippy -- -D warnings gh release upload ${{ github.ref_name }} dist-manifest.json
echo "uploaded manifest!"
- name: Run tests # Disable all the upload-artifacts tasks if we have no actual releases
run: cargo test -- --test-threads=1 HAS_RELEASES=$(jq --raw-output ".releases != null" dist-manifest.json)
echo "has-releases=$HAS_RELEASES" >> "$GITHUB_OUTPUT"
build: # Build and packages all the things
needs: lint_test upload-artifacts:
runs-on: ${{ matrix.os }} # Let the initial task tell us to not run (currently very blunt)
needs: create-release
if: ${{ needs.create-release.outputs.has-releases == 'true' }}
strategy: strategy:
matrix: matrix:
# For these target platforms
include: include:
- target: aarch64-unknown-linux-gnu - os: macos-11
os: ubuntu-latest dist-args: --artifacts=local --target=aarch64-apple-darwin --target=x86_64-apple-darwin
artifact_name: linux-aarch64 install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh
rustflags: "-C target-feature=+aes,+neon" - os: ubuntu-20.04
- target: x86_64-unknown-linux-gnu dist-args: --artifacts=local --target=x86_64-unknown-linux-gnu
os: ubuntu-latest install-dist: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.sh | sh
artifact_name: linux-amd64 - os: windows-2019
rustflags: "-C target-feature=+aes,+sse2" dist-args: --artifacts=local --target=x86_64-pc-windows-msvc
- target: x86_64-apple-darwin install-dist: irm https://github.com/axodotdev/cargo-dist/releases/download/v0.0.7/cargo-dist-installer.ps1 | iex
os: macos-14
artifact_name: macos-amd64
rustflags: "-C target-feature=+aes,+sse2"
- target: aarch64-apple-darwin
os: macos-14
artifact_name: macos-arm64
rustflags: "-C target-feature=+aes,+neon"
runs-on: ${{ matrix.os }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps: steps:
- name: Checkout repository - uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Rust - name: Install Rust
uses: dtolnay/rust-toolchain@stable run: rustup update 1.71.0 --no-self-update && rustup default 1.71.0
- name: Install cargo-dist
- name: Add target run: ${{ matrix.install-dist }}
run: rustup target add ${{ matrix.target }} - name: Run cargo-dist
# This logic is a bit janky because it's trying to be a polyglot between
- name: Install cross-compilation toolchain (Linux ARM only) # powershell and bash since this will run on windows, macos, and linux!
if: matrix.target == 'aarch64-unknown-linux-gnu' # The two platforms don't agree on how to talk about env vars but they
# do agree on 'cat' and '$()' so we use that to marshal values between commands.
run: | run: |
sudo apt-get update # Actually do builds and make zips and whatnot
sudo apt-get install -y gcc-aarch64-linux-gnu cargo dist build --tag=${{ github.ref_name }} --output-format=json ${{ matrix.dist-args }} > dist-manifest.json
echo "dist ran successfully"
cat dist-manifest.json
- name: Build release binary # Parse out what we just built and upload it to the Github Release™
env: jq --raw-output ".artifacts[]?.path | select( . != null )" dist-manifest.json > uploads.txt
RUSTFLAGS: ${{ matrix.rustflags }} echo "uploading..."
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: ${{ matrix.target == 'aarch64-unknown-linux-gnu' && 'aarch64-linux-gnu-gcc' || '' }} cat uploads.txt
run: | gh release upload ${{ github.ref_name }} $(cat uploads.txt)
if [ "${{ matrix.target }}" = "aarch64-unknown-linux-gnu" ]; then echo "uploaded!"
export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
fi
cargo build --release --target ${{ matrix.target }}
- name: Package binary # Mark the Github Release™ as a non-draft now that everything has succeeded!
run: | publish-release:
if [[ "${{ matrix.os }}" == macos* ]]; then # Only run after all the other tasks, but it's ok if upload-artifacts was skipped
BINARY_NAME=$(find target/${{ matrix.target }}/release -maxdepth 1 -type f -perm +111 -print0 | xargs -0 basename | head -1) needs: [create-release, upload-artifacts]
else if: ${{ always() && needs.create-release.result == 'success' && (needs.upload-artifacts.result == 'skipped' || needs.upload-artifacts.result == 'success') }}
BINARY_NAME=$(find target/${{ matrix.target }}/release -maxdepth 1 -type f -executable -print0 | xargs -0 basename | head -1)
fi
echo "Packaging binary: $BINARY_NAME"
mkdir -p release
cp target/${{ matrix.target }}/release/$BINARY_NAME release/$BINARY_NAME
tar -C release -czf ${{ matrix.artifact_name }}.tar.gz $BINARY_NAME
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.artifact_name }}-binary
path: ${{ matrix.artifact_name }}.tar.gz
create_release:
needs: build
runs-on: ubuntu-latest runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps: steps:
- name: Checkout repository - uses: actions/checkout@v3
uses: actions/checkout@v4 - name: mark release as non-draft
run: |
- name: Download all artifacts gh release edit ${{ github.ref_name }} --draft=false
uses: actions/download-artifact@v4
with:
path: artifacts
- name: Create GitHub Release
id: create_release
uses: softprops/action-gh-release@v1
with:
tag_name: ${{ github.ref_name }}
name: Release ${{ github.ref_name }}
body: "Automated release for version ${{ github.ref_name }}"
files: |
artifacts/*/*.tar.gz
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

574
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "deduplicator" name = "deduplicator"
version = "0.3.2" version = "0.3.0"
edition = "2021" edition = "2021"
description = "find,filter and delete duplicate files" description = "find,filter and delete duplicate files"
repository = "https://github.com/sreedevk/deduplicator" repository = "https://github.com/sreedevk/deduplicator"
@@ -18,20 +18,19 @@ path = "src/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
anyhow = "1.0.68" anyhow = "1.0.68"
bytesize = "2.0.1" bytesize = "1.1.0"
chrono = "0.4.23" chrono = "0.4.23"
clap = { version = "4.0.32", features = ["derive"] } clap = { version = "4.0.32", features = ["derive"] }
dashmap = { version = "6.1.0", features = ["rayon"] } dashmap = { version = "5.4.0", features = ["rayon"] }
globwalk = "0.9.1" globwalk = "0.8.1"
gxhash = { version = "3.4.1", default-features = false } gxhash = { version = "3.4.1", default-features = false }
indicatif = { version = "0.18.0", features = ["rayon"] } indicatif = { version = "0.17.2", features = ["rayon"] }
memmap2 = "0.9.7" memmap2 = "0.5.8"
pathdiff = "0.2.1" pathdiff = "0.2.1"
prettytable-rs = "0.10.0" prettytable-rs = "0.10.0"
rand = "0.9.1" rand = "0.9.1"
rayon = "1.6.1" rayon = "1.6.1"
threadpool = "1.8.1" threadpool = "1.8.1"
unicode-segmentation = "1.12.0"
[profile.release] [profile.release]
strip = true strip = true

176
README.md
View File

@@ -4,9 +4,6 @@
Find, Sort, Filter & Delete duplicate files Find, Sort, Filter & Delete duplicate files
</p> </p>
> [!NOTE]
> This project is maintained with the assistance of AI tools. All changes are subject to manual review and a comprehensive test suite to ensure stability and quality.
## Usage ## Usage
```bash ```bash
@@ -18,17 +15,16 @@ Arguments:
[scan_dir_path] Run Deduplicator on dir different from pwd (e.g., ~/Pictures ) [scan_dir_path] Run Deduplicator on dir different from pwd (e.g., ~/Pictures )
Options: Options:
-T, --exclude-types <EXCLUDE_TYPES> Exclude Filetypes [default = none] -t, --types <TYPES> Filetypes to deduplicate [default = all]
-t, --types <TYPES> Filetypes to deduplicate [default = all] -i, --interactive Delete files interactively
-i, --interactive Delete files interactively -m, --min-size <MIN_SIZE> Minimum filesize of duplicates to scan (e.g., 100B/1K/2M/3G/4T) [default: 1b]
-m, --min-size <MIN_SIZE> Minimum filesize of duplicates to scan (e.g., 100B/1K/2M/3G/4T) [default: 1b] -D, --max-depth <MAX_DEPTH> Max Depth to scan while looking for duplicates
-D, --max-depth <MAX_DEPTH> Max Depth to scan while looking for duplicates -d, --min-depth <MIN_DEPTH> Min Depth to scan while looking for duplicates
-d, --min-depth <MIN_DEPTH> Min Depth to scan while looking for duplicates -f, --follow-links Follow links while scanning directories
-f, --follow-links Follow links while scanning directories -s, --strict Guarantees that two files are duplicate (performs a full hash)
-s, --strict Guarantees that two files are duplicate (performs a full hash) -p, --progress Show Progress spinners & metrics
-p, --progress Show Progress spinners & metrics -h, --help Print help
-h, --help Print help -V, --version Print version
-V, --version Print version
``` ```
### Examples ### Examples
@@ -36,9 +32,6 @@ Options:
# Scan for duplicates recursively from the current dir, only look for png, jpg & pdf file types & interactively delete files # Scan for duplicates recursively from the current dir, only look for png, jpg & pdf file types & interactively delete files
deduplicator -t pdf,jpg,png -i deduplicator -t pdf,jpg,png -i
# Scan for duplicates recursively from current dir, exclude png and jpg file types
deduplicator -T jpg,png
# Scan for duplicates recursively from the ~/Pictures dir, only look for png, jpeg, jpg & pdf file types & interactively delete files # Scan for duplicates recursively from the ~/Pictures dir, only look for png, jpeg, jpg & pdf file types & interactively delete files
deduplicator ~/Pictures/ -t png,jpeg,jpg,pdf -i deduplicator ~/Pictures/ -t png,jpeg,jpg,pdf -i
@@ -53,8 +46,7 @@ deduplicator ~/Media --min-size 100mb
``` ```
## Demo ## Demo
![demo](https://github.com/user-attachments/assets/bdb95831-542d-4902-a458-4e0f5d171a33) ![record](https://github.com/user-attachments/assets/fcfdd9bf-4d05-41b6-a82e-367634eeaa73)
@@ -65,31 +57,16 @@ Currently, you can only install deduplicator using cargo package manager.
> GxHash relies on aes hardware acceleration, so please set `RUSTFLAGS` to `"-C target-feature=+aes"` or `"-C target-cpu=native"` before > GxHash relies on aes hardware acceleration, so please set `RUSTFLAGS` to `"-C target-feature=+aes"` or `"-C target-cpu=native"` before
> installing. > installing.
#### install from crates.io (stable) #### install from crates.io
```bash ```bash
$ RUSTFLAGS="-C target-cpu=native" cargo install deduplicator $ RUSTFLAGS="-C target-cpu=native" cargo install deduplicator
# or
$ RUSTFLAGS="-C target-feature=+aes,+sse2" cargo install deduplicator
``` ```
#### install from git (nightly) #### install from git
```bash ```bash
$ RUSTFLAGS="-C target-cpu=native" cargo install deduplicator --git https://github.com/sreedevk/deduplicator $ RUSTFLAGS="-C target-cpu=native" cargo install deduplicator --git https://github.com/sreedevk/deduplicator
# or
$ RUSTFLAGS="-C target-feature=+aes,+sse2" cargo install --git https://github.com/sreedevk/deduplicator
``` ```
### Manual Installation
- Download the right pre-compiled binary archive for your platform from [github release page](https://github.com/sreedevk/deduplicator/releases/tag/latest).
- Decompress it using `tar -zxvf <archive>.tar.gz`
- Move it to a directory included in `$PATH`.
- ideally `/usr/local/bin/`.
## Performance ## Performance
Deduplicator uses size comparison and [GxHash](https://docs.rs/gxhash/latest/gxhash/) to quickly check a large number of files to find duplicates. its also heavily parallelized. The default behavior of deduplicator is to only hash the first page (4K) of the file. This is to ensure that performance is the default priority. You can modify this behavior by using the `--strict` flag which will hash the whole file and ensure that 2 files are indeed duplicates. I'll add benchmarks in future versions. Deduplicator uses size comparison and [GxHash](https://docs.rs/gxhash/latest/gxhash/) to quickly check a large number of files to find duplicates. its also heavily parallelized. The default behavior of deduplicator is to only hash the first page (4K) of the file. This is to ensure that performance is the default priority. You can modify this behavior by using the `--strict` flag which will hash the whole file and ensure that 2 files are indeed duplicates. I'll add benchmarks in future versions.
@@ -100,108 +77,77 @@ I've used hyperfine to run deduplicator on files generated by the rake file at `
``` ```
# hyperfine -N --warmup 80 './target/release/deduplicator bench_artifacts' # hyperfine -N --warmup 80 './target/release/deduplicator bench_artifacts'
Benchmark 1: ./target/release/deduplicator bench_artifacts Benchmark 1: ./target/release/deduplicator bench_artifacts
Time (mean ± σ): 2.2 ms ± 0.4 ms [User: 2.2 ms, System: 4.4 ms] Time (mean ± σ): 2.5 ms ± 0.4 ms [User: 2.2 ms, System: 4.8 ms]
Range (min … max): 1.3 ms … 7.1 ms 1522 runs Range (min … max): 1.9 ms … 6.8 ms 1322 runs
dust 'bench_artifacts' # dust 'bench_artifacts'
37M ┌── file_1_fwds.bin │██ │ 1%
54M ── file_0_fwds.bin │████ │ 2% 201M ── file_0_fwds.bin │██████████ 8%
122M ├── file_1_fwds.bin │████████ 5% 390M ├── file_0_fwdcbss.bin│████████████████████ │ 15%
390M ├── file_0_fwdcbss.bin│██████████████████████████ │ 15% 390M ├── file_0_fwscas.bin │████████████████████ │ 15%
390M ├── file_0_fwscas.bin │██████████████████████████ │ 15% 390M ├── file_0_fwss.bin │████████████████████ │ 15%
390M ├── file_0_fwss.bin │██████████████████████████ │ 15% 390M ├── file_1_fwdcbss.bin│████████████████████ │ 15%
390M ├── file_1_fwdcbss.bin│██████████████████████████ │ 15% 390M ├── file_1_fwscas.bin │████████████████████ │ 15%
390M ├── file_1_fwscas.bin │██████████████████████████ │ 15% 390M ├── file_1_fwss.bin │████████████████████ │ 15%
390M ├── file_1_fwss.bin │██████████████████████████ 15% 2.5G ┌─┴ bench_artifacts │███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ │ 100%
2.5G ┌─┴ bench_artifacts │██████████████████████████████████████████████████████████████████ │ 100%
``` ```
#### Many Small Files #### Many Small Files
``` ```
# hyperfine --warmup 20 './target/release/deduplicator bench_artifacts' # hyperfine --warmup 20 './target/release/deduplicator bench_artifacts'
Benchmark 1: ./target/release/deduplicator bench_artifacts Benchmark 1: ./target/release/deduplicator bench_artifacts
Time (mean ± σ): 40.1 ms ± 2.3 ms [User: 251.0 ms, System: 277.3 ms] Time (mean ± σ): 22.3 ms ± 1.7 ms [User: 35.8 ms, System: 46.3 ms]
Range (min … max): 35.0 ms … 45.9 ms 72 runs Range (min … max): 18.9 ms … 27.2 ms 112 runs
dust 'bench_artifacts' # dust 'bench_artifacts'
3.9M ┌── file_992_fwscas.bin │█ │ 0% 3.9M ┌── file_992_fwss.bin │█ │ 0%
3.9M ├── file_992_fwss.bin │█ │ 0% 3.9M ├── file_993_fwdcbss.bin│█ │ 0%
3.9M ├── file_993_fwdcbss.bin│█ │ 0% 3.9M ├── file_993_fwscas.bin │█ │ 0%
3.9M ├── file_993_fwscas.bin │█ │ 0% 3.9M ├── file_993_fwss.bin │█ │ 0%
3.9M ├── file_993_fwss.bin │█ │ 0% 3.9M ├── file_994_fwdcbss.bin│█ │ 0%
3.9M ├── file_994_fwdcbss.bin│█ │ 0% 3.9M ├── file_994_fwscas.bin │█ │ 0%
3.9M ├── file_994_fwscas.bin │█ │ 0% 3.9M ├── file_994_fwss.bin │█ │ 0%
3.9M ├── file_994_fwss.bin │█ │ 0% 3.9M ├── file_995_fwdcbss.bin│█ │ 0%
3.9M ├── file_995_fwdcbss.bin│█ │ 0% 3.9M ├── file_995_fwscas.bin │█ │ 0%
3.9M ├── file_995_fwscas.bin │█ │ 0% 3.9M ├── file_995_fwss.bin │█ │ 0%
3.9M ├── file_995_fwss.bin │█ │ 0% 3.9M ├── file_996_fwdcbss.bin│█ │ 0%
3.9M ├── file_996_fwdcbss.bin│█ │ 0% 3.9M ├── file_996_fwscas.bin │█ │ 0%
3.9M ├── file_996_fwscas.bin │█ │ 0% 3.9M ├── file_996_fwss.bin │█ │ 0%
3.9M ├── file_996_fwss.bin │█ │ 0% 3.9M ├── file_997_fwdcbss.bin│█ │ 0%
3.9M ├── file_997_fwdcbss.bin│█ │ 0% 3.9M ├── file_997_fwscas.bin │█ │ 0%
3.9M ├── file_997_fwscas.bin │█ │ 0% 3.9M ├── file_997_fwss.bin │█ │ 0%
3.9M ├── file_997_fwss.bin │█ │ 0% 3.9M ├── file_998_fwdcbss.bin│█ │ 0%
3.9M ├── file_998_fwdcbss.bin│█ │ 0% 3.9M ├── file_998_fwscas.bin │█ │ 0%
3.9M ├── file_998_fwscas.bin │█ │ 0% 3.9M ├── file_998_fwss.bin │█ │ 0%
3.9M ├── file_998_fwss.bin │█ │ 0% 3.9M ├── file_999_fwdcbss.bin│█ │ 0%
3.9M ├── file_999_fwdcbss.bin│█ │ 0% 3.9M ├── file_999_fwscas.bin │█ │ 0%
3.9M ├── file_999_fwscas.bin │█ │ 0% 3.9M ├── file_999_fwss.bin │█ │ 0%
3.9M ├── file_999_fwss.bin │█ │ 0% 3.9M ├── file_99_fwdcbss.bin │█ │ 0%
3.9M ├── file_99_fwdcbss.bin │█ │ 0% 3.9M ├── file_99_fwscas.bin │█ │ 0%
3.9M ├── file_99_fwscas.bin │█ │ 0% 3.9M ├── file_99_fwss.bin │█ │ 0%
3.9M ├── file_99_fwss.bin │█ │ 0% 3.9M ├── file_9_fwdcbss.bin │█ │ 0%
3.9M ├── file_9_fwdcbss.bin │█ │ 0% 3.9M ├── file_9_fwscas.bin │█ │ 0%
3.9M ├── file_9_fwscas.bin │█ │ 0% 3.9M ├── file_9_fwss.bin │█ │ 0%
3.9M ├── file_9_fwss.bin │█ 0% 11G ┌─┴ bench_artifacts │█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████100%
11G ┌─┴ bench_artifacts │████████████████████████████████████████████████████████████████ │ 100%
``` ```
## proposed ## proposed
- [ ] parallelization - [ ] parallelization
- [ ] (scanning + processing sw + processing hw) & formatting & printing
- [ ] scanning + processing sw + processing hw + formatting + printing - [ ] scanning + processing sw + processing hw + formatting + printing
- [ ] user supplied cache file path for faster re-runs
- [ ] hardlinks / symlinks support
- [ ] max file path size should use the last set of duplicates - [ ] max file path size should use the last set of duplicates
- [ ] add more unit tests - [ ] add more unit tests
- [ ] test against different filesystems - [ ] test against different filesystems
- [ ] test against different file name encodings - [ ] test against different file name encodings
- [ ] restore json output (was removed in 0.3 due to quality issues) - [ ] restore json output (was removed in 0.3)
- [ ] fix memory leak on very large filesystems - [ ] fix memory leak on very large filesystems
- [ ] maybe use a bloom filter - [ ] maybe use a bloom filter
- [ ] reduce FileInfo size - [ ] reduce FileInfo size
- [ ] output in a tree format
- [ ] tui - [ ] tui
- [ ] change the default hashing method to include the first & last page of a file (8K) - [ ] change the default hashing method to include the first & last page of a file (8K)
- [ ] provide option to localize duplicate detection to arbitrary levels relative to current directory
- [ ] localize file meta store locks to sub path levels to avoid global lock contention from multiple threads.
- [ ] bulk operations
- [ ] --keep-latest
- [ ] --keep-oldest
- [ ] --keep-last-modified
- [ ] --keep-first-modified
- [ ] fix: partial hash collision - a file full of null bytes ("\0") and an empty file. This is a known trade off in gxhash. ## v0.3
- [ ] include initial pages and final pages of the file
- [ ] append the offset between the last initial page hashed and the first final page hashed in the content passed to the hasher.
- [ ] potential optimizations
- [ ] lookup the memory efficiency gains if instead of directly inserting into a hashmap, deduplicator looksup the file in a bloom filter. this way, the duplicate store hashmap does
not require to be locked for every single file. The bloom filter can be stored in an atomically updateable type to improve performance as well.
## v0.3.2
- [x] fix: single file groups are printed to screen
- [x] fix: --exclude-types and --types flag behave identically.
## v0.3.1
- [x] parallelization
- [x] (scanning + processing sw + processing hw) & formatting & printing
- [x] remove formatting step and write directly to stdout
- [x] simplify output to improve performance
- [x] increase the number of pages hashed in partial hashing
- [x] updated dependencies
- [x] fix: full file hash collision between a file full of null bytes ("\0") and an empty file. This is a known trade off in gxhash.
- [x] appending the file size at the end of content before hashing.
- [x] created an automated release pipeline
## v0.3.0
- [x] parallelization - [x] parallelization
- [x] (scanning) + (processing sw & processing hw & formatting & printing) - [x] (scanning) + (processing sw & processing hw & formatting & printing)
- [x] reduce cloning values on the heap - [x] reduce cloning values on the heap

View File

@@ -13,11 +13,9 @@ namespace :benchmark do
# Range (min … max): 1.8 ms … 9.7 ms 1474 runs # Range (min … max): 1.8 ms … 9.7 ms 1474 runs
root = "bench_artifacts" root = "bench_artifacts"
FileUtils.rm_rf(root)
Dir.mkdir(root) Dir.mkdir(root)
# files with same size # files with same size
puts "generating files of same size ..."
2.times.map do |i| 2.times.map do |i|
File.open(File.join(root, "file_#{i}_fwss.bin"), 'wb') do |f| File.open(File.join(root, "file_#{i}_fwss.bin"), 'wb') do |f|
f.write(SecureRandom.bytes(4096 * 100_000)) f.write(SecureRandom.bytes(4096 * 100_000))
@@ -25,7 +23,6 @@ namespace :benchmark do
end end
# files with different sizes # files with different sizes
puts "generating files of different sizes ..."
2.times.map do |i| 2.times.map do |i|
File.open(File.join(root, "file_#{i}_fwds.bin"), 'wb') do |f| File.open(File.join(root, "file_#{i}_fwds.bin"), 'wb') do |f|
f.write(SecureRandom.bytes(4096 * (rand * 100_000).ceil)) f.write(SecureRandom.bytes(4096 * (rand * 100_000).ceil))
@@ -33,7 +30,6 @@ namespace :benchmark do
end end
# files with same content & size # files with same content & size
puts "generating files of same content and sizes ..."
2.times.each do |i| 2.times.each do |i|
File.open(File.join(root, "file_#{i}_fwscas.bin"), 'wb') do |f| File.open(File.join(root, "file_#{i}_fwscas.bin"), 'wb') do |f|
f.write("\0" * (4096 * 100_000)) f.write("\0" * (4096 * 100_000))
@@ -41,7 +37,6 @@ namespace :benchmark do
end end
# files with different content but same size # files with different content but same size
puts "generating files of different content but same sizes ..."
2.times.each do |i| 2.times.each do |i|
File.open(File.join(root, "file_#{i}_fwdcbss.bin"), 'wb') do |f| File.open(File.join(root, "file_#{i}_fwdcbss.bin"), 'wb') do |f|
f.write(SecureRandom.bytes(4096 * 100_000)) f.write(SecureRandom.bytes(4096 * 100_000))
@@ -63,7 +58,6 @@ namespace :benchmark do
Dir.mkdir(root) Dir.mkdir(root)
# files with same size # files with same size
puts "generating 1000 files of the same size ... "
1000.times.each do |i| 1000.times.each do |i|
File.open(File.join(root, "file_#{i}_fwss.bin"), 'wb') do |f| File.open(File.join(root, "file_#{i}_fwss.bin"), 'wb') do |f|
f.write(SecureRandom.bytes(4096 * 1000)) f.write(SecureRandom.bytes(4096 * 1000))
@@ -71,7 +65,6 @@ namespace :benchmark do
end end
# files with different sizes # files with different sizes
puts "generating 1000 files of different sizes ... "
1000.times.each do |i| 1000.times.each do |i|
File.open(File.join(root, "file_#{i}_fwds.bin"), 'wb') do |f| File.open(File.join(root, "file_#{i}_fwds.bin"), 'wb') do |f|
f.write(SecureRandom.bytes(4096 * (rand * 100).ceil)) f.write(SecureRandom.bytes(4096 * (rand * 100).ceil))
@@ -79,7 +72,6 @@ namespace :benchmark do
end end
# files with same content & size # files with same content & size
puts "generating files of same content and sizes ..."
1000.times.each do |i| 1000.times.each do |i|
File.open(File.join(root, "file_#{i}_fwscas.bin"), 'wb') do |f| File.open(File.join(root, "file_#{i}_fwscas.bin"), 'wb') do |f|
f.write("\0" * (4096 * 1000)) f.write("\0" * (4096 * 1000))
@@ -87,7 +79,6 @@ namespace :benchmark do
end end
# files with different content but same size # files with different content but same size
puts "generating files of different content but same sizes ..."
1000.times.each do |i| 1000.times.each do |i|
File.open(File.join(root, "file_#{i}_fwdcbss.bin"), 'wb') do |f| File.open(File.join(root, "file_#{i}_fwdcbss.bin"), 'wb') do |f|
f.write(SecureRandom.bytes(4096 * 1000)) f.write(SecureRandom.bytes(4096 * 1000))

View File

@@ -5,43 +5,30 @@ use std::{
fs, fs,
io::Read, io::Read,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::{Arc, Mutex},
time::SystemTime, time::SystemTime,
}; };
#[derive(Debug, Clone, PartialEq)]
pub enum FileState {
Unprocessed,
SwProcessed,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct FileInfo { pub struct FileInfo {
pub path: Box<Path>, pub path: Box<Path>,
pub size: u64, pub size: u64,
pub modified: SystemTime, pub modified: SystemTime,
pub state: Arc<Mutex<FileState>>,
} }
impl FileInfo { impl FileInfo {
pub fn hash(&self, seed: i64) -> Result<u128> { pub fn hash(&self, seed: i64) -> Result<u128> {
if self.size == 0 {
return Ok(0u128);
};
let file = fs::File::open(&self.path)?; let file = fs::File::open(&self.path)?;
let mapper = unsafe { Mmap::map(&file)? }; let mapper = unsafe { Mmap::map(&file)? };
let content_hash = mapper let final_hash = mapper.chunks(4096).fold(0u128, |acc, chunk: &[u8]| {
.chunks(4096) acc.wrapping_add(gxhash128(chunk, seed))
.fold(0u128, |acc, chunk: &[u8]| acc ^ gxhash128(chunk, seed)); });
// NOTE: avoids collision bw an empty file & a file full of null bytes. Ok(final_hash)
Ok(content_hash ^ gxhash128(&self.size.to_ne_bytes(), seed))
} }
pub fn initpages_hash(&self, seed: i64) -> Result<u128> { pub fn initial_page_hash(&self, seed: i64) -> Result<u128> {
let mut file = fs::File::open(&self.path)?; let mut file = fs::File::open(&self.path)?;
let mut buffer = [0; 16384]; let mut buffer = [0; 4096];
let bytes_read = file.read(&mut buffer)?; let bytes_read = file.read(&mut buffer)?;
Ok(gxhash128(&buffer[..bytes_read], seed)) Ok(gxhash128(&buffer[..bytes_read], seed))
@@ -53,52 +40,6 @@ impl FileInfo {
path: path.into_boxed_path(), path: path.into_boxed_path(),
size: filemeta.len(), size: filemeta.len(),
modified: filemeta.modified()?, modified: filemeta.modified()?,
state: Arc::new(Mutex::new(FileState::Unprocessed)),
}) })
} }
pub fn sw_processed(&self) {
let mut self_state = self.state.lock().unwrap();
*self_state = FileState::SwProcessed;
}
pub fn is_sw_processed(&self) -> bool {
let self_state = self.state.lock().unwrap();
*self_state == FileState::SwProcessed
}
}
#[cfg(test)]
mod test {
use super::*;
use tempfile::TempDir;
use std::fs::File;
use std::io::Write;
use anyhow::Result;
fn generate_null_bytes(size: usize) -> Vec<u8> {
(0..size).map(|_| 0).collect::<Vec<u8>>()
}
#[test]
fn hash_differentiates_between_a_file_of_null_bytes_vs_an_empty_file() -> Result<()> {
let root = TempDir::new()?;
let empty_file_name = root.path().join("empty_file.bin");
File::create_new(&empty_file_name)?;
let file_with_null_bytes_name = root.path().join("file_with_null_bytes.bin");
let mut file_with_null_bytes = File::create_new(&file_with_null_bytes_name)?;
file_with_null_bytes.write_all(&generate_null_bytes(1000 * 4096))?;
let empty_file_info = FileInfo::new(empty_file_name)?;
let file_with_empty_bytes_info = FileInfo::new(file_with_null_bytes_name)?;
let seed: i64 = 246910456374;
assert_ne!(empty_file_info.hash(seed)?, file_with_empty_bytes_info.hash(seed)?);
Ok(())
}
} }

View File

@@ -2,24 +2,26 @@ use crate::{fileinfo::FileInfo, params::Params};
use anyhow::Result; use anyhow::Result;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use dashmap::DashMap; use dashmap::DashMap;
use indicatif::{ParallelProgressIterator, ProgressBar, ProgressFinish, ProgressStyle};
use pathdiff::diff_paths; use pathdiff::diff_paths;
use prettytable::{format, row, Row, Table};
use rayon::prelude::*; use rayon::prelude::*;
use std::sync::atomic::AtomicU64; use std::{borrow::Cow, path::PathBuf, sync::Arc, time::Duration};
use std::{path::PathBuf, sync::Arc};
const YELLOW: &str = "\x1b[33m";
const RESET: &str = "\x1b[0m";
pub struct Formatter; pub struct Formatter;
impl Formatter { impl Formatter {
pub fn human_path(file: &FileInfo, aargs: &Params, max_path_length: usize) -> Result<String> { pub fn human_path(
let base_directory: PathBuf = aargs.get_directory()?; file: &FileInfo,
app_args: &Params,
min_path_length: usize,
) -> Result<String> {
let base_directory: PathBuf = app_args.get_directory()?;
let relative_path = diff_paths(&file.path, base_directory).unwrap_or_default(); let relative_path = diff_paths(&file.path, base_directory).unwrap_or_default();
let formatted_path = format!( let formatted_path = format!(
"{:<0width$}", "{:<0width$}",
relative_path.to_str().unwrap_or_default().to_string(), relative_path.to_str().unwrap_or_default().to_string(),
width = max_path_length width = min_path_length
); );
Ok(formatted_path) Ok(formatted_path)
@@ -34,47 +36,66 @@ impl Formatter {
Ok(modified_time.format("%Y-%m-%d %H:%M:%S").to_string()) Ok(modified_time.format("%Y-%m-%d %H:%M:%S").to_string())
} }
pub fn print(raw: Arc<DashMap<u128, Vec<FileInfo>>>, max_path_len: u64, aargs: &Params) { pub fn gen_sub_tbl(items: Vec<FileInfo>, app_args: &Params, max_path_len: u64) -> Table {
print!("{}", "\n".repeat(if aargs.progress { 2 } else { 1 })); // spacing let mut inner_table = Table::new();
inner_table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
items.iter().for_each(|file| {
inner_table.add_row(row![
Self::human_path(file, app_args, max_path_len as usize).unwrap_or_default(),
Self::human_filesize(file).unwrap_or_default(),
Self::human_mtime(file).unwrap_or_default()
]);
});
inner_table
}
pub fn generate_table(
raw: Arc<DashMap<u128, Vec<FileInfo>>>,
mpath_len: u64,
args: &Params,
) -> Result<Table> {
let progress_bar = match args.progress {
true => ProgressBar::new_spinner(),
false => ProgressBar::hidden(),
};
let progress_style = ProgressStyle::with_template("[{elapsed_precise}] {pos:>7} {msg}")?;
progress_bar.set_style(progress_style);
progress_bar.enable_steady_tick(Duration::from_millis(50));
progress_bar.set_message("generating output");
let rows = raw
.par_iter_mut()
.progress_with(progress_bar)
.with_finish(ProgressFinish::WithMessage(Cow::from("output generated")))
.filter(|i| i.value().len() > 1)
.map(|i| {
row![
i.key(),
Self::gen_sub_tbl(i.value().to_vec(), args, mpath_len)
]
})
.collect::<Vec<Row>>();
let mut output_table = Table::new();
output_table.set_titles(row!["hash", "duplicates"]);
output_table.extend(rows);
Ok(output_table)
}
pub fn print(
raw: Arc<DashMap<u128, Vec<FileInfo>>>,
max_path_len: u64,
app_args: &Params,
) -> Result<()> {
if raw.is_empty() { if raw.is_empty() {
println!("No duplicates found matching your search criteria."); println!("\n\nNo duplicates found matching your search criteria.\n");
} else { return Ok(());
let printed_count: AtomicU64 = AtomicU64::new(0);
raw.par_iter().for_each(|sref| {
if sref.value().len() > 1 {
printed_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let mut ostring = format!("{}{:32x}{}\n", YELLOW, sref.key(), RESET);
let subfields = sref
.value()
.par_iter()
.enumerate()
.map(|(i, finfo)| {
let nodechar = if i == sref.value().len() - 1 {
"└─"
} else {
"├─"
};
format!(
"{}\t{}\t{}\t{}\n",
nodechar,
Self::human_path(finfo, aargs, max_path_len as usize)
.expect("path formatting failed."),
Self::human_filesize(finfo).expect("filesize formatting failed."),
Self::human_mtime(finfo).expect("modified time formatting failed.")
)
})
.collect::<String>();
ostring.push_str(&subfields);
println!("{ostring}");
}
});
if printed_count.load(std::sync::atomic::Ordering::Relaxed) < 1 {
println!("No duplicates found matching your search criteria.");
}
} }
let output_table = Self::generate_table(raw, max_path_len, app_args)?;
output_table.printstd();
Ok(())
} }
} }

View File

@@ -2,7 +2,6 @@ use crate::{fileinfo::FileInfo, formatter::Formatter, params::Params};
use anyhow::Result; use anyhow::Result;
use dashmap::DashMap; use dashmap::DashMap;
use prettytable::{format, row, Table}; use prettytable::{format, row, Table};
use std::sync::atomic::AtomicU64;
use std::{ use std::{
io::{self, Write}, io::{self, Write},
sync::Arc, sync::Arc,
@@ -12,19 +11,12 @@ pub struct Interactive;
impl Interactive { impl Interactive {
pub fn init(result: Arc<DashMap<u128, Vec<FileInfo>>>, app_args: &Params) -> Result<()> { pub fn init(result: Arc<DashMap<u128, Vec<FileInfo>>>, app_args: &Params) -> Result<()> {
let store = result.clone(); result
if store.is_empty() { .clone()
println!("No duplicates found matching your search criteria.");
}
let printed_count: AtomicU64 = AtomicU64::new(0);
store
.iter() .iter()
.filter(|i| i.value().len() > 1) .filter(|i| i.value().len() > 1)
.enumerate() .enumerate()
.for_each(|(gindex, i)| { .for_each(|(gindex, i)| {
printed_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let group = i.value(); let group = i.value();
let mut itable = Table::new(); let mut itable = Table::new();
itable.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR); itable.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
@@ -48,10 +40,6 @@ impl Interactive {
Self::process_group_action(group, gindex, result.len(), itable); Self::process_group_action(group, gindex, result.len(), itable);
}); });
if printed_count.load(std::sync::atomic::Ordering::Relaxed) < 1 {
println!("No duplicates found matching your search criteria.");
}
Ok(()) Ok(())
} }

View File

@@ -24,7 +24,7 @@ fn main() -> Result<()> {
server.hw_duplicate_set, server.hw_duplicate_set,
server.max_file_path_len.load(Ordering::Acquire), server.max_file_path_len.load(Ordering::Acquire),
&app_args, &app_args,
); )?;
} }
true => { true => {
Interactive::init(server.hw_duplicate_set, &app_args)?; Interactive::init(server.hw_duplicate_set, &app_args)?;

View File

@@ -6,9 +6,6 @@ use clap::{Parser, ValueHint};
#[derive(Parser, Debug, Default, Clone)] #[derive(Parser, Debug, Default, Clone)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
pub struct Params { pub struct Params {
/// Exclude Filetypes [default = none]
#[arg(short = 'T', long)]
pub exclude_types: Option<String>,
/// Filetypes to deduplicate [default = all] /// Filetypes to deduplicate [default = all]
#[arg(short, long)] #[arg(short, long)]
pub types: Option<String>, pub types: Option<String>,
@@ -55,4 +52,8 @@ impl Params {
let dir = fs::canonicalize(dir_path)?; let dir = fs::canonicalize(dir_path)?;
Ok(dir) Ok(dir)
} }
pub fn get_types(&self) -> Option<String> {
self.types.clone()
}
} }

View File

@@ -1,12 +1,13 @@
use anyhow::Result; use anyhow::Result;
use dashmap::DashMap; use dashmap::DashMap;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use indicatif::{
use rayon::iter::IntoParallelRefMutIterator; MultiProgress, ParallelProgressIterator, ProgressBar, ProgressFinish, ProgressStyle,
};
use rayon::prelude::{IntoParallelIterator, ParallelIterator}; use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use std::borrow::Cow;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, TryLockError, TryLockResult}; use std::sync::{Arc, Mutex, TryLockError, TryLockResult};
use std::time::Duration; use std::time::Duration;
use unicode_segmentation::UnicodeSegmentation;
use crate::fileinfo::FileInfo; use crate::fileinfo::FileInfo;
use crate::params::Params; use crate::params::Params;
@@ -21,68 +22,57 @@ impl Processor {
progress_bar_box: Arc<MultiProgress>, progress_bar_box: Arc<MultiProgress>,
max_file_size: Arc<AtomicU64>, max_file_size: Arc<AtomicU64>,
seed: i64, seed: i64,
sw_sorting_finished: Arc<AtomicBool>,
) -> Result<()> { ) -> Result<()> {
let progress_bar = match app_args.progress { let progress_bar = match app_args.progress {
true => progress_bar_box.add(ProgressBar::new_spinner()), true => progress_bar_box.add(ProgressBar::new_spinner()),
false => ProgressBar::hidden(), false => ProgressBar::hidden(),
}; };
let keys: Vec<u64> = sw_store.clone().iter().map(|i| *i.key()).collect();
let progress_style = ProgressStyle::with_template("[{elapsed_precise}] {pos:>7} {msg}")?; let progress_style = ProgressStyle::with_template("[{elapsed_precise}] {pos:>7} {msg}")?;
progress_bar.set_style(progress_style); progress_bar.set_style(progress_style);
progress_bar.enable_steady_tick(Duration::from_millis(50)); progress_bar.enable_steady_tick(Duration::from_millis(50));
progress_bar.set_message("files grouped by hash."); progress_bar.set_message("files grouped by hash.");
loop { keys.into_par_iter()
let keys: Vec<u64> = sw_store .progress_with(progress_bar)
.clone() .with_finish(ProgressFinish::WithMessage(Cow::from(
.iter() "files grouped by hash.",
.filter(|i| !i.value().iter().all(|x| x.is_sw_processed())) )))
.filter(|i| i.value().len() > 1) .for_each(|key| {
.map(|i| *i.key()) let group: Vec<FileInfo> = sw_store.get(&key).unwrap().to_vec();
.collect(); if group.len() > 1 {
group.into_par_iter().for_each(|file| {
let fhash = if app_args.strict {
file.hash(seed).expect("hashing file failed.")
} else {
file.initial_page_hash(seed).expect("hashing file failed.")
};
if keys.is_empty() { Self::compare_and_update_max_path_len(
match sw_sorting_finished.load(std::sync::atomic::Ordering::Relaxed) { max_file_size.clone(),
true => { file.path.to_string_lossy().len() as u64,
progress_bar.finish_with_message("files grouped by hash."); )
break Ok(()); .unwrap();
}
false => continue, hw_store
.entry(fhash)
.and_modify(|fileset| fileset.push(file.clone()))
.or_insert_with(|| vec![file]);
});
} }
} else { });
keys.into_par_iter().for_each(|key| {
let mut group: Vec<FileInfo> = sw_store.get(&key).unwrap().to_vec();
if group.len() > 1 {
group.par_iter_mut().for_each(|file| {
progress_bar.inc(1);
file.sw_processed();
let fhash = match app_args.strict { Ok(())
true => file.hash(seed).expect("hashing file failed."),
false => file.initpages_hash(seed).expect("hashing file failed."),
};
Self::compare_and_update_max_path_len(
max_file_size.clone(),
file.path.to_string_lossy().graphemes(true).count() as u64,
);
hw_store
.entry(fhash)
.and_modify(|fileset| fileset.push(file.clone()))
.or_insert_with(|| vec![file.clone()]);
});
};
});
}
}
} }
pub fn compare_and_update_max_path_len(current: Arc<AtomicU64>, next: u64) { pub fn compare_and_update_max_path_len(current: Arc<AtomicU64>, next: u64) -> Result<()> {
if current.load(Ordering::Relaxed) < next { if current.load(Ordering::Relaxed) < next {
current.store(next, Ordering::Release); current.store(next, Ordering::Release);
} }
Ok(())
} }
pub fn sizewise( pub fn sizewise(
@@ -154,9 +144,9 @@ mod tests {
} }
#[test] #[test]
fn hashwise_sorting_two_files_with_identical_init_pages_only_strict_mode() -> Result<()> { fn hashwise_sorting_two_files_with_identical_init_page_only_strict_mode() -> Result<()> {
let root = TempDir::new()?; let root = TempDir::new()?;
let content = generate_bytes(16384); let content = generate_bytes(4096);
let mut content_x = content.clone(); let mut content_x = content.clone();
let mut content_y = content.clone(); let mut content_y = content.clone();
@@ -203,7 +193,6 @@ mod tests {
Arc::new(MultiProgress::new()), Arc::new(MultiProgress::new()),
Arc::new(AtomicU64::new(32)), Arc::new(AtomicU64::new(32)),
300, 300,
Arc::new(AtomicBool::new(true)),
)?; )?;
assert_eq!(hw_dupstore.len(), 2); assert_eq!(hw_dupstore.len(), 2);
@@ -212,9 +201,9 @@ mod tests {
} }
#[test] #[test]
fn hashwise_sorting_two_files_with_identical_init_pages_only_fast_mode() -> Result<()> { fn hashwise_sorting_two_files_with_identical_init_page_only_fast_mode() -> Result<()> {
let root = TempDir::new()?; let root = TempDir::new()?;
let content = generate_bytes(16384); let content = generate_bytes(4096);
let mut content_x = content.clone(); let mut content_x = content.clone();
let mut content_y = content.clone(); let mut content_y = content.clone();
@@ -256,7 +245,6 @@ mod tests {
Arc::new(MultiProgress::new()), Arc::new(MultiProgress::new()),
Arc::new(AtomicU64::new(32)), Arc::new(AtomicU64::new(32)),
300, 300,
Arc::new(AtomicBool::new(true)),
)?; )?;
assert_eq!(hw_dupstore.len(), 1); assert_eq!(hw_dupstore.len(), 1);
@@ -302,7 +290,6 @@ mod tests {
Arc::new(MultiProgress::new()), Arc::new(MultiProgress::new()),
Arc::new(AtomicU64::new(32)), Arc::new(AtomicU64::new(32)),
300, 300,
Arc::new(AtomicBool::new(true)),
)?; )?;
assert_eq!(hw_dupstore.len(), 1); assert_eq!(hw_dupstore.len(), 1);

View File

@@ -8,10 +8,9 @@ use globwalk::{GlobWalker, GlobWalkerBuilder};
pub struct Scanner { pub struct Scanner {
pub directory: Box<Path>, pub directory: Box<Path>,
pub filetypes: Option<String>,
pub min_depth: Option<usize>, pub min_depth: Option<usize>,
pub max_depth: Option<usize>, pub max_depth: Option<usize>,
pub include_types: Option<String>,
pub exclude_types: Option<String>,
pub min_size: Option<u64>, pub min_size: Option<u64>,
pub follow_links: bool, pub follow_links: bool,
pub progress: bool, pub progress: bool,
@@ -21,8 +20,7 @@ impl Scanner {
pub fn new(app_args: Arc<Params>) -> Result<Self> { pub fn new(app_args: Arc<Params>) -> Result<Self> {
Ok(Self { Ok(Self {
directory: app_args.get_directory()?.into_boxed_path(), directory: app_args.get_directory()?.into_boxed_path(),
include_types: app_args.types.clone(), filetypes: app_args.get_types(),
exclude_types: app_args.exclude_types.clone(),
min_depth: app_args.min_depth, min_depth: app_args.min_depth,
max_depth: app_args.max_depth, max_depth: app_args.max_depth,
min_size: app_args.get_min_size(), min_size: app_args.get_min_size(),
@@ -31,21 +29,11 @@ impl Scanner {
}) })
} }
fn scan_patterns(&self) -> Result<Vec<String>> { fn scan_patterns(&self) -> Result<String> {
let include_types = match &self.include_types { Ok(match &self.filetypes {
Some(ftypes) => Some(format!("**/*.{{{ftypes}}}")), Some(ftypes) => format!("**/*{{{ftypes}}}"),
None => Some("**/*".to_string()), None => "**/*".to_string(),
}; })
let exclude_types = self
.exclude_types
.as_ref()
.map(|ftypes| format!("!**/*.{{{ftypes}}}"));
Ok(vec![include_types, exclude_types]
.into_iter()
.flatten()
.collect())
} }
fn attach_link_opts(&self, walker: GlobWalkerBuilder) -> Result<GlobWalkerBuilder> { fn attach_link_opts(&self, walker: GlobWalkerBuilder) -> Result<GlobWalkerBuilder> {
@@ -68,7 +56,7 @@ impl Scanner {
fn build_walker(&self) -> Result<GlobWalker> { fn build_walker(&self) -> Result<GlobWalker> {
let walker = Ok(GlobWalkerBuilder::from_patterns( let walker = Ok(GlobWalkerBuilder::from_patterns(
self.directory.clone(), self.directory.clone(),
&self.scan_patterns()?, &[self.scan_patterns()?],
)) ))
.and_then(|walker| self.attach_walker_min_depth(walker)) .and_then(|walker| self.attach_walker_min_depth(walker))
.and_then(|walker| self.attach_walker_max_depth(walker)) .and_then(|walker| self.attach_walker_max_depth(walker))
@@ -91,7 +79,7 @@ impl Scanner {
progress_bar.set_style(progress_style); progress_bar.set_style(progress_style);
progress_bar.enable_steady_tick(Duration::from_millis(50)); progress_bar.enable_steady_tick(Duration::from_millis(50));
progress_bar.set_message("paths mapped"); progress_bar.set_message("paths mapped");
let min_size = self.min_size.unwrap_or(0); let min_size = self.min_size.unwrap_or_default();
self.build_walker()? self.build_walker()?
.filter_map(Result::ok) .filter_map(Result::ok)
@@ -100,7 +88,7 @@ impl Scanner {
.filter(|path| path.is_file()) .filter(|path| path.is_file())
.map(FileInfo::new) .map(FileInfo::new)
.filter_map(Result::ok) .filter_map(Result::ok)
.filter(|file| file.size >= min_size) .filter(|file| file.size > min_size)
.for_each(|file| { .for_each(|file| {
let mut flock = files.lock().unwrap(); let mut flock = files.lock().unwrap();
flock.push(file); flock.push(file);
@@ -110,151 +98,3 @@ impl Scanner {
Ok(()) Ok(())
} }
} }
#[cfg(test)]
mod tests {
use crate::fileinfo::FileInfo;
use crate::params::Params;
use std::fs::File;
use std::sync::{Arc, Mutex};
use super::Scanner;
use indicatif::MultiProgress;
use tempfile::TempDir;
#[test]
fn ensure_file_include_type_filter_includes_expected_file_types() {
let root =
TempDir::with_prefix("deduplicator_test_root").expect("unable to create tempdir");
[
"this-is-a-js-file.js",
"this-is-a-css-file.css",
"this-is-a-csv-file.csv",
"this-is-a-rust-file.rs",
]
.iter()
.for_each(|path| {
File::create_new(root.path().join(path)).unwrap_or_else(|_| {
panic!("unable to create file {path}");
});
});
let params = Params {
types: Some(String::from("js,csv")),
dir: Some(root.path().into()),
..Default::default()
};
let progress = Arc::new(MultiProgress::new());
let scanlist = Arc::new(Mutex::<Vec<FileInfo>>::new(vec![]));
let scanner = Scanner::new(Arc::new(params)).expect("scanner initialization failed");
scanner
.scan(scanlist.clone(), progress)
.expect("scanning failed.");
let scan_list_mg = scanlist.lock().unwrap();
assert!(scan_list_mg.iter().any(|f| f.path.to_str().unwrap()
== root.path().join("this-is-a-js-file.js").to_str().unwrap()));
assert!(scan_list_mg.iter().any(|f| f.path.to_str().unwrap()
== root.path().join("this-is-a-csv-file.csv").to_str().unwrap()));
assert!(scan_list_mg.iter().all(|f| f.path.to_str().unwrap()
!= root.path().join("this-is-a-css-file.css").to_str().unwrap()));
assert!(scan_list_mg.iter().all(|f| f.path.to_str().unwrap()
!= root.path().join("this-is-a-rust-file.rs").to_str().unwrap()));
}
#[test]
fn ensure_file_exclude_type_filter_excludes_expected_file_types() {
let root =
TempDir::with_prefix("deduplicator_test_root").expect("unable to create tempdir");
[
"this-is-a-js-file.js",
"this-is-a-css-file.css",
"this-is-a-csv-file.csv",
"this-is-a-rust-file.rs",
]
.iter()
.for_each(|path| {
File::create_new(root.path().join(path)).unwrap_or_else(|_| {
panic!("unable to create file {path}");
});
});
let params = Params {
exclude_types: Some(String::from("js,csv")),
dir: Some(root.path().into()),
..Default::default()
};
let progress = Arc::new(MultiProgress::new());
let scanlist = Arc::new(Mutex::<Vec<FileInfo>>::new(vec![]));
let scanner = Scanner::new(Arc::new(params)).expect("scanner initialization failed");
scanner
.scan(scanlist.clone(), progress)
.expect("scanning failed.");
let scan_list_mg = scanlist.lock().unwrap();
assert!(scan_list_mg.iter().all(|f| f.path.to_str().unwrap()
!= root.path().join("this-is-a-js-file.js").to_str().unwrap()));
assert!(scan_list_mg.iter().all(|f| f.path.to_str().unwrap()
!= root.path().join("this-is-a-csv-file.csv").to_str().unwrap()));
assert!(scan_list_mg.iter().any(|f| f.path.to_str().unwrap()
== root.path().join("this-is-a-css-file.css").to_str().unwrap()));
assert!(scan_list_mg.iter().any(|f| f.path.to_str().unwrap()
== root.path().join("this-is-a-rust-file.rs").to_str().unwrap()));
}
#[test]
fn complex_file_type_params() {
let root =
TempDir::with_prefix("deduplicator_test_root").expect("unable to create tempdir");
[
"this-is-a-js-file.js",
"this-is-a-css-file.css",
"this-is-a-csv-file.csv",
"this-is-a-rust-file.rs",
]
.iter()
.for_each(|path| {
File::create_new(root.path().join(path)).unwrap_or_else(|_| {
panic!("unable to create file {path}");
});
});
let params = Params {
types: Some(String::from("js,csv,rs")),
exclude_types: Some(String::from("csv")),
dir: Some(root.path().into()),
..Default::default()
};
let progress = Arc::new(MultiProgress::new());
let scanlist = Arc::new(Mutex::<Vec<FileInfo>>::new(vec![]));
let scanner = Scanner::new(Arc::new(params)).expect("scanner initialization failed");
scanner
.scan(scanlist.clone(), progress)
.expect("scanning failed.");
let scan_list_mg = scanlist.lock().unwrap();
assert!(scan_list_mg.iter().any(|f| f.path.to_str().unwrap()
== root.path().join("this-is-a-js-file.js").to_str().unwrap()));
assert!(scan_list_mg.iter().all(|f| f.path.to_str().unwrap()
!= root.path().join("this-is-a-csv-file.csv").to_str().unwrap()));
assert!(scan_list_mg.iter().any(|f| f.path.to_str().unwrap()
== root.path().join("this-is-a-rust-file.rs").to_str().unwrap()));
}
}

View File

@@ -42,70 +42,52 @@ impl Server {
progbarbox.set_draw_target(ProgressDrawTarget::hidden()); progbarbox.set_draw_target(ProgressDrawTarget::hidden());
} }
let (app_args_sc, app_args_sw, app_args_hw) = ( let app_args_clone_for_sc = self.app_args.clone();
Arc::clone(&self.app_args), let app_args_clone_for_pr = self.app_args.clone();
Arc::clone(&self.app_args), let file_queue_clone_sc = self.filequeue.clone();
Arc::clone(&self.app_args), let file_queue_clone_pr = self.filequeue.clone();
);
let (file_queue_sc, file_queue_pr) = (
Arc::clone(&self.filequeue),
Arc::clone(&self.filequeue),
);
let scanner_finished = Arc::new(AtomicBool::new(false)); let scanner_finished = Arc::new(AtomicBool::new(false));
let sw_sort_finished = Arc::new(AtomicBool::new(false));
let (sfin_sc, sfin_pr) = ( let sfin_sc_tr_cl = scanner_finished.clone();
Arc::clone(&scanner_finished), let sfin_pr_tr_cl = scanner_finished.clone();
Arc::clone(&scanner_finished),
); let store_dupl_sw_for_sw = self.sw_duplicate_set.clone();
let (swfin_pr_sw, swfin_pr_hw) = ( let store_dupl_sw_for_hw = self.sw_duplicate_set.clone();
Arc::clone(&sw_sort_finished), let store_dupl_hw = self.hw_duplicate_set.clone();
Arc::clone(&sw_sort_finished), let max_file_path_len_clone = self.max_file_path_len.clone();
);
let (store_sw, store_sw2, store_hw) = ( let progbarbox_sc_clone = progbarbox.clone();
Arc::clone(&self.sw_duplicate_set),
Arc::clone(&self.sw_duplicate_set),
Arc::clone(&self.hw_duplicate_set),
);
let max_file_path_len = Arc::clone(&self.max_file_path_len);
let (prog_sc, prog_sw, prog_hw) = (
Arc::clone(&progbarbox),
Arc::clone(&progbarbox),
Arc::clone(&progbarbox),
);
self.threadpool.execute(move || { self.threadpool.execute(move || {
Scanner::new(app_args_sc) Scanner::new(app_args_clone_for_sc)
.expect("unable to initialize scanner.") .unwrap()
.scan(file_queue_sc, prog_sc) .scan(file_queue_clone_sc, progbarbox_sc_clone)
.expect("scanner failed."); .unwrap();
sfin_sc.store(true, std::sync::atomic::Ordering::Relaxed); sfin_sc_tr_cl.store(true, std::sync::atomic::Ordering::Relaxed);
}); });
let progbarbox_pr_clone = progbarbox.clone();
self.threadpool.execute(move || { self.threadpool.execute(move || {
Processor::sizewise( Processor::sizewise(
app_args_sw, app_args_clone_for_pr.clone(),
sfin_pr, sfin_pr_tr_cl,
store_sw, store_dupl_sw_for_sw,
file_queue_pr, file_queue_clone_pr,
prog_sw, progbarbox_pr_clone.clone(),
) )
.expect("sizewise scanner failed."); .unwrap();
swfin_pr_sw.store(true, std::sync::atomic::Ordering::Relaxed);
});
self.threadpool.execute(move || {
Processor::hashwise( Processor::hashwise(
app_args_hw, app_args_clone_for_pr,
store_sw2, store_dupl_sw_for_hw,
store_hw, store_dupl_hw,
prog_hw, progbarbox_pr_clone,
max_file_path_len, max_file_path_len_clone,
seed, seed,
swfin_pr_hw,
) )
.expect("sizewise scanner failed."); .unwrap();
}); });
progbarbox.clear()?; progbarbox.clear()?;