66
67
:param write_func: a callable that will be called when this
67
68
ContainerWriter needs to write some bytes.
69
self.write_func = write_func
70
self._write_func = write_func
71
self.current_offset = 0
72
74
"""Begin writing a container."""
73
75
self.write_func(FORMAT_ONE + "\n")
77
def write_func(self, bytes):
78
self._write_func(bytes)
79
self.current_offset += len(bytes)
76
82
"""Finish writing a container."""
77
83
self.write_func("E")
79
85
def add_bytes_record(self, bytes, names):
80
"""Add a Bytes record with the given 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. Each name is
90
a tuple of bytestrings. The bytestrings may not contain
92
:return: An offset, length tuple. The offset is the offset
93
of the record within the container, and the length is the
94
length of data that will need to be read to reconstitute the
95
record. These offset and length can only be used with the pack
96
interface - they might be offset by headers or other such details
97
and thus are only suitable for use by a ContainerReader.
99
current_offset = self.current_offset
82
101
self.write_func("B")
84
103
self.write_func(str(len(bytes)) + "\n")
105
for name_tuple in names:
87
106
# Make sure we're writing valid names. Note that we will leave a
88
107
# half-written record if a name is bad!
90
self.write_func(name + "\n")
108
for name in name_tuple:
110
self.write_func('\x00'.join(name_tuple) + "\n")
92
112
self.write_func("\n")
93
113
# Finally, the contents.
94
114
self.write_func(bytes)
115
# return a memo of where we wrote data to allow random access.
116
return current_offset, self.current_offset - current_offset
119
class ReadVFile(object):
120
"""Adapt a readv result iterator to a file like protocol."""
122
def __init__(self, readv_result):
123
self.readv_result = readv_result
124
# the most recent readv result block
128
if (self._string is None or
129
self._string.tell() == self._string_length):
130
length, data = self.readv_result.next()
131
self._string_length = len(data)
132
self._string = StringIO(data)
134
def read(self, length):
136
result = self._string.read(length)
137
if len(result) < length:
138
raise errors.BzrError('request for too much data from a readv hunk.')
142
"""Note that readline will not cross readv segments."""
144
result = self._string.readline()
145
if self._string.tell() == self._string_length and result[-1] != '\n':
146
raise errors.BzrError('short readline in the readvfile hunk.')
150
def make_readv_reader(transport, filename, requested_records):
151
"""Create a ContainerReader that will read selected records only.
153
:param transport: The transport the pack file is located on.
154
:param filename: The filename of the pack file.
155
:param requested_records: The record offset, length tuples as returned
156
by add_bytes_record for the desired records.
158
readv_blocks = [(0, len(FORMAT_ONE)+1)]
159
readv_blocks.extend(requested_records)
160
result = ContainerReader(ReadVFile(
161
transport.readv(filename, readv_blocks)))
97
165
class BaseReader(object):
197
265
all_names = set()
198
266
for record_names, read_bytes in self.iter_records():
200
for name in record_names:
201
_check_name_encoding(name)
268
for name_tuple in record_names:
269
for name in name_tuple:
270
_check_name_encoding(name)
202
271
# Check that the name is unique. Note that Python will refuse
203
272
# to decode non-shortest forms of UTF-8 encoding, so there is no
204
273
# risk that the same unicode string has been encoded two
205
274
# different ways.
206
if name in all_names:
207
raise errors.DuplicateRecordNameError(name)
275
if name_tuple in all_names:
276
raise errors.DuplicateRecordNameError(name_tuple)
277
all_names.add(name_tuple)
209
278
excess_bytes = self.reader_func(1)
210
279
if excess_bytes != '':
211
280
raise errors.ContainerHasExcessDataError(excess_bytes)