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/";
27 static const char* kOdexCacheSuffix = "@classes.dex";
28 static const char* kVdexCacheSuffix = "@classes.vdex";
29
30 // Returns the ISA extracted from the file_location.
31 // file_location is formatted like /system/app/<app_name>/oat/<isa>/<app_name>.{odex,vdex}
32 // for all functions. We return an empty string "" in error cases.
ExtractISA(const std::string & file_location)33 static std::string ExtractISA(const std::string& file_location) {
34 std::vector<std::string> split_file_location = android::base::Split(file_location, "/");
35 if (split_file_location.size() <= 1) {
36 return "";
37 } else if (split_file_location.size() != 7) {
38 LOG(WARNING) << "Unexpected length for file-location. We expected 7 segments but found "
39 << split_file_location.size();
40 }
41 return split_file_location[split_file_location.size() - 2];
42 }
43
44 // Returns the apk name extracted from the file_location.
45 // file_location is formatted like /system/app/<app_name>/oat/<isa>/<app_name>.{odex,vdex}.
46 // We return the final <app_name> with the .{odex,vdex} replaced with .apk.
ExtractAPKName(const std::string & file_location)47 static std::string ExtractAPKName(const std::string& file_location) {
48 // Find and copy filename.
49 size_t file_location_start = file_location.rfind('/');
50 if (file_location_start == std::string::npos) {
51 return "";
52 }
53 size_t ext_start = file_location.rfind('.');
54 if (ext_start == std::string::npos || ext_start < file_location_start) {
55 return "";
56 }
57 std::string apk_name = file_location.substr(file_location_start + 1,
58 ext_start - file_location_start);
59
60 // Replace extension with .apk.
61 apk_name += "apk";
62 return apk_name;
63 }
64
65 // The cache file name is /data/dalvik-cache/<isa>/ prior to this function
SystemBFilenameToCacheFile(const std::string & file_location,std::string & cache_file)66 static bool SystemBFilenameToCacheFile(const std::string& file_location,
67 /*in-out*/std::string& cache_file) {
68 // Skip the first '/' in file_location.
69 size_t initial_position = file_location[0] == '/' ? 1 : 0;
70 size_t apk_position = file_location.find("/oat", initial_position);
71 if (apk_position == std::string::npos) {
72 LOG(ERROR) << "Unable to find oat directory!";
73 return false;
74 }
75
76 size_t cache_file_position = cache_file.size();
77 cache_file += file_location.substr(initial_position, apk_position);
78 // '/' -> '@' up to where the apk would be.
79 cache_file_position = cache_file.find('/', cache_file_position);
80 while (cache_file_position != std::string::npos) {
81 cache_file[cache_file_position] = '@';
82 cache_file_position = cache_file.find('/', cache_file_position);
83 }
84
85 // Add <apk_name>.
86 std::string apk_name = ExtractAPKName(file_location);
87 if (apk_name.empty()) {
88 LOG(ERROR) << "Unable to determine apk name from file name '" << file_location << "'";
89 return false;
90 }
91 cache_file += apk_name;
92 if (file_location.size() >= 5 &&
93 file_location.substr(file_location.size() - 5) == std::string(".vdex")) {
94 cache_file += kVdexCacheSuffix;
95 } else {
96 cache_file += kOdexCacheSuffix;
97 }
98 return true;
99 }
100
101 // Do the overall transformation from file_location to output_file_location. Prior to this the
102 // output_file_location is empty.
SystemBFileToCacheFile(const std::string & file_location,std::string & output_file_location)103 static bool SystemBFileToCacheFile(const std::string& file_location,
104 /*out*/std::string& output_file_location) {
105 std::string isa = ExtractISA(file_location);
106 if (isa.empty()) {
107 LOG(ERROR) << "Unable to determine isa for file '" << file_location << "', skipping";
108 return false;
109 }
110 output_file_location += isa;
111 output_file_location += '/';
112 return SystemBFilenameToCacheFile(file_location, output_file_location);
113 }
114
115 // This program is used to determine where in the /data directory the runtime will search for an
116 // odex file if it is unable to find one at the given 'preopt-name' location. This is used to allow
117 // us to store these preopted files in the unused system_b partition and copy them out on first
118 // boot of the device.
main(int argc,char * argv[])119 int main(int argc, char *argv[]) {
120 if (argc != 2) {
121 LOG(ERROR) << "usage: preopt2cachename preopt-location";
122 return 2;
123 }
124 std::string file_location(argv[1]);
125 std::string output_file_location(kDalvikCacheDir);
126 if (!SystemBFileToCacheFile(file_location, output_file_location)) {
127 return 1;
128 } else {
129 std::cout << output_file_location;
130 }
131 return 0;
132 }
133