xref: /aosp_15_r20/system/extras/cppreopts/cppreopts.sh (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker#!/system/bin/sh
2*288bf522SAndroid Build Coastguard Worker#
3*288bf522SAndroid Build Coastguard Worker# Copyright (C) 2016 The Android Open Source Project
4*288bf522SAndroid Build Coastguard Worker#
5*288bf522SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*288bf522SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*288bf522SAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*288bf522SAndroid Build Coastguard Worker#
9*288bf522SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
10*288bf522SAndroid Build Coastguard Worker#
11*288bf522SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*288bf522SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*288bf522SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*288bf522SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*288bf522SAndroid Build Coastguard Worker# limitations under the License.
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker# create files with 644 (global read) permissions.
18*288bf522SAndroid Build Coastguard Workerumask 022
19*288bf522SAndroid Build Coastguard Worker
20*288bf522SAndroid Build Coastguard Worker# Helper function to copy files
21*288bf522SAndroid Build Coastguard Workerfunction do_copy() {
22*288bf522SAndroid Build Coastguard Worker  source_file=$1
23*288bf522SAndroid Build Coastguard Worker  dest_name=$2
24*288bf522SAndroid Build Coastguard Worker  # Move to a temporary file so we can do a rename and have the preopted file
25*288bf522SAndroid Build Coastguard Worker  # appear atomically in the filesystem.
26*288bf522SAndroid Build Coastguard Worker  temp_dest_name=${dest_name}.tmp
27*288bf522SAndroid Build Coastguard Worker  if ! cp ${source_file} ${temp_dest_name} ; then
28*288bf522SAndroid Build Coastguard Worker    log -p w -t cppreopts "Unable to copy file ${source_file} to ${temp_dest_name}!"
29*288bf522SAndroid Build Coastguard Worker  else
30*288bf522SAndroid Build Coastguard Worker    log -p i -t cppreopts "Copied file from ${source_file} to ${temp_dest_name}"
31*288bf522SAndroid Build Coastguard Worker    fsync ${temp_dest_name}
32*288bf522SAndroid Build Coastguard Worker    if ! mv ${temp_dest_name} ${dest_name} ; then
33*288bf522SAndroid Build Coastguard Worker      log -p w -t cppreopts "Unable to rename temporary file from ${temp_dest_name} to ${dest_name}"
34*288bf522SAndroid Build Coastguard Worker      rm ${temp_dest_name} || log -p w -t cppreopts "Unable to remove temporary file ${temp_dest_name}"
35*288bf522SAndroid Build Coastguard Worker    else
36*288bf522SAndroid Build Coastguard Worker      fsync ${dest_name}
37*288bf522SAndroid Build Coastguard Worker      log -p i -t cppreopts "Renamed temporary file from ${temp_dest_name} to ${dest_name}"
38*288bf522SAndroid Build Coastguard Worker    fi
39*288bf522SAndroid Build Coastguard Worker  fi
40*288bf522SAndroid Build Coastguard Worker}
41*288bf522SAndroid Build Coastguard Worker
42*288bf522SAndroid Build Coastguard Workerif [ $# -eq 1 ]; then
43*288bf522SAndroid Build Coastguard Worker  # Where the system_b is mounted that contains the preopt'd files
44*288bf522SAndroid Build Coastguard Worker  mountpoint=$1
45*288bf522SAndroid Build Coastguard Worker
46*288bf522SAndroid Build Coastguard Worker  if ! test -f ${mountpoint}/system-other-odex-marker ; then
47*288bf522SAndroid Build Coastguard Worker    log -p i -t cppreopts "system_other partition does not appear to have been built to contain preopted files."
48*288bf522SAndroid Build Coastguard Worker    exit 1
49*288bf522SAndroid Build Coastguard Worker  fi
50*288bf522SAndroid Build Coastguard Worker
51*288bf522SAndroid Build Coastguard Worker  log -p i -t cppreopts "cppreopts from ${mountpoint}"
52*288bf522SAndroid Build Coastguard Worker  # For each odex and vdex file do the copy task
53*288bf522SAndroid Build Coastguard Worker  # NOTE: this implementation will break in any path with spaces to favor
54*288bf522SAndroid Build Coastguard Worker  # background copy tasks
55*288bf522SAndroid Build Coastguard Worker  for file in $(find ${mountpoint} -type f -name "*.odex" -o -type f -name "*.vdex" -o -type f -name "*.art"); do
56*288bf522SAndroid Build Coastguard Worker    real_name=${file/${mountpoint}/}
57*288bf522SAndroid Build Coastguard Worker    dest_name=$(preopt2cachename ${real_name})
58*288bf522SAndroid Build Coastguard Worker    if ! test $? -eq 0 ; then
59*288bf522SAndroid Build Coastguard Worker      log -p i -t cppreopts "Unable to figure out destination for ${file}"
60*288bf522SAndroid Build Coastguard Worker      continue
61*288bf522SAndroid Build Coastguard Worker    fi
62*288bf522SAndroid Build Coastguard Worker    # Copy files in background to speed things up
63*288bf522SAndroid Build Coastguard Worker    do_copy ${file} ${dest_name} &
64*288bf522SAndroid Build Coastguard Worker  done
65*288bf522SAndroid Build Coastguard Worker  # Wait for jobs to finish
66*288bf522SAndroid Build Coastguard Worker  wait
67*288bf522SAndroid Build Coastguard Worker  exit 0
68*288bf522SAndroid Build Coastguard Workerelse
69*288bf522SAndroid Build Coastguard Worker  log -p e -t cppreopts "Usage: cppreopts <preopts-mount-point>"
70*288bf522SAndroid Build Coastguard Worker  exit 1
71*288bf522SAndroid Build Coastguard Workerfi
72