aria2-protocol 0.3.9

Multi-protocol networking stack for aria2-rust: HTTP/HTTPS client, FTP/SFTP, full BitTorrent (DHT/PEX/MSE), and Metalink V3/V4 parser
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! SFTP file operations and high-level file I/O abstractions.
//!
//! Provides `SftpFileOps` for issuing file system operations (open, close,
//! read, write, stat, etc.) over an active SFTP session, plus `SftpRemoteFile`
//! for streaming read/write access to a remote file handle.
//!
//! ## Architecture
//!
//! ```text
//! SftpFileOps  -- issues SftpPacket requests via SftpSession
//!      |
//!      +-- open()  --> SftpRemoteFile (holds open handle)
//!      |                     |
//!      |                     +-- read_at() / write_at() / close()
//!      |
//!      +-- stat() / lstat() / set_stat() / mkdir() / rmdir() / ...
//! ```
//!
//! All operations translate into the corresponding `SftpPacket` variants and
//! delegate to the session's single serialized request path for request ID
//! assignment, response correlation, and packet I/O.

use tracing::{debug, warn};

use super::packet::{SSH_FX_EOF, SSH_FX_OK, SftpFileAttrs, SftpPacket};
use super::session::SftpSession;

#[cfg(test)]
mod tests;
mod types;

pub use types::{FileAttributes, FileOpError, OpenFlags};

async fn request(
    session: &SftpSession,
    packet: SftpPacket,
    operation: &'static str,
) -> Result<SftpPacket, FileOpError> {
    session
        .request(packet)
        .await
        .map_err(|error| FileOpError::session(operation, error))
}

fn unexpected_response(operation: &str, packet: SftpPacket) -> FileOpError {
    FileOpError::other(format!(
        "Unexpected response to {operation}: type={}",
        packet.packet_type()
    ))
}

fn expect_handle(
    response: SftpPacket,
    operation: &'static str,
    path: Option<&str>,
) -> Result<Vec<u8>, FileOpError> {
    match response {
        SftpPacket::Handle { handle, .. } => Ok(handle),
        SftpPacket::Status { code, message, .. } => {
            Err(FileOpError::from_status(operation, code, message, path))
        }
        other => Err(unexpected_response(operation, other)),
    }
}

fn expect_attrs(
    response: SftpPacket,
    operation: &'static str,
    path: &str,
) -> Result<FileAttributes, FileOpError> {
    match response {
        SftpPacket::Attrs { attrs, .. } => Ok(FileAttributes::from_wire(&attrs)),
        SftpPacket::Status { code, message, .. } => Err(FileOpError::from_status(
            operation,
            code,
            message,
            Some(path),
        )),
        other => Err(unexpected_response(operation, other)),
    }
}

fn expect_ok(
    response: SftpPacket,
    operation: &'static str,
    path: Option<&str>,
) -> Result<(), FileOpError> {
    match response {
        SftpPacket::Status {
            code: SSH_FX_OK, ..
        } => Ok(()),
        SftpPacket::Status { code, message, .. } => {
            Err(FileOpError::from_status(operation, code, message, path))
        }
        other => Err(unexpected_response(operation, other)),
    }
}

fn expect_name(
    response: SftpPacket,
    operation: &'static str,
    path: &str,
) -> Result<String, FileOpError> {
    match response {
        SftpPacket::Name { entries, .. } => entries
            .into_iter()
            .next()
            .map(|entry| entry.filename)
            .ok_or_else(|| FileOpError::other(format!("{operation} returned an empty name list"))),
        SftpPacket::Status { code, message, .. } => Err(FileOpError::from_status(
            operation,
            code,
            message,
            Some(path),
        )),
        other => Err(unexpected_response(operation, other)),
    }
}

/// An open remote file handle obtained via `SftpFileOps::open()`.
///
/// Provides streaming read/write access at arbitrary offsets. The handle is
/// automatically closed when dropped, but callers should prefer explicit
/// `close()` for error handling.
pub struct SftpRemoteFile<'a> {
    /// Reference to the session for issuing requests
    session: &'a SftpSession,
    /// The opaque file handle returned by the server (SSH_FXP_HANDLE)
    handle: Vec<u8>,
    /// Whether this handle has been closed
    closed: bool,
}

impl<'a> std::fmt::Debug for SftpRemoteFile<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SftpRemoteFile")
            .field("handle_len", &self.handle.len())
            .field("closed", &self.closed)
            .finish()
    }
}

impl<'a> SftpRemoteFile<'a> {
    fn new(session: &'a SftpSession, handle: Vec<u8>) -> Self {
        Self {
            session,
            handle,
            closed: false,
        }
    }

    /// Read up to `len` bytes starting at `offset` from the remote file.
    ///
    /// Returns the data bytes on success, or an empty Vec on EOF.
    pub async fn read_at(&self, offset: u64, len: u32) -> Result<Vec<u8>, FileOpError> {
        let pkt = SftpPacket::Read {
            request_id: 0, // Will be set by session.request()
            handle: self.handle.clone(),
            offset,
            length: len,
        };

        let resp = request(self.session, pkt, "READ").await?;

        match resp {
            SftpPacket::Data { data, .. } => Ok(data),
            SftpPacket::Status { code, .. } if code == SSH_FX_EOF => Ok(Vec::new()),
            SftpPacket::Status { code, message, .. } => {
                Err(FileOpError::from_status("Read", code, message, None))
            }
            other => Err(unexpected_response("READ", other)),
        }
    }

