allow auto with taz

This commit is contained in:
fatalerrors
2026-05-19 17:19:50 +02:00
parent 5faae67d11
commit 1e31712b60

View File

@@ -395,18 +395,32 @@ export -f utaz
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Compress directories or files into one or more archive # Compress directories or files into one or more archive
# Usage: taz [option] [--parallel=<n>] [--format=<format>] [directory1 ... directoryN] # Usage: taz [option] [--parallel=<n|auto>] [--format=<format>] [directory1 ... directoryN]
# Options: # Options:
# -h, --help Display that help screen # -h, --help Display that help screen
# -d, --delete Delete source file or directory after success # -d, --delete Delete source file or directory after success
# -f, --format Chose archive format in the given list. If several format are # -f, --format Chose archive format in the given list. If several format are
# given, the smalest is kept # given, the smalest is kept
# -p, --parallel Number of threads to use (if allowed by underlying utility) # -p, --parallel Number of threads to use, or 'auto' to use detected CPU count
# -v, --verbose Display progress where possible # -v, --verbose Display progress where possible
# -q, --quiet Display less messages (only errors and warnings) # -q, --quiet Display less messages (only errors and warnings)
# -1, .., -9 Compression level to use [1=fast/biggest, 9=slow/smallest] # -1, .., -9 Compression level to use [1=fast/biggest, 9=slow/smallest]
taz() taz()
{ {
# Resolve runtime CPU count for --parallel=auto.
_taz_detect_cpus()
{
local cpus=1
if command -v nproc >/dev/null 2>&1; then
cpus=$(nproc 2>/dev/null)
elif command -v getconf >/dev/null 2>&1; then
cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
fi
[[ $cpus =~ ^[1-9][0-9]*$ ]] || cpus=1
printf "%s\n" "$cpus"
}
# shellcheck disable=SC2329 # shellcheck disable=SC2329
_doxz() _doxz()
{ {
@@ -537,13 +551,13 @@ taz()
case "$1" in case "$1" in
-h|--help) -h|--help)
printf "taz: archive all files of a directory.\n\n" printf "taz: archive all files of a directory.\n\n"
printf "Usage: taz [option] [--parallel=<n>] [--format=<format>] [directory1 ... directoryN]\n\n" printf "Usage: taz [option] [--parallel=<n|auto>] [--format=<format>] [directory1 ... directoryN]\n\n"
printf "Options:\n" printf "Options:\n"
printf "\t-h, --help\tDisplay that help screen\n" printf "\t-h, --help\tDisplay that help screen\n"
printf "\t-d, --delete\tDelete source file or directory after success\n" printf "\t-d, --delete\tDelete source file or directory after success\n"
printf "\t-f, --format\tChose archive format in the given list. If several format are" printf "\t-f, --format\tChose archive format in the given list. If several format are"
printf "\t\t\tgiven, the smalest is kept\n" printf "\t\t\tgiven, the smalest is kept\n"
printf "\t-p, --parallel\tNumber of threads to use (if allowed by underlying utility)\n" printf "\t-p, --parallel\tNumber of threads, or 'auto' for runtime CPU count\n"
printf "\t-v, --verbose\tDisplay progress where possible\n" printf "\t-v, --verbose\tDisplay progress where possible\n"
printf "\t-q, --quiet\tDisplay less messages (only errors and warnings)\n" printf "\t-q, --quiet\tDisplay less messages (only errors and warnings)\n"
printf "\t-1, .., -9\tCompression level to use [1=fast/biggest, 9=slow/smallest]\n\n" printf "\t-1, .., -9\tCompression level to use [1=fast/biggest, 9=slow/smallest]\n\n"
@@ -606,11 +620,20 @@ taz()
[[ ${#FILES[@]} -eq 0 ]] && FILES=(".") [[ ${#FILES[@]} -eq 0 ]] && FILES=(".")
[[ ! $compform ]] && compform=${TAZ_DEFAULT_FORMAT:-lz} [[ ! $compform ]] && compform=${TAZ_DEFAULT_FORMAT:-lz}
[[ ! $nproc ]] && nproc=${TAZ_DEFAULT_THREADS:-1} [[ ! $nproc ]] && nproc=${TAZ_DEFAULT_THREADS:-auto}
[[ ! $complevel ]] && complevel=${TAZ_DEFAULT_LEVEL:-6} [[ ! $complevel ]] && complevel=${TAZ_DEFAULT_LEVEL:-6}
[[ $verbose -gt 1 && $quiet -gt 1 ]] && [[ $verbose -gt 1 && $quiet -gt 1 ]] &&
disp E "The --verbose and --quiet options can't be used together." disp E "The --verbose and --quiet options can't be used together."
# Backward compatibility: 0 previously meant auto-detect.
[[ $nproc == 0 ]] && nproc=auto
if [[ $nproc == auto ]]; then
nproc=$(_taz_detect_cpus)
elif [[ ! $nproc =~ ^[1-9][0-9]*$ ]]; then
disp E "Invalid value for --parallel: '$nproc' (expected auto or a positive integer)."
return 1
fi
for item in "${FILES[@]}"; do for item in "${FILES[@]}"; do
local donetar=0 local donetar=0
disp I "Processing $item..." disp I "Processing $item..."