8335267: [XWayland] move screencast tokens from .awt to .java folder

Reviewed-by: prr, avu
This commit is contained in:
Alexander Zvegintsev 2024-08-08 10:51:47 +00:00
parent 3bc4a1acc4
commit 088871ce36

View File

@ -60,35 +60,33 @@ import static sun.awt.screencast.ScreencastHelper.SCREENCAST_DEBUG;
* The restore token allows the ScreenCast session to be restored
* with previously granted screen access permissions.
*/
@SuppressWarnings("removal")
final class TokenStorage {
private TokenStorage() {}
private static final String REL_NAME =
".java/robot/screencast-tokens.properties";
private static final String REL_NAME_SECONDARY =
".awt/robot/screencast-tokens.properties";
private static final Properties PROPS = new Properties();
private static final Path PROPS_PATH;
private static final Path PROP_FILENAME;
@SuppressWarnings("removal")
private static void doPrivilegedRunnable(Runnable runnable) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
@Override
public Void run() {
runnable.run();
return null;
}
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
runnable.run();
return null;
});
}
static {
PROPS_PATH = AccessController.doPrivileged(new PrivilegedAction<Path>() {
@Override
public Path run() {
return setupPath();
}
});
@SuppressWarnings("removal")
Path propsPath = AccessController
.doPrivileged((PrivilegedAction<Path>) () -> setupPath());
PROPS_PATH = propsPath;
if (PROPS_PATH != null) {
PROP_FILENAME = PROPS_PATH.getFileName();
@ -110,27 +108,34 @@ final class TokenStorage {
}
Path path = Path.of(userHome, REL_NAME);
Path secondaryPath = Path.of(userHome, REL_NAME_SECONDARY);
boolean copyFromSecondary = !Files.isWritable(path)
&& Files.isWritable(secondaryPath);
Path workdir = path.getParent();
if (!Files.exists(workdir)) {
try {
Files.createDirectories(workdir);
} catch (Exception e) {
if (!Files.isWritable(path)) {
if (!Files.exists(workdir)) {
try {
Files.createDirectories(workdir);
} catch (Exception e) {
if (SCREENCAST_DEBUG) {
System.err.printf("Token storage: cannot create" +
" directory %s %s\n", workdir, e);
}
return null;
}
}
if (!Files.isWritable(workdir)) {
if (SCREENCAST_DEBUG) {
System.err.printf("Token storage: cannot create" +
" directory %s %s\n", workdir, e);
System.err.printf("Token storage: %s is not writable\n", workdir);
}
return null;
}
}
if (!Files.isWritable(workdir)) {
if (SCREENCAST_DEBUG) {
System.err.printf("Token storage: %s is not writable\n", workdir);
}
return null;
}
try {
Files.setPosixFilePermissions(
workdir,
@ -145,7 +150,17 @@ final class TokenStorage {
}
}
if (Files.exists(path)) {
if (copyFromSecondary) {
if (SCREENCAST_DEBUG) {
System.out.println("Token storage: copying from the secondary location "
+ secondaryPath);
}
synchronized (PROPS) {
if (readTokens(secondaryPath)) {
store(path, "copy from the secondary location");
}
}
} else if (Files.exists(path)) {
if (!setFilePermission(path)) {
return null;
}
@ -302,7 +317,7 @@ final class TokenStorage {
}
if (changed) {
doPrivilegedRunnable(() -> store("save tokens"));
doPrivilegedRunnable(() -> store(PROPS_PATH, "save tokens"));
}
}
}
@ -315,7 +330,7 @@ final class TokenStorage {
PROPS.clear();
PROPS.load(reader);
}
} catch (IOException e) {
} catch (IOException | IllegalArgumentException e) {
if (SCREENCAST_DEBUG) {
System.err.printf("""
Token storage: failed to load property file %s
@ -410,7 +425,7 @@ final class TokenStorage {
}
private static void removeMalformedRecords(Set<String> malformedRecords) {
if (!isWritable()
if (!isWritable(PROPS_PATH)
|| malformedRecords == null
|| malformedRecords.isEmpty()) {
return;
@ -424,17 +439,17 @@ final class TokenStorage {
}
}
store("remove malformed records");
store(PROPS_PATH, "remove malformed records");
}
}
private static void store(String failMsg) {
if (!isWritable()) {
private static void store(Path path, String failMsg) {
if (!isWritable(path)) {
return;
}
synchronized (PROPS) {
try (BufferedWriter writer = Files.newBufferedWriter(PROPS_PATH)) {
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
PROPS.store(writer, null);
} catch (IOException e) {
if (SCREENCAST_DEBUG) {
@ -445,13 +460,13 @@ final class TokenStorage {
}
}
private static boolean isWritable() {
if (PROPS_PATH == null
|| (Files.exists(PROPS_PATH) && !Files.isWritable(PROPS_PATH))) {
private static boolean isWritable(Path path) {
if (path == null
|| (Files.exists(path) && !Files.isWritable(path))) {
if (SCREENCAST_DEBUG) {
System.err.printf(
"Token storage: %s is not writable\n", PROPS_PATH);
"Token storage: %s is not writable\n", path);
}
return false;
} else {