• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
17set -e
18
19if [[ $# -le 1 ]]; then
20  cat <<EOF
21Usage:
22  package-check.sh <jar-file> <package-list>
23Checks that the class files in the <jar file> are in the <package-list> or
24sub-packages.
25EOF
26  exit 1
27fi
28
29jar_file=$1
30shift
31if [[ ! -f ${jar_file} ]]; then
32  echo "jar file \"${jar_file}\" does not exist."
33  exit 1
34fi
35
36prefixes=()
37while [[ $# -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
47done
48
49# Get the file names from the jar file.
50zip_contents=`zipinfo -1 $jar_file`
51
52# Check all class file names against the expected prefixes.
53old_ifs=${IFS}
54IFS=$'\n'
55for 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
71done
72IFS=${old_ifs}
73