Blame tcar-scripts/tcar_checkFiles.sh

Alain Reguera Delgado 66223d
#!/bin/bash
Alain Reguera Delgado 66223d
######################################################################
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
#   tcar_checkFiles.sh -- This function standardizes the way file
Alain Reguera Delgado 66223d
#   conditional expressions are applied to files.  Here is where
Alain Reguera Delgado 66223d
#   tcar.sh script answers questions like: is the file a regular
Alain Reguera Delgado 66223d
#   file or a directory?  Or, is it a symbolic link? Or even, does it
Alain Reguera Delgado 66223d
#   have execution rights, etc.  If the verification fails somehow at
Alain Reguera Delgado 66223d
#   any point, an error message is output and tcar.sh script
Alain Reguera Delgado 66223d
#   finishes its execution. 
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
#   Written by:
Alain Reguera Delgado 66223d
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
# Copyright (C) 2009-2013 The CentOS Artwork SIG
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 66223d
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 66223d
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 66223d
# your option) any later version.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 66223d
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 66223d
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 66223d
# General Public License for more details.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 66223d
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 66223d
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
######################################################################
Alain Reguera Delgado 66223d
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
}