Procházet zdrojové kódy

fix(core): propagate archive extraction and copy failures (#5109)

fn_dl_extract could report success when nothing was extracted:
- an unsupported MIME type matched no branch and the if returned 0
- in selective ZIP extraction the status came from rm, hiding unzip
  and cp failures

Fail explicitly on unsupported archive types, chain unzip and cp so
either failure is reported, clean up the temp directory only after
capturing the exit code, and capture stderr so the failure reason is
written to the log.

Co-authored-by: Guilherme Praça <247475208+guilherme-praca@users.noreply.github.com>
Guilherme Praça před 1 dnem
rodič
revize
aa5344ba14
1 změnil soubory, kde provedl 17 přidání a 11 odebrání
  1. 17 11
      lgsm/modules/core_dl.sh

+ 17 - 11
lgsm/modules/core_dl.sh

@@ -293,7 +293,7 @@ fn_dl_hash() {
 	fi
 	fi
 }
 }
 
 
-# Extracts bzip2, gzip or zip files.
+# Extracts gzip, bzip2, xz or zip files. Any other file type fails.
 # Extracts can be defined in code like so:
 # Extracts can be defined in code like so:
 # fn_dl_extract "${local_filedir}" "${local_filename}" "${extractdest}" "${extractsrc}"
 # fn_dl_extract "${local_filedir}" "${local_filename}" "${extractdest}" "${extractsrc}"
 # fn_dl_extract "/home/gameserver/lgsm/tmp" "file.tar.bz2" "/home/gamserver/serverfiles"
 # fn_dl_extract "/home/gameserver/lgsm/tmp" "file.tar.bz2" "/home/gamserver/serverfiles"
@@ -318,33 +318,39 @@ fn_dl_extract() {
 	mime=$(file -b --mime-type "${local_filedir}/${local_filename}")
 	mime=$(file -b --mime-type "${local_filedir}/${local_filename}")
 	if [ "${mime}" == "application/gzip" ] || [ "${mime}" == "application/x-gzip" ]; then
 	if [ "${mime}" == "application/gzip" ] || [ "${mime}" == "application/x-gzip" ]; then
 		if [ -n "${extractsrc}" ]; then
 		if [ -n "${extractsrc}" ]; then
-			extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}")
+			extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}" 2>&1)
 		else
 		else
-			extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdest}")
+			extractcmd=$(tar -zxf "${local_filedir}/${local_filename}" -C "${extractdest}" 2>&1)
 		fi
 		fi
 	elif [ "${mime}" == "application/x-bzip2" ]; then
 	elif [ "${mime}" == "application/x-bzip2" ]; then
 		if [ -n "${extractsrc}" ]; then
 		if [ -n "${extractsrc}" ]; then
-			extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}")
+			extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}" 2>&1)
 		else
 		else
-			extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdest}")
+			extractcmd=$(tar -jxf "${local_filedir}/${local_filename}" -C "${extractdest}" 2>&1)
 		fi
 		fi
 	elif [ "${mime}" == "application/x-xz" ]; then
 	elif [ "${mime}" == "application/x-xz" ]; then
 		if [ -n "${extractsrc}" ]; then
 		if [ -n "${extractsrc}" ]; then
-			extractcmd=$(tar -Jxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}")
+			extractcmd=$(tar -Jxf "${local_filedir}/${local_filename}" -C "${extractdest}" --strip-components=1 "${extractsrc}" 2>&1)
 		else
 		else
-			extractcmd=$(tar -Jxf "${local_filedir}/${local_filename}" -C "${extractdest}")
+			extractcmd=$(tar -Jxf "${local_filedir}/${local_filename}" -C "${extractdest}" 2>&1)
 		fi
 		fi
 	elif [ "${mime}" == "application/zip" ]; then
 	elif [ "${mime}" == "application/zip" ]; then
 		if [ -n "${extractsrc}" ]; then
 		if [ -n "${extractsrc}" ]; then
 			temp_extractdir="${tmpdir}/${extractsrc}"
 			temp_extractdir="${tmpdir}/${extractsrc}"
-			extractcmd=$(unzip -qo "${local_filedir}/${local_filename}" "${extractsrc}/*" -d "${temp_extractdir}")
-			cp -a "${temp_extractdir}/${extractsrc}/." "${extractdest}/"
-			rm -rf "${temp_extractdir}"
+			extractcmd=$(unzip -qo "${local_filedir}/${local_filename}" "${extractsrc}/*" -d "${temp_extractdir}" 2>&1 && cp -a "${temp_extractdir}/${extractsrc}/." "${extractdest}/" 2>&1)
 		else
 		else
-			extractcmd=$(unzip -qo -d "${extractdest}" "${local_filedir}/${local_filename}")
+			extractcmd=$(unzip -qo -d "${extractdest}" "${local_filedir}/${local_filename}" 2>&1)
 		fi
 		fi
+	else
+		extractcmd="Unsupported archive type: ${mime}"
+		false
 	fi
 	fi
 	exitcode=$?
 	exitcode=$?
+	# Clean up after capturing the exit code so cleanup cannot mask a failed extraction or copy.
+	if [ -n "${temp_extractdir}" ]; then
+		rm -rf "${temp_extractdir}"
+		unset temp_extractdir
+	fi
 	if [ "${exitcode}" -ne 0 ]; then
 	if [ "${exitcode}" -ne 0 ]; then
 		fn_print_fail_eol_nl
 		fn_print_fail_eol_nl
 		fn_script_log_fail "Extracting ${local_filename}"
 		fn_script_log_fail "Extracting ${local_filename}"