aconfig: move aconfig_storage_metadata proto and its apis to
aconfig_storage_file crate

Bug: b/321077378
Test: m aconfig_storage_file.test; m aconfig_storage_read_api.test
Change-Id: Ifeeeff62dc09e172b7e88c45860d2febccc29570
diff --git a/tools/aconfig/Cargo.toml b/tools/aconfig/Cargo.toml
index 7112fd4..6bd0d06 100644
--- a/tools/aconfig/Cargo.toml
+++ b/tools/aconfig/Cargo.toml
@@ -5,6 +5,7 @@
     "aconfig_protos",
     "aconfig_storage_file",
     "aconfig_storage_read_api",
+    "aconfig_storage_write_api",
     "aflags",
     "printflags"
 ]
diff --git a/tools/aconfig/aconfig_storage_file/Android.bp b/tools/aconfig/aconfig_storage_file/Android.bp
index 1d74f69..c089d54 100644
--- a/tools/aconfig/aconfig_storage_file/Android.bp
+++ b/tools/aconfig/aconfig_storage_file/Android.bp
@@ -11,6 +11,9 @@
         "libanyhow",
         "libthiserror",
         "libtempfile",
+        "libprotobuf",
+        "libclap",
+        "libaconfig_storage_protos",
     ],
 }
 
@@ -26,3 +29,25 @@
     test_suites: ["general-tests"],
     defaults: ["aconfig_storage_file.defaults"],
 }
+
+rust_protobuf {
+    name: "libaconfig_storage_protos",
+    protos: ["protos/aconfig_storage_metadata.proto"],
+    crate_name: "aconfig_storage_protos",
+    source_stem: "aconfig_storage_protos",
+    host_supported: true,
+}
+
+cc_library_static {
+    name: "libaconfig_storage_protos_cc",
+    proto: {
+        export_proto_headers: true,
+        type: "lite",
+    },
+    srcs: ["protos/aconfig_storage_metadata.proto"],
+    apex_available: [
+        "//apex_available:platform",
+        "//apex_available:anyapex",
+    ],
+    host_supported: true,
+}
diff --git a/tools/aconfig/aconfig_storage_file/Cargo.toml b/tools/aconfig/aconfig_storage_file/Cargo.toml
index 6a9483e..9b9a615 100644
--- a/tools/aconfig/aconfig_storage_file/Cargo.toml
+++ b/tools/aconfig/aconfig_storage_file/Cargo.toml
@@ -9,11 +9,8 @@
 
 [dependencies]
 anyhow = "1.0.69"
-memmap2 = "0.8.0"
 protobuf = "3.2.0"
-once_cell = "1.19.0"
 tempfile = "3.9.0"
-cxx = "1.0"
 thiserror = "1.0.56"
 clap = { version = "4.1.8", features = ["derive"] }
 
diff --git a/tools/aconfig/aconfig_storage_file/build.rs b/tools/aconfig/aconfig_storage_file/build.rs
new file mode 100644
index 0000000..1feeb60
--- /dev/null
+++ b/tools/aconfig/aconfig_storage_file/build.rs
@@ -0,0 +1,17 @@
+use protobuf_codegen::Codegen;
+
+fn main() {
+    let proto_files = vec!["protos/aconfig_storage_metadata.proto"];
+
+    // tell cargo to only re-run the build script if any of the proto files has changed
+    for path in &proto_files {
+        println!("cargo:rerun-if-changed={}", path);
+    }
+
+    Codegen::new()
+        .pure()
+        .include("protos")
+        .inputs(proto_files)
+        .cargo_out_dir("aconfig_storage_protos")
+        .run_from_script();
+}
diff --git a/tools/aconfig/aconfig_storage_read_api/protos/aconfig_storage_metadata.proto b/tools/aconfig/aconfig_storage_file/protos/aconfig_storage_metadata.proto
similarity index 100%
rename from tools/aconfig/aconfig_storage_read_api/protos/aconfig_storage_metadata.proto
rename to tools/aconfig/aconfig_storage_file/protos/aconfig_storage_metadata.proto
diff --git a/tools/aconfig/aconfig_storage_file/src/lib.rs b/tools/aconfig/aconfig_storage_file/src/lib.rs
index 7544838..ec41a4e 100644
--- a/tools/aconfig/aconfig_storage_file/src/lib.rs
+++ b/tools/aconfig/aconfig_storage_file/src/lib.rs
@@ -35,6 +35,7 @@
 pub mod flag_table;
 pub mod flag_value;
 pub mod package_table;
+pub mod protos;
 
 #[cfg(test)]
 mod test_utils;
diff --git a/tools/aconfig/aconfig_storage_read_api/src/protos.rs b/tools/aconfig/aconfig_storage_file/src/protos.rs
similarity index 78%
rename from tools/aconfig/aconfig_storage_read_api/src/protos.rs
rename to tools/aconfig/aconfig_storage_file/src/protos.rs
index 37df3e1..8b86205 100644
--- a/tools/aconfig/aconfig_storage_read_api/src/protos.rs
+++ b/tools/aconfig/aconfig_storage_file/src/protos.rs
@@ -49,8 +49,11 @@
 pub use auto_generated::*;
 
 use anyhow::Result;
+use protobuf::Message;
+use std::io::Write;
+use tempfile::NamedTempFile;
 
-pub mod storage_files {
+pub mod storage_record_pb {
     use super::*;
     use anyhow::ensure;
 
@@ -80,15 +83,28 @@
         }
         Ok(())
     }
+
+    pub fn get_binary_proto_from_text_proto(text_proto: &str) -> Result<Vec<u8>> {
+        let storage_files: ProtoStorageFiles = protobuf::text_format::parse_from_str(text_proto)?;
+        let mut binary_proto = Vec::new();
+        storage_files.write_to_vec(&mut binary_proto)?;
+        Ok(binary_proto)
+    }
+
+    pub fn write_proto_to_temp_file(text_proto: &str) -> Result<NamedTempFile> {
+        let bytes = get_binary_proto_from_text_proto(text_proto).unwrap();
+        let mut file = NamedTempFile::new()?;
+        let _ = file.write_all(&bytes);
+        Ok(file)
+    }
 }
 
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::test_utils::get_binary_storage_proto_bytes;
 
     #[test]
-    fn test_parse_storage_files() {
+    fn test_parse_storage_record_pb() {
         let text_proto = r#"
 files {
     version: 0
@@ -107,8 +123,9 @@
     timestamp: 54321
 }
 "#;
-        let binary_proto_bytes = get_binary_storage_proto_bytes(text_proto).unwrap();
-        let storage_files = storage_files::try_from_binary_proto(&binary_proto_bytes).unwrap();
+        let binary_proto_bytes =
+            storage_record_pb::get_binary_proto_from_text_proto(text_proto).unwrap();
+        let storage_files = storage_record_pb::try_from_binary_proto(&binary_proto_bytes).unwrap();
         assert_eq!(storage_files.files.len(), 2);
         let system_file = &storage_files.files[0];
         assert_eq!(system_file.version(), 0);
@@ -127,7 +144,7 @@
     }
 
     #[test]
-    fn test_parse_invalid_storage_files() {
+    fn test_parse_invalid_storage_record_pb() {
         let text_proto = r#"
 files {
     version: 0
@@ -138,8 +155,9 @@
     timestamp: 12345
 }
 "#;
-        let binary_proto_bytes = get_binary_storage_proto_bytes(text_proto).unwrap();
-        let err = storage_files::try_from_binary_proto(&binary_proto_bytes).unwrap_err();
+        let binary_proto_bytes =
+            storage_record_pb::get_binary_proto_from_text_proto(text_proto).unwrap();
+        let err = storage_record_pb::try_from_binary_proto(&binary_proto_bytes).unwrap_err();
         assert_eq!(
             format!("{:?}", err),
             "invalid storage file record: missing package map file for container system"
@@ -155,8 +173,9 @@
     timestamp: 12345
 }
 "#;
-        let binary_proto_bytes = get_binary_storage_proto_bytes(text_proto).unwrap();
-        let err = storage_files::try_from_binary_proto(&binary_proto_bytes).unwrap_err();
+        let binary_proto_bytes =
+            storage_record_pb::get_binary_proto_from_text_proto(text_proto).unwrap();
+        let err = storage_record_pb::try_from_binary_proto(&binary_proto_bytes).unwrap_err();
         assert_eq!(
             format!("{:?}", err),
             "invalid storage file record: missing flag map file for container system"
@@ -172,8 +191,9 @@
     timestamp: 12345
 }
 "#;
-        let binary_proto_bytes = get_binary_storage_proto_bytes(text_proto).unwrap();
-        let err = storage_files::try_from_binary_proto(&binary_proto_bytes).unwrap_err();
+        let binary_proto_bytes =
+            storage_record_pb::get_binary_proto_from_text_proto(text_proto).unwrap();
+        let err = storage_record_pb::try_from_binary_proto(&binary_proto_bytes).unwrap_err();
         assert_eq!(
             format!("{:?}", err),
             "invalid storage file record: missing flag val file for container system"
diff --git a/tools/aconfig/aconfig_storage_read_api/Android.bp b/tools/aconfig/aconfig_storage_read_api/Android.bp
index 5f4329c..721f9a5 100644
--- a/tools/aconfig/aconfig_storage_read_api/Android.bp
+++ b/tools/aconfig/aconfig_storage_read_api/Android.bp
@@ -9,9 +9,7 @@
     srcs: ["src/lib.rs"],
     rustlibs: [
         "libanyhow",
-        "libaconfig_storage_protos",
         "libonce_cell",
-        "libprotobuf",
         "libtempfile",
         "libmemmap2",
         "libcxx",
@@ -38,28 +36,6 @@
     ],
 }
 
-rust_protobuf {
-    name: "libaconfig_storage_protos",
-    protos: ["protos/aconfig_storage_metadata.proto"],
-    crate_name: "aconfig_storage_protos",
-    source_stem: "aconfig_storage_protos",
-    host_supported: true,
-}
-
-cc_library_static {
-    name: "libaconfig_storage_protos_cc",
-    proto: {
-        export_proto_headers: true,
-        type: "lite",
-    },
-    srcs: ["protos/aconfig_storage_metadata.proto"],
-    apex_available: [
-        "//apex_available:platform",
-        "//apex_available:anyapex",
-    ],
-    host_supported: true,
-}
-
 // cxx source codegen from rust api
 genrule {
     name: "libcxx_aconfig_storage_read_api_bridge_code",
diff --git a/tools/aconfig/aconfig_storage_read_api/Cargo.toml b/tools/aconfig/aconfig_storage_read_api/Cargo.toml
index 4c65d40..30a4298 100644
--- a/tools/aconfig/aconfig_storage_read_api/Cargo.toml
+++ b/tools/aconfig/aconfig_storage_read_api/Cargo.toml
@@ -10,7 +10,6 @@
 [dependencies]
 anyhow = "1.0.69"
 memmap2 = "0.8.0"
-protobuf = "3.2.0"
 once_cell = "1.19.0"
 tempfile = "3.9.0"
 cxx = "1.0"
diff --git a/tools/aconfig/aconfig_storage_read_api/build.rs b/tools/aconfig/aconfig_storage_read_api/build.rs
index 894b71c..7b1aa53 100644
--- a/tools/aconfig/aconfig_storage_read_api/build.rs
+++ b/tools/aconfig/aconfig_storage_read_api/build.rs
@@ -1,20 +1,4 @@
-use protobuf_codegen::Codegen;
-
 fn main() {
-    let proto_files = vec!["protos/aconfig_storage_metadata.proto"];
-
-    // tell cargo to only re-run the build script if any of the proto files has changed
-    for path in &proto_files {
-        println!("cargo:rerun-if-changed={}", path);
-    }
-
-    Codegen::new()
-        .pure()
-        .include("protos")
-        .inputs(proto_files)
-        .cargo_out_dir("aconfig_storage_protos")
-        .run_from_script();
-
     let _ = cxx_build::bridge("src/lib.rs");
     println!("cargo:rerun-if-changed=src/lib.rs");
 }
diff --git a/tools/aconfig/aconfig_storage_read_api/src/lib.rs b/tools/aconfig/aconfig_storage_read_api/src/lib.rs
index 0da81dd..3958b2e 100644
--- a/tools/aconfig/aconfig_storage_read_api/src/lib.rs
+++ b/tools/aconfig/aconfig_storage_read_api/src/lib.rs
@@ -38,14 +38,13 @@
 pub mod flag_value_query;
 pub mod mapped_file;
 pub mod package_table_query;
-pub mod protos;
 
 #[cfg(test)]
 mod test_utils;
 
-pub use crate::protos::ProtoStorageFiles;
 pub use aconfig_storage_file::{
-    read_u32_from_bytes, AconfigStorageError, StorageFileSelection, FILE_VERSION,
+    protos::ProtoStorageFiles, read_u32_from_bytes, AconfigStorageError, StorageFileSelection,
+    FILE_VERSION,
 };
 pub use flag_table_query::FlagOffset;
 pub use package_table_query::PackageOffset;
@@ -380,7 +379,8 @@
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::test_utils::{write_storage_text_to_temp_file, TestStorageFileSet};
+    use crate::test_utils::TestStorageFileSet;
+    use aconfig_storage_file::protos::storage_record_pb::write_proto_to_temp_file;
 
     fn create_test_storage_files(read_only: bool) -> TestStorageFileSet {
         TestStorageFileSet::new(
@@ -410,7 +410,7 @@
             ro_files.package_map.name, ro_files.flag_map.name, ro_files.flag_val.name
         );
 
-        let file = write_storage_text_to_temp_file(&text_proto).unwrap();
+        let file = write_proto_to_temp_file(&text_proto).unwrap();
         let file_full_path = file.path().display().to_string();
         let package_offset = get_package_offset_impl(
             &file_full_path,
@@ -461,7 +461,7 @@
             ro_files.package_map.name, ro_files.flag_map.name, ro_files.flag_val.name
         );
 
-        let file = write_storage_text_to_temp_file(&text_proto).unwrap();
+        let file = write_proto_to_temp_file(&text_proto).unwrap();
         let file_full_path = file.path().display().to_string();
         let baseline = vec![
             (0, "enabled_ro", 1u16),
@@ -500,7 +500,7 @@
             ro_files.package_map.name, ro_files.flag_map.name, ro_files.flag_val.name
         );
 
-        let file = write_storage_text_to_temp_file(&text_proto).unwrap();
+        let file = write_proto_to_temp_file(&text_proto).unwrap();
         let file_full_path = file.path().display().to_string();
         let baseline: Vec<bool> = vec![false; 8];
         for (offset, expected_value) in baseline.into_iter().enumerate() {
diff --git a/tools/aconfig/aconfig_storage_read_api/src/mapped_file.rs b/tools/aconfig/aconfig_storage_read_api/src/mapped_file.rs
index d8f2570..e94c56f 100644
--- a/tools/aconfig/aconfig_storage_read_api/src/mapped_file.rs
+++ b/tools/aconfig/aconfig_storage_read_api/src/mapped_file.rs
@@ -23,13 +23,13 @@
 use memmap2::Mmap;
 use once_cell::sync::Lazy;
 
-use crate::protos::{
-    storage_files::try_from_binary_proto, ProtoStorageFileInfo, ProtoStorageFiles,
-};
 use crate::AconfigStorageError::{
     self, FileReadFail, MapFileFail, ProtobufParseFail, StorageFileNotFound,
 };
 use crate::StorageFileSelection;
+use aconfig_storage_file::protos::{
+    storage_record_pb::try_from_binary_proto, ProtoStorageFileInfo, ProtoStorageFiles,
+};
 
 /// Cache for already mapped files
 static ALL_MAPPED_FILES: Lazy<Mutex<HashMap<String, MappedStorageFileSet>>> = Lazy::new(|| {
@@ -148,7 +148,8 @@
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::test_utils::{write_storage_text_to_temp_file, TestStorageFileSet};
+    use crate::test_utils::TestStorageFileSet;
+    use aconfig_storage_file::protos::storage_record_pb::write_proto_to_temp_file;
 
     #[test]
     fn test_find_storage_file_location() {
@@ -170,7 +171,7 @@
     timestamp: 54321
 }
 "#;
-        let file = write_storage_text_to_temp_file(text_proto).unwrap();
+        let file = write_proto_to_temp_file(&text_proto).unwrap();
         let file_full_path = file.path().display().to_string();
         let file_info = find_container_storage_location(&file_full_path, "system").unwrap();
         assert_eq!(file_info.version(), 0);
@@ -235,7 +236,7 @@
             ro_files.package_map.name, ro_files.flag_map.name, ro_files.flag_val.name
         );
 
-        let file = write_storage_text_to_temp_file(&text_proto).unwrap();
+        let file = write_proto_to_temp_file(&text_proto).unwrap();
         let file_full_path = file.path().display().to_string();
         map_and_verify(
             &file_full_path,
@@ -264,7 +265,7 @@
             rw_files.package_map.name, ro_files.flag_map.name, ro_files.flag_val.name
         );
 
-        let file = write_storage_text_to_temp_file(&text_proto).unwrap();
+        let file = write_proto_to_temp_file(&text_proto).unwrap();
         let file_full_path = file.path().display().to_string();
         let error = map_container_storage_files(&file_full_path, "system").unwrap_err();
         assert_eq!(
@@ -289,7 +290,7 @@
             ro_files.package_map.name, rw_files.flag_map.name, ro_files.flag_val.name
         );
 
-        let file = write_storage_text_to_temp_file(&text_proto).unwrap();
+        let file = write_proto_to_temp_file(&text_proto).unwrap();
         let file_full_path = file.path().display().to_string();
         let error = map_container_storage_files(&file_full_path, "system").unwrap_err();
         assert_eq!(
@@ -314,7 +315,7 @@
             ro_files.package_map.name, ro_files.flag_map.name, rw_files.flag_val.name
         );
 
-        let file = write_storage_text_to_temp_file(&text_proto).unwrap();
+        let file = write_proto_to_temp_file(&text_proto).unwrap();
         let file_full_path = file.path().display().to_string();
         let error = map_container_storage_files(&file_full_path, "system").unwrap_err();
         assert_eq!(
diff --git a/tools/aconfig/aconfig_storage_read_api/src/test_utils.rs b/tools/aconfig/aconfig_storage_read_api/src/test_utils.rs
index 588ecba..cc5938d 100644
--- a/tools/aconfig/aconfig_storage_read_api/src/test_utils.rs
+++ b/tools/aconfig/aconfig_storage_read_api/src/test_utils.rs
@@ -14,27 +14,10 @@
  * limitations under the License.
  */
 
-use crate::protos::ProtoStorageFiles;
 use anyhow::Result;
-use protobuf::Message;
 use std::fs;
-use std::io::Write;
 use tempfile::NamedTempFile;
 
-pub(crate) fn get_binary_storage_proto_bytes(text_proto: &str) -> Result<Vec<u8>> {
-    let storage_files: ProtoStorageFiles = protobuf::text_format::parse_from_str(text_proto)?;
-    let mut binary_proto = Vec::new();
-    storage_files.write_to_vec(&mut binary_proto)?;
-    Ok(binary_proto)
-}
-
-pub(crate) fn write_storage_text_to_temp_file(text_proto: &str) -> Result<NamedTempFile> {
-    let bytes = get_binary_storage_proto_bytes(text_proto).unwrap();
-    let mut file = NamedTempFile::new()?;
-    let _ = file.write_all(&bytes);
-    Ok(file)
-}
-
 fn set_file_read_only(file: &NamedTempFile) {
     let mut perms = fs::metadata(file.path()).unwrap().permissions();
     if !perms.readonly() {