This file is indexed.

/usr/lib/python3/dist-packages/postgresql/copyman.py is in python3-postgresql 1.1.0-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
##
# .copyman - COPY manager
##
"""
Manage complex COPY operations; one-to-many COPY streaming.

Primarily this module houses the `CopyManager` class, and the `transfer`
function for a high-level interface to using the `CopyManager`.
"""
import sys
from abc import abstractmethod, abstractproperty
from collections import Iterator
from .python.element import Element, ElementSet
from .python.structlib import ulong_unpack, ulong_pack
from .protocol.buffer import pq_message_stream
from .protocol.element3 import CopyData, CopyDone, Complete, cat_messages
from .protocol.xact3 import Complete as xactComplete

#: 10KB buffer for COPY messages by default.
default_buffer_size = 1024 * 10

class Fault(Exception):
	pass

class ProducerFault(Fault):
	"""
	Exception raised when the Producer caused an exception.

	Normally, Producer faults are fatal.
	"""
	def __init__(self, manager):
		self.manager = manager

	def __str__(self):
		return "producer raised exception"

class ReceiverFault(Fault):
	"""
	Exception raised when Receivers cause an exception.

	Faults should be trapped if recovery from an exception is
	possible, or if the failed receiver is optional to the succes of the
	operation.

	The 'manager' attribute is the CopyManager that raised the fault.

	The 'faults' attribute is a dictionary mapping the receiver to the exception
	instance raised.
	"""
	def __init__(self, manager, faults):
		self.manager = manager
		self.faults = faults

	def __str__(self):
		return "{0} faults occurred".format(len(self.faults))

class CopyFail(Exception):
	"""
	Exception thrown by the CopyManager when the COPY operation failed.

	The 'manager' attribute the CopyManager that raised the CopyFail.

	The 'reason' attribute is a string indicating why it failed.

	The 'receiver_faults' attribute is a mapping of receivers to exceptions that were
	raised on exit.

	The 'producer_fault' attribute specifies if the producer raise an exception
	on exit.
	"""
	def __init__(self, manager, reason = None,
		receiver_faults = None,
		producer_fault = None,
	):
		self.manager = manager
		self.reason = reason
		self.receiver_faults = receiver_faults or {}
		self.producer_fault = producer_fault

	def __str__(self):
		return self.reason or 'copy aborted'

# The identifier for PQv3 copy data.
PROTOCOL_PQv3 = "PQv3"
# The identifier for iterables of copy data sequences.
# iter([[row1, row2], [row3, row4]])
PROTOCOL_CHUNKS = "CHUNKS"
# The protocol identifier for NULL producers and receivers.
PROTOCOL_NULL = None

class ChunkProtocol(object):
	__slots__ = ('buffer',)
	def __init__(self):
		self.buffer = pq_message_stream()

	def __call__(self, data):
		self.buffer.write(bytes(data))
		return [x[1] for x in self.buffer.read()]

# Null protocol mapping.
def EmptyView(arg):
	return memoryview(b'')
def EmptyList(arg):
	return []
def ReturnNone(arg):
	return None
# Zero-Transformation
def NoTransformation(arg):
	return arg

# Copy protocols being at the Python level; *not* wire/serialization format.
copy_protocol_mappings = {
	# PQv3 -> Chunks
	(PROTOCOL_PQv3, PROTOCOL_CHUNKS) : ChunkProtocol,
	# Chunks -> PQv3
	(PROTOCOL_CHUNKS, PROTOCOL_PQv3) : lambda: cat_messages,
	# Null Producers and Receivers
	(PROTOCOL_NULL, PROTOCOL_PQv3) : lambda: EmptyView,
	(PROTOCOL_NULL, PROTOCOL_CHUNKS) : lambda: EmptyList,
	(PROTOCOL_PQv3, PROTOCOL_NULL) : lambda: ReturnNone,
	(PROTOCOL_CHUNKS, PROTOCOL_NULL) : lambda: ReturnNone,
	# Zero Transformations
	(PROTOCOL_NULL, PROTOCOL_NULL) : lambda: NoTransformation,
	(PROTOCOL_CHUNKS, PROTOCOL_CHUNKS) : lambda: NoTransformation,
	(PROTOCOL_PQv3, PROTOCOL_PQv3) : lambda: NoTransformation,
}

