Skip to content

Commit ca15e11

Browse files
committed
Fix a couple of SFTP block size issues and a couple of doc errors
This commit fixes an issue with AsyncSSH not honoring the block_size in calls to the high level SFTP get/put/copy functions when the default block size is smaller than the requested value. It also, fixes a problem when a block_size of 0 is passed in to these functions. Now, a block_size of 0 is treated the same as leaving it set to the default, automatically choosing a value based on the advertised server limits, falling back to 16 KB if the server doesn't advertise limits. This commit also fixes a copy of errors in the documentation for the SFTP put() and copy() methods. Thanks go to Krzysztof Kotlenga for finding and reporting these issues!
1 parent e83874a commit ca15e11

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

asyncssh/sftp.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ async def symlink(self, oldpath: bytes, newpath: bytes) -> None:
272272
"""Create a symbolic link"""
273273

274274
@async_context_manager
275-
async def open(self, path: bytes, mode: str) -> SFTPFileProtocol:
275+
async def open(self, path: bytes, mode: str,
276+
block_size: int = -1) -> SFTPFileProtocol:
276277
"""Open a file"""
277278

278279

@@ -797,8 +798,10 @@ async def run(self) -> None:
797798
"""Perform parallel file copy"""
798799

799800
try:
800-
self._src = await self._srcfs.open(self._srcpath, 'rb')
801-
self._dst = await self._dstfs.open(self._dstpath, 'wb')
801+
self._src = await self._srcfs.open(self._srcpath, 'rb',
802+
block_size=0)
803+
self._dst = await self._dstfs.open(self._dstpath, 'wb',
804+
block_size=0)
802805

803806
if self._progress_handler and self._total_bytes == 0:
804807
self._progress_handler(self._srcpath, self._dstpath, 0, 0)
@@ -3787,7 +3790,7 @@ async def _begin_copy(self, srcfs: _SFTPFSProtocol, dstfs: _SFTPFSProtocol,
37873790
error_handler: SFTPErrorHandler) -> None:
37883791
"""Begin a new file upload, download, or copy"""
37893792

3790-
if block_size == -1:
3793+
if block_size <= 0:
37913794
block_size = min(srcfs.limits.max_read_len,
37923795
dstfs.limits.max_write_len)
37933796

@@ -3989,7 +3992,7 @@ async def put(self, localpaths: _SFTPPaths,
39893992
watch out for links that result in loops.
39903993
39913994
The block_size argument specifies the size of read and write
3992-
requests issued when downloading the files, defaulting to
3995+
requests issued when uploading the files, defaulting to
39933996
the maximum allowed by the server, or 16 KB if the server
39943997
doesn't advertise limits.
39953998
@@ -4095,8 +4098,8 @@ async def copy(self, srcpaths: _SFTPPaths,
40954098
watch out for links that result in loops.
40964099
40974100
The block_size argument specifies the size of read and write
4098-
requests issued when downloading the files, defaulting to
4099-
the maximum allowed by the server, or 16 KB if the server
4101+
requests issued when copying the files, defaulting to the
4102+
maximum allowed by the server, or 16 KB if the server
41004103
doesn't advertise limits.
41014104
41024105
The max_requests argument specifies the maximum number of
@@ -7622,7 +7625,8 @@ async def symlink(self, oldpath: bytes, newpath: bytes) -> None:
76227625
os.symlink(_to_local_path(oldpath), _to_local_path(newpath))
76237626

76247627
@async_context_manager
7625-
async def open(self, path: bytes, mode: str) -> LocalFile:
7628+
async def open(self, path: bytes, mode: str,
7629+
block_size: int = -1) -> LocalFile:
76267630
"""Open a local file"""
76277631

76287632
# pylint: disable=unused-argument

0 commit comments

Comments
 (0)