Blame tcar_checkFiles.sh

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