# Used to manage the conversions of COPY data.
# Notably, chunks -> PQv3 or PQv3 -> chunks.
class CopyTransformer(object):
	__slots__ = ('current', 'transformers', 'get')
	def __init__(self, source_protocol, target_protocols):
		self.current = {}
		self.transformers = {
			x : copy_protocol_mappings[(source_protocol, x)]()
			for x in set(target_protocols)
		}
		self.get = self.current.__getitem__

	def __call__(self, data):
		for protocol, transformer in self.transformers.items():
			self.current[protocol] = transformer(data)

##
# This is the object that does the magic.
# It tracks the state of the wire.
# It ends when non-COPY data is found.
class WireState(object):
	"""
	Manages the state of the wire.

	This class manages three possible positions:

	 1. Between wire messages
	 2. Inside message header
	 3. Inside message (with complete header)

	The wire state will become unusable when the configured condition is True.
	"""
	__slots__ = ('remaining_bytes', 'size_fragment', 'final_view', 'condition',)

	def update(self, view, getlen = ulong_unpack, len = len):
		"""
		Given the state of the COPY and new data, advance the position on the
		COPY stream.
		"""
		# Only usable until the terminating condition.
		if self.final_view is not None:
			raise RuntimeError("wire state encountered exceptional condition")

		nmessages = 0

		# State carried over from prior run.
		remaining_bytes = self.remaining_bytes
		size_fragment = self.size_fragment

		# Terminating condition.
		CONDITION = self.condition

		# Is it a continuation of a message header?
		if remaining_bytes == -1:
			##
			# Inside message header; after message type.
			# Continue adding to the 'size_fragment'
			# until there are four bytes to unpack.
			##
			o = len(size_fragment)
			size_fragment += bytes(view[:4-o])
			if len(size_fragment) == 4:
				# The size fragment is completed; only part
				# of the fragment remains to be consumed.
				remaining_bytes = getlen(size_fragment) - o
				size_fragment = b''
			else:
				assert len(size_fragment) < 4
				# size_fragment got updated..

		if remaining_bytes >= 0:
			vlen = len(view)
			while True:
				if remaining_bytes:
					##
					# Inside message body. Message length has been unpacked.
					##
					view = view[remaining_bytes:]
					# How much is remaining now?
					rb = remaining_bytes - vlen
					if rb <= 0:
						# Finished it.
						vlen = -rb
						remaining_bytes = 0
						nmessages += 1
					else:
						vlen = 0
						remaining_bytes = rb
				##
				# In between protocol messages.
				##
				if not view:
					# no more data to analyze
					break
				# There is at least one byte in the view.
				if CONDITION(view[0]):
					# State is dead now.
					# User needs to handle unexpected message, then continue.
					self.final_view = view
					assert remaining_bytes == 0
					break
				if vlen < 5:
					# Header continuation.
					remaining_bytes = -1
					view = view[1:]
					size_fragment += bytes(view)
					# Not enough left for the header of the next message?
					break
				# Update remaining_bytes to include the header, and start over.
				remaining_bytes = getlen(view[1:5]) + 1

		# Update the state for the next update.
		self.remaining_bytes, self.size_fragment = (
			remaining_bytes, size_fragment,
		)
		# Emit the number of messages "consumed" this round.
		return nmessages

	def __init__(self, condition = (CopyData.type[0].__ne__ if isinstance(memoryview(b'f')[0], int) else CopyData.type.__ne__)):
		self.remaining_bytes = 0
		self.size_fragment = b''
		self.final_view = None
		self.condition = condition

