Blame Scripts/Bash/Functions/Commons/cli_checkFiles.sh

878a2b
#!/bin/bash
878a2b
#
878a2b
# cli_checkFiles.sh -- This function standardizes the way file
878a2b
# conditional expressions are applied inside centos-art.sh script.
878a2b
# Here is where we answer questions like: is the file a regular file
0b8c8e
# or a directory?  Or, is it a symbolic link? Or even, does it have
878a2b
# execution rights, etc.  If the verification fails somehow at any
80c9e6
# point, an error message is output and centos-art.sh script finishes
80c9e6
# its execution. 
878a2b
#
03486a
# Copyright (C) 2009, 2010, 2011, 2012 The CentOS Project
878a2b
#
878a2b
# This program is free software; you can redistribute it and/or modify
878a2b
# it under the terms of the GNU General Public License as published by
878a2b
# the Free Software Foundation; either version 2 of the License, or (at
878a2b
# your option) any later version.
878a2b
#
878a2b
# This program is distributed in the hope that it will be useful, but
878a2b
# WITHOUT ANY WARRANTY; without even the implied warranty of
878a2b
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
878a2b
# General Public License for more details.
878a2b
#
878a2b
# You should have received a copy of the GNU General Public License
878a2b
# along with this program; if not, write to the Free Software
878a2b
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
878a2b
#
878a2b
# ----------------------------------------------------------------------
878a2b
# $Id$
878a2b
# ----------------------------------------------------------------------
878a2b
878a2b
function cli_checkFiles {
878a2b
878a2b
    # Define short options.
3b9515
    local ARGSS='i:,r,m:,n,d,e,f,h,x'
878a2b
878a2b
    # Define long options.
3b9515
    local ARGSL='mime:,is-versioned,match:,is-installed'
3b0a5c
3b0a5c
    # Initialize array variables.
0b8c8e
    local -a CONDITION_COMMAND
3b0a5c
    local -a CONDITION_PATTERN
3b0a5c
    local -a CONDITION_MESSAGE
3b0a5c
3b0a5c
    # Initialize array variable counter.
3b0a5c
    local COUNTER=0
878a2b
878a2b
    # Initialize arguments with an empty value and set it as local
3b0a5c
    # variable to this function scope. Doing this is very important to
3b0a5c
    # avoid any clash with higher execution environments.
878a2b
    local ARGUMENTS=''
878a2b
3b0a5c
    # Prepare ARGUMENTS for getopt.
878a2b
    cli_parseArgumentsReDef "$@"
878a2b
3b0a5c
    # Redefine ARGUMENTS using getopt(1) command parser.
878a2b
    cli_parseArguments
878a2b
878a2b
    # Redefine positional parameters using ARGUMENTS variable.
878a2b
    eval set -- "$ARGUMENTS"
878a2b
878a2b
    # Look for options passed through positional parameters.
878a2b
    while true; do
878a2b
878a2b
        case "$1" in
878a2b
aa68b3
            -d )
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='test'
3b0a5c
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-d'
3b0a5c
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a directory."`"
878a2b
                shift 1
878a2b
                ;;
878a2b
aa68b3
            -e )
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='test'
aa68b3
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-e'
aa68b3
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "doesn't exist."`"
3b9515
                shift 1
aa68b3
                ;;
aa68b3
aa68b3
            -f )
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='test'
3b0a5c
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-f'
3b0a5c
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a regular file."`"
878a2b
                shift 1
878a2b
                ;;
878a2b
aa68b3
            -h )
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='test'
3b0a5c
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-h'
3b0a5c
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a symbolic link."`"
878a2b
                shift 1
878a2b
                ;;
878a2b
aa68b3
            -x )
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='test'
3b0a5c
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-x'
3b0a5c
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't an executable file."`"
878a2b
                shift 1
878a2b
                ;;
878a2b
0b8c8e
            -i | --mime )
0b8c8e
                local MIME=$2
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='file'
0b8c8e
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-bi'
0b8c8e
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`eval_gettext "isn't a \\\"\\\$MIME\\\" file."`"
0b8c8e
                shift 2
0b8c8e
                ;;
0b8c8e
cf8bc6
            -m | --match )
cf8bc6
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='match'
cf8bc6
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]="$2"
cf8bc6
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`eval_gettext "doesn't match its pattern."`"
cf8bc6
                shift 2
3b9515
               ;;
cf8bc6
f73bab
            -r | --is-versioned )
3b9515
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]="centos-art"
3b9515
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]="vcs --is-versioned"
3b9515
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]=""
3b9515
                shift 1
3b9515
                ;;
3b9515
3b9515
            -n | --is-installed )
3b9515
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]="rpm"
3b9515
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]="-q --quiet"
3b9515
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't installed in the system."`"
f73bab
                shift 1
f73bab
                ;;
f73bab
878a2b
            -- )
878a2b
                shift 1
878a2b
                break
878a2b
                ;;
3b9515
878a2b
        esac
878a2b
    done
878a2b
3b0a5c
    # Define list of files we want to apply verifications to, now that
3b0a5c
    # all option-like arguments have been removed from positional
6c95c4
    # parameters list so we are free to go with the verifications.
3b0a5c
    local FILE=''
3b0a5c
    local FILES=$@
3b0a5c
3b0a5c
    for FILE in $FILES;do
3b0a5c
3b9515
        until [[ ${COUNTER} -eq ${#CONDITION_PATTERN[*]} ]];do
3b0a5c
0b8c8e
            case ${CONDITION_COMMAND[$COUNTER]} in
0b8c8e
3b9515
                "test" | "rpm" )
0b8c8e
                ${CONDITION_COMMAND[$COUNTER]} ${CONDITION_PATTERN[$COUNTER]} ${FILE} \
0b8c8e
                    || cli_printMessage "${FILE} ${CONDITION_MESSAGE[$COUNTER]}" --as-error-line
0b8c8e
                ;;
0b8c8e
3b9515
                "centos-art" )
3b9515
                # Don't create another level for error messages here
3b9515
                # (that would duplicate them unnecessarily).  Instead,
3b9515
                # set error messages inside specific functionalities
3b9515
                # and use them directly from there.
3b9515
                cli_runFnEnvironment ${CONDITION_PATTERN[$COUNTER]} ${FILE}
3b9515
                ;;
3b9515
cf8bc6
                "file" )
0b8c8e
                if [[ ! $(${CONDITION_COMMAND[$COUNTER]} ${CONDITION_PATTERN[$COUNTER]} ${FILE}) == "$MIME" ]];then
0b8c8e
                    cli_printMessage "${FILE} ${CONDITION_MESSAGE[$COUNTER]}" --as-error-line
0b8c8e
                fi
0b8c8e
                ;;
0b8c8e
cf8bc6
                "match" )
cf8bc6
                if [[ ! ${FILE} =~ "${CONDITION_PATTERN[$COUNTER]}" ]];then
cf8bc6
                    cli_printMessage "${FILE} ${CONDITION_MESSAGE[$COUNTER]}" --as-error-line
cf8bc6
                fi
cf8bc6
                ;;
cf8bc6
0b8c8e
                * )
0b8c8e
                cli_printMessage "`gettext "The condition command provided isn't supported."`" --as-error-line
0b8c8e
                ;;
3b9515
0b8c8e
            esac
3b0a5c
3b0a5c
            COUNTER=$(($COUNTER + 1))
3b0a5c
3b0a5c
        done
3b0a5c
3b0a5c
    done
3b0a5c
878a2b
}