<commit_before>#!/bin/bash

: ${TRAVIS:="false"}
: ${FAST:="false"}
: ${test:=""}

iter=10

if [ "$TRAVIS" != "false" ]; then
    set -o xtrace
    iter=1
fi

if [ "$FAST" != "false" ]; then
    iter=1
fi

set -o errexit
set -o nounset

if [ $# -lt 2 ]; then
    echo "Usage: $0 <mount cmd> <dir>"
    exit 1
fi

cmd=$1
mnt=$2
if [ $# -gt 2 ]; then
    t=$3
else
    t=
fi

prefix=$mnt/test_dir

$cmd >& mount.log &
PID=$!

function cleanup {
    popd >/dev/null
    if [ "$TRAVIS" != "false" ]; then
        rmdir $prefix
    else
        rmdir $prefix >& /dev/null || true # riofs doesn't support rmdir
    fi

    if [ "$PID" != "" ]; then
        kill $PID >& /dev/null || true
        fusermount -u $mnt >& /dev/null || true
    fi
}

function cleanup_err {
    err=$?
    popd >&/dev/null || true
    rmdir $prefix >&/dev/null || true

    if [ "$PID" != "" ]; then
        kill $PID >& /dev/null || true
        fusermount -u $mnt >& /dev/null || true
    fi

    return $err
}

trap cleanup EXIT
trap cleanup_err ERR

if [ "$TRAVIS" == "false" ]; then
    for i in $(seq 1 5); do
        (grep -q $mnt /proc/mounts && break) || true
        sleep 1
    done
    if ! grep -q $mnt /proc/mounts; then
        echo "$mnt not mounted"
        cat mount.log
        exit 1
    fi
fi
mkdir -p "$prefix"
pushd "$prefix" >/dev/null

SUDO=
if [ $(id -u) != 0 ]; then
    SUDO=sudo
fi

function drop_cache {
    if [ "$TRAVIS" == "false" ]; then
        (echo 3 | $SUDO tee /proc/sys/vm/drop_caches) > /dev/null
    fi
}

export TIMEFORMAT=%R

function run_test {
    test=$1
    drop_cache
    sleep 1
    echo -n "$test "
    time $test
}
<commit_msg>better cleanup for mount failures<commit_after>#!/bin/bash

: ${TRAVIS:="false"}
: ${FAST:="false"}
: ${test:=""}

iter=10

if [ "$TRAVIS" != "false" ]; then
    set -o xtrace
    iter=1
fi

if [ "$FAST" != "false" ]; then
    iter=1
fi

set -o errexit
set -o nounset

if [ $# -lt 2 ]; then
    echo "Usage: $0 <mount cmd> <dir>"
    exit 1
fi

cmd=$1
mnt=$2
if [ $# -gt 2 ]; then
    t=$3
else
    t=
fi

prefix=$mnt/test_dir

MOUNTED=0

$cmd >& mount.log &
PID=$!

function cleanup {
    popd >/dev/null
    if [ "$TRAVIS" != "false" ]; then
        rmdir $prefix
    else
        rmdir $prefix >& /dev/null || true # riofs doesn't support rmdir
    fi

    if [ "$PID" != "" ]; then
        kill $PID >& /dev/null || true
        fusermount -u $mnt >& /dev/null || true
    fi
}

function cleanup_err {
    err=$?
    if [ $MOUNTED == 1 ]; then
        popd >&/dev/null || true
        rmdir $prefix >&/dev/null || true
    fi

    if [ "$PID" != "" ]; then
        kill $PID >& /dev/null || true
        fusermount -u $mnt >& /dev/null || true
    fi

    return $err
}

trap cleanup EXIT
trap cleanup_err ERR

if [ "$TRAVIS" == "false" ]; then
    for i in $(seq 1 5); do
        (grep -q $mnt /proc/mounts && break) || true
        sleep 1
    done
    if ! grep -q $mnt /proc/mounts; then
        echo "$mnt not mounted"
        cat mount.log
        exit 1
    fi
    MOUNTED=1
else
    # in travis we mount things externally so we know we are mounted
    MOUNTED=1
fi

mkdir -p "$prefix"
pushd "$prefix" >/dev/null

SUDO=
if [ $(id -u) != 0 ]; then
    SUDO=sudo
fi

function drop_cache {
    if [ "$TRAVIS" == "false" ]; then
        (echo 3 | $SUDO tee /proc/sys/vm/drop_caches) > /dev/null
    fi
}

export TIMEFORMAT=%R

function run_test {
    test=$1
    drop_cache
    sleep 1
    echo -n "$test "
    time $test
}

{'fraction_non_alphanumeric': 0.16255025847214244, 'fraction_numerical': 0.012062033314187249, 'mean_word_length': 2.0445804195804196, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, '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': '1402226', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1361, 'n_words': 411}