This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

java.io.File


Here at last is my long-awaited often-promised patch for bringing the
File class up to 1.3 spec. It also fixes a few bugs I noticed, and
gets rid of canonicalisation of paths for security checks. Doing this
seemed kind of innefficient, at my tests with a custom security
manager showed that doing that is was not consisted with the
JDK implementation.

The win32 changes are incomplete and untested. I tried not to actually
break anything, but since I don't have a build environment set up on
Windows I can't test the changes. Hopefully someone else can fill in
the blanks here?

I'm checking this in.

regards

  [ bryce ]


2001-04-01  Bryce McKinlay  <bryce@albatross.co.nz>

	* java/lang/natSystem.cc (init_properties): Get "file.separator",
	"path.separator", and "java.io.tmpdir" from the File class, instead
	of setting them explicitly here.
	* java/io/File.java: Do not canonicalize paths for security manager
	checks. Call init_native() from static initializer. Do not pass path 
	argument to native methods. New native method declarations. Some 
	security manager checks moved to checkWrite().
	(equals): Check file system case sensitivity and act appropriatly.
	(hashCode): Likewise.
	(isHidden): New method implemented.
	(performList): Changed prototype. Now takes a class argument specifying
	the class of the returned array: Strings or File objects. Also added
	FileFilter argument.
	(listFiles): New variants with "File" return type implemented.
	(createTempFile): Use createNewFile(). Use maxPathLen.
	(setReadOnly): New method implemented.
	(listRoots): Likewise.
	(compareTo): Likewise.
	(setLastModified): Likewise.
	(checkWrite): New method.
	(setPath): Removed.
	* java/io/natFile.cc: Various functions no longer take canonical path
	argument.
	(stat): Handle ISHIDDEN query.
	(isAbsolute): Remove WIN32 cruft.
	(performList): New arguments. Handle returning either File[] or String[]
	arrays. Check with FileFilter or FilenameFilter arguments as 
	appropriate. Use an ArrayList, not a Vector, for the temporary list.
	(performSetReadOnly): New method implemented.
	(performListRoots): Likewise.
	(performSetLastModified): Likewise.
	(performCreate): Likewise.
	(init_native): New initialization function.
	* java/io/natFileWin32.cc: Various functions no longer take canonical 
	path argument.
	(stat): Add FIXME about ISHIDDEN query.
	(performList): New arguments. Handle returning either File[] or String[]
	arrays. Check with FileFilter or FilenameFilter arguments as 
	appropriate. Use an ArrayList, not a Vector, for the temporary list.
	(performSetReadOnly): New. Stubbed.
	(performListRoots): Likewise.
	(performSetLastModified): Likewise.
	(performCreate): Likewise.
	(init_native) New initialization function.	
	* configure.in: Check for utime() and chmod().
	* configure: Rebuilt.
	* include/config.h.in: Rebuilt.

Index: configure.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/configure.in,v
retrieving revision 1.73.2.2
diff -u -r1.73.2.2 configure.in
--- configure.in	2001/03/21 18:41:24	1.73.2.2
+++ configure.in	2001/04/01 10:24:45
@@ -398,7 +398,7 @@
 else
    AC_CHECK_FUNCS(strerror ioctl select fstat open fsync sleep)
    AC_CHECK_FUNCS(gmtime_r localtime_r readdir_r getpwuid_r getcwd)
-   AC_CHECK_FUNCS(access stat mkdir rename rmdir unlink realpath)
+   AC_CHECK_FUNCS(access stat mkdir rename rmdir unlink realpath utime chmod)
    AC_CHECK_FUNCS(iconv nl_langinfo setlocale)
    AC_CHECK_FUNCS(inet_aton inet_addr, break)
    AC_CHECK_FUNCS(inet_pton uname inet_ntoa)
@@ -637,6 +637,7 @@
 gcjvers="`$GCJ -v 2>&1 | sed -n 's/^.*version \([^ ]*\).*$/\1/p'`"
 changequote([,])
 AC_DEFINE_UNQUOTED(GCJVERSION, "$gcjvers")
+AC_SUBST(GCJVERSION)
 
 AC_SUBST(AM_RUNTESTFLAGS)
 
