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.
22
from cStringIO import StringIO
25
from bzrlib import errors
28
FORMAT_ONE = "Bazaar pack format 1 (introduced in 0.18)"
31
_whitespace_re = re.compile('[\t\n\x0b\x0c\r ]')
34
def _check_name(name):
35
"""Do some basic checking of 'name'.
37
At the moment, this just checks that there are no whitespace characters in a
40
:raises InvalidRecordError: if name is not valid.
41
:seealso: _check_name_encoding
43
if _whitespace_re.search(name) is not None:
44
raise errors.InvalidRecordError("%r is not a valid name." % (name,))
47
def _check_name_encoding(name):
48
"""Check that 'name' is valid UTF-8.
50
This is separate from _check_name because UTF-8 decoding is relatively
51
expensive, and we usually want to avoid it.
53
:raises InvalidRecordError: if name is not valid UTF-8.
57
except UnicodeDecodeError, e:
58
raise errors.InvalidRecordError(str(e))
61
class ContainerWriter(object):
62
"""A class for writing containers."""
64
def __init__(self, write_func):
67
:param write_func: a callable that will be called when this
68
ContainerWriter needs to write some bytes.
70
self._write_func = write_func
71
self.current_offset = 0
74
"""Begin writing a container."""
75
self.write_func(FORMAT_ONE + "\n")
77
def write_func(self, bytes):
78
self._write_func(bytes)
79
self.current_offset += len(bytes)
82
"""Finish writing a container."""
85
def add_bytes_record(self, bytes, names):
86
"""Add a Bytes record with the given names.
88
:param bytes: The bytes to insert.
89
:param names: The names to give the inserted bytes.
90
:return: An offset, length tuple. The offset is the offset
91
of the record within the container, and the length is the
92
length of data that will need to be read to reconstitute the
93
record. These offset and length can only be used with the pack
94
interface - they might be offset by headers or other such details
95
and thus are only suitable for use by a ContainerReader.
97
current_offset = self.current_offset
101
self.write_func(str(len(bytes)) + "\n")
104
# Make sure we're writing valid names. Note that we will leave a
105
# half-written record if a name is bad!
107
self.write_func(name + "\n")
109
self.write_func("\n")
110
# Finally, the contents.
111
self.write_func(bytes)
112
# return a memo of where we wrote data to allow random access.
113
return current_offset, self.current_offset - current_offset
116
class ReadVFile(object):
117
"""Adapt a readv result iterator to a file like protocol."""
119
def __init__(self, readv_result):
120
self.readv_result = readv_result
121
# the most recent readv result block
125
if (self._string is None or
126
self._string.tell() == self._string_length):
127
length, data = self.readv_result.next()
128
self._string_length = len(data)
129
self._string = StringIO(data)
131
def read(self, length):
133
result = self._string.read(length)
134
if len(result) < length:
135
raise errors.BzrError('request for too much data from a readv hunk.')
139
"""Note that readline will not cross readv segments."""
141
result = self._string.readline()
142
if self._string.tell() == self._string_length and result[-1] != '\n':
143
raise errors.BzrError('short readline in the readvfile hunk.')
147
def make_readv_reader(transport, filename, requested_records):
148
"""Create a ContainerReader that will read selected records only.
150
:param transport: The transport the pack file is located on.
151
:param filename: The filename of the pack file.
152
:param requested_records: The record offset, length tuples as returned
153
by add_bytes_record for the desired records.
155
readv_blocks = [(0, len(FORMAT_ONE)+1)]
156
readv_blocks.extend(requested_records)
157
result = ContainerReader(ReadVFile(
158
transport.readv(filename, readv_blocks)))
162
class BaseReader(object):
164
def __init__(self, source_file):
167
:param source_file: a file-like object with `read` and `readline`
170
self._source = source_file
172
def reader_func(self, length=None):
173
return self._source.read(length)
175
def _read_line(self):
176
line = self._source.readline()
177
if not line.endswith('\n'):
178
raise errors.UnexpectedEndOfContainerError()
179
return line.rstrip('\n')
182
class ContainerReader(BaseReader):
183
"""A class for reading Bazaar's container format."""
185
def iter_records(self):
186
"""Iterate over the container, yielding each record as it is read.
188
Each yielded record will be a 2-tuple of (names, callable), where names
189
is a ``list`` and bytes is a function that takes one argument,
192
You **must not** call the callable after advancing the interator to the
193
next record. That is, this code is invalid::
195
record_iter = container.iter_records()
196
names1, callable1 = record_iter.next()
197
names2, callable2 = record_iter.next()
198
bytes1 = callable1(None)
200
As it will give incorrect results and invalidate the state of the
203
:raises ContainerError: if any sort of containter corruption is
204
detected, e.g. UnknownContainerFormatError is the format of the
205
container is unrecognised.
206
:seealso: ContainerReader.read
209
return self._iter_records()
211
def iter_record_objects(self):
212
"""Iterate over the container, yielding each record as it is read.
214
Each yielded record will be an object with ``read`` and ``validate``
215
methods. Like with iter_records, it is not safe to use a record object
216
after advancing the iterator to yield next record.
218
:raises ContainerError: if any sort of containter corruption is
219
detected, e.g. UnknownContainerFormatError is the format of the
220
container is unrecognised.
221
:seealso: iter_records
224
return self._iter_record_objects()
226
def _iter_records(self):
227
for record in self._iter_record_objects():
230
def _iter_record_objects(self):
232
record_kind = self.reader_func(1)
233
if record_kind == 'B':
235
reader = BytesRecordReader(self._source)
237
elif record_kind == 'E':
238
# End marker. There are no more records.
240
elif record_kind == '':
241
# End of stream encountered, but no End Marker record seen, so
242
# this container is incomplete.
243
raise errors.UnexpectedEndOfContainerError()
245
# Unknown record type.
246
raise errors.UnknownRecordTypeError(record_kind)
248
def _read_format(self):
249
format = self._read_line()
250
if format != FORMAT_ONE:
251
raise errors.UnknownContainerFormatError(format)
254
"""Validate this container and its records.
256
Validating consumes the data stream just like iter_records and
257
iter_record_objects, so you cannot call it after
258
iter_records/iter_record_objects.
260
:raises ContainerError: if something is invalid.
263
for record_names, read_bytes in self.iter_records():
265
for name in record_names:
266
_check_name_encoding(name)
267
# Check that the name is unique. Note that Python will refuse
268
# to decode non-shortest forms of UTF-8 encoding, so there is no
269
# risk that the same unicode string has been encoded two
271
if name in all_names:
272
raise errors.DuplicateRecordNameError(name)
274
excess_bytes = self.reader_func(1)
275
if excess_bytes != '':
276
raise errors.ContainerHasExcessDataError(excess_bytes)
279
class BytesRecordReader(BaseReader):
284
You can either validate or read a record, you can't do both.
286
:returns: A tuple of (names, callable). The callable can be called
287
repeatedly to obtain the bytes for the record, with a max_length
288
argument. If max_length is None, returns all the bytes. Because
289
records can be arbitrarily large, using None is not recommended
290
unless you have reason to believe the content will fit in memory.
292
# Read the content length.
293
length_line = self._read_line()
295
length = int(length_line)
297
raise errors.InvalidRecordError(
298
"%r is not a valid length." % (length_line,))
300
# Read the list of names.
303
name = self._read_line()
309
self._remaining_length = length
310
return names, self._content_reader
312
def _content_reader(self, max_length):
313
if max_length is None:
314
length_to_read = self._remaining_length
316
length_to_read = min(max_length, self._remaining_length)
317
self._remaining_length -= length_to_read
318
bytes = self.reader_func(length_to_read)
319
if len(bytes) != length_to_read:
320
raise errors.UnexpectedEndOfContainerError()
324
"""Validate this record.
326
You can either validate or read, you can't do both.
328
:raises ContainerError: if this record is invalid.
330
names, read_bytes = self.read()
332
_check_name_encoding(name)