Skip to content

Commit 15aac74

Browse files
kephaleshcheklein
andauthored
Add _pipe_file and test (#47)
* Add _pipe_file and test * Remove sync_wrapper * Fix formatting * Fix formatting * Fix formatting * Fix formatting * Remove flush call, flush is implicit in SFTP --------- Co-authored-by: Ivan Shcheklein <shcheklein@gmail.com>
1 parent 64cc6ef commit 15aac74

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

sshfs/spec.py

+13
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,16 @@ async def _cat_file(self, path, **kwargs):
331331
async with self._pool.get() as channel:
332332
async with channel.open(path, "rb") as f:
333333
return await f.read()
334+
335+
@wrap_exceptions
336+
async def _pipe_file(self, path, data, chunksize=50 * 2**20, **kwargs):
337+
"""Asynchronously writes the given data to a remote file in chunks."""
338+
await self._makedirs(self._parent(path), exist_ok=True)
339+
340+
async with self._pool.get() as channel:
341+
async with channel.open(path, "wb") as f:
342+
for i in range(0, len(data), chunksize):
343+
chunk = data[i : i + chunksize]
344+
await f.write(chunk)
345+
346+
self.invalidate_cache(path)

tests/test_sshfs.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ def read_random_file(name):
296296
return stream.read()
297297

298298
with futures.ThreadPoolExecutor() as executor:
299-
300299
write_futures, _ = futures.wait(
301300
[executor.submit(create_random_file) for _ in range(64)],
302301
return_when=futures.ALL_COMPLETED,
@@ -361,3 +360,15 @@ def test_cat_file_sync(fs, remote_dir):
361360
assert (
362361
read_content == test_content
363362
), "The content read from the file does not match the content written."
363+
364+
365+
def test_pipe_file(fs, remote_dir):
366+
test_data = b"Test data for pipe_file" * (2**20) # 1 MB of test data
367+
test_file_path = remote_dir + "/test_pipe_file.txt"
368+
369+
fs.pipe_file(test_file_path, test_data)
370+
371+
with fs.open(test_file_path, "rb") as f:
372+
assert (
373+
f.read() == test_data
374+
), "The data read from the file does not match the data written."

0 commit comments

Comments
 (0)