codewhale-runtime 0.10.3

Headless Codewhale runtime, split out of the terminal UI one module at a time; no terminal UI dependency
Documentation
use std::io::Write as _;
use std::path::Path;

/// Open or create a file for appending at `path`, optionally syncing after
/// every write. Use this for append-only logs like `audit.log`.
///
/// The returned `BufWriter<fs::File>` wraps the append handle. Call
/// `.flush()` followed by `.get_ref().sync_all()` after each batch.
pub fn open_append(path: &Path) -> std::io::Result<std::io::BufWriter<std::fs::File>> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let file = private_log_options().append(true).open(path)?;
    restrict_to_owner(&file)?;
    Ok(std::io::BufWriter::new(file))
}

/// Open options for an owner-only log or lock file: created 0600 and never
/// opened through a link at the final component (Unix). Callers add the
/// access mode they need.
pub fn private_log_options() -> std::fs::OpenOptions {
    let mut options = std::fs::OpenOptions::new();
    options.create(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600).custom_flags(libc::O_NOFOLLOW);
    }
    options
}

/// Tighten a log created by an earlier version at the default mode. No-op
/// outside Unix.
///
/// Best effort on purpose. The file was created `0600`, so this only narrows a
/// log left wider by an earlier version or a different umask — and a mount that
/// assigns modes itself (HarmonyOS hmdfs answers `EPERM` to every `chmod`) will
/// refuse it for every object it holds. Failing here would turn a write that
/// already succeeded into "permission denied"; a log left wide is reported
/// instead, so an operator can still see it.
pub fn restrict_to_owner(file: &std::fs::File) -> std::io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt as _;
        if file
            .set_permissions(std::fs::Permissions::from_mode(0o600))
            .is_err()
            && file
                .metadata()
                .is_ok_and(|metadata| metadata.permissions().mode() & 0o077 != 0)
        {
            tracing::warn!(
                "a private log could not be narrowed to 0600 and is still readable by group or \
                 other; this filesystem does not apply Unix mode bits"
            );
        }
    }
    #[cfg(not(unix))]
    let _ = file;
    Ok(())
}

/// Flush a `BufWriter` wrapping a `File`, then `fsync` the underlying file.
pub fn flush_and_sync(writer: &mut std::io::BufWriter<std::fs::File>) -> std::io::Result<()> {
    writer.flush()?;
    writer.get_ref().sync_all()
}

#[cfg(test)]
mod tests {
    use super::{flush_and_sync, open_append};

    #[test]
    fn flush_and_sync_writes_and_syncs() {
        use std::io::Write as _;
        let tmp = tempfile::tempdir().expect("tempdir");
        let path = tmp.path().join("append.log");
        {
            let mut writer = open_append(&path).expect("open_append");
            writeln!(writer, "line 1").expect("write");
            flush_and_sync(&mut writer).expect("flush_and_sync");
            writeln!(writer, "line 2").expect("write");
            flush_and_sync(&mut writer).expect("flush_and_sync");
        }
        let content = std::fs::read_to_string(&path).expect("read");
        assert_eq!(content, "line 1\nline 2\n");
    }

    #[cfg(unix)]
    #[test]
    fn append_logs_are_owner_only_and_not_opened_through_links() {
        use std::os::unix::fs::PermissionsExt;
        let dir = tempfile::tempdir().expect("tempdir");

        let log = dir.path().join("audit.log");
        drop(open_append(&log).expect("open"));
        let mode = std::fs::metadata(&log).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode, 0o600);

        // A log left world-readable by an earlier version is tightened.
        std::fs::set_permissions(&log, std::fs::Permissions::from_mode(0o644)).unwrap();
        drop(open_append(&log).expect("reopen"));
        let mode = std::fs::metadata(&log).unwrap().permissions().mode() & 0o777;
        assert_eq!(mode, 0o600);

        let target = dir.path().join("elsewhere");
        std::fs::write(&target, "").unwrap();
        let link = dir.path().join("linked.log");
        std::os::unix::fs::symlink(&target, &link).unwrap();
        assert!(open_append(&link).is_err());
    }
}