class Fitting(Element):
	_e_label = 'FITTING'

	def _e_metas(self):
		yield None, '[' + self.state + ']'

	@abstractproperty
	def protocol(self):
		"""
		The COPY data format produced or consumed.
		"""

	# Used to setup the Receiver/Producer
	def __enter__(self):
		pass

	# Used to tear down the Receiver/Producer
	def __exit__(self, typ, val, tb):
		pass

class Producer(Fitting, Iterator):
	_e_label = 'PRODUCER'

	def _e_metas(self):
		for x in super()._e_metas():
			yield x
		yield 'data', str(self.total_bytes / (1024**2)) + 'MB'
		yield 'messages', self.total_messages
		yield 'average size', (self.total_bytes / self.total_messages)

	def __init__(self):
		self.total_messages = 0
		self.total_bytes = 0

	@abstractmethod
	def realign(self):
		"""
		Method implemented by producers that emit COPY data that is not
		guaranteed to be aligned.

		This is only necessary in failure cases where receivers still need more
		data to complete the message.
		"""

	@abstractmethod
	def __next__(self):
		"""
		Produce the next set of data.
		"""

class Receiver(Fitting):
	_e_label = 'RECEIVER'

	@abstractmethod
	def transmit(self):
		"""
		Finish the reception of the accepted data.
		"""

	@abstractmethod
	def accept(self, data):
		"""
		Take the data object to be processed.
		"""

class NullProducer(Producer):
	"""
	Produces no copy data.
	"""
	_e_factors = ()
	protocol = PROTOCOL_NULL

	def realign(self):
		# Never needs to realigned.
		pass

	def __next__(self):
		raise StopIteration

class IteratorProducer(Producer):
	_e_factors = ('iterator',)
	protocol = PROTOCOL_CHUNKS

	def __init__(self, iterator):
		self.iterator = iter(iterator)
		self.__next__ = self.iterator.__next__
		super().__init__()

	def realign(self):
		# Never needs to realign; data is emitted on message boundaries.
		pass

	def __next__(self, next = next):
		n = next(self.iterator)
		self.total_messages += len(n)
		self.total_bytes += sum(map(len, n))
		return n

class ProtocolProducer(Producer):
	"""
	Producer using a PQv3 data stream.

	Normally, this class needs to be subclassed as it assumes that the given
	recv_into function will write COPY messages.
	"""
	protocol = PROTOCOL_PQv3

	@abstractmethod
	def recover(self, view):
		"""
		Given a view containing data read from the wire, recover the
		controller's state.

		This needs to be implemented by subclasses in order for the
		ProtocolReceiver to pass control back to the original state machine.
		"""

	##
	# When a COPY is interrupted, this can be used to accommodate
	# the original state machine to identify the message boundaries.
	def realign(self):
		s = self._state

		if s is None:
			# It's already aligned.
			self.nextchunk = iter(()).__next__
			return

		if s.final_view:
			# It was at the end or non-COPY.
			for_producer = bytes(s.final_view)
			for_receivers = b''
		elif s.remaining_bytes == -1:
			# In the middle of a message header.
			for_producer = CopyData.type + s.size_fragment
			# receivers:
			header = (self._state.size_fragment.ljust(3, b'\x00') + b'\x04')
			# Don't include the already sent parts.
			buf = header[len(self._state.size_fragment):]
			bodylen = ulong_unpack(header) - 4
			# This will often cause an invalid copy data error,
			# but it doesn't matter much because we will issue a copy fail.
			buf += b'\x00' * bodylen
			for_receivers = buf
		elif s.remaining_bytes > 0:
			# In the middle of a message.
			for_producer = CopyData.type + ulong_pack(s.remaining_bytes + 4)
			for_receivers = b'\x00' * self._state.remaining_bytes
		else:
			for_producer = for_receivers = b''

		self.recover(for_producer)
		if for_receivers:
			self.nextchunk = iter((for_receivers,)).__next__
		else:
			self.nextchunk = iter(()).__next__

	def process_copy_data(self, view):
		self.total_messages += self._state.update(view)
		if self._state.final_view is not None:
			# It's not COPY data.
			fv = self._state.final_view
			# Only publish up to the final_view.
			if fv:
				view = view[:-len(fv)]
			# The next next() will handle the async, error, or completion.
			self.recover(fv)
			self._state = None
		self.total_bytes += len(view)
		return view

	# Given a view, begin tracking the state of the wire.
	def track_state(self, view):
		self._state = WireState()
		self.nextchunk = self.recv_view
		return self.process_copy_data(view)

	# The usual method for receiving more data.
	def recv_view(self):
		view = self.buffer_view[:self.recv_into(self.buffer, self.buffer_size)]
		if not view:
			# Zero read; let the subclass handle the situation.
			self.recover(memoryview(b''))
			return self.nextchunk()
		view = self.process_copy_data(view)
		return view

	def nextchunk(self):
		raise RuntimeError("producer not properly initialized")

	def __next__(self):
		return self.nextchunk()

	def __init__(self,
		recv_into : "callable taking writable buffer and size",
		buffer_size = default_buffer_size
	):
		super().__init__()
		self.recv_into = recv_into
		self.buffer_size = buffer_size
		self.buffer = bytearray(buffer_size)
		self.buffer_view = memoryview(self.buffer)
		self._state = None

