<commit_before>#!/bin/bash

INCOMING_DIR="$1"
INFECTED_DIR="$2"
CLEAN_DIR="$3"
CLAMSCAN_CMD="/opt/clamav/bin/clamscan"
START_TIME=`date +%s`

usage() {
  echo "USAGE: $0 <incoming_dir> <infected_dir> [<clean_dir>]"
  echo
  echo "Purpose:"
  echo "Scans <incoming_dir> and moves infected files to <infected_dir>. Optionally"
  echo "moves clean files to <clean_dir>."
  echo
  exit 1
}

if [ $# -lt 2 ]; then
  usage
fi

make_result_file() {
  if [ -n "`which tempfile`" ]; then
    RESULTFILE=`tempfile -p CLAM-`
  elif [ -n "`which mktemp`" ]; then
    RESULTFILE=`mktemp -t CLAM-`
  else
    echo "ERROR: no mktemp or tempname command"
    exit 1
  fi
}

check_dir_exists() {
  if [ ! -d "$1" ] || [ ! -w "$1" ]; then
    echo "ERROR: directory '$1' does not exist or is not writable"
    exit 1
  fi
}

logger -t virus_scan "virus scan in '${INCOMING_DIR}' starting"

check_dir_exists $INCOMING_DIR
check_dir_exists $INFECTED_DIR
if [ -n "$CLEAN_DIR" ]; then
  check_dir_exists $CLEAN_DIR
fi

set -o errexit # fail on errors
pushd "$INCOMING_DIR" > /dev/null 2>&1
set +o errexit # disable fail on errors

make_result_file
$CLAMSCAN_CMD --no-summary --stdout -r . > "$RESULTFILE"
set -o errexit # enable fail on errors

grep 'FOUND$' "$RESULTFILE" | sed 's/: [^:]* FOUND$//' | rsync --remove-source-files --files-from=- . "$INFECTED_DIR/."
if [ -n "$CLEAN_DIR" ]; then
  grep ': OK$' "$RESULTFILE" | sed 's/: OK$//' | rsync --remove-source-files --files-from=- . "$CLEAN_DIR/."
fi

COUNT_FOUND=$(grep -c 'FOUND$' "$RESULTFILE")
COUNT_CLEAN=$(grep -c ': OK$' "$RESULTFILE")<commit_msg>virus_scan catch non-zero exits from grep -c<commit_after>#!/bin/bash

INCOMING_DIR="$1"
INFECTED_DIR="$2"
CLEAN_DIR="$3"
CLAMSCAN_CMD="/opt/clamav/bin/clamscan"
START_TIME=`date +%s`

usage() {
  echo "USAGE: $0 <incoming_dir> <infected_dir> [<clean_dir>]"
  echo
  echo "Purpose:"
  echo "Scans <incoming_dir> and moves infected files to <infected_dir>. Optionally"
  echo "moves clean files to <clean_dir>."
  echo
  exit 1
}

if [ $# -lt 2 ]; then
  usage
fi

make_result_file() {
  if [ -n "`which tempfile`" ]; then
    RESULTFILE=`tempfile -p CLAM-`
  elif [ -n "`which mktemp`" ]; then
    RESULTFILE=`mktemp -t CLAM-`
  else
    echo "ERROR: no mktemp or tempname command"
    exit 1
  fi
}

check_dir_exists() {
  if [ ! -d "$1" ] || [ ! -w "$1" ]; then
    echo "ERROR: directory '$1' does not exist or is not writable"
    exit 1
  fi
}

logger -t virus_scan "virus scan in '${INCOMING_DIR}' starting"

check_dir_exists $INCOMING_DIR
check_dir_exists $INFECTED_DIR
if [ -n "$CLEAN_DIR" ]; then
  check_dir_exists $CLEAN_DIR
fi

set -o errexit # fail on errors
pushd "$INCOMING_DIR" > /dev/null 2>&1
set +o errexit # disable fail on errors

make_result_file
$CLAMSCAN_CMD --no-summary --stdout -r . > "$RESULTFILE"
set -o errexit # enable fail on errors

grep 'FOUND$' "$RESULTFILE" | sed 's/: [^:]* FOUND$//' | rsync --remove-source-files --files-from=- . "$INFECTED_DIR/."
if [ -n "$CLEAN_DIR" ]; then
  grep ': OK$' "$RESULTFILE" | sed 's/: OK$//' | rsync --remove-source-files --files-from=- . "$CLEAN_DIR/."
fi

COUNT_FOUND=$(grep -c 'FOUND$' "$RESULTFILE" || true)
COUNT_CLEAN=$(grep -c ': OK$' "$RESULTFILE" || true)
{'fraction_non_alphanumeric': 0.16827072337783297, 'fraction_numerical': 0.008072027320707855, 'mean_word_length': 3.662807525325615, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '927669', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1272, 'n_words': 353}