forked from lablup/bssh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_transfer.rs
More file actions
361 lines (314 loc) · 13.2 KB
/
file_transfer.rs
File metadata and controls
361 lines (314 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! SFTP file transfer operations.
//!
//! This module provides file transfer capabilities including:
//! - Single file upload/download
//! - Recursive directory upload/download
//! - Support for glob patterns
use russh_sftp::{client::SftpSession, protocol::OpenFlags};
use std::path::Path;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
/// Chunk size used for streaming SFTP uploads/downloads.
///
/// Sized to match russh-sftp's default MAX_WRITE_LENGTH (255 KiB) so each
/// chunk maps to a single SFTP WRITE/READ packet without further fragmentation.
const STREAM_CHUNK_SIZE: usize = 255 * 1024;
/// Stream `reader` to `writer` in fixed-size chunks so a single transfer never
/// holds more than `STREAM_CHUNK_SIZE` of file payload in memory at once.
async fn stream_copy<R, W>(reader: &mut R, writer: &mut W) -> std::io::Result<()>
where
R: tokio::io::AsyncRead + Unpin,
W: tokio::io::AsyncWrite + Unpin,
{
let mut buf = vec![0u8; STREAM_CHUNK_SIZE];
loop {
let n = reader.read(&mut buf).await?;
if n == 0 {
break;
}
writer.write_all(&buf[..n]).await?;
}
Ok(())
}
use super::connection::Client;
impl Client {
/// Upload a file with sftp to the remote server.
///
/// `src_file_path` is the path to the file on the local machine.
/// `dest_file_path` is the path to the file on the remote machine.
/// Some sshd_config does not enable sftp by default, so make sure it is enabled.
/// A config line like a `Subsystem sftp internal-sftp` or
/// `Subsystem sftp /usr/lib/openssh/sftp-server` is needed in the sshd_config in remote machine.
pub async fn upload_file<T: AsRef<Path>, U: Into<String>>(
&self,
src_file_path: T,
//fa993: This cannot be AsRef<Path> because of underlying lib constraints as described here
//https://github.com/AspectUnk/russh-sftp/issues/7#issuecomment-1738355245
dest_file_path: U,
) -> Result<(), super::Error> {
// start sftp session
let channel = self.get_channel().await?;
channel.request_subsystem(true, "sftp").await?;
let sftp = SftpSession::new(channel.into_stream()).await?;
// Open local file for streaming reads (avoids loading whole file in memory).
let mut local_file = tokio::fs::File::open(src_file_path)
.await
.map_err(super::Error::IoError)?;
let mut file = sftp
.open_with_flags(
dest_file_path,
OpenFlags::CREATE | OpenFlags::TRUNCATE | OpenFlags::WRITE | OpenFlags::READ,
)
.await?;
stream_copy(&mut local_file, &mut file)
.await
.map_err(super::Error::IoError)?;
file.flush().await.map_err(super::Error::IoError)?;
file.shutdown().await.map_err(super::Error::IoError)?;
Ok(())
}
/// Download a file from the remote server using sftp.
///
/// `remote_file_path` is the path to the file on the remote machine.
/// `local_file_path` is the path to the file on the local machine.
/// Some sshd_config does not enable sftp by default, so make sure it is enabled.
/// A config line like a `Subsystem sftp internal-sftp` or
/// `Subsystem sftp /usr/lib/openssh/sftp-server` is needed in the sshd_config in remote machine.
pub async fn download_file<T: AsRef<Path>, U: Into<String>>(
&self,
remote_file_path: U,
local_file_path: T,
) -> Result<(), super::Error> {
// start sftp session
let channel = self.get_channel().await?;
channel.request_subsystem(true, "sftp").await?;
let sftp = SftpSession::new(channel.into_stream()).await?;
// open remote file for reading
let mut remote_file = sftp
.open_with_flags(remote_file_path, OpenFlags::READ)
.await?;
// Stream remote file directly to local disk to avoid buffering the
// whole file in memory.
let mut local_file = tokio::fs::File::create(local_file_path.as_ref())
.await
.map_err(super::Error::IoError)?;
stream_copy(&mut remote_file, &mut local_file)
.await
.map_err(super::Error::IoError)?;
remote_file
.shutdown()
.await
.map_err(super::Error::IoError)?;
local_file.flush().await.map_err(super::Error::IoError)?;
Ok(())
}
/// Upload a directory to the remote server using sftp recursively.
///
/// `local_dir_path` is the path to the directory on the local machine.
/// `remote_dir_path` is the path to the directory on the remote machine.
/// All files and subdirectories will be uploaded recursively.
pub async fn upload_dir<T: AsRef<Path>, U: Into<String>>(
&self,
local_dir_path: T,
remote_dir_path: U,
) -> Result<(), super::Error> {
let local_dir = local_dir_path.as_ref();
let remote_dir = remote_dir_path.into();
// Verify local directory exists
if !local_dir.is_dir() {
return Err(super::Error::IoError(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Local directory does not exist: {local_dir:?}"),
)));
}
// Start SFTP session
let channel = self.get_channel().await?;
channel.request_subsystem(true, "sftp").await?;
let sftp = SftpSession::new(channel.into_stream()).await?;
// Create remote directory if it doesn't exist
let _ = sftp.create_dir(&remote_dir).await; // Ignore error if already exists
// Process directory recursively
self.upload_dir_recursive(&sftp, local_dir, &remote_dir)
.await?;
Ok(())
}
/// Helper function to recursively upload directory contents
#[allow(clippy::only_used_in_recursion)]
fn upload_dir_recursive<'a>(
&'a self,
sftp: &'a SftpSession,
local_dir: &'a Path,
remote_dir: &'a str,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), super::Error>> + Send + 'a>>
{
Box::pin(async move {
// Read local directory contents
let entries = tokio::fs::read_dir(local_dir)
.await
.map_err(super::Error::IoError)?;
let mut entries = entries;
while let Some(entry) = entries.next_entry().await.map_err(super::Error::IoError)? {
let path = entry.path();
let file_name = entry.file_name();
let file_name_str = file_name.to_string_lossy();
let remote_path = format!("{remote_dir}/{file_name_str}");
let metadata = entry.metadata().await.map_err(super::Error::IoError)?;
if metadata.is_dir() {
// Create remote directory and recurse
let _ = sftp.create_dir(&remote_path).await; // Ignore error if already exists
self.upload_dir_recursive(sftp, &path, &remote_path).await?;
} else if metadata.is_file() {
// Stream local file to remote in chunks instead of loading
// the entire file in memory before send.
let mut local_file = tokio::fs::File::open(&path)
.await
.map_err(super::Error::IoError)?;
let mut remote_file = sftp
.open_with_flags(
&remote_path,
OpenFlags::CREATE | OpenFlags::TRUNCATE | OpenFlags::WRITE,
)
.await?;
stream_copy(&mut local_file, &mut remote_file)
.await
.map_err(super::Error::IoError)?;
remote_file.flush().await.map_err(super::Error::IoError)?;
remote_file
.shutdown()
.await
.map_err(super::Error::IoError)?;
}
}
Ok(())
})
}
/// Download a directory from the remote server using sftp recursively.
///
/// `remote_dir_path` is the path to the directory on the remote machine.
/// `local_dir_path` is the path to the directory on the local machine.
/// All files and subdirectories will be downloaded recursively.
pub async fn download_dir<T: AsRef<Path>, U: Into<String>>(
&self,
remote_dir_path: U,
local_dir_path: T,
) -> Result<(), super::Error> {
let local_dir = local_dir_path.as_ref();
let remote_dir = remote_dir_path.into();
// Start SFTP session
let channel = self.get_channel().await?;
channel.request_subsystem(true, "sftp").await?;
let sftp = SftpSession::new(channel.into_stream()).await?;
// Create local directory if it doesn't exist
tokio::fs::create_dir_all(local_dir)
.await
.map_err(super::Error::IoError)?;
// Process directory recursively
self.download_dir_recursive(&sftp, &remote_dir, local_dir)
.await?;
Ok(())
}
/// Helper function to recursively download directory contents
#[allow(clippy::only_used_in_recursion)]
fn download_dir_recursive<'a>(
&'a self,
sftp: &'a SftpSession,
remote_dir: &'a str,
local_dir: &'a Path,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), super::Error>> + Send + 'a>>
{
Box::pin(async move {
// Read remote directory contents
let entries = sftp.read_dir(remote_dir).await?;
for entry in entries {
let name = entry.file_name();
let metadata = entry.metadata();
// Skip . and .. (already handled by iterator)
if name == "." || name == ".." {
continue;
}
let remote_path = format!("{remote_dir}/{name}");
let local_path = local_dir.join(&name);
if metadata.file_type().is_dir() {
// Create local directory and recurse
tokio::fs::create_dir_all(&local_path)
.await
.map_err(super::Error::IoError)?;
self.download_dir_recursive(sftp, &remote_path, &local_path)
.await?;
} else if metadata.file_type().is_file() {
// Stream remote file directly to local disk in chunks.
let mut remote_file =
sftp.open_with_flags(&remote_path, OpenFlags::READ).await?;
let mut local_file = tokio::fs::File::create(&local_path)
.await
.map_err(super::Error::IoError)?;
stream_copy(&mut remote_file, &mut local_file)
.await
.map_err(super::Error::IoError)?;
remote_file
.shutdown()
.await
.map_err(super::Error::IoError)?;
local_file.flush().await.map_err(super::Error::IoError)?;
}
}
Ok(())
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::{
io::Cursor,
pin::Pin,
task::{Context, Poll},
};
use tokio::io::AsyncWrite;
#[derive(Default)]
struct RecordingWriter {
bytes: Vec<u8>,
write_lengths: Vec<usize>,
}
impl AsyncWrite for RecordingWriter {
fn poll_write(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<std::io::Result<usize>> {
self.write_lengths.push(buf.len());
self.bytes.extend_from_slice(buf);
Poll::Ready(Ok(buf.len()))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
Poll::Ready(Ok(()))
}
}
#[tokio::test]
async fn stream_copy_writes_sftp_sized_chunks() {
let input = vec![0xAB; STREAM_CHUNK_SIZE * 2 + 17];
let mut reader = Cursor::new(input.clone());
let mut writer = RecordingWriter::default();
stream_copy(&mut reader, &mut writer).await.unwrap();
assert_eq!(writer.bytes, input);
assert_eq!(
writer.write_lengths,
vec![STREAM_CHUNK_SIZE, STREAM_CHUNK_SIZE, 17]
);
}
}