Skip to content

Commit 7848ed7

Browse files
author
Alexey Semenyuk
committed
8301856: Generated .spec file for RPM installers uninstalls desktop launcher on update
Reviewed-by: almatvee
1 parent 726f854 commit 7848ed7

16 files changed

+377
-74
lines changed

src/jdk.jpackage/linux/classes/jdk/jpackage/internal/DesktopIntegration.java

+21-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -63,9 +63,10 @@ final class DesktopIntegration extends ShellCustomAction {
6363
private static final String COMMANDS_INSTALL = "DESKTOP_COMMANDS_INSTALL";
6464
private static final String COMMANDS_UNINSTALL = "DESKTOP_COMMANDS_UNINSTALL";
6565
private static final String SCRIPTS = "DESKTOP_SCRIPTS";
66+
private static final String COMMON_SCRIPTS = "COMMON_SCRIPTS";
6667

6768
private static final List<String> REPLACEMENT_STRING_IDS = List.of(
68-
COMMANDS_INSTALL, COMMANDS_UNINSTALL, SCRIPTS);
69+
COMMANDS_INSTALL, COMMANDS_UNINSTALL, SCRIPTS, COMMON_SCRIPTS);
6970

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

232-
boolean needCleanupScripts = !associations.isEmpty();
233-
234233
// Take care of additional launchers if there are any.
235234
// Process every additional launcher as the main application launcher.
236235
// Collect shell commands to install/uninstall integration with desktop
@@ -241,10 +240,6 @@ protected Map<String, String> createImpl() throws IOException {
241240
List<String> uninstallShellCmds = new ArrayList<>(Arrays.asList(
242241
data.get(COMMANDS_UNINSTALL)));
243242
for (var integration: nestedIntegrations) {
244-
if (!integration.associations.isEmpty()) {
245-
needCleanupScripts = true;
246-
}
247-
248243
Map<String, String> launcherData = integration.create();
249244

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

257-
if (needCleanupScripts) {
258-
// Pull in desktop_utils.sh scrips library.
259-
data.put(SCRIPTS, stringifyTextFile("desktop_utils.sh"));
260-
}
252+
data.put(COMMON_SCRIPTS, stringifyTextFile("common_utils.sh"));
253+
data.put(SCRIPTS, stringifyTextFile("desktop_utils.sh"));
261254

262255
return data;
263256
}
@@ -295,16 +288,20 @@ private class ShellCommands {
295288

296289
registerDesktopFileCmd = String.join(" ", "xdg-desktop-menu",
297290
"install", desktopFile.installPath().toString());
298-
unregisterDesktopFileCmd = String.join(" ", "xdg-desktop-menu",
291+
unregisterDesktopFileCmd = String.join(" ",
292+
"do_if_file_belongs_to_single_package", desktopFile.
293+
installPath().toString(), "xdg-desktop-menu",
299294
"uninstall", desktopFile.installPath().toString());
300295
}
301296

302297
void setFileAssociations() {
303298
registerFileAssociationsCmd = String.join(" ", "xdg-mime",
304299
"install",
305300
mimeInfoFile.installPath().toString());
306-
unregisterFileAssociationsCmd = String.join(" ", "xdg-mime",
307-
"uninstall", mimeInfoFile.installPath().toString());
301+
unregisterFileAssociationsCmd = String.join(" ",
302+
"do_if_file_belongs_to_single_package", mimeInfoFile.
303+
installPath().toString(), "xdg-mime", "uninstall",
304+
mimeInfoFile.installPath().toString());
308305

309306
//
310307
// Add manual cleanup of system files to get rid of
@@ -320,26 +317,26 @@ void setFileAssociations() {
320317
// of non-existing desktop file.
321318
//
322319
String cleanUpCommand = String.join(" ",
323-
"uninstall_default_mime_handler",
324-
desktopFile.installPath().getFileName().toString(),
325-
String.join(" ", getMimeTypeNamesFromFileAssociations()));
320+
"do_if_file_belongs_to_single_package", desktopFile.
321+
installPath().toString(),
322+
"desktop_uninstall_default_mime_handler", desktopFile.
323+
installPath().getFileName().toString(), String.join(
324+
" ", getMimeTypeNamesFromFileAssociations()));
326325

327326
unregisterFileAssociationsCmd = stringifyShellCommands(
328327
unregisterFileAssociationsCmd, cleanUpCommand);
329328
}
330329

331-
void addIcon(String mimeType, Path iconFile) {
332-
addIcon(mimeType, iconFile, getSquareSizeOfImage(iconFile.toFile()));
333-
}
334-
335330
void addIcon(String mimeType, Path iconFile, int imgSize) {
336331
imgSize = normalizeIconSize(imgSize);
337332
final String dashMime = mimeType.replace('/', '-');
338333
registerIconCmds.add(String.join(" ", "xdg-icon-resource",
339334
"install", "--context", "mimetypes", "--size",
340335
Integer.toString(imgSize), iconFile.toString(), dashMime));
341-
unregisterIconCmds.add(String.join(" ", "xdg-icon-resource",
342-
"uninstall", dashMime, "--size", Integer.toString(imgSize)));
336+
unregisterIconCmds.add(String.join(" ",
337+
"do_if_file_belongs_to_single_package", iconFile.toString(),
338+
"xdg-icon-resource", "uninstall", dashMime, "--size",
339+
Integer.toString(imgSize)));
343340
}
344341

345342
void applyTo(Map<String, String> data) {

src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LinuxLaunchersAsServices.java

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,8 @@
2626

2727
import java.io.IOException;
2828
import java.nio.file.Path;
29+
import java.util.ArrayList;
30+
import java.util.Collections;
2931
import java.util.List;
3032
import java.util.Map;
3133
import static jdk.jpackage.internal.OverridableResource.createResource;
@@ -42,10 +44,24 @@ private LinuxLaunchersAsServices(PlatformPackage thePackage,
4244
});
4345
}
4446

47+
@Override
48+
protected List<String> replacementStringIds() {
49+
return LINUX_REPLACEMENT_STRING_IDS;
50+
}
51+
52+
@Override
53+
protected Map<String, String> createImpl() throws IOException {
54+
var data = super.createImpl();
55+
if (!data.isEmpty()) {
56+
data.put(COMMON_SCRIPTS, stringifyTextFile("common_utils.sh"));
57+
}
58+
return data;
59+
}
60+
4561
static ShellCustomAction create(PlatformPackage thePackage,
4662
Map<String, Object> params) throws IOException {
4763
if (StandardBundlerParam.isRuntimeInstaller(params)) {
48-
return ShellCustomAction.nop(REPLACEMENT_STRING_IDS);
64+
return ShellCustomAction.nop(LINUX_REPLACEMENT_STRING_IDS);
4965
}
5066
return new LinuxLaunchersAsServices(thePackage, params);
5167
}
@@ -84,4 +100,16 @@ Path descriptorFilePath(Path root) {
84100

85101
private final static List<String> REQUIRED_PACKAGES = List.of("systemd",
86102
"coreutils" /* /usr/bin/wc */, "grep");
103+
104+
private static final String COMMON_SCRIPTS = "COMMON_SCRIPTS";
105+
106+
private static final List<String> LINUX_REPLACEMENT_STRING_IDS;
107+
108+
static {
109+
ArrayList<String> buf = new ArrayList<>();
110+
buf.addAll(UnixLaunchersAsServices.REPLACEMENT_STRING_IDS);
111+
buf.add(COMMON_SCRIPTS);
112+
113+
LINUX_REPLACEMENT_STRING_IDS = Collections.unmodifiableList(buf);
114+
}
87115
}

src/jdk.jpackage/linux/classes/jdk/jpackage/internal/LinuxPackageBundler.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -155,7 +155,8 @@ public final Path execute(Map<String, ? super Object> params,
155155
Map<String, String> data = createDefaultReplacementData(params);
156156

157157
for (var ca : customActions) {
158-
data.putAll(ca.instance.create());
158+
ShellCustomAction.mergeReplacementData(data, ca.instance.
159+
create());
159160
}
160161

161162
data.putAll(createReplacementData(params));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
file_belongs_to_single_package ()
2+
{
3+
if [ ! -e "$1" ]; then
4+
false
5+
elif [ "$package_type" = rpm ]; then
6+
test `rpm -q --whatprovides "$1" | wc -l` = 1
7+
elif [ "$package_type" = deb ]; then
8+
test `dpkg -S "$1" | wc -l` = 1
9+
else
10+
exit 1
11+
fi
12+
}
13+
14+
15+
do_if_file_belongs_to_single_package ()
16+
{
17+
local file="$1"
18+
shift
19+
20+
if file_belongs_to_single_package "$file"; then
21+
"$@"
22+
fi
23+
}

src/jdk.jpackage/linux/classes/jdk/jpackage/internal/resources/desktop_utils.sh

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Remove $1 desktop file from the list of default handlers for $2 mime type
33
# in $3 file dumping output to stdout.
44
#
5-
_filter_out_default_mime_handler ()
5+
desktop_filter_out_default_mime_handler ()
66
{
77
local defaults_list="$3"
88

@@ -50,7 +50,7 @@ EOF
5050
# in $1 file.
5151
# Result is saved in $1 file.
5252
#
53-
_uninstall_default_mime_handler ()
53+
desktop_uninstall_default_mime_handler_0 ()
5454
{
5555
local defaults_list=$1
5656
shift
@@ -66,20 +66,20 @@ _uninstall_default_mime_handler ()
6666
local v
6767
local update=
6868
for mime in "$@"; do
69-
_filter_out_default_mime_handler "$desktop_file" "$mime" "$tmpfile1" > "$tmpfile2"
69+
desktop_filter_out_default_mime_handler "$desktop_file" "$mime" "$tmpfile1" > "$tmpfile2"
7070
v="$tmpfile2"
7171
tmpfile2="$tmpfile1"
7272
tmpfile1="$v"
7373

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

8080
if [ -n "$update" ]; then
8181
cat "$tmpfile1" > "$defaults_list"
82-
trace "$defaults_list" file updated
82+
desktop_trace "$defaults_list" file updated
8383
fi
8484

8585
rm -f "$tmpfile1" "$tmpfile2"
@@ -90,15 +90,15 @@ _uninstall_default_mime_handler ()
9090
# Remove $1 desktop file from the list of default handlers for $@ mime types
9191
# in all known system defaults lists.
9292
#
93-
uninstall_default_mime_handler ()
93+
desktop_uninstall_default_mime_handler ()
9494
{
9595
for f in /usr/share/applications/defaults.list /usr/local/share/applications/defaults.list; do
96-
_uninstall_default_mime_handler "$f" "$@"
96+
desktop_uninstall_default_mime_handler_0 "$f" "$@"
9797
done
9898
}
9999

100100

101-
trace ()
101+
desktop_trace ()
102102
{
103103
echo "$@"
104104
}

src/jdk.jpackage/linux/classes/jdk/jpackage/internal/resources/services_utils.sh

-13
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,3 @@ unregister_services ()
2323
fi
2424
done
2525
}
26-
27-
file_belongs_to_single_package ()
28-
{
29-
if [ ! -e "$1" ]; then
30-
false
31-
elif [ "$package_type" = rpm ]; then
32-
test `rpm -q --whatprovides "$1" | wc -l` = 1
33-
elif [ "$package_type" = deb ]; then
34-
test `dpkg -S "$1" | wc -l` = 1
35-
else
36-
exit 1
37-
fi
38-
}

src/jdk.jpackage/linux/classes/jdk/jpackage/internal/resources/template.preinst

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ set -e
1414
# the debian-policy package
1515

1616
package_type=deb
17+
COMMON_SCRIPTS
1718
LAUNCHER_AS_SERVICE_SCRIPTS
1819

1920
case "$1" in
2021
install|upgrade)
2122
if [ -n "$2" ]; then
22-
true; LAUNCHER_AS_SERVICE_COMMANDS_UNINSTALL
23+
:; LAUNCHER_AS_SERVICE_COMMANDS_UNINSTALL
2324
fi
2425
;;
2526

src/jdk.jpackage/linux/classes/jdk/jpackage/internal/resources/template.prerm

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set -e
1818

1919

2020
package_type=deb
21+
COMMON_SCRIPTS
2122
DESKTOP_SCRIPTS
2223
LAUNCHER_AS_SERVICE_SCRIPTS
2324

src/jdk.jpackage/linux/classes/jdk/jpackage/internal/resources/template.spec

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ LAUNCHER_AS_SERVICE_COMMANDS_INSTALL
8383

8484
%pre
8585
package_type=rpm
86+
COMMON_SCRIPTS
8687
LAUNCHER_AS_SERVICE_SCRIPTS
87-
if [ "$1" = 2 ]; then
88-
true; LAUNCHER_AS_SERVICE_COMMANDS_UNINSTALL
88+
if [ "$1" -gt 1 ]; then
89+
:; LAUNCHER_AS_SERVICE_COMMANDS_UNINSTALL
8990
fi
9091

9192
%preun
9293
package_type=rpm
94+
COMMON_SCRIPTS
9395
DESKTOP_SCRIPTS
9496
LAUNCHER_AS_SERVICE_SCRIPTS
9597
DESKTOP_COMMANDS_UNINSTALL

src/jdk.jpackage/unix/classes/jdk/jpackage/internal/ShellCustomAction.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,6 +34,7 @@
3434
import java.util.HashMap;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.Objects;
3738
import java.util.stream.Collectors;
3839

3940
/**
@@ -74,6 +75,26 @@ protected Map<String, String> createImpl() throws IOException {
7475
};
7576
}
7677

78+
static void mergeReplacementData(Map<String, String> target,
79+
Map<String, String> newValues) {
80+
Objects.requireNonNull(target);
81+
Objects.requireNonNull(newValues);
82+
83+
for (var kvp : newValues.entrySet()) {
84+
String newValue = kvp.getValue();
85+
String existingValue = target.putIfAbsent(kvp.getKey(), newValue);
86+
if (existingValue != null) {
87+
if (existingValue.isEmpty()) {
88+
target.replace(kvp.getKey(), newValue);
89+
} else if (!newValue.isEmpty() && !newValue.
90+
equals(existingValue)) {
91+
throw new IllegalArgumentException(String.format(
92+
"Key [%s] value mismatch", kvp.getKey()));
93+
}
94+
}
95+
}
96+
}
97+
7798
protected static String stringifyShellCommands(String... commands) {
7899
return stringifyShellCommands(Arrays.asList(commands));
79100
}

0 commit comments

Comments
 (0)