<commit_before>/*
 * Copyright 2016 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.api.internal.tasks;

import java.io.File;
import java.util.Collection;

import static org.gradle.internal.FileUtils.canonicalize;
import static org.gradle.util.GFileUtils.mkdirs;

public class TaskOutputsUtil {
    public static void validateFile(String propertyName, File file, Collection<String> messages) {
        if (file.exists() && file.isDirectory()) {
            messages.add(String.format("Cannot write to file '%s' specified for property '%s' as it is a directory.", file, propertyName));
        }

        for (File candidate = file.getParentFile(); candidate != null && !candidate.isDirectory(); candidate = candidate.getParentFile()) {
            if (candidate.exists() && !candidate.isDirectory()) {
                messages.add(String.format("Cannot write to file '%s' specified for property '%s', as ancestor '%s' is not a directory.", file, propertyName, candidate));
                break;
            }
        }
    }

    public static void validateDirectory(String propertyName, File directory, Collection<String> messages) {
        if (directory.exists() && !directory.isDirectory()) {
            messages.add(String.format("Directory '%s' specified for property '%s' is not a directory.", directory, propertyName));
            return;
        }

        for (File candidate = directory; candidate != null && !candidate.isDirectory(); candidate = candidate.getParentFile()) {
            if (candidate.exists() && !candidate.isDirectory()) {
                messages.add(String.format("Cannot write to directory '%s' specified for property '%s', as ancestor '%s' is not a directory.", directory, propertyName, candidate));
                return;
            }
        }
    }

    public static void ensureDirectoryExists(File directory) {
        mkdirs(canonicalize(directory));
    }

    public static void ensureParentDirectoryExists(File file) {
        mkdirs(canonicalize(file).getParentFile());
    }
}
<commit_msg>Tweaked the task output file and directory validation checks to reduce the number of times the filesystem is touched when the output file or directory already exists.<commit_after>/*
 * Copyright 2016 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.api.internal.tasks;

import java.io.File;
import java.util.Collection;

import static org.gradle.internal.FileUtils.canonicalize;
import static org.gradle.util.GFileUtils.mkdirs;

public class TaskOutputsUtil {
    public static void validateFile(String propertyName, File file, Collection<String> messages) {
        if (file.exists()) {
            if (file.isDirectory()) {
                messages.add(String.format("Cannot write to file '%s' specified for property '%s' as it is a directory.", file, propertyName));
            }
            // else, assume we can write to anything that exists and is not a directory
        } else {
            for (File candidate = file.getParentFile(); candidate != null && !candidate.isDirectory(); candidate = candidate.getParentFile()) {
                if (candidate.exists() && !candidate.isDirectory()) {
                    messages.add(String.format("Cannot write to file '%s' specified for property '%s', as ancestor '%s' is not a directory.", file, propertyName, candidate));
                    break;
                }
            }
        }
    }

    public static void validateDirectory(String propertyName, File directory, Collection<String> messages) {
        if (directory.exists()) {
            if (!directory.isDirectory()) {
                messages.add(String.format("Directory '%s' specified for property '%s' is not a directory.", directory, propertyName));
            }
        } else {
            for (File candidate = directory.getParentFile(); candidate != null && !candidate.isDirectory(); candidate = candidate.getParentFile()) {
                if (candidate.exists() && !candidate.isDirectory()) {
                    messages.add(String.format("Cannot write to directory '%s' specified for property '%s', as ancestor '%s' is not a directory.", directory, propertyName, candidate));
                    return;
                }
            }
        }
    }

    public static void ensureDirectoryExists(File directory) {
        mkdirs(canonicalize(directory));
    }

    public static void ensureParentDirectoryExists(File file) {
        mkdirs(canonicalize(file).getParentFile());
    }
}

{'fraction_non_alphanumeric': 0.10326960871895659, 'fraction_numerical': 0.002858674289798106, 'mean_word_length': 2.9450317124735728, 'pattern_counts': {'":': 0, '<': 7, '<?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': '5015665', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1341, 'n_words': 503}