public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Ulrich Müller" <ulm@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] data/api:master commit in: bin/
Date: Sat, 13 Nov 2021 20:09:38 +0000 (UTC)	[thread overview]
Message-ID: <1636834124.dfe2af855cc35367bc2eae5d4be240d0c56515ba.ulm@gentoo> (raw)

commit:     dfe2af855cc35367bc2eae5d4be240d0c56515ba
Author:     Jaco Kroon <jaco <AT> uls <DOT> co <DOT> za>
AuthorDate: Fri Nov 12 18:24:20 2021 +0000
Commit:     Ulrich Müller <ulm <AT> gentoo <DOT> org>
CommitDate: Sat Nov 13 20:08:44 2021 +0000
URL:        https://gitweb.gentoo.org/data/api.git/commit/?id=dfe2af85

used_free_uidgids.sh: permit multiple ranges, and adjust stats accordingly.

Closes: https://github.com/gentoo/api-gentoo-org/pull/445
Signed-off-by: Jaco Kroon <jaco <AT> uls.co.za>
[For now, comment out the additional ranges.]
Signed-off-by: Ulrich Müller <ulm <AT> gentoo.org>

 bin/used_free_uidgids.sh | 205 +++++++++++++++++++++++++++++------------------
 1 file changed, 127 insertions(+), 78 deletions(-)

diff --git a/bin/used_free_uidgids.sh b/bin/used_free_uidgids.sh
index bbc04e9..ff5fa31 100755
--- a/bin/used_free_uidgids.sh
+++ b/bin/used_free_uidgids.sh
@@ -7,12 +7,15 @@
 # So that you can contact me if you need help with the below insanity.
 #
 # Configuration options:
-# max => maximum value of uid/gid that we're interested in/willing to allocate
-#	from.  Can be set to - to go maximum possible 32-bit value.
+# selection_ranges => an array of start-stop values.  There is an assumption of
+#  incremental ordering, ie, start values should be in incrementing order, and
 # debug => if non-zero outputs some cryptic debug output (will inherit from environment).
 #
-min=101
-max=499
+selection_ranges=(
+	499-101
+	#500-799
+	#60001-60999
+)
 debug=${debug:+1} # set non-zero to enable debug output.
 
 #
@@ -172,93 +175,139 @@ fi
 
 ui=0 # index into uids array.
 gi=0 # index into gids array.
-idbase=${min} # "start" of range about to be output.
-freeuid=0 # count number of free UIDs
-freegid=0 # count number of free GIDs
-freepair=0 # count number of free UID+GID pairs.
 
