/** * Deletes a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful * @throws IllegalArgumentException if {@code directory} does not exist or is not a directory */ public static void deleteDirectory(final File directory) throws IOException { if (!directory.exists()) { return; } if (!isSymlink(directory)) { cleanDirectory(directory); } if (!directory.delete()) { final String message = "Unable to delete directory " + directory + "."; throw new IOException(message); } }
/** * Schedules a directory recursively for deletion on JVM exit. * * @param directory directory to delete, must not be {@code null} * @throws NullPointerException if the directory is {@code null} * @throws IOException in case deletion is unsuccessful */ private static void deleteDirectoryOnExit(final File directory) throws IOException { if (!directory.exists()) { return; } directory.deleteOnExit(); if (!isSymlink(directory)) { cleanDirectoryOnExit(directory); } }
/** * Finds the size of a directory * * @param directory The directory * @return the size */ private static BigInteger sizeOfDirectoryBig0(final File directory) { final File[] files = directory.listFiles(); if (files == null) { // null if security restricted return BigInteger.ZERO; } BigInteger size = BigInteger.ZERO; for (final File file : files) { try { if (!isSymlink(file)) { size = size.add(sizeOfBig0(file)); } } catch (final IOException ioe) { // Ignore exceptions caught when asking if a File is a symlink. } } return size; }
/** * the size of a director * @param directory the directory to check * @return the size */ private static long sizeOfDirectory0(final File directory) { final File[] files = directory.listFiles(); if (files == null) { // null if security restricted return 0L; } long size = 0; for (final File file : files) { try { if (!isSymlink(file)) { size += sizeOf0(file); // internal method if (size < 0) { break; } } } catch (final IOException ioe) { // Ignore exceptions caught when asking if a File is a symlink. } } return size; }
boolean isSymLink; try { isSymLink = org.apache.commons.io.FileUtils.isSymlink(allFiles[i]); } catch (IOException ioe) { isSymLink = true;
boolean isSymLink; try { isSymLink = org.apache.commons.io.FileUtils.isSymlink(allFiles[i]); } catch(IOException ioe) { isSymLink = true;
@Test public void testIdentifiesBrokenSymlinkFile() throws Exception { if (System.getProperty("os.name").startsWith("Win")) { // cant create symlinks in windows. return; } final File noexistFile = new File(top, "noexist"); final File symlinkFile = new File(top, "fakeinner"); final File badSymlinkInPathFile = new File(symlinkFile, "fakeinner"); final File noexistParentFile = new File("noexist", "file"); setupSymlink(noexistFile, symlinkFile); assertTrue(FileUtils.isSymlink(symlinkFile)); assertFalse(FileUtils.isSymlink(noexistFile)); assertFalse(FileUtils.isSymlink(noexistParentFile)); assertFalse(FileUtils.isSymlink(badSymlinkInPathFile)); }
@Test public void testIdentifiesSymlinkFile() throws Exception { if (System.getProperty("os.name").startsWith("Win")) { // cant create symlinks in windows. return; } final File randomFile = new File(top, "randomfile"); FileUtils.touch(randomFile); final File symlinkFile = new File(top, "fakeinner"); setupSymlink(randomFile, symlinkFile); assertTrue(FileUtils.isSymlink(symlinkFile)); assertFalse(FileUtils.isSymlink(randomFile)); }
@Test public void testIdentifiesSymlinkDir() throws Exception { if (System.getProperty("os.name").startsWith("Win")) { // cant create symlinks in windows. return; } final File randomDirectory = new File(top, "randomDir"); assertTrue(randomDirectory.mkdirs()); final File symlinkDirectory = new File(top, "fakeDir"); setupSymlink(randomDirectory, symlinkDirectory); assertTrue(FileUtils.isSymlink(symlinkDirectory)); assertFalse(FileUtils.isSymlink(randomDirectory)); }
@Test public void testCorrectlyIdentifySymlinkWithParentSymLink() throws Exception { if (System.getProperty("os.name").startsWith("Win")) { // cant create symlinks in windows. return; } final File realParent = new File(top, "realparent"); assertTrue(realParent.mkdirs()); final File symlinkParentDirectory = new File(top, "fakeparent"); setupSymlink(realParent, symlinkParentDirectory); final File realChild = new File(symlinkParentDirectory, "realChild"); assertTrue(realChild.mkdirs()); final File symlinkChild = new File(symlinkParentDirectory, "fakeChild"); setupSymlink(realChild, symlinkChild); assertTrue(FileUtils.isSymlink(symlinkChild)); assertFalse(FileUtils.isSymlink(realChild)); }
@Override public boolean accept(File file) { try { if (FileUtils.isSymlink(file)) { throw new IOException("ODPS-0140171: not allow symlink in archive files:" + file.getName()); } return true; } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public boolean accept(File file) { try { if (FileUtils.isSymlink(file)) { throw new IOException("ODPS-0140171: not allow symlink in archive files:" + file.getName()); } return true; } catch (IOException e) { throw new RuntimeException(e); } } }
/** * Deletes a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful */ public static void deleteDirectory(File directory) throws IOException { if (!directory.exists()) { return; } if (!isSymlink(directory)) { cleanDirectory(directory); } if (!directory.delete()) { String message = "Unable to delete directory " + directory + "."; throw new IOException(message); } }
/** * Deletes a directory recursively. * * @param directory directory to delete * @throws IOException in case deletion is unsuccessful * @throws IllegalArgumentException if {@code directory} does not exist or is not a directory */ public static void deleteDirectory(final File directory) throws IOException { if (!directory.exists()) { return; } if (!isSymlink(directory)) { cleanDirectory(directory); } if (!directory.delete()) { final String message = "Unable to delete directory " + directory + "."; throw new IOException(message); } }
/** * Will clean up symlinks that were created by a launch script */ private void cleanUp() { try { File file = new File(System.getProperty("user.dir")); String[] links = file.list(); for (String name : links) { File potentialSymLink = new File(file, name); if (FileUtils.isSymlink(potentialSymLink)) { if (logger.isDebugEnabled()) { logger.debug("DELETING: " + potentialSymLink); } potentialSymLink.delete(); } } } catch (Exception e) { logger.warn("Failed to remove symlinks", e); } }
/** * Schedules a directory recursively for deletion on JVM exit. * * @param directory directory to delete, must not be {@code null} * @throws NullPointerException if the directory is {@code null} * @throws IOException in case deletion is unsuccessful */ private static void deleteDirectoryOnExit(final File directory) throws IOException { if (!directory.exists()) { return; } directory.deleteOnExit(); if (!isSymlink(directory)) { cleanDirectoryOnExit(directory); } }
/** * Schedules a directory recursively for deletion on JVM exit. * * @param directory directory to delete, must not be <code>null</code> * @throws NullPointerException if the directory is <code>null</code> * @throws IOException in case deletion is unsuccessful */ private static void deleteDirectoryOnExit(File directory) throws IOException { if (!directory.exists()) { return; } directory.deleteOnExit(); if (!isSymlink(directory)) { cleanDirectoryOnExit(directory); } }
/** * Schedules a directory recursively for deletion on JVM exit. * * @param directory directory to delete, must not be {@code null} * @throws NullPointerException if the directory is {@code null} * @throws IOException in case deletion is unsuccessful */ private static void deleteDirectoryOnExit(final File directory) throws IOException { if (!directory.exists()) { return; } directory.deleteOnExit(); if (!isSymlink(directory)) { cleanDirectoryOnExit(directory); } }
/** * Schedules a directory recursively for deletion on JVM exit. * * @param directory directory to delete, must not be {@code null} * @throws NullPointerException if the directory is {@code null} * @throws IOException in case deletion is unsuccessful */ private static void deleteDirectoryOnExit(File directory) throws IOException { if (!directory.exists()) { return; } directory.deleteOnExit(); if (!isSymlink(directory)) { cleanDirectoryOnExit(directory); } }
private void backupBuildFiles(final File source, final File destination) throws IOException { if (source.isDirectory()) { final IOFileFilter changelogFilter = FileFilterUtils.and(DirectoryFileFilter.DIRECTORY, FileFilterUtils.nameFileFilter(CHANGELOG_HISTORY_PLUGIN_DIR_NAME)); final IOFileFilter fileFilter = FileFilterUtils.and(FileFileFilter.FILE, getFileAgeDiffFilter()); IOFileFilter filter = FileFilterUtils.and( FileFilterUtils.or(changelogFilter, fileFilter), getExcludedFilesFilter(), FileFilterUtils.notFileFilter(FileFilterUtils.suffixFileFilter(ZIP_FILE_EXTENSION))); FileUtils.copyDirectory(source, destination, filter); } else if (FileUtils.isSymlink(source)) { // TODO: check if copy symlink needed here } else if (source.isFile()) { FileUtils.copyFile(source, destination); } }