Skip to content

Commit 11cb73a

Browse files
Merge pull request #28 from RandomCoderOrg/revamp-v2.5-integrity-check
Revamp v2.5 integrity check
2 parents 9535733 + 1225e58 commit 11cb73a

File tree

2 files changed

+130
-9
lines changed

2 files changed

+130
-9
lines changed

udroid/src/proot-utils/proot-utils.sh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#!/bin/bash
22

33
[[ -z $TMPDIR ]] && TMPDIR=/tmp
4-
ERROR_DUMP_FILE="$TMPDIR/proot-utils.log"
4+
LOG_FILE="$TMPDIR/udroid.log"
5+
[[ ! -f $LOG_FILE ]] && touch $LOG_FILE
56

67
msg() { echo -e "${*} \e[0m" >&2;:;}
7-
ELOG() { echo "[$(date +%F) | $(date +%R)] Error: ${*}" >> "${ERROR_DUMP_FILE}";:;}
8-
LOG() { echo "[$(date +%F) | $(date +%R)] MSG:${*}" >> "${ERROR_DUMP_FILE}";:;}
8+
ELOG() { echo "[$(date +%F) | $(date +%R)] Error: ${*}" >> "${LOG_FILE}";:;}
9+
LOG() { echo "[$(date +%F) | $(date +%R)] MSG:${*}" >> "${LOG_FILE}";:;}
10+
11+
manage_log_size() {
12+
log_size=$(du -k $LOG_FILE | awk '{print $1}')
13+
[[ $log_size -gt 1024 ]] && rm -f $LOG_FILE
14+
}
915

