Search in sources :

Example 1 with FileInfo

use of net.rubygrapefruit.platform.file.FileInfo 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)

Aggregations

NativeException (net.rubygrapefruit.platform.NativeException)1 FileInfo (net.rubygrapefruit.platform.file.FileInfo)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 AccessType (org.gradle.internal.file.FileMetadata.AccessType)1