• 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'
55failed=false
56for zip_entry in ${zip_contents}; do
57  # Check the suffix.
58  if [[ "${zip_entry}" = *.class ]]; then
59    # Match against prefixes.
60    found=false
61    for prefix in ${prefixes[@]}; do
62      if [[ "${zip_entry}" = "${prefix}"* ]]; then
63        found=true
64        break
65      fi
66    done
67    if [[ "${found}" == "false" ]]; then
68      echo "Class file ${zip_entry} is outside specified packages."
69      failed=true
70    fi
71  fi
72done
73if [[ "${failed}" == "true" ]]; then
74  exit 1
75fi
76IFS=${old_ifs}
77