replace mtime with birth time

This commit is contained in:
Adithya Nair 2023-03-21 22:34:47 +05:30
parent bdcada4bc2
commit e033a2fcaf
No known key found for this signature in database
GPG Key ID: 0C7F35F4F821290F
3 changed files with 9 additions and 12 deletions

View File

@ -1,6 +1,5 @@
use std::fmt;
use std::fs;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use anyhow::{anyhow, Context, Result};
@ -96,7 +95,7 @@ impl fmt::Display for Generation {
}
fn read_build_time(path: &Path) -> Result<Date> {
let build_time = time::OffsetDateTime::from_unix_timestamp(fs::metadata(path)?.mtime())?.date();
let build_time = time::OffsetDateTime::from(fs::metadata(path)?.created().unwrap()).date();
Ok(build_time)
}

View File

@ -70,9 +70,6 @@ pub fn setup_generation_link_from_toplevel(
let mut file = fs::File::create(bootspec_path)?;
file.write_all(&serde_json::to_vec(&bootspec)?)?;
// Explicitly set modification time so that snapshot test of os-release reliably works.
// This has to happen after any modifications to the directory.
filetime::set_file_mtime(&generation_link_path, filetime::FileTime::zero())?;
Ok(generation_link_path)
}

View File

@ -1,7 +1,6 @@
use std::fs;
use anyhow::{Context, Result};
use expect_test::expect;
use tempfile::tempdir;
mod common;
@ -15,6 +14,9 @@ fn generate_expected_os_release() -> Result<()> {
let generation_link = common::setup_generation_link(tmpdir.path(), profiles.path(), 1)
.expect("Failed to setup generation link");
// Expect the 'Built on' date in VERSION_ID to be from the birth time of generation_link
let expected_built_on = time::OffsetDateTime::from(fs::metadata(generation_link.as_path())?.created().unwrap()).date();
let output0 = common::lanzaboote_install(0, esp_mountpoint.path(), vec![generation_link])?;
assert!(output0.status.success());
@ -27,13 +29,12 @@ fn generate_expected_os_release() -> Result<()> {
.context("Failed to read .osrelease PE section.")?
.to_owned();
let expected = expect![[r#"
ID=lanza
PRETTY_NAME=LanzaOS
VERSION_ID=Generation 1, Built on 1970-01-01
"#]];
let expected_os_release_section = format!(r#"ID=lanza
PRETTY_NAME=LanzaOS
VERSION_ID=Generation 1, Built on {}
"#, expected_built_on.to_string());
expected.assert_eq(&String::from_utf8(os_release_section)?);
assert_eq!(&expected_os_release_section, &String::from_utf8(os_release_section)?);
Ok(())
}