Index: java/lang/natSystem.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natSystem.cc,v
retrieving revision 1.35
diff -u -r1.35 natSystem.cc
--- natSystem.cc	2001/02/08 01:49:53	1.35
+++ natSystem.cc	2001/04/01 10:24:45
@@ -55,6 +55,7 @@
 #include <java/lang/StringBuffer.h>
 #include <java/util/Properties.h>
 #include <java/util/TimeZone.h>
+#include <java/io/File.h>
 #include <java/io/PrintStream.h>
 #include <java/io/InputStream.h>
 
@@ -323,20 +324,19 @@
   
   SET ("file.encoding", default_file_encoding);
 
+  JvInitClass (&java::io::File::class$);  
+  newprops->put (JvNewStringLatin1 ("file.separator"), 
+		 java::io::File::separator);
+  newprops->put (JvNewStringLatin1 ("path.separator"),
+		 java::io::File::pathSeparator);
+  newprops->put (JvNewStringLatin1 ("java.io.tmpdir"), 
+		 java::io::File::tmpdir);
+
 #ifdef WIN32
-  SET ("file.separator", "\\");
-  SET ("path.separator", ";");
   SET ("line.separator", "\r\n");
-  SET ("java.io.tmpdir", "C:\\temp");
 #else
   // Unix.
-  SET ("file.separator", "/");
-  SET ("path.separator", ":");
   SET ("line.separator", "\n");
-  char *tmpdir = ::getenv("TMPDIR");
-  if (! tmpdir)
-    tmpdir = "/tmp";
-  SET ("java.io.tmpdir", tmpdir);
 #endif
 
 #ifdef HAVE_UNAME
Index: java/io/File.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/File.java,v
retrieving revision 1.15
diff -u -r1.15 File.java
--- File.java	2001/02/09 08:30:31	1.15
+++ File.java	2001/04/01 10:24:45
@@ -1,6 +1,6 @@
 // File.java - File name
 
-/* Copyright (C) 1998, 1999, 2000  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -21,42 +21,40 @@
 
 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  * "The Java Language Specification", ISBN 0-201-63451-1
- * Status:  Complete to version 1.1; 1.2 functionality missing.
- * A known bug: most calls to the security manager can generate
- * IOException since we use the canonical path.
+ * Status:  Complete to version 1.3.
  */
 
-public class File implements Serializable
+public class File implements Serializable, Comparable
 {
   public boolean canRead ()
   {
-    return access (checkRead (), READ);
+    checkRead();
+    return access (READ);
   }
 
   public boolean canWrite ()
   {
-    SecurityManager s = System.getSecurityManager();
-    String p = safeCanonicalPath ();
-    // FIXME: it isn't entirely clear what to do if we can't find the
-    // canonical path.
-    if (p == null)
-      return false;
-    if (s != null)
-      s.checkWrite(p);
-    return access (p, WRITE);
+    checkWrite();
+    return access (WRITE);
   }
+  
+  private native boolean performCreate() throws IOException;
 
-  private final native static boolean performDelete (String canon);
+  /** @since 1.2 */
+  public boolean createNewFile() throws IOException
+  {
+    checkWrite();
+    return performCreate();
+  }
+  
+  private native boolean performDelete ();
   public boolean delete ()
   {
     SecurityManager s = System.getSecurityManager();
-    String p = safeCanonicalPath ();
-    // FIXME: what is right?
-    if (p == null)
-      return false;
+    String name = path;
     if (s != null)
-      s.checkDelete(p);
-    return performDelete (p);
+      s.checkDelete(path);
+    return performDelete ();
   }
 
   public boolean equals (Object obj)
@@ -64,12 +62,16 @@
     if (! (obj instanceof File))
       return false;
     File other = (File) obj;
-    return path.compareTo(other.path) == 0;
+    if (caseSensitive)
+      return (path.equals(other.path));
+    else
+      return (path.equalsIgnoreCase(other.path));      
   }
 
   public boolean exists ()
   {
-    return access (checkRead (), EXISTS);
+    checkRead();
+    return access (EXISTS);
   }
 
   public File (String p)
@@ -100,6 +102,7 @@
     this (dir == null ? null : dir.path, name);
   }
 
+  // FIXME  ???
   public String getAbsolutePath ()
   {
     if (isAbsolute ())
@@ -107,6 +110,7 @@
     return System.getProperty("user.dir") + separatorChar + path;
   }
 
