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* kOdexCacheSuffix = "@classes.dex";
     28 static const char* kVdexCacheSuffix = "@classes.vdex";
     29 static const char* kArtCacheSuffix = "@classes.art";
     30 
     31 // Returns the ISA extracted from the file_location.
     32 // file_location is formatted like /system/app/<app_name>/oat/<isa>/<app_name>.{odex,vdex}
     33 // for all functions. We return an empty string "" in error cases.
     34 static std::string ExtractISA(const std::string& file_location) {
     35   std::vector<std::string> split_file_location = android::base::Split(file_location, "/");
     36   if (split_file_location.size() <= 1) {
     37     return "";
     38   } else if (split_file_location.size() != 7) {
     39     LOG(WARNING) << "Unexpected length for file-location. We expected 7 segments but found "
     40                  << split_file_location.size();
     41   }
     42   return split_file_location[split_file_location.size() - 2];
     43 }
     44 
     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) {
     49   // Find and copy filename.
     50   size_t file_location_start = file_location.rfind('/');
     51   if (file_location_start == std::string::npos) {
     52     return "";
     53   }
     54   size_t ext_start = file_location.rfind('.');
     55   if (ext_start == std::string::npos || ext_start < file_location_start) {
     56     return "";
     57   }
     58   std::string apk_name = file_location.substr(file_location_start + 1,
     59                                               ext_start - file_location_start);
     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
     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);
     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();
     78   cache_file += file_location.substr(initial_position, apk_position);
     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>.
     87   std::string apk_name = ExtractAPKName(file_location);
     88   if (apk_name.empty()) {
     89     LOG(ERROR) << "Unable to determine apk name from file name '" << file_location << "'";
     90     return false;
     91   }
     92   std::string::size_type pos = file_location.find_last_of(".");
     93   if (pos == std::string::npos) {
     94     LOG(ERROR) << "Invalid file location '" << file_location << "'";
     95     return false;
     96   }
     97   cache_file += apk_name;
     98   std::string extension(file_location.substr(pos));
     99   if (extension == ".vdex") {
    100     cache_file += kVdexCacheSuffix;
    101   } else if (extension == ".art") {
    102     cache_file += kArtCacheSuffix;
    103   } else {
    104     cache_file += kOdexCacheSuffix;
    105   }
    106   return true;
    107 }
    108 
    109 // Do the overall transformation from file_location to output_file_location. Prior to this the
    110 // output_file_location is empty.
    111 static bool SystemBFileToCacheFile(const std::string& file_location,
    112                                    /*out*/std::string& output_file_location) {
    113   std::string isa = ExtractISA(file_location);
    114   if (isa.empty()) {
    115     LOG(ERROR) << "Unable to determine isa for file '" << file_location << "', skipping";
    116     return false;
    117   }
    118   output_file_location += isa;
    119   output_file_location += '/';
    120   return SystemBFilenameToCacheFile(file_location, output_file_location);
    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   }
    132   std::string file_location(argv[1]);
    133   std::string output_file_location(kDalvikCacheDir);
    134   if (!SystemBFileToCacheFile(file_location, output_file_location)) {
    135     return 1;
    136   } else {
    137     std::cout << output_file_location;
    138   }
    139   return 0;
    140 }
    141