Home | History | Annotate | Download | only in scripts
      1 #!/bin/bash
      2 #
      3 # Copyright (C) 2019 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 set -e
     18 
     19 if [[ $# -le 1 ]]; then
     20   cat <<EOF
     21 Usage:
     22   package-check.sh <jar-file> <package-list>
     23 Checks that the class files in the <jar file> are in the <package-list> or
     24 sub-packages.
     25 EOF
     26   exit 1
     27 fi
     28 
     29 jar_file=$1
     30 shift
     31 if [[ ! -f ${jar_file} ]]; then
     32   echo "jar file \"${jar_file}\" does not exist."
     33   exit 1
     34 fi
     35 
     36 prefixes=()
     37 while [[ $# -ge 1 ]]; do
     38   package="$1"
     39   if [[ "${package}" = */* ]]; then
     40     echo "Invalid package \"${package}\". Use dot notation for packages."
     41     exit 1
     42   fi
     43   # Transform to a slash-separated path and add a trailing slash to enforce
     44   # package name boundary.
     45   prefixes+=("${package//\./\/}/")
     46   shift
     47 done
     48 
     49 # Get the file names from the jar file.
     50 zip_contents=`zipinfo -1 $jar_file`
     51 
     52 # Check all class file names against the expected prefixes.
     53 old_ifs=${IFS}
     54 IFS=$'\n'
     55 for zip_entry in ${zip_contents}; do
     56   # Check the suffix.
     57   if [[ "${zip_entry}" = *.class ]]; then
     58     # Match against prefixes.
     59     found=false
     60     for prefix in ${prefixes[@]}; do
     61       if [[ "${zip_entry}" = "${prefix}"* ]]; then
     62         found=true
     63         break
     64       fi
     65     done
     66     if [[ "${found}" == "false" ]]; then
     67       echo "Class file ${zip_entry} is outside specified packages."
     68       exit 1
     69     fi
     70   fi
     71 done
     72 IFS=${old_ifs}
     73