Blame Scripts/tcar_getFilesList.sh

Alain Reguera Delgado 66223d
#!/bin/bash
Alain Reguera Delgado 66223d
######################################################################
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   tcar - The CentOS Artwork Repository automation tool.
Alain Reguera Delgado b33753
#   Copyright © 2014 The CentOS Artwork SIG
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   This program is free software; you can redistribute it and/or
Alain Reguera Delgado b33753
#   modify it under the terms of the GNU General Public License as
Alain Reguera Delgado b33753
#   published by the Free Software Foundation; either version 2 of the
Alain Reguera Delgado b33753
#   License, or (at your option) any later version.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   This program is distributed in the hope that it will be useful,
Alain Reguera Delgado b33753
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado b33753
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado b33753
#   General Public License for more details.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   You should have received a copy of the GNU General Public License
Alain Reguera Delgado b33753
#   along with this program; if not, write to the Free Software
Alain Reguera Delgado b33753
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   Alain Reguera Delgado <al@centos.org.cu>
Alain Reguera Delgado b33753
#   39 Street No. 4426 Cienfuegos, Cuba.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
######################################################################
Alain Reguera Delgado 66223d
Alain Reguera Delgado b33753
# Standardize the way list of files are built inside tcar.sh script.
Alain Reguera Delgado b33753
# This function outputs a sorted and unique list of files based on the
Alain Reguera Delgado b33753
# options and locations passed as argument.
Alain Reguera Delgado 66223d
function tcar_getFilesList {
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 1b4ebe
    OPTIND=1
Alain Reguera Delgado 1b4ebe
    while getopts "p:,a:,i:,t:,u:" OPTION "${@}"; do
Alain Reguera Delgado 66223d
Alain Reguera Delgado 1b4ebe
        case "${OPTION}" in
Alain Reguera Delgado 1b4ebe
            p )
Alain Reguera Delgado 1b4ebe
                PATTERN="${OPTARG}"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 1b4ebe
            a )
Alain Reguera Delgado 1b4ebe
                OPTIONS="${OPTIONS} -maxdepth ${OPTARG}"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 1b4ebe
            i )
Alain Reguera Delgado 1b4ebe
                OPTIONS="${OPTIONS} -mindepth ${OPTARG}"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 1b4ebe
            t )
Alain Reguera Delgado 1b4ebe
                OPTIONS="${OPTIONS} -type ${OPTARG}"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 1b4ebe
            u )
Alain Reguera Delgado 1b4ebe
                OPTIONS="${OPTIONS} -uid ${OPTARG}"
Alain Reguera Delgado 66223d
                ;;
Alain Reguera Delgado 66223d
        esac
Alain Reguera Delgado 1b4ebe
Alain Reguera Delgado 66223d
    done
Alain Reguera Delgado 66223d
Alain Reguera Delgado 1b4ebe
    # Clean up positional parameters to reflect the fact that options
Alain Reguera Delgado 1b4ebe
    # have been processed already.
Alain Reguera Delgado 1b4ebe
    shift $(( ${OPTIND} - 1 ))
Alain Reguera Delgado 1b4ebe
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
}