    /// Write `data` starting at `offset` to the remote file.
    pub async fn write_at(&self, offset: u64, data: &[u8]) -> Result<(), FileOpError> {
        let pkt = SftpPacket::Write {
            request_id: 0,
            handle: self.handle.clone(),
            offset,
            data: data.to_vec(),
        };

        let resp = request(self.session, pkt, "WRITE").await?;
        expect_ok(resp, "Write", None)
    }

    /// Close the remote file handle explicitly.
    ///
    /// Sends SSH_FXP_CLOSE and marks the handle as closed. Calling `close()`
    /// more than once is a no-op.
    pub async fn close(&mut self) -> Result<(), FileOpError> {
        if self.closed {
            return Ok(());
        }

        let pkt = SftpPacket::Close {
            request_id: 0,
            handle: self.handle.clone(),
        };

        let resp = request(self.session, pkt, "CLOSE").await?;
        expect_ok(resp, "Close", None)?;
        self.closed = true;
        Ok(())
    }
}

impl<'a> Drop for SftpRemoteFile<'a> {
    fn drop(&mut self) {
        if !self.closed {
            warn!(
                "[SFTP] SftpRemoteFile dropped without explicit close (handle_len={})",
                self.handle.len()
            );
            // Cannot await close() in drop; the handle will be orphaned on
            // the server side and eventually cleaned up when the session ends.
        }
    }
}

// =============================================================================
// SftpFileOps -- high-level file operation interface
// =============================================================================

/// High-level SFTP file operations bound to an active session.
///
/// Each method maps to one or more SFTP protocol packets and returns
/// ergonomic Rust types rather than raw protocol packets.
///
/// # Example
///
/// ```ignore
/// let session = SftpSession::open(&mut conn).await?;
/// let ops = SftpFileOps::new(&session);
///
/// let attr = ops.lstat("/remote/file.txt").await?;
/// println!("Size: {} bytes", attr.size);
///
/// let mut f = ops.open("/remote/file.txt", OpenFlags::readonly(), 0).await?;
/// let data = f.read_at(0, 4096).await?;
/// f.close().await?;
/// ```
pub struct SftpFileOps<'a> {
    session: &'a SftpSession,
}

impl<'a> std::fmt::Debug for SftpFileOps<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SftpFileOps").finish()
    }
}

impl<'a> SftpFileOps<'a> {
    /// Create a new file operations interface bound to the given session.
    pub fn new(session: &'a SftpSession) -> Self {
        Self { session }
    }

    // -----------------------------------------------------------------
    // File Open / Close
    // -----------------------------------------------------------------

    /// Open a remote file with the specified flags and initial attributes.
    ///
    /// Returns an `SftpRemoteFile` that supports streaming read/write at
    /// arbitrary offsets.
    pub async fn open(
        &self,
        path: &str,
        flags: OpenFlags,
        mode: u32,
    ) -> Result<SftpRemoteFile<'a>, FileOpError> {
        debug!("[SFTP] open({}, {})", path, flags);

        let attrs = SftpFileAttrs {
            flags: if mode != 0 {
                super::packet::SSH_FILEXFER_ATTR_PERMISSIONS
            } else {
                0
            },
            permissions: if mode != 0 { Some(mode) } else { None },
            ..Default::default()
        };

        let pkt = SftpPacket::Open {
            request_id: 0,
            filename: path.to_string(),
            flags: flags.bits(),
            attrs,
        };

