use of com.google.common.hash.HashCode in project buck by facebook.
the class DefaultFileHashCacheTest method whenJarMemberWithHashInManifestIsQueriedThenCacheCorrectlyObtainsIt.
@Test
public void whenJarMemberWithHashInManifestIsQueriedThenCacheCorrectlyObtainsIt() throws IOException {
ProjectFilesystem filesystem = new FakeProjectFilesystem();
DefaultFileHashCache cache = new DefaultFileHashCache(filesystem, Optional.empty());
Path abiJarPath = Paths.get("test-abi.jar");
Path memberPath = Paths.get("SomeClass.class");
String memberContents = "Some contents";
try (HashingDeterministicJarWriter jar = new HashingDeterministicJarWriter(new JarOutputStream(filesystem.newFileOutputStream(abiJarPath)))) {
jar.writeEntry(memberPath.toString(), new ByteArrayInputStream(memberContents.getBytes(StandardCharsets.UTF_8)));
}
HashCode actual = cache.get(ArchiveMemberPath.of(abiJarPath, memberPath));
HashCode expected = Hashing.murmur3_128().hashString(memberContents, StandardCharsets.UTF_8);
assertEquals(expected, actual);
}
use of com.google.common.hash.HashCode in project buck by facebook.
the class HashInputJarsToDexStep method execute.
@Override
public StepExecutionResult execute(final ExecutionContext context) {
ImmutableList.Builder<Path> allInputs = ImmutableList.builder();
allInputs.addAll(primaryInputsToDex.get());
if (secondaryOutputToInputs.isPresent()) {
allInputs.addAll(secondaryOutputToInputs.get().get().values());
}
final Map<String, HashCode> classNamesToHashes = classNamesToHashesSupplier.get();
for (Path path : allInputs.build()) {
try {
final Hasher hasher = Hashing.sha1().newHasher();
new DefaultClasspathTraverser().traverse(new ClasspathTraversal(Collections.singleton(path), filesystem) {
@Override
public void visit(FileLike fileLike) throws IOException {
String className = fileLike.getRelativePath().replaceAll("\\.class$", "");
if (classNamesToHashes.containsKey(className)) {
HashCode classHash = Preconditions.checkNotNull(classNamesToHashes.get(className));
hasher.putBytes(classHash.asBytes());
}
}
});
dexInputsToHashes.put(path, Sha1HashCode.fromHashCode(hasher.hash()));
} catch (IOException e) {
context.logError(e, "Error hashing smart dex input: %s", path);
return StepExecutionResult.ERROR;
}
}
stepFinished = true;
return StepExecutionResult.SUCCESS;
}
use of com.google.common.hash.HashCode in project buck by facebook.
the class DefaultProjectFilesystemDelegate method computeSha1.
@Override
public Sha1HashCode computeSha1(Path pathRelativeToProjectRootOrJustAbsolute) throws IOException {
final Path fileToHash = getPathForRelativePath(pathRelativeToProjectRootOrJustAbsolute);
// Normally, we would just use `Files.hash(fileToHash.toFile(), Hashing.sha1())`, but if
// fileToHash is backed by Jimfs, its toFile() method throws an UnsupportedOperationException.
// Creating the input stream via java.nio.file.Files.newInputStream() avoids this issue.
ByteSource source = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
// which already buffers.
return Files.newInputStream(fileToHash);
}
};
HashCode hashCode = source.hash(Hashing.sha1());
return Sha1HashCode.fromHashCode(hashCode);
}
use of com.google.common.hash.HashCode in project buck by facebook.
the class FilePathHashLoaderTest method doesNotCareAboutFileContents.
@Test
public void doesNotCareAboutFileContents() throws IOException {
FilePathHashLoader loader = new FilePathHashLoader(cellRoot, ImmutableSet.of());
HashCode hashBefore = loader.get(file);
Files.write(file, "Goodbye!".getBytes());
HashCode hashAfter = loader.get(file);
assertThat(hashBefore, equalTo(hashAfter));
}
use of com.google.common.hash.HashCode in project buck by facebook.
the class FilePathHashLoaderTest method cellRootPathDoesNotInfluenceTheHashes.
@Test
public void cellRootPathDoesNotInfluenceTheHashes() throws IOException {
FilePathHashLoader baseLoader = new FilePathHashLoader(cellRoot, ImmutableSet.of(fileInDirectory));
HashCode fileHashCode = baseLoader.get(file);
HashCode fileInDirectoryHashCode = baseLoader.get(fileInDirectory);
HashCode directoryHashCode = baseLoader.get(directory);
Path newCellRoot = vfs.getPath("/different");
Files.move(cellRoot, newCellRoot);
file = newCellRoot.resolve("a.txt");
directory = newCellRoot.resolve("dir");
fileInDirectory = directory.resolve("a.txt");
FilePathHashLoader newLoader = new FilePathHashLoader(newCellRoot, ImmutableSet.of(fileInDirectory));
assertThat(newLoader.get(file), equalTo(fileHashCode));
assertThat(newLoader.get(fileInDirectory), equalTo(fileInDirectoryHashCode));
assertThat(newLoader.get(directory), equalTo(directoryHashCode));
}
Aggregations