Search in sources :

Example 56 with IOException

use of java.io.IOException in project bazel by bazelbuild.

the class InMemoryFileSystem method getDirectoryEntries.

@Override
protected Collection<Path> getDirectoryEntries(Path path) throws IOException {
    InMemoryDirectoryInfo dirInfo;
    synchronized (this) {
        dirInfo = getDirectory(path);
        if (!dirInfo.outOfScope()) {
            FileStatus status = stat(path, false);
            Preconditions.checkState(status instanceof InMemoryContentInfo);
            if (!((InMemoryContentInfo) status).isReadable()) {
                throw new IOException("Directory is not readable");
            }
            Collection<String> allChildren = dirInfo.getAllChildren();
            List<Path> result = new ArrayList<>(allChildren.size());
            for (String child : allChildren) {
                if (!(child.equals(".") || child.equals(".."))) {
                    result.add(path.getChild(child));
                }
            }
            return result;
        }
    }
    // If we get here, we're out of scope.
    return getDelegatedPath(dirInfo.getEscapingPath()).getDirectoryEntries();
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) FileStatus(com.google.devtools.build.lib.vfs.FileStatus) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 57 with IOException

use of java.io.IOException in project bazel by bazelbuild.

the class FileSystem method getFileSystemType.

/**
   * Returns the type of the file system path belongs to.
   *
   * <p>The string returned is obtained directly from the operating system, so
   * it's a best guess in absence of a guaranteed api.
   *
   * <p>This implementation uses <code>/proc/mounts</code> to determine the
   * file system type.
   */
public String getFileSystemType(Path path) {
    String fileSystem = "unknown";
    int bestMountPointSegmentCount = -1;
    try {
        Path canonicalPath = path.resolveSymbolicLinks();
        Path mountTable = path.getRelative("/proc/mounts");
        try (InputStreamReader reader = new InputStreamReader(mountTable.getInputStream(), ISO_8859_1)) {
            for (String line : CharStreams.readLines(reader)) {
                String[] words = line.split("\\s+");
                if (words.length >= 3) {
                    if (!words[1].startsWith("/")) {
                        continue;
                    }
                    Path mountPoint = path.getFileSystem().getPath(words[1]);
                    int segmentCount = mountPoint.asFragment().segmentCount();
                    if (canonicalPath.startsWith(mountPoint) && segmentCount > bestMountPointSegmentCount) {
                        bestMountPointSegmentCount = segmentCount;
                        fileSystem = words[2];
                    }
                }
            }
        }
    } catch (IOException e) {
    // pass
    }
    return fileSystem;
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Example 58 with IOException

use of java.io.IOException in project bazel by bazelbuild.

the class FileSystemUtils method moveFile.

/**
   * Moves the file from location "from" to location "to", while overwriting a
   * potentially existing "to". File's last modified time, executable and
   * writable bits are also preserved.
   *
   * <p>If no error occurs, the method returns normally. If a parent directory does
   * not exist, a FileNotFoundException is thrown. An IOException is thrown when
   * other erroneous situations occur. (e.g. read errors)
   */
// but not atomic
@ThreadSafe
public static void moveFile(Path from, Path to) throws IOException {
    long mtime = from.getLastModifiedTime();
    boolean writable = from.isWritable();
    boolean executable = from.isExecutable();
    // We don't try-catch here for better performance.
    to.delete();
    try {
        from.renameTo(to);
    } catch (IOException e) {
        asByteSource(from).copyTo(asByteSink(to));
        if (!from.delete()) {
            if (!to.delete()) {
                throw new IOException("Unable to delete " + to);
            }
            throw new IOException("Unable to delete " + from);
        }
    }
    // Preserve mtime.
    to.setLastModifiedTime(mtime);
    if (!writable) {
        // Make file read-only if original was read-only.
        to.setWritable(false);
    }
    // Copy executable bit.
    to.setExecutable(executable);
}
Also used : IOException(java.io.IOException) ThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe) ConditionallyThreadSafe(com.google.devtools.build.lib.concurrent.ThreadSafety.ConditionallyThreadSafe)

Example 59 with IOException

use of java.io.IOException in project bazel by bazelbuild.

the class JavaIoFileSystem method getDirectoryEntries.

@Override
protected Collection<Path> getDirectoryEntries(Path path) throws IOException {
    File file = getIoFile(path);
    String[] entries = null;
    long startTime = Profiler.nanoTimeMaybe();
    try {
        entries = file.list();
        if (entries == null) {
            if (file.exists()) {
                throw new IOException(path + ERR_NOT_A_DIRECTORY);
            } else {
                throw new FileNotFoundException(path + ERR_NO_SUCH_FILE_OR_DIR);
            }
        }
    } finally {
        profiler.logSimpleTask(startTime, ProfilerTask.VFS_DIR, file.getPath());
    }
    Collection<Path> result = new ArrayList<>(entries.length);
    for (String entry : entries) {
        if (!entry.equals(".") && !entry.equals("..")) {
            result.add(path.getChild(entry));
        }
    }
    return result;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File)

Example 60 with IOException

use of java.io.IOException in project bazel by bazelbuild.

the class JavaIoFileSystem method renameTo.

@Override
protected void renameTo(Path sourcePath, Path targetPath) throws IOException {
    synchronized (sourcePath) {
        File sourceFile = getIoFile(sourcePath);
        File targetFile = getIoFile(targetPath);
        if (!sourceFile.renameTo(targetFile)) {
            if (!sourceFile.exists()) {
                throw new FileNotFoundException(sourcePath + ERR_NO_SUCH_FILE_OR_DIR);
            }
            if (targetFile.exists()) {
                if (targetFile.isDirectory() && targetFile.list().length > 0) {
                    throw new IOException(targetPath + ERR_DIRECTORY_NOT_EMPTY);
                } else if (sourceFile.isDirectory() && targetFile.isFile()) {
                    throw new IOException(sourcePath + " -> " + targetPath + ERR_NOT_A_DIRECTORY);
                } else if (sourceFile.isFile() && targetFile.isDirectory()) {
                    throw new IOException(sourcePath + " -> " + targetPath + ERR_IS_DIRECTORY);
                } else {
                    throw new IOException(sourcePath + " -> " + targetPath + ERR_PERMISSION_DENIED);
                }
            } else {
                throw new FileAccessException(sourcePath + " -> " + targetPath + ERR_PERMISSION_DENIED);
            }
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Aggregations

IOException (java.io.IOException)46678 File (java.io.File)8666 InputStream (java.io.InputStream)4827 ArrayList (java.util.ArrayList)4154 Test (org.junit.Test)3912 FileInputStream (java.io.FileInputStream)3019 FileOutputStream (java.io.FileOutputStream)2620 HashMap (java.util.HashMap)2492 FileNotFoundException (java.io.FileNotFoundException)2376 BufferedReader (java.io.BufferedReader)2351 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1970 InputStreamReader (java.io.InputStreamReader)1957 URL (java.net.URL)1923 ByteArrayInputStream (java.io.ByteArrayInputStream)1775 OutputStream (java.io.OutputStream)1543 Path (org.apache.hadoop.fs.Path)1460 Map (java.util.Map)1384 List (java.util.List)1255 Properties (java.util.Properties)1125 ServletException (javax.servlet.ServletException)996