Skip to content

Commit

Permalink
download_file rewritten
Browse files Browse the repository at this point in the history
  • Loading branch information
bkauler committed Dec 4, 2023
1 parent de04e37 commit 3144062
Showing 1 changed file with 108 additions and 165 deletions.
273 changes: 108 additions & 165 deletions easyos/easy-code/rootfs-skeleton/usr/bin/download_file
@@ -1,50 +1,21 @@
#!/bin/sh
#(c) Copyright Barry Kauler 2010. License GPL v3 /usr/share/doc/legal.
#BK a simple frontend for wget.
#called from /usr/local/petget/petget, downloadpkgs.sh.
#URL passed in, only downloads to current directory.
#only put up user-response dlg windows if failure.
#101116 first version.
#101117 url sanity check. more download size check.
#101118 for ftp connection have to get size differently.
#101126 not sure, it seems that wget now defaults to ipv6, fails for some users, use '-4' to force ipv4.
#101206 fix find filesize ftp://ftp.lug.udel.edu/pub/puppylinux ...
#120201 rodin.s: internationalized. Add LANG=C to 1-st wget for proper work
#120218 removed a LANG=C line (line 43).
#120220 jemimah: Technosaurus' wget progressbar function to the download_file utility. It also replaces an rxvt window with an Xdialog tailbox. refer: http://murga-linux.com/puppy/viewtopic.php?t=71767&start=420
#120224 small tweak.
#120908 revert 120220.
#121019 extra msg if missing-url, when called from ppm (refer /usr/local/petget/downloadpkgs.sh).
#140106 use urxvt instead of rxvt, in case 'misc' dir missing. see also snapshot-manager.
#150712 added an abort button (in its own window), in case download hangs. 150808 fix.
#170521 wget no longer logs to stderr, but to ~/wget-log*.
#170521 oe has wget 1.19.1, rolled back to 1.17.1, so don't need to "fix" this script.
#20201122 different font in terminal, need to move abort-button down a bit.
#20220614 change text "Puppy" to "Easy"

# adding gettext
export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=download_file
eval_gettext () {
local myMESSAGE=$(gettext "$1")
eval echo \"$myMESSAGE\"
}
LANG="`cat /etc/profile|grep '^LANG='`"
eval $LANG
export LANG_USER=$LANG
#-----------------------------#
#!/bin/ash
#20231204 completely rewritten.

#exit number: 0 ok, 1 unable to download, 2 download fail, 3 invalid url.

#export LANG=C #keep Xdialog happy.
LANG=$LANG_USER # i18n
export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=download_file
xLANG="`cat /etc/profile|grep '^LANG='`"
eval $xLANG
URLSPEC="$@"
FILENAME="`basename "$@"`"
URLDIR="`dirname "$@"`" #121019
. /etc/rc.d/PUPSTATE
FILENAME="${URLSPEC##*/}"
URLDIR="${URLSPEC%/*}"
export TEXTDOMAINDIR TEXTDOMAIN LANG URLSPEC FILENAME URLDIR

#121019 ***TODO***
#note: in future maybe make this into a gtkdialog window, with a button to launch web browser...
mkdir -p /tmp/efd
echo -n '0' > /tmp/efd/exit.err

#121019 leaving this for now...
#this is a quick hack. in future, modify download-file so that only a missing-file runs "exit 1", then
#put up this info in downloadpkgs.sh. then no need for exported DL_F_CALLED_FROM.
PPM_NO_URL_MSG=""
Expand All @@ -54,50 +25,52 @@ if [ "$DL_F_CALLED_FROM" = "ppm" ];then #see /usr/local/petget/downloadpkgs.sh
$(gettext "NOTE:")
$(gettext "It may be that the local package database needs to be updated. In some cases, the packages in the online package repository change, so you may be trying to download a package that no longer exists. One way to check this is to go to the URL with your web browser:")
${URLDIR}
$(gettext "If the URL is working and the repository has a later package, cancel the current operation. In the PKGget Package Manager main window, click the 'Configure' button, then the 'Update now' button -- this will update the local database. Be warned though, for some large repositories this process may take awhile.")"
$(gettext "If the URL is working and the repository has a later package, cancel the current operation. In the PKGget Package Manager main window, click the 'Configure' button, then the 'Update now' button -- this will update the local database.")"
fi
export PPM_NO_URL_MSG


