blob: c4444418d50f2496f476805c5e1d8d4767e3bf98 [file] [log] [blame]
Alex Light9b71cad2016-06-21 16:17:48 -07001/*
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
26static const char* kDalvikCacheDir = "/data/dalvik-cache/";
Nicolas Geoffray986ae622016-10-20 16:58:52 +010027static const char* kOdexCacheSuffix = "@classes.dex";
28static const char* kVdexCacheSuffix = "@classes.vdex";
Mathieu Chartier496f0212017-05-05 10:56:11 -070029static const char* kArtCacheSuffix = "@classes.art";
Alex Light9b71cad2016-06-21 16:17:48 -070030
Alex Lightc3728ab2019-04-09 10:19:11 -070031// 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 Geoffray986ae622016-10-20 16:58:52 +010034static std::string ExtractISA(const std::string& file_location) {
35 std::vector<std::string> split_file_location = android::base::Split(file_location, "/");
Alex Light9b71cad2016-06-21 16:17:48 -070036 if (split_file_location.size() <= 1) {
37 return "";
Alex Lightc3728ab2019-04-09 10:19:11 -070038 } 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 Light9b71cad2016-06-21 16:17:48 -070041 }
42 return split_file_location[split_file_location.size() - 2];
43}
44
Nicolas Geoffray986ae622016-10-20 16:58:52 +010045// 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.
48static std::string ExtractAPKName(const std::string& file_location) {
Alex Light9b71cad2016-06-21 16:17:48 -070049 // Find and copy filename.
Nicolas Geoffray986ae622016-10-20 16:58:52 +010050 size_t file_location_start = file_location.rfind('/');
Alex Light9b71cad2016-06-21 16:17:48 -070051 if (file_location_start == std::string::npos) {
52 return "";
53 }
Nicolas Geoffray986ae622016-10-20 16:58:52 +010054 size_t ext_start = file_location.rfind('.');
Alex Light9b71cad2016-06-21 16:17:48 -070055 if (ext_start == std::string::npos || ext_start < file_location_start) {
56 return "";
57 }
Nicolas Geoffray986ae622016-10-20 16:58:52 +010058 std::string apk_name = file_location.substr(file_location_start + 1,
59 ext_start - file_location_start);
Alex Light9b71cad2016-06-21 16:17:48 -070060
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 Geoffray986ae622016-10-20 16:58:52 +010067static 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 Light9b71cad2016-06-21 16:17:48 -070072 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 Geoffray986ae622016-10-20 16:58:52 +010078 cache_file += file_location.substr(initial_position, apk_position);
Alex Light9b71cad2016-06-21 16:17:48 -070079 // '/' -> '@' 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 Geoffray986ae622016-10-20 16:58:52 +010087 std::string apk_name = ExtractAPKName(file_location);
Alex Light9b71cad2016-06-21 16:17:48 -070088 if (apk_name.empty()) {
Nicolas Geoffray986ae622016-10-20 16:58:52 +010089 LOG(ERROR) << "Unable to determine apk name from file name '" << file_location << "'";
Alex Light9b71cad2016-06-21 16:17:48 -070090 return false;
91 }
Chih-Hung Hsiehad807702018-09-17 15:23:47 -070092 std::string::size_type pos = file_location.find_last_of('.');
Mathieu Chartier496f0212017-05-05 10:56:11 -070093 if (pos == std::string::npos) {
94 LOG(ERROR) << "Invalid file location '" << file_location << "'";
95 return false;
96 }
Alex Light9b71cad2016-06-21 16:17:48 -070097 cache_file += apk_name;
Mathieu Chartier496f0212017-05-05 10:56:11 -070098 std::string extension(file_location.substr(pos));
99 if (extension == ".vdex") {
Nicolas Geoffray986ae622016-10-20 16:58:52 +0100100 cache_file += kVdexCacheSuffix;
Mathieu Chartier496f0212017-05-05 10:56:11 -0700101 } else if (extension == ".art") {
102 cache_file += kArtCacheSuffix;
Nicolas Geoffray986ae622016-10-20 16:58:52 +0100103 } else {
104 cache_file += kOdexCacheSuffix;
105 }
Alex Light9b71cad2016-06-21 16:17:48 -0700106 return true;
107}
108
Nicolas Geoffray986ae622016-10-20 16:58:52 +0100109// Do the overall transformation from file_location to output_file_location. Prior to this the
Alex Light9b71cad2016-06-21 16:17:48 -0700110// output_file_location is empty.
Nicolas Geoffray986ae622016-10-20 16:58:52 +0100111static bool SystemBFileToCacheFile(const std::string& file_location,
112 /*out*/std::string& output_file_location) {
113 std::string isa = ExtractISA(file_location);
Alex Light9b71cad2016-06-21 16:17:48 -0700114 if (isa.empty()) {
Nicolas Geoffray986ae622016-10-20 16:58:52 +0100115 LOG(ERROR) << "Unable to determine isa for file '" << file_location << "', skipping";
Alex Light9b71cad2016-06-21 16:17:48 -0700116 return false;
117 }
118 output_file_location += isa;
119 output_file_location += '/';
Nicolas Geoffray986ae622016-10-20 16:58:52 +0100120 return SystemBFilenameToCacheFile(file_location, output_file_location);
Alex Light9b71cad2016-06-21 16:17:48 -0700121}
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.
127int main(int argc, char *argv[]) {
128 if (argc != 2) {
129 LOG(ERROR) << "usage: preopt2cachename preopt-location";
130 return 2;
131 }
Nicolas Geoffray986ae622016-10-20 16:58:52 +0100132 std::string file_location(argv[1]);
Alex Light9b71cad2016-06-21 16:17:48 -0700133 std::string output_file_location(kDalvikCacheDir);
Nicolas Geoffray986ae622016-10-20 16:58:52 +0100134 if (!SystemBFileToCacheFile(file_location, output_file_location)) {
Alex Light9b71cad2016-06-21 16:17:48 -0700135 return 1;
136 } else {
137 std::cout << output_file_location;
138 }
139 return 0;
140}