From e04d86d3cbd4dcf6be15374e44264fd6aeaedef4 Mon Sep 17 00:00:00 2001 From: Darryl Nixon Date: Sun, 16 Jul 2023 12:45:14 -0700 Subject: [PATCH] Optimize finding --- melamine/shred.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/melamine/shred.py b/melamine/shred.py index 0c172ca..4859ff5 100644 --- a/melamine/shred.py +++ b/melamine/shred.py @@ -1,4 +1,7 @@ import argparse +from collections import defaultdict + +import aiofiles from .classes import get_all_hardlinks from .classes import ShredDir @@ -32,8 +35,21 @@ async def main(job: argparse.Namespace) -> bool: # Just in case, use "find" to delete any remaining hardlinks # from the mount point + logger.info("Deleting remaining hardlinks using find") + inodes_in_mount_points = defaultdict(set) for path in job.paths: - await path.delete_hardlinks_by_inode() + inodes_in_mount_points[path.mount_point].add(path.inode) + + for mount_point, inodes in inodes_in_mount_points.items(): + async for item in aiofiles.os.scandir(mount_point): + if item.name == "." or item.name == "..": + continue + if item.stat().st_ino in inodes: + log_buf = f"Deleting hardlink: {item.path}" + if not job.dryrun: + log_buf = "DRY RUN " + log_buf + await aiofiles.os.unlink(item.path) + logger.info(log_buf) # Shred all physical files including hardlinks for path in job.paths: @@ -41,3 +57,5 @@ async def main(job: argparse.Namespace) -> bool: await path.shred(hash=job.exhaustive, dryrun=job.dryrun) elif isinstance(path, ShredDir): await path.shred(hash=job.exhaustive, dryrun=job.dryrun) + + logger.info("Done")