Enhance OSDK performance by using size and modified time instead of sha256sum

This commit is contained in:
Ruize Tang
2024-12-09 17:07:48 +08:00
committed by Tate, Hongliang Tian
parent 9d82ac8958
commit ea1874aa46
7 changed files with 73 additions and 102 deletions

66
osdk/Cargo.lock generated
View File

@ -138,15 +138,6 @@ version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "bstr"
version = "1.9.1"
@ -202,7 +193,6 @@ dependencies = [
"rev_buf_reader",
"serde",
"serde_json",
"sha2",
"shlex",
"syn",
"toml",
@ -311,15 +301,6 @@ dependencies = [
"memchr",
]
[[package]]
name = "cpufeatures"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.4.2"
@ -344,16 +325,6 @@ version = "0.8.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "dary_heap"
version = "0.3.6"
@ -380,16 +351,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "doc-comment"
version = "0.3.3"
@ -431,16 +392,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.15"
@ -871,17 +822,6 @@ dependencies = [
"serde",
]
[[package]]
name = "sha2"
version = "0.10.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "shlex"
version = "1.3.0"
@ -958,12 +898,6 @@ dependencies = [
"winnow",
]
[[package]]
name = "typenum"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unicode-ident"
version = "1.0.12"

View File

@ -23,7 +23,6 @@ regex = "1.10.4"
rev_buf_reader = "0.3.0"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
sha2 = "0.10.8"
shlex = "1.3.0"
syn = { version = "2.0.52", features = ["extra-traits", "full", "parsing", "printing"] }
toml = { version = "0.8.8", features = ["preserve_order"] }

View File

@ -1,6 +1,10 @@
// SPDX-License-Identifier: MPL-2.0
use std::path::{Path, PathBuf};
use std::{
os::unix::fs::MetadataExt,
path::{Path, PathBuf},
time::SystemTime,
};
use super::file::BundleFile;
use crate::{arch::Arch, util::hard_link_or_copy};
@ -11,7 +15,8 @@ pub struct AsterBin {
arch: Arch,
typ: AsterBinType,
version: String,
sha256sum: String,
modified_time: SystemTime,
size: u64,
stripped: bool,
}
@ -41,8 +46,12 @@ impl BundleFile for AsterBin {
&self.path
}
fn sha256sum(&self) -> &String {
&self.sha256sum
fn modified_time(&self) -> &SystemTime {
&self.modified_time
}
fn size(&self) -> &u64 {
&self.size
}
}
@ -59,11 +68,13 @@ impl AsterBin {
arch,
typ,
version,
sha256sum: String::new(),
modified_time: SystemTime::UNIX_EPOCH,
size: 0,
stripped,
};
Self {
sha256sum: created.calculate_sha256sum(),
modified_time: created.get_modified_time(),
size: created.get_size(),
..created
}
}
@ -84,13 +95,15 @@ impl AsterBin {
pub fn copy_to(self, base: impl AsRef<Path>) -> Self {
let file_name = self.path.file_name().unwrap();
let copied_path = base.as_ref().join(file_name);
hard_link_or_copy(&self.path, copied_path).unwrap();
hard_link_or_copy(&self.path, &copied_path).unwrap();
let copied_metadata = copied_path.metadata().unwrap();
Self {
path: PathBuf::from(file_name),
arch: self.arch,
typ: self.typ,
version: self.version,
sha256sum: self.sha256sum,
modified_time: copied_metadata.modified().unwrap(),
size: copied_metadata.size(),
stripped: self.stripped,
}
}

View File

@ -1,36 +1,39 @@
// SPDX-License-Identifier: MPL-2.0
use std::{
fs, io,
os::unix::fs::MetadataExt,
path::{Path, PathBuf},
time::SystemTime,
};
use sha2::{Digest, Sha256};
use crate::util::hard_link_or_copy;
/// A trait for files in a bundle. The file in a bundle should have it's digest and be validatable.
/// A trait for files in a bundle. The file in a bundle should have its modified time and be validatable.
pub trait BundleFile {
fn path(&self) -> &PathBuf;
fn sha256sum(&self) -> &String;
fn modified_time(&self) -> &SystemTime;
fn calculate_sha256sum(&self) -> String {
let mut file = fs::File::open(self.path()).unwrap();
let mut hasher = Sha256::new();
let _n = io::copy(&mut file, &mut hasher).unwrap();
format!("{:x}", hasher.finalize())
fn size(&self) -> &u64;
fn get_modified_time(&self) -> SystemTime {
self.path().metadata().unwrap().modified().unwrap()
}
fn get_size(&self) -> u64 {
self.path().metadata().unwrap().size()
}
fn validate(&self) -> bool {
self.sha256sum() == &self.calculate_sha256sum()
self.size() == &self.get_size() && self.modified_time() >= &self.get_modified_time()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Initramfs {
path: PathBuf,
sha256sum: String,
modified_time: SystemTime,
size: u64,
}
impl BundleFile for Initramfs {
@ -38,8 +41,12 @@ impl BundleFile for Initramfs {
&self.path
}
fn sha256sum(&self) -> &String {
&self.sha256sum
fn modified_time(&self) -> &SystemTime {
&self.modified_time
}
fn size(&self) -> &u64 {
&self.size
}
}
@ -47,10 +54,12 @@ impl Initramfs {
pub fn new(path: impl AsRef<Path>) -> Self {
let created = Self {
path: path.as_ref().to_path_buf(),
sha256sum: String::new(),
modified_time: SystemTime::UNIX_EPOCH,
size: 0,
};
Self {
sha256sum: created.calculate_sha256sum(),
modified_time: created.get_modified_time(),
size: created.get_size(),
..created
}
}
@ -59,9 +68,10 @@ impl Initramfs {
pub fn copy_to(self, base: impl AsRef<Path>) -> Self {
let name = self.path.file_name().unwrap();
let dest = base.as_ref().join(name);
hard_link_or_copy(&self.path, dest).unwrap();
hard_link_or_copy(&self.path, &dest).unwrap();
Self {
path: PathBuf::from(name),
modified_time: dest.metadata().unwrap().modified().unwrap(),
..self
}
}

View File

@ -168,7 +168,9 @@ impl Bundle {
match (&self.manifest.initramfs, &config_action.boot.initramfs) {
(Some(initramfs), Some(initramfs_path)) => {
let config_initramfs = Initramfs::new(initramfs_path);
if initramfs.sha256sum() != config_initramfs.sha256sum() {
if initramfs.size() != config_initramfs.size()
|| initramfs.modified_time() < config_initramfs.modified_time()
{
return Err(initramfs_err);
}
}

View File

@ -1,6 +1,10 @@
// SPDX-License-Identifier: MPL-2.0
use std::path::{Path, PathBuf};
use std::{
os::unix::fs::MetadataExt,
path::{Path, PathBuf},
time::SystemTime,
};
use crate::util::hard_link_or_copy;
@ -11,7 +15,8 @@ pub struct AsterVmImage {
path: PathBuf,
typ: AsterVmImageType,
aster_version: String,
sha256sum: String,
modified_time: SystemTime,
size: u64,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
@ -35,8 +40,12 @@ impl BundleFile for AsterVmImage {
&self.path
}
fn sha256sum(&self) -> &String {
&self.sha256sum
fn modified_time(&self) -> &SystemTime {
&self.modified_time
}
fn size(&self) -> &u64 {
&self.size
}
}
@ -46,10 +55,12 @@ impl AsterVmImage {
path: path.as_ref().to_path_buf(),
typ,
aster_version,
sha256sum: String::new(),
modified_time: SystemTime::UNIX_EPOCH,
size: 0,
};
Self {
sha256sum: created.calculate_sha256sum(),
modified_time: created.get_modified_time(),
size: created.get_size(),
..created
}
}
@ -62,12 +73,14 @@ impl AsterVmImage {
pub fn copy_to(self, base: impl AsRef<Path>) -> Self {
let file_name = self.path.file_name().unwrap();
let copied_path = base.as_ref().join(file_name);
hard_link_or_copy(&self.path, copied_path).unwrap();
hard_link_or_copy(&self.path, &copied_path).unwrap();
let copied_metadata = copied_path.metadata().unwrap();
Self {
path: PathBuf::from(file_name),
typ: self.typ,
aster_version: self.aster_version,
sha256sum: self.sha256sum,
modified_time: copied_metadata.modified().unwrap(),
size: copied_metadata.size(),
}
}

View File

@ -140,7 +140,7 @@ pub fn do_cached_build(
// Check the existing bundle's reusability
if let Some(existing_bundle) = get_reusable_existing_bundle(&bundle_path, config, action) {
if get_last_modified_time(aster_elf.path()) < existing_bundle.last_modified_time() {
if aster_elf.modified_time() < &existing_bundle.last_modified_time() {
info!("Reusing existing bundle: aster_elf is unchanged");
return existing_bundle;
}