+  /** @since 1.2 */
   public File getAbsoluteFile () throws IOException
   {
     return new File (getAbsolutePath());
@@ -114,6 +118,7 @@
 
   public native String getCanonicalPath () throws IOException;
 
+  /** @since 1.2 */
   public File getCanonicalFile () throws IOException
   {
     return new File (getCanonicalPath());
@@ -133,6 +138,7 @@
     return path.substring(0, last);
   }
 
+  /** @since 1.2 */
   public File getParentFile ()
   {
     String parent = getParent ();
@@ -146,42 +152,85 @@
 
   public int hashCode ()
   {
-    // FIXME: test.
-    return path.hashCode();
+    if (caseSensitive)
+      return (path.hashCode() ^ 1234321);
+    else
+      return (path.toLowerCase().hashCode() ^ 1234321);
   }
 
   public native boolean isAbsolute ();
 
   public boolean isDirectory ()
   {
-    return stat (checkRead (), DIRECTORY);
+    checkRead();
+    return stat (DIRECTORY);
   }
 
   public boolean isFile ()
   {
-    return stat (checkRead (), ISFILE);
+    checkRead();
+    return stat (ISFILE);
   }
 
+  /** @since 1.2 */
+  public boolean isHidden()
+  {
+    checkRead();
+    return stat (ISHIDDEN);
+  }
+
   public long lastModified ()
   {
-    return attr (checkRead (), MODIFIED);
+    checkRead();
+    return attr (MODIFIED);
   }
 
   public long length ()
   {
-    return attr (checkRead (), LENGTH);
+    checkRead();
+    return attr (LENGTH);
   }
+    
+  private final native Object[] performList (FilenameFilter filter,
+					     FileFilter fileFilter,
+					     Class result_type);
+
+  // Arguments for the performList function. Specifies whether we want
+  // File objects or path strings in the returned object array.
+  private final static int OBJECTS = 0;
+  private final static int STRINGS = 1;
 
-  private final native String[] performList (String canon,
-					     FilenameFilter filter);
   public String[] list (FilenameFilter filter)
   {
-    return performList (checkRead (), filter);
+    checkRead();
+    return (String[]) performList (filter, null, String.class);
   }
 
   public String[] list ()
+  {
+    checkRead();
+    return (String[]) performList (null, null, String.class);
+  }
+
+  /** @since 1.2 */
+  public File[] listFiles()
+  {
+    checkRead();
+    return (File[]) performList (null, null, File.class);
+  }
+  
+  /** @since 1.2 */
+  public File[] listFiles(FilenameFilter filter)
+  {
+    checkRead();
+    return (File[]) performList (filter, null, File.class);
+  }
+  
+  /** @since 1.2 */
+  public File[] listFiles(FileFilter filter)
   {
-    return performList (checkRead (), null);
+    checkRead();
+    return (File[]) performList (null, filter, File.class);
   }
 
   public String toString ()
@@ -195,16 +244,10 @@
   }
 
   private final native boolean performMkdir ();
+
   public boolean mkdir ()
   {
-    SecurityManager s = System.getSecurityManager();
-    if (s != null)
-      {
-	// NOTE: in theory we should use the canonical path.  In
-	// practice, we can't compute the canonical path until we've
-	// made this completely.  Lame.
-	s.checkWrite(path);
-      }
+    checkWrite();
     return performMkdir ();
   }
 
@@ -216,25 +259,17 @@
     String parent = x.getParent();
     if (parent != null)
       {
-	x.setPath(parent);
+	x.path = parent;
 	if (! mkdirs (x))
 	  return false;
-	x.setPath(p);
+	x.path = p;
       }
     return x.mkdir();
   }
 
   public boolean mkdirs ()
   {
-    SecurityManager s = System.getSecurityManager();
-    if (s != null)
-      {
-	// NOTE: in theory we should use the canonical path.  In
-	// practice, we can't compute the canonical path until we've
-	// made this completely.  Lame.
-	s.checkWrite(path);
-      }
-
+    checkWrite();
     if (isDirectory ())
       return false;
     return mkdirs (new File (path));
@@ -245,6 +280,7 @@
     return Long.toString(counter++, Character.MAX_RADIX);
   }
 
+  /** @since 1.2 */
   public static File createTempFile (String prefix, String suffix,
 				     File directory)
     throws IOException
