corrupt.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python
  2. import struct
  3. import sys
  4. import os
  5. import argparse
  6. def corrupt(block):
  7. with open(block, 'r+b') as file:
  8. # skip rev
  9. file.read(4)
  10. # go to last commit
  11. tag = 0xffffffff
  12. while True:
  13. try:
  14. ntag, = struct.unpack('<I', file.read(4))
  15. except struct.error:
  16. break
  17. tag ^= ntag
  18. file.seek(tag & 0xfff, os.SEEK_CUR)
  19. # lob off last 3 bytes
  20. file.seek(-((tag & 0xfff) + 3), os.SEEK_CUR)
  21. file.truncate()
  22. def main(args):
  23. if args.n or not args.blocks:
  24. with open('blocks/.history', 'rb') as file:
  25. for i in range(int(args.n or 1)):
  26. last, = struct.unpack('<I', file.read(4))
  27. args.blocks.append('blocks/%x' % last)
  28. for block in args.blocks:
  29. print 'corrupting %s' % block
  30. corrupt(block)
  31. if __name__ == "__main__":
  32. parser = argparse.ArgumentParser()
  33. parser.add_argument('-n')
  34. parser.add_argument('blocks', nargs='*')
  35. main(parser.parse_args())