-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckFileIntegrity.sh
executable file
·88 lines (69 loc) · 2.1 KB
/
CheckFileIntegrity.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#/bin/bash
# File : CheckFileIntegrity.sh
# Author : Anton Riedel <[email protected]>
# Date : 17.06.2021
# Last Modified Date: 03.03.2022
# Last Modified By : Anton Riedel <[email protected]>
# check integrity of all .root files in the local output directory
set -o pipefail
[ ! -f config.json ] && echo "No config file!!!" && exit 1
LockFile="$(jq -r '.LockFile' config.json)"
StatusFile="$(jq -r '.StatusFile' config.json)"
[ ! -f $StatusFile ] && echo "No $StatusFile file!!!" && exit 1
# get variables from config file
echo "Waiting for lock..."
{
flock 100
Runs="$(jq -r 'keys[]' $StatusFile)"
} 100>$LockFile
LocalOutputDir="$(jq -r '.task.GridOutputDir' config.json)"
FileLog="CheckedFiles.log"
if [ ! -f $FileLog ]; then
: >$FileLog
fi
# check integrity
Check_Integrity() {
# set flag
local Flag=0
# check if file was already checked
if grep -Fxq "$1" "$2"; then
return 0
fi
# check file
local Timeout=20
{
timeout $Timeout file -E $1 || Flag=1
timeout $Timeout rootls $1 || Flag=1
# ADD MORE CHECKS
} &>/dev/null
# remove file if a check is not passed
if [ $Flag -eq 1 ]; then
return 1
else
# if it passes, print the name
echo $1
# and remove files on grid to preserve space
timout $Timeout alien_rm -fr "alien:${GridHomeDir}/${GridWorkDir}/$(dirname $1)" &>/dev/null
return 0
fi
}
# needed to run a function with parallel
export -f Check_Integrity
Status=""
Dir=""
FilesChecked=""
GridHomeDir="$(jq -r '.task.GridHomeDir' config.json)"
GridWorkDir="$(jq -r '.task.GridWorkDir' config.json)"
GridOutputFile="$(jq -r '.task.GridOutputFile' config.json)"
for Run in $Runs; do
Dir="${LocalOutputDir}/${Run}"
echo "Checking .root files in $Dir"
find $Dir -type f -name $GridOutputFile | parallel --progress --bar Check_Integrity {} $FileLog >>$FileLog
FilesChecked=$(find "${LocalOutputDir}/${Run}" -type f -name $GridOutputFile | wc -l)
echo "Waiting for lock..."
{
flock 100
jq --arg Run $Run --arg FilesChecked ${FilesChecked:=0} 'setpath([$Run,"FilesChecked"];$FilesChecked)' $StatusFile | sponge $StatusFile
} 100>$LockFile
done
exit 0