-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgddrescue_mapfile_to_dfxml.py
151 lines (127 loc) · 5.04 KB
/
gddrescue_mapfile_to_dfxml.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env/python3
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
# of their official duties. Pursuant to title 17 Section 105 of the
# United States Code this software is not subject to copyright
# protection and is in the public domain. NIST assumes no
# responsibility whatsoever for its use by other parties, and makes
# no guarantees, expressed or implied, about its quality,
# reliability, or any other characteristic.
#
# We would appreciate acknowledgement if the software is used.
"""
The GNU ddrescue mapfile format reports a status character for every block in a disk image. For the purposes of analyzing data in the disk image, the relevant status character is "+", indicating a "finished block" - an imaged block. Everything else is effectively an unretrieved, or unretrievable, block.
"""
__version__ = "0.1.0"
import os
import sys
import logging
import enum
from dfxml import objects as Objects
_logger = logging.getLogger(os.path.basename(__file__))
class ParseState(enum.Enum):
FILE_OPENED = 0
PRE_TABLE = 1
CURRENT_POS_HEAD = 2
CURRENT_POS_RECORD = 3
TABLE_HEAD = 4
IN_TABLE = 5
STREAM_COMPLETE = 99
STATE_TRANSMISSION_MATRIX = {
ParseState.CURRENT_POS_HEAD: {
ParseState.CURRENT_POS_RECORD
},
ParseState.CURRENT_POS_RECORD: {
ParseState.TABLE_HEAD
},
ParseState.FILE_OPENED: {
ParseState.PRE_TABLE
},
ParseState.IN_TABLE: {
ParseState.IN_TABLE,
ParseState.STREAM_COMPLETE
},
ParseState.PRE_TABLE: {
ParseState.CURRENT_POS_HEAD,
ParseState.PRE_TABLE
},
ParseState.STREAM_COMPLETE: set(),
ParseState.TABLE_HEAD: {
ParseState.IN_TABLE
}
}
class MapfileParser(object):
def __init__(self):
self._disk_image_len = None
self._state = None
self._line_no = None
def parse(self, in_fh):
"""
Returns a DFXMLObject.
"""
dobj = Objects.DFXMLObject(version="1.2.0+")
dobj.program = sys.argv[0]
dobj.program_version = __version__
dobj.command_line = " ".join(sys.argv)
dobj.dc["type"] = "Disk image sector map"
dobj.add_creator_library("Python", ".".join(map(str, sys.version_info[0:3]))) #A bit of a bend, but gets the major version information out.
dobj.add_creator_library("Objects.py", Objects.__version__)
dobj.add_creator_library("dfxml.py", Objects.dfxml.__version__)
diobj = Objects.DiskImageObject()
dobj.append(diobj)
brs = Objects.ByteRuns()
diobj.byte_runs = brs
dobj.add_namespace("gddr", Objects.dfxml.XMLNS_DFXML + "#gddrescue")
self._state = ParseState.FILE_OPENED
self._disk_image_len = 0
for (line_no, line) in enumerate(in_fh):
self._line_no = line_no
cleaned_line = line.strip()
if cleaned_line.startswith("0x"):
if self._state in (ParseState.TABLE_HEAD, ParseState.IN_TABLE):
self.transition(ParseState.IN_TABLE)
else:
self.transition(ParseState.CURRENT_POS_RECORD)
elif cleaned_line == "# pos size status":
self.transition(ParseState.TABLE_HEAD)
elif cleaned_line == "# current_pos current_status current_pass":
self.transition(ParseState.CURRENT_POS_HEAD)
else:
self.transition(ParseState.PRE_TABLE)
if self._state != ParseState.IN_TABLE:
continue
br = Objects.ByteRun()
line_parts = cleaned_line.split(" ")
br.img_offset = int(line_parts[0], base=16)
br.len = int(line_parts[1], base=16)
self._disk_image_len = br.img_offset + br.len
# TODO
# Independent design decision, while awaiting a consensus design:
# Only report the byte runs ddrescue was able to collect.
if line_parts[2] != "+":
continue
brs.append(br)
diobj.filesize = self._disk_image_len
_logger.info("diobj.filesize = %r." % diobj.filesize)
self.transition(ParseState.STREAM_COMPLETE)
return dobj
def transition(self, to_state):
if not to_state in STATE_TRANSMISSION_MATRIX[self._state]:
_logger.info("self._line_no = %d." % self._line_no)
raise ValueError("Unexpected state transition: %r -> %r." % (self._state, to_state))
self._state = to_state
def main():
with open(args.out_dfxml, "w") as out_fh:
with open(args.in_mapfile, "r") as in_fh:
parser = MapfileParser()
dobj = parser.parse(in_fh)
dobj.print_dfxml(output_fh=out_fh)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", action="store_true")
parser.add_argument("in_mapfile")
parser.add_argument("out_dfxml")
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
main()