aria2-protocol 0.2.1

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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
//! SFTP File Operations
//!
//! Provides a high-level API for SFTP file and directory operations built entirely
//! on the pure Rust `packet.rs` codec and russh channel I/O. Maps SFTP protocol
//! errors to application-level error codes for consistent error handling.
//!
//! ## Operation Categories
//!
//! - **File operations**: open, close, read, write, stat, fstat
//! - **Directory operations**: opendir, readdir, mkdir, rmdir
//! - **Path operations**: realpath, rename, unlink, symlink, readlink
//! - **Attribute operations**: setstat, setfstat
//!
//! ## Architecture
//!
//! ```text
//! SftpFileOps  ->  SftpSession::request(SftpPacket)  ->  channel encode/send
//!       |                    |                              |
//!   High-level API    Request ID management           recv/decode response
//!       |                    |                              |
//!       v                    v                              v
//! FileAttributes     SftpPacket (request)          SftpPacket (response)
//! ```

use std::sync::Arc;
use tracing::{debug, warn};

use super::packet::{
    SSH_FX_EOF, SSH_FX_NO_SUCH_FILE, SSH_FX_PERMISSION_DENIED, SSH_FXF_APPEND, SSH_FXF_CREAT,
    SSH_FXF_EXCL, SSH_FXF_READ, SSH_FXF_TRUNC, SSH_FXF_WRITE, SftpFileAttrs, is_fatal_error,
    is_retryable_error, status_code_description,
};
use super::session::SftpSession;

/// Default buffer size for read operations (32 KB)
#[allow(dead_code)] // Referenced by test_buf_size_constants; will be used when SFTP read chunking is implemented
const DEFAULT_READ_BUF_SIZE: usize = 32 * 1024;
/// Default buffer size for write operations (32 KB)
#[allow(dead_code)] // Referenced by test_buf_size_constants; will be used when SFTP write chunking is implemented
const DEFAULT_WRITE_BUF_SIZE: usize = 32 * 1024;

// =============================================================================
// File Attributes
// =============================================================================

/// Represents file metadata as returned by SFTP STAT/LSTAT/FSTAT operations.
///
/// This struct mirrors the SFTP protocol v3 `attrs` structure with concrete
/// values (no Option wrappers) for ease of use in application code.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FileAttributes {
    /// File size in bytes
    pub size: u64,
    /// Owner user ID
    pub uid: u32,
    /// Owner group ID
    pub gid: u32,
    /// Permission/mode bits (e.g., 0o644, 0o755)
    pub permissions: u32,
    /// Last access time (Unix timestamp)
    pub atime: u64,
    /// Last modification time (Unix timestamp)
    pub mtime: u64,
    /// True if this entry represents a directory
    pub is_directory: bool,
    /// True if this entry represents a regular file
    pub is_regular_file: bool,
    /// True if this entry represents a symbolic link
    pub is_symlink: bool,
}

impl Default for FileAttributes {
    fn default() -> Self {
        Self {
            size: 0,
            uid: 0,
            gid: 0,
            permissions: 0o644,
            atime: 0,
            mtime: 0,
            is_directory: false,
            is_regular_file: false,
            is_symlink: false,
        }
    }
}

impl std::fmt::Display for FileAttributes {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let type_str = if self.is_directory {
            "dir"
        } else if self.is_regular_file {
            "file"
        } else if self.is_symlink {
            "symlink"
        } else {
            "unknown"
        };
        write!(
            f,
            "{}(size={}, perm={:o}, uid={}, gid={})",
            type_str, self.size, self.permissions, self.uid, self.gid
        )
    }
}

impl From<&SftpFileAttrs> for FileAttributes {
    /// Convert from packet-level SftpFileAttrs to the public FileAttributes.
    fn from(attrs: &SftpFileAttrs) -> Self {
        let perm = attrs.permissions.unwrap_or(0);
        Self {
            size: attrs.size.unwrap_or(0),
            uid: attrs.uid.unwrap_or(0),
            gid: attrs.gid.unwrap_or(0),
            permissions: perm,
            atime: attrs.atime.unwrap_or(0),
            mtime: attrs.mtime.unwrap_or(0),
            is_directory: (perm & 0o170000) == 0o040000,
            is_regular_file: (perm & 0o170000) == 0o100000,
            is_symlink: (perm & 0o170000) == 0o120000,
        }
    }
}

impl FileAttributes {
    /// Create attributes suitable for a regular file of given size.
    pub fn for_file(size: u64) -> Self {
        Self {
            size,
            is_regular_file: true,
            ..Default::default()
        }
    }

    /// Create attributes suitable for a directory.
    pub fn for_directory() -> Self {
        Self {
            permissions: 0o755,
            is_directory: true,
            ..Default::default()
        }
    }

    /// Check if this appears to be an empty file or directory.
    pub fn is_empty(&self) -> bool {
        self.size == 0 && !self.is_directory
    }

