Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lock path computation on Windows and minor simplifications #2426

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

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 @@ -441,8 +444,6 @@ protected String getWorkspaceLockInfo(URL workspaceUrl) {
/**
* Write lock owner details onto workspace lock file. Data includes user, host,
* display and current java process id.
*
* @param instanceLoc
*/
protected void writeWsLockInfo(URL workspaceUrl) {
Properties props = new Properties();
Expand All @@ -468,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 @@ -523,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);
}
Comment on lines +528 to +532
Copy link
Member Author

Choose a reason for hiding this comment

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

Btw. this reminded me of a work that I started a while ago and that I have now made a draft in Equinox:

When that's available this could be further simplified to (but at the moment this is not possible yet!):

Suggested change
try {
return Path.of(URIUtil.toURI(workspaceUrl)).resolve(LOCK_INFO_FILE);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
return URIUtil.toFilePath(workspaceUrl).resolve(LOCK_INFO_FILE);

}

/**
Expand All @@ -534,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
Loading