compress.sh: multiple correction and securisation

This commit is contained in:
Geoffray Levasseur-Brandin
2025-06-19 14:49:49 +02:00
parent f0f80e2924
commit 47e89b3b09

View File

@@ -35,7 +35,7 @@
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Smartly uncompress archives (zip only) # Smartly uncompress archives (zip only for now)
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
utaz() utaz()
{ {
@@ -81,28 +81,32 @@ utaz()
esac esac
done done
[[ $createdir && $nodir ]] && disp E "The --create-dir and --no-dir options are mutually exclusive." [[ $createdir && $nodir ]] && \
disp E "The --create-dir and --no-dir options are mutually exclusive."
[[ ! $LIST ]] && local LIST="." [[ ! $LIST ]] && local LIST="."
for zitem in $LIST; do for zitem in $LIST; do
[[ $(ls $zitem/*.zip 2>/dev/null | wc -l) -eq 0 ]] && shopt -s nullglob
disp W "$zitem contains no supported archive file, skipping." && local zips=("$zitem"/*.zip)
shopt -u nullglob
[[ ${#zips[@]} -eq 0 ]] && \
disp W "$zitem contains no supported archive file, skipping." && \
continue continue
for f in $zitem/*.zip; do for f in "$zitem"/*.zip; do
disp I "Processing archive $zitem/$f... " disp I "Processing archive $f... "
local dir=${f::-4} local dir=${f::-4}
mkdir -p $dir mkdir -p "$dir"
[[ $? -gt 0 ]] && [[ $? -gt 0 ]] &&
disp E "The filesystem can't create directories, exit!" && disp E "The filesystem can't create directories, exit!" &&
return 1 return 1
unzip -o $f -d $dir >/dev/null 2>&1 unzip -o "$f" -d "$dir" >/dev/null 2>&1
case $? in case $? in
0) 0)
[[ $willrm ]] && rm -f $f && disp I "File $zitem/$f deleted." [[ $willrm ]] && rm -f "$f" && disp I "File $zitem/$f deleted."
;; ;;
1) 1)
@@ -110,7 +114,7 @@ utaz()
;; ;;
*) *)
disp E "The zip file seems corrupted, failed." disp E "The zip file seems corrupted, failed."
rm -rf $dir >/dev/null 2>&1 rm -rf "$dir" >/dev/null 2>&1
continue continue
;; ;;
esac esac
@@ -118,7 +122,7 @@ utaz()
if [[ $createdir ]]; then if [[ $createdir ]]; then
disp I "Archive extracted successfully in subdirectory." disp I "Archive extracted successfully in subdirectory."
elif [[ $nodir ]]; then elif [[ $nodir ]]; then
mv ./$dir/* ./ && rmdir $dir mv "./$dir/"* ./ && rmdir "$dir"
disp I "Archive extracted successfully, no subdirectory needed." disp I "Archive extracted successfully, no subdirectory needed."
else else
subdirs=$(find $dir -maxdepth 1 | wc -l) subdirs=$(find $dir -maxdepth 1 | wc -l)
@@ -167,7 +171,7 @@ taz()
disp E "Program 'plzip' or 'lzip' are not installed, aborting." disp E "Program 'plzip' or 'lzip' are not installed, aborting."
return 127 return 127
} }
local command=lzip command=lzip
local procopt="" local procopt=""
[[ $2 -gt 1 ]] && [[ $2 -gt 1 ]] &&
disp W "lzip doesn't support multithreading, falling back to 1 thread." && disp W "lzip doesn't support multithreading, falling back to 1 thread." &&
@@ -195,7 +199,7 @@ taz()
local procopt="" local procopt=""
[[ $2 -gt 1 ]] && [[ $2 -gt 1 ]] &&
disp W "gzip doesn't support multithreading, falling back to 1 thread." && disp W "gzip doesn't support multithreading, falling back to 1 thread." &&
disp W "Consitder installing pigz to obtain multithreading abilities." disp W "Consider installing pigz to obtain multithreading abilities."
} }
[[ $4 ]] && local verb="--verbose" [[ $4 ]] && local verb="--verbose"
@@ -219,7 +223,7 @@ taz()
local procopt="" local procopt=""
[[ $2 -gt 1 ]] && [[ $2 -gt 1 ]] &&
disp W "bzip2 doesn't support multithreading, falling back to 1 thread." && disp W "bzip2 doesn't support multithreading, falling back to 1 thread." &&
disp W "Consitder installing pbzip2 to obtain multithreading abilities." disp W "Consider installing pbzip2 to obtain multithreading abilities."
} }
[[ $4 ]] && local verb="-v" [[ $4 ]] && local verb="-v"
@@ -291,7 +295,7 @@ taz()
;; ;;
"-q" | "--quiet") "-q" | "--quiet")
QUIET=1 local quiet=1
;; ;;
"-"*) "-"*)
@@ -312,17 +316,17 @@ taz()
[[ ! $compform ]] && compform=lz # safe and efficient (unless data are already compressed) [[ ! $compform ]] && compform=lz # safe and efficient (unless data are already compressed)
[[ ! $nproc ]] && nproc=1 [[ ! $nproc ]] && nproc=1
[[ ! $complevel ]] && complevel=6 [[ ! $complevel ]] && complevel=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."
for item in $LIST; do for item in $LIST; do
local donetar=0 local donetar=0
disp I "Processing $item..." disp I "Processing $item..."
if [[ -d $item ]]; then if [[ -d "$item" ]]; then
disp I "\t Creating $item.tar... " disp I "\t Creating $item.tar... "
tar -cf $item{.tar,} tar -cf "$item.tar" "$item"
if [[ ! $? -eq 0 ]]; then if [[ ! $? -eq 0 ]]; then
disp E "tar file creation failed, skipping to next item." disp E "tar file creation failed, skipping to next item."
continue continue
@@ -337,7 +341,7 @@ taz()
# Skip compression part if tar is asked # Skip compression part if tar is asked
if [[ $compform != "tar" ]]; then if [[ $compform != "tar" ]]; then
disp I "\t Compressing archive..." disp I "\t Compressing archive..."
_do$compform $fname $nproc $complevel $verbose _do$compform "$fname" "$nproc" "$complevel" "$verbose"
[[ ! $? -eq 0 ]] && case $? in [[ ! $? -eq 0 ]] && case $? in
127) 127)
disp E "Compression program unavailable, aborting." disp E "Compression program unavailable, aborting."
@@ -349,15 +353,15 @@ taz()
;; ;;
esac esac
[[ $donetar -gt 0 ]] && rm $fname [[ $donetar -gt 0 ]] && rm "$fname"
fi fi
if [[ $willrm ]]; then if [[ $willrm ]]; then
disp I "\t Deleting original source as asked... " disp I "\t Deleting original source as asked... "
rm -r $item rm -r "$item"
fi fi
done done
unset QUIET unset quiet
} }
export -f taz export -f taz