    /// Get the human-readable permission string (e.g., "rw-r--r--").
    pub fn permission_string(&self) -> String {
        format!("{:o}", self.permissions)
    }

    /// Create a SftpFileAttrs from this FileAttributes (for use in SETSTAT/FSETSTAT).
    pub fn to_sftp_attrs(&self) -> SftpFileAttrs {
        SftpFileAttrs {
            size: Some(self.size),
            uid: Some(self.uid),
            gid: Some(self.gid),
            permissions: Some(self.permissions),
            atime: Some(self.atime),
            mtime: Some(self.mtime),
            extended: Vec::new(),
        }
    }
}

// =============================================================================
// Open Flags
// =============================================================================

bitflags::bitflags! {
    /// Flags controlling how files are opened in SFTP operations.
    ///
    /// These map directly to the SSH_FXF_* constants defined in the SFTP protocol:
    /// - READ (0x00000001): Open for reading
    /// - WRITE (0x00000002): Open for writing
    /// - APPEND (0x00000004): Force writes to append
    /// - CREATE (0x00000008): Create file if it doesn't exist
    /// - TRUNCATE (0x00000010): Truncate existing file to zero length
    /// - CREATE_NEW (0x00000020): Exclusive create (fail if exists)
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct OpenFlags: u32 {
        const READ   = 0x0001;
        const WRITE  = 0x0002;
        const APPEND = 0x0004;
        const CREATE = 0x0008;
        const TRUNCATE = 0x0010;
        const CREATE_NEW = 0x0020;
    }
}

impl Default for OpenFlags {
    fn default() -> Self {
        Self::READ
    }
}

impl OpenFlags {
    /// Create flags for reading an existing file.
    pub fn readonly() -> Self {
        Self::READ
    }

    /// Create flags for writing (create or truncate).
    pub fn write_create() -> Self {
        Self::WRITE | Self::CREATE | Self::TRUNCATE
    }

    /// Create flags for appending to a file.
    pub fn append() -> Self {
        Self::WRITE | Self::APPEND | Self::CREATE
    }

    /// Create flags for resume writing (read+write without truncation).
    pub fn resume() -> Self {
        Self::READ | Self::WRITE | Self::CREATE
    }

    /// Convert our OpenFlags to the protocol-level SSH_FXF_* bitmask.
    pub fn to_protocol_flags(&self) -> u32 {
        let mut flags = 0u32;
        if self.contains(Self::READ) {
            flags |= SSH_FXF_READ;
        }
        if self.contains(Self::WRITE) || self.contains(Self::APPEND) {
            flags |= SSH_FXF_WRITE;
            // Many SFTP servers require READ access for WRITE operations
            // (e.g., to verify file existence before writing)
            flags |= SSH_FXF_READ;
        }
        if self.contains(Self::APPEND) {
            flags |= SSH_FXF_APPEND;
        }
        if self.contains(Self::CREATE) || self.contains(Self::CREATE_NEW) {
            flags |= SSH_FXF_CREAT;
        }
        if self.contains(Self::TRUNCATE) {
            flags |= SSH_FXF_TRUNC;
        }
        if self.contains(Self::CREATE_NEW) {
            flags |= SSH_FXF_EXCL;
        }
        flags
    }
}

// =============================================================================
// Directory Entry
// =============================================================================

/// A single entry returned by directory listing (READDIR) operations.
#[derive(Debug, Clone)]
pub struct DirEntry {
    /// The base filename (not the full path)
    pub name: String,
    /// Full file attributes for this entry
    pub attributes: FileAttributes,
}

impl std::fmt::Display for DirEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} ({})", self.name, self.attributes)
    }
}

// =============================================================================
// Error Types
// =============================================================================

/// Application-level error type for SFTP file operations.
///
/// Maps low-level SFTP status codes into categories that the download engine
/// can use for retry logic, user messaging, and logging decisions.
#[derive(Debug, Clone, thiserror::Error)]
pub enum FileOpError {
    /// The requested file does not exist on the remote server
    #[error("No such file or directory: {path}")]
    NotFound { path: String },

    /// Permission denied accessing the resource
    #[error("Permission denied: {path}")]
    PermissionDenied { path: String },

    /// A network or connection-level failure occurred
    #[error("Network/IO error on {operation}: {message}")]
    Network { operation: String, message: String },

    /// The operation is not supported by the server
    #[error("Operation not supported: {operation}")]
    Unsupported { operation: String },

    /// A generic SFTP protocol error occurred
    #[error("SFTP error ({code}) on {operation}: {message}")]
    Protocol {
        code: u32,
        operation: String,
        message: String,
    },

    /// Invalid input parameters
    #[error("Invalid argument: {message}")]
    InvalidArgument { message: String },
}