#note, can use wildcard to test if file exists, if need to look for alternate versions:
# wget -4 -t 2 -T 20 --waitretry=20 --spider -S --recursive --no-parent --no-directories -A 'avast-*.pet' 'http://distro.ibiblio.org/quirky/pet_packages-common/'
echo -n '' > /tmp/efd/efd-exec
cat << 'EOF1' >> /tmp/efd/efd-exec
#!/bin/bash
CR='
'
# ref: https://unix.stackexchange.com/questions/701466/how-can-i-check-if-the-first-command-on-a-pipe-gets-an-error
set -o pipefail
#test url... -t 2 means try 2 times, -T 20 means timeout in 20 seconds
while [ 1 ];do
echo -n "" > /tmp/download_file_spider.log1
#gtklogfileviewer "/tmp/download_file_spider.log" "Close" 0 0 600 200 "Testing remote file exists:" &
#150712 make it borderless, so there is no close-box at top-left...
urxvt -borderLess -geometry 130x10+0+0 -bg orange -fg black -title "$(gettext 'Testing file exists:') ${URLSPEC}" -e tail -f /tmp/download_file_spider.log1 & #120908 restored.
#Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Testing file exists:') ${URLSPEC}" --tailbox /tmp/download_file_spider.log1 30 100 & #120220
PID1=$!
LANG=C wget -4 -t 2 -T 20 --waitretry=20 --spider -S "${URLSPEC}" > /tmp/download_file_spider.log1 2>&1 #170521
#LANG=C wget -4 -t 2 -T 20 --waitretry=20 --spider -S "${URLSPEC}" -o /tmp/download_file_spider.log1 #170521
echo -en '\033[1;35m' #magenta
echo "$(gettext 'Testing file exists:')"
echo -n " ${URLSPEC}"
echo -e '\033[0;39m' #reset.
#test url... -t 2 means try 2 times, -T 20 means timeout in 20 seconds
LANG=C wget -4 -t 2 -T 20 --waitretry=20 --spider -S "${URLSPEC}" 2>&1 | tee /tmp/efd/wget.log1
if [ $? -ne 0 ];then
Xdialog --wmclass "gtkdialog2" --left --wrap --title "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --ok-label "$(gettext 'TEST AGAIN')" --cancel-label "$(gettext 'GIVE UP')" --icon "/usr/local/lib/X11/pixmaps/error.xpm" --yesno "$(gettext 'Tested if remote file exists, but failed, see log top-left of screen.
Do you want to test again, or give up?')${PPM_NO_URL_MSG}" 0 0
REPLY1=$?
kill $PID1
[ $REPLY1 -eq 0 ] && continue
Xdialog --wmclass "splashwindow" --left --wrap --backtitle "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --ok-label "$(gettext 'TEST AGAIN')" --cancel-label "$(gettext 'GIVE UP')" --icon "/usr/local/lib/X11/pixmaps/error.xpm" --yesno "$(gettext 'Tested if remote file exists, but failed, see log top-left of screen.')${CR}$(gettext 'Do you want to test again, or give up?')${PPM_NO_URL_MSG}" 0 0
[ $? -eq 0 ] && continue
echo -n '1' > /tmp/efd/exit.err
exit 1
fi
kill $PID1
break
done
#LANG=C # i18n
#find out size of online file...
SIZEB_ONLINE=`grep '^Length: ' /tmp/download_file_spider.log1 | tr -s ' ' | cut -f 2 -d ' ' | sed -e 's%[^0-9]%%g'`
#101118 for ftp connection have to get size differently...
[ "$SIZEB_ONLINE" = "" ] && SIZEB_ONLINE=`grep "${FILENAME}" /tmp/download_file_spider.log1 | tr '\t' ' ' | tr -s ' ' | grep ' ftp ' | cut -f 5 -d ' ' | sed -e 's%[^0-9]%%g'`
#101206 ftp://ftp.lug.udel.edu/pub/puppylinux/pet_packages-3/gpm-1.20.1-1.pet ...
[ "$SIZEB_ONLINE" = "" ] && SIZEB_ONLINE=`grep "${FILENAME}" /tmp/download_file_spider.log1 | tr '\t' ' ' | tr -s ' ' | grep ' root ' | cut -f 5 -d ' ' | sed -e 's%[^0-9]%%g'`
[ "$SIZEB_ONLINE" = "" ] && SIZEB_ONLINE=`grep "${FILENAME}" /tmp/download_file_spider.log1 | tr '\t' ' ' | tr -s ' ' | grep ' 2[0-9][0-9][0-9] ' | cut -f 5 -d ' ' | sed -e 's%[^0-9]%%g'`
#101117 sanity check...
SIZEB_ONLINE=`grep '^Length: ' /tmp/efd/wget.log1 | tr -s ' ' | cut -f 2 -d ' ' | sed -e 's%[^0-9]%%g'`
#for ftp connection have to get size differently...
[ "$SIZEB_ONLINE" = "" ] && SIZEB_ONLINE=`grep "${FILENAME}" /tmp/efd/wget.log1 | tr '\t' ' ' | tr -s ' ' | grep ' ftp ' | cut -f 5 -d ' ' | sed -e 's%[^0-9]%%g'`
#ftp://ftp.lug.udel.edu/pub/puppylinux/pet_packages-3/gpm-1.20.1-1.pet ...
[ "$SIZEB_ONLINE" = "" ] && SIZEB_ONLINE=`grep "${FILENAME}" /tmp/efd/wget.log1 | tr '\t' ' ' | tr -s ' ' | grep ' root ' | cut -f 5 -d ' ' | sed -e 's%[^0-9]%%g'`
[ "$SIZEB_ONLINE" = "" ] && SIZEB_ONLINE=`grep "${FILENAME}" /tmp/efd/wget.log1 | tr '\t' ' ' | tr -s ' ' | grep ' 2[0-9][0-9][0-9] ' | cut -f 5 -d ' ' | sed -e 's%[^0-9]%%g'`
#sanity check...
URL_BROKEN='0'
if [ "$SIZEB_ONLINE" = "" ];then
MSG_NO1="$(gettext 'Oh dear, unable to obtain size of remote file:')
${URLSPEC}
$(gettext 'although it does seem to exist.')"
URL_BROKEN='1'
else
case $SIZEB_ONLINE in
case "$SIZEB_ONLINE" in
unspecified)
MSG_NO1="$(gettext 'Oh dear, this URL seems to be wrong:')
${URLSPEC}
Expand All @@ -116,150 +89,120 @@ on it.') "
esac
fi
if [ "$URL_BROKEN" != "0" ];then
Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --icon "/usr/local/lib/X11/pixmaps/error.xpm" --msgbox "${MSG_NO1}
$(gettext 'Click button to abort download...')" 0 0
Xdialog --wmclass "splashwindow" --backtitle "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --icon "/usr/local/lib/X11/pixmaps/error.xpm" --msgbox "${MSG_NO1}${CR}${CR}$(gettext 'Click button to abort download...')" 0 0
echo -n '3' > /tmp/efd/exit.err
exit 3
fi
#101117 check enough free space...
#FREEK=`df -k | grep ' /$' | tr -s ' ' | cut -f 4 -d ' '`
#no, need to know free space in whatever is current dir...
#check enough free space...
PWD="`pwd`"
FREEblocks=`stat -f --format=%a ${PWD}`
BLOCKsize=`stat -f --format=%S ${PWD}`
FREEbytes=`expr $FREEblocks \* $BLOCKsize`
FREEK=`expr $FREEbytes \/ 1024`
FREEbytes=$(($FREEblocks*$BLOCKsize))
FREEK=$(($FREEbytes/1024))
SIZEK_ONLINE=`expr $SIZEB_ONLINE \/ 1024`
case $PUPMODE in
3|7|13|77) #tmpfs on top
#when install pkg, it writes direct to save-layer.
SIZEK_REQUEST=`expr $SIZEK_ONLINE + 6000` #6MB slack.
;;
*)
SIZEK_REQUEST=`expr $SIZEK_ONLINE \* 3` #3MB slack, no, need install space too.
;;
esac
#LANG=$LANG_USER
SIZEK_ONLINE=$(($SIZEB_ONLINE/1024))
SIZEK_REQUEST=$(($SIZEK_ONLINE*3)) #maybe need extra space if install pkg.
if [ $SIZEK_REQUEST -gt $FREEK ];then
case $PUPMODE in
5)
MSGs="$(gettext 'You need to shutdown and create a save-file, then you will have more space.
See menu Shutdown -> Reboot')"
;;
2|6)
MSGs="$(gettext 'The partition is full, you will have to delete something.')"
;;
3|7|77) #tmpfs on top, full partition underneath
MSGs="$(gettext 'Do not have enough space in the RAM. Maybe you need a swap file
or swap partition (or it needs to be bigger)')"
;;
*)
MSGs="$(gettext 'You need to increase the size of the save-file.
See menu Utility -> Resize personal storage file')"
;;
esac
Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --icon "/usr/local/lib/X11/pixmaps/error.xpm" --msgbox "
`eval_gettext \"Sorry, but there is not enough free space to download to.
Attempted download directory: \\\${PWD}
The online file is \\\${SIZEK_ONLINE}KB, but your free space is \\\${FREEK}KB.
\\\${MSGs}
Click button to abort download...\"`" 0 0
Xdialog --wmclass "splashwindow" --backtitle "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --icon "/usr/local/lib/X11/pixmaps/error.xpm" --msgbox "$(gettext 'Sorry, but there is not enough free space to download to.')${CR}$(gettext 'Attempted download directory:')${CR} ${PWD}${CR}$(gettext 'The online file size is:') ${SIZEK_ONLINE}KB${CR}$(gettext 'However, your free space is:') ${FREEK}KB${CR}${CR}$(gettext 'Click button to abort download...')" 0 0
echo -n '1' > /tmp/efd/exit.err
exit 1
fi
INSERT1="$(gettext 'already ')"

