|
- #!/bin/bash
- # Fetch the SHA-1 hashes from an IA item and ensure that they match the local files (i.e. that the upload was successful)
-
- identifier="$1"
- escapedIdentifier="$(sed 's/[.[\*^$()+?{|]/\\&/g' <<<"${identifier}")"
- readarray -t iasha1sums < <(curl -sL "https://archive.org/download/${identifier}/${identifier}_files.xml" | tr -d '\n' | grep -Po '<file .*?</file>' | grep 'source="original".*<sha1>' | sed 's,^.*name=",,; s,".*<sha1>, ,; s,</sha1>.*$,,' | grep -Pv "^${escapedIdentifier}"'(\.cdx\.(gz|idx)|_meta\.(sqlite|xml)) ' | sed 's,&,\&,g' | awk '{ print $NF " " substr($0, 1, length($0) - length($NF) - 1) }')
-
- localFiles=()
- while IFS= read -r -d $'\0' f; do localFiles+=("${f:2}"); done < <(find . -type f -print0)
- readarray -t iaFiles < <(printf "%s\n" "${iasha1sums[@]}" | sed 's,^.\{40\} ,,')
- readarray -t localFilesSorted < <(printf "%s\n" "${localFiles[@]}" | sort)
- readarray -t iaFilesSorted < <(printf "%s\n" "${iaFiles[@]}" | sort)
- readarray -t localMissing < <(comm -13 <(printf "%s\n" "${localFilesSorted[@]}") <(printf "%s\n" "${iaFilesSorted[@]}"))
- readarray -t iaMissing < <(comm -23 <(printf "%s\n" "${localFilesSorted[@]}") <(printf "%s\n" "${iaFilesSorted[@]}"))
-
- status=0
-
- RED=$'\x1b[1;31m'
- GREEN=$'\x1b[0;32m'
- RESET=$'\x1b[0m'
-
- if [[ ${#localMissing[@]} -eq 0 && ${#iaMissing[@]} -eq 0 ]]
- then
- echo "File list comparison: ${GREEN}OK${RESET}"
- else
- echo "File list comparison: ${RED}FAIL${RESET}"
- fi
- if [[ ${#iaMissing[@]} -gt 0 ]]
- then
- echo "Local files that are not in the IA item:"
- printf " %s\n" "${iaMissing[@]}"
- status=1
- fi
- if [[ ${#localMissing[@]} -gt 0 ]]
- then
- echo "IA item files that are not in the local directory:"
- printf " %s\n" "${localMissing[@]}"
- status=1
- fi
-
- echo
-
- echo "SHA-1 comparison:"
- sha1sum -c < <(printf "%s\n" "${iasha1sums[@]}") > >(perl -pe 's,^(.*)(?<!: OK)$,'"${RED}"'\1'"${RESET}"',; s,: OK$,: '"${GREEN}OK${RESET}"',; s,^, ,') 2>&1
- if [[ $? -eq 0 ]]
- then
- echo "SHA-1 comparison: ${GREEN}OK${RESET}"
- else
- echo "SHA-1 comparison: ${RED}FAIL${RESET}"
- status=1
- fi
-
- echo
-
- if [[ ${status} -eq 0 ]]
- then
- echo "${GREEN}OK${RESET}"
- else
- echo "${RED}FAIL${RESET}"
- fi
- exit ${status}
|