Skip to content

Commit a65d03b

Browse files
committed
With --verify-checksum and target file exists, verify checksum.
1 parent c532193 commit a65d03b

File tree

1 file changed

+75
-34
lines changed

1 file changed

+75
-34
lines changed

ada/ada

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ usage() {
103103
If the uploaded file's checksum does not match,
104104
dCache aborts the transfer and removes the corrupt file.
105105
This ensures data integrity during transfer.
106+
107+
Ada does not overwrite files. If the destination file
108+
exists, Ada will print an error message, except when
109+
you specify --verify-checksum: then Ada will compare
110+
checksums of source and destination.
106111
107112
Ada will try several ways to discover a WebDAV door
108113
to use for the transfer.
@@ -115,6 +120,11 @@ usage() {
115120
With --verify-checksum, Ada will verify the file's checksum
116121
after download, and show an error message if it doesn't match.
117122
123+
Ada does not overwrite files. If the destination file
124+
exists, Ada will print an error message, except when
125+
you specify --verify-checksum: then Ada will compare
126+
checksums of source and destination.
127+
118128
--setlabel <file> <label>
119129
Attach a label to a file.
120130
--rmlabel <file> <label>
@@ -1849,6 +1859,43 @@ get_local_checksum () {
18491859
}
18501860

18511861

1862+
compare_checksums() {
1863+
local local_file="$1"
1864+
local remote_file="$2"
1865+
$debug && echo 1>&2 "Comparing checksums..."
1866+
# Get the checksum of the remote file from the dCache database.
1867+
# First try Adler32 because it is the default in dCache,
1868+
# and fastest to calculate locally.
1869+
remote_checksum=$(get_checksum ADLER32 "$remote_file")
1870+
if [[ -n $remote_checksum ]] ; then
1871+
local_checksum=$(get_local_checksum ADLER32 "$local_file")
1872+
else
1873+
# No Adler32 in the dCache database? Then try MD5.
1874+
remote_checksum=$(get_checksum MD5 "$remote_file")
1875+
if [[ -z $remote_checksum ]] ; then
1876+
echo 1>&2 "ERROR: unable to get a checksum for file '$remote_file'."
1877+
exit 1
1878+
fi
1879+
local_checksum=$(get_local_checksum MD5 "$local_file")
1880+
fi
1881+
if [[ "$local_checksum" == "$remote_checksum" ]] ; then
1882+
if $debug ; then
1883+
echo 1>&2 "Remote checksum: $remote_checksum for file '$remote_file'." ;
1884+
echo 1>&2 "Local checksum : $local_checksum for file '$local_file'." ;
1885+
fi
1886+
echo "Checksum OK."
1887+
return 0
1888+
else
1889+
{
1890+
echo "ERROR: Checksum mismatch!"
1891+
echo "Remote checksum: $remote_checksum" ;
1892+
echo "Local checksum : $local_checksum" ;
1893+
} 1>&2
1894+
return 1
1895+
fi
1896+
}
1897+
1898+
18521899
get_channel_by_name () {
18531900
local channel_name="$1"
18541901
# Many other API calls depend on this one.
@@ -2810,8 +2857,17 @@ api_call () {
28102857
# overwrites can be dangerous.
28112858
case $(pathtype "$path") in
28122859
REGULAR )
2813-
echo 1>&2 "ERROR: target '$path' is an existing file. Ada doesn't support overwriting a file."
2814-
exit 1
2860+
# Target (remote) exists and is a file.
2861+
# If user wants to verify checksums, we verify checksums!
2862+
if $verify_checksum ; then
2863+
echo 1>&2 "Target (remote) file '$path' already exists. Verifying checksum..."
2864+
compare_checksums "$source" "$path" || exit 1
2865+
exit 0
2866+
else
2867+
# User not interested in checksums. We show an error message.
2868+
echo 1>&2 "ERROR: target '$path' is an existing file. Ada doesn't support overwriting a file."
2869+
exit 1
2870+
fi
28152871
;;
28162872
LINK )
28172873
echo 1>&2 "ERROR: target '$path' is a symlink. A symlink can point to a file or a directory." \
@@ -2865,6 +2921,11 @@ api_call () {
28652921
$debug && echo 1>&2 "User specified WebDAV door: $webdav_door"
28662922
fi
28672923
# We should be ready to upload now!
2924+
if $verify_checksum ; then
2925+
echo 1>&2 "Uploading with checksum verification..."
2926+
else
2927+
echo 1>&2 "Uploading..."
2928+
fi
28682929
$debug && set -x # If --debug is specified, show curl command (tracing on)
28692930
curl "${curl_authorization[@]}" \
28702931
--location --post302 \
@@ -2879,10 +2940,18 @@ api_call () {
28792940
# We check the target first because it is local and thus faster.
28802941
#
28812942
curl_target=()
2882-
# If a file already exists, quit.
2943+
# Does the local (target) file already exist?
28832944
if [[ -f $target ]] ; then
2884-
echo 1>&2 "ERROR: Target file '$target' already exists."
2885-
exit 1
2945+
# If user wants to verify checksums, we verify checksums!
2946+
if $verify_checksum ; then
2947+
echo 1>&2 "Target (local) file '$target' already exists. Verifying checksum..."
2948+
compare_checksums "$target" "$source" || exit 1
2949+
exit 0
2950+
else
2951+
# User not interested in checksums. We show an error message.
2952+
echo 1>&2 "ERROR: Target file '$target' already exists."
2953+
exit 1
2954+
fi
28862955
fi
28872956
# If the target is a directory, we tell curl to use the
28882957
# remote filename
@@ -2975,35 +3044,7 @@ api_call () {
29753044
"${curl_target[@]}"
29763045
{ set +x ; } 2>/dev/null # Silently switch off tracing
29773046
if $verify_checksum ; then
2978-
$debug && echo 1>&2 "Comparing checksums..."
2979-
# Get the checksum of the remote file from the dCache database.
2980-
# First try Adler32 because it is the default in dCache,
2981-
# and fastest to calculate locally.
2982-
remote_checksum=$(get_checksum ADLER32 "$path")
2983-
if [[ -n $remote_checksum ]] ; then
2984-
local_checksum=$(get_local_checksum ADLER32 "$real_target")
2985-
else
2986-
# No Adler32 in the dCache database? Then try MD5.
2987-
remote_checksum=$(get_checksum MD5 "$path")
2988-
if [[ -z $remote_checksum ]] ; then
2989-
echo 1>&2 "ERROR: unable to get a checksum for file '$path'."
2990-
exit 1
2991-
fi
2992-
local_checksum=$(get_local_checksum MD5 "$real_target")
2993-
fi
2994-
if [[ "$local_checksum" == "$remote_checksum" ]] ; then
2995-
if $debug ; then
2996-
echo 1>&2 "Remote checksum: $remote_checksum" ;
2997-
echo 1>&2 "Local checksum : $local_checksum" ;
2998-
fi
2999-
echo "Checksum OK."
3000-
else
3001-
{
3002-
echo "ERROR: Checksum incorrect! File '$real_target' might be damaged."
3003-
echo "Remote checksum: $remote_checksum" ;
3004-
echo "Local checksum : $local_checksum" ;
3005-
} 1>&2
3006-
fi
3047+
compare_checksums "$real_target" "$source" || exit 1
30073048
fi
30083049
;;
30093050
setlabel | lslabel | rmlabel )

0 commit comments

Comments
 (0)