JDK-4515191 : File.list() needs to improve handling of symbolic links
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.3.1
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2001-10-16
  • Updated: 2001-10-23
  • Resolved: 2001-10-23
Related Reports
Duplicate :  
Description

Name: nt126004			Date: 10/16/2001


java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

Suppose Test is a directory created under /home/sanjoy. Under Test there is only
one directory called t1 i.e. hiearchy is /home/sanjoy/Test/t1. Under t1 there
are two files x.java and y.java. Now we have created a symbolic link of y.java named y 
also in the Test/t1 directory.


The source code of T.java located at /home/sanjoy/ is given below.

Now we run the program under two conditions:

Cond-1: directory t1 has x.java , y.java and hard-link y as stated above.
Cond-2: delete the y.java and run the program.

Cond-1 Output:
==============
File name : x.java
File name : y.java
File name : y

Cond-2 Output:
==============
File name : x.java
Not a directory or file : y

Now my question is:
	Why should we consider hard-link y , when source of the link ( here y.java ) is
deleted , in listing ? Because it is neither a file nor a directory.

One implementation of File.list() shoud not return symbolic links.
Another implementation of File.list() should return symbolic links
provided their sources do exist.

Present implementation of File.list() returns symbolic links although their 
sources do not exist.
In this situation I somehow filter the hard-link y.That means , for Cond-2 my
desired output should be:

Cond-2 Output:
==============
File name : x.java


Source code:

import java.io.*;
import java.lang.*;
 
public class T{
    public static void main(String[] args){
        try{
            File f = new File("Test/t1");
            String[] list = f.list();
 
            for ( int i = 0; i < list.length; i++ ){
                File f1 = new File(f,list[i]);
 
                if ( f1.isFile() || f1.isDirectory() ){
                    System.out.println("File name : " + f1.getName());
                }else{
                    System.out.println("Not a directory or file : " +
f1.getName());
                }
            }
        }catch(Exception e){}
    }
} 
(Review ID: 133681) 
======================================================================

Comments
WORK AROUND Name: nt126004 Date: 10/16/2001 Customer Workaround: I can loop through the listing and consider each entry whether it is a file or directory. If it is neither , I simply ignore it. But it is too costly for a large directory. ======================================================================
11-06-2004