Skip to content

Commit b419f8c

Browse files
committed
wip
1 parent c0e8c86 commit b419f8c

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

src/ci/scripts/free-disk-space.sh

+20-24
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,31 @@
55

66
# print a line of the specified character
77
printSeparationLine() {
8-
for ((i=0; i < 80; i++)); do
9-
printf "$1"
8+
for ((i = 0; i < 80; i++)); do
9+
printf "%s" "$1"
1010
done
1111
printf "\n"
1212
}
1313

1414
# compute available space
1515
# REF: https://unix.stackexchange.com/a/42049/60849
1616
# REF: https://stackoverflow.com/a/450821/408734
17-
getAvailableSpace() { echo $(df -a $1 | awk 'NR > 1 {avail+=$4} END {print avail}'); }
17+
getAvailableSpace() { echo $(df -a | awk 'NR > 1 {avail+=$4} END {print avail}'); }
1818

1919
# make Kb human readable (assume the input is Kb)
2020
# REF: https://unix.stackexchange.com/a/44087/60849
2121
formatByteCount() { echo $(numfmt --to=iec-i --suffix=B --padding=7 $1'000'); }
2222

2323
# macro to output saved space
2424
printSavedSpace() {
25-
local saved=${1}
25+
# Disk space before the operation
26+
local before=${1}
2627
local title=${2:-}
2728

29+
local after
30+
after=$(getAvailableSpace)
31+
local saved=$((after - before))
32+
2833
echo ""
2934
printSeparationLine "*"
3035
if [ -n "${title}" ]; then
@@ -43,33 +48,32 @@ printDF() {
4348
printSeparationLine "="
4449
echo "${caption}"
4550
echo ""
46-
echo "$ df -h /"
51+
echo "$ df -h"
4752
echo ""
48-
df -h /
53+
df -h
4954
printSeparationLine "="
5055
}
5156

5257
removeDir() {
5358
dir=${1}
5459

55-
local before=$(getAvailableSpace)
60+
local before
61+
before=$(getAvailableSpace)
5662

5763
sudo rm -rf "$dir" || true
5864

59-
local after=$(getAvailableSpace)
60-
local saved=$((after-before))
61-
printSavedSpace $saved "$dir"
65+
printSavedSpace "$before" "$dir"
6266
}
6367

6468
execAndMeasureSpaceChange() {
65-
local operation=${1} # Function to execute
69+
local operation=${1} # Function to execute
6670
local title=${2}
6771

68-
local before=$(getAvailableSpace)
72+
local before
73+
before=$(getAvailableSpace)
6974
$operation
70-
local after=$(getAvailableSpace)
71-
local saved=$((after-before))
72-
printSavedSpace $saved "$title"
75+
76+
printSavedSpace "$before" "$title"
7377
}
7478

7579
# Remove large packages
@@ -118,7 +122,6 @@ cleanSwap() {
118122
# Display initial disk space stats
119123

120124
AVAILABLE_INITIAL=$(getAvailableSpace)
121-
AVAILABLE_ROOT_INITIAL=$(getAvailableSpace '/')
122125

123126
printDF "BEFORE CLEAN-UP:"
124127
echo ""
@@ -135,17 +138,10 @@ execAndMeasureSpaceChange cleanDocker "Docker images"
135138
execAndMeasureSpaceChange cleanSwap "Swap storage"
136139

137140
# Output saved space statistic
138-
139-
AVAILABLE_END=$(getAvailableSpace)
140-
AVAILABLE_ROOT_END=$(getAvailableSpace '/')
141-
142141
echo ""
143142
printDF "AFTER CLEAN-UP:"
144143

145144
echo ""
146145
echo ""
147146

148-
echo "/dev/root:"
149-
printSavedSpace $((AVAILABLE_ROOT_END - AVAILABLE_ROOT_INITIAL))
150-
echo "overall:"
151-
printSavedSpace $((AVAILABLE_END - AVAILABLE_INITIAL))
147+
printSavedSpace "$AVAILABLE_INITIAL" "Total saved"

0 commit comments

Comments
 (0)