archive : add xz conversion.

This commit is contained in:
Dmitry Voronin 2023-12-16 21:56:30 +03:00
parent e4e778d544
commit c52f748f88
2 changed files with 104 additions and 30 deletions

View file

@ -300,40 +300,31 @@ function archive_rm() {
fi
}
# Parse archive file name to get: name, date, hash and format.
# Usage: _archive_parse <FILENAME>
function _archive_parse() {
local input="${1}"
local name="${input%_*}"
local format="${input##*.}"
local data="${input##*_}"; data="${data%.*}"
local date="${data%%-*}"
local hash="${data##*-}"
# Recompress previously created archive_fast with better compression.
# Usage: archive_xz [FILES]
function archive_xz() {
local targets="$(ls | grep -E ${_archive_pattern} | grep \.tgz\$)"
echo "${name}"
echo "${date}"
echo "${hash}"
echo "${format}"
}
function process() {
local IFS=$'\n'
local target="${1}"
local data=($(_archive_parse "${target}"))
local tmp="${data[0]}.txz"
# Autocomplete for archive_name function.
# First arg is the archives list, second one is selected archive's current name.
function _archive_name() {
local IFS=$'\n'
COMPREPLY=()
# Check that old format.
if [[ "${data[3]}" != "tgz" ]]; then
echo -e "${color_bred}Not in .tgz format!${color_default}"
return 1
fi
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
# Recompress.
local hash=$(pv "${target}" | gzip -d | xz -9e | tee "${tmp}" | sha1sum | cut -d\ -f1)
if [[ "${prev}" = "${command}" ]]; then
COMPREPLY=( $(compgen -W "$(ls | grep -E ${_archive_pattern})" -- ${cur}) )
return 0
else
local name="${prev%_*}"
COMPREPLY=( $(compgen -W "${name}" -- ${cur}) )
return 0
fi
# Rename.
mv -- "${tmp}" "${data[0]}_${data[1]}-${hash}.txz" && rm "${target}"
}
_iterate_targets process ${targets[@]}
}
# Extract previously created archive with checksum validation.
@ -402,6 +393,42 @@ function unarchive() {
fi
}
# Parse archive file name to get: name, date, hash and format.
# Usage: _archive_parse <FILENAME>
function _archive_parse() {
local input="${1}"
local name="${input%_*}"
local format="${input##*.}"
local data="${input##*_}"; data="${data%.*}"
local date="${data%%-*}"
local hash="${data##*-}"
echo "${name}"
echo "${date}"
echo "${hash}"
echo "${format}"
}
# Autocomplete for archive_name function.
# First arg is the archives list, second one is selected archive's current name.
function _archive_name() {
local IFS=$'\n'
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[0]}"
if [[ "${prev}" = "${command}" ]]; then
COMPREPLY=( $(compgen -W "$(ls | grep -E ${_archive_pattern})" -- ${cur}) )
return 0
else
local name="${prev%_*}"
COMPREPLY=( $(compgen -W "${name}" -- ${cur}) )
return 0
fi
}
# Autocomplete with archives in current dir.
function _archive_grep() {
_autocomplete_grep ${_archive_pattern}

View file

@ -7,3 +7,50 @@ function _core_count() {
function _parse_ints() {
echo "${*}" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g'
}
# Function-wrapper to iterate with specified function with provided files.
# By default Iterates on all non-hidden files and directories.
# Usage: _iterate_targets <FUNCTION> [FILES]
function _iterate_targets() {
local IFS=$'\n'
local foo="${1}"
local targets=("${@:2}")
local total=${#targets[@]}
local count=0
local failed=0
# set dafult value to target all supported archives.
if [[ "${targets}" = "" ]]; then
targets=($(ls))
total=${#targets[@]}
fi
# iterate each target.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# status info.
if [[ "${total}" -gt 1 ]]; then
local status="[${count}/${total}] ${target}"
else
local status="${target}"
fi
echo -e "${color_bwhite}${status}${color_default}"
# Call function.
${foo} "${target}"
# Show error.
if [[ ${?} != 0 ]]; then
((failed++))
echo -e "${color_bred}${status}: Failed.${color_default}"
fi
done
# Show error.
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
}