corrupt.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python2
  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. size = (tag & 0x3ff) if (tag & 0x3ff) != 0x3ff else 0
  19. file.seek(size, os.SEEK_CUR)
  20. # lob off last 3 bytes
  21. file.seek(-(size + 3), os.SEEK_CUR)
  22. file.truncate()
  23. def main(args):
  24. if args.n or not args.blocks:
  25. with open('blocks/.history', 'rb') as file:
  26. for i in range(int(args.n or 1)):
  27. last, = struct.unpack('<I', file.read(4))
  28. args.blocks.append('blocks/%x' % last)
  29. for block in args.blocks:
  30. print 'corrupting %s' % block
  31. corrupt(block)
  32. if __name__ == "__main__":
  33. parser = argparse.ArgumentParser()
  34. parser.add_argument('-n')
  35. parser.add_argument('blocks', nargs='*')
  36. main(parser.parse_args())