@@ -272,19 +308,16 @@
     if (suffix == null)
       suffix = ".tmp";
 
-    // FIXME: filename length varies by architecture and filesystem.
-    int max_length = 255;
-
     // Truncation rules.
     // `6' is the number of characters we generate.
-    if (prefix.length () + 6 + suffix.length () > max_length)
+    if (prefix.length () + 6 + suffix.length () > maxPathLen)
       {
 	int suf_len = 0;
 	if (suffix.charAt(0) == '.')
 	  suf_len = 4;
 	suffix = suffix.substring(0, suf_len);
-	if (prefix.length () + 6 + suf_len > max_length)
-	  prefix = prefix.substring(0, max_length - 6 - suf_len);
+	if (prefix.length () + 6 + suf_len > maxPathLen)
+	  prefix = prefix.substring(0, maxPathLen - 6 - suf_len);
       }
 
     File f;
@@ -298,27 +331,10 @@
 	try
 	  {
 	    f = new File(directory, l);
-	    if (f.exists())
-	      continue;
-	    else
-	      {
-		String af = f.getAbsolutePath ();
-		
-		// Check to see if we're allowed to write to it.
-		SecurityManager s = System.getSecurityManager();
-		if (s != null)
-		  s.checkWrite (af);
-		
-		// Now create the file.
-		FileDescriptor fd = 
-		  new FileDescriptor (af, 
-				      FileDescriptor.WRITE
-				      | FileDescriptor.EXCL);
-		fd.close ();
-		return f;
-	      }
+	    if (f.createNewFile())
+	      return f;
 	  }
-	catch (IOException _)
+	catch (IOException ignored)
 	  {
 	  }
       }
@@ -326,75 +342,148 @@
     throw new IOException ("cannot create temporary file");
   }
 
+  private native boolean performSetReadOnly();
+
+  /** @since 1.2 */
+  public boolean setReadOnly()
+  {
+    checkWrite();
+    return performSetReadOnly();
+  }
+
+  private static native File[] performListRoots();
+
+  /** @since 1.2 */
+  public static File[] listRoots()
+  {
+    File[] roots = performListRoots();
+    
+    SecurityManager s = System.getSecurityManager();
+    if (s != null)
+      {
+	// Only return roots to which the security manager permits read access.
+	int count = roots.length;
+	for (int i = 0; i < roots.length; i++)
+	  {
+	    try
+	      {
+        	s.checkRead(roots[i].path);		
+	      }
+	    catch (SecurityException sx)
+	      {
+	        roots[i] = null;
+		count--;
+	      }
+	  }
+	if (count != roots.length)
+	  {
+	    File[] newRoots = new File[count];
+	    int k = 0;
+	    for (int i=0; i < roots.length; i++)
+	      {
+	        if (roots[i] != null)
+		  newRoots[k++] = roots[i];
+	      }
+	    roots = newRoots;
+	  }
+      }
+    return roots;
+  }
+
   public static File createTempFile (String prefix, String suffix)
     throws IOException
   {
     return createTempFile (prefix, suffix, null);
   }
+
+  /** @since 1.2 */
+  public int compareTo(File other)
+  {
+    if (caseSensitive)
+      return path.compareTo (other.path);
+    else
+      return path.compareToIgnoreCase (other.path);
+  }
+
+  /** @since 1.2 */
+  public int compareTo(Object o)
+  {
+    File other = (File) o;
+    return compareTo (other);
+  }
 
-  private final native boolean performRenameTo (File dest);
+  private native boolean performRenameTo (File dest);
   public boolean renameTo (File dest)
   {
     SecurityManager s = System.getSecurityManager();
+    String sname = getName();
+    String dname = dest.getName();
     if (s != null)
       {
-	// FIXME: JCL doesn't specify which path to check.  We check the
-	// source since we can canonicalize it.
-	s.checkWrite(safeCanonicalPath());
+	s.checkWrite(sname);
+	s.checkWrite(dname);
       }
     return performRenameTo (dest);
   }
 
