Skip to content

Commit 7624a01

Browse files
check filesystem integrity on download
1 parent b6de816 commit 7624a01

File tree

1 file changed

+102
-4
lines changed

1 file changed

+102
-4
lines changed

udroid/src/udroid.sh

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,57 @@ fetch_distro_data() {
7373
fi
7474
}
7575

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

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

93168
# final checks
94169
[[ "$link" == "null" ]] && {
@@ -122,6 +197,29 @@ install() {
122197
# create $name directory
123198
mkdir -p $DEFAULT_FS_INSTALL_DIR/$name
124199

200+
# verify integrity
201+
if verify_integrity "$DLCACHE/$name.tar.$ext" "$shasum"; then
202+
LOG "file integrity check passed"
203+
else
204+
GWARN "file integrity check failed"
205+
if $no_check_integrity; then
206+
GWARN "skipping integrity check"
207+
else
208+
if ask "Do you want to retry [ deleteing the file and re-download it? ]"; then
209+
rm "$DLCACHE/$name.tar.$ext"
210+
download "$name.tar.$ext" "$link"
211+
if verify_integrity "$DLCACHE/$name.tar.$ext" "$shasum"; then
212+
LOG "file integrity check passed"
213+
else
214+
GWARN "file integrity check failed"
215+
DIE "Exiting gracefully.."
216+
fi
217+
else
218+
DIE "Integrity check failed. Exiting gracefully.."
219+
fi
220+
fi
221+
fi
222+
125223
# call proot extract
126224
msg_extract "$DEFAULT_FS_INSTALL_DIR/$name"
127225
p_extract --file "$DLCACHE/$name.tar.$ext" --path "$DEFAULT_FS_INSTALL_DIR/$name"

0 commit comments

Comments
 (0)