Skip to content

Commit 3673564

Browse files
tjgqcopybara-github
authored andcommitted
Improve the FileSystem inheritance hierarchy.
By repurposing AbstractFileSystem as DiskBackedFileSystem, to be extended only by disk-backed subclasses. This encourages other subclasses to make an explicit decision on how to open files instead of inheriting a potentially incorrect implementation. As a bonus, we can also dispense with the duplicate profiling logic in UnixFileSystem. PiperOrigin-RevId: 837473162 Change-Id: I970d0a9711ecc583b1ecae650703d14d4a5db2a4
1 parent 9fbda5b commit 3673564

File tree

10 files changed

+303
-312
lines changed

10 files changed

+303
-312
lines changed

src/main/java/com/google/devtools/build/lib/remote/RemoteActionFileSystem.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import com.google.devtools.build.lib.clock.Clock;
4141
import com.google.devtools.build.lib.remote.common.BulkTransferException;
4242
import com.google.devtools.build.lib.skyframe.TreeArtifactValue;
43-
import com.google.devtools.build.lib.vfs.AbstractFileSystem;
4443
import com.google.devtools.build.lib.vfs.DigestHashFunction;
4544
import com.google.devtools.build.lib.vfs.Dirent;
4645
import com.google.devtools.build.lib.vfs.FileStatus;
@@ -101,8 +100,7 @@
101100
* sources, such as the same path existing in multiple underlying sources with different type or
102101
* contents.
103102
*/
104-
public class RemoteActionFileSystem extends AbstractFileSystem
105-
implements PathCanonicalizer.Resolver {
103+
public class RemoteActionFileSystem extends FileSystem implements PathCanonicalizer.Resolver {
106104
private final PathFragment execRoot;
107105
private final PathFragment outputBase;
108106
private final InputMetadataProvider inputArtifactData;
@@ -901,7 +899,7 @@ private Dirent maybeFollowSymlinkForDirent(
901899
@Override
902900
public void createFSDependentHardLink(PathFragment linkPath, PathFragment originalPath)
903901
throws IOException {
904-
// Only called by the AbstractFileSystem#createHardLink base implementation, overridden below.
902+
// Only called by the FileSystem#createHardLink base implementation, overridden below.
905903
throw new UnsupportedOperationException();
906904
}
907905

src/main/java/com/google/devtools/build/lib/unix/UnixFileSystem.java

Lines changed: 6 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,26 @@
2121
import com.google.devtools.build.lib.util.Blocker;
2222
import com.google.devtools.build.lib.util.OS;
2323
import com.google.devtools.build.lib.util.StringEncoding;
24-
import com.google.devtools.build.lib.vfs.AbstractFileSystem;
2524
import com.google.devtools.build.lib.vfs.DigestHashFunction;
2625
import com.google.devtools.build.lib.vfs.Dirent;
26+
import com.google.devtools.build.lib.vfs.DiskBackedFileSystem;
2727
import com.google.devtools.build.lib.vfs.FileStatus;
2828
import com.google.devtools.build.lib.vfs.FileSymlinkLoopException;
2929
import com.google.devtools.build.lib.vfs.Path;
3030
import com.google.devtools.build.lib.vfs.PathFragment;
3131
import com.google.devtools.build.lib.vfs.SymlinkTargetType;
3232
import java.io.File;
33-
import java.io.FileInputStream;
34-
import java.io.FileNotFoundException;
35-
import java.io.FileOutputStream;
3633
import java.io.IOException;
37-
import java.io.InputStream;
38-
import java.io.OutputStream;
3934
import java.util.ArrayDeque;
4035
import java.util.Collection;
4136
import javax.annotation.Nullable;
4237

43-
/** This class implements the FileSystem interface using direct calls to the UNIX filesystem. */
38+
/**
39+
* A disk-backed filesystem suitable for Unix systems, implemented using a mix of JNI and standard
40+
* library calls.
41+
*/
4442
@ThreadSafe
45-
public class UnixFileSystem extends AbstractFileSystem {
43+
public class UnixFileSystem extends DiskBackedFileSystem {
4644
protected final String hashAttributeName;
4745

4846
public UnixFileSystem(DigestHashFunction hashFunction, String hashAttributeName) {
@@ -441,80 +439,4 @@ public File getIoFile(PathFragment path) {
441439
public java.nio.file.Path getNioPath(PathFragment path) {
442440
return java.nio.file.Path.of(StringEncoding.internalToPlatform(path.getPathString()));
443441
}
444-
445-
@Override
446-
protected InputStream createFileInputStream(PathFragment path) throws IOException {
447-
return new FileInputStream(StringEncoding.internalToPlatform(path.getPathString()));
448-
}
449-
450-
protected OutputStream createFileOutputStream(PathFragment path, boolean append)
451-
throws FileNotFoundException {
452-
return createFileOutputStream(path, append, /* internal= */ false);
453-
}
454-
455-
@Override
456-
protected OutputStream createFileOutputStream(PathFragment path, boolean append, boolean internal)
457-
throws FileNotFoundException {
458-
String name = path.getPathString();
459-
var profiler = Profiler.instance();
460-
if (!internal
461-
&& profiler.isActive()
462-
&& (profiler.isProfiling(ProfilerTask.VFS_WRITE)
463-
|| profiler.isProfiling(ProfilerTask.VFS_OPEN))) {
464-
long startTime = Profiler.instance().nanoTimeMaybe();
465-
var comp = Blocker.begin();
466-
try {
467-
return new ProfiledFileOutputStream(name, /* append= */ append);
468-
} finally {
469-
Blocker.end(comp);
470-
profiler.logSimpleTask(startTime, ProfilerTask.VFS_OPEN, name);
471-
}
472-
} else {
473-
var comp = Blocker.begin();
474-
try {
475-
return new FileOutputStream(StringEncoding.internalToPlatform(name), /* append= */ append);
476-
} finally {
477-
Blocker.end(comp);
478-
}
479-
}
480-
}
481-
482-
private static final class ProfiledFileOutputStream extends FileOutputStream {
483-
private final String name;
484-
485-
private ProfiledFileOutputStream(String name, boolean append) throws FileNotFoundException {
486-
super(StringEncoding.internalToPlatform(name), append);
487-
this.name = name;
488-
}
489-
490-
@Override
491-
public void write(int b) throws IOException {
492-
long startTime = Profiler.instance().nanoTimeMaybe();
493-
try {
494-
super.write(b);
495-
} finally {
496-
Profiler.instance().logSimpleTask(startTime, ProfilerTask.VFS_WRITE, name);
497-
}
498-
}
499-
500-
@Override
501-
public void write(byte[] b) throws IOException {
502-
long startTime = Profiler.instance().nanoTimeMaybe();
503-
try {
504-
super.write(b);
505-
} finally {
506-
Profiler.instance().logSimpleTask(startTime, ProfilerTask.VFS_WRITE, name);
507-
}
508-
}
509-
510-
@Override
511-
public void write(byte[] b, int off, int len) throws IOException {
512-
long startTime = Profiler.instance().nanoTimeMaybe();
513-
try {
514-
super.write(b, off, len);
515-
} finally {
516-
Profiler.instance().logSimpleTask(startTime, ProfilerTask.VFS_WRITE, name);
517-
}
518-
}
519-
}
520442
}

src/main/java/com/google/devtools/build/lib/vfs/AbstractFileSystem.java

Lines changed: 0 additions & 204 deletions
This file was deleted.

0 commit comments

Comments
 (0)