class StatementProducer(ProtocolProducer):
	_e_factors = ('statement', 'parameters',)

	def _e_metas(self):
		for x in super()._e_metas():
			yield x

	@property
	def state(self):
		if self._chunks is None:
			return 'created'
		return 'producing'

	def count(self):
		return self._chunks.count()

	def command(self):
		return self._chunks.command()

	def __init__(self, statement, *args, **kw):
		super().__init__(statement.database.pq.socket.recv_into, **kw)
		self.statement = statement
		self.parameters = args
		self._chunks = None

	##
	# Take any data held by the statement's chunks and connection.
	def confiscate(self, next = next):
		current = []
		try:
			while not current:
				current.extend(next(self._chunks))
		except StopIteration:
			if not current:
				# End of COPY.
				raise
		pq = self._chunks.database.pq
		buffer = cat_messages(current) + pq.message_buffer.getvalue() + (pq.read_data or b'')
		view = memoryview(buffer)
		pq.read_data = None
		pq.message_buffer.truncate()
		# Reconstruct the buffer from the already parsed lines.
		r = self.track_state(view)
		# XXX: Better way? Probably shouldn't do the full track_state if complete..
		if self._chunks._xact.state is xactComplete:
			# It's over, don't hand off to recv_view.
			self.nextchunk = self.confiscate
			assert self._state.final_view is None
		return r

	def recover(self, view):
		# Method used when non-COPY data is found.
		self._chunks.database.pq.message_buffer.write(bytes(view))
		self.nextchunk = self.confiscate

	def __enter__(self):
		super().__enter__()
		if self._chunks is not None:
			raise RuntimeError("receiver already used")
		self._chunks = self.statement.chunks(*self.parameters)
		# Start by confiscating the connection state.
		self.nextchunk = self.confiscate

	def __exit__(self, typ, val, tb):
		if typ is None or issubclass(typ, Exception):
			db = self.statement.database
			if not db.closed and self._chunks._xact is not None:
				# The COPY transaction is still happening,
				# force an interrupt if the connection still exists.
				db.interrupt()
				if db.pq.xact:
					# Raise, CopyManager should trap.
					db._pq_complete()
		super().__exit__(typ, val, tb)

class NullReceiver(Receiver):
	_e_factors = ()
	protocol = PROTOCOL_NULL
	state = 'null'

	def transmit(self):
		# Nothing to do.
		pass

	def accept(self, data):
		pass

