Bug #77368 "LOAD DATA LOCAL INFILE” doesn't work properly with relative paths
Submitted: 16 Jun 2015 8:35 Modified: 22 Mar 2023 15:46
Reporter: Jakub Denk Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:5.1.35 OS:Any
Assigned to: Filipe Silva CPU Architecture:Any

[16 Jun 2015 8:35] Jakub Denk
Description:
When I try to use the command like this: "LOAD DATA LOCAL INFILE ./data/file.csv", mysql driver doesn't resolve it against the java system property "user.dir", as it should.

The root cause is in the class com.mysql.jdbc.MysqlIO. The code, constructing InputStream looks like this:

  } else if (!this.connection.getAllowUrlInLocalInfile()) {
                fileIn = new BufferedInputStream(new FileInputStream(fileName));
  } 

But regarding this "feature" (more likely bug) in java itself : http://bugs.java.com/bugdatabase/view_bug.do;:YfiG?bug_id=4483097 
the code should look like this:

  } else if (!this.connection.getAllowUrlInLocalInfile()) {
                fileIn = new BufferedInputStream(new FileInputStream(new File(fileName)).getCanonicalFile());
  } 

How to repeat:
- Prepare simple script using "LOAD DATA LOCAL INFILE" command
- put csv file to different path, than the application jar (e.g. application is on path "~/app.jar" and the csv file is on path "/mnt/tmp/file.csv")
- regarding the example - the application should set "user.dir" to "/mnt/tmp/"
- regarding the example - the script should reference the csv file like this: "LOAD DATA LOCAL INFILE ./file.csv" 

Suggested fix:
Resolve all files like this:
new File(fileName)).getCanonicalFile()
[25 Jun 2015 18:18] Filipe Silva
Hi Jakub,

Thank you for this bug report.

Verified as described.
[22 Mar 2023 15:46] Daniel So
Posted by developer:
 
Added the following entry to the C/J 8.0.33:

"When executing a LOAD DATA LOCAL INFILE statement, the file could not be loaded if a relative path of it was used and the path was not relative to the directory in which the JVM was started. This patch fixes the behavior by resolving the relative path using the Java system property user.id, which was ignored before in the process."
[4 Apr 2023 17:31] Daniel So
Posted by developer:
 
Updated the changelog entry to the following: 

"When executing a LOAD DATA LOCAL INFILE statement, the file could not be loaded if a relative path of it was used and the path was not relative to the directory in which the JVM was started. This patch fixes the behavior by resolving the relative path using the Java system property user.dir, which was ignored before in the process."