[RPM] rpmdevtools not found #38

Fix RPM Tools when working under Flatpak

Issue: https://github.com/flathub/org.eclipse.Java/issues/38
Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
Change-Id: I512c041dc8129cb9f2748d5d1a5d3daf34f7dc95
Reviewed-on: https://git.eclipse.org/r/c/linuxtools/org.eclipse.linuxtools/+/185613
Tested-by: Linux Tools Bot <linuxtools-bot@eclipse.org>
Reviewed-by: Alexander Kurtakov <akurtako@redhat.com>
diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/UiUtils.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/UiUtils.java
index d6a07ea..b0feb2a 100644
--- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/UiUtils.java
+++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/UiUtils.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2018 Alphonse Van Assche and others.
+ * Copyright (c) 2007, 2021 Alphonse Van Assche and others.
  *
  * This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License 2.0
@@ -42,7 +42,7 @@
 		boolean exists = (new File(PreferenceConstants.RPMMACRO_FILE)).exists();
 		// Check if ~/.rpmmacros exist, if the file don't exist we create
 		// it with the appropriate command.
-		if (!exists && Files.exists(Paths.get("/usr/bin/rpmdev-setuptree"))) { //$NON-NLS-1$
+		if (!exists && fileExists("/usr/bin/rpmdev-setuptree")) { //$NON-NLS-1$
 			org.eclipse.linuxtools.rpm.core.utils.Utils.runCommandToInputStream("rpmdev-setuptree"); //$NON-NLS-1$
 		}
 
@@ -50,11 +50,11 @@
 		IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE,
 				FrameworkUtil.getBundle(UiUtils.class).getSymbolicName());
 		String currentRpmTool = store.getString(PreferenceConstants.P_CURRENT_RPMTOOLS);
