Debian Codegraph @

scripts/imgfile.py

raw symbols SCIP
1 #
2 # Copyright (C) 2006 Steve Ratcliffe
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as
6 # published by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 #
14 # Author: Steve Ratcliffe
15 # Create date: 18 Dec 2006
16 #
17
18 import sys
19 import time
20 from struct import *
21
22 class Imgfile:
23 def __init__(self, name):
24 self.name = name
25 self.block_size = 512
26 self.fp = open(name, 'r+')
27
28 def close(self):
29 self.fp.close()
30
31 def get_block(self, bn):
32 pos = bn * self.block_size
33 self.fp.seek(pos)
34 return self.fp.read(self.block_size)
35
36 def write_block(self, bn, bl):
37 pos = bn * self.block_size
38 self.fp.seek(pos)
39 return self.fp.write(bl)
40
41 def get_dirent(self, type):
42 for bn in xrange(2, 12):
43 bl = self.get_block(bn)
44 print bl[9:12], type
45 if bl[9:12] == type:
46 dent = Dirent(self, bl)
47 dent.dir_block_num = bn
48 return dent
49
50 def set_time(self):
51 self.fp.seek(0)
52 self.fp.read(self.block_size)
53 t = time.localtime()
54 s = pack("<HBBBBB", t[0], t[1], t[2], t[3], t[4], t[5])
55 self.fp.seek(0x39)
56 self.fp.write(s)
57
58 class Dirent:
59 def __init__(self, img, block):
60 self.img = img
61 self.block = block
62
63 def get_block(self, lbn):
64 pbn = self.get_phys_block(lbn)
65 return self.img.get_block(pbn)
66
67 def write_block(self, lbn, bl):
68 pbn = self.get_phys_block(lbn)
69 self.img.write_block(pbn, bl)
70
71 def get_phys_block(self, lbn):
72 """Get the physical block number from the logical."""
73 loc = 0x20 + 2*lbn
74 pbn = unpack("<H", self.block[loc:loc+2])
75 return pbn[0]