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
878a2b
# 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
878a2b
# point, an error message is output and centos-art.sh script ends its
878a2b
# execution. 
878a2b
#
878a2b
# More than one file can be passed to this function, so we want to
878a2b
# process them all as specified by the options. Since we are using
878a2b
# getopt output it is possible to determine where options and
878a2b
# non-option arguments are in the list of arguments  (e.g., options
878a2b
# are on the left side of ` -- ' and non-options on the rigth side of
878a2b
# ` -- '). Non-options are the files we want to verify and options how
878a2b
# we want to verify them.
878a2b
#
878a2b
# Another issue to consider, when more than one file is passed to this
878a2b
# function, is that we cannot shift positional parameters as we
878a2b
# frequently do whe just one argument is passsed, doing so would
878a2b
# annulate the validation for the second and later files passed to the
878a2b
# function. So, in order to provide verification to all files passed
878a2b
# to the function, the verification loop must be set individual for
878a2b
# each option in this function.
878a2b
#
878a2b
# Assuming no option be passed to the function, a general verification
878a2b
# is performed to determine whether or not the file exists without
878a2b
# considering the file type just its existence.
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.
878a2b
    local ARGSS='d,r,h,n,x,w'
878a2b
878a2b
    # Define long options.
878a2b
    local ARGSL='directory,regular-file,symbolic-link,execution,versioned,working-copy'
878a2b
878a2b
    # Initialize arguments with an empty value and set it as local
878a2b
    # variable to this function scope.
878a2b
    local ARGUMENTS=''
878a2b
878a2b
    # Initialize file variable as local to avoid conflicts outside
878a2b
    # this function scope. In the file variable will set the file path
878a2b
    # we'r going to verify.
878a2b
    local FILE=''
878a2b
878a2b
    # Redefine ARGUMENTS variable using current positional parameters. 
878a2b
    cli_parseArgumentsReDef "$@"
878a2b
878a2b
    # Redefine ARGUMENTS variable using getopt output.
878a2b
    cli_parseArguments
878a2b
878a2b
    # Redefine positional parameters using ARGUMENTS variable.
878a2b
    eval set -- "$ARGUMENTS"
878a2b
878a2b
    # Define list of files we want to apply verifications to.
878a2b
    local FILES=$(echo $@ | sed -r 's!^.*--[[:space:]](.+)$!\1!')
878a2b
878a2b
    # Verify files in the list. It is required at least one.
878a2b
    if [[ $FILES =~ '--$' ]];then
878a2b
        cli_printMessage "You need to provide one file at least." --as-error-line 
878a2b
    fi
878a2b
878a2b
    # Look for options passed through positional parameters.
878a2b
    while true; do
878a2b
878a2b
        case "$1" in
878a2b
878a2b
            -d|--directory )
878a2b
                for FILE in $(echo $FILES);do
878a2b
                    if [[ ! -d $FILE ]];then
878a2b
                        cli_printMessage "`eval_gettext "The directory \\\"\\\$FILE\\\" does not exist."`" --as-error-line
878a2b
                    fi
878a2b
                done
878a2b
                shift 1
878a2b
                ;;
878a2b
878a2b
            -f|--regular-file )
878a2b
                for FILE in $(echo $FILES);do
878a2b
                    if [[ ! -f $FILE ]];then
878a2b
                        cli_printMessage "`eval_gettext "The file \\\"\\\$FILE\\\" is not a regular file."`" --as-error-line
878a2b
                    fi
878a2b
                done
878a2b
                shift 1
878a2b
                ;;
878a2b
878a2b
            -h|--symbolic-link )
878a2b
                for FILE in $(echo $FILES);do
878a2b
                    if [[ ! -h $FILE ]];then
878a2b
                        cli_printMessage "`eval_gettext "The file \\\"\\\$FILE\\\" is not a symbolic link."`" --as-error-line
878a2b
                    fi
878a2b
                done
878a2b
                shift 1
878a2b
                ;;
878a2b
878a2b
            -n|--versioned )
878a2b
                for FILE in $(echo $FILES);do
878a2b
                    if [[ $(cli_isVersioned $FILE) == 'false' ]];then
878a2b
                        cli_printMessage "`eval_gettext "The path \\\"\\\$FILE\\\" is not versioned."`" --as-error-line
878a2b
                    fi
878a2b
                done
878a2b
                shift 1
878a2b
                ;;
878a2b
878a2b
            -x|--execution )
878a2b
                for FILE in $(echo $FILES);do
878a2b
                    if [[ ! -x $FILE ]];then
878a2b
                        cli_printMessage "`eval_gettext "The file \\\"\\\$FILE\\\" is not executable."`" --as-error-line
878a2b
                    fi
878a2b
                done
878a2b
                shift 1
878a2b
                ;;
878a2b
878a2b
            -w|--working-copy )
878a2b
                for FILE in $(echo $FILES);do
878a2b
                    if [[ ! $FILE =~ "^${CLI_WRKCOPY}/.+$" ]];then
878a2b
                        cli_printMessage "`eval_gettext "The path \\\"\\\$FILE\\\" does not exist inside the working copy."`" --as-error-line
878a2b
                    fi
878a2b
                done
878a2b
                shift 1
878a2b
                ;;
878a2b
878a2b
            -- )
878a2b
                for FILE in $(echo $FILES);do
878a2b
                    if [[ ! -a $FILE ]];then
878a2b
                        cli_printMessage "`eval_gettext "The path \\\"\\\$FILE\\\" does not exist."`" --as-error-line
878a2b
                    fi
878a2b
                done
878a2b
                shift 1
878a2b
                break
878a2b
                ;;
878a2b
878a2b
        esac
878a2b
    done
878a2b
878a2b
}