geolith 0.2.1

Shared library of the geolith builders: source readers, conflation, admin areas, tile encoding and the PMTiles and MBTiles writers
Documentation
//! `PMTiles` output — archive writing, Hilbert encoding, deduplication.

pub mod format;
pub use format::{OutputFormat, PmtilesOutput};

pub mod mbtiles;
pub use mbtiles::MbtilesOutput;

pub mod dedup;
pub mod directory;
pub mod hilbert;
pub mod writer;

/// Make the directory entry of `path` durable: `fsync` its parent directory.
///
/// `File::sync_all` makes the data of a file durable, not its name in the
/// directory: after a power loss a synced run or archive can be missing from
/// its folder. Call this after a file is created or renamed. A no-op where a
/// directory cannot be opened for syncing (Windows).
///
/// # Errors
///
/// The I/O error of opening or syncing the parent directory.
pub fn sync_parent_dir(path: &std::path::Path) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        let parent = match path.parent() {
            Some(p) if !p.as_os_str().is_empty() => p,
            _ => std::path::Path::new("."),
        };
        std::fs::File::open(parent)?.sync_all()
    }
    #[cfg(not(unix))]
    {
        let _ = path;
        Ok(())
    }
}

#[cfg(test)]
mod sync_tests {
    use super::sync_parent_dir;

    #[test]
    fn syncing_the_parent_of_a_new_file_succeeds() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("run.bin");
        std::fs::write(&file, b"x").unwrap();
        sync_parent_dir(&file).unwrap();
        // A bare file name syncs the current directory.
        sync_parent_dir(std::path::Path::new("run-in-cwd.bin")).unwrap();
    }

    #[cfg(unix)]
    #[test]
    fn a_missing_parent_directory_is_an_error_not_a_silent_skip() {
        let dir = tempfile::tempdir().unwrap();
        let file = dir.path().join("gone").join("run.bin");
        let err = sync_parent_dir(&file).expect_err("the folder does not exist");
        assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
    }
}