Crate file_with_meta

source ·
Expand description

Store a file’s metadata for caching purposes.

The FileHttpMetadata structure may be serialized and stored in a JSON file alongside the real file, e.g. one with “.meta” appended to the file name. Then either the match_meta function may be used directly, or the build_req one may be used to modify an HTTP request, adding the necessary headers to make sure that the file is not downloaded if there have been no changes on the remote server.

Example for checking whether a file needs to be downloaded:

use std::fs::{self, File};

let dst = destdir.join("data.json");
let dst_meta = destdir.join("data.json.meta");
let (req, stored_meta) = file_with_meta::build_req(
    agent.get("https://example.com/"),
    &dst,
    &dst_meta,
)?;
let resp = req.call()?;
match resp.status() {
    304 => println!("Nothing was fetched"),
    _ => {
        println!("Storing the content");
        /* ... */

        println!("Updating the file's metadata");
        let meta = file_with_meta::FileHttpMetadata::from_file(&dst)?;
        fs::write(&dst_meta, serde_json::to_string(&meta).unwrap())?;
    }
};

Example for checking whether a file has changed since its metadata was last updated:

let dst = "/path/to/file.dat";
let dst_meta = "/path/to/file.dat.meta";

match file_with_meta::match_meta(&dst, &dst_meta)?.is_some() {
    true => println!("No change"),
    false => println!("Somebody touched our file, recreate it?"),
};

The match_meta_with_source function may be used to additionally make sure that a “source” file has not been modified since this file was last generated from its data.

Structs

Information about a single file’s last modification time and, if specified, some relevant HTTP headers returned by the server that the file was fetched from.
Information about the format of the JSON-serialized metadata.
The version of the format of the serialized metadata.

Enums

An error that occurred during processing the metadata.

Functions

Add the “If-Modified-Since” and/or “If-None-Match” headers to an HTTP request if the relevant fields (“Last-Modified” and “ETag” respectively) have been returned in the last response from the server when the file has been downloaded.
Verify that a file has not been changed since the last time the metadata was stored.
Verify that a file has not been changed, and additionally verify that its source file, specified by the src local path, has also not been changed. Useful when e.g. uncompressing or otherwise processing downloaded files.
Unwrap a Metadata object’s last modified timestamp, assume it may be converted to a Unix timestamp, and return the number of seconds since the Unix epoch.