close the local/remote files in finally blocks in sftp get & put, so fds don't get lost. bug #379240

This commit is contained in:
Robey Pointer 2009-07-19 15:19:10 -07:00
parent ac42ba88d7
commit fe35f44f2e
1 changed files with 33 additions and 25 deletions

View File

@ -546,9 +546,11 @@ class SFTPClient (BaseSFTP):
"""
file_size = os.stat(localpath).st_size
fl = file(localpath, 'rb')
try:
fr = self.file(remotepath, 'wb')
fr.set_pipelined(True)
size = 0
try:
while True:
data = fl.read(32768)
if len(data) == 0:
@ -557,8 +559,10 @@ class SFTPClient (BaseSFTP):
size += len(data)
if callback is not None:
callback(size, file_size)
fl.close()
finally:
fr.close()
finally:
fl.close()
s = self.stat(remotepath)
if s.st_size != size:
raise IOError('size mismatch in put! %d != %d' % (s.st_size, size))
@ -584,7 +588,9 @@ class SFTPClient (BaseSFTP):
fr = self.file(remotepath, 'rb')
file_size = self.stat(remotepath).st_size
fr.prefetch()
try:
fl = file(localpath, 'wb')
try:
size = 0
while True:
data = fr.read(32768)
@ -594,7 +600,9 @@ class SFTPClient (BaseSFTP):
size += len(data)
if callback is not None:
callback(size, file_size)
finally:
fl.close()
finally:
fr.close()
s = os.stat(localpath)
if s.st_size != size: