1 // Copyright 2022, The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 //! Command to create or update an idsig for APK
16 
17 use android_system_virtualizationservice::aidl::android::system::virtualizationservice::IVirtualizationService::IVirtualizationService;
18 use binder::ParcelFileDescriptor;
19 use anyhow::{Context, Error};
20 use std::fs::{File, OpenOptions};
21 use std::path::Path;
22 
23 /// Creates or update the idsig file by digesting the input APK file.
command_create_idsig( service: &dyn IVirtualizationService, apk: &Path, idsig: &Path, ) -> Result<(), Error>24 pub fn command_create_idsig(
25     service: &dyn IVirtualizationService,
26     apk: &Path,
27     idsig: &Path,
28 ) -> Result<(), Error> {
29     let apk_file = File::open(apk).with_context(|| format!("Failed to open {:?}", apk))?;
30     let idsig_file = OpenOptions::new()
31         .create(true)
32         .truncate(true)
33         .read(true)
34         .write(true)
35         .open(idsig)
36         .with_context(|| format!("Failed to create/open {:?}", idsig))?;
37     service
38         .createOrUpdateIdsigFile(
39             &ParcelFileDescriptor::new(apk_file),
40             &ParcelFileDescriptor::new(idsig_file),
41         )
42         .with_context(|| format!("Failed to create/update idsig for {:?}", apk))?;
43     Ok(())
44 }
45