Skip to content

Commit

Permalink
8301856: Generated .spec file for RPM installers uninstalls desktop l…
Browse files Browse the repository at this point in the history
…auncher on update

Reviewed-by: almatvee
  • Loading branch information
Alexey Semenyuk committed Nov 27, 2023
1 parent 726f854 commit 7848ed7
Show file tree
Hide file tree
Showing 16 changed files with 377 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -63,9 +63,10 @@ final class DesktopIntegration extends ShellCustomAction {
private static final String COMMANDS_INSTALL = "DESKTOP_COMMANDS_INSTALL";
private static final String COMMANDS_UNINSTALL = "DESKTOP_COMMANDS_UNINSTALL";
private static final String SCRIPTS = "DESKTOP_SCRIPTS";
private static final String COMMON_SCRIPTS = "COMMON_SCRIPTS";

private static final List<String> REPLACEMENT_STRING_IDS = List.of(
COMMANDS_INSTALL, COMMANDS_UNINSTALL, SCRIPTS);
COMMANDS_INSTALL, COMMANDS_UNINSTALL, SCRIPTS, COMMON_SCRIPTS);

private DesktopIntegration(PlatformPackage thePackage,
Map<String, ? super Object> params,
Expand Down Expand Up @@ -229,8 +230,6 @@ protected Map<String, String> createImpl() throws IOException {
shellCommands.applyTo(data);
}

boolean needCleanupScripts = !associations.isEmpty();

// Take care of additional launchers if there are any.
// Process every additional launcher as the main application launcher.
// Collect shell commands to install/uninstall integration with desktop
Expand All @@ -241,10 +240,6 @@ protected Map<String, String> createImpl() throws IOException {
List<String> uninstallShellCmds = new ArrayList<>(Arrays.asList(
data.get(COMMANDS_UNINSTALL)));
for (var integration: nestedIntegrations) {
if (!integration.associations.isEmpty()) {
needCleanupScripts = true;
}

Map<String, String> launcherData = integration.create();

installShellCmds.add(launcherData.get(COMMANDS_INSTALL));
Expand All @@ -254,10 +249,8 @@ protected Map<String, String> createImpl() throws IOException {
data.put(COMMANDS_INSTALL, stringifyShellCommands(installShellCmds));
data.put(COMMANDS_UNINSTALL, stringifyShellCommands(uninstallShellCmds));

if (needCleanupScripts) {
// Pull in desktop_utils.sh scrips library.
data.put(SCRIPTS, stringifyTextFile("desktop_utils.sh"));
}
data.put(COMMON_SCRIPTS, stringifyTextFile("common_utils.sh"));
data.put(SCRIPTS, stringifyTextFile("desktop_utils.sh"));

return data;
}
Expand Down Expand Up @@ -295,16 +288,20 @@ private class ShellCommands {

registerDesktopFileCmd = String.join(" ", "xdg-desktop-menu",
"install", desktopFile.installPath().toString());
unregisterDesktopFileCmd = String.join(" ", "xdg-desktop-menu",
unregisterDesktopFileCmd = String.join(" ",
"do_if_file_belongs_to_single_package", desktopFile.
installPath().toString(), "xdg-desktop-menu",
"uninstall", desktopFile.installPath().toString());
}

void setFileAssociations() {
registerFileAssociationsCmd = String.join(" ", "xdg-mime",
"install",
mimeInfoFile.installPath().toString());
unregisterFileAssociationsCmd = String.join(" ", "xdg-mime",
"uninstall", mimeInfoFile.installPath().toString());
unregisterFileAssociationsCmd = String.join(" ",
"do_if_file_belongs_to_single_package", mimeInfoFile.
installPath().toString(), "xdg-mime", "uninstall",
mimeInfoFile.installPath().toString());

//
// Add manual cleanup of system files to get rid of
Expand All @@ -320,26 +317,26 @@ void setFileAssociations() {
// of non-existing desktop file.
//
String cleanUpCommand = String.join(" ",
"uninstall_default_mime_handler",
desktopFile.installPath().getFileName().toString(),
String.join(" ", getMimeTypeNamesFromFileAssociations()));
"do_if_file_belongs_to_single_package", desktopFile.
installPath().toString(),
"desktop_uninstall_default_mime_handler", desktopFile.
installPath().getFileName().toString(), String.join(
" ", getMimeTypeNamesFromFileAssociations()));

unregisterFileAssociationsCmd = stringifyShellCommands(
unregisterFileAssociationsCmd, cleanUpCommand);
}

void addIcon(String mimeType, Path iconFile) {
addIcon(mimeType, iconFile, getSquareSizeOfImage(iconFile.toFile()));
}

void addIcon(String mimeType, Path iconFile, int imgSize) {
imgSize = normalizeIconSize(imgSize);
final String dashMime = mimeType.replace('/', '-');
registerIconCmds.add(String.join(" ", "xdg-icon-resource",
"install", "--context", "mimetypes", "--size",
Integer.toString(imgSize), iconFile.toString(), dashMime));
unregisterIconCmds.add(String.join(" ", "xdg-icon-resource",
"uninstall", dashMime, "--size", Integer.toString(imgSize)));
unregisterIconCmds.add(String.join(" ",
"do_if_file_belongs_to_single_package", iconFile.toString(),
"xdg-icon-resource", "uninstall", dashMime, "--size",
Integer.toString(imgSize)));
}

void applyTo(Map<String, String> data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,6 +26,8 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static jdk.jpackage.internal.OverridableResource.createResource;
Expand All @@ -42,10 +44,24 @@ private LinuxLaunchersAsServices(PlatformPackage thePackage,
});
}

@Override
protected List<String> replacementStringIds() {
return LINUX_REPLACEMENT_STRING_IDS;
}

@Override
protected Map<String, String> createImpl() throws IOException {
var data = super.createImpl();
if (!data.isEmpty()) {
data.put(COMMON_SCRIPTS, stringifyTextFile("common_utils.sh"));
}
return data;
}

static ShellCustomAction create(PlatformPackage thePackage,
Map<String, Object> params) throws IOException {
if (StandardBundlerParam.isRuntimeInstaller(params)) {
return ShellCustomAction.nop(REPLACEMENT_STRING_IDS);
return ShellCustomAction.nop(LINUX_REPLACEMENT_STRING_IDS);
}
return new LinuxLaunchersAsServices(thePackage, params);
}
Expand Down Expand Up @@ -84,4 +100,16 @@ Path descriptorFilePath(Path root) {

private final static List<String> REQUIRED_PACKAGES = List.of("systemd",
"coreutils" /* /usr/bin/wc */, "grep");

private static final String COMMON_SCRIPTS = "COMMON_SCRIPTS";

private static final List<String> LINUX_REPLACEMENT_STRING_IDS;

static {
ArrayList<String> buf = new ArrayList<>();
buf.addAll(UnixLaunchersAsServices.REPLACEMENT_STRING_IDS);
buf.add(COMMON_SCRIPTS);

LINUX_REPLACEMENT_STRING_IDS = Collections.unmodifiableList(buf);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -155,7 +155,8 @@ public final Path execute(Map<String, ? super Object> params,
Map<String, String> data = createDefaultReplacementData(params);

for (var ca : customActions) {
data.putAll(ca.instance.create());
ShellCustomAction.mergeReplacementData(data, ca.instance.
create());
}

data.putAll(createReplacementData(params));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
file_belongs_to_single_package ()
{
if [ ! -e "$1" ]; then
false
elif [ "$package_type" = rpm ]; then
test `rpm -q --whatprovides "$1" | wc -l` = 1
elif [ "$package_type" = deb ]; then
test `dpkg -S "$1" | wc -l` = 1
else
exit 1
fi
}


do_if_file_belongs_to_single_package ()
{
local file="$1"
shift

if file_belongs_to_single_package "$file"; then
"$@"
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Remove $1 desktop file from the list of default handlers for $2 mime type
# in $3 file dumping output to stdout.
#
_filter_out_default_mime_handler ()
desktop_filter_out_default_mime_handler ()
{
local defaults_list="$3"

Expand Down Expand Up @@ -50,7 +50,7 @@ EOF
# in $1 file.
# Result is saved in $1 file.
#
_uninstall_default_mime_handler ()
desktop_uninstall_default_mime_handler_0 ()
{
local defaults_list=$1
shift
Expand All @@ -66,20 +66,20 @@ _uninstall_default_mime_handler ()
local v
local update=
for mime in "$@"; do
_filter_out_default_mime_handler "$desktop_file" "$mime" "$tmpfile1" > "$tmpfile2"
desktop_filter_out_default_mime_handler "$desktop_file" "$mime" "$tmpfile1" > "$tmpfile2"
v="$tmpfile2"
tmpfile2="$tmpfile1"
tmpfile1="$v"

if ! diff -q "$tmpfile1" "$tmpfile2" > /dev/null; then
update=yes
trace Remove $desktop_file default handler for $mime mime type from $defaults_list file
desktop_trace Remove $desktop_file default handler for $mime mime type from $defaults_list file
fi
done

if [ -n "$update" ]; then
cat "$tmpfile1" > "$defaults_list"
trace "$defaults_list" file updated
desktop_trace "$defaults_list" file updated
fi

rm -f "$tmpfile1" "$tmpfile2"
Expand All @@ -90,15 +90,15 @@ _uninstall_default_mime_handler ()
# Remove $1 desktop file from the list of default handlers for $@ mime types
# in all known system defaults lists.
#
uninstall_default_mime_handler ()
desktop_uninstall_default_mime_handler ()
{
for f in /usr/share/applications/defaults.list /usr/local/share/applications/defaults.list; do
_uninstall_default_mime_handler "$f" "$@"
desktop_uninstall_default_mime_handler_0 "$f" "$@"
done
}


trace ()
desktop_trace ()
{
echo "$@"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,3 @@ unregister_services ()
fi
done
}

file_belongs_to_single_package ()
{
if [ ! -e "$1" ]; then
false
elif [ "$package_type" = rpm ]; then
test `rpm -q --whatprovides "$1" | wc -l` = 1
elif [ "$package_type" = deb ]; then
test `dpkg -S "$1" | wc -l` = 1
else
exit 1
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ set -e
# the debian-policy package

package_type=deb
COMMON_SCRIPTS
LAUNCHER_AS_SERVICE_SCRIPTS

case "$1" in
install|upgrade)
if [ -n "$2" ]; then
true; LAUNCHER_AS_SERVICE_COMMANDS_UNINSTALL
:; LAUNCHER_AS_SERVICE_COMMANDS_UNINSTALL
fi
;;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set -e


package_type=deb
COMMON_SCRIPTS
DESKTOP_SCRIPTS
LAUNCHER_AS_SERVICE_SCRIPTS

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ LAUNCHER_AS_SERVICE_COMMANDS_INSTALL

%pre
package_type=rpm
COMMON_SCRIPTS
LAUNCHER_AS_SERVICE_SCRIPTS
if [ "$1" = 2 ]; then
true; LAUNCHER_AS_SERVICE_COMMANDS_UNINSTALL
if [ "$1" -gt 1 ]; then
:; LAUNCHER_AS_SERVICE_COMMANDS_UNINSTALL
fi

%preun
package_type=rpm
COMMON_SCRIPTS
DESKTOP_SCRIPTS
LAUNCHER_AS_SERVICE_SCRIPTS
DESKTOP_COMMANDS_UNINSTALL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -34,6 +34,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -74,6 +75,26 @@ protected Map<String, String> createImpl() throws IOException {
};
}

static void mergeReplacementData(Map<String, String> target,
Map<String, String> newValues) {
Objects.requireNonNull(target);
Objects.requireNonNull(newValues);

for (var kvp : newValues.entrySet()) {
String newValue = kvp.getValue();
String existingValue = target.putIfAbsent(kvp.getKey(), newValue);
if (existingValue != null) {
if (existingValue.isEmpty()) {
target.replace(kvp.getKey(), newValue);
} else if (!newValue.isEmpty() && !newValue.
equals(existingValue)) {
throw new IllegalArgumentException(String.format(
"Key [%s] value mismatch", kvp.getKey()));
}
}
}
}

protected static String stringifyShellCommands(String... commands) {
return stringifyShellCommands(Arrays.asList(commands));
}
Expand Down
Loading

1 comment on commit 7848ed7

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.