Blame Automation/Scripts/tcar_getFilesList.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado eb3010
#   tcar_getFilesList.sh -- This function standardizes the way list of
Alain Reguera Delgado 615395
#   files are built inside centos-art.sh script. This function outputs
Alain Reguera Delgado 615395
#   a sorted and unique list of files based on the options and
Alain Reguera Delgado 615395
#   locations passed as argument.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
#   Written by: 
Alain Reguera Delgado 615395
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 615395
#     Key fingerprint = D67D 0F82 4CBD 90BC 6421  DF28 7CCE 757C 17CA 3951
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# Copyright (C) 2009-2013 The CentOS Project
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 8f60cb
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 8f60cb
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 8f60cb
# your option) any later version.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 8f60cb
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 8f60cb
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 8f60cb
# General Public License for more details.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 8f60cb
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 8f60cb
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado eb3010
function tcar_getFilesList {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define short options.
Alain Reguera Delgado 8f60cb
    local ARGSS=''
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define long options.
Alain Reguera Delgado 8f60cb
    local ARGSL='pattern:,mindepth:,maxdepth:,type:,uid:'
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize pattern used to reduce the find output.
Alain Reguera Delgado 615395
    local PATTERN="${TCAR_FLAG_FILTER}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize options used with find command.
Alain Reguera Delgado 8f60cb
    local OPTIONS=''
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize arguments with an empty value and set it as local
Alain Reguera Delgado 8f60cb
    # variable to this function scope. Doing this is very important to
Alain Reguera Delgado 8f60cb
    # avoid any clash with higher execution environments.
Alain Reguera Delgado ddf7d2
    local TCAR_ARGUMENTS=''
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 615395
    # Process all arguments currently available in this function
Alain Reguera Delgado 615395
    # environment. If either ARGSS or ARGSL local variables have been
Alain Reguera Delgado 615395
    # defined, argument processing goes through getopt for validation.
Alain Reguera Delgado eb3010
    tcar_setArguments "${@}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado ddf7d2
    # Redefine positional parameters using TCAR_ARGUMENTS variable.
Alain Reguera Delgado ddf7d2
    eval set -- "${TCAR_ARGUMENTS}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    while true;do
Alain Reguera Delgado 615395
        case "${1}" in
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            --pattern )
Alain Reguera Delgado 615395
                PATTERN="${2}"
Alain Reguera Delgado 8f60cb
                shift 2
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            --maxdepth )
Alain Reguera Delgado 615395
                OPTIONS="${OPTIONS} -maxdepth ${2}"
Alain Reguera Delgado 8f60cb
                shift 2
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            --mindepth )
Alain Reguera Delgado 615395
                OPTIONS="${OPTIONS} -mindepth ${2}"
Alain Reguera Delgado 8f60cb
                shift 2
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            --type )
Alain Reguera Delgado 615395
                OPTIONS="${OPTIONS} -type ${2}"
Alain Reguera Delgado 8f60cb
                shift 2
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            --uid )
Alain Reguera Delgado 615395
                OPTIONS="${OPTIONS} -uid ${2}"
Alain Reguera Delgado 8f60cb
                shift 2
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -- )
Alain Reguera Delgado 8f60cb
                shift 1
Alain Reguera Delgado 8f60cb
                break
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
        esac
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # At this point all options arguments have been processed and
Alain Reguera Delgado 8f60cb
    # removed from positional parameters. Only non-option arguments
Alain Reguera Delgado 8f60cb
    # remain so we use them as source location for find command to
Alain Reguera Delgado 8f60cb
    # look files for.
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Verify that locations does exist.
Alain Reguera Delgado eb3010
    tcar_checkFiles -e ${@}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Redefine pattern as regular expression. When we use regular
Alain Reguera Delgado 8f60cb
    # expressions with find, regular expressions are evaluated against
Alain Reguera Delgado 8f60cb
    # the whole file path.  This way, when the regular expression is
Alain Reguera Delgado 8f60cb
    # specified, we need to build it in a way that matches the whole
Alain Reguera Delgado 8f60cb
    # path we are using. Doing so, every time we pass the `--filter'
Alain Reguera Delgado 8f60cb
    # option in the command-line could be a tedious task.  Instead, in
Alain Reguera Delgado 8f60cb
    # the sake of reducing some typing, we prepare the regular
Alain Reguera Delgado 8f60cb
    # expression here to match the whole path using the regular
Alain Reguera Delgado 615395
    # expression provided by the user as pattern. Do not use locations
Alain Reguera Delgado 615395
    # as part of regular expression so it could be possible to use
Alain Reguera Delgado 615395
    # path expansion.  Using path expansion reduce the amount of
Alain Reguera Delgado 615395
    # places to find out things and so the time required to finish the
Alain Reguera Delgado 615395
    # task.
Alain Reguera Delgado 8f60cb
    #
Alain Reguera Delgado 8f60cb
    # Don't do such path expansion here. Instead, do it when you call
Alain Reguera Delgado 8f60cb
    # this function. Otherwise you would be prohibiting the
Alain Reguera Delgado 8f60cb
    # application of exact patterns. 
Alain Reguera Delgado 8f60cb
    #PATTERN="^/.*${PATTERN}$"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define list of files to process. At this point we cannot verify
Alain Reguera Delgado 615395
    # whether the location is a directory or a file since path
Alain Reguera Delgado 8f60cb
    # expansion could be introduced to it. The best we can do is
Alain Reguera Delgado 8f60cb
    # verifying exit status and go on.
Alain Reguera Delgado 615395
    find ${@} -regextype posix-egrep ${OPTIONS} -regex "${PATTERN}" | sort | uniq
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}