head	1.3;
access;
symbols;
locks; strict;
comment	@# @;


1.3
date	2003.11.18.04.09.37;	author seemant;	state dead;
branches;
next	1.2;

1.2
date	2003.11.18.01.36.58;	author tantive;	state Exp;
branches;
next	1.1;

1.1
date	2003.11.18.01.27.39;	author tantive;	state Exp;
branches;
next	;


desc
@@


1.3
log
@removing, as we are NOT yet authorized to have this in portage
@
text
@#!/bin/bash

# SCRIPT OPTIONS
O_DISTFILES="/var/www/distfiles"
O_MIRROR="http://gentoo.oregonstate.edu/distfiles"
O_DOWNQ="/var/www/distfiles/.queue"
O_LOGINFO="/usr/bin/logger -t gentoo-distfiles-mirror -p local5.info"
O_LOGERR="/usr/bin/logger -t gentoo-distfiles-mirror -p local5.err"
O_IMMEDIATE="TRUE"
O_IMMEDIATEQ="TRUE"
O_MINFREE="104857600"

# SCRIPT PROPERTIES
VERSION="1.0.7"
P_DOWNQ="FALSE"
P_FORCE="FALSE"
P_FILE=""
P_REMOTE=""

# EXIT CODES
E_OPTERROR=65

# FUNCTIONS
usage() {
  echo "Usage: $(basename $0) [OPTIONS | FILE]"
  echo "Incrementally build a distfiles mirror."
  echo
  echo "      FILE specifies the file to provide, if FILE"
  echo "      is absent, no additions will be made to the"
  echo "      download queue, and no redirection will be"
  echo "      performed."
  echo
  echo "  -d  download files listed in download queue"
  echo "  -f  force download of file even if it already exists"
  echo "  -h  display this help and exit"
  echo "  -v  output version information and exit"
  echo
  echo "Report bugs to <wolfwood@@users.sourceforge.net>."
  exit 0
}

version() {
  echo "$(basename $0) $VERSION"
  echo "Written by Nicholas D. Wolfwood."
  echo
  echo "Copyright (C) 2003 Free Software Foundation, Inc."
  echo "This is free software; see the source for copying conditions.  There is NO"
  echo "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
  exit 0
}

badopt() {
  echo "$(basename $0): invalid option -- $OPTARG"
  echo "Try '$(basename $0) -h' for more information."
  exit $E_OPTERROR
}

log() {
  if [ "$1" = "info" ]; then
    if [ -n "$O_LOGINFO" ]; then
      $O_LOGINFO "${2}"
    fi
  elif [ "$1" = "err" -o "$1" = "error" ]; then
    if [ -n "$O_LOGERR" ]; then
      $O_LOGERR "${2}"
    fi
  elif [ "$1" = "all" ]; then
    if [ -n "$O_LOGINFO" ]; then
      $O_LOGINFO "${2}"
    fi
    if [ -n "$O_LOGERR" ]; then
      $O_LOGERR "${2}"
    fi
  else
    return 1
  fi
  return 0
}

httpout() {
  if [ -z "$1" ]; then
    return 1
  else
    echo -en "Content-Type: text/plain\n\n"
    echo -e "${1}"
  fi
}

httpredirect() {
  if [ -z "$1" ]; then
    return 1
  else
    echo -en "Location: ${1}\n"
    echo -en "Content-Type: text/plain\n\n"
    echo "The requested resource resides temporarily under a different URI: ${1}"
  fi
}

httpnotfound() {
  echo -en "Status: 404 Not Found\n"
  echo -en "Content-Type: text/plain\n\n"
  echo "The server has not found anything matching the Request-URI."
}

httpdownload() {
  if [ -z "$1" ]; then
    return 1
  else
    if [ -n "$2" ]; then
      echo -en "Content-Length: ${2}\n"
    fi
    echo -en "Content-Type: ${1}\n\n"
  fi
}

# PARSE OPTIONS
while getopts ":dfhv" Option
do
  case $Option in
    d) P_DOWNQ="TRUE"; break;;
    f) P_FORCE="TRUE"; break;;
    h) usage; break;;
    v) version; break;;
    *) badopt; break;;
  esac
done
shift $(($OPTIND - 1))

# PARSE ARGUMENTS
if [ -n "$REQ_REMOTEADDR" ]; then
  P_REMOTE="$REQ_REMOTEADDR"
