Blame Scripts/Bash/Functions/Commons/cli_getFilesList.sh

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