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