diff --git a/src/backup/manager.rs b/src/backup/manager.rs index a2949af..d833a59 100644 --- a/src/backup/manager.rs +++ b/src/backup/manager.rs @@ -85,7 +85,7 @@ impl Manager { self.progress.file_start(&file.path, file.size); - { + let downloaded = { let progress = &mut self.progress; let path = file.path.clone(); let size = file.size; @@ -97,9 +97,24 @@ impl Manager { }; conn.download(&path, &mut writer) .with_context(|| format!("download {}", file.path))?; - } + writer.written + }; self.progress.file_done(&file.path); + + // Guard against silent transfer corruption (e.g. an FTP server + // whose session is still in ASCII mode rewriting line endings). + // Sizes are only checked when the server reported a usable one; + // connectors may report 0 for entries whose size is unknown. + if file.size > 0 && downloaded != file.size { + bail!( + "download {}: expected {} bytes but received {}; \ + the file was not transferred intact", + file.path, + file.size, + downloaded + ); + } } self.progress.close(); @@ -273,6 +288,33 @@ mod tests { assert!(log.contains("nested/file2.txt")); } + #[test] + fn backup_fails_on_download_size_mismatch() { + let tmp = tempfile::tempdir().expect("tempdir"); + let mut conn = MockConnector { + files: vec![FileInfo { + path: "world.dat".to_string(), + size: 10, + mod_time: SystemTime::now(), + is_dir: false, + }], + data: std::collections::HashMap::from([("world.dat".to_string(), "short".to_string())]), + connected: false, + }; + + let mut manager = Manager { + temp_dir: String::new(), + backup_location: tmp.path().to_string_lossy().into_owned(), + server_name: "test-server".to_string(), + progress: crate::progress::new(&crate::log::Logger::new(), "json"), + }; + + let err = manager.backup(&mut conn).expect_err("mismatch must fail"); + let message = err.to_string(); + assert!(message.contains("expected 10 bytes"), "{message}"); + assert!(message.contains("received 5"), "{message}"); + } + #[test] fn backup_requires_location() { let mut conn = mock(); diff --git a/src/connector/ftp.rs b/src/connector/ftp.rs index 0f62c81..e32c964 100644 --- a/src/connector/ftp.rs +++ b/src/connector/ftp.rs @@ -6,6 +6,7 @@ use std::time::Duration; use anyhow::{bail, Result}; use suppaftp::list::ListParser; +use suppaftp::types::FileType; use suppaftp::{Mode, NativeTlsFtpStream}; use super::matcher::matches_patterns; @@ -118,6 +119,17 @@ impl Connector for FtpConnector { .login(&self.config.username, &self.config.password) .map_err(|e| anyhow::anyhow!("FTP login failed: {e}"))?; + // Backups contain arbitrary bytes (archives, save files, SQLite + // databases), so every transfer must use binary mode. `suppaftp` never + // sends `TYPE` on its own, which leaves the server on its default - + // usually `TYPE A` (RFC 959). ASCII mode rewrites line endings and + // silently corrupts downloaded files, which is especially noticeable on + // Nitrado's FTP servers. `TYPE` is a session setting, so it has to be + // issued after login and before the first `RETR`/`STOR`. + stream + .transfer_type(FileType::Binary) + .map_err(|e| anyhow::anyhow!("failed to set FTP binary transfer mode: {e}"))?; + self.stream = Some(stream); Ok(()) }