Use quick-protobuf

This commit is contained in:
LoveSy 2023-06-21 00:22:48 +08:00 committed by John Wu
parent 9929b25339
commit 51e985ae7f
8 changed files with 68 additions and 186 deletions

180
native/src/Cargo.lock generated
View File

@ -2,15 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "aho-corasick"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "anstream" name = "anstream"
version = "0.3.2" version = "0.3.2"
@ -198,12 +189,6 @@ dependencies = [
"syn 2.0.18", "syn 2.0.18",
] ]
[[package]]
name = "either"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.1" version = "0.3.1"
@ -225,21 +210,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "fastrand"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be"
dependencies = [
"instant",
]
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.4.1" version = "0.4.1"
@ -252,25 +222,6 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "indexmap"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"cfg-if",
]
[[package]] [[package]]
name = "io-lifetimes" name = "io-lifetimes"
version = "1.0.11" version = "1.0.11"
@ -333,8 +284,8 @@ dependencies = [
"clap", "clap",
"cxx", "cxx",
"cxx-gen", "cxx-gen",
"protobuf", "pb-rs",
"protobuf-codegen", "quick-protobuf",
"size", "size",
] ]
@ -364,6 +315,22 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]] [[package]]
name = "num-derive" name = "num-derive"
version = "0.3.3" version = "0.3.3"
@ -390,6 +357,15 @@ version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "pb-rs"
version = "0.10.0"
source = "git+https://github.com/tafia/quick-protobuf.git?rev=2f37d5a65504de7d716b5b28fd82219501a901a9#2f37d5a65504de7d716b5b28fd82219501a901a9"
dependencies = [
"log",
"nom",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.60" version = "1.0.60"
@ -400,54 +376,11 @@ dependencies = [
] ]
[[package]] [[package]]
name = "protobuf" name = "quick-protobuf"
version = "3.2.0" version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/tafia/quick-protobuf.git?rev=2f37d5a65504de7d716b5b28fd82219501a901a9#2f37d5a65504de7d716b5b28fd82219501a901a9"
checksum = "b55bad9126f378a853655831eb7363b7b01b81d19f8cb1218861086ca4a1a61e"
dependencies = [ dependencies = [
"once_cell", "byteorder",
"protobuf-support",
"thiserror",
]
[[package]]
name = "protobuf-codegen"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dd418ac3c91caa4032d37cb80ff0d44e2ebe637b2fb243b6234bf89cdac4901"
dependencies = [
"anyhow",
"once_cell",
"protobuf",
"protobuf-parse",
"regex",
"tempfile",
"thiserror",
]
[[package]]
name = "protobuf-parse"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d39b14605eaa1f6a340aec7f320b34064feb26c93aec35d6a9a2272a8ddfa49"
dependencies = [
"anyhow",
"indexmap",
"log",
"protobuf",
"protobuf-support",
"tempfile",
"thiserror",
"which",
]
[[package]]
name = "protobuf-support"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5d4d7b8601c814cfb36bcebb79f0e61e45e1e93640cf778837833bbed05c372"
dependencies = [
"thiserror",
] ]
[[package]] [[package]]
@ -459,32 +392,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "redox_syscall"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29"
dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0ab3ca65655bb1e41f2a8c8cd662eb4fb035e67c3f78da1d61dffe89d07300f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.37.19" version = "0.37.19"
@ -533,20 +440,6 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "tempfile"
version = "3.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31c0432476357e58790aaa47a8efb0c5138f137343f3b5f23bd36a27e3b0a6d6"
dependencies = [
"autocfg",
"cfg-if",
"fastrand",
"redox_syscall",
"rustix",
"windows-sys",
]
[[package]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.2.0" version = "1.2.0"
@ -594,17 +487,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "which"
version = "4.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2441c784c52b289a054b7201fc93253e288f094e2f4be9058343127c4226a269"
dependencies = [
"either",
"libc",
"once_cell",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View File

@ -12,11 +12,11 @@ anyhow = "1.0"
num-traits = "0.2" num-traits = "0.2"
num-derive = "0.3" num-derive = "0.3"
thiserror = "1.0" thiserror = "1.0"
protobuf = "3.2.0"
protobuf-codegen = "3.2.0"
byteorder = "1" byteorder = "1"
size = "0.4" size = "0.4"
clap = "4.3" clap = "4.3"
pb-rs = { git = "https://github.com/tafia/quick-protobuf.git", rev = "2f37d5a65504de7d716b5b28fd82219501a901a9", default-features = false }
quick-protobuf = { git = "https://github.com/tafia/quick-protobuf.git", rev = "2f37d5a65504de7d716b5b28fd82219501a901a9" }
[profile.dev] [profile.dev]
opt-level = "z" opt-level = "z"

View File

@ -1 +1,2 @@
update_metadata.rs proto/update_metadata.rs
proto/mod.rs

View File

@ -8,14 +8,14 @@ crate-type = ["staticlib"]
path = "lib.rs" path = "lib.rs"
[build-dependencies] [build-dependencies]
protobuf-codegen = { workspace = true }
cxx-gen = { workspace = true } cxx-gen = { workspace = true }
pb-rs = { workspace = true }
[dependencies] [dependencies]
base = { path = "../base" } base = { path = "../base" }
cxx = { path = "../external/cxx-rs" } cxx = { path = "../external/cxx-rs" }
protobuf = { workspace = true }
byteorder = { workspace = true } byteorder = { workspace = true }
anyhow = { workspace = true } anyhow = { workspace = true }
clap = { workspace = true, features = ["derive"] } clap = { workspace = true, features = ["derive"] }
size = { workspace = true } size = { workspace = true }
quick-protobuf = { workspace = true }

View File

@ -1,19 +1,26 @@
use protobuf_codegen::Customize;
use crate::gen::gen_cxx_binding; use crate::gen::gen_cxx_binding;
use pb_rs::{types::FileDescriptor, ConfigBuilder};
#[path = "../include/gen.rs"] #[path = "../include/gen.rs"]
mod gen; mod gen;
fn main() { fn main() {
println!("cargo:rerun-if-changed=update_metadata.proto"); println!("cargo:rerun-if-changed=proto/update_metadata.proto");
protobuf_codegen::Codegen::new()
.pure()
.include(".")
.input("update_metadata.proto")
.customize(Customize::default().gen_mod_rs(false))
.out_dir(".")
.run_from_script();
gen_cxx_binding("boot-rs"); gen_cxx_binding("boot-rs");
let cb = ConfigBuilder::new(
&["proto/update_metadata.proto"],
None,
Some(&"proto"),
&["."],
)
.unwrap();
FileDescriptor::run(
&cb.single_module(true)
.dont_use_cow(true)
.generate_getters(true)
.build(),
)
.unwrap();
} }

View File

@ -10,8 +10,8 @@ pub use ramdisk::*;
mod cpio; mod cpio;
mod payload; mod payload;
mod proto;
mod ramdisk; mod ramdisk;
mod update_metadata;
#[cxx::bridge] #[cxx::bridge]
pub mod ffi { pub mod ffi {

View File

@ -4,15 +4,15 @@ use std::os::fd::{AsRawFd, FromRawFd};
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use byteorder::{BigEndian, ReadBytesExt}; use byteorder::{BigEndian, ReadBytesExt};
use protobuf::{EnumFull, Message}; use quick_protobuf::{BytesReader, MessageRead};
use base::libc::c_char; use base::libc::c_char;
use base::{ReadSeekExt, StrErr, Utf8CStr}; use base::{ReadSeekExt, StrErr, Utf8CStr};
use base::{ResultExt, WriteExt}; use base::{ResultExt, WriteExt};
use crate::ffi; use crate::ffi;
use crate::update_metadata::install_operation::Type; use crate::proto::update_metadata::mod_InstallOperation::Type;
use crate::update_metadata::DeltaArchiveManifest; use crate::proto::update_metadata::DeltaArchiveManifest;
macro_rules! bad_payload { macro_rules! bad_payload {
($msg:literal) => { ($msg:literal) => {
@ -64,42 +64,43 @@ fn do_extract_boot_from_payload(
let manifest = { let manifest = {
let manifest = &mut buf[..manifest_len]; let manifest = &mut buf[..manifest_len];
reader.read_exact(manifest)?; reader.read_exact(manifest)?;
DeltaArchiveManifest::parse_from_bytes(manifest)? let mut br = BytesReader::from_bytes(manifest);
DeltaArchiveManifest::from_reader(&mut br, manifest)?
}; };
if !manifest.has_minor_version() || manifest.minor_version() != 0 { if manifest.get_minor_version() != 0 {
return Err(bad_payload!( return Err(bad_payload!(
"delta payloads are not supported, please use a full payload file" "delta payloads are not supported, please use a full payload file"
)); ));
} }
let block_size = manifest.block_size() as u64; let block_size = manifest.get_block_size() as u64;
let partition = match partition_name { let partition = match partition_name {
None => { None => {
let boot = manifest let boot = manifest
.partitions .partitions
.iter() .iter()
.find(|p| p.partition_name() == "init_boot"); .find(|p| p.partition_name == "init_boot");
let boot = match boot { let boot = match boot {
Some(boot) => Some(boot), Some(boot) => Some(boot),
None => manifest None => manifest
.partitions .partitions
.iter() .iter()
.find(|p| p.partition_name() == "boot"), .find(|p| p.partition_name == "boot"),
}; };
boot.ok_or(anyhow!("boot partition not found"))? boot.ok_or(anyhow!("boot partition not found"))?
} }
Some(name) => manifest Some(name) => manifest
.partitions .partitions
.iter() .iter()
.find(|p| p.partition_name() == name) .find(|p| p.partition_name.as_str() == &*name)
.ok_or(anyhow!("partition '{name}' not found"))?, .ok_or(anyhow!("partition '{name}' not found"))?,
}; };
let out_str: String; let out_str: String;
let out_path = match out_path { let out_path = match out_path {
None => { None => {
out_str = format!("{}.img", partition.partition_name()); out_str = format!("{}.img", partition.partition_name);
out_str.as_str() out_str.as_str()
} }
Some(s) => s, Some(s) => s,
@ -126,11 +127,7 @@ fn do_extract_boot_from_payload(
.data_offset .data_offset
.ok_or(bad_payload!("data offset not found"))?; .ok_or(bad_payload!("data offset not found"))?;
let data_type = operation let data_type = operation.type_pb;
.type_
.ok_or(bad_payload!("operation type not found"))?
.enum_value()
.map_err(|_| bad_payload!("operation type not valid"))?;
buf.resize(data_len, 0u8); buf.resize(data_len, 0u8);
let data = &mut buf[..data_len]; let data = &mut buf[..data_len];
@ -171,12 +168,7 @@ fn do_extract_boot_from_payload(
return Err(bad_payload!("decompression failed")); return Err(bad_payload!("decompression failed"));
} }
} }
_ => { _ => return Err(bad_payload!("unsupported operation type")),
return Err(bad_payload!(
"unsupported operation type: {}",
data_type.descriptor().name()
));
}
}; };
} }