fi
if [ -n "$REQ_FILENAME" ]; then
  if [ ${#REQ_FILENAME} -ge 256 ]; then
    log err "Failure on request for ${P_REMOTE}: request filename is too long."
    exit 1
  else
    P_FILE="$REQ_FILENAME"
  fi
fi

# MAIN
if [ "$P_DOWNQ" = "TRUE" ]; then
  if [ -f "${O_DOWNQ}" ]; then
    declare -a downq=( $(cat "${O_DOWNQ}") )
    if [ ${#downq[@@]} -gt 0 ]; then
      for i in $(seq 0 $((${#downq[@@]} - 1))); do
        if [ -n "${downq[$i]}" ]; then
          if [ "$P_FORCE" = "TRUE" ]; then
            /usr/bin/wget -qrP "${O_DISTFILES}/" "${O_MIRROR}/${downq[$i]}"
          else
            /usr/bin/wget -qP "${O_DISTFILES}/" "${O_MIRROR}/${downq[$i]}"
          fi
          retval=$?

          if [ $retval -ne 0 ]; then
            if [ -f "${O_DISTFILES}/${downq[$i]}" ]; then
              /bin/rm "${O_DISTFILES}/${downq[$i]}"
            fi
            log err "Failed download: wget exited code ${retval} on file: ${downq[$i]}"
          else
            /bin/chmod 0444 "${O_DISTFILES}/${downq[$i]}"
            retval=$?

            if [ $retval -ne 0 ]; then
              if [ -f "${O_DISTFILES}/${downq[$i]}" ]; then
                /bin/rm "${O_DISTFILES}/${downq[$i]}"
              fi
              log err "Failed to chmod file: ${downq[$i]} (chmod exited code ${retval})"
            else
              log info "File downloaded successfully: ${downq[$i]}"
            fi
          fi
          downq[$i]=""
        fi
      done
      cat /dev/null >"$O_DOWNQ"
      for i in $(seq 0 $((${#downq[@@]} - 1))); do
        if [ -n "${downq[$i]}" ]; then
          echo "${downq[$i]}" >>"$O_DOWNQ"
        fi
      done
    fi
  else
    log err "No download queue: ${O_DOWNQ}"
  fi
else
  if [ -n "$P_FILE" ]; then
    if [ "$P_FORCE" = "FALSE" ]; then
      if [ -f "${O_DISTFILES}/${P_FILE}" ]; then
        httpout "File already exists."
        exit 1
      fi
    fi

    if [ "$P_FILE" != "index.html" ]; then
      if [ "$O_IMMEDIATE" = "TRUE" ]; then
        log info "Downloading file: ${P_FILE} and serving to ${P_REMOTE}"

        curl -is "${O_MIRROR}/${P_FILE}" | while read -r line; do
          if [ ${#line} -eq 1 ]; then
            echo ""
            tee "${O_DISTFILES}/${P_FILE}"
            break
          else
            case $line in
              [Cc]ontent-[Ll]ength:*)
		len=$(echo "$line" | sed -r -e 's/.*[Cc]ontent-[Ll]ength:[ ]*([0-9]+).*$/\1/');
		dsf=$(df -P -B1 "${O_DISTFILES}" | tail -n1 | awk '{ print $4 }');
                if [ "$(($dsf - $len))" -lt "${O_MINFREE}" ]; then
                  log err "Not enough free disk space for file: ${P_FILE} requested by ${P_REMOTE}"
                  httpredirect "${O_MIRROR}/${P_FILE}"
                  exit 1
                else
                  echo "$line"
                fi;;
              [Cc]ontent-[Tt]ype:*) echo "$line";;
            esac
          fi
        done
        retval=$?

        if [ $retval -ne 0 ]; then
          if [ -f "${O_DISTFILES}/${P_FILE}" ]; then
            /bin/rm "${O_DISTFILES}/${P_FILE}"
          fi
          log err "Failed download: curl | tee exited code ${retval} on file: ${P_FILE}"
          httpnotfound
        else
          /bin/chmod 0444 "${O_DISTFILES}/${P_FILE}"
          retval=$?

          if [ $retval -ne 0 ]; then
            if [ -f "${O_DISTFILES}/${P_FILE}" ]; then
              /bin/rm "${O_DISTFILES}/${P_FILE}"
            fi
            log err "Failed to chmod file: ${P_FILE} (chmod exited code ${retval}), file deleted."
          else
            if [ -s "${O_DISTFILES}/${P_FILE}" ]; then
              log info "File downloaded: ${P_FILE} and served to ${P_REMOTE} successfully."
            else
              #ZERO LENGTH FAILURE
              log info "Client ${P_REMOTE} canceled download of file: ${P_FILE}"

              retval=0
              if [ -f "${O_DISTFILES}/${P_FILE}" ]; then
                /bin/rm "${O_DISTFILES}/${P_FILE}"
                retval=$?
              fi
              if [ $retval -ne 0 ]; then
                log err "Failed to delete file: ${P_FILE}, please do so ASAP."
              else
                log info "Download of file: ${P_FILE} for ${P_REMOTE} canceled, file deleted."
              fi

              if [ "$O_IMMEDIATEQ" = "TRUE" ]; then
                echo "$P_FILE" >>"$O_DOWNQ"
                retval=$?

                if [ $retval -ne 0 ]; then
                  log err "Failed to queue file: ${P_FILE} for ${P_REMOTE} (echo exited code ${retval})"
                else
                  log info "Queued file originally requested by ${P_REMOTE} for download: ${P_FILE}"
                fi
              fi
              #END ZERO LENGTH FAILURE SECTION
            fi
          fi
        fi
      else
        echo "$P_FILE" >>"$O_DOWNQ"
        retval=$?

        if [ $retval -ne 0 ]; then
          log err "Failed to queue file: ${P_FILE} for ${P_REMOTE} (echo exited code ${retval})"
        else
          log info "Queued file originally requested by ${P_REMOTE} for download: ${P_FILE}"
        fi

        httpredirect "${O_MIRROR}/${P_FILE}"
      fi
    else
      httpredirect "../listdistfiles/"
    fi
    exit 0
  else
    httpout "No file specified for download."
    exit 0
  fi
fi

exit 0
@


1.2
log
@some fixes in the script
@
text
@@


1.1
log
@Initial release.
@
text
@d209 1
a209 1
                dsf=$(df -B1 "${O_DISTFILES}" | head -n2 | tail -n1 | awk '{ print $4 }');
@

