19b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light#!/system/bin/sh
29b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light#
39b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light# Copyright (C) 2016 The Android Open Source Project
49b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light#
59b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light# Licensed under the Apache License, Version 2.0 (the "License");
69b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light# you may not use this file except in compliance with the License.
79b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light# You may obtain a copy of the License at
89b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light#
99b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light#      http://www.apache.org/licenses/LICENSE-2.0
109b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light#
119b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light# Unless required by applicable law or agreed to in writing, software
129b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light# distributed under the License is distributed on an "AS IS" BASIS,
139b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light# See the License for the specific language governing permissions and
159b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light# limitations under the License.
169b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light
179b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light# create files with 644 (global read) permissions.
189b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Lightumask 022
199b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light
20757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang# Helper function to copy files
21757d341496d8c7e69420eb51c82be2d2bc694079Wei Wangfunction do_copy() {
22986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray  source_file=$1
23757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  dest_name=$2
249b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light  # Move to a temporary file so we can do a rename and have the preopted file
259b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light  # appear atomically in the filesystem.
269b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light  temp_dest_name=${dest_name}.tmp
27986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray  if ! cp ${source_file} ${temp_dest_name} ; then
28986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray    log -p w -t cppreopts "Unable to copy file ${source_file} to ${temp_dest_name}!"
299b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light  else
30986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray    log -p i -t cppreopts "Copied file from ${source_file} to ${temp_dest_name}"
319b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light    sync
329b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light    if ! mv ${temp_dest_name} ${dest_name} ; then
33986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray      log -p w -t cppreopts "Unable to rename temporary file from ${temp_dest_name} to ${dest_name}"
340b2d37205d738bf702e839b2b21d582c50cffaa2Alex Light      rm ${temp_dest_name} || log -p w -t cppreopts "Unable to remove temporary file ${temp_dest_name}"
359b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light    else
36986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray      log -p i -t cppreopts "Renamed temporary file from ${temp_dest_name} to ${dest_name}"
379b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light    fi
389b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light  fi
39757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang}
40757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang
41757d341496d8c7e69420eb51c82be2d2bc694079Wei Wangif [ $# -eq 1 ]; then
42757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  # Where the system_b is mounted that contains the preopt'd files
43757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  mountpoint=$1
44757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang
45757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  if ! test -f ${mountpoint}/system-other-odex-marker ; then
46986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray    log -p i -t cppreopts "system_other partition does not appear to have been built to contain preopted files."
47757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang    exit 1
48757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  fi
499b71cad2381cbd67d7e3a9ca2af357ae6998be83Alex Light
50757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  log -p i -t cppreopts "cppreopts from ${mountpoint}"
51986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray  # For each odex and vdex file do the copy task
52757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  # NOTE: this implementation will break in any path with spaces to favor
53757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  # background copy tasks
54496f0213f35f681f90d503f8d45e5f29a3139b4fMathieu Chartier  for file in $(find ${mountpoint} -type f -name "*.odex" -o -type f -name "*.vdex" -o -type f -name "*.art"); do
55986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray    real_name=${file/${mountpoint}/\/system}
56986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray    dest_name=$(preopt2cachename ${real_name})
57757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang    if ! test $? -eq 0 ; then
58986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray      log -p i -t cppreopts "Unable to figure out destination for ${file}"
59757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang      continue
60757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang    fi
61757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang    # Copy files in background to speed things up
62986ae62325751fcd74e6efc825a4f09470b94aacNicolas Geoffray    do_copy ${file} ${dest_name} &
63757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  done
64757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  # Wait for jobs to finish
65757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  wait
66757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  exit 0
67757d341496d8c7e69420eb51c82be2d2bc694079Wei Wangelse
68757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  log -p e -t cppreopts "Usage: cppreopts <preopts-mount-point>"
69757d341496d8c7e69420eb51c82be2d2bc694079Wei Wang  exit 1
70757d341496d8c7e69420eb51c82be2d2bc694079Wei Wangfi
71