Blame Scripts/tcar_checkFiles.sh

Alain Reguera Delgado 66223d
#!/bin/bash
Alain Reguera Delgado 66223d
######################################################################
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   tcar - The CentOS Artwork Repository automation tool.
Alain Reguera Delgado b33753
#   Copyright © 2014 The CentOS Artwork SIG
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   This program is free software; you can redistribute it and/or
Alain Reguera Delgado b33753
#   modify it under the terms of the GNU General Public License as
Alain Reguera Delgado b33753
#   published by the Free Software Foundation; either version 2 of the
Alain Reguera Delgado b33753
#   License, or (at your option) any later version.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   This program is distributed in the hope that it will be useful,
Alain Reguera Delgado b33753
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado b33753
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado b33753
#   General Public License for more details.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   You should have received a copy of the GNU General Public License
Alain Reguera Delgado b33753
#   along with this program; if not, write to the Free Software
Alain Reguera Delgado b33753
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   Alain Reguera Delgado <al@centos.org.cu>
Alain Reguera Delgado b33753
#   39 Street No. 4426 Cienfuegos, Cuba.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
######################################################################
Alain Reguera Delgado 66223d
Alain Reguera Delgado b33753
# Standardizes the way file conditional expressions are applied to
Alain Reguera Delgado b33753
# files.  Here is where tcar.sh script answers questions like: is the
Alain Reguera Delgado b33753
# file a regular file or a directory?  Or, is it a symbolic link? Or
Alain Reguera Delgado b33753
# even, does it have execution rights, etc.  If the verification fails
Alain Reguera Delgado b33753
# somehow at any point, an error message is output and tcar.sh script
Alain Reguera Delgado b33753
# finishes its execution.
Alain Reguera Delgado 66223d
function tcar_checkFiles {
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Initialize local array variables.
Alain Reguera Delgado 66223d
    local -a CONDITION_COMMAND
Alain Reguera Delgado 66223d
    local -a CONDITION_PATTERN
Alain Reguera Delgado 66223d
    local -a CONDITION_MESSAGE
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Initialize local counter.
Alain Reguera Delgado 66223d
    local COUNTER=0
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    OPTIND=1
Alain Reguera Delgado 66223d
    while getopts "i:,r,m:,n,d,e,f,h,x" OPTION "${@}"; do
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        case "${OPTION}" in
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            d )
Alain Reguera Delgado 66223d
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/test'
Alain Reguera Delgado 66223d
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-d'
Alain Reguera Delgado 66223d
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a directory."`"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            e )
Alain Reguera Delgado 66223d
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/test'
Alain Reguera Delgado 66223d
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-e'
Alain Reguera Delgado 66223d
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "doesn't exist."`"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            f )
Alain Reguera Delgado 66223d
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/test'
Alain Reguera Delgado 66223d
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-f'
Alain Reguera Delgado 66223d
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a regular file."`"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            h )
Alain Reguera Delgado 66223d
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/test'
Alain Reguera Delgado 66223d
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-h'
Alain Reguera Delgado 66223d
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a symbolic link."`"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            x )
Alain Reguera Delgado 66223d
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/test'
Alain Reguera Delgado 66223d
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-x'
Alain Reguera Delgado 66223d
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't an executable file."`"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            i )
Alain Reguera Delgado 66223d
                local MIME=${OPTARG}
Alain Reguera Delgado 66223d
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/file'
Alain Reguera Delgado 66223d
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-bi'
Alain Reguera Delgado 66223d
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`eval_gettext "isn't a \\\"\\\$MIME\\\" file."`"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            m )
Alain Reguera Delgado 66223d
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='match'
Alain Reguera Delgado 66223d
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]="${OPTARG}"
Alain Reguera Delgado 66223d
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "doesn't match its pattern."`"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            n )
Alain Reguera Delgado 66223d
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]="/bin/rpm"
Alain Reguera Delgado 66223d
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]="-q --quiet"
Alain Reguera Delgado 66223d
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't installed in the system."`"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        esac
Alain Reguera Delgado 66223d
    done
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Clean up positional parameters to reflect the fact that options
Alain Reguera Delgado 66223d
    # have been processed already.
Alain Reguera Delgado 66223d
    shift $(( ${OPTIND} - 1 ))
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define list of files we want to apply verifications to, now that
Alain Reguera Delgado 66223d
    # all option-like arguments have been removed from positional
Alain Reguera Delgado 66223d
    # parameters list so we are free to go with the verifications.
Alain Reguera Delgado 66223d
    local FILE=''
Alain Reguera Delgado 66223d
    local FILES=${@}
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Verify existence of files to prevent tcar.sh script from
Alain Reguera Delgado 66223d
    # using the current location in cases when it shouldn't (e.g.,
Alain Reguera Delgado 66223d
    # here it is expecting a list of files to process.).
Alain Reguera Delgado 66223d
    if [[ -z ${FILES} ]];then
Alain Reguera Delgado 66223d
        tcar_printMessage "`gettext "No file for processing found."`" --as-error-line
Alain Reguera Delgado 66223d
    fi
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    for FILE in ${FILES};do
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        until [[ ${COUNTER} -eq ${#CONDITION_PATTERN[*]} ]];do
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            case ${CONDITION_COMMAND[${COUNTER}]} in
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
                "/usr/bin/test" | "/bin/rpm" )
Alain Reguera Delgado 66223d
                ${CONDITION_COMMAND[${COUNTER}]} ${CONDITION_PATTERN[${COUNTER}]} ${FILE} \
Alain Reguera Delgado 66223d
                    || tcar_printMessage "${FILE} ${CONDITION_MESSAGE[${COUNTER}]}" --as-error-line
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
                "/usr/bin/file" )
Alain Reguera Delgado 66223d
                if [[ ! $(${CONDITION_COMMAND[${COUNTER}]} ${CONDITION_PATTERN[${COUNTER}]} ${FILE}) =~ "^${MIME}" ]];then
Alain Reguera Delgado 66223d
                    tcar_printMessage "${FILE} ${CONDITION_MESSAGE[${COUNTER}]}" --as-error-line
Alain Reguera Delgado 66223d
                fi
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
                "match" )
Alain Reguera Delgado 66223d
                if [[ ! ${FILE} =~ "${CONDITION_PATTERN[${COUNTER}]}" ]];then
Alain Reguera Delgado 66223d
                    tcar_printMessage "${FILE} ${CONDITION_MESSAGE[${COUNTER}]}" --as-error-line
Alain Reguera Delgado 66223d
                fi
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
                * )
Alain Reguera Delgado 66223d
                tcar_printMessage "`gettext "The condition command provided isn't supported."`" --as-error-line
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            esac
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
            COUNTER=$((${COUNTER} + 1))
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        done
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    done
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
}