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();
}
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;
}
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);
}
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;
}
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);
}
}
}
}
Aggregations