Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #include <iostream> |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/strings.h> |
| 21 | |
| 22 | #ifndef LOG_TAG |
| 23 | #define LOG_TAG "preopt2cachename" |
| 24 | #endif |
| 25 | |
| 26 | static const char* kDalvikCacheDir = "/data/dalvik-cache/"; |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 27 | static const char* kOdexCacheSuffix = "@classes.dex"; |
| 28 | static const char* kVdexCacheSuffix = "@classes.vdex"; |
Mathieu Chartier | 496f021 | 2017-05-05 10:56:11 -0700 | [diff] [blame] | 29 | static const char* kArtCacheSuffix = "@classes.art"; |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 30 | |
Alex Light | c3728ab | 2019-04-09 10:19:11 -0700 | [diff] [blame] | 31 | // Returns the ISA extracted from the file_location. file_location is formatted like |
| 32 | // /system{/product,}/{priv-,}app/<app_name>/oat/<isa>/<app_name>.{odex,vdex} for all functions. We |
| 33 | // return an empty string "" in error cases. |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 34 | static std::string ExtractISA(const std::string& file_location) { |
| 35 | std::vector<std::string> split_file_location = android::base::Split(file_location, "/"); |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 36 | if (split_file_location.size() <= 1) { |
| 37 | return ""; |
Alex Light | c3728ab | 2019-04-09 10:19:11 -0700 | [diff] [blame] | 38 | } else if (split_file_location.size() != 7 && split_file_location.size() != 8) { |
| 39 | LOG(WARNING) << "Unexpected length for file-location. We expected 7 or 8 segments but found " |
| 40 | << split_file_location.size() << " for " << file_location; |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 41 | } |
| 42 | return split_file_location[split_file_location.size() - 2]; |
| 43 | } |
| 44 | |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 45 | // Returns the apk name extracted from the file_location. |
| 46 | // file_location is formatted like /system/app/<app_name>/oat/<isa>/<app_name>.{odex,vdex}. |
| 47 | // We return the final <app_name> with the .{odex,vdex} replaced with .apk. |
| 48 | static std::string ExtractAPKName(const std::string& file_location) { |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 49 | // Find and copy filename. |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 50 | size_t file_location_start = file_location.rfind('/'); |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 51 | if (file_location_start == std::string::npos) { |
| 52 | return ""; |
| 53 | } |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 54 | size_t ext_start = file_location.rfind('.'); |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 55 | if (ext_start == std::string::npos || ext_start < file_location_start) { |
| 56 | return ""; |
| 57 | } |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 58 | std::string apk_name = file_location.substr(file_location_start + 1, |
| 59 | ext_start - file_location_start); |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 60 | |
| 61 | // Replace extension with .apk. |
| 62 | apk_name += "apk"; |
| 63 | return apk_name; |
| 64 | } |
| 65 | |
| 66 | // The cache file name is /data/dalvik-cache/<isa>/ prior to this function |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 67 | static bool SystemBFilenameToCacheFile(const std::string& file_location, |
| 68 | /*in-out*/std::string& cache_file) { |
| 69 | // Skip the first '/' in file_location. |
| 70 | size_t initial_position = file_location[0] == '/' ? 1 : 0; |
| 71 | size_t apk_position = file_location.find("/oat", initial_position); |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 72 | if (apk_position == std::string::npos) { |
| 73 | LOG(ERROR) << "Unable to find oat directory!"; |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | size_t cache_file_position = cache_file.size(); |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 78 | cache_file += file_location.substr(initial_position, apk_position); |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 79 | // '/' -> '@' up to where the apk would be. |
| 80 | cache_file_position = cache_file.find('/', cache_file_position); |
| 81 | while (cache_file_position != std::string::npos) { |
| 82 | cache_file[cache_file_position] = '@'; |
| 83 | cache_file_position = cache_file.find('/', cache_file_position); |
| 84 | } |
| 85 | |
| 86 | // Add <apk_name>. |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 87 | std::string apk_name = ExtractAPKName(file_location); |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 88 | if (apk_name.empty()) { |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 89 | LOG(ERROR) << "Unable to determine apk name from file name '" << file_location << "'"; |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
Chih-Hung Hsieh | ad80770 | 2018-09-17 15:23:47 -0700 | [diff] [blame] | 92 | std::string::size_type pos = file_location.find_last_of('.'); |
Mathieu Chartier | 496f021 | 2017-05-05 10:56:11 -0700 | [diff] [blame] | 93 | if (pos == std::string::npos) { |
| 94 | LOG(ERROR) << "Invalid file location '" << file_location << "'"; |
| 95 | return false; |
| 96 | } |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 97 | cache_file += apk_name; |
Mathieu Chartier | 496f021 | 2017-05-05 10:56:11 -0700 | [diff] [blame] | 98 | std::string extension(file_location.substr(pos)); |
| 99 | if (extension == ".vdex") { |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 100 | cache_file += kVdexCacheSuffix; |
Mathieu Chartier | 496f021 | 2017-05-05 10:56:11 -0700 | [diff] [blame] | 101 | } else if (extension == ".art") { |
| 102 | cache_file += kArtCacheSuffix; |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 103 | } else { |
| 104 | cache_file += kOdexCacheSuffix; |
| 105 | } |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 109 | // Do the overall transformation from file_location to output_file_location. Prior to this the |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 110 | // output_file_location is empty. |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 111 | static bool SystemBFileToCacheFile(const std::string& file_location, |
| 112 | /*out*/std::string& output_file_location) { |
| 113 | std::string isa = ExtractISA(file_location); |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 114 | if (isa.empty()) { |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 115 | LOG(ERROR) << "Unable to determine isa for file '" << file_location << "', skipping"; |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | output_file_location += isa; |
| 119 | output_file_location += '/'; |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 120 | return SystemBFilenameToCacheFile(file_location, output_file_location); |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // This program is used to determine where in the /data directory the runtime will search for an |
| 124 | // odex file if it is unable to find one at the given 'preopt-name' location. This is used to allow |
| 125 | // us to store these preopted files in the unused system_b partition and copy them out on first |
| 126 | // boot of the device. |
| 127 | int main(int argc, char *argv[]) { |
| 128 | if (argc != 2) { |
| 129 | LOG(ERROR) << "usage: preopt2cachename preopt-location"; |
| 130 | return 2; |
| 131 | } |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 132 | std::string file_location(argv[1]); |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 133 | std::string output_file_location(kDalvikCacheDir); |
Nicolas Geoffray | 986ae62 | 2016-10-20 16:58:52 +0100 | [diff] [blame] | 134 | if (!SystemBFileToCacheFile(file_location, output_file_location)) { |
Alex Light | 9b71cad | 2016-06-21 16:17:48 -0700 | [diff] [blame] | 135 | return 1; |
| 136 | } else { |
| 137 | std::cout << output_file_location; |
| 138 | } |
| 139 | return 0; |
| 140 | } |