Search in sources :

Example 96 with HashCode

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);
}
Also used : ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) Path(java.nio.file.Path) HashCode(com.google.common.hash.HashCode) ByteArrayInputStream(java.io.ByteArrayInputStream) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) HashingDeterministicJarWriter(com.facebook.buck.io.HashingDeterministicJarWriter) JarOutputStream(java.util.jar.JarOutputStream) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 97 with HashCode

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;
}
Also used : Path(java.nio.file.Path) ClasspathTraversal(com.facebook.buck.jvm.java.classes.ClasspathTraversal) Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) ImmutableList(com.google.common.collect.ImmutableList) DefaultClasspathTraverser(com.facebook.buck.jvm.java.classes.DefaultClasspathTraverser) IOException(java.io.IOException) FileLike(com.facebook.buck.jvm.java.classes.FileLike)

Example 98 with HashCode

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);
}
Also used : Path(java.nio.file.Path) HashCode(com.google.common.hash.HashCode) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) ByteSource(com.google.common.io.ByteSource)

Example 99 with 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));
}
Also used : HashCode(com.google.common.hash.HashCode) Test(org.junit.Test)

Example 100 with HashCode

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));
}
Also used : Path(java.nio.file.Path) HashCode(com.google.common.hash.HashCode) Test(org.junit.Test)

Aggregations

HashCode (com.google.common.hash.HashCode)120 Path (java.nio.file.Path)40 Test (org.junit.Test)39 BuildTarget (com.facebook.buck.model.BuildTarget)19 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)13 IOException (java.io.IOException)13 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)12 PathSourcePath (com.facebook.buck.rules.PathSourcePath)11 FileHashCache (com.facebook.buck.util.cache.FileHashCache)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)10 Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)10 Hasher (com.google.common.hash.Hasher)9 Map (java.util.Map)9 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)7 SourcePath (com.facebook.buck.rules.SourcePath)7 File (java.io.File)7 BuckEventBus (com.facebook.buck.event.BuckEventBus)6 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)6 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)5