aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs
diff options
context:
space:
mode:
authorGravatar tomlu <tomlu@google.com>2017-10-20 20:59:44 +0200
committerGravatar Dmitry Lomov <dslomov@google.com>2017-10-23 17:16:03 +0200
commit51de88c017910e77f929bc38d5b1b1c315170443 (patch)
treeab2a18da2845d21686570edb6be05b33da00547e /src/main/java/com/google/devtools/build/lib/vfs
parent10a6b77e342472e29d168060e06f05a9c7cad66f (diff)
Lazy-initialise rootPath in FileSystem.
Path construction entails calling isFileSystemCaseSensitive at a time when subclasses have not been initialised. This can cause a crash. Solve by deferring init of the path. PiperOrigin-RevId: 172914501
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs')
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java20
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystem.java24
2 files changed, 28 insertions, 16 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java b/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java
index ee4bff1785..75851faa91 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java
@@ -119,11 +119,8 @@ public abstract class FileSystem {
}
}
- protected final Path rootPath;
-
- protected FileSystem() {
- this.rootPath = getPathFactory().createRootPath(this);
- }
+ /** Lazy-initialized on first access, always use {@link FileSystem#getRootDirectory} */
+ private Path rootPath;
/** Returns filesystem-specific path factory. */
protected PathFactory getPathFactory() {
@@ -152,13 +149,20 @@ public abstract class FileSystem {
if (!pathName.isAbsolute()) {
throw new IllegalArgumentException(pathName.getPathString() + " (not an absolute path)");
}
- return rootPath.getRelative(pathName);
+ return getRootDirectory().getRelative(pathName);
}
/**
* Returns a path representing the root directory of the current file system.
*/
public final Path getRootDirectory() {
+ if (rootPath == null) {
+ synchronized (this) {
+ if (rootPath == null) {
+ rootPath = getPathFactory().createRootPath(this);
+ }
+ }
+ }
return rootPath;
}
@@ -395,7 +399,9 @@ public abstract class FileSystem {
if (maxLinks-- == 0) {
throw new IOException(naive + " (Too many levels of symbolic links)");
}
- if (linkTarget.isAbsolute()) { dir = rootPath; }
+ if (linkTarget.isAbsolute()) {
+ dir = getRootDirectory();
+ }
for (String name : linkTarget.segments()) {
if (name.equals(".") || name.isEmpty()) {
// no-op
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystem.java b/src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystem.java
index d2db9967a1..7a91db7eed 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystem.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs/InMemoryFileSystem.java
@@ -332,7 +332,7 @@ public class InMemoryFileSystem extends FileSystem {
// and it may only appear as part of a contiguous prefix sequence.
Stack<String> stack = new Stack<>();
- PathFragment rootPathFragment = rootPath.asFragment();
+ PathFragment rootPathFragment = getRootDirectory().asFragment();
for (Path p = path; !p.asFragment().equals(rootPathFragment); p = p.getParentDirectory()) {
stack.push(p.getBaseName());
}
@@ -408,7 +408,7 @@ public class InMemoryFileSystem extends FileSystem {
if (followSymlinks) {
return scopeLimitedStat(path, true);
} else {
- if (path.equals(rootPath)) {
+ if (path.equals(getRootDirectory())) {
return rootInode;
} else {
return getNoFollowStatOrOutOfScopeParent(path);
@@ -441,7 +441,7 @@ public class InMemoryFileSystem extends FileSystem {
if (followSymlinks) {
return pathWalk(path, false);
} else {
- if (path.equals(rootPath)) {
+ if (path.equals(getRootDirectory())) {
return rootInode;
} else {
return getNoFollowStatOrOutOfScopeParent(path);
@@ -582,7 +582,9 @@ public class InMemoryFileSystem extends FileSystem {
@Override
protected boolean createDirectory(Path path) throws IOException {
- if (path.equals(rootPath)) { throw Error.EACCES.exception(path); }
+ if (path.equals(getRootDirectory())) {
+ throw Error.EACCES.exception(path);
+ }
InMemoryDirectoryInfo parent;
synchronized (this) {
@@ -608,7 +610,9 @@ public class InMemoryFileSystem extends FileSystem {
@Override
protected void createSymbolicLink(Path path, PathFragment targetFragment)
throws IOException {
- if (path.equals(rootPath)) { throw Error.EACCES.exception(path); }
+ if (path.equals(getRootDirectory())) {
+ throw Error.EACCES.exception(path);
+ }
synchronized (this) {
InMemoryDirectoryInfo parent = getDirectory(path.getParentDirectory());
@@ -659,7 +663,9 @@ public class InMemoryFileSystem extends FileSystem {
@Override
protected boolean delete(Path path) throws IOException {
- if (path.equals(rootPath)) { throw Error.EBUSY.exception(path); }
+ if (path.equals(getRootDirectory())) {
+ throw Error.EBUSY.exception(path);
+ }
if (!exists(path, false)) { return false; }
synchronized (this) {
@@ -731,10 +737,10 @@ public class InMemoryFileSystem extends FileSystem {
@Override
protected void renameTo(Path sourcePath, Path targetPath)
throws IOException {
- if (sourcePath.equals(rootPath)) {
+ if (sourcePath.equals(getRootDirectory())) {
throw Error.EACCES.exception(sourcePath);
}
- if (targetPath.equals(rootPath)) {
+ if (targetPath.equals(getRootDirectory())) {
throw Error.EACCES.exception(targetPath);
}
synchronized (this) {
@@ -782,7 +788,7 @@ public class InMemoryFileSystem extends FileSystem {
throws IOException {
// Same check used when creating a symbolic link
- if (originalPath.equals(rootPath)) {
+ if (originalPath.equals(getRootDirectory())) {
throw Error.EACCES.exception(originalPath);
}