-
Notifications
You must be signed in to change notification settings - Fork 3
/
fix-file-size
executable file
·170 lines (135 loc) · 2.9 KB
/
fix-file-size
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env bash
show_help() {
cat <<EOF
$ExecName version $Version
Usage:
$ExecName [options]
Updates the ICAT size information for the replicas of a set of data objects
Options:
-h, --help show help and exit
-J, --jobs NUM-JOBS the number of fixes to perform simultaneously
-R, --resc RESC only fix replicas on resource RESC
-v, --version show version and exit
Summary:
It reads a list of data object paths, one per line, from standard in. For each
replica, it updates the ICAT size information based on the size of the
corresponding file in storage. No object name may have a carriage return in its
path. The user must be initialized with iRODS as an admin user. Finally, the
user must have passwordless access to the root account on the relevant storage
resources.
EOF
}
readonly Version=2
set -o errexit -o nounset -o pipefail
ExecPath="$(readlink --canonicalize "$0")"
ExecName="$(basename "$ExecPath")"
readonly ExecName ExecPath
export RESC
main() {
local opts
if ! opts="$(format_opts "$@")"
then
printf '\n' >&2
show_help >&2
exit 1
fi
eval set -- "$opts"
local jobs
while true
do
case "$1" in
-h|--help)
show_help
exit 0
;;
-J|--jobs)
jobs="$2"
shift 2
;;
-R|--resc)
RESC="$2"
shift 2
;;
-v|--version)
show_version
exit 0
;;
--)
shift
break
;;
*)
show_help >&2
exit 1
;;
esac
done
export EXEC_DIR
EXEC_DIR="$(dirname "$ExecPath")"
if [ -n "${jobs-}" ]
then
readonly JobsOpt=-j"$jobs"
else
readonly JobsOpt=
fi
parallel --eta --no-notice --delimiter '\n' --max-args 1 "$JobsOpt" FIX > /dev/null
}
format_opts() {
getopt --name "$ExecName" --options hJ:R:v --longoptions help,jobs:,resc:,version -- "$@"
}
show_version() {
printf '%s\n' "$Version"
}
FIX() {
local objPath="$1"
#printf '"%s"\n' "$RESC" >&2
set -o errexit -o nounset -o pipefail
exec_on_rs() {
local rs="$1"
local cmd="$2"
local superCmd="if [[ \$USER == irods ]]; then $cmd; else sudo -i -u irods <<'EOSUDO'
$cmd
EOSUDO
fi"
#shellcheck disable=SC2087
ssh -q -t "$rs" "$superCmd" < /dev/null
}
mk_physical_size_cmd() {
local filePath="$1"
cat <<CMD
stat --format '%s' "$filePath"
CMD
}
local fixed
printf '%s\n' "$objPath"
local icatSize rescHier storeHost filePath
while read -r icatSize rescHier storeHost filePath
do
fixed=
local sizeCmd
if ! sizeCmd="$(mk_physical_size_cmd "$filePath")"; then
return 1
fi
local size
if ! size="$(exec_on_rs "$storeHost" "$sizeCmd")"
then
break
fi
if (( size != icatSize ))
then
if ! \
iadmin modrepl logical_path "$objPath" resource_hierarchy "$rescHier" DATA_SIZE "$size"
then
break
fi
fi
fixed="$objPath"
done < <("$EXEC_DIR"/get-replicas --resc="${RESC-}" "$objPath") >&2
if [[ -z "${fixed-}" ]]
then
printf 'ERROR: failed to fix %s\n' "$objPath" >&2
exit 1
fi
}
export -f FIX
main "$@"