Optimize finding

This commit is contained in:
Darryl Nixon 2023-07-16 12:45:14 -07:00
parent 0bcbb0a4b0
commit e04d86d3cb

View file

@ -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")