class ProtocolReceiver(Receiver):
	protocol = PROTOCOL_PQv3
	__slots__ = ('send', 'view')

	def __init__(self, send):
		super().__init__()
		self.send = send
		self.view = memoryview(b'')

	def accept(self, data):
		self.view = data

	def transmit(self):
		while self.view:
			self.view = self.view[self.send(self.view):]

	def __enter__(self):
		return self

	def __exit__(self, typ, val, tb):
		pass

class StatementReceiver(ProtocolReceiver):
	_e_factors = ('statement', 'parameters',)
	__slots__ = ProtocolReceiver.__slots__ + _e_factors + ('xact',)

	def _e_metas(self):
		yield None, '[' + self.state + ']'

	def __init__(self, statement, *parameters):
		self.statement = statement
		self.parameters = parameters
		self.xact = None
		super().__init__(statement.database.pq.socket.send,)

	# XXX: A bit of a hack...
	# This is actually a good indication that statements need a .copy()
	# execution method for producing a "CopyCursor" that reads or writes.
	class WireReady(BaseException):
		pass
	def raise_wire_ready(self):
		raise self.WireReady()
		yield None

	def __enter__(self, iter = iter):
		super().__enter__()
		# Get the connection in the COPY state.
		try:
			self.statement.load_chunks(
				iter(self.raise_wire_ready()), *self.parameters
			)
		except self.WireReady:
			# It's a BaseException; nothing should trap it.
			# Note the transaction object; we'll use it on exit.
			self.xact = self.statement.database.pq.xact

	def __exit__(self, typ, val, tb):
		if self.xact is None:
			# Nothing to do.
			return super().__exit__(typ, val, tb)

		if self.view:
			# The realigned producer emitted the necessary
			# data for message boundary alignment.
			#
			# In this case, we unconditionally fail.
			pq = self.statement.database.pq
			# There shouldn't be any message_data, atm.
			pq.message_data = bytes(self.view)
			self.statement.database._pq_complete()
			# It is possible for a non-alignment view to exist in cases of
			# faults. However, exit should *not* be called in those cases.
			##
		elif typ is None:
			# Success?
			self.xact.messages = self.xact.CopyDoneSequence
			# If not, this will blow up.
			self.statement.database._pq_complete()
			# Find the complete message for command and count.
			for x in self.xact.messages_received():
				if getattr(x, 'type', None) == Complete.type:
					self._complete_message = x
		elif issubclass(typ, Exception):
			# Likely raises. CopyManager should trap.
			self.statement.database._pq_complete()

		return super().__exit__(typ, val, tb)

	def count(self):
		return self._complete_message.extract_count()

	def command(self):
		return self._complete_message.extract_command().decode('ascii')

class CallReceiver(Receiver):
	"""
	Call the given object with a list of COPY lines.
	"""
	_e_factors = ('callable',)
	protocol = PROTOCOL_CHUNKS

	def __init__(self, callable):
		self.callable = callable
		self.lines = None
		super().__init__()

	def transmit(self):
		if self.lines is not None:
			self.callable(self.lines)
		self.lines = None

	def accept(self, lines):
		self.lines = lines

