public Path createIfNotExists(String name) { Path path = basePath.resolve(name); try { Files.createDirectory(path); } catch (FileAlreadyExistsException e) { // that's ok return path; } catch (IOException e) { throw new RuntimeException(e); } return path; }
private void addEmptyUsernameIfExists(Map<String, File> users) throws IOException { File emptyUsernameConfigFile = new File(usersDirectory, User.CONFIG_XML); if (emptyUsernameConfigFile.exists()) { File newEmptyUsernameDirectory = new File(usersDirectory, EMPTY_USERNAME_DIRECTORY_NAME); Files.createDirectory(newEmptyUsernameDirectory.toPath()); File newEmptyUsernameConfigFile = new File(newEmptyUsernameDirectory, User.CONFIG_XML); Files.move(emptyUsernameConfigFile.toPath(), newEmptyUsernameConfigFile.toPath()); users.put("", newEmptyUsernameDirectory); } }
public Path create(String name) { Path path = basePath.resolve(name); try { Files.createDirectory(path); } catch (IOException e) { throw new RuntimeException(e); } return path; }
private File createUsersDirectoryAsNeeded() throws IOException { File usersDirectory = getUsersDirectory(); if (!usersDirectory.exists()) { try { Files.createDirectory(usersDirectory.toPath()); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Unable to create users directory: " + usersDirectory, e); throw e; } } return usersDirectory; }
/** * Creates a new local Alluxio master with a isolated port. * * @param workDirectory Alluxio work directory, this method will create it if it doesn't exist yet * @return the created Alluxio master */ public static LocalAlluxioMaster create(final String workDirectory) throws IOException { if (!Files.isDirectory(Paths.get(workDirectory))) { Files.createDirectory(Paths.get(workDirectory)); } return new LocalAlluxioMaster(null, null); }
public static DefaultInputModule newDefaultInputModule(AbstractProjectOrModule parent, String key) throws IOException { Path basedir = parent.getBaseDir().resolve(key); Files.createDirectory(basedir); return newDefaultInputModule(key, basedir.toFile()); }
@CanIgnoreReturnValue private Path newDir(String name) throws IOException { Path dir = rootDir.resolve(name); Files.createDirectory(dir); return dir; }
@Override public boolean mkdir( File fileName ) { if ( !fileExists( fileName ) ) { try { Files.createDirectory( path( fileName ) ); return true; } catch ( IOException ignore ) { } } return false; }
@Override public void run() { boolean createSymlink = false; while (!Thread.interrupted()) { try { // trying to switch between a real directory and a symlink (dir -> /a) if (Files.deleteIfExists(file)) { if (createSymlink) { Files.createSymbolicLink(file, target); } else { Files.createDirectory(file); } createSymlink = !createSymlink; } } catch (IOException tolerated) { // it's expected that some of these will fail } Thread.yield(); } } });
private static String prepareCassandraYaml() throws IOException { String original = Resources.toString(getResource("cu-cassandra.yaml"), UTF_8); File tempDirFile = createTempDir(); tempDirFile.deleteOnExit(); Path tmpDirPath = tempDirFile.toPath(); Path dataDir = tmpDirPath.resolve("data"); Files.createDirectory(dataDir); String modified = original.replaceAll("\\$\\{data_directory\\}", dataDir.toAbsolutePath().toString()); Path yamlLocation = tmpDirPath.resolve("cu-cassandra.yaml"); write(modified, yamlLocation.toFile(), UTF_8); return yamlLocation.toAbsolutePath().toString(); }
@Override public void setUp() throws IOException { rootDir = Jimfs.newFileSystem(Configuration.unix()).getPath("/tmp"); Files.createDirectory(rootDir); }
public static DefaultInputDir newDefaultInputDir(AbstractProjectOrModule module, String relativePath) throws IOException { Path basedir = module.getBaseDir().resolve(relativePath); Files.createDirectory(basedir); return new DefaultInputDir(module.key(), relativePath) .setModuleBaseDir(module.getBaseDir()); }
@AndroidIncompatible // Path (for symlink creation) public void testScanDirectory_symlinkToRootCycle() throws IOException { ClassLoader loader = ClassPathTest.class.getClassLoader(); // directory with a cycle, // /root // /child // /[grandchild -> root] java.nio.file.Path root = createTempDirectory("ClassPathTest"); try { createFile(root.resolve("some.txt")); java.nio.file.Path child = createDirectory(root.resolve("child")); createSymbolicLink(child.resolve("grandchild"), root); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(root.toFile(), loader); assertEquals(ImmutableSet.of(new ResourceInfo("some.txt", loader)), scanner.getResources()); } finally { deleteRecursivelyOrLog(root); } }
@AndroidIncompatible // Path (for symlink creation) public void testScanDirectory_symlinkCycle() throws IOException { ClassLoader loader = ClassPathTest.class.getClassLoader(); // directory with a cycle, // /root // /left // /[sibling -> right] // /right // /[sibling -> left] java.nio.file.Path root = createTempDirectory("ClassPathTest"); try { java.nio.file.Path left = createDirectory(root.resolve("left")); createFile(left.resolve("some.txt")); java.nio.file.Path right = createDirectory(root.resolve("right")); createFile(right.resolve("another.txt")); createSymbolicLink(left.resolve("sibling"), right); createSymbolicLink(right.resolve("sibling"), left); ClassPath.DefaultScanner scanner = new ClassPath.DefaultScanner(); scanner.scan(root.toFile(), loader); assertEquals( ImmutableSet.of( new ResourceInfo("left/some.txt", loader), new ResourceInfo("left/sibling/another.txt", loader), new ResourceInfo("right/another.txt", loader), new ResourceInfo("right/sibling/some.txt", loader)), scanner.getResources()); } finally { deleteRecursivelyOrLog(root); } }
public void testByteSource_size_ofDirectory() throws IOException { try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { Path dir = fs.getPath("dir"); Files.createDirectory(dir); ByteSource source = MoreFiles.asByteSource(dir); assertThat(source.sizeIfKnown()).isAbsent(); try { source.size(); fail(); } catch (IOException expected) { } } }
public void testByteSource_size_ofSymlinkToDirectory() throws IOException { try (FileSystem fs = Jimfs.newFileSystem(Configuration.unix())) { Path dir = fs.getPath("dir"); Files.createDirectory(dir); Path link = fs.getPath("link"); Files.createSymbolicLink(link, dir); ByteSource source = MoreFiles.asByteSource(link); assertThat(source.sizeIfKnown()).isAbsent(); try { source.size(); fail(); } catch (IOException expected) { } } }
@Test public void should_not_delete_report_if_property_is_set() throws IOException { when(properties.shouldKeepReport()).thenReturn(true); Path reportDir = temp.getRoot().toPath().resolve("scanner-report"); Files.createDirectory(reportDir); underTest.start(); underTest.stop(); assertThat(reportDir).isDirectory(); }
@Test public void should_delete_report_by_default() throws IOException { Path reportDir = temp.getRoot().toPath().resolve("scanner-report"); Files.createDirectory(reportDir); underTest.start(); underTest.stop(); assertThat(reportDir).doesNotExist(); }
public void testPredicates() throws IOException { Path file = createTempFile(); Path dir = tempDir.resolve("dir"); Files.createDirectory(dir); assertTrue(MoreFiles.isDirectory().apply(dir)); assertFalse(MoreFiles.isRegularFile().apply(dir)); assertFalse(MoreFiles.isDirectory().apply(file)); assertTrue(MoreFiles.isRegularFile().apply(file)); Path symlinkToDir = tempDir.resolve("symlinkToDir"); Path symlinkToFile = tempDir.resolve("symlinkToFile"); Files.createSymbolicLink(symlinkToDir, dir); Files.createSymbolicLink(symlinkToFile, file); assertTrue(MoreFiles.isDirectory().apply(symlinkToDir)); assertFalse(MoreFiles.isRegularFile().apply(symlinkToDir)); assertFalse(MoreFiles.isDirectory().apply(symlinkToFile)); assertTrue(MoreFiles.isRegularFile().apply(symlinkToFile)); assertFalse(MoreFiles.isDirectory(NOFOLLOW_LINKS).apply(symlinkToDir)); assertFalse(MoreFiles.isRegularFile(NOFOLLOW_LINKS).apply(symlinkToFile)); }
private AlluxioURI createPersistedDirectories(int levels) throws Exception { AlluxioURI ufsMount = new AlluxioURI(mTestFolder.newFolder().getAbsolutePath()); // Create top-level directory in UFS. Files.createDirectory(Paths.get(ufsMount.join(DIR_TOP_LEVEL).getPath())); createPersistedDirectoryLevel(levels, ufsMount.join(DIR_TOP_LEVEL)); return ufsMount; }