Welcome to mirror list, hosted at ThFree Co, Russian Federation.

corrupt.py « tests - github.com/littlefs-project/littlefs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c452c423a6b7680dddbe4e3f3d119fefab7348b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python2

import struct
import sys
import os
import argparse

def corrupt(block):
    with open(block, 'r+b') as file:
        # skip rev
        file.read(4)

        # go to last commit
        tag = 0xffffffff
        while True:
            try:
                ntag, = struct.unpack('>I', file.read(4))
            except struct.error:
                break

            tag ^= ntag
            size = (tag & 0x3ff) if (tag & 0x3ff) != 0x3ff else 0
            file.seek(size, os.SEEK_CUR)

        # lob off last 3 bytes
        file.seek(-(size + 3), os.SEEK_CUR)
        file.truncate()

def main(args):
    if args.n or not args.blocks:
        with open('blocks/.history', 'rb') as file:
            for i in range(int(args.n or 1)):
                last, = struct.unpack('<I', file.read(4))
                args.blocks.append('blocks/%x' % last)

    for block in args.blocks:
        print 'corrupting %s' % block
        corrupt(block)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-n')
    parser.add_argument('blocks', nargs='*')
    main(parser.parse_args())