DNflg=0
while [ 1 ];do

CONTPARAM=''
if [ -f $FILENAME ];then
SIZEB_LOCAL=`stat --format=%s "${FILENAME}"`
if [ $SIZEB_LOCAL -lt $SIZEB_ONLINE ];then
Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --ok-label "$(gettext 'CONTINUE')" --cancel-label "$(gettext 'START AGAIN')" --icon "/usr/local/lib/X11/pixmaps/question.xpm" --yesno "`eval_gettext \"File '\\\${FILENAME}' is \\\${INSERT1}partly downloaded (although there is
no guarantee it is uncorrupted), do you want to continue downloading it,
or erase it and download entire file again?\"`" 0 0
if [ $DNflg -eq 0 ];then
DNmsg1="$(gettext 'It is already partly downloaded.')"
else
DNmsg1="$(gettext 'It is partly downloaded.')"
fi
Xdialog --wmclass "splashwindow" --backtitle "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --wrap --ok-label "$(gettext 'CONTINUE')" --cancel-label "$(gettext 'START AGAIN')" --icon "/usr/local/lib/X11/pixmaps/question.xpm" --yesno "$(gettext 'Downloading file:') ${FILENAME}${CR}${DNmsg1}${CR}$(gettext 'Do you want to attempt to continue downloading it, or erase it and download entire file again?')" 0 65
if [ $? -eq 0 ];then
CONTPARAM='-c'
else
rm -f "$FILENAME"
fi
else
if [ $SIZEB_LOCAL -gt $SIZEB_ONLINE ];then #101117
Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --ok-label "$(gettext 'DOWNLOAD AGAIN')" --cancel-label "$(gettext 'NO')" --icon "/usr/local/lib/X11/pixmaps/error.xpm" --yesno "`eval_gettext \"File '\\\${FILENAME}' is \\\${INSERT1}downloaded but is larger
than the online file. Hmmm... this probably means that the online
file is a more recent version, so it is recommended that you erase
the local file and download it again...
Do you want to erase it and download entire file again?\"`" 0 0
if [ $SIZEB_LOCAL -gt $SIZEB_ONLINE ];then
if [ $DNflg -eq 0 ];then
DNmsg2="$(gettext 'This file is already downloaded:')"
else
DNmsg2="$(gettext 'Downloaded file:')"
fi
Xdialog --wmclass "splashwindow" --backtitle "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --wrap --ok-label "$(gettext 'DOWNLOAD AGAIN')" --cancel-label "$(gettext 'NO')" --icon "/usr/local/lib/X11/pixmaps/error.xpm" --yesno "${DNmsg2} ${FILENAME}${CR}$(gettext 'However, it is larger than the online file.')${CR}$(gettext 'Hmmm... this probably means that the online file is a more recent version, so it is recommended that you erase the local file and download it again.')${CR}$(gettext 'Do you want to erase it and download the entire file again?')" 0 65
if [ $? -eq 0 ];then
rm -f "$FILENAME"
else
echo -n '0' > /tmp/efd/exit.err
exit 0
fi
else
Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --ok-label "$(gettext 'DOWNLOAD AGAIN')" --cancel-label "$(gettext 'NO')" --icon "/usr/local/lib/X11/pixmaps/question.xpm" --yesno "`eval_gettext \"File '\\\${FILENAME}' is \\\${INSERT1}fully downloaded (although there is
no guarantee it is uncorrupted), do you want to erase it and download
entire file again?\"`" 0 0
if [ $DNflg -eq 0 ];then
DNmsg3="$(gettext 'It is already fully downloaded.')"
else
DNmsg3="$(gettext 'It is fully downloaded.')"
fi
Xdialog --wmclass "splashwindow" --backtitle "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --wrap --ok-label "$(gettext 'DOWNLOAD AGAIN')" --cancel-label "$(gettext 'NO')" --icon "/usr/local/lib/X11/pixmaps/question.xpm" --yesno "$(gettext 'Downloaded file:') ${FILENAME}${CR}${DNmsg3}${CR}$(gettext 'Do you want to erase it and download the entire file again?')" 0 65
if [ $? -eq 0 ];then
rm -f "$FILENAME"
else
echo -n '0' > /tmp/efd/exit.err
exit 0
fi
fi
fi
fi
#150712 display an abort-button in case download hangs...
echo "export DL_ABORT_DLG='<window decorated=\"false\" skip_taskbar_hint=\"true\">
<hbox>
<text><label>$(gettext 'Click this button if download hangs:')</label></text>
<button><input file>/usr/local/lib/X11/mini-icons/mini-record.xpm</input><action>killall wget</action></button>
</hbox>
</window>'
gtkdialog --geometry=+0+190 --program=DL_ABORT_DLG" > /tmp/df_abort_dl #20201122 up from 180
chmod 766 /tmp/df_abort_dl
/tmp/df_abort_dl &

