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

J.P. commented on IO-545:
-------------------------

The OS is irrelevant because getFullPath just does parsing, it doesn't hit the 
file system. To be totally sure, I tested on Ubuntu 16.04.2 and got the same 
result. 

FilenameUtils.getFullPath("~tildefilename.txt") returns:
 ~tildefilename.txt/

It should return an empty string.

I went through the code for getFullPath and see where the issue is. It 
eventually calls getPrefixLength which has this block:
{code:java}
if (ch0 == '~') {
    int posUnix = filename.indexOf(UNIX_SEPARATOR, 1);
    int posWin = filename.indexOf(WINDOWS_SEPARATOR, 1);
    if (posUnix == -1 && posWin == -1) {
        return len + 1;  // return a length greater than the input
    }
    posUnix = posUnix == -1 ? posWin : posUnix;
    posWin = posWin == -1 ? posUnix : posWin;
    return Math.min(posUnix, posWin) + 1;
}
{code}

So if you pass getFullPath just a file name, with no path part, and that file 
name starts with a '~', the if (posUnix == -1 && posWin == -1) condition will 
be true. It is interpreting it as a "named user". It's actually in the javadoc 
for the method:
 * ~user/a/b/c.txt     --> "~user/"    --> named user
 * ~user               --> "~user/"    --> named user (slash added)

However, since you can have a file named something like "~tildefilename.txt", 
this is a problem. Either the method needs to be altered to account for this 
(although I am not sure it is possible to figure out if it is a file or a named 
user without hitting the file system), or at the very least the javadoc needs 
to be updated to note that the method will fail in these cases.

> FilenameUtils.getFullPath incorrectly parses file names that begin with a 
> tilde
> -------------------------------------------------------------------------------
>
>                 Key: IO-545
>                 URL: https://issues.apache.org/jira/browse/IO-545
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Utilities
>    Affects Versions: 2.4, 2.5
>         Environment: OpenJDK Runtime Environment (build 1.8.0_92-b14)
> FreeBSD 10.0-RELEASE-p11
>            Reporter: J.P.
>            Priority: Minor
>
> For any file with a name that begins with a tilde, which is a valid file name 
> in BSD and Windows, if you call FilenameUtils.getFullPath and pass the file 
> name, it returns the file name followed by a path separator. It should return 
> an empty string. It seems to be parsing it as a unix user directory because 
> it starts with the tilde.



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

Reply via email to