From 951970ac42106224970fc7f58bab4bd35a97df45 Mon Sep 17 00:00:00 2001 From: Andrey Loskutov Date: Wed, 24 Jan 2024 08:34:01 +0100 Subject: [PATCH] Fix NullFileSystem & LocalFileSystem getInstance() implementation getInstance() should always return same value, but that was not always the case. Public constructors of both file systems allowed clients to override static instance field, causing potential and hard to find errors at at runtime. Fixes https://github.com/eclipse-platform/eclipse.platform/issues/1114 --- .../filesystem/InternalFileSystemCore.java | 25 +++++++++++++++---- .../internal/filesystem/NullFileSystem.java | 5 ++-- .../filesystem/local/LocalFileSystem.java | 9 ++++--- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/InternalFileSystemCore.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/InternalFileSystemCore.java index 9e63137265e..4df66c062a7 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/InternalFileSystemCore.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/InternalFileSystemCore.java @@ -15,9 +15,18 @@ import java.net.URI; import java.util.HashMap; -import org.eclipse.core.filesystem.*; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.filesystem.IFileSystem; import org.eclipse.core.filesystem.provider.FileSystem; -import org.eclipse.core.runtime.*; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtension; +import org.eclipse.core.runtime.IExtensionDelta; +import org.eclipse.core.runtime.IExtensionPoint; +import org.eclipse.core.runtime.IRegistryChangeEvent; +import org.eclipse.core.runtime.IRegistryChangeListener; +import org.eclipse.core.runtime.RegistryFactory; import org.eclipse.osgi.util.NLS; /** @@ -72,9 +81,15 @@ public IFileSystem getFileSystem(String scheme) throws CoreException { IConfigurationElement element = (IConfigurationElement) result; FileSystem fs = (FileSystem) element.createExecutableExtension("run"); //$NON-NLS-1$ fs.initialize(scheme); - //store the file system instance so we don't have to keep recreating it - registry.put(scheme, fs); - return fs; + synchronized (this) { + result = registry.get(scheme); + if (result instanceof IFileSystem) { + return (IFileSystem) result; + } + //store the file system instance so we don't have to keep recreating it + registry.put(scheme, fs); + return fs; + } } catch (CoreException e) { //remove this invalid file system from the registry registry.remove(scheme); diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/NullFileSystem.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/NullFileSystem.java index 1d20161fb35..88e7d5d7295 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/NullFileSystem.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/NullFileSystem.java @@ -29,7 +29,7 @@ public class NullFileSystem extends FileSystem { /** * The singleton instance of this file system. */ - private static IFileSystem instance; + private static final IFileSystem INSTANCE = EFS.getNullFileSystem(); /** * Returns the instance of this file system @@ -37,7 +37,7 @@ public class NullFileSystem extends FileSystem { * @return The instance of this file system. */ public static IFileSystem getInstance() { - return instance; + return INSTANCE; } /** @@ -45,7 +45,6 @@ public static IFileSystem getInstance() { */ public NullFileSystem() { super(); - instance = this; } @Override diff --git a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/local/LocalFileSystem.java b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/local/LocalFileSystem.java index 8c519aa0a0b..c12862ba452 100644 --- a/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/local/LocalFileSystem.java +++ b/resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/local/LocalFileSystem.java @@ -19,7 +19,9 @@ import java.io.File; import java.net.URI; -import org.eclipse.core.filesystem.*; +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.filesystem.IFileSystem; import org.eclipse.core.filesystem.provider.FileSystem; import org.eclipse.core.runtime.IPath; import org.eclipse.osgi.service.environment.Constants; @@ -47,7 +49,7 @@ public class LocalFileSystem extends FileSystem { /** * The singleton instance of this file system. */ - private static IFileSystem instance; + private static final IFileSystem INSTANCE = EFS.getLocalFileSystem(); /** * Returns the instance of this file system @@ -55,7 +57,7 @@ public class LocalFileSystem extends FileSystem { * @return The instance of this file system. */ public static IFileSystem getInstance() { - return instance; + return INSTANCE; } /** @@ -71,7 +73,6 @@ static String getOS() { */ public LocalFileSystem() { super(); - instance = this; } @Override