• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Copyright (C) 2021 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
17readme() {
18  echo '''
19  check permissions xml from a apk bundle release aginast those on a device
20  ./batch_check_permission.sh ~/Downloads/apk_bundle_dir ~/Downloads/override.csv
21'''
22}
23
24SECONDS=0
25MY_NAME=$0
26SCRIPT_NAME="${MY_NAME##*/}"
27SCRIPT_DIR="${MY_NAME%/$SCRIPT_NAME}"
28echo Running from $SCRIPT_DIR
29
30if [[ -z $OUT_DIR ]]; then
31  OUT_DIR="${HOME}/Downloads"
32fi
33
34INPUT_DIR=$1
35if [[ -z "${INPUT_DIR}" ]]; then
36  readme
37  exit
38fi
39
40RENAME_CSV=$2
41# Read rename csv to create xmlRenameDic
42declare -A xmlRenameDic
43if [[ -f ${RENAME_CSV} ]]; then
44  while IFS=',' read -r name newName others || [ -n "${name}" ]; do
45    if [[ "${name}" == "name" ]]; then
46      # skip header
47      header="${name},${newName}"
48    else
49      xmlRenameDic["${name}"]="${newName}"
50    fi
51  done < $RENAME_CSV
52fi
53
54echo "LOG=${LOG}"
55log() {
56  if [[ -n ${LOG} ]]; then
57    echo $1
58  fi
59}
60
61echo "Listing xmls in ${INPUT_DIR}"
62declare -A relXmlDic
63
64declare -A relXmlDic="$(${SCRIPT_DIR}/get_file_dir.sh ${INPUT_DIR} xml)"
65echo "Found: ${#relXmlDic[@]} xmls"
66
67echo "Listing xmls in the device"
68declare -A deviceXmlDic
69deviceXmlList=$(adb shell "find / -name *.xml" 2>/dev/null)
70for xml in ${deviceXmlList}; do
71  file=${xml##*/}
72  fPath=${xml%/*}
73  fParentPathPostfix=${fPath:(-11)}
74  if [[ "permissions" == ${fParentPathPostfix} ]]; then
75    deviceXmlDic[${file}]=${xml}
76    log "${file} ${fPath} ${fParentPathPostfix}"
77  fi
78done
79echo "Found: ${#deviceXmlDic[@]} xmls"
80
81echo "Comparing xmls from ${INPUT_DIR} to those on the device."
82i=1
83for xml in "${!relXmlDic[@]}"; do
84  # relFile="...google/etc/permissions/privapp-permissions-car.xml"
85  relFile=${relXmlDic[$xml]}
86  # fPath="...google/etc/permissions"
87  fPath=${relFile%/*}
88  # fParentPathPostfix="permissions"
89  fParentPathPostfix=${fPath:(-11)}
90  log "${xml} ${fPath} ${fParentPathPostfix}"
91
92  # Only care about permissions
93  if [[ "permissions" == ${fParentPathPostfix} ]]; then
94    echo "$i Comparing permission file: $xml"
95
96    deviceFile=${deviceXmlDic[$xml]}
97    if [[ -z ${deviceFile} ]]; then
98      # Maybe it's renamed
99      newXml=${xmlRenameDic[$xml]}
100      log "Rename $xml to $newXml"
101      deviceFile=${deviceXmlDic[$newXml]}
102      if [[ -z ${deviceFile} ]]; then
103        echo "Error: no ${xml} on the device."
104        echo
105        i=$(($i + 1))
106        continue
107      fi
108    fi
109
110    # Pull the xml from device & diff
111    adb pull "${deviceFile}" "${OUT_DIR}/${xml}"
112    diff "${relXmlDic[$xml]}" "${OUT_DIR}/${xml}"
113    i=$(($i + 1))
114    echo
115  fi
116done
117echo "Took ${SECONDS} seconds"
118