/var/lib/sorcery/modules/libdownload

     1	#!/bin/bash
     2	#-------------------------------------------------------------------------
     3	##
     4	## @Synopsis Frontend interface for dl_handlers
     5	##
     6	## This file contains functions for accessing download handlers.
     7	## Download handlers handle the physical acquiring of files as opposed to
     8	## the more abstract process of parsing and manipulating urls.
     9	## A url handler will specify a download handler for a given url.
    10	##
    11	## dl handlers may call back to url handlers to have a url parsed
    12	## it is up to the dl handler to interprit the results of the url
    13	## parsing as it may be different for each type of url.
    14	##
    15	## dl handlers are responsible for connecting to the internet if they
    16	## need to.
    17	##
    18	## WRITING NEW DL HANDLERS
    19	## <pre>
    20	## Current dl handlers only need a single function
    21	## "dl_get_<handler>" This function takes the following arguments:
    22	## $target
    23	## $url_list      List of urls to get the target from
    24	## $hints         Hints, these help">help">help the function determine what
    25	##                type of thing it is downloading or other tunables.
    26	## $dl_target     Name of variable in which to store the name of the
    27	##                result directory or file
    28	## $dl_type       Name of the variable in which to store the type of
    29	##                thing downloaded
    30	## </pre>
    31	##
    32	## The target parameter is advisory, it is a hint at what might be downloaded.
    33	##
    34	## The hints value is a list, values of interest to a particular
    35	## dl_handler should be prefixed "dl_<handler>", the handler may define
    36	## any hints which it see's necessary.
    37	## There are also well defined hints: "tree" "file" these indicate that the
    38	## type of thing being downloaded is probably a tree or file despite
    39	## what we may think.
    40	##
    41	## The basic form of a dl handler's get function is simply a loop over each
    42	## url until one successfully downloads, it should then set the target and
    43	## type reference variable successfully.
    44	##
    45	## @Copyright Copyright 2002 by the Source Mage Team
    46	## @Copyright Copyright 2005 by the Source Mage Team
    47	##
    48	#-------------------------------------------------------------------------
    49	
    50	if ! [[ $DL_HANDLER_FILES ]] ; then
    51	  DL_HANDLER_FILES=`ls $SGL_LIBRARY_MODULES/dl_handlers/dl_*[^~]`
    52	  for  dl_handler_file  in  $DL_HANDLER_FILES;  do
    53	    [  -x $dl_handler_file  ] && . $dl_handler_file
    54	  done
    55	fi
    56	
    57	
    58	#-------------------------------------------------------------------------
    59	##
    60	## Download a specified group of urls, they are assumed to all be understood
    61	## by the specified handler.
    62	##
    63	## @Param type  Name of handler
    64	## @Note The remainder of the arguments are target, url_list, hints, dl_target
    65	## @Note and dl_type. See above for information.
    66	##
    67	##
    68	#-------------------------------------------------------------------------
    69	function dl_get_bucket() {
    70	  $STD_DEBUG
    71	  local handler=$1
    72	  local target=$2
    73	  local url_list=$3
    74	  local hints=$4
    75	  local dl_target=$5
    76	  local dl_type=$6
    77	
    78	  if ! [[ "$handler" ]] ; then
    79	    message "Missing handler, this is probably a sorcery bug"
    80	    return 255
    81	  elif ! misc_is_function dl_${handler}_get; then
    82	    message "$handler is not a valid handler, this is probably a sorcery bug"
    83	    return 255
    84	  fi
    85	
    86	  dl_${handler}_get "$target" "$url_list" "$hints" "$dl_target" "$dl_type"
    87	  if [[ $? == 0 ]] ; then
    88	    debug "libdownload" "dl_handler -- downloaded $url"
    89	    eval debug "\$$dl_target \$$dl_type"
    90	    return 0
    91	  fi
    92	  return 1
    93	}
    94	
    95	function dl_command_check() {
    96	  local cmd
    97	  smgl_which $1 cmd
    98	  if ! [[ $cmd ]] || ! test -x $cmd ; then
    99	    message "${PROBLEM_COLOR}${cmd:-$1} is not installed, please cast it..." \
   100	            "${DEFAULT_COLOR}"
   101	    return 1
   102	  fi
   103	}
   104	
   105	
   106	#-------------------------------------------------------------------------
   107	## this nonsense is to get us connected to the outside world
   108	## it should eventually be a seperate, userdefinable script, with
   109	## this as the default of course
   110	#-------------------------------------------------------------------------
   111	function dl_connect()  {
   112	
   113	  [[ $INTERNET_AUTOCONNECT == on ]] || return 0
   114	
   115	  debug "libdownload" "connect()"
   116	
   117	  # We agree that while this lock is held, no other script will be
   118	  # connecting or disconnecting internet service out from under us
   119	  lock_resources internet signon
   120	  for interface in $INTERNET_INTERFACES; do
   121	    if ifconfig  |  grep  -q  "^$interface" ; then
   122	      debug "libdownload" "connected through $interface"
   123	      unlock_resources internet signon
   124	      return
   125	    fi
   126	  done
   127	
   128	
   129	  if $CONNECT_SCRIPT ; then
   130	    message "${PROBLEM_COLOR}Cannot find or initiate network connection"
   131	    message "${DEFAULT_COLOR}"
   132	    message "Please setup your network connection."
   133	    message "Try netconf if you use ethernet (with dsl), or see man pppd"
   134	    message "for dialup.\n"
   135	    message "If you still have trouble and have another way of getting"
   136	    message "online, please come by #sourcemage on irc.freenode.net and"
   137	    message "someone will help">help">help you."
   138	    return 1
   139	  fi
   140	
   141	  # Timeout in deciseconds to wait for Interface to come up.
   142	  local TIMEOUT=30
   143	
   144	  until  ifconfig  |  grep  -q  eth   ||
   145	         ifconfig  |  grep  -q  atml   ||
   146	         ifconfig  |  grep  -q  ppp   ||
   147	         [  $TIMEOUT == 0  ]
   148	  do
   149	    sleep  10
   150	    (( TIMEOUT-- ))
   151	  done
   152	  counter_up internet counter
   153	  unlock_resources internet signon
   154	}
   155	
   156	#-------------------------------------------------------------------------
   157	## this clever function disconnects us if we aren't downloading something
   158	## the heuristic is rather poor.
   159	#-------------------------------------------------------------------------
   160	function dl_disconnect()  {
   161	
   162	  [[ $INTERNET_AUTOCONNECT == on ]] || return 0
   163	
   164	  local i_counter=/tmp/sorcery/i_counter
   165	
   166	  # dont bother disconnecting if we didnt have to connect
   167	  if test $(counter_check internet counter) -lt 1 ; then
   168	    return
   169	  fi
   170	
   171	  counter_down internet counter
   172	  disconnect_script /tmp/sorcery/disconnect.$$
   173	  bash /tmp/sorcery/disconnect.$$ &
   174	}
   175	
   176	
   177	
   178	function disconnect_script() {
   179	cat > $1 << EOF
   180	
   181	. /etc/sorcery/config
   182	# return if a script is already running
   183	trylock_resources internet disconnect || exit
   184	let i=0
   185	#
   186	# the following loop polls the "internet counter" counter
   187	# if after some number of pollings the counter remains consistently 0
   188	# assume that no one is going to use the connect and we disconnect
   189	while :; do
   190	  if trylock_resources internet signon &&
   191	     test \$(counter_check internet counter) -lt 1 ; then
   192	    let i++
   193	    if test \$i -gt 10; then
   194	      DISCONNECT=yes
   195	      counter_reset internet counter
   196	    fi
   197	  else
   198	    let i=0
   199	  fi
   200	  if [[ \$DISCONNECT ]] ; then
   201	    \$DISCONNECT_SCRIPT
   202	    rm $0
   203	    unlock_resources internet signon
   204	    exit $?
   205	  else
   206	    unlock_resources internet signon
   207	  fi
   208	  sleep 10
   209	done
   210	EOF
   211	}
   212	
   213	#---------------------------------------------------------------------
   214	## This software is free software; you can redistribute it and/or modify
   215	## it under the terms of the GNU General Public License as published by
   216	## the Free Software Foundation; either version 2 of the License, or
   217	## (at your option) any later version.
   218	##
   219	## This software is distributed in the hope that it will be useful,
   220	## but WITHOUT ANY WARRANTY; without even the implied warranty of
   221	## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   222	## GNU General Public License for more details.
   223	##
   224	## You should have received a copy of the GNU General Public License
   225	## along with this software; if not, write to the Free Software
   226	## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   227	#---------------------------------------------------------------------