impl FileOpError {
    /// Map an SFTP status code + context to a FileOpError.
    ///
    /// Classifies the error based on the SFTP status code and operation context.
    pub fn from_status_code(code: u32, operation: &str, path: &str) -> Self {
        match code {
            SSH_FX_NO_SUCH_FILE => Self::NotFound {
                path: path.to_string(),
            },
            SSH_FX_PERMISSION_DENIED => Self::PermissionDenied {
                path: path.to_string(),
            },
            _ if is_fatal_error(code) => Self::Protocol {
                code,
                operation: operation.to_string(),
                message: status_code_description(code).to_string(),
            },
            _ if is_retryable_error(code) => Self::Network {
                operation: operation.to_string(),
                message: format!(
                    "retryable error {} ({})",
                    code,
                    status_code_description(code)
                ),
            },
            _ => Self::Protocol {
                code,
                operation: operation.to_string(),
                message: status_code_description(code).to_string(),
            },
        }
    }

    /// Check if this error indicates the target simply doesn't exist.
    pub fn is_not_found(&self) -> bool {
        matches!(self, Self::NotFound { .. })
    }

    /// Check if this error indicates a permission problem.
    pub fn is_permission_denied(&self) -> bool {
        matches!(self, Self::PermissionDenied { .. })
    }

    /// Check if this error might be resolved by retrying.
    pub fn is_retryable(&self) -> bool {
        matches!(self, Self::Network { .. })
    }

    /// Get the path associated with this error, if any.
    pub fn path(&self) -> Option<&str> {
        match self {
            Self::NotFound { path } | Self::PermissionDenied { path } => Some(path),
            _ => None,
        }
    }
}

// =============================================================================
// SFTP File Handle
// =============================================================================

/// A handle to an open SFTP file on the remote server.
///
/// This wraps a server-side opaque handle (returned by SSH_FXP_OPEN)
/// and provides methods for positioned I/O operations through the SFTP session.
pub struct SftpFileHandle {
    /// The server-side opaque handle bytes (returned by OPEN response)
    handle_bytes: Vec<u8>,
    /// Reference to the owning session for issuing requests
    session: Arc<SftpSession>,
}

impl SftpFileHandle {
    /// Get file attributes via the open handle (FSTAT).
    ///
    /// Uses `SSH_FXP_FSTAT` which operates on the handle rather than path.
    pub async fn fstat(&self) -> Result<FileAttributes, FileOpError> {
        debug!("[SFTP] FSTAT on handle");

        let pkt = super::packet::SftpPacket::Fstat {
            request_id: 0, // Will be set by request()
            handle: self.handle_bytes.clone(),
        };

        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: "FSTAT".to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Attrs { attrs, .. } => Ok(FileAttributes::from(&attrs)),
            super::packet::SftpPacket::Status { code, .. } => {
                Err(FileOpError::from_status_code(code, "FSTAT", "<handle>"))
            }
            other => Err(FileOpError::Protocol {
                code: other.packet_type() as u32,
                operation: "FSTAT".to_string(),
                message: format!("unexpected packet type: {}", other.packet_type()),
            }),
        }
    }

    /// Set file attributes via the open handle (FSETSTAT).
    pub async fn set_fstat(&self, attrs: &FileAttributes) -> Result<(), FileOpError> {
        debug!("[SFTP] FSETSTAT on handle");

        let pkt = super::packet::SftpPacket::Fsetstat {
            request_id: 0,
            handle: self.handle_bytes.clone(),
            attrs: attrs.to_sftp_attrs(),
        };

        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: "FSETSTAT".to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Status { code: 0, .. } => Ok(()),
            super::packet::SftpPacket::Status { code, .. } => {
                Err(FileOpError::from_status_code(code, "FSETSTAT", "<handle>"))
            }
            other => Err(FileOpError::Protocol {
                code: other.packet_type() as u32,
                operation: "FSETSTAT".to_string(),
                message: format!("unexpected packet type: {}", other.packet_type()),
            }),
        }
    }

    /// Read data at a specific offset into the returned buffer.
    ///
    /// # Arguments
    /// * `offset` - Byte offset from start of file
    /// * `length` - Maximum number of bytes to read
    ///
    /// # Returns
    /// The data bytes read (may be less than `length` near EOF)
    pub async fn read_at(&self, offset: u64, length: u32) -> Result<Vec<u8>, FileOpError> {
        debug!("[SFTP] READ handle offset={} len={}", offset, length);

        let pkt = super::packet::SftpPacket::Read {
            request_id: 0,
            handle: self.handle_bytes.clone(),
            offset,
            length,
        };

        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: "READ".to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Data { data, .. } => Ok(data),
            super::packet::SftpPacket::Status { code, .. } if code == SSH_FX_EOF => {
                // EOF means no more data -- return empty buffer
                Ok(Vec::new())
            }
            super::packet::SftpPacket::Status { code, .. } => {
                Err(FileOpError::from_status_code(code, "READ", "<handle>"))
            }
            other => Err(FileOpError::Protocol {
                code: other.packet_type() as u32,
                operation: "READ".to_string(),
                message: format!("unexpected packet type: {}", other.packet_type()),
            }),
        }
    }

    /// Write data at a specific offset.
    ///
    /// # Arguments
    /// * `offset` - Byte offset from start of file
    /// * `data` - Data bytes to write
    ///
    /// # Returns
    /// Number of bytes written
    pub async fn write_at(&self, offset: u64, data: &[u8]) -> Result<u64, FileOpError> {
        debug!("[SFTP] WRITE handle offset={} len={}", offset, data.len());

        let pkt = super::packet::SftpPacket::Write {
            request_id: 0,
            handle: self.handle_bytes.clone(),
            offset,
            data: data.to_vec(),
        };

        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: "WRITE".to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Status { code: 0, .. } => Ok(data.len() as u64),
            super::packet::SftpPacket::Status { code, .. } => {
                Err(FileOpError::from_status_code(code, "WRITE", "<handle>"))
            }
            other => Err(FileOpError::Protocol {
                code: other.packet_type() as u32,
                operation: "WRITE".to_string(),
                message: format!("unexpected packet type: {}", other.packet_type()),
            }),
        }
    }

    /// Close the file handle and release server-side resources.
    pub async fn close(self) -> Result<(), FileOpError> {
        debug!("[SFTP] Closing file handle");

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

        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: "CLOSE".to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Status { code: 0, .. } => {
                debug!("[SFTP] File handle closed successfully");
                Ok(())
            }
            super::packet::SftpPacket::Status { code, message, .. } => {
                warn!("[SFTP] Close returned error {}: {}", code, message);
                // Still return Ok since we're cleaning up
                Ok(())
            }
            other => {
                warn!(
                    "[SFTP] Close unexpected response: {:?}",
                    other.packet_type()
                );
                Ok(())
            }
        }
    }
}

