Blame Scripts/centos-art/Functions/cli_checkFiles.sh

4c79b5
#!/bin/bash
4c79b5
#
b59d4d
# cli_checkFiles.sh -- This function standardizes the way file
b59d4d
# conditional expressions are applied inside centos-art.sh script.
b59d4d
# Here is where we answer questions like: is the file a regular file
b59d4d
# or a directory?  or, is it a symbolic link? or even, does it have
b59d4d
# execution rights, etc.  If the verification fails somehow at any
b59d4d
# point, an error message is output and centos-art.sh script ends its
b59d4d
# execution. 
b76c02
#
b59d4d
# More than one file can be passed to this function, so we want to
b59d4d
# process them all as specified by the options. Since we are using
b59d4d
# getopt output it is possible to determine where options and
b59d4d
# non-option arguments are in the list of arguments  (e.g., options
b59d4d
# are on the left side of ` -- ' and non-options on the rigth side of
b59d4d
# ` -- '). Non-options are the files we want to verify and options how
b59d4d
# we want to verify them.
b76c02
#
b59d4d
# Another issue to consider, when more than one file is passed to this
b59d4d
# function, is that we cannot shift positional parameters as we
b59d4d
# frequently do whe just one argument is passsed, doing so would
b59d4d
# annulate the validation for the second and later files passed to the
b59d4d
# function. So, in order to provide verification to all files passed
b59d4d
# to the function, the verification loop must be set individual for
b59d4d
# each option in this function.
b59d4d
#
b59d4d
# Assuming no option be passed to the function, a general verification
b59d4d
# is performed to determine whether or not the file exists without
b59d4d
# considering the file type just its existence.
4c79b5
#
2fe9b7
# Copyright (C) 2009, 2010, 2011 The CentOS Project
fa95b1
#
fa95b1
# This program is free software; you can redistribute it and/or modify
fa95b1
# it under the terms of the GNU General Public License as published by
dcd347
# the Free Software Foundation; either version 2 of the License, or (at
dcd347
# your option) any later version.
fa95b1
#
74a058
# This program is distributed in the hope that it will be useful, but
74a058
# WITHOUT ANY WARRANTY; without even the implied warranty of
4c79b5
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c79b5
# General Public License for more details.
4c79b5
#
4c79b5
# You should have received a copy of the GNU General Public License
4c79b5
# along with this program; if not, write to the Free Software
dcd347
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
7ac5a5
#
4c79b5
# ----------------------------------------------------------------------
418249
# $Id$
4c79b5
# ----------------------------------------------------------------------
4c79b5
4c79b5
function cli_checkFiles {
4c79b5
b59d4d
    # Define short options.
2a7de9
    local ARGSS='d,r,h,n,x,w'
b59d4d
b59d4d
    # Define long options.
2a7de9
    local ARGSL='directory,regular-file,symbolic-link,execution,versioned,working-copy'
b59d4d
b59d4d
    # Initialize arguments with an empty value and set it as local
b59d4d
    # variable to this function scope.
b59d4d
    local ARGUMENTS=''
b59d4d
b59d4d
    # Initialize file variable as local to avoid conflicts outside
b59d4d
    # this function scope. In the file variable will set the file path
b59d4d
    # we'r going to verify.
288be2
    local FILE=''
288be2
b59d4d
    # Redefine ARGUMENTS variable using current positional parameters. 
793c4c
    cli_parseArgumentsReDef "$@"
b59d4d
b59d4d
    # Redefine ARGUMENTS variable using getopt output.
793c4c
    cli_parseArguments
b59d4d
b59d4d
    # Redefine positional parameters using ARGUMENTS variable.
b59d4d
    eval set -- "$ARGUMENTS"
b59d4d
728eac
    # Define list of files we want to apply verifications to.
728eac
    local FILES=$(echo $@ | sed -r 's!^.*--[[:space:]](.+)$!\1!')
728eac
728eac
    # Verify files in the list. It is required at least one.
2a7de9
    if [[ $FILES =~ '--$' ]];then
728eac
        cli_printMessage "You need to provide one file at least." --as-error-line 
728eac
    fi
728eac
b59d4d
    # Look for options passed through positional parameters.
b59d4d
    while true; do
b59d4d
b59d4d
        case "$1" in
288be2
b59d4d
            -d|--directory )
728eac
                for FILE in $(echo $FILES);do
b59d4d
                    if [[ ! -d $FILE ]];then
b59d4d
                        cli_printMessage "`eval_gettext "The directory \\\"\\\$FILE\\\" does not exist."`" --as-error-line
b59d4d
                    fi
b59d4d
                done
b59d4d
                shift 1
288be2
                ;;
4c79b5
b59d4d
            -f|--regular-file )
728eac
                for FILE in $(echo $FILES);do
b59d4d
                    if [[ ! -f $FILE ]];then
b59d4d
                        cli_printMessage "`eval_gettext "The file \\\"\\\$FILE\\\" is not a regular file."`" --as-error-line
b59d4d
                    fi
b59d4d
                done
b59d4d
                shift 1
288be2
                ;;
288be2
b59d4d
            -h|--symbolic-link )
728eac
                for FILE in $(echo $FILES);do
288be2
                    if [[ ! -h $FILE ]];then
b59d4d
                        cli_printMessage "`eval_gettext "The file \\\"\\\$FILE\\\" is not a symbolic link."`" --as-error-line
288be2
                    fi
b59d4d
                done
b59d4d
                shift 1
288be2
                ;;
288be2
2a7de9
            -n|--versioned )
2a7de9
                for FILE in $(echo $FILES);do
c1289b
                    if [[ $(cli_isVersioned $FILE) == 'false' ]];then
d07c7e
                        cli_printMessage "`eval_gettext "The path \\\"\\\$FILE\\\" is not versioned."`" --as-error-line
2a7de9
                    fi
2a7de9
                done
2a7de9
                shift 1
2a7de9
                ;;
2a7de9
b59d4d
            -x|--execution )
728eac
                for FILE in $(echo $FILES);do
b59d4d
                    if [[ ! -x $FILE ]];then
b59d4d
                        cli_printMessage "`eval_gettext "The file \\\"\\\$FILE\\\" is not executable."`" --as-error-line
288be2
                    fi
b59d4d
                done
b59d4d
                shift 1
288be2
                ;;
288be2
b59d4d
            -w|--working-copy )
728eac
                for FILE in $(echo $FILES);do
32bf98
                    if [[ ! $FILE =~ "^${CLI_WRKCOPY}/.+$" ]];then
b59d4d
                        cli_printMessage "`eval_gettext "The path \\\"\\\$FILE\\\" does not exist inside the working copy."`" --as-error-line
b59d4d
                    fi
b59d4d
                done
b59d4d
                shift 1
288be2
                ;;
288be2
b59d4d
            -- )
728eac
                for FILE in $(echo $FILES);do
b59d4d
                    if [[ ! -a $FILE ]];then
b59d4d
                        cli_printMessage "`eval_gettext "The path \\\"\\\$FILE\\\" does not exist."`" --as-error-line
b59d4d
                    fi
b59d4d
                done
b59d4d
                shift 1
b59d4d
                break
b59d4d
                ;;
288be2
288be2
        esac
288be2
    done
4c79b5
4c79b5
}