class CopyManager(Element, Iterator):
	"""
	A class for managing COPY operations.

	Connects the producer to the receivers.
	"""
	_e_label = 'COPY'
	_e_factors = ('producer', 'receivers',)

	def _e_metas(self):
		yield None, '[' + self.state + ']'

	@property
	def state(self):
		if self.transformer is None:
			return 'initialized'
		return str(self.producer.total_messages) + ' messages transferred'

	def __init__(self, producer, *receivers):
		self.producer = producer
		self.transformer = None
		self.receivers = ElementSet(receivers)
		self._seen_stop_iteration = False
		rp = set()
		add = rp.add
		for x in self.receivers:
			add(x.protocol)
		self.protocols = rp

	def __enter__(self):
		if self.transformer:
			raise RuntimeError("copy already started")
		self._stats = (0, 0)
		self.transformer = CopyTransformer(self.producer.protocol, self.protocols)
		self.producer.__enter__()
		try:
			for x in self.receivers:
				x.__enter__()
		except Exception:
			self.__exit__(*sys.exc_info())
		return self

	def __exit__(self, typ, val, tb):
		##
		# Exiting the CopyManager is a fairly complex operation.
		#
		# In cases of failure, re-alignment may need to happen
		# for when the receivers are not on a message boundary.
		##
		if typ is not None and not issubclass(typ, Exception):
			# Don't bother, it's an interrupt or sufficient resources.
			return

		profail = None
		try:
			# Does nothing if the COPY was successful.
			self.producer.realign()
			try:
				##
				# If the producer is not aligned to a message boundary,
				# it can emit completion data that will put the receivers
				# back on track.
				# This last service call will move that data onto the receivers.
				self._service_producer()
				##
				# The receivers need to handle any new data in their __exit__.
			except StopIteration:
				# No re-alignment needed.
				pass

			self.producer.__exit__(typ, val, tb)
		except Exception as x:
			# reference profail later.
			profail = x

		# No receivers? It wasn't a success.
		if not self.receivers:
			raise CopyFail(self, "no receivers", producer_fault = profail)

		exit_faults = {}
		for x in self.receivers:
			try:
				x.__exit__(typ, val, tb)
			except Exception as e:
				exit_faults[x] = e

		if typ or exit_faults or profail or not self._seen_stop_iteration:
			raise CopyFail(self,
				"could not complete the COPY operation",
				receiver_faults = exit_faults or None,
				producer_fault = profail
			)

	def reconcile(self, r):
		"""
		Reconcile a receiver that faulted.

		This method should be used to add back a receiver that failed to
		complete its write operation, but is capable of completing the
		operation at this time.
		"""
		if r.protocol not in self.protocols:
			raise RuntimeError("cannot add new receivers to copy operations")
		r.transmit()
		# Okay, add it back.
		self.receivers.add(r)

	def _service_producer(self):
		# Setup current data.
		if not self.receivers:
			# No receivers to take the data.
			raise StopIteration

		try:
			nextdata = next(self.producer)
		except StopIteration:
			# Should be over.
			self._seen_stop_iteration = True
			raise
		except Exception:
			raise ProducerFault(self)

		self.transformer(nextdata)

		# Distribute data to receivers.
		for x in self.receivers:
			x.accept(self.transformer.get(x.protocol))

	def _service_receivers(self):
		faults = {}
		for x in self.receivers:
			# Process all the receivers.
			try:
				x.transmit()
			except Exception as e:
				faults[x] = e
		if faults:
			# The CopyManager is eager to continue the operation.
			for x in faults:
				self.receivers.discard(x)
			raise ReceiverFault(self, faults)

	# Run the COPY to completion.
	def run(self):
		with self:
			try:
				while True:
					self._service_producer()
					self._service_receivers()
			except StopIteration:
				# It's done.
				pass

	def __iter__(self):
		return self

	def __next__(self):
		messages = self.producer.total_messages
		bytes = self.producer.total_bytes

		self._service_producer()
		# Record the progress in case a receiver faults.
		self._stats = (
			self._stats[0] + (self.producer.total_messages - messages),
			self._stats[1] + (self.producer.total_bytes - bytes),
		)
		self._service_receivers()
		# Return the progress.
		current_stats = self._stats
		self._stats = (0, 0)
		return current_stats

def transfer(producer, *receivers):
	"""
	Perform a COPY operation using the given statements::

		>>> import copyman
		>>> copyman.transfer(src.prepare("COPY table TO STDOUT"), dst.prepare("COPY table FROM STDIN"))
	"""
	cm = CopyManager(
		StatementProducer(producer),
		*[x if isinstance(x, Receiver) else StatementReceiver(x) for x in receivers]
	)
	cm.run()
	return (cm.producer.total_messages, cm.producer.total_bytes)