Search in sources :

Example 1 with AccessType

use of org.gradle.internal.file.FileMetadata.AccessType in project gradle by gradle.

the class NativePlatformBackedFileMetadataAccessor method stat.

@Override
public FileMetadata stat(File f) {
    FileInfo stat;
    try {
        stat = files.stat(f, false);
    } catch (NativeException e) {
        throw new UncheckedIOException("Could not stat file " + f.getAbsolutePath(), e);
    }
    AccessType accessType = AccessType.viaSymlink(stat.getType() == FileInfo.Type.Symlink);
    if (accessType == AccessType.VIA_SYMLINK) {
        try {
            stat = files.stat(f, true);
        } catch (NativeException e) {
            // For a symlink cycle, file.exists() returns false when unable to stat the file.
            if (!f.exists()) {
                return DefaultFileMetadata.missing(accessType);
            }
            throw new UncheckedIOException("Could not stat file " + f.getAbsolutePath(), e);
        }
    }
    switch(stat.getType()) {
        case File:
            return DefaultFileMetadata.file(stat.getLastModifiedTime(), stat.getSize(), accessType);
        case Directory:
            return DefaultFileMetadata.directory(accessType);
        case Missing:
            return DefaultFileMetadata.missing(accessType);
        case Other:
            throw new UncheckedIOException("Unsupported file type for " + f.getAbsolutePath());
        default:
            throw new IllegalArgumentException("Unrecognised file type: " + stat.getType());
    }
}
Also used : FileInfo(net.rubygrapefruit.platform.file.FileInfo) UncheckedIOException(org.gradle.api.UncheckedIOException) NativeException(net.rubygrapefruit.platform.NativeException) AccessType(org.gradle.internal.file.FileMetadata.AccessType)

Example 2 with AccessType

use of org.gradle.internal.file.FileMetadata.AccessType in project gradle by gradle.

the class NioFileMetadataAccessor method stat.

@Override
public FileMetadata stat(File file) {
    Path path = file.toPath();
    BasicFileAttributes attributes;
    try {
        attributes = Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
    } catch (IOException e) {
        return DefaultFileMetadata.missing(AccessType.DIRECT);
    }
    AccessType accessType = AccessType.viaSymlink(attributes.isSymbolicLink());
    if (accessType == AccessType.VIA_SYMLINK) {
        try {
            attributes = Files.readAttributes(path, BasicFileAttributes.class);
        } catch (IOException e) {
            return DefaultFileMetadata.missing(AccessType.VIA_SYMLINK);
        }
    }
    if (attributes.isDirectory()) {
        return DefaultFileMetadata.directory(accessType);
    }
    if (attributes.isOther()) {
        throw new UncheckedIOException("Unsupported file type for " + file.getAbsolutePath());
    }
    return DefaultFileMetadata.file(attributes.lastModifiedTime().toMillis(), attributes.size(), accessType);
}
Also used : Path(java.nio.file.Path) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) AccessType(org.gradle.internal.file.FileMetadata.AccessType)

Aggregations

UncheckedIOException (org.gradle.api.UncheckedIOException)2 AccessType (org.gradle.internal.file.FileMetadata.AccessType)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 NativeException (net.rubygrapefruit.platform.NativeException)1 FileInfo (net.rubygrapefruit.platform.file.FileInfo)1