Blame Scripts/Functions/cli_checkPathComponent.sh

64e627
#!/bin/bash
64e627
#
64e627
# cli_checkPathComponent.sh -- This function checks parts/components
0c6cdd
# from repository paths.
64e627
#
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
64e627
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
64e627
# General Public License for more details.
64e627
#
64e627
# You should have received a copy of the GNU General Public License
64e627
# along with this program; if not, write to the Free Software
dcd347
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
7ac5a5
#
64e627
# ----------------------------------------------------------------------
7b46a4
# $Id$
64e627
# ----------------------------------------------------------------------
64e627
64e627
function cli_checkPathComponent {
64e627
0c6cdd
    # Define short options.
0c6cdd
    local ARGSS=''
0c6cdd
0c6cdd
    # Define long options.
0c6cdd
    local ARGSL='release,architecture,theme'
0c6cdd
0c6cdd
    # Initialize arguments with an empty value and set it as local
0c6cdd
    # variable to this function scope.
0c6cdd
    local ARGUMENTS=''
0c6cdd
0c6cdd
    # Initialize file variable as local to avoid conflicts outside
0c6cdd
    # this function scope. In the file variable will set the file path
0c6cdd
    # we are going to verify.
0c6cdd
    local FILE=''
0c6cdd
0c6cdd
    # Redefine ARGUMENTS variable using current positional parameters. 
0c6cdd
    cli_doParseArgumentsReDef "$@"
0c6cdd
0c6cdd
    # Redefine ARGUMENTS variable using getopt output.
0c6cdd
    cli_doParseArguments
0c6cdd
0c6cdd
    # Redefine positional parameters using ARGUMENTS variable.
0c6cdd
    eval set -- "$ARGUMENTS"
0c6cdd
0c6cdd
    # Define list of locations we want to apply verifications to.
0c6cdd
    local FILES=$(echo $@ | sed -r 's!^.*--[[:space:]](.+)$!\1!')
64e627
0c6cdd
    # Verify list of locations, it is required that one location be
0c6cdd
    # present in the list and also be a valid file.
0c6cdd
    if [[ $FILES == '--' ]];then
0c6cdd
        cli_printMessage "You need to provide one file at least." --as-error-line 
64e627
    fi
64e627
0c6cdd
    # Look for options passed through positional parameters.
0c6cdd
    while true; do
0c6cdd
0c6cdd
        case "$1" in
0c6cdd
0c6cdd
            --release )
0c6cdd
                for FILE in $(echo $FILES);do
6667dc
                    if [[ ! $FILE =~ "^.+/$(cli_getPathComponent --release-pattern)/.*$" ]];then
0c6cdd
                        cli_printMessage "`eval_gettext "The release \\\"\\\$FILE\\\" is not valid."`" --as-error-line
0c6cdd
                    fi
0c6cdd
                done
0c6cdd
                shift 2
0c6cdd
                break
0c6cdd
                ;;
0c6cdd
0c6cdd
            --architecture )
0c6cdd
                for FILE in $(echo $FILES);do
6667dc
                    if [[ ! $FILE =~ $(cli_getPathComponent --architecture-pattern) ]];then
0c6cdd
                        cli_printMessage "`eval_gettext "The architecture \\\"\\\$FILE\\\" is not valid."`" --as-error-line
0c6cdd
                    fi
0c6cdd
                done
0c6cdd
                shift 2
0c6cdd
                break
0c6cdd
                ;;
0c6cdd
0c6cdd
            --theme )
0c6cdd
                for FILE in $(echo $FILES);do
6667dc
                    if [[ ! $FILE =~ $(cli_getPathComponent --theme-pattern) ]];then
0c6cdd
                        cli_printMessage "`eval_gettext "The theme \\\"\\\$FILE\\\" is not valid."`" --as-error-line
0c6cdd
                    fi
0c6cdd
                done
0c6cdd
                shift 2
0c6cdd
                break
0c6cdd
                ;;
0c6cdd
0c6cdd
            -- )
0c6cdd
                for FILE in $(echo $FILES);do
0c6cdd
                    if [[ $FILE == '' ]] \
0c6cdd
                        || [[ $FILE =~ '(\.\.(/)?)' ]] \
0c6cdd
                        || [[ ! $FILE =~ '^[A-Za-z0-9\.:/_-]+$' ]]; then 
0c6cdd
                        cli_printMessage "`eval_gettext "The value \\\"\\\$FILE\\\" is not valid."`" --as-error-line
0c6cdd
                    fi
0c6cdd
                done
0c6cdd
                shift 2
0c6cdd
                break
0c6cdd
                ;;
0c6cdd
0c6cdd
        esac
0c6cdd
    done
0c6cdd
64e627
}