[arch-projects] [dbscripts] [PATCH 2/3] ftpdir-cleanup, sourceballs: replace external find command with bash globbing

Eli Schwartz eschwartz at archlinux.org
Fri Feb 16 03:45:03 UTC 2018


This fully removes the use of find from the codebase, leads to a
micro-optimization in a couple cases,  and ensures that $PKGEXT is
consistently treated as a shell globbing character (which is important
because it is used as one).

Of the eight instances in these files:

- One was unnecessary as `cat` can natively consume all files passed to
  it and no directory traversal was in use.

- Two were unnecessary as they were hardcoded to read a single file....

- Another four were only being used to strip leading directory paths,
  and can be replaced by globstar and ${filepath##*/}

- The final two were checking the modification time of the files, and
  can be replaced with touch(1) and [[ -nt ]]. Although this introduces
  an additional temporary file, this is not such a big deal.

Signed-off-by: Eli Schwartz <eschwartz at archlinux.org>
---
 cron-jobs/ftpdir-cleanup | 24 ++++++++++++++++++------
 cron-jobs/sourceballs    | 21 ++++++++++++++++-----
 2 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/cron-jobs/ftpdir-cleanup b/cron-jobs/ftpdir-cleanup
index 2f3d5aa..2d33047 100755
--- a/cron-jobs/ftpdir-cleanup
+++ b/cron-jobs/ftpdir-cleanup
@@ -38,7 +38,11 @@ for repo in ${PKGREPOS[@]}; do
 			continue
 		fi
 		# get a list of actual available package files
-		find "${FTP_BASE}/${repo}/os/${arch}" -xtype f -name "*${PKGEXT}" -printf '%f\n' | sort > "${WORKDIR}/repo-${repo}-${arch}"
+		for f in "${FTP_BASE}"/${repo}/os/${arch}/*${PKGEXT}; do
+			if [[ -f $f ]]; then
+				printf '%s\n' "${f##*/}"
+			fi
+		done | sort > "${WORKDIR}/repo-${repo}-${arch}"
 		# get a list of package files defined in the repo db
 		bsdtar -xOf "${FTP_BASE}/${repo}/os/${arch}/${repo}${DBEXT}" | awk '/^%FILENAME%/{getline;print}' | sort > "${WORKDIR}/db-${repo}-${arch}"
 
@@ -61,10 +65,12 @@ for repo in ${PKGREPOS[@]}; do
 	done
 done
 
-# get a list of all available packages in the pacakge pool
-find "$FTP_BASE/${PKGPOOL}" -name "*${PKGEXT}" -printf '%f\n' | sort > "${WORKDIR}/pool"
+# get a list of all available packages in the package pool
+for f in "$FTP_BASE/${PKGPOOL}"/*${PKGEXT}; do
+	printf '%s\n' "${f##*/}"
+done | sort > "${WORKDIR}/pool"
 # create a list of packages in our db
-find "${WORKDIR}" -maxdepth 1 -type f -name 'db-*' -exec cat {} \; | sort -u > "${WORKDIR}/db"
+cat "${WORKDIR}"/db-* 2>/dev/null | sort -u > "${WORKDIR}/db"
 
 old_pkgs=($(comm -23 "${WORKDIR}/pool" "${WORKDIR}/db"))
 if [ ${#old_pkgs[@]} -ge 1 ]; then
@@ -75,8 +81,14 @@ if [ ${#old_pkgs[@]} -ge 1 ]; then
 	done
 fi
 
-old_pkgs=($(find ${CLEANUP_DESTDIR} -type f -name "*${PKGEXT}" -mtime +${CLEANUP_KEEP} -printf '%f\n'))
-if [ ${#old_pkgs[@]} -ge 1 ]; then
+unset old_pkgs
+touch -d "${CLEANUP_KEEP} days ago"  "${WORKDIR}/cleanup_timestamp"
+for f in "${CLEANUP_DESTDIR}"/**/*${PKGEXT}; do
+	if [[ ${WORKDIR}/cleanup_timestamp -nt $f ]]; then
+		old_pkgs+=("${f##*/}")
+	fi
+done
+if (( ${#old_pkgs[@]} > 1 )); then
 	msg "Removing old packages from the cleanup directory..."
 	for old_pkg in ${old_pkgs[@]}; do
 		msg2 "${old_pkg}"
diff --git a/cron-jobs/sourceballs b/cron-jobs/sourceballs
index 9ab4e98..5844817 100755
--- a/cron-jobs/sourceballs
+++ b/cron-jobs/sourceballs
@@ -46,7 +46,11 @@ for repo in ${PKGREPOS[@]}; do
 done
 
 # Create a list of all available source package file names
-find "${FTP_BASE}/${SRCPOOL}" -xtype f -name "*${SRCEXT}" -printf '%f\n' | sort -u > "${WORKDIR}/available-src-pkgs"
+for f in "${FTP_BASE}"/${SRCPOOL}/*${SRCEXT}; do
+	if [[ -f $f ]]; then
+		printf '%s\n' "${f##*/}"
+	fi
+done | sort -u > "${WORKDIR}/available-src-pkgs"
 
 # Check for all packages if we need to build a source package
 for repo in ${PKGREPOS[@]}; do
@@ -117,8 +121,8 @@ for repo in ${PKGREPOS[@]}; do
 done
 
 # Cleanup old source packages
-find "${WORKDIR}" -maxdepth 1 -type f -name 'expected-src-pkgs' -exec cat {} \; | sort -u > "${WORKDIR}/expected-src-pkgs.sort"
-find "${WORKDIR}" -maxdepth 1 -type f -name 'available-src-pkgs' -exec cat {} \; | sort -u > "${WORKDIR}/available-src-pkgs.sort"
+sort -u "${WORKDIR}/expected-src-pkgs" > "${WORKDIR}/expected-src-pkgs.sort"
+sort -u "${WORKDIR}/available-src-pkgs" > "${WORKDIR}/available-src-pkgs.sort"
 old_pkgs=($(comm -23 "${WORKDIR}/available-src-pkgs.sort" "${WORKDIR}/expected-src-pkgs.sort"))
 
 if [ ${#old_pkgs[@]} -ge 1 ]; then
@@ -133,8 +137,15 @@ if [ ${#old_pkgs[@]} -ge 1 ]; then
 	done
 fi
 
-old_pkgs=($(find ${SOURCE_CLEANUP_DESTDIR} -type f -name "*${SRCEXT}" -mtime +${SOURCE_CLEANUP_KEEP} -printf '%f\n'))
-if [ ${#old_pkgs[@]} -ge 1 ]; then
+unset old_pkgs
+touch -d "${SOURCE_CLEANUP_KEEP} days ago"  "${WORKDIR}/cleanup_timestamp"
+for f in "${SOURCE_CLEANUP_DESTDIR}"/*${SRCEXT}; do
+	if [[ ${WORKDIR}/cleanup_timestamp -nt $f ]]; then
+		old_pkgs+=("${f##*/}")
+	fi
+done
+
+if (( ${#old_pkgs[@]} > 1 )); then
 	msg "Removing old source packages from the cleanup directory..."
 	for old_pkg in ${old_pkgs[@]}; do
 		msg2 "${old_pkg}"
-- 
2.16.1


More information about the arch-projects mailing list