aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com
diff options
context:
space:
mode:
authorGravatar shahan <shahan@google.com>2018-05-30 10:46:40 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-30 10:48:22 -0700
commit252e3b817e444ddfe264e8cbc5f1203023e61926 (patch)
tree2169737c081cc3cc5e258da1be891f8268dc975e /src/main/java/com
parent088d8de6584864adaec82712c8ace601404afd0b (diff)
Adds support for InlineFileArtifactValue.
PiperOrigin-RevId: 198584000
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/ActionFileSystem.java36
-rw-r--r--src/main/java/com/google/devtools/build/lib/skyframe/FileArtifactValue.java57
2 files changed, 86 insertions, 7 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/ActionFileSystem.java b/src/main/java/com/google/devtools/build/lib/skyframe/ActionFileSystem.java
index 5b35c40f0e..d6d695cd71 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/ActionFileSystem.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/ActionFileSystem.java
@@ -19,6 +19,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Streams;
+import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.ActionInputFileCache;
@@ -32,6 +33,7 @@ import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.vfs.Root;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.protobuf.ByteString;
+import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -338,18 +340,23 @@ final class ActionFileSystem extends FileSystem implements ActionInputFileCache
@Override
protected InputStream getInputStream(Path path) throws IOException {
- // TODO(shahan): cleanup callers of this method and disable or maybe figure out a reasonable
- // implementation.
- LOGGER.severe("Raw read of path: " + path);
+ FileArtifactValue metadata = getMetadataChecked(asExecPath(path));
+ if (metadata instanceof FileArtifactValue.InlineFileArtifactValue) {
+ return ((FileArtifactValue.InlineFileArtifactValue) metadata).getInputStream();
+ }
+ Preconditions.checkArgument(
+ !(metadata instanceof FileArtifactValue.RemoteFileArtifactValue),
+ "getInputStream called for remote file: %s",
+ path);
return delegate.getPath(path.asFragment()).getInputStream();
}
@Override
protected OutputStream getOutputStream(Path path, boolean append) throws IOException {
- // TODO(shahan): cleanup callers of this method and disable or maybe figure out a reasonable
- // implementation.
- LOGGER.severe("Raw write of path: " + path);
- return delegate.getPath(path.asFragment()).getOutputStream(append);
+ Preconditions.checkArgument(!append, "ActionFileSystem doesn't support append.");
+ return Preconditions.checkNotNull(
+ outputs.get(asExecPath(path)), "getOutputStream called for non-output: %s", path)
+ .getOutputStream();
}
@Override
@@ -520,5 +527,20 @@ final class ActionFileSystem extends FileSystem implements ActionInputFileCache
metadataConsumer.accept(artifact, metadata);
this.metadata = metadata;
}
+
+ /** Callers are expected to close the returned stream. */
+ public ByteArrayOutputStream getOutputStream() {
+ Preconditions.checkState(metadata == null, "getOutputStream called twice for: %s", artifact);
+ return new ByteArrayOutputStream() {
+ @Override
+ public void close() throws IOException {
+ super.close();
+ byte[] data = toByteArray();
+ set(
+ new FileArtifactValue.InlineFileArtifactValue(
+ data, Hashing.md5().hashBytes(data).asBytes()));
+ }
+ };
+ }
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/FileArtifactValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/FileArtifactValue.java
index 8786a75909..b5add7ca4e 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/FileArtifactValue.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/FileArtifactValue.java
@@ -28,6 +28,7 @@ import com.google.devtools.build.lib.vfs.FileStatus;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.Symlinks;
import com.google.devtools.build.skyframe.SkyValue;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
@@ -298,6 +299,62 @@ public abstract class FileArtifactValue implements SkyValue, Metadata {
}
}
+ static final class InlineFileArtifactValue extends FileArtifactValue {
+ private final byte[] data;
+ private final byte[] digest;
+
+ InlineFileArtifactValue(byte[] data, byte[] digest) {
+ this.data = Preconditions.checkNotNull(data);
+ this.digest = Preconditions.checkNotNull(digest);
+ }
+
+ public ByteArrayInputStream getInputStream() {
+ return new ByteArrayInputStream(data);
+ }
+
+ @Override
+ public FileStateType getType() {
+ return FileStateType.REGULAR_FILE;
+ }
+
+ @Override
+ public byte[] getDigest() {
+ return digest;
+ }
+
+ @Override
+ public long getSize() {
+ return data.length;
+ }
+
+ @Override
+ public long getModifiedTime() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o == this) {
+ return true;
+ }
+ if (!(o instanceof InlineFileArtifactValue)) {
+ return false;
+ }
+ InlineFileArtifactValue that = (InlineFileArtifactValue) o;
+ return Arrays.equals(digest, that.digest);
+ }
+
+ @Override
+ public int hashCode() {
+ return Arrays.hashCode(digest);
+ }
+
+ @Override
+ public boolean wasModifiedSinceDigest(Path path) {
+ throw new UnsupportedOperationException();
+ }
+ }
+
static FileArtifactValue create(Artifact artifact, FileValue fileValue) throws IOException {
boolean isFile = fileValue.isFile();
FileContentsProxy proxy = getProxyFromFileStateValue(fileValue.realFileStateValue());