/*
 * Copyright 2015 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.changedetection.state;

import org.gradle.api.UncheckedIOException;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTreeElement;
import org.gradle.api.file.FileVisitDetails;
import org.gradle.api.file.RelativePath;
import org.gradle.api.internal.cache.StringInterner;
import org.gradle.api.internal.file.DefaultFileVisitDetails;
import org.gradle.api.internal.file.FileResolver;
import org.gradle.cache.CacheAccess;
import org.gradle.internal.UncheckedException;
import org.gradle.internal.nativeplatform.filesystem.FileSystem;
import org.gradle.internal.serialize.SerializerRegistry;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * A minimal file set snapshotter is different from the default file collection snapshotter in that it creates a snapshot for every file
 * in the input FileCollection without visiting the files on disk.  This allows files that do not exist yet to be considered part of the
 * FileCollectionSnapshot without that information being lost.
 */
public class MinimalFileSetSnapshotter extends AbstractFileCollectionSnapshotter {
    private final FileSystem fileSystem;

    public MinimalFileSetSnapshotter(FileSnapshotter snapshotter, CacheAccess cacheAccess, StringInterner stringInterner, FileResolver fileResolver, FileSystem fileSystem) {
        super(snapshotter, cacheAccess, stringInterner, fileResolver);
        this.fileSystem = fileSystem;
    }

    @SuppressWarnings("Since15")
    @Override
    protected void visitFiles(FileCollection input, List<FileTreeElement> fileTreeElements, List<FileTreeElement> missingFiles) {
        for (File file : input.getFiles()) {
            BasicFileAttributes fileAttributes = getFileAttributes(file);
            if (fileAttributes != null) {
                fileTreeElements.add(createFileVisitDetails(file, fileAttributes));
            } else {
                missingFiles.add(new MissingFileVisitDetails(file));
            }
        }
    }

    @SuppressWarnings("Since15")
    private DefaultFileVisitDetails createFileVisitDetails(File file, BasicFileAttributes fileAttributes) {
        return new DefaultFileVisitDetails(file, new RelativePath(!fileAttributes.isDirectory(), file.getName()), new AtomicBoolean(), fileSystem, fileSystem, fileAttributes.isDirectory(), fileAttributes.lastModifiedTime().toMillis(), fileAttributes.size());
    }

    @SuppressWarnings("Since15")
    private BasicFileAttributes getFileAttributes(File file) {
        try {
            // Reading the file attributes replaces 4 separate system "stat" calls with a single call
            return Files.readAttributes(file.toPath(), BasicFileAttributes.class);
        } catch (NoSuchFileException e) { // The api isn't optimal for performance because a non-existent file throws an exception
            return null;
        } catch (IOException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        }
    }

    @Override
    public void registerSerializers(SerializerRegistry registry) {
    }

    private static class MissingFileVisitDetails implements FileVisitDetails {
        private final File file;
        private final RelativePath relativePath;

        public MissingFileVisitDetails(File file) {
            this.file = file;
            this.relativePath = new RelativePath(true, file.getName());
        }

        @Override
        public File getFile() {
            return file;
        }

        @Override
        public boolean isDirectory() {
            return false;
        }

        @Override
        public String getName() {
            return file.getName();
        }

        @Override
        public String getPath() {
            return file.getName();
        }

        @Override
        public RelativePath getRelativePath() {
            return relativePath;
        }

        @Override
        public int getMode() {
            return 0;
        }

        @Override
        public long getLastModified() {
            return 0L;
        }

        @Override
        public long getSize() {
            return 0L;
        }

        @Override
        public InputStream open() {
            throw new UncheckedIOException(new FileNotFoundException(file.getAbsolutePath()));
        }

        @Override
        public void copyTo(OutputStream output) {
            throw new UncheckedIOException(new FileNotFoundException(file.getAbsolutePath()));
        }

        @Override
        public boolean copyTo(File target) {
            throw new UncheckedIOException(new FileNotFoundException(file.getAbsolutePath()));
        }

        @Override
        public void stopVisiting() {
            // Ignore
        }
    }
}

@@ -1088,55 +1088,8 @@
 ss;%0A
-import org.gradle.internal.UncheckedException;%0A
 impo
@@ -1270,36 +1270,8 @@
 on;%0A
-import java.io.IOException;%0A
 impo
@@ -1339,189 +1339,17 @@
 ava.
-nio.file.Files;%0Aimport java.nio.file.NoSuchFileException;%0Aimport java.nio.file.attribute.BasicFileAttributes;%0Aimport java.util.List;%0Aimport java.util.concurrent.atomic.AtomicBoolean
+util.List
 ;%0A%0A/
@@ -2111,41 +2111,8 @@
  %7D%0A%0A
-    @SuppressWarnings(%22Since15%22)%0A
     
@@ -2312,721 +2312,130 @@
     
-BasicFileAttributes fileAttributes = getFileAttributes(file);%0A            if (fileAttributes != null) %7B%0A                fileTreeElements.add(createFileVisitDetails(file, fileAttributes));%0A            %7D else %7B%0A                missingFiles.add(new MissingFileVisitDetails(file));%0A            %7D%0A        %7D%0A    %7D%0A%0A    @SuppressWarnings(%22Since15%22)%0A    private DefaultFileVisitDetails createFileVisitDetails(File file, BasicFileAttributes fileAttributes) %7B%0A        return new DefaultFileVisitDetails(file, new RelativePath(!fileAttributes.isDirectory(), file.getName()), new AtomicBoolean(), fileSystem, fileSystem, fileAttributes.isDirectory(), fileAttributes.lastModifiedTime().toMillis(), fileAttributes.size(
+if (file.exists()) %7B%0A                fileTreeElements.add(new DefaultFileVisitDetails(file, fileSystem, fileSystem
 ));%0A    
 %7D%0A%0A 
@@ -2434,104 +2434,22 @@
     
-%7D%0A%0A
     
-@SuppressWarnings(%22Since15%22)%0A    private BasicFileAttributes getFileAttributes(File file)
+    %7D else
  %7B%0A 
@@ -2459,22 +2459,16 @@
     
-try %7B%0A
         
     
@@ -2467,441 +2467,74 @@
     
-    // Reading the file attributes replaces 4 separate system %22stat%22 calls with a single call%0A            return Files.readAttributes(file.toPath(), BasicFileAttributes.class);%0A        %7D catch (NoSuchFileException e) %7B // The api isn't optimal for performance because a non-existent file throws an exception%0A            return null;%0A        %7D catch (IOException e) %7B%0A            throw UncheckedException.throwAsUncheckedException(e);
+missingFiles.add(new MissingFileVisitDetails(file));%0A            %7D
 %0A   

Revert "Use Java 7 nio2 api for reading file metadata"