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

View file

@ -7,3 +7,50 @@ function _core_count() {
function _parse_ints() { function _parse_ints() {
echo "${*}" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g' 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
}