mirror of
https://github.com/DarrylNixon/melamine.git
synced 2024-04-22 06:27:20 -07:00
MVP for testing
This commit is contained in:
parent
3396d2d69b
commit
9bc6f8d9e1
11 changed files with 459 additions and 3 deletions
43
melamine/shred.py
Normal file
43
melamine/shred.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
from .classes import ShredDir
|
||||
from .classes import ShredFile
|
||||
from .fileops import mount_to_fs
|
||||
from .logs import logger
|
||||
|
||||
|
||||
async def main(job) -> bool:
|
||||
"""
|
||||
This is the main function for processing a shred request.
|
||||
It is called by the CLI and builds a job queue based on the arguments passed.
|
||||
"""
|
||||
new_paths = set()
|
||||
|
||||
# Expand all directories and files, and collect mount point information
|
||||
for path in job.paths:
|
||||
if path.is_file():
|
||||
logger.info(f"Adding file: {path}")
|
||||
shred_file = ShredFile(path)
|
||||
shred_file.fs_handler = await mount_to_fs(shred_file.mount_point)
|
||||
new_paths.add(shred_file)
|
||||
elif path.is_dir():
|
||||
if job.recursive:
|
||||
logger.info(f"Adding directory: {path}")
|
||||
shred_dir = ShredDir(path)
|
||||
shred_dir.fs_handler = await mount_to_fs(shred_dir.mount_point)
|
||||
new_paths.add(shred_dir)
|
||||
else:
|
||||
logger.info(f"Skipping directory: {path} (try -r/--recursive)")
|
||||
else:
|
||||
raise TypeError(f"Not a file or directory: {path}")
|
||||
job.paths = new_paths
|
||||
|
||||
# Get hardlinks to subsequently unlink for all files
|
||||
for path in job.paths:
|
||||
if isinstance(path, ShredFile):
|
||||
path.hardlinks = set(link async for link in path.fs_handler.get_hardlinks(path))
|
||||
|
||||
# Shred all physical files including hardlinks
|
||||
for path in job.paths:
|
||||
if isinstance(path, ShredFile):
|
||||
await path.shred(job.hash, job.dryrun)
|
||||
elif isinstance(path, ShredDir):
|
||||
await path.shred(job.hash, job.dryrun)
|
Loading…
Add table
Add a link
Reference in a new issue