2
# Copyright (c) 2012 Canonical
4
# This file is part of Checkbox.
6
# Storm is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU Lesser General Public License as
8
# published by the Free Software Foundation; either version 2.1 of
9
# the License, or (at your option) any later version.
11
# Storm is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Lesser General Public License for more details.
16
# You should have received a copy of the GNU Lesser General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
29
from checkbox.journal.event import (
38
" ctime=(?P<ctime>\d+)"
40
" sequence=(?P<sequence>\d+)"
41
" size=(?P<size>\d+)L?"
42
" events=(?P<events>\d+)L?"
43
" offset=(?P<offset>\d+)L?"
44
" event_offset=(?P<event_offset>\d+)L?")
45
JOURNAL_RE = re.compile(JOURNAL_PATTERN)
49
"""Simple class to extract info from a journal header event."""
62
def __init__(self, other=None):
75
def copyFrom(self, other):
76
"""Copy the parameters from another header object into this one.
78
:param other: Other header object.
81
self.sequence = other.sequence
82
self.ctime = other.ctime
83
self.size = other.size
84
self.num_events = other.num_events
85
self.file_offset = other.file_offset
86
self.event_offset = other.event_offset
87
self._valid = other._valid
91
"""Return true if the header is valid."""
94
def incrementSequence(self):
95
"""Increment the sequence number."""
98
def addFileOffset(self, offset):
99
"""Add to the file offset.
101
:param offset: Offset to add.
103
self.file_offset += offset
105
def addEventOffset(self, offset):
106
"""Add to the event offset.
108
:param offset: Offset to add.
110
self.event_offset += offset
112
def extractEvent(self, event):
113
"""Extract data from an event and update the header.
115
:param event: Event from which to extract.
117
if event.type != EventType.GENERIC:
118
return EventOutcome.NO_EVENT
120
match = JOURNAL_RE.match(event.data)
122
return EventOutcome.NO_EVENT
124
self.ctime = int(match.group("ctime"))
125
self.id = match.group("id")
126
self.sequence = int(match.group("sequence"))
127
self.size = int(match.group("size"))
128
self.num_events = int(match.group("events"))
129
self.file_offset = int(match.group("offset"))
130
self.event_offset = int(match.group("event_offset"))
133
return EventOutcome.OK
136
class HeaderReader(Header):
138
def read(self, reader):
139
"""Read the header from a file.
141
:param reader: Journal reader object.
144
outcome = reader.readEvent(event)
146
if outcome != EventOutcome.OK:
149
if event.type != EventType.GENERIC:
150
return EventOutcome.NO_EVENT
152
return self.extractEvent(event)
155
class HeaderWriter(Header):
157
def write(self, writer, file=None):
158
"""Write the header to a file.
160
:param reader: Journal writer object.
162
event = Event(EventType.GENERIC)
163
if not self.generateEvent(event):
164
return EventOutcome.UNKNOWN_ERROR
166
return writer.writeGlobalEvent(event, file, True)
168
def generateEvent(self, event):
169
"""Generate a header event.
171
:param event: Generic event to contain the header data.
181
" event_offset=%d" % (