xref: /llvm-project/bolt/test/runtime/wait_file.sh (revision dfc759929644ed1ea3224ab30e1086f7acc60da6)
1#!/bin/bash
2
3check_file() {
4    local file="$1"
5    if [ -z "$file" ]; then
6        echo "No file passed!"
7        exit 1
8    fi
9    if [ ! -f "$file" ]; then
10        return 1
11    fi
12
13    fuser -s "$file"
14    local ret=$?
15    if [ $ret -eq 1 ]; then # noone has file open
16        return 0
17    fi
18    if [ $ret -eq 0 ]; then # file open by some processes
19        return 1
20    fi
21    if [ $ret -eq 127 ]; then
22        echo "fuser command not found!"
23        exit 1
24    fi
25
26    echo "Unexpected exit code $ret from fuser!"
27    exit 1
28}
29
30wait_file() {
31    local file="$1"
32    local max_sleep=10
33    check_file "$file"
34    local ret=$?
35    while [ $ret -ne 0 ] && [ $max_sleep -ne 0 ]; do
36        sleep 1
37        max_sleep=$((max_sleep - 1))
38        check_file $file
39        ret=$?
40    done
41    if [ $max_sleep -eq 0 ]; then
42        echo "The file does not exist or the test hung!"
43        exit 1
44    fi
45
46}
47file="$1"
48wait_file "$file"
49