-		if (!Files.exists(Paths.get("/usr/bin/yum"))) { //$NON-NLS-1$
+		if (!fileExists("/usr/bin/yum")) { //$NON-NLS-1$
 			if (currentRpmTool.equals(PreferenceConstants.DP_RPMTOOLS_YUM)) {
 				store.setValue(PreferenceConstants.P_CURRENT_RPMTOOLS, PreferenceConstants.DP_RPMTOOLS_RPM);
 			}
-		} else if (!Files.exists(Paths.get("/usr/bin/urpmq"))) { //$NON-NLS-1$
+		} else if (!fileExists("/usr/bin/urpmq")) { //$NON-NLS-1$
 			if (currentRpmTool.equals(PreferenceConstants.DP_RPMTOOLS_URPM)) {
 				store.setValue(PreferenceConstants.P_CURRENT_RPMTOOLS, PreferenceConstants.DP_RPMTOOLS_RPM);
 			}
@@ -99,4 +99,33 @@
 	public static String getPackageDefineId(String defineName, SpecfilePackage rpmPackage) {
 		return defineName.toLowerCase() + ':' + rpmPackage.getPackageName();
 	}
+
+	/**
+	 * Detects if we're running under Flatpak.
+	 *
+	 * @return Returns true in case we're running under Flatpak, otherwise - false
+	 */
+	public static boolean isFlatpak() {
+		return (System.getenv("FLATPAK_SANDBOX_DIR") != null); //$NON-NLS-1$
+	}
+
+	/**
+	 * Flatpak Sandbox mapping path
+	 */
+	public static final String SANDBOX_MAPPING_PATHNAME = "/var/run/host"; //$NON-NLS-1$
+
+	/**
+	 * Detects if a file exists under its normal path or, in case of Flatpak, in
+	 * Sandbox mapped path
+	 *
+	 * @param absPath A file path to check
+	 *
+	 * @return Returns true a file exists, otherwise - false
+	 */
+	public static boolean fileExists(String absPath) {
+		if (Files.exists(Paths.get(absPath)))
+			return true;
+
+		return (isFlatpak() && Files.exists(Paths.get(SANDBOX_MAPPING_PATHNAME + absPath)));
+	}
 }
diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/preferences/RpmProposalsPreferencePage.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/preferences/RpmProposalsPreferencePage.java
index 233d7ca..b4bc237 100644
--- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/preferences/RpmProposalsPreferencePage.java
+++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/preferences/RpmProposalsPreferencePage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2018 Alphonse Van Assche and others.
+ * Copyright (c) 2007, 2021 Alphonse Van Assche and others.
  *
  * This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License 2.0
@@ -25,6 +25,7 @@
 import org.eclipse.jface.preference.StringFieldEditor;
 import org.eclipse.jface.util.PropertyChangeEvent;
 import org.eclipse.linuxtools.internal.rpm.ui.editor.Activator;
+import org.eclipse.linuxtools.internal.rpm.ui.editor.UiUtils;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.widgets.Composite;
@@ -81,10 +82,10 @@
 		 * Show only installed tools. Don't forgot to add sanity check in
 		 * UiUtils.pluginSanityCheck().
 		 */
-		if (Files.exists(Paths.get("/usr/bin/yum"))) { //$NON-NLS-1$
+		if (UiUtils.fileExists("/usr/bin/yum")) { //$NON-NLS-1$
 			list.add(new String[] { Messages.RpmProposalsPreferencePage_4, PreferenceConstants.DP_RPMTOOLS_YUM });
 		}
-		if (Files.exists(Paths.get("/usr/bin/urpmq"))) { //$NON-NLS-1$
+		if (UiUtils.fileExists("/usr/bin/urpmq")) { //$NON-NLS-1$
 			list.add(new String[] { Messages.RpmProposalsPreferencePage_5, PreferenceConstants.DP_RPMTOOLS_URPM });
 		}
 
@@ -97,14 +98,13 @@
 
 		}
 
-		return new RadioGroupFieldEditor(
-				PreferenceConstants.P_CURRENT_RPMTOOLS, Messages.RpmProposalsPreferencePage_6, 1, radioItems,
-				getFieldEditorParent(), true);
+		return new RadioGroupFieldEditor(PreferenceConstants.P_CURRENT_RPMTOOLS, Messages.RpmProposalsPreferencePage_6,
+				1, radioItems, getFieldEditorParent(), true);
 	}
 
 	private FieldEditor buildTimeListRateFieldEditor() {
-		return new RadioGroupFieldEditor(
-				PreferenceConstants.P_RPM_LIST_BUILD_PERIOD, Messages.RpmProposalsPreferencePage_7, 1,
+		return new RadioGroupFieldEditor(PreferenceConstants.P_RPM_LIST_BUILD_PERIOD,
+				Messages.RpmProposalsPreferencePage_7, 1,
 				new String[][] { { Messages.RpmProposalsPreferencePage_8, "1" }, //$NON-NLS-1$
 						{ Messages.RpmProposalsPreferencePage_10, "2" }, //$NON-NLS-1$
 						{ Messages.RpmProposalsPreferencePage_12, "3" } }, //$NON-NLS-1$
diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/wizards/SpecfileNewWizard.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/wizards/SpecfileNewWizard.java
index 7356f4c..fb30288 100644
--- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/wizards/SpecfileNewWizard.java
+++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/internal/rpm/ui/editor/wizards/SpecfileNewWizard.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2018 Alphonse Van Assche and others.
+ * Copyright (c) 2007, 2021 Alphonse Van Assche and others.
  *
  * This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License 2.0
@@ -18,8 +18,6 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.InvocationTargetException;
-import java.nio.file.Files;
-import java.nio.file.Paths;
 
 import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
@@ -38,6 +36,7 @@
 import org.eclipse.jface.wizard.Wizard;
 import org.eclipse.linuxtools.internal.rpm.ui.editor.Activator;
 import org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileLog;
+import org.eclipse.linuxtools.internal.rpm.ui.editor.UiUtils;
 import org.eclipse.linuxtools.rpm.ui.editor.wizards.Messages;
 import org.eclipse.linuxtools.rpm.ui.editor.wizards.SpecfileNewWizardPage;
 import org.eclipse.ui.INewWizard;
@@ -63,7 +62,7 @@
 	 */
 	@Override
 	public void addPages() {
-		if (!Files.exists(Paths.get("/usr/bin/rpmdev-newspec"))) { //$NON-NLS-1$
+		if (!UiUtils.fileExists("/usr/bin/rpmdev-newspec")) { //$NON-NLS-1$
 			addPage(new NoExecutableWizardPage());
 		} else {
 			page = new SpecfileNewWizardPage(selection);
diff --git a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/rpm/ui/editor/wizards/SpecfileNewWizardPage.java b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/rpm/ui/editor/wizards/SpecfileNewWizardPage.java
index 5a9842d..5fcffb8 100644
--- a/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/rpm/ui/editor/wizards/SpecfileNewWizardPage.java
+++ b/rpm/org.eclipse.linuxtools.rpm.ui.editor/src/org/eclipse/linuxtools/rpm/ui/editor/wizards/SpecfileNewWizardPage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2007, 2018 Alphonse Van Assche and others.
+ * Copyright (c) 2007, 2021 Alphonse Van Assche and others.
  *
  * This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License 2.0
@@ -35,6 +35,7 @@
 import org.eclipse.jface.wizard.WizardPage;
 import org.eclipse.linuxtools.internal.rpm.ui.editor.Activator;
 import org.eclipse.linuxtools.internal.rpm.ui.editor.SpecfileLog;
+import org.eclipse.linuxtools.internal.rpm.ui.editor.UiUtils;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.layout.GridData;
@@ -367,8 +368,11 @@
 	private void populateTemplateCombo(Combo templateCombo) throws CoreException {
 		// get a list of all files in a directory
 		File dir = new File("/etc/rpmdevtools"); //$NON-NLS-1$
-		String[] files = dir.list();
+		if (!dir.exists() && UiUtils.isFlatpak()) {
+			dir = new File(UiUtils.SANDBOX_MAPPING_PATHNAME + "/etc/rpmdevtools"); //$NON-NLS-1$
+		}
 		if (dir.exists()) {
+			String[] files = dir.list();
 			String templateCSV = ""; //$NON-NLS-1$
 			for (String file : files) {
 				if (file.startsWith("spectemplate-")) { //$NON-NLS-1$