@@ -22,6 +22,7 @@ export distro_data
2222
2323DIE () { echo -e " ${@ } " ; exit 1 ; : ; }
2424GWARN () { echo -e " \e[90m${* } \e[0m" ; : ; }
25+ WARN () { echo -e " [WARN]: ${* } \e[0m" ; : ; }
2526INFO () { echo -e " \e[32m${* } \e[0m" ; : ; }
2627TITLE () { 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+
76127install () {
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
583693list () {
@@ -716,13 +826,15 @@ remove() {
716826}
717827
718828update () {
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# ###################
728840download () {
@@ -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