Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/backup/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!(
Comment on lines +109 to +110

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Revalidate remote metadata before rejecting a size mismatch

When backing up a running server, a log or save file can grow between the initial conn.list() and its download. Both FTP and SFTP collect sizes during directory traversal, then download files sequentially without locking or snapshotting them, so a successful binary transfer can legitimately contain more bytes than the stale listing reports. This check now aborts the entire backup without creating an archive in that situation, including for SFTP. Recheck the remote metadata and distinguish a changed source file from a truncated transfer, with an appropriate retry or explicit policy for changing files.

Useful? React with 👍 / 👎.

"download {}: expected {} bytes but received {}; \
the file was not transferred intact",
file.path,
file.size,
downloaded
);
}
}

self.progress.close();
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions src/connector/ftp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
Expand Down