<commit_before>/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.internal.classpath;

import org.gradle.api.GradleException;
import org.gradle.api.UncheckedIOException;
import org.gradle.api.file.RelativePath;
import org.gradle.api.internal.file.archive.ZipEntry;
import org.gradle.api.internal.file.archive.ZipInput;
import org.gradle.api.internal.file.archive.impl.FileZipInput;
import org.gradle.internal.Pair;
import org.gradle.internal.UncheckedException;
import org.gradle.internal.file.FileException;
import org.gradle.internal.file.FileType;
import org.gradle.internal.hash.HashCode;
import org.gradle.internal.hash.Hasher;
import org.gradle.internal.hash.Hashing;
import org.gradle.internal.io.ExponentialBackoff;
import org.gradle.internal.snapshot.FileSystemLocationSnapshot;
import org.gradle.util.GFileUtils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.util.concurrent.TimeUnit;

import static java.lang.String.format;

class InstrumentingClasspathFileTransformer implements ClasspathFileTransformer {
    private static final Logger LOGGER = LoggerFactory.getLogger(InstrumentingClasspathFileTransformer.class);
    private static final int CACHE_FORMAT = 1;

    private final ClasspathWalker classpathWalker;
    private final ClasspathBuilder classpathBuilder;
    private final CachedClasspathTransformer.Transform transform;
    private final HashCode configHash;

    public InstrumentingClasspathFileTransformer(ClasspathWalker classpathWalker, ClasspathBuilder classpathBuilder, CachedClasspathTransformer.Transform transform) {
        this.classpathWalker = classpathWalker;
        this.classpathBuilder = classpathBuilder;
        this.transform = transform;
        this.configHash = configHashFor(transform);
    }

    private HashCode configHashFor(CachedClasspathTransformer.Transform transform) {
        Hasher hasher = Hashing.defaultFunction().newHasher();
        hasher.putInt(CACHE_FORMAT);
        transform.applyConfigurationTo(hasher);
        return hasher.hash();
    }

    @Override
    public File transform(File source, FileSystemLocationSnapshot sourceSnapshot, File cacheDir) {
        String name = sourceSnapshot.getType() == FileType.Directory
            ? source.getName() + ".jar"
            : source.getName();
        String destFileName = hashOf(sourceSnapshot).toString() + '/' + name;
        return transformIfNeeded(source, cacheDir, destFileName);
    }

    private File transformIfNeeded(File source, File cacheDir, String destFileName) {
        File receipt = new File(cacheDir, destFileName + ".receipt");
        File transformed = new File(cacheDir, destFileName);
        if (receipt.isFile()) {
            return transformed;
        }
        if (transformed.isFile()) {
            // A concurrent writer has already started writing to the file.
            // Just wait until the transformed file is ready for consumption.
            waitFor(receipt);
            return transformed;
        }
        try {
            transform(source, transformed);
        } catch (GradleException e) {
            if (e.getCause() instanceof FileAlreadyExistsException) {
                // Mostly harmless race-condition, a concurrent writer has already started writing to the file.
                // We run identical transforms concurrently and we can sometimes finish two transforms at the same
                // time in a way that Files.move (see [ClasspathBuilder.nonReplacingJar]) will see [transformed] created before
                // the move is done.
                // Just wait until the transformed file is ready for consumption.
                LOGGER.debug("Instrumented classpath file '{}' already exists.", destFileName, e);
                waitFor(receipt);
            } else {
                throw e;
            }
        }
        try {
            receipt.createNewFile();
        } catch (IOException e) {
            LOGGER.debug("Failed to create receipt for instrumented classpath file '{}'.", destFileName, e);
        }
        return transformed;
    }

    private void waitFor(File receipt) {
        try {
            @Nullable final File found = ExponentialBackoff
                .of(5, TimeUnit.SECONDS, 50, TimeUnit.MILLISECONDS)
                .retryUntil(() -> receipt.isFile() ? receipt : null);
            if (found != receipt) {
                throw new IllegalStateException(
                    format("Timeout waiting for instrumented classpath file receipt: '{}'", receipt.getName())
                );
            }
        } catch (InterruptedException | IOException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        }
    }

    private HashCode hashOf(FileSystemLocationSnapshot sourceSnapshot) {
        Hasher hasher = Hashing.defaultFunction().newHasher();
        hasher.putHash(configHash);
        // TODO - apply runtime classpath normalization?
        hasher.putHash(sourceSnapshot.getHash());
        return hasher.hash();
    }

    private void transform(File source, File dest) {
        if (isSignedJar(source)) {
            LOGGER.debug("Signed archive '{}'. Skipping instrumentation.", source.getName());
            GFileUtils.copyFile(source, dest);
        } else {
            instrument(source, dest);
        }
    }

