From 3ea5b7fb451f009b5324dfdf111f7baffa71c6aa Mon Sep 17 00:00:00 2001 From: nathan Date: Sat, 27 Oct 2018 16:04:42 +0200 Subject: [PATCH] Added ability to check if package is installed --- src/dorkbox/util/OSUtil.java | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/dorkbox/util/OSUtil.java b/src/dorkbox/util/OSUtil.java index 1594f1e..2eb44c6 100644 --- a/src/dorkbox/util/OSUtil.java +++ b/src/dorkbox/util/OSUtil.java @@ -452,6 +452,62 @@ class OSUtil { return isSudoOrRoot; } + + + /** + * @return true if the package is installed + */ + public static + boolean isPackageInstalled(final String packageName) { + // dpkg + // dpkg -L libappindicator3 + // dpkg-query: package 'libappindicator3' is not installed + boolean is_dpkg = new File("/usr/bin/dpkg").canExecute(); + if (is_dpkg) { + final ShellExecutor shell = new ShellExecutor(); + shell.setExecutable("dpkg"); + shell.addArgument("-L"); + shell.addArgument(packageName); + shell.start(); + + String output = shell.getOutput(); + return !output.contains("is not installed"); + } + + // rpm + // rpm -q libappindicator234 + // package libappindicator234 is not installed + boolean is_rpm = new File("/usr/bin/rpm").canExecute(); + if (is_rpm) { + final ShellExecutor shell = new ShellExecutor(); + shell.setExecutable("rpm"); + shell.addArgument("-q"); + shell.addArgument(packageName); + shell.start(); + + String output = shell.getOutput(); + return !output.contains("is not installed"); + } + + + // pacman + // pacman -Qi + // use the exit code to determine if the packages existes on the system or not (0 the package exists, 1 it doesn't) + boolean is_pacmac = new File("/usr/bin/pacman").canExecute(); + if (is_rpm) { + final ShellExecutor shell = new ShellExecutor(); + shell.setExecutable("pacman"); + shell.addArgument("-Qi"); + shell.addArgument(packageName); + int start = shell.start(); + + // 0 the package exists, 1 it doesn't + return start == 0; + } + + return false; + } + } @SuppressWarnings("WeakerAccess")