DEV Community

Cover image for Smart Resume for File Transfers in Rust — Never Start Over
hiyoyo
hiyoyo

Posted on

Smart Resume for File Transfers in Rust — Never Start Over

All tests run on an 8-year-old MacBook Air. All results from shipping 7 Mac apps as a solo developer. No sponsored opinion.

A 2GB video transfer that fails at 90% and restarts from zero is a terrible experience. HiyokoMTP and HiyokoAutoSync both implement smart resume. Here's how.


The problem

File transfers fail. USB connections drop. Devices sleep. The user unplugs at the wrong moment.

Without resume: start over. With resume: pick up where you left off.


The approach

Track transfer state in SQLite. On failure, record the partial state. On retry, check the record and resume from the last confirmed position.

CREATE TABLE transfer_state (
    id INTEGER PRIMARY KEY,
    file_path TEXT NOT NULL,
    total_bytes INTEGER NOT NULL,
    transferred_bytes INTEGER DEFAULT 0,
    file_hash TEXT,
    status TEXT DEFAULT 'in_progress',
    started_at INTEGER,
    updated_at INTEGER
);
Enter fullscreen mode Exit fullscreen mode

Writing with resume support

pub async fn transfer_with_resume(
    source: &Path,
    dest: &Path,
    db: &Connection,
) -> Result<(), AppError> {
    let file_hash = compute_hash(source)?;

    // Check for existing partial transfer
    let existing = db.query_row(
        "SELECT transferred_bytes FROM transfer_state
         WHERE file_path = ? AND file_hash = ? AND status = 'in_progress'",
        params![source.to_str().unwrap(), &file_hash],
        |r| r.get::<_, i64>(0),
    ).ok();

    let start_offset = existing.unwrap_or(0) as u64;

    // Open source file, seek to offset
    let mut reader = File::open(source)?;
    reader.seek(SeekFrom::Start(start_offset))?;

    // Open dest file for append if resuming
    let mut writer = if start_offset > 0 {
        OpenOptions::new().append(true).open(dest)?
    } else {
        File::create(dest)?
    };

    // Transfer in chunks, updating DB periodically
    let mut transferred = start_offset;
    let mut buf = vec![0u8; 65536];

    loop {
        let n = reader.read(&mut buf)?;
        if n == 0 { break; }

        writer.write_all(&buf[..n])?;
        transferred += n as u64;

        // Update progress every 1MB
        if transferred % (1024 * 1024) == 0 {
            db.execute(
                "UPDATE transfer_state SET transferred_bytes = ?, updated_at = ? WHERE file_path = ?",
                params![transferred as i64, unix_now(), source.to_str().unwrap()],
            )?;
        }
    }

    // Mark complete
    db.execute(
        "UPDATE transfer_state SET status = 'complete' WHERE file_path = ?",
        params![source.to_str().unwrap()],
    )?;

    Ok(())
}
Enter fullscreen mode Exit fullscreen mode

Validating resumed files

After a resume, verify the file hash of the completed transfer:

if compute_hash(dest)? != expected_hash {
    // Hash mismatch — delete and retry from scratch
    std::fs::remove_file(dest)?;
    return Err(AppError::Transfer("Hash mismatch after resume".into()));
}
Enter fullscreen mode Exit fullscreen mode

A corrupted partial transfer is worse than starting over. Always validate.


The verdict

Smart resume is the difference between a frustrating tool and a reliable one. SQLite tracking + offset-based writes cover the implementation. The complexity is worth it for any app that transfers large files.


TL;DR: Track transfer state in SQLite with transferred_bytes and file_hash. On retry, seek to the last confirmed offset and append. Update progress every 1MB to keep DB writes cheap. Always verify the final hash — a corrupted resume is worse than starting over.


If this was useful, a ❤️ helps more than you'd think — thanks!

HiyokoAutoSync | X → @hiyoyok

Top comments (0)