Files
vendor_gapps/build/meta/com/google/android/update-binary
Alessandro Astone 1d1a30c107 Improve free space checks
And don't count as needed space that will be freed from the replaced
files. This will allow to flash the zip over an existing installation
when space is tight.

Also:
awk      -->  tr | cut  because recovery might not provide awk
grep -v  -->  tail -1   in du because it might spit out 3 lines
2021-12-08 18:20:20 +01:00

182 lines
4.1 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
ui_print "Unmounting partitions"
umount -l "$SYSTEM_MNT"
}
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 " $1 " | tail -n1 | tr -s ' ' | 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 "Mounting partitions"
# Ensure system is unmounted so mounting succeeds
umount /system || umount /mnt/system || 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")
# 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"
# Compute storage requirements
SYSTEM_STORAGE=`df $SYSTEM_MNT | tail -1 | tr -s ' ' | cut -d ' ' -f4`
STORAGE_BUFFER=10240
ui_print "Extracting files"
cd /tmp
unzip -o "$ZIP"
rm -rf META-INF
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 . -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}/priv-app/Provision"
fi
cleanup
ui_print "Done!"
exit 0