You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by zh...@apache.org on 2020/07/20 12:28:38 UTC

[flink] branch master updated: [FLINK-18612][fs] Fix the relative path issue in LocalFileSystem

This is an automated email from the ASF dual-hosted git repository.

zhijiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new 858de06  [FLINK-18612][fs] Fix the relative path issue in LocalFileSystem
858de06 is described below

commit 858de0640aef525e7eeb2bbd80f3f489a77a0ac4
Author: Yun Gao <ga...@gmail.com>
AuthorDate: Mon Jul 20 20:27:09 2020 +0800

    [FLINK-18612][fs] Fix the relative path issue in LocalFileSystem
    
    This closes #12918
---
 .../apache/flink/core/fs/local/LocalFileSystem.java    | 17 +++++++++++++++--
 .../flink/core/fs/local/LocalFileSystemTest.java       | 18 ++++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/flink-core/src/main/java/org/apache/flink/core/fs/local/LocalFileSystem.java b/flink-core/src/main/java/org/apache/flink/core/fs/local/LocalFileSystem.java
index 1a748f6..32ae50b 100644
--- a/flink-core/src/main/java/org/apache/flink/core/fs/local/LocalFileSystem.java
+++ b/flink-core/src/main/java/org/apache/flink/core/fs/local/LocalFileSystem.java
@@ -52,6 +52,7 @@ import java.nio.file.NoSuchFileException;
 import java.nio.file.StandardCopyOption;
 
 import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
 
 /**
  * The class {@code LocalFileSystem} is an implementation of the {@link FileSystem} interface
@@ -307,10 +308,22 @@ public class LocalFileSystem extends FileSystem {
 	// ------------------------------------------------------------------------
 
 	/**
-	 * Converts the given Path to a File for this file system.
+	 * Converts the given Path to a File for this file system. If the path is empty,
+	 * we will return <tt>new File(".")</tt> instead of <tt>new File("")</tt>, since
+	 * the latter returns <tt>false</tt> for <tt>isDirectory</tt> judgement (See issue
+	 * https://issues.apache.org/jira/browse/FLINK-18612).
 	 */
 	public File pathToFile(Path path) {
-		return new File(path.getPath());
+		String localPath = path.getPath();
+		checkState(
+			localPath != null,
+			"Cannot convert a null path to File");
+
+		if (localPath.length() == 0) {
+			return new File(".");
+		}
+
+		return new File(localPath);
 	}
 
 	// ------------------------------------------------------------------------
diff --git a/flink-core/src/test/java/org/apache/flink/core/fs/local/LocalFileSystemTest.java b/flink-core/src/test/java/org/apache/flink/core/fs/local/LocalFileSystemTest.java
index 12f1476..5dc6ee4 100644
--- a/flink-core/src/test/java/org/apache/flink/core/fs/local/LocalFileSystemTest.java
+++ b/flink-core/src/test/java/org/apache/flink/core/fs/local/LocalFileSystemTest.java
@@ -29,6 +29,7 @@ import org.apache.flink.util.ExecutorUtils;
 import org.apache.flink.util.FileUtils;
 import org.apache.flink.util.TestLogger;
 
+import org.apache.commons.lang3.RandomStringUtils;
 import org.junit.Assume;
 import org.junit.Rule;
 import org.junit.Test;
@@ -370,6 +371,23 @@ public class LocalFileSystemTest extends TestLogger {
 		}
 	}
 
+	/**
+	 * This test verifies the issue https://issues.apache.org/jira/browse/FLINK-18612.
+	 */
+	@Test
+	public void testCreatingFileInCurrentDirectoryWithRelativePath() throws IOException {
+		FileSystem fs = FileSystem.getLocalFileSystem();
+
+		Path filePath = new Path("local_fs_test_" + RandomStringUtils.randomAlphanumeric(16));
+		try (FSDataOutputStream outputStream = fs.create(filePath, WriteMode.OVERWRITE)) {
+			// Do nothing.
+		} finally {
+			for (int i = 0; i < 10 && fs.exists(filePath); ++i) {
+				fs.delete(filePath, true);
+			}
+		}
+	}
+
 	private Collection<File> createTargetDirectories(File root, int directoryDepth, int numberDirectories) {
 		final StringBuilder stringBuilder = new StringBuilder();