diff --git a/profile.d/compress.sh b/profile.d/compress.sh index 85fe75e..48671a9 100644 --- a/profile.d/compress.sh +++ b/profile.d/compress.sh @@ -395,18 +395,32 @@ export -f utaz # ------------------------------------------------------------------------------ # Compress directories or files into one or more archive -# Usage: taz [option] [--parallel=] [--format=] [directory1 ... directoryN] +# Usage: taz [option] [--parallel=] [--format=] [directory1 ... directoryN] # Options: # -h, --help Display that help screen # -d, --delete Delete source file or directory after success # -f, --format Chose archive format in the given list. If several format are # 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 # -q, --quiet Display less messages (only errors and warnings) # -1, .., -9 Compression level to use [1=fast/biggest, 9=slow/smallest] 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 _doxz() { @@ -537,13 +551,13 @@ taz() case "$1" in -h|--help) printf "taz: archive all files of a directory.\n\n" - printf "Usage: taz [option] [--parallel=] [--format=] [directory1 ... directoryN]\n\n" + printf "Usage: taz [option] [--parallel=] [--format=] [directory1 ... directoryN]\n\n" printf "Options:\n" printf "\t-h, --help\tDisplay that help screen\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\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-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" @@ -606,11 +620,20 @@ taz() [[ ${#FILES[@]} -eq 0 ]] && FILES=(".") [[ ! $compform ]] && compform=${TAZ_DEFAULT_FORMAT:-lz} - [[ ! $nproc ]] && nproc=${TAZ_DEFAULT_THREADS:-1} + [[ ! $nproc ]] && nproc=${TAZ_DEFAULT_THREADS:-auto} [[ ! $complevel ]] && complevel=${TAZ_DEFAULT_LEVEL:-6} [[ $verbose -gt 1 && $quiet -gt 1 ]] && 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 local donetar=0 disp I "Processing $item..."