You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by se...@apache.org on 2017/10/27 16:26:23 UTC

[6/8] flink git commit: [hotfix] [core] Fix FileUtils.deletePathIfEmpty

[hotfix] [core] Fix FileUtils.deletePathIfEmpty


Project: http://git-wip-us.apache.org/repos/asf/flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/9c21c742
Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/9c21c742
Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/9c21c742

Branch: refs/heads/master
Commit: 9c21c742f9bcaeb8fbb0039d1c3a607d28078f5b
Parents: b5e2551
Author: Stephan Ewen <se...@apache.org>
Authored: Wed Oct 25 13:46:34 2017 +0200
Committer: Stephan Ewen <se...@apache.org>
Committed: Fri Oct 27 17:37:23 2017 +0200

----------------------------------------------------------------------
 .../java/org/apache/flink/util/FileUtils.java   | 29 +++++++++++++++-----
 .../org/apache/flink/util/FileUtilsTest.java    | 24 ++++++++++++++++
 2 files changed, 46 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/9c21c742/flink-core/src/main/java/org/apache/flink/util/FileUtils.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/util/FileUtils.java b/flink-core/src/main/java/org/apache/flink/util/FileUtils.java
index 0d527d5..a7afb43 100644
--- a/flink-core/src/main/java/org/apache/flink/util/FileUtils.java
+++ b/flink-core/src/main/java/org/apache/flink/util/FileUtils.java
@@ -28,6 +28,7 @@ import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.NoSuchFileException;
 import java.nio.file.StandardOpenOption;
+import java.util.Random;
 
 import static org.apache.flink.util.Preconditions.checkNotNull;
 
@@ -55,10 +56,11 @@ public final class FileUtils {
 	 * @return the generated random filename with the given prefix
 	 */
 	public static String getRandomFilename(final String prefix) {
+		final Random rnd = new Random();
 		final StringBuilder stringBuilder = new StringBuilder(prefix);
 
 		for (int i = 0; i < RANDOM_FILE_NAME_LENGTH; i++) {
-			stringBuilder.append(ALPHABET[(int) Math.floor(Math.random() * (double) ALPHABET.length)]);
+			stringBuilder.append(ALPHABET[rnd.nextInt(ALPHABET.length)]);
 		}
 
 		return stringBuilder.toString();
@@ -198,7 +200,7 @@ public final class FileUtils {
 	 *                     the directory could not be cleaned for some reason, for example
 	 *                     due to missing access/write permissions.
 	 */
-	public static void cleanDirectory(File directory) throws IOException, FileNotFoundException {
+	public static void cleanDirectory(File directory) throws IOException {
 		checkNotNull(directory, "directory");
 
 		if (directory.isDirectory()) {
@@ -243,22 +245,35 @@ public final class FileUtils {
 	 * @throws IOException if the delete operation fails
 	 */
 	public static boolean deletePathIfEmpty(FileSystem fileSystem, Path path) throws IOException {
-		FileStatus[] fileStatuses = null;
+		final FileStatus[] fileStatuses;
 
 		try {
 			fileStatuses = fileSystem.listStatus(path);
-		} catch (Exception ignored) {}
+		}
+		catch (FileNotFoundException e) {
+			// path already deleted
+			return true;
+		}
+		catch (Exception e) {
+			// could not access directory, cannot delete
+			return false;
+		}
 
 		// if there are no more files or if we couldn't list the file status try to delete the path
-		if (fileStatuses == null || fileStatuses.length == 0) {
+		if (fileStatuses == null) {
+			// another indicator of "file not found"
+			return true;
+		}
+		else if (fileStatuses.length == 0) {
 			// attempt to delete the path (will fail and be ignored if the path now contains
 			// some files (possibly added concurrently))
 			return fileSystem.delete(path, false);
-		} else {
+		}
+		else {
 			return false;
 		}
 	}
-	
+
 	// ------------------------------------------------------------------------
 
 	/**

http://git-wip-us.apache.org/repos/asf/flink/blob/9c21c742/flink-core/src/test/java/org/apache/flink/util/FileUtilsTest.java
----------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/util/FileUtilsTest.java b/flink-core/src/test/java/org/apache/flink/util/FileUtilsTest.java
index 28bb24a..ada442f 100644
--- a/flink-core/src/test/java/org/apache/flink/util/FileUtilsTest.java
+++ b/flink-core/src/test/java/org/apache/flink/util/FileUtilsTest.java
@@ -18,6 +18,8 @@
 
 package org.apache.flink.util;
 
+import org.apache.flink.core.fs.FileSystem;
+import org.apache.flink.core.fs.Path;
 import org.apache.flink.core.testutils.CheckedThread;
 
 import org.junit.Rule;
@@ -47,6 +49,28 @@ public class FileUtilsTest {
 	// ------------------------------------------------------------------------
 
 	@Test
+	public void testDeletePathIfEmpty() throws IOException {
+		final FileSystem localFs = FileSystem.getLocalFileSystem();
+		
+		final File dir = tmp.newFolder();
+		assertTrue(dir.exists());
+
+		final Path dirPath = new Path(dir.toURI());
+
+		// deleting an empty directory should work
+		assertTrue(FileUtils.deletePathIfEmpty(localFs, dirPath));
+
+		// deleting a non existing directory should work
+		assertTrue(FileUtils.deletePathIfEmpty(localFs, dirPath));
+
+		// create a non-empty dir
+		final File nonEmptyDir = tmp.newFolder();
+		final Path nonEmptyDirPath = new Path(nonEmptyDir.toURI());
+		new FileOutputStream(new File(nonEmptyDir, "filename")).close();
+		assertFalse(FileUtils.deletePathIfEmpty(localFs, nonEmptyDirPath));
+	}
+
+	@Test
 	public void testDeleteQuietly() throws Exception {
 		// should ignore the call
 		FileUtils.deleteDirectoryQuietly(null);