-  public static final String pathSeparator
-    = System.getProperty("path.separator");
-  public static final char pathSeparatorChar = pathSeparator.charAt(0);
-  public static final String separator = System.getProperty("file.separator");
-  public static final char separatorChar = separator.charAt(0);
+  private native boolean performSetLastModified(long time);
+  
+  /** @since 1.2 */
+  public boolean setLastModified(long time)
+  {
+    checkWrite();
+    return performSetLastModified(time);
+  }
+
+  public static final String separator = null;
+  public static final String pathSeparator = null;
+  static final String tmpdir = null;
+  static int maxPathLen;
+  static boolean caseSensitive;
+  
+  public static final char separatorChar;
+  public static final char pathSeparatorChar;
+  
+  static
+  {
+    init_native();
+    pathSeparatorChar = pathSeparator.charAt(0);
+    separatorChar = separator.charAt(0);
+  }
+  
+  // Native function called at class initialization. This should should
+  // set the separator, pathSeparator, tmpdir, maxPathLen, and caseSensitive
+  // variables.
+  private static native void init_native();
 
-  private static final String tmpdir = System.getProperty("java.io.tmpdir");
-
   // The path.
   private String path;
 
   // We keep a counter for use by createTempFile.  We choose the first
   // value randomly to try to avoid clashes with other VMs.
   private static long counter = Double.doubleToLongBits (Math.random ());
-
-  // mkdirs() uses this to avoid repeated allocations.
-  private final void setPath (String n)
-  {
-    path = n;
-  }
-
 
-  private final String checkRead ()
+  private void checkWrite ()
   {
     SecurityManager s = System.getSecurityManager();
-    String p = safeCanonicalPath ();
-    if (p == null)
-      return null;
     if (s != null)
-      s.checkRead(p);
-    return p;
+      s.checkWrite(path);
   }
 
-  // Return canonical path, or null.
-  private final String safeCanonicalPath ()
+  private void checkRead ()
   {
-    String p = null;
-    try
-      {
-	p = getCanonicalPath ();
-      }
-    catch (IOException x)
-      {
-	// Nothing.
-      }
-    return p;
+    SecurityManager s = System.getSecurityManager();
+    if (s != null)
+      s.checkRead(path);
   }
 
-  // Add this File to the set of files to be deleted upon normal
-  // termination.
+  /** 
+    * Add this File to the set of files to be deleted upon normal
+    * termination.
+    *
+    * @since 1.2 
+    */
+  // FIXME: This should use the ShutdownHook API once we implement that.
   public void deleteOnExit ()
   {
     SecurityManager sm = System.getSecurityManager ();
@@ -430,14 +519,15 @@
   // QUERY arguments to stat function.
   private final static int DIRECTORY = 0;
   private final static int ISFILE = 1;
+  private final static int ISHIDDEN = 2;
 
   // QUERY arguments to attr function.
   private final static int MODIFIED = 0;
   private final static int LENGTH = 1;
-
-  private final native long attr (String p, int query);
-  private final native boolean access (String p, int query);
-  private final native boolean stat (String p, int query);
+  
+  private final native long attr (int query);
+  private final native boolean access (int query);
+  private final native boolean stat (int query);
 
   private static final long serialVersionUID = 301077366599181567L;
 }
Index: java/io/natFile.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFile.cc,v
retrieving revision 1.7
diff -u -r1.7 natFile.cc
--- natFile.cc	2000/04/21 20:38:42	1.7
+++ natFile.cc	2001/04/01 10:24:45
@@ -1,6 +1,6 @@
-// natFile.cc - Native part of File class.
+// natFile.cc - Native part of File class for POSIX.
 
