corrupt.py 832 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python
  2. import struct
  3. import sys
  4. import os
  5. def main(*paths):
  6. # find most recent block
  7. file = None
  8. rev = None
  9. for path in paths:
  10. try:
  11. nfile = open(path, 'r+b')
  12. nrev, = struct.unpack('<I', nfile.read(4))
  13. assert rev != nrev
  14. if not file or ((rev - nrev) & 0x80000000):
  15. file = nfile
  16. rev = nrev
  17. except IOError:
  18. pass
  19. # go to last commit
  20. tag = 0
  21. while True:
  22. try:
  23. ntag, = struct.unpack('<I', file.read(4))
  24. except struct.error:
  25. break
  26. tag ^= ntag
  27. file.seek(tag & 0xfff, os.SEEK_CUR)
  28. # lob off last 3 bytes
  29. file.seek(-((tag & 0xfff) + 3), os.SEEK_CUR)
  30. file.truncate()
  31. if __name__ == "__main__":
  32. main(*sys.argv[1:])