[jira] [Commented] (IO-552) FilenameUtils.concat fails if second argument (fullFilenameToAdd) starts with '~' (tilde)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Commented] (IO-552) FilenameUtils.concat fails if second argument (fullFilenameToAdd) starts with '~' (tilde)

JIRA jira@apache.org

    [ https://issues.apache.org/jira/browse/IO-552?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16201579#comment-16201579 ]

Bruno P. Kinoshita commented on IO-552:
---------------------------------------

Interesting. I didn't know, but looks like in Linux ~kinow is equivalent to /home/kinow, or to ~ if kinow is the currently logged-in user. My user is kinow, and trying `mkdir ~kinow` fails with directory already exists. But a random `mkdir ~jj` works. But so does `touch ~123`. Which means you can have a file or directory that starts with ~.

The [getPrefixLength() method|https://github.com/apache/commons-io/blob/43720d02405e0b96939b331c1be7812fe5fec877/src/main/java/org/apache/commons/io/FilenameUtils.java#L651] seems to assume that a file like ~kinow is going to be a directory. Thus moving the prefix to the last character + 1.

When that happens, the [doNormalize()|https://github.com/apache/commons-io/blob/43720d02405e0b96939b331c1be7812fe5fec877/src/main/java/org/apache/commons/io/FilenameUtils.java#L348] simply appends the / to the end of the file name.

Altering the getPrefixLength() method to return 0 instead of the length+1 for the case in question, results in "c:/temp/~abc.txt", which I believe is the intended output.

However, this change also breaks 7 tests.

{noformat}
Results :

Failed tests:
  FilenameUtilsTestCase.testConcat:474 expected:<[~user/]> but was:<[a/b/~user]>
  FilenameUtilsTestCase.testGetFullPath:744 expected:<[~user/]> but was:<[]>
  FilenameUtilsTestCase.testGetFullPathNoEndSeparator:779 expected:<[~user]> but was:<[]>
  FilenameUtilsTestCase.testGetPrefix:603 expected:<[~user/]> but was:<[]>
  FilenameUtilsTestCase.testGetPrefixLength:535 expected:<6> but was:<0>
  FilenameUtilsTestCase.testNormalize:192 expected:<~user[/]> but was:<~user[]>
  FilenameUtilsTestCase.testNormalizeNoEndSeparator:368 expected:<~user[/]> but was:<~user[]>

Tests run: 1318, Failures: 7, Errors: 0, Skipped: 4
{noformat}

Not sure how to fix it now. Might need to think about it some more. But would be easier if someone else chimed in to add a second opinion or alternatives. At work I am using FilenameUtils for parsing some file names in RESTful URL's. So might get some extra time to work on it if it impact us.



> FilenameUtils.concat fails if second argument (fullFilenameToAdd) starts with '~' (tilde)
> -----------------------------------------------------------------------------------------
>
>                 Key: IO-552
>                 URL: https://issues.apache.org/jira/browse/IO-552
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Utilities
>    Affects Versions: 2.2, 2.5
>         Environment: Windows 7 64bit, JavaVM 1.8 32bit
>            Reporter: Jochen Tümmers
>            Priority: Critical
>
> {{FilenameUtils.concat("c:/temp", "~abc.txt") returns "~abc.txt/" instead of "c:/temp/~abc.txt".}}
> As a result, the file would be created in the user's home directory instead of c:/temp.
> (Note: I Had to replace all instances of double backslashes that would normally appear in the java code with forward slashes as the editor cannot handle backslashes properly.)
> commons io 2.2. and 2.5 behave the same. 2.3 and 2.4 not tested.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)