    private void instrument(File source, File dest) {
        classpathBuilder.nonReplacingJar(dest, builder -> {
            try {
                visitEntries(source, builder);
            } catch (FileException e) {
                // Badly formed archive, so discard the contents and produce an empty JAR<commit_msg>Include instrumented file name in timeout exception<commit_after>/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.internal.classpath;

import org.gradle.api.GradleException;
import org.gradle.api.UncheckedIOException;
import org.gradle.api.file.RelativePath;
import org.gradle.api.internal.file.archive.ZipEntry;
import org.gradle.api.internal.file.archive.ZipInput;
import org.gradle.api.internal.file.archive.impl.FileZipInput;
import org.gradle.internal.Pair;
import org.gradle.internal.UncheckedException;
import org.gradle.internal.file.FileException;
import org.gradle.internal.file.FileType;
import org.gradle.internal.hash.HashCode;
import org.gradle.internal.hash.Hasher;
import org.gradle.internal.hash.Hashing;
import org.gradle.internal.io.ExponentialBackoff;
import org.gradle.internal.snapshot.FileSystemLocationSnapshot;
import org.gradle.util.GFileUtils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.util.concurrent.TimeUnit;

import static java.lang.String.format;

class InstrumentingClasspathFileTransformer implements ClasspathFileTransformer {
    private static final Logger LOGGER = LoggerFactory.getLogger(InstrumentingClasspathFileTransformer.class);
    private static final int CACHE_FORMAT = 1;

    private final ClasspathWalker classpathWalker;
    private final ClasspathBuilder classpathBuilder;
    private final CachedClasspathTransformer.Transform transform;
    private final HashCode configHash;

    public InstrumentingClasspathFileTransformer(ClasspathWalker classpathWalker, ClasspathBuilder classpathBuilder, CachedClasspathTransformer.Transform transform) {
        this.classpathWalker = classpathWalker;
        this.classpathBuilder = classpathBuilder;
        this.transform = transform;
        this.configHash = configHashFor(transform);
    }

    private HashCode configHashFor(CachedClasspathTransformer.Transform transform) {
        Hasher hasher = Hashing.defaultFunction().newHasher();
        hasher.putInt(CACHE_FORMAT);
        transform.applyConfigurationTo(hasher);
        return hasher.hash();
    }

    @Override
    public File transform(File source, FileSystemLocationSnapshot sourceSnapshot, File cacheDir) {
        String name = sourceSnapshot.getType() == FileType.Directory
            ? source.getName() + ".jar"
            : source.getName();
        String destFileName = hashOf(sourceSnapshot).toString() + '/' + name;
        return transformIfNeeded(source, cacheDir, destFileName);
    }

    private File transformIfNeeded(File source, File cacheDir, String destFileName) {
        File receipt = new File(cacheDir, destFileName + ".receipt");
        File transformed = new File(cacheDir, destFileName);
        if (receipt.isFile()) {
            return transformed;
        }
        if (transformed.isFile()) {
            // A concurrent writer has already started writing to the file.
            // Just wait until the transformed file is ready for consumption.
            waitForReceiptOf(destFileName, receipt);
            return transformed;
        }
        try {
            transform(source, transformed);
        } catch (GradleException e) {
            if (e.getCause() instanceof FileAlreadyExistsException) {
                // Mostly harmless race-condition, a concurrent writer has already started writing to the file.
                // We run identical transforms concurrently and we can sometimes finish two transforms at the same
                // time in a way that Files.move (see [ClasspathBuilder.nonReplacingJar]) will see [transformed] created before
                // the move is done.
                // Just wait until the transformed file is ready for consumption.
                LOGGER.debug("Instrumented classpath file '{}' already exists.", destFileName, e);
                waitForReceiptOf(destFileName, receipt);
            } else {
                throw e;
            }
        }
        try {
            receipt.createNewFile();
        } catch (IOException e) {
            LOGGER.debug("Failed to create receipt for instrumented classpath file '{}'.", destFileName, e);
        }
        return transformed;
    }

    private void waitForReceiptOf(String destFileName, File receipt) {
        if (!waitFor(receipt)) {
            throw new IllegalStateException(
                format("Timeout waiting for instrumented classpath file: '%s'.", destFileName)
            );
        }
    }

    private boolean waitFor(File receipt) {
        try {
            return ExponentialBackoff
                .of(5, TimeUnit.SECONDS, 50, TimeUnit.MILLISECONDS)
                .retryUntil(() -> receipt.isFile() ? receipt : null) == receipt;
        } catch (InterruptedException | IOException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        }
    }

    private HashCode hashOf(FileSystemLocationSnapshot sourceSnapshot) {
        Hasher hasher = Hashing.defaultFunction().newHasher();
        hasher.putHash(configHash);
        // TODO - apply runtime classpath normalization?
        hasher.putHash(sourceSnapshot.getHash());
        return hasher.hash();
    }

    private void transform(File source, File dest) {
        if (isSignedJar(source)) {
            LOGGER.debug("Signed archive '{}'. Skipping instrumentation.", source.getName());
            GFileUtils.copyFile(source, dest);
        } else {
            instrument(source, dest);
        }
    }

    private void instrument(File source, File dest) {
        classpathBuilder.nonReplacingJar(dest, builder -> {
            try {
                visitEntries(source, builder);
            } catch (FileException e) {
                // Badly formed archive, so discard the contents and produce an empty JAR
{'fraction_non_alphanumeric': 0.08501032978804805, 'fraction_numerical': 0.0021424745581146223, 'mean_word_length': 2.907324364723468, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2503411', 'n_tokens_mistral': 3416, 'n_tokens_neox': 3254, 'n_words': 938}