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