Files
vendor_gapps/build/meta/com/google/android/update-binary
2022-02-01 18:33:25 +01:00

211 lines
4.8 KiB
Bash

#!/sbin/sh
OUTFD="/proc/self/fd/$2"
ZIP=$3
set_con() {
chcon -h u:object_r:"$1":s0 $2
chcon u:object_r:"$1":s0 $2
}
set_perm() {
chmod $1 $2
}
set_owner() {
chown $1:$2 $3
}
ui_print() {
echo "ui_print $1" > "$OUTFD";
echo "ui_print" > "$OUTFD";
}
cleanup() {
ui_print "Cleaning up files"
cd ../
rm -rf system
rm -rf bin
rm toybox
ui_print "Unmounting partitions"
umount -l "$SYSTEM_MNT"
umount -l /product || true
}
error_no_space() {
ui_print "Not enough space for GApps! Aborting"
cleanup
exit 1
}
get_block_for_mount_point() {
grep -v "^#" /etc/recovery.fstab | grep "[[:blank:]]$1[[:blank:]]" | tail -n1 | tr -s [:blank:] ' ' | cut -d' ' -f1
}
find_block() {
local name="$1"
local fstab_entry=$(get_block_for_mount_point "/$name")
# P-SAR hacks
[ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/")
[ -z "$fstab_entry" ] && [ "$name" = "system" ] && fstab_entry=$(get_block_for_mount_point "/system_root")
local dev
if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
if [ -n "$fstab_entry" ]; then
dev="${BLK_PATH}/${fstab_entry}${SLOT_SUFFIX}"
else
dev="${BLK_PATH}/${name}${SLOT_SUFFIX}"
fi
else
if [ -n "$fstab_entry" ]; then
dev="${fstab_entry}${SLOT_SUFFIX}"
else
dev="${BLK_PATH}/${name}${SLOT_SUFFIX}"
fi
fi
if [ -b "$dev" ]; then
echo "$dev"
fi
}
compute_apps_size() {
NEEDED_STORAGE_SYSTEM=$(expr $(du -cs `find -maxdepth 1 -mindepth 1` | tail -n1 | cut -f1) + $STORAGE_BUFFER)
RECLAIMABLE_STORAGE_SYSTEM=$(find . -type f | sed "s|^./|$SYSTEM_OUT/|" | xargs ls -d 2>/dev/null | xargs du -cs PLACEHOLDER 2>/dev/null | tail -n1 | cut -f1)
NEEDED_STORAGE_SYSTEM=$(expr $NEEDED_STORAGE_SYSTEM - $RECLAIMABLE_STORAGE_SYSTEM)
}
remove_big_optional_apps() {
ui_print "Low resource device detected, removing large extras"
rm -rf app/MarkupGoogle
rm -rf priv-app/AndroidMigratePrebuilt
rm -rf priv-app/SetupWizardPrebuilt
rm -rf priv-app/Velvet
}
ui_print "**********************"
ui_print "MindTheGapps installer"
ui_print "**********************"
ui_print "Extracting files"
TMP=/tmp
cd "$TMP"
unzip -o "$ZIP"
rm -rf META-INF
ui_print "Setting up environment"
TOYBOX="${TMP}/toybox"
chmod +x "$TOYBOX"
TOYS_PATH="${TMP}/bin"
rm -rf "$TOYS_PATH";
$TOYBOX install -d "$TOYS_PATH"
for toy in $($TOYBOX); do
$TOYBOX ln -sf "$TOYBOX" "${TOYS_PATH}/${toy}"
done
PATH="${TOYS_PATH}:${PATH}"
ui_print "Mounting partitions"
# Ensure system is unmounted so mounting succeeds
umount /system || umount /mnt/system || true
umount /product || true
# Find partitions
DYNAMIC_PARTITIONS=`getprop ro.boot.dynamic_partitions`
if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
BLK_PATH="/dev/block/mapper"
else
BLK_PATH=/dev/block/bootdevice/by-name
fi
CURRENTSLOT=`getprop ro.boot.slot_suffix`
if [ ! -z "$CURRENTSLOT" ]; then
if [ "$CURRENTSLOT" == "_a" ]; then
SLOT_SUFFIX="_a"
else
SLOT_SUFFIX="_b"
fi
fi
SYSTEM_BLOCK=$(find_block "system")
PRODUCT_BLOCK=$(find_block "product")
# Disable rw protection on dynamic partitions
if [ "$DYNAMIC_PARTITIONS" = "true" ]; then
blockdev --setrw "$SYSTEM_BLOCK"
fi
# Mount and define SYSTEM_OUT
SYSTEM_MNT=/mnt/system
mkdir -p "$SYSTEM_MNT" || true
if mount -o rw "$SYSTEM_BLOCK" "$SYSTEM_MNT"; then
ui_print "$SYSTEM_MNT mounted"
else
ui_print "Could not mount $SYSTEM_MNT! Aborting"
exit 1
fi
SYSTEM_OUT="${SYSTEM_MNT}/system"
if [ -n "$PRODUCT_BLOCK" ]; then
mkdir /product || true
if mount -o rw "$PRODUCT_BLOCK" /product; then
ui_print "/product mounted"
else
ui_print "Could not mount /product"
exit 1
fi
fi
# Compute storage requirements
SYSTEM_STORAGE=`df $SYSTEM_MNT | tail -1 | tr -s ' ' | cut -d ' ' -f4`
STORAGE_BUFFER=10240
cd system
compute_apps_size
if [ "$SYSTEM_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM" ]; then
remove_big_optional_apps
compute_apps_size
if [ "$SYSTEM_STORAGE" -lt "$NEEDED_STORAGE_SYSTEM" ]; then
error_no_space
fi
fi
ui_print "Generating addon.d file"
cat addon.d/addond_head > addon.d/30-gapps.sh
for f in `find . ! -path "./addon.d/*" -type f`; do
line=$(echo "$f" | sed 's/\.\///')
echo "$line" >> addon.d/30-gapps.sh
done
cat addon.d/addond_tail >> addon.d/30-gapps.sh
rm addon.d/addond_head addon.d/addond_tail
ui_print "Preparing files for copying"
for d in `find . -mindepth 1 -type d -type d`; do
set_perm 0755 $d
set_owner root root $d
done
for f in `find . -type f`; do
type=$(echo "$f" | sed 's/.*\.//')
if [ "$type" == "sh" ] || [ "$type" == "$f" ]; then
set_perm 0755 $f
else
set_perm 0644 $f
fi
set_owner root root $f
set_con system_file $f
done
ui_print "Copying files"
cp --preserve=a -r ./* "${SYSTEM_OUT}/"
if [ -e priv-app/SetupWizardPrebuilt ] ; then
rm -rf "${SYSTEM_OUT}/product/priv-app/Provision"
fi
cleanup
ui_print "Done!"
exit 0