[PATCH] contrib: Cleanup downloaded archives in download_prerequisites

Даниил Гусев <[email protected]>
Newsgroups gmane.comp.gcc.patches
Message-ID <[email protected]>
From c8b1c009cf24145c0ae6521dfb466931839cd181 Mon Sep 17 00:00:00 2001
From: Daniil-Gusev <[email protected]>
Date: Thu, 30 Jul 2026 19:25:39 +0300
Subject: [PATCH] contrib: Cleanup downloaded archives in
 download_prerequisites

By default, download_prerequisites left downloaded .tar archives in the
destination directory after extraction. Clean them up after successful
extraction, restoring the behavior of earlier script versions.

Add --save-archives (and --no-save-archives) options to preserve downloaded
archives when explicitly requested.

Avoid re-downloading, verifying, and extracting archives if target component
directories already exist and are not empty.

contrib/ChangeLog:

	* download_prerequisites (dir_has_content): New function.
	(save_archives): New variable.
	Document and handle --save-archives and --no-save-archives options.
	Skip download, verify, and extract steps if the target directory
	already has content. Delete downloaded archives at the end unless
	save_archives is set.
---
 contrib/download_prerequisites | 40 +++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)

diff --git a/contrib/download_prerequisites b/contrib/download_prerequisites
index 2e6693a2a..738018689 100755
--- a/contrib/download_prerequisites
+++ b/contrib/download_prerequisites
@@ -44,10 +44,16 @@ echo_archives() {
     if [ ${graphite} -gt 0 ]; then echo "${isl}"; fi
 }
 
+# 1 if the directory is empty or not exists
+dir_has_content() {
+    [ -d "$1" ] && [ -n "$(ls "$1" 2>/dev/null)" ]
+}
+
 graphite=1
 verify=1
 force=0
 only_gettext=false
+save_archives=0
 OS=$(uname)
 
 die() {
@@ -84,6 +90,8 @@ The following options are available:
  --sha512         use SHA512 checksum to verify package integrity (default)
  --md5            use MD5 checksum to verify package integrity
  --only-gettext   inhibit downloading any package but gettext
+ --save-archives  do not delete downloaded archive files after extraction
+ --no-save-archives delete downloaded archive files after extraction (default)
  --help           show this text and exit
  --version        show version information and exit
 "
@@ -167,6 +175,12 @@ do
             --only-gettext)
                 only_gettext=true
                 ;;
+	    --save-archives)
+		save_archives=1
+		;;
+	    --no-save-archives)
+		save_archives=0
+		;;
             -*)
                 die "unknown option: ${arg}"
                 ;;
@@ -231,12 +245,15 @@ esac
 
 for ar in $(echo_archives)
 do
-    if [ ${force} -gt 0 ]; then rm -f "${directory}/${ar}"; fi
+    dir_has_content "${directory:?}/${ar%.tar*}"; hascontent=$?
+    if [ ${force} -gt 0 ]; then
+	rm -f "${directory:?}/${ar}"
+     elif [ ${hascontent} -eq 0 ]; then continue; fi
     [ -e "${directory}/${ar}" ]                                               \
         || ( cd "${directory}" && ${fetch} --no-verbose "${base_url}${ar}" )  \
         || die "Cannot download ${ar} from ${base_url}"
 done
-unset ar
+unset ar hascontent
 
 if [ ${verify} -gt 0 ]
 then
@@ -244,18 +261,23 @@ then
     [ -r "${chksumfile}" ] || die "No checksums available"
     for ar in $(echo_archives)
     do
+	dir_has_content "${directory:?}/${ar%.tar*}"; hascontent=$?
+	if [ ${hascontent} -eq 0 ]; then continue; fi
         grep "${ar}" "${chksumfile}"                                          \
             | ( cd "${directory}" && ${chksum} )                              \
             || die "Cannot verify integrity of possibly corrupted file ${ar}"
     done
     unset chksumfile
 fi
-unset ar
+unset ar hascontent
 
 for ar in $(echo_archives)
 do
     package="${ar%.tar*}"
-    if [ ${force} -gt 0 ]; then rm -rf "${directory}/${package}"; fi
+    dir_has_content "${directory:?}/${package}"; hascontent=$?
+    if [ ${force} -gt 0 ] || [ ${hascontent} -gt 0 ]; then
+	rm -rf "${directory:?}/${package}"
+    elif [ ${hascontent} -eq 0 ]; then continue; fi
     case $ar in
     *.gz)
 	uncompress='gzip -d'
@@ -272,7 +294,7 @@ do
         || die "Cannot extract package from ${ar}"
     unset package
 done
-unset ar
+unset ar hascontent
 
 for ar in $(echo_archives)
 do
@@ -286,4 +308,12 @@ do
 done
 unset ar
 
+for ar in $(echo_archives)
+do
+    if [ ${save_archives} -eq 0 ]; then
+	rm -f "${directory:?}/${ar}"
+    fi
+done
+unset ar
+
 echo "All prerequisites downloaded successfully."
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.