        let resp = request(self.session, pkt, "OPEN").await?;
        let handle = expect_handle(resp, "Open", Some(path))?;
        debug!("[SFTP] open() got handle (len={})", handle.len());
        Ok(SftpRemoteFile::new(self.session, handle))
    }

    // -----------------------------------------------------------------
    // Stat / Lstat
    // -----------------------------------------------------------------

    /// Get file attributes, following symlinks (SSH_FXP_STAT).
    pub async fn stat(&self, path: &str) -> Result<FileAttributes, FileOpError> {
        let pkt = SftpPacket::Stat {
            request_id: 0,
            path: path.to_string(),
        };

        let resp = request(self.session, pkt, "STAT").await?;
        expect_attrs(resp, "Stat", path)
    }

    /// Get file attributes without following symlinks (SSH_FXP_LSTAT).
    pub async fn lstat(&self, path: &str) -> Result<FileAttributes, FileOpError> {
        let pkt = SftpPacket::Lstat {
            request_id: 0,
            path: path.to_string(),
        };

        let resp = request(self.session, pkt, "LSTAT").await?;
        expect_attrs(resp, "Lstat", path)
    }

    // -----------------------------------------------------------------
    // Setstat
    // -----------------------------------------------------------------

    /// Set file attributes by path (SSH_FXP_SETSTAT).
    pub async fn set_stat(&self, path: &str, attrs: &FileAttributes) -> Result<(), FileOpError> {
        let pkt = SftpPacket::Setstat {
            request_id: 0,
            path: path.to_string(),
            attrs: attrs.to_wire(),
        };

        let resp = request(self.session, pkt, "SETSTAT").await?;
        expect_ok(resp, "Setstat", Some(path))
    }

    // -----------------------------------------------------------------
    // Directory Operations
    // -----------------------------------------------------------------

    /// Open a directory for listing (SSH_FXP_OPENDIR).
    pub async fn opendir(&self, path: &str) -> Result<SftpRemoteFile<'a>, FileOpError> {
        let pkt = SftpPacket::Opendir {
            request_id: 0,
            path: path.to_string(),
        };

        let resp = request(self.session, pkt, "OPENDIR").await?;
        let handle = expect_handle(resp, "Opendir", Some(path))?;
        Ok(SftpRemoteFile::new(self.session, handle))
    }

    /// Read directory entries from an open directory handle (SSH_FXP_READDIR).
    ///
    /// Returns a list of `(filename, longname, FileAttributes)` tuples.
    /// An empty list indicates end-of-directory.
    pub async fn readdir(
        &self,
        dir_handle: &SftpRemoteFile<'_>,
    ) -> Result<Vec<(String, String, FileAttributes)>, FileOpError> {
        let pkt = SftpPacket::Readdir {
            request_id: 0,
            handle: dir_handle.handle.clone(),
        };

        let resp = request(self.session, pkt, "READDIR").await?;

        match resp {
            SftpPacket::Name { entries, .. } => Ok(entries
                .into_iter()
                .map(|e| (e.filename, e.longname, FileAttributes::from_wire(&e.attrs)))
                .collect()),
            SftpPacket::Status { code, .. } if code == SSH_FX_EOF => Ok(Vec::new()),
            SftpPacket::Status { code, message, .. } => {
                Err(FileOpError::from_status("Readdir", code, message, None))
            }
            other => Err(unexpected_response("READDIR", other)),
        }
    }

    /// Create a directory (SSH_FXP_MKDIR).
    pub async fn mkdir(&self, path: &str) -> Result<(), FileOpError> {
        let pkt = SftpPacket::Mkdir {
            request_id: 0,
            path: path.to_string(),
            attrs: SftpFileAttrs::default(),
        };

        let resp = request(self.session, pkt, "MKDIR").await?;
        expect_ok(resp, "Mkdir", Some(path))
    }

    /// Remove a directory (SSH_FXP_RMDIR).
    pub async fn rmdir(&self, path: &str) -> Result<(), FileOpError> {
        let pkt = SftpPacket::Rmdir {
            request_id: 0,
            path: path.to_string(),
        };

        let resp = request(self.session, pkt, "RMDIR").await?;
        expect_ok(resp, "Rmdir", Some(path))
    }

    // -----------------------------------------------------------------
    // File Manipulation
    // -----------------------------------------------------------------

    /// Delete a file (SSH_FXP_REMOVE).
    pub async fn remove(&self, path: &str) -> Result<(), FileOpError> {
        let pkt = SftpPacket::Remove {
            request_id: 0,
            filename: path.to_string(),
        };

        let resp = request(self.session, pkt, "REMOVE").await?;
        expect_ok(resp, "Remove", Some(path))
    }

    /// Rename a file or directory (SSH_FXP_RENAME).
    pub async fn rename(&self, old_path: &str, new_path: &str) -> Result<(), FileOpError> {
        let pkt = SftpPacket::Rename {
            request_id: 0,
            old_path: old_path.to_string(),
            new_path: new_path.to_string(),
        };

        let resp = request(self.session, pkt, "RENAME").await?;
        expect_ok(resp, "Rename", Some(old_path))
    }

    /// Canonicalize a path (SSH_FXP_REALPATH).
    pub async fn realpath(&self, path: &str) -> Result<String, FileOpError> {
        let pkt = SftpPacket::Realpath {
            request_id: 0,
            path: path.to_string(),
        };

        let resp = request(self.session, pkt, "REALPATH").await?;
        expect_name(resp, "REALPATH", path)
    }

    /// Read the target of a symbolic link (SSH_FXP_READLINK).
    pub async fn readlink(&self, path: &str) -> Result<String, FileOpError> {
        let pkt = SftpPacket::Readlink {
            request_id: 0,
            path: path.to_string(),
        };

        let resp = request(self.session, pkt, "READLINK").await?;
        expect_name(resp, "READLINK", path)
    }

    /// Create a symbolic link (SSH_FXP_SYMLINK).
    pub async fn symlink(&self, link_path: &str, target_path: &str) -> Result<(), FileOpError> {
        let pkt = SftpPacket::Symlink {
            request_id: 0,
            link_path: link_path.to_string(),
            target_path: target_path.to_string(),
        };

        let resp = request(self.session, pkt, "SYMLINK").await?;
        expect_ok(resp, "Symlink", Some(link_path))
    }
}

// =============================================================================
// Unit Tests
// =============================================================================