Skip to content

Commit

Permalink
Fix lock path computation on Windows and minor simplifications
Browse files Browse the repository at this point in the history
Fixes #2423
  • Loading branch information
HannesWell authored and iloveeclipse committed Oct 21, 2024
1 parent c9c9ec7 commit 9f38380
Showing 1 changed file with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -40,6 +42,7 @@
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.URIUtil;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.equinox.app.IApplication;
Expand Down Expand Up @@ -86,7 +89,7 @@ public class IDEApplication implements IApplication, IExecutableExtension {

private static final String VERSION_FILENAME = "version.ini"; //$NON-NLS-1$

private static final String LOCK_INFO_FILENAME = ".lock_info"; //$NON-NLS-1$
private static final Path LOCK_INFO_FILE = Path.of(METADATA_FOLDER, ".lock_info"); //$NON-NLS-1$

private static final String DISPLAY_VAR = "DISPLAY"; //$NON-NLS-1$

Expand Down Expand Up @@ -404,14 +407,14 @@ protected Control createCustomArea(Composite parent) {
*/
protected String getWorkspaceLockInfo(URL workspaceUrl) {
try {
File lockFile = getLockInfoFile(workspaceUrl);
if (!lockFile.exists()) {
Path lockFile = getLockInfoFile(workspaceUrl);
if (!Files.exists(lockFile)) {
return null;
}

StringBuilder sb = new StringBuilder();
Properties props = new Properties();
try (FileInputStream is = new FileInputStream(lockFile)) {
try (InputStream is = Files.newInputStream(lockFile)) {
props.load(is);
String prop = props.getProperty(USER);
if (prop != null) {
Expand Down Expand Up @@ -466,7 +469,7 @@ protected void writeWsLockInfo(URL workspaceUrl) {
return;
}

try (OutputStream output = new FileOutputStream(createLockInfoFile(workspaceUrl))) {
try (OutputStream output = Files.newOutputStream(createLockInfoFile(workspaceUrl))) {
props.store(output, null);
} catch (Exception e) {
IDEWorkbenchPlugin.log("Could not write lock info file", e); //$NON-NLS-1$
Expand Down Expand Up @@ -521,9 +524,12 @@ private String getHostName() {
* @param workspaceUrl
* @return .lock_info file.
*/
private File getLockInfoFile(URL workspaceUrl) {
Path lockInfoPath = Path.of(workspaceUrl.getPath(), METADATA_FOLDER, LOCK_INFO_FILENAME);
return lockInfoPath.toFile();
private static Path getLockInfoFile(URL workspaceUrl) {
try {
return Path.of(URIUtil.toURI(workspaceUrl)).resolve(LOCK_INFO_FILE);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}

/**
Expand All @@ -532,17 +538,12 @@ private File getLockInfoFile(URL workspaceUrl) {
* @param workspaceUrl
* @return .lock_info file.
*/
private File createLockInfoFile(URL workspaceUrl) throws Exception {
File lockInfoFile = getLockInfoFile(workspaceUrl);

if (lockInfoFile.exists())
return lockInfoFile;

Path createdPath = Files.createFile(lockInfoFile.toPath());
if (createdPath != null) {
return createdPath.toFile();
private static Path createLockInfoFile(URL workspaceUrl) throws Exception {
Path lockInfoFile = getLockInfoFile(workspaceUrl);
if (!Files.exists(lockInfoFile)) {
Files.createFile(lockInfoFile);
}
return null;
return lockInfoFile;
}

@SuppressWarnings("rawtypes")
Expand Down

0 comments on commit 9f38380

Please sign in to comment.