From b5ccc453c9a03a7f5570188d9f4a31bb667b7541 Mon Sep 17 00:00:00 2001 From: phone Date: Thu, 18 Jan 2024 17:06:53 +0300 Subject: [PATCH] Archive : Check before xz convert. --- .config/bash/module/Archive.sh | 54 ++++++++++++++++------------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/.config/bash/module/Archive.sh b/.config/bash/module/Archive.sh index cf10c0f..65ab16c 100644 --- a/.config/bash/module/Archive.sh +++ b/.config/bash/module/Archive.sh @@ -108,15 +108,7 @@ function archive_check() { [[ "${targets}" = "" ]] && targets=($(_ls_archive)) process() { - # extract hash from name. - local data=($(_archive_parse ${target})) - local saved=${data[2]} - - # calculate actual hash. - local actual=$(pv ${target} | sha1sum | cut -d\ -f1) - - # compare hashes. - [[ "${actual}" = "${saved}" ]] + _archive_check "${target}" } _iterate_targets process ${targets[@]} @@ -180,6 +172,9 @@ function archive_xz() { return 1 fi + # Check integrity. + _archive_check "${target}" || return 1 + # Recompress. local hash=$(pv "${target}" | gzip -d | xz -9e | tee "${tmp}" | sha1sum | cut -d\ -f1) @@ -236,27 +231,18 @@ function unarchive() { [[ "${targets}" = "" ]] && targets=$(_ls_archive) process() { - # extract hash from name. - local data=($(_archive_parse ${target})) - local saved=${data[2]} - - # calculate actual hash. - local actual=$(pv ${target} | sha1sum | cut -d\ -f1) + # Validate. + _archive_check "${target}" || return 1 # extract if hash matched or show error if not. - if [[ "${saved}" = "${actual}" ]]; then - case "${target##*.}" in - "txz") - pv ${target} | xz -d | tar -xf - - ;; - "tgz") - pv ${target} | gzip -d | tar -xf - - ;; - esac - else - _error "Failed." - return 1 - fi + case "${target##*.}" in + "txz") + pv ${target} | xz -d | tar -xf - + ;; + "tgz") + pv ${target} | gzip -d | tar -xf - + ;; + esac } _iterate_targets process ${targets[@]} @@ -358,6 +344,18 @@ function _filter_archive() { grep -E ${_archive_pattern} } +function _archive_check() { + # extract hash from name. + local data=($(_archive_parse ${target})) + local saved=${data[2]} + + # calculate actual hash. + local actual=$(pv ${target} | sha1sum | cut -d\ -f1) + + # compare hashes. + [[ "${actual}" = "${saved}" ]] +} + complete -o filenames -F _comp_archive_grep archive_check unarchive archive_rm complete -o filenames -F _comp_archive_grep_fast archive_xz complete -o filenames -F _comp_archive_name archive_name