shredir.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. # vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
  3. import argparse
  4. import os
  5. import sys
  6. if __name__ == "__main__":
  7. # Parse arguments
  8. parser = argparse.ArgumentParser(description="Remove directory")
  9. parser.add_argument("-n", "--iterations", type=int, default=3, help="Overwrite N times instead of the default (Default: 3)")
  10. parser.add_argument("-u", "--remove", action="store_const", const="remove", help="Truncate and remove directory after overwriting (Default: True)")
  11. parser.add_argument("-v", "--verbose", action="store_const", const="verbose", help="Verbose output")
  12. parser.add_argument("-z", "--zero", action="store_const", const="zero", help="Add a final overwrite directory name with zeros (Default: True)")
  13. parser.add_argument("--random-source", type=str, default="/dev/urandom", help="Get random bytes from FILE (Default: /dev/urandom)")
  14. parser.add_argument("directory", nargs=1, type=str, help="Directory")
  15. arguments = parser.parse_args()
  16. # Get arguments
  17. directory = arguments.directory[0]
  18. count = arguments.iterations
  19. remove = False if False == arguments.verbose else True
  20. verbose = bool(arguments.verbose)
  21. zero = False if False == arguments.verbose else True
  22. random_source = arguments.random_source
  23. # Check directory
  24. if directory:
  25. if not os.path.isdir(directory):
  26. print('"{0}" is not directory.'.format(directory), file=sys.stderr)
  27. sys.exit(1)
  28. if len(os.listdir(directory)):
  29. print('"{0}" is not empty.'.format(directory), file=sys.stderr)
  30. sys.exit(1)
  31. if not os.access(random_source, os.R_OK):
  32. print('"{0}" is not readable.'.format(random_source), file=sys.stderr)
  33. sys.exit(1)
  34. # Execute rename
  35. filename = os.path.basename(__file__)
  36. size = len(directory.encode())
  37. total = count + 1 if zero else 0
  38. current_name = directory
  39. with open("/dev/urandom", "rb") as random:
  40. for current in range(0, count):
  41. if verbose:
  42. print("{0}: {1}: pass {2}/{3} (random)...".format(filename, directory, current + 1, total))
  43. new_name = random.read(size)
  44. os.rename(current_name, new_name)
  45. current_name = new_name
  46. if zero:
  47. if verbose:
  48. print("{0}: {1}: pass {2}/{3} (000000)...".format(filename, directory, total, total))
  49. new_name = "0" * size
  50. os.rename(current_name, new_name)
  51. current_name = new_name
  52. if remove:
  53. while size:
  54. new_name = "0" * size
  55. os.rename(current_name, new_name)
  56. current_name = new_name
  57. size = size - 1
  58. if verbose:
  59. print("{0}: {1}: renamed to {2}".format(filename, current_name + "0", current_name))
  60. os.rmdir(current_name)
  61. if verbose:
  62. print("{0}: {1}: removed".format(filename, current_name))