Refactor groomer.sh to use functions

pull/58/head
Dan Puttick 2017-08-10 11:51:05 -04:00
parent dd427b8c8e
commit 6845f2b607
3 changed files with 75 additions and 59 deletions

View File

@ -1,19 +1,7 @@
#!/bin/bash #!/bin/bash
# set -e (exit when a line returns non-0 status) and -x (xtrace) flags
set -e
set -x
# Import constants from config file
source ./config.sh
if ! [ "${ID}" -ge "1000" ]; then
echo "GROOMER: groomer.sh cannot run as root."
exit
fi
clean(){ clean(){
if [ ${DEBUG} = true ]; then if [ "${DEBUG}" = true ]; then
sleep 20 sleep 20
fi fi
@ -21,58 +9,84 @@ clean(){
${SYNC} ${SYNC}
# Remove temporary files from destination key # Remove temporary files from destination key
rm -rf ${TEMP} rm -rf "${TEMP}"
} }
trap clean EXIT TERM INT check_not_root() {
if ! [ "${ID}" -ge "1000" ]; then
echo "GROOMER: groomer.sh cannot run as root."
exit
fi
}
# Find the partition names on the device available at /dev/source_key check_partitions_not_empty () {
DEV_PARTITIONS=`ls "${DEV_SRC}"* | grep "${DEV_SRC}[1-9][0-6]*" || true` local partitions=$1
if [ -z "${DEV_PARTITIONS}" ]; then if [ -z "${partitions}" ]; then
echo "GROOMER: ${DEV_SRC} does not have any partitions." echo "GROOMER: ${DEV_SRC} does not have any partitions."
exit exit
fi fi
}
PARTCOUNT=1 unmount_source_partition() {
for partition in ${DEV_PARTITIONS}
do
echo "GROOMER: Processing partition ${partition}"
# Unmount anything that is mounted on /media/src # Unmount anything that is mounted on /media/src
if [ `${MOUNT} | grep -c ${SRC}` -ne 0 ]; then if [ "$(${MOUNT} | grep -c "${SRC}")" -ne 0 ]; then
${PUMOUNT} ${SRC} ${PUMOUNT} "${SRC}"
fi fi
}
# Mount the current partition in write mode run_groomer() {
${PMOUNT} -w ${partition} ${SRC} local dev_partitions
# Mark any autorun.inf files as dangerous on the source device # Find the partition names on the device
ls ${SRC_MNT} | grep -i autorun.inf | xargs -I {} mv "${SRC_MNT}"/{} "{SRC_MNT}"/DANGEROUS_{}_DANGEROUS || true let dev_partitions=$(ls "${DEV_SRC}"* | grep "${DEV_SRC}[1-9][0-6]*" || true)
# Unmount and remount the current partition in read-only mode check_has_partitions dev_partitions
${PUMOUNT} ${SRC} local partcount=1
${PMOUNT} -r ${partition} ${SRC} local partition
if [ ${?} -ne 0 ]; then for partition in ${dev_partitions}
# Previous command (mounting current partition) failed do
echo "GROOMER: Unable to mount ${partition} on /media/${SRC}" echo "GROOMER: Processing partition ${partition}"
else unmount_source_partition
echo "GROOMER: ${partition} mounted at /media/${SRC}" # Mount the current partition in write mode
${PMOUNT} -w ${partition} "${SRC}"
# Mark any autorun.inf files as dangerous on the source device to be extra careful
ls "${SRC_MNT}" | grep -i autorun.inf | xargs -I {} mv "${SRC_MNT}"/{} "{SRC_MNT}"/DANGEROUS_{}_DANGEROUS || true
# Unmount and remount the current partition in read-only mode
${PUMOUNT} "${SRC}"
# Put the filenames from the current partition in a logfile if ${PMOUNT} -r "${partition}" "${SRC}"; then
find "/media/${SRC}" -fls "${LOGS_DIR}/contents_partition_${PARTCOUNT}.txt" echo "GROOMER: ${partition} mounted at /media/${SRC}"
# Create a directory on ${DST} named PARTION_$PARTCOUNT # Put the filenames from the current partition in a logfile
target_dir="/media/${DST}/FROM_PARTITION_${PARTCOUNT}" find "/media/${SRC}" -fls "${LOGS_DIR}/contents_partition_${partcount}.txt"
mkdir -p "${target_dir}"
LOGFILE="${LOGS_DIR}/processing_log.txt"
# Run the current partition through filecheck.py # Create a directory on ${DST} named PARTION_$PARTCOUNT
echo "==== Starting processing of /media/${SRC} to ${target_dir}. ====" >> ${LOGFILE} local target_dir="/media/${DST}/FROM_PARTITION_${partcount}"
filecheck.py --source /media/${SRC} --destination ${target_dir} || true mkdir -p "${target_dir}"
echo "==== Done with /media/${SRC} to ${target_dir}. ====" >> ${LOGFILE} local logfile="${LOGS_DIR}/processing_log.txt"
# List destination files (recursively) for debugging # Run the current partition through filecheck.py
ls -lR "${target_dir}" echo "==== Starting processing of /media/${SRC} to ${target_dir}. ====" >> "${logfile}"
filecheck.py --source /media/"${SRC}" --destination "${target_dir}" || true
echo "==== Done with /media/${SRC} to ${target_dir}. ====" >> "${logfile}"
# List destination files (recursively) for debugging
ls -lR "${target_dir}"
else
# Previous command (mounting current partition) failed
echo "GROOMER: Unable to mount ${partition} on /media/${SRC}"
fi
let partcount=$((partcount + 1))
done
}
main() {
set -eu # exit when a line returns non-0 status, treat unset variables as errors
trap clean EXIT TERM INT # run clean when the script ends or is interrupted
source ./config.sh # get config values
if [ "${DEBUG}" = true ]; then
set -x
fi fi
let PARTCOUNT=`expr $PARTCOUNT + 1` run_groomer
done }
# The cleanup is automatically done in the function clean called when main
# the program exits

View File

@ -36,10 +36,10 @@ main() {
set -eu # exit when a line returns non-0 status, treat unset variables as errors set -eu # exit when a line returns non-0 status, treat unset variables as errors
trap clean EXIT TERM INT # run clean when the script ends or is interrupted trap clean EXIT TERM INT # run clean when the script ends or is interrupted
source ./config.sh # get config values source ./config.sh # get config values
check_is_root
if [ "${DEBUG}" = true ]; then if [ "${DEBUG}" = true ]; then
set -x set -x
fi fi
check_is_root
if [ "${MUSIC}" = true ]; then if [ "${MUSIC}" = true ]; then
start_music start_music
fi fi

View File

@ -61,9 +61,11 @@ prepare_dest_partition() {
main() { main() {
set -eu # exit when a line returns non-0 status, treat unset variables as errors set -eu # exit when a line returns non-0 status, treat unset variables as errors
trap clean EXIT TERM INT trap clean EXIT TERM INT # run clean when the script ends or is interrupted
set -x source ./config.sh # get config values
source ./config.sh if [ "${DEBUG}" = true ]; then
set -x
fi
check_not_root check_not_root
check_source_exists check_source_exists
check_dest_exists check_dest_exists