Skip to content

Commit

Permalink
beefind: some KISSing
Browse files Browse the repository at this point in the history
now beefind just scans a directory and prints a file list and not the files metadata
  • Loading branch information
Tobias Dreyer committed May 22, 2012
1 parent f49a331 commit 78cba40
Showing 1 changed file with 16 additions and 105 deletions.
121 changes: 16 additions & 105 deletions src/beefind.sh.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#
# beefind - scan directory and print files and metadata
# beefind - scan directory and print files
#
# Copyright (C) 2009-2012
# Tobias Dreyer <dreyer@molgen.mpg.de>
Expand Down Expand Up @@ -31,77 +31,30 @@ BEE_VERSION="@BEE_VERSION@"

: ${BEEVERSION:=${BEE_BINDIR}/beeversion}
: ${BEESEP=${BEE_BINDIR}/beesep}
: ${BEEGETOPT:=${BEE_BINDIR}/beegetopt}

function usage() {
cat <<-EOF
beefind v${BEE_VERSION} 2009-2012
by Tobias Dreyer and Marius Tolzmann <{dreyer,tolzmann}@molgen.mpg.de>
Max Planck Institute for Molecular Genetics Berlin Dahlem
Usage: beefind [options] <files>
Usage: beefind [options] <directory>
Options:
-c | --cutroot <path> strip <path> from displayed files
-e | --exclude <pattern> ignore files matching <pattern>
-s | --skiplist <file> ignore files matching pattern described in <file>
-c, --cutroot <path> strip <path> from printed files
-e, --exclude <pattern> ignore files matching <pattern>
-s, --skiplist <file> ignore files matching pattern described in <file>
-o | --output <file> output is redirected into <file> instead of <stdout>
-d | --dump-files <file> read <file> and just display files; <file> must contain
beefind output
-o, --output <file> output is redirected into <file> instead of <stdout>
-h | --help display this help
-h, --help display this help
EOF
}

function dump_files() {
local infile=${1}

if [ ! -r "${infile}" ] ; then
print_warning "cannot read file '${infile}'"
exit 1
fi

for line in $(cat ${infile}) ; do
eval $(${BEESEP} ${line})

echo ${file%//*}
done
}

function get_md5() {
local file=${1}

if [ -L "${file}" ] ; then
md5='link'
elif [ -d "${file}" ] ; then
md5='directory'
elif [ -f "${file}" ] ; then
md5=$(md5sum ${file})
md5=${md5%% *}
if [ "$?" != 0 -o -z "${md5}" ] ; then
print_warning "failed to create md5 sum for ${file}"
md5='#MD5ERROR#'
fi
elif [ -p "${file}" ] ; then
md5='pipe'
elif [ -S "${file}" ] ; then
md5='socket'
elif [ -b "${file}" ] ; then
md5='block'
elif [ -c "${file}" ] ; then
md5='char'
elif [ -S "${file}" ] ; then
md5='socket'
else
md5='#MD5UNKNOWN#'
fi

echo ${md5}
}

function bee_find() {
local options=$(${BEE_BINDIR}/beegetopt --name beefind \
local options=$(${BEEGETOPT} --name beefind \
--option exclude/e= \
--option skiplist/s= \
-- "${@}")
Expand All @@ -114,7 +67,7 @@ function bee_find() {
eval set -- "${options}"

declare -a OPT_EXCLUDE
declare -a OPT_SKIPLSIT
declare -a OPT_SKIPLIST

while true ; do
case "${1}" in
Expand All @@ -134,7 +87,7 @@ function bee_find() {
done

if [ -z "${1}" ] ; then
print_warning "no directory to scan specified"
usage
exit 1
fi

Expand All @@ -151,7 +104,7 @@ function bee_find() {
}

function do_beefind() {
local options=$(${BEE_BINDIR}/beegetopt --name beefind \
local options=$(${BEEGETOPT} --name beefind \
--option cutroot/c= \
-- "${@}")

Expand All @@ -177,44 +130,17 @@ function do_beefind() {
esac
done

for f in $(cat <(bee_find ${@})) ; do
md5=$(get_md5 ${f})

data=( $(stat -c "%05a %h %u %g %s %Y" ${f}) )
if [ "$?" != 0 -o -z "${data}" ] ; then
print_warning "can't stat ${f}"
exit 1
fi

echo -n "md5=${md5}:"
echo -n "omode=${data[0]}:"
echo -n "nlink=${data[1]}:"
echo -n "uid=${data[2]}:"
echo -n "gid=${data[3]}:"
echo -n "size=${data[4]}:"
echo -n "mtime=${data[5]}:"

filename=${f}
if [ -n "${OPT_CUTROOT}" ] ; then
filename=${f#${OPT_CUTROOT}}
fi
echo -n "file=${filename}"

if [ "${md5}" = "link" ] ; then
echo -n "//$(readlink ${f})"
fi

echo ""
done
while read line ; do
echo ${line#${OPT_CUTROOT}}
done < <(bee_find ${@})
}

###############################################################################
###############################################################################
###############################################################################

options=$(${BEE_BINDIR}/beegetopt --name beefind \
options=$(${BEEGETOPT} --name beefind \
--option output/o= \
--option dump-files/d= \
--option help/h \
-- "${@}")

Expand All @@ -226,18 +152,13 @@ fi
eval set -- "${options}"

declare OPT_OUTPUT=
declare OPT_DUMPFILES=

while true ; do
case "${1}" in
--output)
OPT_OUTPUT="${2}"
shift 2
;;
--dump-files)
OPT_DUMPFILES="${2}"
shift 2
;;
--help)
usage
exit 0
Expand All @@ -256,14 +177,4 @@ if [ -n "${OPT_OUTPUT}" ] ; then
fi
fi

if [ -n "${OPT_DUMPFILES}" ] ; then
if [ ! -r "${OPT_DUMPFILES}" ] ; then
print_warning "cannot read file '${OPT_DUMPFILES}'"
exit 1
fi

dump_files ${OPT_DUMPFILES}
exit 0
fi

do_beefind ${@}

0 comments on commit 78cba40

Please sign in to comment.