Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //! Helpers for running odrefresh |
| 18 | |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame^] | 19 | use anyhow::{anyhow, Result}; |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 20 | use num_derive::FromPrimitive; |
| 21 | use num_traits::FromPrimitive; |
| 22 | |
| 23 | /// The path to the odrefresh binary |
| 24 | pub const ODREFRESH_PATH: &str = "/apex/com.android.art/bin/odrefresh"; |
| 25 | |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame^] | 26 | // The highest "standard" exit code defined in sysexits.h (as EX__MAX); odrefresh error codes |
| 27 | // start above here to avoid clashing. |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 28 | // TODO: What if this changes? |
| 29 | const EX_MAX: i8 = 78; |
| 30 | |
| 31 | /// The defined odrefresh exit codes - see art/odrefresh/include/odrefresh/odrefresh.h |
| 32 | #[derive(Debug, PartialEq, Eq, FromPrimitive)] |
| 33 | #[repr(i8)] |
| 34 | pub enum ExitCode { |
| 35 | /// No compilation required, all artifacts look good |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame^] | 36 | Okay = 0, |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 37 | /// Compilation required |
| 38 | CompilationRequired = EX_MAX + 1, |
| 39 | /// New artifacts successfully generated |
| 40 | CompilationSuccess = EX_MAX + 2, |
| 41 | /// Compilation failed |
| 42 | CompilationFailed = EX_MAX + 3, |
| 43 | /// Removal of existing invalid artifacts failed |
| 44 | CleanupFailed = EX_MAX + 4, |
| 45 | } |
| 46 | |
| 47 | impl ExitCode { |
| 48 | /// Map an integer to the corresponding ExitCode enum, if there is one |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame^] | 49 | pub fn from_i32(exit_code: i32) -> Result<Self> { |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 50 | FromPrimitive::from_i32(exit_code) |
Alan Stokes | 126fd51 | 2021-12-16 15:00:01 +0000 | [diff] [blame^] | 51 | .ok_or_else(|| anyhow!("Unexpected odrefresh exit code: {}", exit_code)) |
Alan Stokes | 46a1dff | 2021-12-14 10:56:05 +0000 | [diff] [blame] | 52 | } |
| 53 | } |