-/* Copyright (C) 1998, 1999, 2000  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -24,23 +24,23 @@
 #include <dirent.h>
 #endif
 #include <string.h>
+#include <utime.h>
 
 #include <gcj/cni.h>
 #include <jvm.h>
 #include <java/io/File.h>
 #include <java/io/IOException.h>
-#include <java/util/Vector.h>
+#include <java/util/ArrayList.h>
 #include <java/lang/String.h>
 #include <java/io/FilenameFilter.h>
+#include <java/io/FileFilter.h>
 #include <java/lang/System.h>
 
 jboolean
-java::io::File::access (jstring canon, jint query)
+java::io::File::access (jint query)
 {
-  if (! canon)
-    return false;
   char buf[MAXPATHLEN];
-  jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   // FIXME?
   buf[total] = '\0';
   JvAssert (query == READ || query == WRITE || query == EXISTS);
@@ -59,15 +59,16 @@
 }
 
 jboolean
-java::io::File::stat (jstring canon, jint query)
+java::io::File::stat (jint query)
 {
-  if (! canon)
-    return false;
   char buf[MAXPATHLEN];
-  jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   // FIXME?
   buf[total] = '\0';
 
+  if (query == ISHIDDEN)
+    return (getName()->charAt(0) == '.');
+
 #ifdef HAVE_STAT
   struct stat sb;
   if (::stat (buf, &sb))
@@ -82,13 +83,10 @@
 }
 
 jlong
-java::io::File::attr (jstring canon, jint query)
+java::io::File::attr (jint query)
 {
-  if (! canon)
-    return false;
-
   char buf[MAXPATHLEN];
-  jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   // FIXME?
   buf[total] = '\0';
 
@@ -99,8 +97,6 @@
     return 0;
 
   JvAssert (query == MODIFIED || query == LENGTH);
-  // FIXME: time computation is very POSIX-specific -- POSIX and Java
-  // have the same Epoch.
   return query == MODIFIED ? (jlong)sb.st_mtime * 1000 : sb.st_size;
 #else
   // There's no good choice here.
@@ -118,7 +114,7 @@
 
 #ifdef HAVE_REALPATH
   if (realpath (buf, buf2) == NULL)
-    _Jv_Throw (new IOException (JvNewStringLatin1 (strerror (errno))));
+    throw new IOException (JvNewStringLatin1 (strerror (errno)));
 
   // FIXME: what encoding to assume for file names?  This affects many
   // calls.
@@ -131,23 +127,7 @@
 jboolean
 java::io::File::isAbsolute (void)
 {
-  // FIXME: cpp define name.
-  // FIXME: cygwin.
-#ifdef WIN32
-  if (path->charAt(0) == '/' || path->charAt(0) == '\\')
-    return true;
-  if (path->length() < 3)
-    return false;
-  // Hard-code A-Za-z because Windows (I think) can't use non-ASCII
-  // letters as drive names.
-  if ((path->charAt(0) < 'a' || path->charAt(0) > 'z')
-      && (path->charAt(0) < 'A' || path->charAt(0) > 'Z'))
-    return false;
-  return (path->charAt(1) == ':'
-	  && (path->charAt(2) == '/' || path->charAt(2) == '\\'));
-#else
   return path->charAt(0) == '/';
-#endif
 }
 
 #ifdef HAVE_DIRENT_H
@@ -173,15 +153,14 @@
 #endif /* defined(__JV_POSIX_THREADS__) && defined(HAVE_READDIR_R) */
 #endif /* HAVE_DIRENT_H */
 
