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

878a2b
#!/bin/bash
878a2b
#
6dce51
# cli_getFilesList.sh -- This function outputs a sorted and unique
6dce51
# list of files to process.
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
878a2b
    # path. Doing so, everytime we pass the `--filter' option in the
878a2b
    # command-line could be a tedious task.  Instead, in the sake of
878a2b
    # reducing some typing, we prepare the regular expression here to
878a2b
    # match the whole path using the regular expression provided by
878a2b
    # the user as pattern. Do not use LOCATION variable as part of
878a2b
    # regular expresion so it could be possible to use path expansion.
878a2b
    # Using path expansion reduce the amount of places to find out
878a2b
    # things and so the time required to finish the task.
cf6966
    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
878a2b
    # expansion coul 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
}