diff options
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/vfs/FileSystem.java | 14 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/vfs/Path.java | 14 |
2 files changed, 28 insertions, 0 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 3e3b94da63..e7f1fa2513 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 @@ -279,6 +279,20 @@ public abstract class FileSystem { } /** + * Returns the MD5 digest of the file denoted by {@code path}. See + * {@link Path#getMD5Digest} for specification. + */ + protected byte[] getSHA1Digest(final Path path) throws IOException { + // Naive I/O implementation. TODO(olaola): optimize! + return new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return getInputStream(path); + } + }.hash(Hashing.sha1()).asBytes(); + } + + /** * Returns true if "path" denotes an existing symbolic link. See * {@link Path#isSymbolicLink} for specification. */ diff --git a/src/main/java/com/google/devtools/build/lib/vfs/Path.java b/src/main/java/com/google/devtools/build/lib/vfs/Path.java index 657bb03c41..dbec227a22 100644 --- a/src/main/java/com/google/devtools/build/lib/vfs/Path.java +++ b/src/main/java/com/google/devtools/build/lib/vfs/Path.java @@ -995,6 +995,20 @@ public class Path implements Comparable<Path>, Serializable { } /** + * Returns the SHA1 digest of the file denoted by the current path, following + * symbolic links. + * + * <p>This method runs in O(n) time where n is the length of the file, but + * certain implementations may be much faster than the worst case. + * + * @return a new 20-byte array containing the file's SHA1 digest + * @throws IOException if the SHA1 digest could not be computed for any reason + */ + public byte[] getSHA1Digest() throws IOException { + return fileSystem.getSHA1Digest(this); + } + + /** * Opens the file denoted by this path, following symbolic links, for reading, * and returns an input stream to it. * |