Skip to content

Commit

Permalink
Backport 088871ce36f85fb30b24c49146f547bc8e2b0dcb
Browse files Browse the repository at this point in the history
  • Loading branch information
duke committed Sep 18, 2024
1 parent 8b54622 commit ee2b35d
Showing 1 changed file with 53 additions and 38 deletions.
91 changes: 53 additions & 38 deletions src/java.desktop/unix/classes/sun/awt/screencast/TokenStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,33 @@
* 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();
Expand All @@ -110,25 +108,32 @@ private static Path setupPath() {
}

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 (SCREENCAST_DEBUG) {
System.err.printf("Token storage: cannot create" +
" directory %s %s\n", workdir, 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;
}
return null;
}
}

if (!Files.isWritable(workdir)) {
if (SCREENCAST_DEBUG) {
System.err.printf("Token storage: %s is not writable\n", workdir);
if (!Files.isWritable(workdir)) {
if (SCREENCAST_DEBUG) {
System.err.printf("Token storage: %s is not writable\n", workdir);
}
return null;
}
return null;
}

try {
Expand All @@ -145,7 +150,17 @@ private static Path setupPath() {
}
}

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;
}
Expand Down Expand Up @@ -302,7 +317,7 @@ private static void storeTokenFromNative(String oldToken,
}

if (changed) {
doPrivilegedRunnable(() -> store("save tokens"));
doPrivilegedRunnable(() -> store(PROPS_PATH, "save tokens"));
}
}
}
Expand All @@ -315,7 +330,7 @@ private static boolean readTokens(Path path) {
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
Expand Down Expand Up @@ -410,7 +425,7 @@ static Set<TokenItem> getTokens(List<Rectangle> affectedScreenBounds) {
}

private static void removeMalformedRecords(Set<String> malformedRecords) {
if (!isWritable()
if (!isWritable(PROPS_PATH)
|| malformedRecords == null
|| malformedRecords.isEmpty()) {
return;
Expand All @@ -424,17 +439,17 @@ private static void removeMalformedRecords(Set<String> malformedRecords) {
}
}

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) {
Expand All @@ -445,13 +460,13 @@ private static void store(String failMsg) {
}
}

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 {
Expand Down

0 comments on commit ee2b35d

Please sign in to comment.