// =============================================================================
// SFTP File Operations Interface
// =============================================================================

/// High-level interface for performing SFTP file and directory operations.
///
/// This struct wraps the underlying SFTP session and provides idiomatic
/// Rust methods for common SFTP operations with proper error handling.
/// Every operation goes through `session.request()` which handles
/// packet encoding, channel I/O, and response decoding.
///
/// # Usage
///
/// ```ignore
/// let session = SftpSession::open(&conn).await?;
/// let ops = SftpFileOps::new(&session);
///
/// // Stat a remote file
/// let attrs = ops.stat("/remote/file.txt")?;
///
/// // Read a file in chunks
/// let mut handle = ops.open("/remote/file.txt", OpenFlags::readonly(), 0)?;
/// let data = handle.read_at(0, 32768).await?;
/// ```
pub struct SftpFileOps<'a> {
    /// Reference to the owning SFTP session
    session: &'a SftpSession,
}

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

    /// Get a reference to the underlying SFTP session.
    pub fn session(&self) -> &'a SftpSession {
        self.session
    }

    /// Helper: send a request and check for STATUS response (OK or error).
    async fn check_status(
        &self,
        pkt: super::packet::SftpPacket,
        operation: &str,
        path: &str,
    ) -> Result<(), FileOpError> {
        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: operation.to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Status { code: 0, .. } => Ok(()),
            super::packet::SftpPacket::Status { code, .. } => {
                Err(FileOpError::from_status_code(code, operation, path))
            }
            other => Err(FileOpError::Protocol {
                code: other.packet_type() as u32,
                operation: operation.to_string(),
                message: format!("expected STATUS, got type={}", other.packet_type()),
            }),
        }
    }

    // -----------------------------------------------------------------
    // Attribute Operations (STAT/LSTAT/FSTAT)
    // -----------------------------------------------------------------

    /// Get file attributes for a path (follows symbolic links).
    ///
    /// Equivalent to the SFTP `SSH_FXP_STAT` request.
    pub async fn stat(&self, path: &str) -> Result<FileAttributes, FileOpError> {
        debug!("[SFTP] STAT: {}", path);

        let pkt = super::packet::SftpPacket::Stat {
            request_id: 0,
            path: path.to_string(),
        };

        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: "STAT".to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Attrs { attrs, .. } => Ok(FileAttributes::from(&attrs)),
            super::packet::SftpPacket::Status { code, .. } => {
                Err(FileOpError::from_status_code(code, "STAT", path))
            }
            other => Err(FileOpError::Protocol {
                code: other.packet_type() as u32,
                operation: "STAT".to_string(),
                message: format!("expected ATTRS, got type={}", other.packet_type()),
            }),
        }
    }

    /// Get file attributes for a path (does NOT follow symbolic links).
    ///
    /// Equivalent to the SFTP `SSH_FXP_LSTAT` request.
    pub async fn lstat(&self, path: &str) -> Result<FileAttributes, FileOpError> {
        debug!("[SFTP] LSTAT: {}", path);

        let pkt = super::packet::SftpPacket::Lstat {
            request_id: 0,
            path: path.to_string(),
        };

        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: "LSTAT".to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Attrs { attrs, .. } => Ok(FileAttributes::from(&attrs)),
            super::packet::SftpPacket::Status { code, .. } => {
                Err(FileOpError::from_status_code(code, "LSTAT", path))
            }
            other => Err(FileOpError::Protocol {
                code: other.packet_type() as u32,
                operation: "LSTAT".to_string(),
                message: format!("expected ATTRS, got type={}", other.packet_type()),
            }),
        }
    }

    /// Set file attributes by path.
    ///
    /// Equivalent to the SFTP `SSH_FXP_SETSTAT` request.
    pub async fn set_stat(&self, path: &str, attrs: &FileAttributes) -> Result<(), FileOpError> {
        debug!("[SFTP] SETSTAT: {} (perm={:o})", path, attrs.permissions);

        let pkt = super::packet::SftpPacket::Setstat {
            request_id: 0,
            path: path.to_string(),
            attrs: attrs.to_sftp_attrs(),
        };

        self.check_status(pkt, "SETSTAT", path).await
    }

    /// Resolve a path to its canonical form.
    ///
    /// Equivalent to the SFTP `SSH_FXP_REALPATH` request.
    /// Useful for resolving relative paths, `..`, symlinks, etc.
    pub async fn realpath(&self, path: &str) -> Result<String, FileOpError> {
        debug!("[SFTP] REALPATH: {}", path);

        let pkt = super::packet::SftpPacket::Realpath {
            request_id: 0,
            path: path.to_string(),
        };

        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: "REALPATH".to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Name { entries, .. } => {
                // REALPATH returns a NAME packet with one entry; take the first filename
                if let Some(entry) = entries.into_iter().next() {
                    Ok(entry.filename)
                } else {
                    Err(FileOpError::Protocol {
                        code: 0,
                        operation: "REALPATH".to_string(),
                        message: "empty NAME response".to_string(),
                    })
                }
            }
            super::packet::SftpPacket::Status { code, .. } => {
                Err(FileOpError::from_status_code(code, "REALPATH", path))
            }
            other => Err(FileOpError::Protocol {
                code: other.packet_type() as u32,
                operation: "REALPATH".to_string(),
                message: format!("expected NAME, got type={}", other.packet_type()),
            }),
        }
    }

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

    /// Open a remote file with the specified flags.
    ///
    /// # Arguments
    /// * `path` - Remote file path to open
    /// * `flags` - Combination of OpenFlags specifying access mode
    /// * `mode` - Creation mode (permissions) if creating a new file (e.g., 0o644)
    ///
    /// # Returns
    /// An `SftpFileHandle` that can be used for read/write operations.
    pub async fn open(
        &self,
        path: &str,
        flags: OpenFlags,
        mode: u32,
    ) -> Result<SftpFileHandle, FileOpError> {
        debug!("[SFTP] OPEN: {} (mode={:o}, flags={:?})", path, mode, flags);

        let pkt = super::packet::SftpPacket::Open {
            request_id: 0,
            filename: path.to_string(),
            flags: flags.to_protocol_flags(),
            attrs: SftpFileAttrs {
                permissions: Some(mode),
                ..Default::default()
            },
        };

        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: "OPEN".to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Handle { handle, .. } => Ok(SftpFileHandle {
                handle_bytes: handle,
                session: Arc::new(self.session.clone()),
            }),
            super::packet::SftpPacket::Status { code, .. } => {
                Err(FileOpError::from_status_code(code, "OPEN", path))
            }
            other => Err(FileOpError::Protocol {
                code: other.packet_type() as u32,
                operation: "OPEN".to_string(),
                message: format!("expected HANDLE, got type={}", other.packet_type()),
            }),
        }
    }

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

    /// Create a directory with the specified permissions.
    pub async fn mkdir(&self, path: &str, mode: u32) -> Result<(), FileOpError> {
        debug!("[SFTP] MKDIR: {} (mode={:o})", path, mode);

        let pkt = super::packet::SftpPacket::Mkdir {
            request_id: 0,
            path: path.to_string(),
            attrs: SftpFileAttrs {
                permissions: Some(mode),
                ..Default::default()
            },
        };

        self.check_status(pkt, "MKDIR", path).await
    }

    /// Recursively create directories (like `mkdir -p`).
    pub async fn mkdir_recursive(&self, path: &str, mode: u32) -> Result<(), FileOpError> {
        // Parse path components
        let mut current = String::new();
        if let Some('/') = path.chars().next() {
            current.push('/');
        }

        for component in path.split('/').filter(|s| !s.is_empty()) {
            current.push_str(component);
            // Try to create; ignore "already exists" errors
            match self.mkdir(&current, mode).await {
                Ok(_) | Err(FileOpError::NotFound { .. }) => {}
                Err(e) if e.is_not_found() => {}
                Err(e) => return Err(e),
            }
            current.push('/');
        }

        // Final mkdir without trailing slash for exact path
        if path.ends_with('/') {
            Ok(())
        } else {
            self.mkdir(path, mode)
                .await
                .or_else(|e| if e.is_not_found() { Ok(()) } else { Err(e) })
        }
    }

    /// Remove an empty directory.
    pub async fn rmdir(&self, path: &str) -> Result<(), FileOpError> {
        debug!("[SFTP] RMDIR: {}", path);

        let pkt = super::packet::SftpPacket::Rmdir {
            request_id: 0,
            path: path.to_string(),
        };

        self.check_status(pkt, "RMDIR", path).await
    }

    /// List contents of a directory.
    ///
    /// Returns entries excluding `.` and `..`.
    pub async fn readdir(&self, path: &str) -> Result<Vec<DirEntry>, FileOpError> {
        debug!("[SFTP] READDIR: {}", path);

        // Step 1: OPENDIR
        let opendir_pkt = super::packet::SftpPacket::Opendir {
            request_id: 0,
            path: path.to_string(),
        };
        let opendir_resp =
            self.session
                .request(opendir_pkt)
                .await
                .map_err(|e| FileOpError::Network {
                    operation: "OPENDIR".to_string(),
                    message: e,
                })?;

        let dir_handle = match opendir_resp {
            super::packet::SftpPacket::Handle { handle, .. } => handle,
            super::packet::SftpPacket::Status { code, .. } => {
                return Err(FileOpError::from_status_code(code, "OPENDIR", path));
            }
            other => {
                return Err(FileOpError::Protocol {
                    code: other.packet_type() as u32,
                    operation: "OPENDIR".to_string(),
                    message: format!("expected HANDLE, got type={}", other.packet_type()),
                });
            }
        };

        // Step 2: Loop READDIR until EOF or empty NAME
        let mut entries = Vec::new();
        loop {
            let readdir_pkt = super::packet::SftpPacket::Readdir {
                request_id: 0,
                handle: dir_handle.clone(),
            };

            let resp =
                self.session
                    .request(readdir_pkt)
                    .await
                    .map_err(|e| FileOpError::Network {
                        operation: "READDIR".to_string(),
                        message: e,
                    })?;

            match resp {
                super::packet::SftpPacket::Name {
                    entries: mut name_entries,
                    ..
                } => {
                    if name_entries.is_empty() {
                        break; // End of directory listing
                    }
                    for entry in name_entries.drain(..) {
                        let name = entry.filename;
                        // Skip special directory entries
                        if name == "." || name == ".." {
                            continue;
                        }
                        entries.push(DirEntry {
                            name,
                            attributes: FileAttributes::from(&entry.attrs),
                        });
                    }
                }
                super::packet::SftpPacket::Status { code, .. } if code == SSH_FX_EOF => {
                    break; // Normal end of directory listing
                }
                super::packet::SftpPacket::Status { code, message, .. } => {
                    warn!("[SFTP] READDIR error [{}]: {} ({})", path, code, message);
                    break;
                }
                other => {
                    warn!("[SFTP] READDIR unexpected: type={}", other.packet_type());
                    break;
                }
            }
        }

        // Step 3: CLOSE the dir handle (best-effort)
        let close_pkt = super::packet::SftpPacket::Close {
            request_id: 0,
            handle: dir_handle,
        };
        let _ = self.session.request(close_pkt).await;

        debug!(
            "[SFTP] READDIR: {} returned {} entries",
            path,
            entries.len()
        );
        Ok(entries)
    }

    /// List only regular files (and symlinks) in a directory.
    pub async fn list_files(&self, path: &str) -> Result<Vec<String>, FileOpError> {
        let entries = self.readdir(path).await?;
        Ok(entries
            .into_iter()
            .filter(|e| e.attributes.is_regular_file || e.attributes.is_symlink)
            .map(|e| e.name)
            .collect())
    }

    /// List only subdirectories in a directory (excluding . and ..).
    pub async fn list_dirs(&self, path: &str) -> Result<Vec<String>, FileOpError> {
        let entries = self.readdir(path).await?;
        Ok(entries
            .into_iter()
            .filter(|e| e.attributes.is_directory)
            .map(|e| e.name)
            .collect())
    }

    // -----------------------------------------------------------------
    // File System Operations
    // -----------------------------------------------------------------

    /// Remove (unlink) a file.
    pub async fn unlink(&self, path: &str) -> Result<(), FileOpError> {
        debug!("[SFTP] UNLINK: {}", path);

        let pkt = super::packet::SftpPacket::Remove {
            request_id: 0,
            filename: path.to_string(),
        };

        self.check_status(pkt, "UNLINK", path).await
    }

    /// Rename a file or directory.
    pub async fn rename(&self, src: &str, dest: &str) -> Result<(), FileOpError> {
        debug!("[SFTP] RENAME: {} -> {}", src, dest);

        let pkt = super::packet::SftpPacket::Rename {
            request_id: 0,
            old_path: src.to_string(),
            new_path: dest.to_string(),
        };

        self.check_status(pkt, "RENAME", src).await
    }

    /// Create a symbolic link.
    pub async fn symlink(&self, target: &str, link_path: &str) -> Result<(), FileOpError> {
        debug!("[SFTP] SYMLINK: {} -> {}", link_path, target);

        let pkt = super::packet::SftpPacket::Symlink {
            request_id: 0,
            link_path: link_path.to_string(),
            target_path: target.to_string(),
        };

        self.check_status(pkt, "SYMLINK", link_path).await
    }

    /// Read the target of a symbolic link.
    pub async fn readlink(&self, path: &str) -> Result<String, FileOpError> {
        debug!("[SFTP] READLINK: {}", path);

        let pkt = super::packet::SftpPacket::Readlink {
            request_id: 0,
            path: path.to_string(),
        };

        let resp = self
            .session
            .request(pkt)
            .await
            .map_err(|e| FileOpError::Network {
                operation: "READLINK".to_string(),
                message: e,
            })?;

        match resp {
            super::packet::SftpPacket::Name { mut entries, .. } => {
                if let Some(entry) = entries.pop() {
                    Ok(entry.filename)
                } else {
                    Err(FileOpError::Protocol {
                        code: 0,
                        operation: "READLINK".to_string(),
                        message: "empty NAME response".to_string(),
                    })
                }
            }
            super::packet::SftpPacket::Status { code, .. } => {
                Err(FileOpError::from_status_code(code, "READLINK", path))
            }
            other => Err(FileOpError::Protocol {
                code: other.packet_type() as u32,
                operation: "READLINK".to_string(),
                message: format!("expected NAME, got type={}", other.packet_type()),
            }),
        }
    }

    /// Check whether a path exists (without following symlinks for final component).
    pub async fn exists(&self, path: &str) -> Result<bool, FileOpError> {
        match self.lstat(path).await {
            Ok(_) => Ok(true),
            Err(e) if e.is_not_found() => Ok(false),
            Err(e) => Err(e),
        }
    }

    /// Check if a path is a regular file.
    pub async fn is_file(&self, path: &str) -> Result<bool, FileOpError> {
        let attr = self.stat(path).await?;
        Ok(attr.is_regular_file)
    }

    /// Check if a path is a directory.
    pub async fn is_dir(&self, path: &str) -> Result<bool, FileOpError> {
        let attr = self.stat(path).await?;
        Ok(attr.is_directory)
    }
}

