JDK-4125993 : java.io.File.getParent does not allow for trailing separator
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1.5
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 1998-04-06
  • Updated: 1998-04-06
  • Resolved: 1998-04-06
Related Reports
Duplicate :  
Description

Name: sg39081			Date: 04/06/98


If the path contains a trailing separator, File.getParent returns the 
same pathname with the trailing separator removed instead of 
returning the parent path.
For example, if  path is "first/second/" then getParent returns 
"first/second" instead of "first".

This problem manifests itself on both win32 and AIX  when 
unpacking a jarred file if the file was created using the 
pathname of subdirectories. For example, if test .jar is created 
using "jar cvf test.jar first/second" then "jar xvf test.jar" 
terminates with: 

java.io.IOException: first/second/: could not create directory
	at sun.tools.jar.Main.extractFile(Main.java:395)
	at sun.tools.jar.Main.extract(Main.java:375)
	at sun.tools.jar.Main.run(Main.java:106)
	at sun.tools.jar.Main.main(Main.java:524) 

This does not occur of the file is created with "jar cvf test.jar 
first" because the structure of the file is different and does 
not require calls to getParent.

The trailing separator can be allowed for by adding a test at 
the start of getParent as follows:
--------------------------------------------------------------------------------------
    public String getParent() {
            int index = path.lastIndexOf(separatorChar);

            //  ignore trailing separator
            if (index == path.length() - 1)   
                 index = path.substring(0, index - 1).lastIndexOf(separatorChar); ..........
---------------------------------------------------------------------------------------

  From the language spec for java.io.File:

"A File object contains a path, which is a character string that
can be used to identify a file within a file system. A path is 
assumed to consist of two parts, the directory and the file name, 
separated by the last occurrence within the path of a particular 
character known as the separator character." 

... and for java.io.File.getParent:
"If the path has a parent directory, a String representing the 
path of that parent directory is returned; otherwise, null is 
returned."

The assumption in the first paragraph about the last occurrence 
of the separator doesn't hold if the path ends with a separator. A final separator is redundant but accepted on many
operating systems. It isn't clear whether the assumption is 
intended to define the meaning of "parent" in the second 
paragraph.  Either:

a) File.getParent should  ignore redundant final separators
     or
b) File.mkdirs should allow for the behaviour of getParent. 
     (The above jar example is a case where it currently fails because it tries to make the
       same directory twice.)
(Review ID: 27654)
======================================================================