Crate phazer

source ·
Expand description

Imagine, if you will, that you are building an application that downloads a file from a website. Let’s say that the application is downloading the baby name data from the U.S. Social Security Administration (https://www.ssa.gov/oact/babynames/names.zip).

A common failure when getting data from the internet is an interrupted download. Unless precautions are taken the file ends up truncated (essentially corrupt). That would result in a bad experience your users. The application might stop running after outputting a cryptic error regarding an unreadable ZIP file.

A similar problem occurs with configuration files. We want our service to only see a complete configuration file. A partial configuration file might even introduce a security vulnerablility.

The purpose of this crate is to present a file to a system in a finished state or not at all. Either the entire names.zip file is downloaded or the file is missing. Either the old complete configuration file is used or the new complete configuration file is used.

The following example shows how an interrupted application avoids putting a partial file in use.

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let p = Phazer::new("test.cfg");
    let mut w = p.simple_writer()?;
    writeln!(w, "[Settings]")?;
    writeln!(w, "Port=1")?;
    panic!("test.cfg never exists because of this panic.  Removing this line results in test.cfg being \"created\" atomically.");
    writeln!(w, "Timeout=10")?;
    p.commit()?;
    Ok(())
}

Structs

  • Phazer is the entry point into this crate.