-printf "%-*s%10s%10s\n" $(( ${#max} * 2 + 5 )) "#ID" UID GID
+free_total_uid=0
+free_total_gid=0
+free_total_pair=0
 
-while [[ ${idbase} -le ${max} ]]; do
-	# skip over uid and gid ranges that we're no longer interested in (end of range is
-	# lower than start of output range).
-	while [[ ${ui} -lt ${#uids[@]} && ${ranges_uid[uids[ui]]} -lt ${idbase} ]]; do
-		(( ui++ ))
-	done
-	while [[ ${gi} -lt ${#gids[@]} && ${ranges_gid[gids[gi]]} -lt ${idbase} ]]; do
-		(( gi++ ))
-	done
-	# Assume that range we're going to output is the remainder of the legal
-	# space we're interested in, and then adjust downwards as needed.  For each
-	# of the UID and GID space, if the start range is beyond the current output
-	# start we're looking at a FREE range, so downward adjust re (range end) to
-	# the next non-FREE range's start - 1, or if we're in the non-FREE range,
-	# adjust downward to that range's end.
-	re=${max}
-	uid_start=-1
-	gid_start=-1
-	if [[ ${ui} -lt ${#uids[@]} ]]; then
-		uid_start=${uids[ui]}
-		if [[ ${uid_start} -gt ${idbase} && ${uid_start} -le ${re} ]]; then
-			re=$(( ${uid_start} - 1 ))
-		fi
-		if [[ ${ranges_uid[uid_start]} -lt ${re} ]]; then
-			re=${ranges_uid[uid_start]}
-		fi
+for r in "${selection_ranges[@]}"; do
+	min=${r%-*} # "start" of range about to be output.
+	max=${r#*-} # "end" of range about to be output.
+	selection=min
+	if [[ $max -lt $min ]]; then
+		selection=max
+		t=${max}
+		max=${min}
+		min=${t}
 	fi
-	if [[ ${gi} -lt ${#gids[@]} ]]; then
-		gid_start=${gids[gi]}
-		if [[ ${gid_start} -gt ${idbase} && ${gid_start} -le ${re} ]]; then
-			re=$(( ${gid_start} - 1 ))
+
+	freeuid=0 # count number of free UIDs
+	freegid=0 # count number of free GIDs
+	freepair=0 # count number of free UID+GID pairs.
+
+	echo "Range: ${min}..${max} (${selection})"
+	printf "%-*s%10s%10s\n" $(( ${#max} * 2 + 5 )) "#ID" UID GID
+
+	idbase=${min}
+	while [[ ${idbase} -le ${max} ]]; do
+		# skip over uid and gid ranges that we're no longer interested in (end of range is
+		# lower than start of output range).
+		while [[ ${ui} -lt ${#uids[@]} && ${ranges_uid[uids[ui]]} -lt ${idbase} ]]; do
+			(( ui++ ))
+		done
+		while [[ ${gi} -lt ${#gids[@]} && ${ranges_gid[gids[gi]]} -lt ${idbase} ]]; do
+			(( gi++ ))
+		done
+		# Assume that range we're going to output is the remainder of the legal
+		# space we're interested in, and then adjust downwards as needed.  For each
+		# of the UID and GID space, if the start range is beyond the current output
+		# start we're looking at a FREE range, so downward adjust re (range end) to
+		# the next non-FREE range's start - 1, or if we're in the non-FREE range,
+		# adjust downward to that range's end.
+		re=${max}
+		uid_start=-1
+		gid_start=-1
+		if [[ ${ui} -lt ${#uids[@]} ]]; then
+			uid_start=${uids[ui]}
+			if [[ ${uid_start} -gt ${idbase} && ${uid_start} -le ${re} ]]; then
+				re=$(( ${uid_start} - 1 ))
+			fi
+			if [[ ${ranges_uid[uid_start]} -lt ${re} ]]; then
+				re=${ranges_uid[uid_start]}
+			fi
 		fi
-		if [[ ${ranges_gid[gid_start]} -lt ${re} ]]; then
-			re=${ranges_gid[gid_start]}
+		if [[ ${gi} -lt ${#gids[@]} ]]; then
+			gid_start=${gids[gi]}
+			if [[ ${gid_start} -gt ${idbase} && ${gid_start} -le ${re} ]]; then
+				re=$(( ${gid_start} - 1 ))
+			fi
+			if [[ ${ranges_gid[gid_start]} -lt ${re} ]]; then
+				re=${ranges_gid[gid_start]}
+			fi
 		fi
-	fi
-
-	# If we're debugging, just dump various variables above, which allows
-	# validating that the above logic works correctly.
-	[[ -n "${debug}" ]] && echo "ui=${ui} (${uid_start}..${ranges_uid[uid_start]}), gi=${gi} (${gid_start}..${ranges_gid[gid_start]}), idbase=${idbase}, re=${re}"
 
-	# Determine the state of the UID and GID ranges.
-	if [[ ${ui} -lt ${#uids[@]} && ${uid_start} -le ${idbase} ]]; then
-		uidstate="${reason_uid[uid_start]}"
-	else
-		uidstate=FREE
-		freeuid=$(( freeuid + re - idbase + 1 ))
-	fi
+		# If we're debugging, just dump various variables above, which allows
+		# validating that the above logic works correctly.
+		[[ -n "${debug}" ]] && echo "ui=${ui} (${uid_start}..${ranges_uid[uid_start]}), gi=${gi} (${gid_start}..${ranges_gid[gid_start]}), idbase=${idbase}, re=${re}"
 
-	if [[ ${gi} -lt ${#gids[@]} && ${gid_start} -le ${idbase} ]]; then
-		gidstate="${reason_gid[gids[gi]]}"
-	else
-		gidstate=FREE
-		freegid=$(( freegid + re - idbase + 1 ))
-	fi
+		# Determine the state of the UID and GID ranges.
+		if [[ ${ui} -lt ${#uids[@]} && ${uid_start} -le ${idbase} ]]; then
+			uidstate="${reason_uid[uid_start]}"
+		else
+			uidstate=FREE
+			freeuid=$(( freeuid + re - idbase + 1 ))
+		fi
 
-	# If the ranges are FREE (or at least one of), adjust selection recommendations
-	# accordingly.
-	if [[ "${gidstate}" == FREE ]]; then
-		if [[ "${uidstate}" == FREE ]]; then
-			uidgidboth=${re}
-			freepair=$(( freepair + re - idbase + 1 ))
+		if [[ ${gi} -lt ${#gids[@]} && ${gid_start} -le ${idbase} ]]; then
+			gidstate="${reason_gid[gids[gi]]}"
 		else
-			gidonly=${re}
+			gidstate=FREE
+			freegid=$(( freegid + re - idbase + 1 ))
+		fi
+
+		# If the ranges are FREE (or at least one of), adjust selection recommendations
+		# accordingly.
+		if [[ "${gidstate}" == FREE ]]; then
+			if [[ "${uidstate}" == FREE ]]; then
+				case "${selection}" in
+					min)
+						[[ -z "${uidgidboth}" ]] && uidgidboth=${idbase}
+						;;
+					max)
+						[[ -z "${uidgidboth}" || ${uidgidboth} -ge ${min} ]] && uidgidboth=${re}
+						;;
+				esac
+				freepair=$(( freepair + re - idbase + 1 ))
+			else
+				case "${selection}" in
+					min)
+						[[ -z "${gidonly}" ]] && gidonly=${idbase}
+						;;
+					max)
+						[[ -z "${gidonly}" || ${gidonly} -ge ${min} ]] && gidonly=${re}
+						;;
+				esac
+			fi
+		elif [[ "${uidstate}" == FREE ]]; then
+			case "${selection}" in
+				min)
+					[[ -z "${uidonly}" ]] && uidonly=${idbase}
+					;;
+				max)
+					[[ -z "${uidonly}" || ${uidonly} -ge ${min} ]] && uidonly=${re}
+					;;
+			esac
 		fi
-	elif [[ "${uidstate}" == FREE ]]; then
-		uidonly=${re}
-	fi
 
-	vn="colour_${uidstate}"
-	colour_uid="${!vn}"
-	vn="colour_${gidstate}"
-	colour_gid="${!vn}"
-	printf "%-*s${colour_uid}%10s${colour_gid}%10s${colour_RESET}\n" $(( ${#max} * 2 + 5 )) "${idbase}$([[ ${re} -gt ${idbase} ]] && echo "..${re}")" "${uidstate}" "${gidstate}"
-	idbase=$(( re + 1 ))
+		vn="colour_${uidstate}"
+		colour_uid="${!vn}"
+		vn="colour_${gidstate}"
+		colour_gid="${!vn}"
+		printf "%-*s${colour_uid}%10s${colour_gid}%10s${colour_RESET}\n" $(( ${#max} * 2 + 5 )) "${idbase}$([[ ${re} -gt ${idbase} ]] && echo "..${re}")" "${uidstate}" "${gidstate}"
+		idbase=$(( re + 1 ))
+	done
+	echo "Range Free UIDs: ${freeuid}"
+	echo "Range Free GIDs: ${freegid}"
+	echo "Range Free UID+GID pairs: ${freepair}"
+	echo
+	(( free_total_uid += freeuid ))
+	(( free_total_gid += freegid ))
+	(( free_total_pair += freepair ))
 done
 
 echo "Recommended GID only: ${gidonly:-${uidgidboth:-none}}"
 echo "Recommended UID only: ${uidonly:=${uidgidboth:-none}}"
 echo "Recommended UID+GID pair: ${uidgidboth:-none}"
-echo "Free UIDs: ${freeuid}"
-echo "Free GIDs: ${freegid}"
-echo "Free UID+GID pairs: ${freepair}"
+echo "Free UIDs: ${free_total_uid}"
+echo "Free GIDs: ${free_total_gid}"
+echo "Free UID+GID pairs: ${free_total_pair}"


             reply	other threads:[~2021-11-13 20:09 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-13 20:09 Ulrich Müller [this message]
  -- strict thread matches above, loose matches on Subject: below --
2023-07-25  2:24 [gentoo-commits] data/api:master commit in: bin/ Sam James
2021-11-18 11:19 Ulrich Müller
2021-11-11 10:47 Ulrich Müller
2021-02-17 15:39 Joonas Niilola
2021-02-10 11:13 Joonas Niilola
2021-02-10 11:13 Joonas Niilola
2021-02-09 17:23 Joonas Niilola
2021-02-09  7:17 Alec Warner
2021-01-24 10:12 Michał Górny
2021-01-24 10:12 Michał Górny
2019-12-08 20:59 Michał Górny
2019-09-11  8:21 Michał Górny
2019-09-11  8:02 Michał Górny
2019-08-24 17:58 Michał Górny
2018-08-07  7:52 Michał Górny
2015-12-26 17:42 Alex Legler
2015-08-15 22:01 Alex Legler
2015-08-15 13:14 Alex Legler
2015-08-15 13:11 Alex Legler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1636834124.dfe2af855cc35367bc2eae5d4be240d0c56515ba.ulm@gentoo \
    --to=ulm@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox