-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch_dir_hashing
executable file
·85 lines (75 loc) · 2.22 KB
/
watch_dir_hashing
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
#! /bin/bash -
# This script estimates the current state of
# the directory hashing process.
# Usage: ./watch_dir_hashing <progress file>
trap 'exit 0' SIGUSR1
trap - PIPE INT TERM ERR HUP EXIT
minWait=5
maxWait=600
hashStartState=0
export LC_ALL=C # faster grep
procFile="$1"
fileHashes="$2"
totalHashed(){
wc -c "$procFile" | cut -d' ' -f1
}
hashStartTime=$(date +%s)
hashStartState=$(totalHashed)
sleepUntil(){
local now=$(date +%s)
(( $1 > $now )) && sleep $(( $1 - $now ))
}
checkpoint=$(date +%s)
total=$( grep -va '^#\|^%' "$fileHashes" | wc -l )
waitTime=$(( $(date +%s) - $checkpoint ))
waitTime=$(( $waitTime>$minWait?$waitTime:$minWait ))
waitTime=$(( $waitTime<$maxWait?$waitTime:$maxWait ))
displayTime(){
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
(( $D > 0 )) && printf '%d days ' $D
(( $H > 0 )) && printf '%d h ' $H
(( $M > 0 )) && printf '%d min ' $M
(( $D > 0 || $H > 0 || $M > 0 )) && printf 'and '
printf '%d sec\n' $S
}
printf 'Updating progress every %s...\n' "$(displayTime $waitTime)"
finished(){
printf '[%(%F %H:%M:%S)T] 100.00 %% (%s/%s)\n' \
"$(date +%s)" "$total" "$total"
}
trap 'finished; exit 0' SIGUSR1
lastTime=$hashStartTime
lastState=$hashStartState
sleepUntil $(( $checkpoint + 1 ))
while true; do
checkpoint=$(date +%s)
current=$(totalHashed)
CURRENT=" ($current/$total)"
printf -v state "%02d" $(( 10000 * $current / $total ))
STATE=" ${state:0:-2}.${state: -2} %"
now=$(date +%s)
if [[ $current -le $hashStartState ]]; then
hashStartState=$current
SPEED=""
ETA=""
else
passed=$(( $now - $lastTime ))
lastTime=$now
diff=$(( $current - $lastState ))
lastState=$current
speed=$(( $diff / $passed ))
SPEED=" at $speed files/s"
rem=$(( $total - $current ))
total_passed=$(( $now - $hashStartTime ))
total_diff=$(( $current - $hashStartState ))
eta=$(( $rem * $total_passed / $total_diff ))
ETA=" ~ $(displayTime $eta) remaining"
fi
printf '[%(%F %H:%M:%S)T]%s%s%s%s\n' \
"$now" "$STATE" "$CURRENT" "$SPEED" "$ETA"
sleepUntil $(( $checkpoint + $waitTime ))
done