Blame Scripts/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
#
2d3646
# 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.
b59d4d
    local ARGSS='d,r,h,x,w'
b59d4d
b59d4d
    # Define long options.
b59d4d
    local ARGSL='directory,regular-file,symbolic-link,execution,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. 
b59d4d
    cli_doParseArgumentsReDef "$@"
b59d4d
b59d4d
    # Redefine ARGUMENTS variable using getopt output.
b59d4d
    cli_doParseArguments
b59d4d
b59d4d
    # Redefine positional parameters using ARGUMENTS variable.
b59d4d
    eval set -- "$ARGUMENTS"
b59d4d
b59d4d
    # Look for options passed through positional parameters.
b59d4d
    while true; do
b59d4d
b59d4d
        case "$1" in
288be2
b59d4d
            -d|--directory )
5b718f
                for FILE in $(echo $@ | sed -r 's!^.*[[:space:]]*--[[:space:]]+!!');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 )
5b718f
                for FILE in $(echo $@ | sed -r 's!^.*[[:space:]]*--[[:space:]]+!!');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 )
5b718f
                for FILE in $(echo $@ | sed -r 's!^.*[[:space:]]*--[[:space:]]+!!');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
b59d4d
            -x|--execution )
5b718f
                for FILE in $(echo $@ | sed -r 's!^.*[[:space:]]*--[[:space:]]+!!');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 )
5b718f
                for FILE in $(echo $@ | sed -r 's!^.*[[:space:]]*--[[:space:]]+!!');do
b59d4d
                    if [[ ! $FILE =~ "^${HOME}/artwork/.+$" ]];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
            -- )
5b718f
                for FILE in $(echo $@ | sed -r 's!^.*[[:space:]]*--[[:space:]]+!!');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
}