~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: Eric Holmberg
  • Date: 2008-05-06 15:02:27 UTC
  • mto: This revision was merged to the branch mainline in revision 3449.
  • Revision ID: eholmberg@arrow.com-20080506150227-l3arq1yntdvnoxum
Fix for Bug #215426 in which bzr can cause a MemoryError in socket.recv while
downloading large packs over http.  This patch limits the request size for
socket.recv to avoid this problem.

Changes:
Added mock file object bzrlib.tests.file_utils.
Added new parameters to bzrlib.osutils.pumpfile.
Added unit tests for bzrlib.osutils.pumpfile.
Added usage of bzrlib.osutils.pumpfile to bzrlib.transport.http.response.

Show diffs side-by-side

added added

removed removed

Lines of Context:
530
530
    return False
531
531
 
532
532
 
533
 
def pumpfile(fromfile, tofile):
 
533
def pumpfile(from_file, to_file, read_length=-1, buff_size=32768):
534
534
    """Copy contents of one file to another.
535
 
    
 
535
 
 
536
    The read_length can either be -1 to read to end-of-file (EOF) or
 
537
    it can specify the maximum number of bytes to read.
 
538
 
 
539
    The buff_size represents the maximum size for each read operation
 
540
    performed on from_file.
 
541
 
536
542
    :return: The number of bytes copied.
537
543
    """
538
 
    BUFSIZE = 32768
539
544
    length = 0
540
 
    while True:
541
 
        b = fromfile.read(BUFSIZE)
542
 
        if not b:
543
 
            break
544
 
        tofile.write(b)
545
 
        length += len(b)
 
545
    if read_length >= 0:
 
546
        # read specified number of bytes
 
547
 
 
548
        while read_length > 0:
 
549
            num_bytes_to_read = min(read_length, buff_size)
 
550
 
 
551
            block = from_file.read(num_bytes_to_read)
 
552
            if not block:
 
553
                # EOF reached
 
554
                break
 
555
            to_file.write(block)
 
556
 
 
557
            actual_bytes_read = len(block)
 
558
            read_length -= actual_bytes_read
 
559
            length += actual_bytes_read
 
560
    else:
 
561
        # read to EOF
 
562
        while True:
 
563
            block = from_file.read(buff_size)
 
564
            if not block:
 
565
                # EOF reached
 
566
                break
 
567
            to_file.write(block)
 
568
            length += len(block)
546
569
    return length
547
570
 
548
571