Blame tcar-scripts/tcar_getFilesList.sh

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