echo -n "" > /tmp/download_file_spider.log2
#150712 make it borderless, so there is no close-box at top-right...
urxvt -borderLess -geometry 130x10+0+0 -bg orange -fg black -title "$(gettext 'Downloading file:') ${URLSPEC}" -e tail -f /tmp/download_file_spider.log2 & #120908 restored.
##120220 jemimah...
#tail -f /tmp/download_file_spider.log2 | while read LINE; do
# case $LINE in
# *%*)LINE=${LINE##*..};echo ${LINE%%%*};;
# esac
#done |Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy File Downloader')" --screen-center --gauge "$(gettext 'Downloading file:') ${URLSPEC}" 0 0&
PID1=$!
#-t 5 means retry 5 times, -w 5 means wait 5 seconds between retries...
wget -4 ${CONTPARAM} -t 5 -w 5 "${URLSPEC}" > /tmp/download_file_spider.log2 2>&1 #170521
#wget -4 ${CONTPARAM} -t 5 -w 5 "${URLSPEC}" -o /tmp/download_file_spider.log2 #170521
###download file###
echo
echo -en '\033[1;35m' #magenta
echo "$(gettext 'Downloading file:')"
echo -n " ${URLSPEC}"
echo -e '\033[0;39m' #reset.
LANG=C wget -4 ${CONTPARAM} -t 5 -w 5 --progress=bar:force "${URLSPEC}" 2>&1 | tee /tmp/efd/wget.log2
DLRET=$?
#150712...
ALLPS="`ps`" #important, this calls the busybox ps.
KILL_PIDS="$(echo "$ALLPS" | grep 'DL_ABORT_DLG' | sed -e 's%^ %%' -e 's%^ %%' -e 's%^ %%' | cut -f 1 -d ' ' | tr '\n' ' ' | sed -e 's% $%%')" #150808
[ "$KILL_PIDS" ] && kill $KILL_PIDS
if [ $DLRET -ne 0 ];then
Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --ok-label "$(gettext 'TRY AGAIN')" --cancel-label "$(gettext 'GIVE UP')" --icon "/usr/local/lib/X11/pixmaps/error.xpm" --yesno "$(gettext 'Download failed, see log top-left of screen.
Do you want to try again, or give up?')" 0 0
Xdialog --wmclass "splashwindow" --backtitle "$(gettext 'Easy File Downloader')" --screen-center --cr-wrap --wrap --ok-label "$(gettext 'TRY AGAIN')" --cancel-label "$(gettext 'GIVE UP')" --icon "/usr/local/lib/X11/pixmaps/error.xpm" --yesno "$(gettext 'Download failed, see log top-left of screen.')${CR}$(gettext 'Do you want to try again, or give up?')" 0 65
REPLY1=$?
kill $PID1
[ $REPLY1 -eq 0 ] && continue
rm -f "$FILENAME"
echo -n '2' > /tmp/efd/exit.err
exit 2
fi
kill $PID1

