summaryrefslogtreecommitdiff
path: root/paramiko/sftp.py
diff options
context:
space:
mode:
authorRobey Pointer <robey@lag.net>2004-06-10 18:08:50 +0000
committerRobey Pointer <robey@lag.net>2004-06-10 18:08:50 +0000
commit146417c56c3a9911b0c4f4eec212e69d2d994455 (patch)
tree37265947a13649cb3dfaa3b99f4f3fb5085ade21 /paramiko/sftp.py
parentf0ba3c482eb937a450cac99b578a6197ed54101b (diff)
downloadparamiko-146417c56c3a9911b0c4f4eec212e69d2d994455.tar.gz
[project @ Arch-1:robey@lag.net--2003-public%secsh--dev--1.0--patch-60]
limit read/write requests to 32KB, advertise 32KB max packet size one of the unit tests was failing because the openssh sftp server was dropping the connection without any error. turns out they have a maximum allowed write size (possibly around 64KB). the sftp rfcs have a small hint that some servers may drop read/write requests of greater than 32KB. so, all reads are limited to 32KB, and all writes > 32KB are now chopped up and sent in 32KB chunks. this seems to keep openssh happy. also, we now advertise 32KB max packet size instead of 8KB (the speed improves a lot), and log when we read/write a packet. and sftp files are flushed on seek.
Diffstat (limited to 'paramiko/sftp.py')
-rw-r--r--paramiko/sftp.py18
1 files changed, 15 insertions, 3 deletions
diff --git a/paramiko/sftp.py b/paramiko/sftp.py
index 0e7f48f7..d5948ed9 100644
--- a/paramiko/sftp.py
+++ b/paramiko/sftp.py
@@ -15,7 +15,7 @@
# details.
#
# You should have received a copy of the GNU Lesser General Public License
-# along with Foobar; if not, write to the Free Software Foundation, Inc.,
+# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
import struct, socket
@@ -121,6 +121,11 @@ class SFTPError (Exception):
class SFTPFile (BufferedFile):
+
+ # some sftp servers will choke if you send read/write requests larger than
+ # this size.
+ MAX_REQUEST_SIZE = 32768
+
def __init__(self, sftp, handle, mode='r', bufsize=-1):
BufferedFile.__init__(self)
self.sftp = sftp
@@ -143,16 +148,23 @@ class SFTPFile (BufferedFile):
self.sftp._request(CMD_CLOSE, self.handle)
def _read(self, size):
+ size = min(size, self.MAX_REQUEST_SIZE)
t, msg = self.sftp._request(CMD_READ, self.handle, long(self._realpos), int(size))
if t != CMD_DATA:
raise SFTPError('Expected data')
return msg.get_string()
def _write(self, data):
- t, msg = self.sftp._request(CMD_WRITE, self.handle, long(self._realpos), str(data))
+ offset = 0
+ while offset < len(data):
+ chunk = min(len(data) - offset, self.MAX_REQUEST_SIZE)
+ t, msg = self.sftp._request(CMD_WRITE, self.handle, long(self._realpos + offset),
+ str(data[offset : offset + chunk]))
+ offset += chunk
return len(data)
def seek(self, offset, whence=0):
+ self.flush()
if whence == self.SEEK_SET:
self._realpos = self._pos = offset
elif whence == self.SEEK_CUR:
@@ -160,7 +172,7 @@ class SFTPFile (BufferedFile):
self._pos += offset
else:
self._realpos = self._pos = self._get_size() + offset
- self._rbuffer = self._wbuffer = ''
+ self._rbuffer = ''
def stat(self):
"""