// =============================================================================
// Note: Clone for SftpSession is implemented in session.rs
// (it requires access to private fields)
// =============================================================================

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_file_attributes_default() {
        let attrs = FileAttributes::default();
        assert_eq!(attrs.size, 0);
        assert_eq!(attrs.permissions, 0o644);
        assert!(!attrs.is_directory);
        assert!(!attrs.is_regular_file);
        assert!(!attrs.is_symlink);
    }

    #[test]
    fn test_file_attributes_for_file() {
        let attrs = FileAttributes::for_file(99999);
        assert_eq!(attrs.size, 99999);
        assert!(attrs.is_regular_file);
        assert!(!attrs.is_directory);
    }

    #[test]
    fn test_file_attributes_for_directory() {
        let attrs = FileAttributes::for_directory();
        assert!(attrs.is_directory);
        assert_eq!(attrs.permissions, 0o755);
        assert!(!attrs.is_regular_file);
    }

    #[test]
    fn test_file_attributes_display() {
        let attrs = FileAttributes {
            size: 12345,
            permissions: 0o644,
            uid: 1000,
            gid: 1000,
            is_regular_file: true,
            ..Default::default()
        };
        let display = format!("{}", attrs);
        assert!(display.contains("file"));
        assert!(display.contains("12345"));
    }

    #[test]
    fn test_permission_string() {
        let attrs = FileAttributes {
            permissions: 0o755,
            ..Default::default()
        };
        assert_eq!(attrs.permission_string(), "755");
    }

    #[test]
    fn test_file_attrs_conversion_roundtrip() {
        // Test conversion from SftpFileAttrs to FileAttributes and back
        let sftp_attrs = SftpFileAttrs::full(12345, 1000, 1000, 0o100644, 1704000000, 1704000100);
        let file_attrs = FileAttributes::from(&sftp_attrs);
        assert_eq!(file_attrs.size, 12345);
        assert_eq!(file_attrs.uid, 1000);
        assert_eq!(file_attrs.permissions, 0o100644);
        assert!(file_attrs.is_regular_file);

        // Convert back
        let roundtrip = file_attrs.to_sftp_attrs();
        assert_eq!(roundtrip.size, Some(12345));
        assert_eq!(roundtrip.uid, Some(1000));
    }

    #[test]
    fn test_open_flags_variants() {
        assert_eq!(OpenFlags::readonly(), OpenFlags::READ);
        let wc = OpenFlags::write_create();
        assert!(wc.contains(OpenFlags::WRITE));
        assert!(wc.contains(OpenFlags::CREATE));
        assert!(wc.contains(OpenFlags::TRUNCATE));

        let app = OpenFlags::append();
        assert!(app.contains(OpenFlags::APPEND));

        let res = OpenFlags::resume();
        assert!(res.contains(OpenFlags::READ));
        assert!(res.contains(OpenFlags::WRITE));
        assert!(!res.contains(OpenFlags::TRUNCATE));
    }

    #[test]
    fn test_open_flags_to_protocol_flags() {
        let readonly = OpenFlags::readonly();
        assert_eq!(readonly.to_protocol_flags(), SSH_FXF_READ);

        let write_create = OpenFlags::write_create();
        let pf = write_create.to_protocol_flags();
        assert!(pf & SSH_FXF_READ != 0); // WRITE implies READ in many servers
        assert!(pf & SSH_FXF_WRITE != 0);
        assert!(pf & SSH_FXF_CREAT != 0);
        assert!(pf & SSH_FXF_TRUNC != 0);
    }

    #[test]
    fn test_open_flags_combinations() {
        let combined = OpenFlags::READ | OpenFlags::WRITE | OpenFlags::CREATE;
        assert!(combined.contains(OpenFlags::READ));
        assert!(combined.contains(OpenFlags::WRITE));
        assert!(combined.contains(OpenFlags::CREATE));
        assert!(!combined.contains(OpenFlags::TRUNCATE));
        assert!(!combined.contains(OpenFlags::APPEND));
    }

    #[test]
    fn test_open_flags_default_is_readonly() {
        assert_eq!(OpenFlags::default(), OpenFlags::READ);
    }

    #[test]
    fn test_dir_entry_display() {
        let entry = DirEntry {
            name: "test.txt".to_string(),
            attributes: FileAttributes::for_file(2048),
        };
        let display = format!("{}", entry);
        assert!(display.contains("test.txt"));
        assert!(display.contains("2048"));
    }

    #[test]
    fn test_buf_size_constants() {
        assert_eq!(DEFAULT_READ_BUF_SIZE, 32768); // 32KB
        assert_eq!(DEFAULT_WRITE_BUF_SIZE, 32768); // 32KB
    }

    // -----------------------------------------------------------------
    // FileOpError Classification Tests
    // -----------------------------------------------------------------

    #[test]
    fn test_file_op_error_not_found_detection() {
        let err = FileOpError::NotFound {
            path: "/missing.txt".to_string(),
        };
        assert!(err.is_not_found());
        assert!(!err.is_permission_denied());
        assert!(!err.is_retryable());
        assert_eq!(err.path(), Some("/missing.txt"));
    }

    #[test]
    fn test_file_op_error_permission_denied_detection() {
        let err = FileOpError::PermissionDenied {
            path: "/secret".to_string(),
        };
        assert!(err.is_permission_denied());
        assert!(!err.is_not_found());
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_file_op_error_network_is_retryable() {
        let err = FileOpError::Network {
            operation: "READ".to_string(),
            message: "Connection reset".to_string(),
        };
        assert!(err.is_retryable());
        assert!(!err.is_not_found());
        assert!(err.path().is_none());
    }

    #[test]
    fn test_file_op_error_protocol_not_retryable() {
        let err = FileOpError::Protocol {
            code: 4, // SSH_FX_FAILURE
            operation: "OPEN".to_string(),
            message: "Generic failure".to_string(),
        };
        // Generic FAILURE is not classified as retryable by default
        assert!(!err.is_retryable());
    }

    #[test]
    fn test_file_op_error_display_messages() {
        let err = FileOpError::NotFound {
            path: "/data/file.bin".to_string(),
        };
        let msg = format!("{}", err);
        assert!(msg.contains("No such file"));
        assert!(msg.contains("/data/file.bin"));

        let err = FileOpError::PermissionDenied {
            path: "/root/.ssh".to_string(),
        };
        let msg = format!("{}", err);
        assert!(msg.contains("Permission denied"));
    }

    #[test]
    fn test_file_op_error_invalid_argument() {
        let err = FileOpError::InvalidArgument {
            message: "empty path".to_string(),
        };
        assert!(format!("{}", err).contains("empty path"));
    }

    #[test]
    fn test_from_status_code_mapping() {
        // No such file
        let err = FileOpError::from_status_code(SSH_FX_NO_SUCH_FILE, "STAT", "/missing");
        assert!(err.is_not_found());

        // Permission denied
        let err = FileOpError::from_status_code(SSH_FX_PERMISSION_DENIED, "OPEN", "/secret");
        assert!(err.is_permission_denied());

        // EOF should map to network/retryable
        let err = FileOpError::from_status_code(SSH_FX_EOF, "READ", "/file");
        assert!(err.is_retryable());

        // OK (code=0) should not produce an error normally, but if used:
        let err = FileOpError::from_status_code(0, "CLOSE", "/file");
        match err {
            FileOpError::Protocol { .. } => {} // OK maps to Protocol with success description
            other => panic!("Unexpected error type for OK: {:?}", other),
        }
    }
}