download_file 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/bin/sh
  2. #(c) Copyright Barry Kauler 2010. License GPL v3 /usr/share/doc/legal.
  3. #BK a simple frontend for wget.
  4. #called from /usr/local/petget/petget, downloadpkgs.sh.
  5. #URL passed in, only downloads to current directory.
  6. #only put up user-response dlg windows if failure.
  7. #101116 first version.
  8. #101117 url sanity check. more download size check.
  9. #101118 for ftp connection have to get size differently.
  10. #101126 not sure, it seems that wget now defaults to ipv6, fails for some users, use '-4' to force ipv4.
  11. #101206 fix find filesize ftp://ftp.lug.udel.edu/pub/puppylinux ...
  12. #120201 rodin.s: internationalized. Add LANG=C to 1-st wget for proper work
  13. #120218 removed a LANG=C line (line 43).
  14. #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
  15. #120224 small tweak.
  16. #120908 revert 120220.
  17. #121019 extra msg if missing-url, when called from ppm (refer /usr/local/petget/downloadpkgs.sh).
  18. BG='#2F2F2F'
  19. FG='#E3DB8B'
  20. # adding gettext
  21. export TEXTDOMAINDIR=/usr/share/locale
  22. export TEXTDOMAIN=download_file
  23. eval_gettext () {
  24. local myMESSAGE=$(gettext "$1")
  25. eval echo \"$myMESSAGE\"
  26. }
  27. LANG="`cat /etc/profile|grep '^LANG='`"
  28. eval $LANG
  29. export LANG_USER=$LANG
  30. #-----------------------------#
  31. #exit number: 0 ok, 1 unable to download, 2 download fail, 3 invalid url.
  32. #export LANG=C #keep Xdialog happy.
  33. LANG=$LANG_USER # i18n
  34. URLSPEC="$@"
  35. FILENAME="`basename "$@"`"
  36. URLDIR="`dirname "$@"`" #121019
  37. . /etc/rc.d/PUPSTATE
  38. #121019 ***TODO***
  39. #note: in future maybe make this into a gtkdialog window, with a button to launch web browser...
  40. #this is a quick hack. in future, modify download-file so that only a missing-file runs "exit 1", then
  41. #put up this info in downloadpkgs.sh. then no need for exported DL_F_CALLED_FROM.
  42. PPM_NO_URL_MSG=""
  43. if [ "$DL_F_CALLED_FROM" = "ppm" ];then #see /usr/local/petget/downloadpkgs.sh
  44. PPM_NO_URL_MSG="
  45. $(gettext "NOTE:")
  46. $(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:")
  47. ${URLDIR}
  48. $(gettext "If the URL is working and the repository has a later package, cancel the current operation. In the Puppy 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.")"
  49. fi
  50. #test url... -t 2 means try 2 times, -T 20 means timeout in 20 seconds
  51. while [ 1 ];do
  52. echo -n "" > /tmp/download_file_spider.log1
  53. rxvt -geometry 130x10+0+0 -bg "$BG" -fg "$FG" -title "$(gettext 'Testing file exists:') ${URLSPEC}" -e tail -f /tmp/download_file_spider.log1 & #120908 restored.
  54. PID1=$!
  55. LANG=C wget -4 -t 2 -T 20 --waitretry=20 --spider -S "${URLSPEC}" > /tmp/download_file_spider.log1 2>&1 #
  56. if [ $? -ne 0 ];then
  57. Xdialog --wmclass "gtkdialog2" --left --wrap --title "$(gettext 'Puppy 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.
  58. Do you want to test again, or give up?')${PPM_NO_URL_MSG}" 0 0
  59. REPLY1=$?
  60. kill $PID1
  61. [ $REPLY1 -eq 0 ] && continue
  62. exit 1
  63. fi
  64. kill $PID1
  65. break
  66. done
  67. #LANG=C # i18n
  68. #find out size of online file...
  69. SIZEB_ONLINE=`grep '^Length: ' /tmp/download_file_spider.log1 | tr -s ' ' | cut -f 2 -d ' ' | sed -e 's%[^0-9]%%g'`
  70. #101118 for ftp connection have to get size differently...
  71. [ "$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'`
  72. #101206 ftp://ftp.lug.udel.edu/pub/puppylinux/pet_packages-3/gpm-1.20.1-1.pet ...
  73. [ "$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'`
  74. [ "$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'`
  75. #101117 sanity check...
  76. URL_BROKEN='0'
  77. if [ "$SIZEB_ONLINE" = "" ];then
  78. MSG_NO1="$(gettext 'Oh dear, unable to obtain size of remote file:')
  79. ${URLSPEC}
  80. $(gettext 'although it does seem to exist.')"
  81. URL_BROKEN='1'
  82. else
  83. case $SIZEB_ONLINE in
  84. unspecified)
  85. MSG_NO1="$(gettext 'Oh dear, this URL seems to be wrong:')
  86. ${URLSPEC}
  87. $(gettext 'It is supposed to be of the format http://foo.org/abiword-1.pet,
  88. or ftp://foo.org/abiword-1.pet (with name of the file to download
  89. on the end), but your URL seems wrong.')"
  90. URL_BROKEN='1'
  91. ;;
  92. *[a-zA-Z]*)
  93. MSG_NO1="$(gettext 'Oh dear, something seems to be wrong with the URL:')
  94. ${URLSPEC}
  95. $(gettext 'A probe of the remote file could not retrieve correct information
  96. on it.') "
  97. URL_BROKEN='1'
  98. ;;
  99. esac
  100. fi
  101. if [ "$URL_BROKEN" != "0" ];then
  102. Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy File Downloader')" --screen-center --cr-wrap --icon "/usr/local/lib/X11/pixmaps/error.xpm" --msgbox "${MSG_NO1}
  103. $(gettext 'Click button to abort download...')" 0 0
  104. exit 3
  105. fi
  106. #101117 check enough free space...
  107. #FREEK=`df -k | grep ' /$' | tr -s ' ' | cut -f 4 -d ' '`
  108. #no, need to know free space in whatever is current dir...
  109. PWD="`pwd`"
  110. FREEblocks=`stat -f --format=%a ${PWD}`
  111. BLOCKsize=`stat -f --format=%S ${PWD}`
  112. FREEbytes=`expr $FREEblocks \* $BLOCKsize`
  113. FREEK=`expr $FREEbytes \/ 1024`
  114. SIZEK_ONLINE=`expr $SIZEB_ONLINE \/ 1024`
  115. case $PUPMODE in
  116. 3|7|13|77) #tmpfs on top
  117. #when install pkg, it writes direct to save-layer.
  118. SIZEK_REQUEST=`expr $SIZEK_ONLINE + 6000` #6MB slack.
  119. ;;
  120. *)
  121. SIZEK_REQUEST=`expr $SIZEK_ONLINE \* 3` #3MB slack, no, need install space too.
  122. ;;
  123. esac
  124. #LANG=$LANG_USER
  125. if [ $SIZEK_REQUEST -gt $FREEK ];then
  126. case $PUPMODE in
  127. 5)
  128. MSGs="$(gettext 'You need to shutdown and create a save-file, then you will have more space.
  129. See menu Shutdown -> Reboot')"
  130. ;;
  131. 2|6)
  132. MSGs="$(gettext 'The partition is full, you will have to delete something.')"
  133. ;;
  134. 3|7|77) #tmpfs on top, full partition underneath
  135. MSGs="$(gettext 'Do not have enough space in the RAM. Maybe you need a swap file
  136. or swap partition (or it needs to be bigger)')"
  137. ;;
  138. *)
  139. MSGs="$(gettext 'You need to increase the size of the save-file.
  140. See menu Utility -> Resize personal storage file')"
  141. ;;
  142. esac
  143. Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy File Downloader')" --screen-center --cr-wrap --icon "/usr/local/lib/X11/pixmaps/error.xpm" --msgbox "
  144. `eval_gettext \"Sorry, but there is not enough free space to download to.
  145. Attempted download directory: \\\${PWD}
  146. The online file is \\\${SIZEK_ONLINE}KB, but your free space is \\\${FREEK}KB.
  147. \\\${MSGs}
  148. Click button to abort download...\"`" 0 0
  149. exit 1
  150. fi
  151. INSERT1="$(gettext 'already ')"
  152. while [ 1 ];do
  153. CONTPARAM=''
  154. if [ -f $FILENAME ];then
  155. SIZEB_LOCAL=`stat --format=%s "${FILENAME}"`
  156. if [ $SIZEB_LOCAL -lt $SIZEB_ONLINE ];then
  157. Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy 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
  158. no guarantee it is uncorrupted), do you want to continue downloading it,
  159. or erase it and download entire file again?\"`" 0 0
  160. if [ $? -eq 0 ];then
  161. CONTPARAM='-c'
  162. else
  163. rm -f "$FILENAME"
  164. fi
  165. else
  166. if [ $SIZEB_LOCAL -gt $SIZEB_ONLINE ];then #101117
  167. Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy 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
  168. than the online file. Hmmm... this probably means that the online
  169. file is a more recent version, so it is recommended that you erase
  170. the local file and download it again...
  171. Do you want to erase it and download entire file again?\"`" 0 0
  172. if [ $? -eq 0 ];then
  173. rm -f "$FILENAME"
  174. else
  175. exit 0
  176. fi
  177. else
  178. Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy 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
  179. no guarantee it is uncorrupted), do you want to erase it and download
  180. entire file again?\"`" 0 0
  181. if [ $? -eq 0 ];then
  182. rm -f "$FILENAME"
  183. else
  184. exit 0
  185. fi
  186. fi
  187. fi
  188. fi
  189. echo -n "" > /tmp/download_file_spider.log2
  190. rxvt -geometry 130x10+0+0 -bg "$BG" -fg "$FG" -title "$(gettext 'Downloading file:') ${URLSPEC}" -e tail -f /tmp/download_file_spider.log2 & #120908 restored.
  191. PID1=$!
  192. #-t 5 means retry 5 times, -w 5 means wait 5 seconds between retries...
  193. wget -4 ${CONTPARAM} -t 5 -w 5 "${URLSPEC}" > /tmp/download_file_spider.log2 2>&1
  194. if [ $? -ne 0 ];then
  195. Xdialog --wmclass "gtkdialog2" --title "$(gettext 'Puppy 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.
  196. Do you want to try again, or give up?')" 0 0
  197. REPLY1=$?
  198. kill $PID1
  199. [ $REPLY1 -eq 0 ] && continue
  200. rm -f "$FILENAME"
  201. exit 2
  202. fi
  203. kill $PID1
  204. #paranoid, check size of downloaded file...
  205. SIZEB_LOCAL=`stat --format=%s "${FILENAME}"`
  206. if [ $SIZEB_LOCAL -ne $SIZEB_ONLINE ];then
  207. INSERT1=''
  208. continue
  209. fi
  210. break
  211. done
  212. exit 0
  213. ###END###