Fix off by one error in default update script

This commit is contained in:
scorpion-26 2023-05-05 12:47:40 +02:00
parent 1c6cd3bd2b
commit 259083a904
4 changed files with 30 additions and 7 deletions

View file

@ -54,8 +54,9 @@ AudioScrollSpeed: 5
# Command that is run to check if there are out-of-date packages. # Command that is run to check if there are out-of-date packages.
# The script should return *ONLY* a number. If it doesn't output a number, updates are no longer checked. # The script should return *ONLY* a number. If it doesn't output a number, updates are no longer checked.
# Default value is applicable for Arch Linux. (See src/Config.h for explanation on the default command) # Default value is applicable for Arch Linux. (See data/update.sh for a human-readable version)
CheckPackagesCommand: pac="$(checkupdates)"; if [ $? -eq 127 ] ; then exit 127; fi; echo -n "$pac" | wc -l CheckPackagesCommand: p="$(checkupdates)"; e=$?; if [ $e -eq 127 ] ; then exit 127; fi; if [ $e -eq 2 ] ; then echo "0" && exit 0; fi; echo "$p" | wc -l
# How often to check for updates. In seconds # How often to check for updates. In seconds
CheckUpdateInterval: 300 CheckUpdateInterval: 300

23
data/update.sh Executable file
View file

@ -0,0 +1,23 @@
#/bin/sh
# This script is not used by default. It is a human-readable version of the default Arch-applicable
# package command with explanations
updates="$(checkupdates)";
exitCode=$?;
if [ $exitCode -eq 127 ] ; then
# checkupdates wasn't found.
# Forward the error to gBar, so gBar can shut down the widget
# This is done, so we don't bother non-Arch systems with update checking
exit 127;
fi
if [ $exitCode -eq 2 ] ; then
# Zero packages out-of-date. We need to handle this case, since 'echo "$updates" | wc -l' would return 1
echo "0" && exit 0
fi
# We need the extra newline (-n option omitted), since 'echo -n $"updates" | wc -l' is off by one,
# since 'echo -n $"updates"' has a \0 at the end
echo "$updates" | wc -l

View file

@ -17,9 +17,8 @@ public:
std::string defaultWorkspaceSymbol = ""; std::string defaultWorkspaceSymbol = "";
// Script that returns how many packages are out-of-date. The script should only print a number! // Script that returns how many packages are out-of-date. The script should only print a number!
// The default script runs checkupdates, and forcefully exits when checkupdates is not found, so gBar can disable the package widget. // See data/update.sh for a human-readable version
// "checkupdates | wc -l" would always return 0 on stdout, which gBar accepts std::string checkPackagesCommand = "p=\"$(checkupdates)\"; e=$?; if [ $e -eq 127 ] ; then exit 127; fi; if [ $e -eq 2 ] ; then echo \"0\" && exit 0; fi; echo \"$p\" | wc -l";
std::string checkPackagesCommand = "pac=\"$(checkupdates)\"; if [ $? -eq 127 ] ; then exit 127; fi; echo -n \"$pac\" | wc -l";
bool centerTime = true; bool centerTime = true;
bool audioRevealer = false; bool audioRevealer = false;

View file

@ -563,10 +563,10 @@ namespace System
ASSERT(feof(pipe), "GetOutdatedPackages: Cannot read to eof!"); ASSERT(feof(pipe), "GetOutdatedPackages: Cannot read to eof!");
int exitCode = pclose(pipe) / 256; int exitCode = pclose(pipe) / 256;
if (exitCode == 127) if (exitCode != 0)
{ {
configMutex.lock(); configMutex.lock();
// Invalid script // Invalid script/error
LOG("GetOutdatedPackages: Invalid command. Disabling package widget!"); LOG("GetOutdatedPackages: Invalid command. Disabling package widget!");
RuntimeConfig::Get().hasPackagesScript = false; RuntimeConfig::Get().hasPackagesScript = false;
configMutex.unlock(); configMutex.unlock();