-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-file-checksum
162 lines (145 loc) · 3.65 KB
/
get-file-checksum
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
#!/bin/bash
# This script has moved to https://github.com/sara-nl/GridScripts/blob/master/get-file-checksum.
# This script talks to a dCache webdav door to get the checksum(s) of a file.
#
# Changes:
# 2018-07-11 - Onno - Initial version
usage() {
cat <<-EOF
Lists checksum(s) for a file in a dCache storage system through WebDAV.
Usage: $0 [options...]
Options are:
--url <url> - A webdav URL of a file in a dCache instance
--adler32 - List only the Adler32 checksum
--md5 - List only the MD5 checksum
Default: list both Adler32 and MD5 if available
--proxy - Authenticate with proxy specified in X509_USER_PROXY
--user <username> - Authenticate with Username/password
--debug - Show additional info for troubleshooting
Examples:
$0 --url https://my-dcache-server.org/users/homer/disk-shared/ --user homer
$0 --url https://my-dcache-server.org:2882/users/homer/disk-shared/ --proxy --md5
EOF
exit 1
}
url=
adler32=false
md5=false
user=
proxy=false
recursive=false
debug=false
while [ $# -gt 0 ] ; do
case "$1" in
--url )
url="$2"
shift 2
;;
--adler32 )
adler32=true
shift
;;
--md5 )
md5=true
shift
;;
--recursive )
echo "Not implemented" ; exit 1
recursive=true
shift
;;
--user )
user=$2
proxy=false
shift 2
;;
--proxy )
proxy=true
shift
;;
--debug )
debug=true
shift 1
;;
*)
usage
;;
esac
done
if [ -z "$url" ] ; then
usage
fi
# If neither adler32 nor md5 is requested, show them both.
if ! $md5 && ! $adler32 ; then
md5=true
adler32=true
fi
if [ -n "$user" -a "$proxy" == "true" ] ; then
echo "ERROR: you can't specify both --user and --proxy." 1>&2
exit 1
fi
server=$(echo "$url" | egrep -o 'https://[^/]+/')
dir=$(echo "$url" | sed -e 's#https://[^/]\+##')
if [ -z "$dir" ] ; then
dir=/
fi
if [ -z "$server" ] ; then
echo "Please include the server in '--url'." 1>&2
exit 1
fi
if $proxy ; then
# Check if the proxy is still valid; if not, exit after the error message.
if [ -x "$(command -v voms-proxy-info)" ]; then
voms-proxy-info --exists 1>&2 || exit 1
fi
authn="--capath /etc/grid-security/certificates/ --cert $X509_USER_PROXY --cacert $X509_USER_PROXY"
else
if [ -z "$user" ] ; then
echo "Please specify --proxy, or specify a username with --user." 1>&2
exit 1
fi
authn="-u $user"
fi
result=false
if $adler32 ; then
if $debug ; then
echo "Curl command:"
echo "curl --head --header 'Want-Digest: ADLER32' --silent --fail " \
"$authn " \
"$url"
echo
fi
result_adler32=$(curl --head --header 'Want-Digest: ADLER32' --silent --fail \
$authn \
$url \
| egrep -o 'adler32=[0-9a-f]+' )
if [ -n "$result_adler32" ] ; then
result=true
echo "$result_adler32 $dir"
fi
fi
if $md5 ; then
if $debug ; then
echo "Curl command:"
echo "curl --head --header 'Want-Digest: MD5' --silent --fail " \
"$authn " \
"$url"
echo
fi
result_md5=$(curl --head --header 'Want-Digest: MD5' --silent --fail \
$authn \
$url \
| grep -o 'md5=.*' \
| sed -e 's/md5=//' -e 's/[\r\n]*$//' \
| base64 --decode \
| xxd -p )
if [ -n "$result_md5" ] ; then
result=true
echo "md5=$result_md5 $dir"
fi
fi
# Still no result? Then show error.
if ! $result ; then
echo "ERROR: no checksum found." 1>&2
exit 1
fi