1016
p_extract() {
1117
# OPTIONS:
@@ -35,7 +41,7 @@ p_extract() {
3541
if ! $NO_PROG; then
3642
pv $file | proot \
3743
--link2symlink \
38-
tar --no-same-owner -xvz -C "$path" &> $ERROR_DUMP_FILE
44+
tar --no-same-owner -xvz -C "$path" &> $LOG_FILE
3945
else
4046
proot \
4147
--link2symlink \
@@ -127,3 +133,6 @@ if [ -n "$RUN_STANDALONE" ]; then
127133
esac
128134
done
129135
fi
136+
137+
# Manage log size
138+
manage_log_size

udroid/src/udroid.sh

Lines changed: 117 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export distro_data
2222

2323
DIE() { echo -e "${@}"; exit 1 ;:;}
2424
GWARN() { echo -e "\e[90m${*}\e[0m";:;}
25+
WARN() { echo -e "[WARN]: ${*}\e[0m";:;}
2526
INFO() { echo -e "\e[32m${*}\e[0m";:;}
2627
TITLE() { echo -e "\e[100m${*}\e[0m";:;}
2728

@@ -73,6 +74,56 @@ fetch_distro_data() {
7374
fi
7475
}
7576

77+
## ask() - prompt the user with a message and wait for a Y/N answer
78+
#
79+
# This function will prompt the user with a message and wait for a Y/N answer.
80+
# It will return 0 if the answer is Y, y, yes or empty. It will return 1 if the
81+
# answer is N, n, no. If the answer is anything else, it will return 1.
82+
#
83+
# Usage:
84+
# ask "Do you want to continue?"
85+
#
86+
# Returns:
87+
# 0 if the answer is Y, y, yes or empty. It will return 1 if the
88+
# answer is N, n, no. If the answer is anything else, it will return 1.
89+
ask() {
90+
local msg=$*
91+
92+
echo -ne "$msg\t[Y/N]: "
93+
read -r choice
94+
95+
case $choice in
96+
y|Y|yes) return 0;;
97+
n|N|No) return 1;;
98+
"") return 0;;
99+
*) return 1;;
100+
esac
101+
}
102+
103+
# This function checks the integrity of a file.
104+
#
105+
# This function takes the filename and the expected SHA256 sum of the file as parameters.
106+
# It calculates the SHA256 sum of the file and compares it to the expected SHA256 sum.
107+
# If the calculated SHA256 sum is the same as the expected SHA256 sum, the file is considered to be intact.
108+
# If the calculated SHA256 sum is not the same as the expected SHA256 sum, the file is considered to be corrupt.
109+
# This function returns 0 if the file is intact, 1 if the file is corrupt.
110+
verify_integrity() {
111+
local filename=$1
112+
local shasum=$2
113+
114+
filesha=$(sha256sum $filename | cut -d " " -f 1)
115+
LOG "filesum=$filesha"
116+
LOG "shasum=$shasum"
117+
118+
if [[ "$filesha" != "$shasum" ]]; then
119+
LOG "file integrity check failed"
120+
return 1
121+
else
122+
LOG "file integrity check passed"
123+
return 0
124+
fi
125+
}
126+
76127
install() {
77128
###
78129
# install()
@@ -85,11 +136,39 @@ install() {
85136
# 4) Extract the filesystem to target path
86137
# 5) execute fixes file
87138

88-
local arg=$1
139+
# local arg=$1
89140
TITLE "> INSTALL $arg"
90-
# parse the arg for suite and varient and get name,link
91-
parser $1 "online"
141+
local no_check_integrity=false
142+
143+
while [[ $# -gt 0 ]]; do
144+
case $1 in
145+
--no-check-integrity)
146+
no_check_integrity=true
147+
shift
148+
;;
149+
*)
150+
# [[ -n $_name ]] && {
151+
# ELOG "login() error: name already set to $_name"
152+
# echo "--name supplied $_name"
153+
# }
154+
155+
if [[ -z $arg ]]; then
156+
arg=$1
157+
else
158+
ELOG "unkown option $1"
159+
fi
160+
shift
161+
break
162+
;;
163+
esac
164+
done
92165

166+
[[ -z $arg ]] && {
167+
ELOG "\$arg not supplied"
168+
}
169+
# parse the arg for suite and varient and get name,link
170+
parser $arg "online"
171+
93172
# final checks
94173
[[ "$link" == "null" ]] && {
95174
ELOG "link not found for $suite:$varient"
@@ -122,6 +201,35 @@ install() {
122201
# create $name directory
123202
mkdir -p $DEFAULT_FS_INSTALL_DIR/$name
124203

204+
# verify integrity
205+
if verify_integrity "$DLCACHE/$name.tar.$ext" "$shasum"; then
206+
LOG "file integrity check passed"
207+
else
208+
WARN "file integrity check failed"
209+
if $no_check_integrity; then
210+
INFO "skipped integrity check .."
211+
GWARN "skipping integrity check"
212+
LOG "skipping integrity check for \"$DLCACHE/$name.tar.$ext\""
213+
else
214+
if ask "Do you want to retry [ deleteing the file and re-download it? ]"; then
215+
rm "$DLCACHE/$name.tar.$ext"
216+
download "$name.tar.$ext" "$link"
217+
218+
# recheck integrity after download
219+
if verify_integrity "$DLCACHE/$name.tar.$ext" "$shasum"; then
220+
LOG "file integrity check passed"
221+
else
222+
# exit condition
223+
GWARN "file integrity check failed"
224+
DIE "Exiting gracefully.."
225+
fi
226+
else
227+
# final exit condition
228+
DIE "Integrity check failed. Exiting gracefully.."
229+
fi
230+
fi
231+
fi
232+
125233
# call proot extract
126234
msg_extract "$DEFAULT_FS_INSTALL_DIR/$name"
127235
p_extract --file "$DLCACHE/$name.tar.$ext" --path "$DEFAULT_FS_INSTALL_DIR/$name"
@@ -578,6 +686,8 @@ parser() {
578686
LOG "link=$link"
579687
name=$(cat $distro_data | jq -r ".$suite.$varient.Name")
580688
LOG "name=$name"
689+
shasum=$(cat $distro_data | jq -r ".$suite.$varient.${arch}sha")
690+
LOG "shasum=$shasum"
581691
}
582692

583693
list() {
@@ -716,13 +826,15 @@ remove() {
716826
}
717827

718828
update() {
719-
TITLE "> UPDATE distro data from remove"
829+
TITLE "> UPDATE distro data from remote"
720830
fetch_distro_data "online" true
721831
}
722832

723833
_reset() {
724834
# TODO
725835
TITLE "[TODO]"
836+
837+
726838
}
727839
####################
728840
download() {
@@ -738,7 +850,7 @@ download() {
738850
LOG "download(): $name already exists in $path"
739851
GWARN "$name already exists, continuing with existing file"
740852
else
741-
wget -q --show-progress --progress=bar:force -O ${path}/$name "$link" 2>&1 || {
853+
wget -q --tries=10 --show-progress --progress=bar:force -O ${path}/$name "$link" 2>&1 || {
742854
ELOG "failed to download $name"
743855
echo "failed to download $name"
744856
exit 1

0 commit comments

Comments
 (0)