use of java.io.IOException in project bazel by bazelbuild.
the class JavaIoFileSystem method createDirectory.
@Override
protected boolean createDirectory(Path path) throws IOException {
// deadlocks.
synchronized (path) {
File file = getIoFile(path);
if (file.mkdir()) {
return true;
}
// We will be checking the state of the parent path as well. Synchronize on it before
// attempting anything.
Path parentDirectory = path.getParentDirectory();
synchronized (parentDirectory) {
if (fileIsSymbolicLink(file)) {
throw new IOException(path + ERR_FILE_EXISTS);
}
if (file.isDirectory()) {
// directory already existed
return false;
} else if (file.exists()) {
throw new IOException(path + ERR_FILE_EXISTS);
} else if (!file.getParentFile().exists()) {
throw new FileNotFoundException(path.getParentDirectory() + ERR_NO_SUCH_FILE_OR_DIR);
}
// synchronization lock.
if (file.mkdir()) {
// Everything is fine finally.
return true;
} else if (!file.getParentFile().canWrite()) {
throw new FileAccessException(path + ERR_PERMISSION_DENIED);
} else {
// Parent exists, is writable, yet we can't create our directory.
throw new FileNotFoundException(path.getParentDirectory() + ERR_NOT_A_DIRECTORY);
}
}
}
}
use of java.io.IOException in project bazel by bazelbuild.
the class WorkspaceASTFunction method compute.
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException, WorkspaceASTFunctionException {
RootedPath workspaceRoot = (RootedPath) skyKey.argument();
FileValue workspaceFileValue = (FileValue) env.getValue(FileValue.key(workspaceRoot));
if (workspaceFileValue == null) {
return null;
}
Path repoWorkspace = workspaceRoot.getRoot().getRelative(workspaceRoot.getRelativePath());
try {
BuildFileAST ast = BuildFileAST.parseBuildFile(ParserInputSource.create(ruleClassProvider.getDefaultWorkspacePrefix(), new PathFragment("/DEFAULT.WORKSPACE")), env.getListener());
if (ast.containsErrors()) {
throw new WorkspaceASTFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Failed to parse default WORKSPACE file"), Transience.PERSISTENT);
}
if (workspaceFileValue.exists()) {
ast = BuildFileAST.parseBuildFile(ParserInputSource.create(repoWorkspace), ast.getStatements(), env.getListener());
if (ast.containsErrors()) {
throw new WorkspaceASTFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Failed to parse WORKSPACE file"), Transience.PERSISTENT);
}
}
ast = BuildFileAST.parseBuildFile(ParserInputSource.create(ruleClassProvider.getDefaultWorkspaceSuffix(), new PathFragment("/DEFAULT.WORKSPACE.SUFFIX")), ast.getStatements(), env.getListener());
if (ast.containsErrors()) {
throw new WorkspaceASTFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Failed to parse default WORKSPACE file suffix"), Transience.PERSISTENT);
}
return new WorkspaceASTValue(splitAST(ast));
} catch (IOException ex) {
throw new WorkspaceASTFunctionException(ex, Transience.TRANSIENT);
}
}
use of java.io.IOException in project bazel by bazelbuild.
the class Classpath method findClasses.
/** Finds all classes that live in or below the given package. */
public static Set<Class<?>> findClasses(String packageName) throws ClassPathException {
Set<Class<?>> result = new LinkedHashSet<>();
String pathPrefix = (packageName + '.').replace('.', '/');
for (String entryName : getClassPath()) {
File classPathEntry = new File(entryName);
if (classPathEntry.exists()) {
try {
Set<String> classNames;
if (classPathEntry.isDirectory()) {
classNames = findClassesInDirectory(classPathEntry, pathPrefix);
} else {
classNames = findClassesInJar(classPathEntry, pathPrefix);
}
for (String className : classNames) {
try {
Class<?> clazz = Class.forName(className);
result.add(clazz);
} catch (UnsatisfiedLinkError | NoClassDefFoundError unused) {
// Ignore: we're most likely running on a different platform.
}
}
} catch (IOException e) {
throw new ClassPathException("Can't read classpath entry %s: %s", entryName, e.getMessage());
} catch (ClassNotFoundException e) {
throw new ClassPathException("Class not found even though it is on the classpath %s: %s", entryName, e.getMessage());
}
}
}
return result;
}
use of java.io.IOException in project bazel by bazelbuild.
the class ResourceFileLoader method loadResource.
/**
* Loads a text resource that is located in a directory on the Java classpath that
* corresponds to the package of <code>relativeToClass</code> using UTF8 encoding.
* E.g.
* <code>loadResource(Class.forName("com.google.foo.Foo", "bar.txt"))</code>
* will look for <code>com/google/foo/bar.txt</code> in the classpath.
*/
public static String loadResource(Class<?> relativeToClass, String resourceName) throws IOException {
ClassLoader loader = relativeToClass.getClassLoader();
// TODO(bazel-team): use relativeToClass.getPackage().getName().
String className = relativeToClass.getName();
String packageName = className.substring(0, className.lastIndexOf('.'));
String path = packageName.replace('.', '/');
String resource = path + '/' + resourceName;
InputStream stream = loader.getResourceAsStream(resource);
if (stream == null) {
throw new IOException(resourceName + " not found.");
}
try {
return new String(ByteStreams.toByteArray(stream), UTF_8);
} finally {
stream.close();
}
}
use of java.io.IOException in project bazel by bazelbuild.
the class WindowsFileSystemTest method testIsJunction.
@Test
public void testIsJunction() throws Exception {
final Map<String, String> junctions = new HashMap<>();
junctions.put("shrtpath/a", "shrttrgt");
junctions.put("shrtpath/b", "longtargetpath");
junctions.put("shrtpath/c", "longta~1");
junctions.put("longlinkpath/a", "shrttrgt");
junctions.put("longlinkpath/b", "longtargetpath");
junctions.put("longlinkpath/c", "longta~1");
junctions.put("abbrev~1/a", "shrttrgt");
junctions.put("abbrev~1/b", "longtargetpath");
junctions.put("abbrev~1/c", "longta~1");
String root = testUtil.scratchDir("shrtpath").getParent().toAbsolutePath().toString();
testUtil.scratchDir("longlinkpath");
testUtil.scratchDir("abbreviated");
testUtil.scratchDir("control/a");
testUtil.scratchDir("control/b");
testUtil.scratchDir("control/c");
testUtil.scratchFile("shrttrgt/file1.txt", "hello");
testUtil.scratchFile("longtargetpath/file2.txt", "hello");
testUtil.createJunctions(junctions);
assertThat(WindowsFileSystem.isJunction(new File(root, "shrtpath/a"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "shrtpath/b"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "shrtpath/c"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "longlinkpath/a"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "longlinkpath/b"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "longlinkpath/c"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "longli~1/a"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "longli~1/b"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "longli~1/c"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "abbreviated/a"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "abbreviated/b"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "abbreviated/c"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "abbrev~1/a"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "abbrev~1/b"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "abbrev~1/c"))).isTrue();
assertThat(WindowsFileSystem.isJunction(new File(root, "control/a"))).isFalse();
assertThat(WindowsFileSystem.isJunction(new File(root, "control/b"))).isFalse();
assertThat(WindowsFileSystem.isJunction(new File(root, "control/c"))).isFalse();
assertThat(WindowsFileSystem.isJunction(new File(root, "shrttrgt/file1.txt"))).isFalse();
assertThat(WindowsFileSystem.isJunction(new File(root, "longtargetpath/file2.txt"))).isFalse();
assertThat(WindowsFileSystem.isJunction(new File(root, "longta~1/file2.txt"))).isFalse();
try {
WindowsFileSystem.isJunction(new File(root, "non-existent"));
Assert.fail("expected failure");
} catch (IOException e) {
assertThat(e.getMessage()).contains("cannot find");
}
assertThat(Arrays.asList(new File(root + "/shrtpath/a").list())).containsExactly("file1.txt");
assertThat(Arrays.asList(new File(root + "/shrtpath/b").list())).containsExactly("file2.txt");
assertThat(Arrays.asList(new File(root + "/shrtpath/c").list())).containsExactly("file2.txt");
assertThat(Arrays.asList(new File(root + "/longlinkpath/a").list())).containsExactly("file1.txt");
assertThat(Arrays.asList(new File(root + "/longlinkpath/b").list())).containsExactly("file2.txt");
assertThat(Arrays.asList(new File(root + "/longlinkpath/c").list())).containsExactly("file2.txt");
assertThat(Arrays.asList(new File(root + "/abbreviated/a").list())).containsExactly("file1.txt");
assertThat(Arrays.asList(new File(root + "/abbreviated/b").list())).containsExactly("file2.txt");
assertThat(Arrays.asList(new File(root + "/abbreviated/c").list())).containsExactly("file2.txt");
}
Aggregations