-jstringArray
-java::io::File::performList (jstring canon, FilenameFilter *filter)
+jobjectArray
+java::io::File::performList (java::io::FilenameFilter *filter, 
+			     java::io::FileFilter *fileFilter, 
+			     java::lang::Class *result_type)
 {
-  if (! canon)
-    return NULL;
-
 #ifdef HAVE_DIRENT_H
   char buf[MAXPATHLEN];
-  jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   // FIXME?
   buf[total] = '\0';
 
@@ -189,26 +168,37 @@
   if (! dir)
     return NULL;
 
-  java::util::Vector *vec = new java::util::Vector ();
+  java::util::ArrayList *list = new java::util::ArrayList ();
   struct dirent *d, d2;
   while ((d = get_entry (dir, &d2)) != NULL)
     {
-      if (! strcmp (d->d_name, ".") || ! strcmp (d->d_name, ".."))
+      // Omit "." and "..".
+      if (d->d_name[0] == '.'
+	  && (d->d_name[1] == '\0'
+	      || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
 	continue;
 
       jstring name = JvNewStringUTF (d->d_name);
       if (filter && ! filter->accept(this, name))
 	continue;
-
-      vec->addElement(name);
+      
+      if (result_type == &java::io::File::class$)
+        {
+	  java::io::File *file = new java::io::File (this, name);
+	  if (fileFilter && ! fileFilter->accept(file))
+	    continue;
+
+	  list->add(file);
+	}
+      else
+	list->add(name);
     }
 
   closedir (dir);
 
-  jobjectArray ret = JvNewObjectArray (vec->size(), canon->getClass(),
-				       NULL);
-  vec->copyInto(ret);
-  return reinterpret_cast<jstringArray> (ret);
+  jobjectArray ret = JvNewObjectArray (list->size(), result_type, NULL);
+  list->toArray(ret);
+  return ret;
 #else /* HAVE_DIRENT_H */
   return NULL;
 #endif /* HAVE_DIRENT_H */
@@ -230,6 +220,42 @@
 }
 
 jboolean
+java::io::File::performSetReadOnly (void)
+{
+  char buf[MAXPATHLEN];
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
+  // FIXME?
+  buf[total] = '\0';
+
+#ifdef HAVE_STAT && HAVE_CHMOD
+  struct stat sb;
+  if (::stat (buf, &sb))
+    return false;
+
+  if (::chmod(buf, sb.st_mode & 0555))
+    return false;  
+  return true;
+#else
+  return false;
+#endif
+}
+
+static JArray<java::io::File *> *unixroot;
+
+JArray< ::java::io::File *>*
+java::io::File::performListRoots ()
+{
+  if (unixroot == NULL)
+    {
+      ::java::io::File *f = new ::java::io::File (JvNewStringLatin1 ("/"));
+      unixroot = reinterpret_cast <JArray<java::io::File *>*> 
+		   (JvNewObjectArray (1, &java::io::File::class$, f));
+      elements (unixroot) [0] = f;
+    }
+  return unixroot;
+}
+
+jboolean
 java::io::File::performRenameTo (File *dest)
 {
   char buf[MAXPATHLEN];
@@ -248,11 +274,53 @@
 #endif
 }
 
+jboolean
+java::io::File::performSetLastModified (jlong time)
+{
+#ifdef HAVE_UTIME
+  utimbuf tb;
+
+  char buf[MAXPATHLEN];
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
+  // FIXME?
+  buf[total] = '\0';
+  
+  tb.actime = time / 1000;
+  tb.modtime = time / 1000;
+  return ::utime (buf, &tb);
+#else
+  return false;
+#endif
+}
+
+jboolean
+java::io::File::performCreate (void)
+{
+  char buf[MAXPATHLEN];
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
+  // FIXME?
+  buf[total] = '\0';
+
+  int fd = ::open (buf, O_CREAT | O_EXCL, 0644);
+
+  if (fd < 0)
+    {
+      if (errno == EEXIST)
+        return false;
+      throw new IOException (JvNewStringLatin1 (strerror (errno)));
+    }
+  else
+    {
+      ::close (fd);
+      return true;
+    }
+}
+
 jboolean
-java::io::File::performDelete (jstring canon)
+java::io::File::performDelete (void)
 {
   char buf[MAXPATHLEN];
-  jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   // FIXME?
   buf[total] = '\0';
 
@@ -265,4 +333,18 @@
     return ::unlink (buf) == 0;
 #endif // HAVE_UNLINK
   return false;
+}
+
+void
+java::io::File::init_native ()
+{
+  separator = JvNewStringLatin1 ("/");
+  pathSeparator = JvNewStringLatin1 (":");
+  
+  char *tmp = ::getenv("TMPDIR");
+  if (! tmp)
+    tmp = "/tmp";
+  tmpdir = JvNewStringLatin1 (tmp);
+  maxPathLen = MAXPATHLEN;
+  caseSensitive = true;
 }
Index: java/io/natFileWin32.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileWin32.cc,v
retrieving revision 1.1
diff -u -r1.1 natFileWin32.cc
--- natFileWin32.cc	2000/03/15 22:03:19	1.1
+++ natFileWin32.cc	2001/04/01 10:24:45
@@ -1,6 +1,6 @@
-// natFileWin32.cc - Native part of File class.
+// natFileWin32.cc - Native part of File class for Win32.
 
-/* Copyright (C) 1998, 1999  Red Hat, Inc.
+/* Copyright (C) 1998, 1999, 2001  Red Hat, Inc.
 
    This file is part of libgcj.
 
@@ -25,12 +25,10 @@
 #include <java/lang/System.h>
 
 jboolean
-java::io::File::access (jstring canon, jint query)
+java::io::File::access (jint query)
 {
-  if (! canon)
-    return false;
   char buf[MAX_PATH];
-  jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   // FIXME?
   buf[total] = '\0';
 
@@ -48,15 +46,15 @@
 }
 
 jboolean
-java::io::File::stat (jstring canon, jint query)
+java::io::File::stat (jint query)
 {
-  if (! canon)
-    return false;
   char buf[MAX_PATH];
-  jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   // FIXME?
   buf[total] = '\0';
 
+  // FIXME: Need to handle ISHIDDEN query.
+
   JvAssert (query == DIRECTORY || query == ISFILE);
 
   DWORD attributes = GetFileAttributes (buf);
@@ -70,12 +68,10 @@
 }
 
 jlong
-java::io::File::attr (jstring canon, jint query)
+java::io::File::attr (jint query)
 {
-  if (! canon)
-    return false;
   char buf[MAX_PATH];
-  jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   // FIXME?
   buf[total] = '\0';
 
@@ -128,12 +124,12 @@
 }
 
 jstringArray
-java::io::File::performList (jstring canon, FilenameFilter *filter)
+java::io::File::performList (java::io::FilenameFilter *filter, 
+			     java::io::FileFilter *fileFilter, 
+			     java::lang::Class *result_type)
 {
-  if (! canon)
-    return NULL;
   char buf[MAX_PATH];
-  jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+  jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
   // FIXME?
   strcpy(&buf[total], "\\*.*");
 
@@ -142,16 +138,28 @@
   if (handle == INVALID_HANDLE_VALUE)
     return NULL;
 
-  java::util::Vector *vec = new java::util::Vector ();
+  java::util::ArrayList *list = new java::util::ArrayList ();
 
   do
     {
       if (strcmp (data.cFileName, ".") && strcmp (data.cFileName, ".."))
         {
           jstring name = JvNewStringUTF (data.cFileName);
-          if (! filter || (filter && filter->accept(this, name)))
-            vec->addElement (name);
-        }
+
+	  if (filter && ! filter->accept(this, name))
+	    continue;
+
+	  if (result_type == &java::io::File::class$)
+            {
+	      java::io::File *file = new java::io::File (this, name);
+	      if (fileFilter && ! fileFilter->accept(file))
+		continue;
+
+	      list->add(file);
+	    }
+	  else
+	    list->add(name);
+	}
     }
   while (FindNextFile (handle, &data));
 
@@ -160,7 +168,7 @@
 
   FindClose (handle);
 
-  jobjectArray ret = JvNewObjectArray (vec->size(), canon->getClass(), NULL);
+  jobjectArray ret = JvNewObjectArray (vec->size(), path->getClass(), NULL);
   vec->copyInto (ret);
   return reinterpret_cast<jstringArray> (ret);
 }
@@ -177,6 +185,20 @@
 }
 
 jboolean
+java::io::File::performSetReadOnly (void)
+{
+  // PLEASE IMPLEMENT ME
+  return false;
+}
+
+JArray< ::java::io::File *>*
+java::io::File::performListRoots ()
+{
+  // PLEASE IMPLEMENT ME
+  return NULL;
+}
+
+jboolean
 java::io::File::performRenameTo (File *dest)
 {
   char buf[MAX_PATH];
@@ -191,11 +213,25 @@
   return (MoveFile(buf, buf2)) ? true : false;
 }
 
+jboolean
+java::io::File::performSetLastModified (jlong time)
+{
+  // PLEASE IMPLEMENT ME
+  return false;
+}
+
 jboolean
-java::io::File::performDelete (jstring canon)
+java::io::File::performCreate (void)
 {
+  // PLEASE IMPLEMENT ME
+  return false;
+}
+
+jboolean
+java::io::File::performDelete ()
+{
   char buf[MAX_PATH];
-  jsize total = JvGetStringUTFRegion(canon, 0, canon->length(), buf);
+  jsize total = JvGetStringUTFRegion(path, 0, path->length(), buf);
   // FIXME?
   buf[total] = '\0';
 
@@ -207,4 +243,14 @@
     return (RemoveDirectory (buf)) ? true : false;
   else
     return (DeleteFile (buf)) ? true : false;
+}
+
+void
+java::io::File::init_native ()
+{
+  separator = JvNewStringLatin1 ("\\");
+  pathSeparator = JvNewStringLatin1 (";");
+  tmpdir = JvNewStringLatin1 ("C:\\temp"); // FIXME?
+  maxPathLen = MAX_PATH;
+  caseSensitive = false;
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]