#!/bin/bash

set -e

FILENAME=$1

set -u

START_TIME=$(date +%s)

usage() {
  echo "USAGE: $0 <path_to_file>"
  echo
  echo "Purpose:"
  echo "Scans specified file and exits conditionally, 0 for clean, 1 for dirty"
  echo
  exit 1
}

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

if [ ! -f "$FILENAME" ]; then
  logger -t virus_scan "ERROR: file '$FILENAME' does not exist"
  exit 1
fi

logger -t virus_scan "Starting scan on $FILENAME"

set +e
clamscan --quiet $FILENAME
EXITCODE=$?
set -e

TIME_TAKEN=$(($(date +%s)-START_TIME))

if [ $EXITCODE == 0 ]; then
  logger -t virus_scan "File $FILENAME clean; took $TIME_TAKEN seconds"
else
  logger -t virus_scan "File $FILENAME infected; took $TIME_TAKEN seconds"
fi

exit $EXITCODE

#!/bin/bash

set -e

FILENAME=$1

set -u

START_TIME=$(date +%s)

usage() {
  echo "USAGE: $0 <path_to_file>"
  echo
  echo "Purpose:"
  echo "Scans specified file and exits conditionally, 0 for clean, 1 for dirty"
  echo
  exit 1
}

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

if [ ! -f "$FILENAME" ]; then
  logger -t virus_scan "ERROR: file '$FILENAME' does not exist"
  exit 1
fi

logger -t virus_scan "Starting scan on $FILENAME"

set +e
clamdscan --fdpass --quiet $FILENAME
EXITCODE=$?
set -e

TIME_TAKEN=$(($(date +%s)-START_TIME))

if [ $EXITCODE == 0 ]; then
  logger -t virus_scan "File $FILENAME clean; took $TIME_TAKEN seconds"
else
  logger -t virus_scan "File $FILENAME infected; took $TIME_TAKEN seconds"
fi

exit $EXITCODE

Use clamd to scan files
Use clamd to scan files

Currently, for every file we scan for viruses, we boot a new instance
of `clamscan`. This means it has to load the entire virus definition
database each time, and each file takes a minimum of 11 seconds to scan.

`clamd` is a server version of `clamscan`. It keeps the definition
database in memory and has the file passed to it from `clamdscan`,
which acts similarly to `clamscan`. We’ve tried this approach before
but we didn’t use `--fdpass`. This made things fail as `clamd` couldn’t
access the files we were trying to scan. Specifying `--fdpass` passes
the file descriptor from `clamdscan` (which does have access) to
`clamd`.

This should bring the per file scan time down from ~11-14 seconds to
~1-2 seconds.
