1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
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.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Container format for Bazaar data.
19
"Containers" and "records" are described in doc/developers/container-format.txt.
24
from bzrlib import errors
27
FORMAT_ONE = "Bazaar pack format 1"
30
_whitespace_re = re.compile('[\t\n\x0b\x0c\r ]')
33
def _check_name(name):
34
"""Do some basic checking of 'name'.
36
At the moment, this just checks that there are no whitespace characters in a
39
:raises InvalidRecordError: if name is not valid.
40
:seealso: _check_name_encoding
42
if _whitespace_re.search(name) is not None:
43
raise errors.InvalidRecordError("%r is not a valid name." % (name,))
46
def _check_name_encoding(name):
47
"""Check that 'name' is valid UTF-8.
49
This is separate from _check_name because UTF-8 decoding is relatively
50
expensive, and we usually want to avoid it.
52
:raises InvalidRecordError: if name is not valid UTF-8.
56
except UnicodeDecodeError, e:
57
raise errors.InvalidRecordError(str(e))
60
class ContainerWriter(object):
61
"""A class for writing containers."""
63
def __init__(self, write_func):
66
:param write_func: a callable that will be called when this
67
ContainerWriter needs to write some bytes.
69
self.write_func = write_func
72
"""Begin writing a container."""
73
self.write_func(FORMAT_ONE + "\n")
76
"""Finish writing a container."""
79
def add_bytes_record(self, bytes, names):
80
"""Add a Bytes record with the given names."""
84
self.write_func(str(len(bytes)) + "\n")
87
# Make sure we're writing valid names. Note that we will leave a
88
# half-written record if a name is bad!
90
self.write_func(name + "\n")
93
# Finally, the contents.
94
self.write_func(bytes)
97
class BaseReader(object):
99
def __init__(self, reader_func):
102
:param reader_func: a callable that takes one optional argument,
103
``size``, and returns at most that many bytes. When the callable
104
returns less than the requested number of bytes, then the end of the
105
file/stream has been reached.
107
self.reader_func = reader_func
109
def _read_line(self):
110
"""Read a line from the input stream.
112
This is a simple but inefficient implementation that just reads one byte
113
at a time. Lines should not be very long, so this is probably
116
:returns: a line, without the trailing newline
118
# XXX: Have a maximum line length, to prevent malicious input from
119
# consuming an unreasonable amount of resources?
120
# -- Andrew Bennetts, 2007-05-07.
122
while not line.endswith('\n'):
123
byte = self.reader_func(1)
125
raise errors.UnexpectedEndOfContainerError()
130
class ContainerReader(BaseReader):
131
"""A class for reading Bazaar's container format."""
133
def iter_records(self):
134
"""Iterate over the container, yielding each record as it is read.
136
Each yielded record will be a 2-tuple of (names, callable), where names
137
is a ``list`` and bytes is a function that takes one argument,
140
You **must not** call the callable after advancing the interator to the
141
next record. That is, this code is invalid::
143
record_iter = container.iter_records()
144
names1, callable1 = record_iter.next()
145
names2, callable2 = record_iter.next()
146
bytes1 = callable1(None)
148
As it will give incorrect results and invalidate the state of the
151
:raises ContainerError: if any sort of containter corruption is
152
detected, e.g. UnknownContainerFormatError is the format of the
153
container is unrecognised.
154
:seealso: ContainerReader.read
157
return self._iter_records()
159
def iter_record_objects(self):
160
"""Iterate over the container, yielding each record as it is read.
162
Each yielded record will be an object with ``read`` and ``validate``
163
methods. Like with iter_records, it is not safe to use a record object
164
after advancing the iterator to yield next record.
166
:raises ContainerError: if any sort of containter corruption is
167
detected, e.g. UnknownContainerFormatError is the format of the
168
container is unrecognised.
169
:seealso: iter_records
172
return self._iter_record_objects()
174
def _iter_records(self):
175
for record in self._iter_record_objects():
178
def _iter_record_objects(self):
180
record_kind = self.reader_func(1)
181
if record_kind == 'B':
183
reader = BytesRecordReader(self.reader_func)
185
elif record_kind == 'E':
186
# End marker. There are no more records.
188
elif record_kind == '':
189
# End of stream encountered, but no End Marker record seen, so
190
# this container is incomplete.
191
raise errors.UnexpectedEndOfContainerError()
193
# Unknown record type.
194
raise errors.UnknownRecordTypeError(record_kind)
196
def _read_format(self):
197
format = self._read_line()
198
if format != FORMAT_ONE:
199
raise errors.UnknownContainerFormatError(format)
202
"""Validate this container and its records.
204
Validating consumes the data stream just like iter_records and
205
iter_record_objects, so you cannot call it after
206
iter_records/iter_record_objects.
208
:raises ContainerError: if something is invalid.
211
for record_names, read_bytes in self.iter_records():
213
for name in record_names:
214
_check_name_encoding(name)
215
# Check that the name is unique. Note that Python will refuse
216
# to decode non-shortest forms of UTF-8 encoding, so there is no
217
# risk that the same unicode string has been encoded two
219
if name in all_names:
220
raise errors.DuplicateRecordNameError(name)
222
excess_bytes = self.reader_func(1)
223
if excess_bytes != '':
224
raise errors.ContainerHasExcessDataError(excess_bytes)
227
class BytesRecordReader(BaseReader):
232
You can either validate or read a record, you can't do both.
234
:returns: A tuple of (names, callable). The callable can be called
235
repeatedly to obtain the bytes for the record, with a max_length
236
argument. If max_length is None, returns all the bytes. Because
237
records can be arbitrarily large, using None is not recommended
238
unless you have reason to believe the content will fit in memory.
240
# Read the content length.
241
length_line = self._read_line()
243
length = int(length_line)
245
raise errors.InvalidRecordError(
246
"%r is not a valid length." % (length_line,))
248
# Read the list of names.
251
name = self._read_line()
257
self._remaining_length = length
258
return names, self._content_reader
260
def _content_reader(self, max_length):
261
if max_length is None:
262
length_to_read = self._remaining_length
264
length_to_read = min(max_length, self._remaining_length)
265
self._remaining_length -= length_to_read
266
bytes = self.reader_func(length_to_read)
267
if len(bytes) != length_to_read:
268
raise errors.UnexpectedEndOfContainerError()
272
"""Validate this record.
274
You can either validate or read, you can't do both.
276
:raises ContainerError: if this record is invalid.
278
names, read_bytes = self.read()
280
_check_name_encoding(name)