#paranoid, check size of downloaded file...
SIZEB_LOCAL=`stat --format=%s "${FILENAME}"`
if [ $SIZEB_LOCAL -ne $SIZEB_ONLINE ];then
INSERT1=''
DNflg=1
continue
fi

break
done
EOF1

chmod 755 /tmp/efd/efd-exec

export EFD_DLG1="<window title=\"$(gettext 'Easy File Downloader')\" image-name=\"/usr/local/lib/X11/mini-icons/mini-lower.xpm\" decorated=\"false\" skip_taskbar_hint=\"true\">
<vbox>
<terminal argv0=\"/tmp/efd/efd-exec\" hscrollbar-policy=\"2\" vscrollbar-policy=\"2\" font-name=\"Monospace 12\" text-foreground-color=\"#FFE0C0\">
<height>13</height>
<width>80</width>
<action>exit:finished</action>
</terminal>
<hbox>
<text><label>$(gettext 'Abort download:')</label></text>
<button><input file>/usr/local/lib/X11/mini-icons/mini-record.xpm</input><action>killall wget</action></button>
</hbox>
</vbox>
</window>"
RETVARS1="$(gtkdialog --program=EFD_DLG1 --geometry='+0+0')"
[ $? -ne 0 ] && exit 1
#eval "$RETVARS1"
#[ "$EXIT" != "finished" ] && exit
exit `cat /tmp/efd/exit.err`

exit 0
###END###
###end###

0 comments on commit 3144062

Please sign in to comment.