Skip to content

Commit 951970a

Browse files
committed
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 #1114
1 parent 2ef6ffd commit 951970a

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/InternalFileSystemCore.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@
1515

1616
import java.net.URI;
1717
import java.util.HashMap;
18-
import org.eclipse.core.filesystem.*;
18+
import org.eclipse.core.filesystem.EFS;
19+
import org.eclipse.core.filesystem.IFileStore;
20+
import org.eclipse.core.filesystem.IFileSystem;
1921
import org.eclipse.core.filesystem.provider.FileSystem;
20-
import org.eclipse.core.runtime.*;
22+
import org.eclipse.core.runtime.CoreException;
23+
import org.eclipse.core.runtime.IConfigurationElement;
24+
import org.eclipse.core.runtime.IExtension;
25+
import org.eclipse.core.runtime.IExtensionDelta;
26+
import org.eclipse.core.runtime.IExtensionPoint;
27+
import org.eclipse.core.runtime.IRegistryChangeEvent;
28+
import org.eclipse.core.runtime.IRegistryChangeListener;
29+
import org.eclipse.core.runtime.RegistryFactory;
2130
import org.eclipse.osgi.util.NLS;
2231

2332
/**
@@ -72,9 +81,15 @@ public IFileSystem getFileSystem(String scheme) throws CoreException {
7281
IConfigurationElement element = (IConfigurationElement) result;
7382
FileSystem fs = (FileSystem) element.createExecutableExtension("run"); //$NON-NLS-1$
7483
fs.initialize(scheme);
75-
//store the file system instance so we don't have to keep recreating it
76-
registry.put(scheme, fs);
77-
return fs;
84+
synchronized (this) {
85+
result = registry.get(scheme);
86+
if (result instanceof IFileSystem) {
87+
return (IFileSystem) result;
88+
}
89+
//store the file system instance so we don't have to keep recreating it
90+
registry.put(scheme, fs);
91+
return fs;
92+
}
7893
} catch (CoreException e) {
7994
//remove this invalid file system from the registry
8095
registry.remove(scheme);

resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/NullFileSystem.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,22 @@ public class NullFileSystem extends FileSystem {
2929
/**
3030
* The singleton instance of this file system.
3131
*/
32-
private static IFileSystem instance;
32+
private static final IFileSystem INSTANCE = EFS.getNullFileSystem();
3333

3434
/**
3535
* Returns the instance of this file system
3636
*
3737
* @return The instance of this file system.
3838
*/
3939
public static IFileSystem getInstance() {
40-
return instance;
40+
return INSTANCE;
4141
}
4242

4343
/**
4444
* Creates the null file system.
4545
*/
4646
public NullFileSystem() {
4747
super();
48-
instance = this;
4948
}
5049

5150
@Override

resources/bundles/org.eclipse.core.filesystem/src/org/eclipse/core/internal/filesystem/local/LocalFileSystem.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
import java.io.File;
2121
import java.net.URI;
22-
import org.eclipse.core.filesystem.*;
22+
import org.eclipse.core.filesystem.EFS;
23+
import org.eclipse.core.filesystem.IFileStore;
24+
import org.eclipse.core.filesystem.IFileSystem;
2325
import org.eclipse.core.filesystem.provider.FileSystem;
2426
import org.eclipse.core.runtime.IPath;
2527
import org.eclipse.osgi.service.environment.Constants;
@@ -47,15 +49,15 @@ public class LocalFileSystem extends FileSystem {
4749
/**
4850
* The singleton instance of this file system.
4951
*/
50-
private static IFileSystem instance;
52+
private static final IFileSystem INSTANCE = EFS.getLocalFileSystem();
5153

5254
/**
5355
* Returns the instance of this file system
5456
*
5557
* @return The instance of this file system.
5658
*/
5759
public static IFileSystem getInstance() {
58-
return instance;
60+
return INSTANCE;
5961
}
6062

6163
/**
@@ -71,7 +73,6 @@ static String getOS() {
7173
*/
7274
public LocalFileSystem() {
7375
super();
74-
instance = this;
7576
}
7677

7778
@Override

0 commit comments

Comments
 (0)