Our usage of `readdir' wasn't portable: on Darwin, you get a NULL return
value, but `errno' will be zero. We were expecting errno to be nonzero
in that case.
This had the side effect that we'd keep reallocating the filename array,
because we'd be looping on the same value for `filename', and this would
exhaust memory.
I also took the liberty of adding a check for readdir_r, and use it in
cpio_readDir if it is available.
2006-09-22 Casey Marshall <[EMAIL PROTECTED]>
* configure.ac (AC_CHECK_FUNCS): check for `readdir_r.'
* native/jni/java-io/java_io_VMFile.c (Java_java_io_VMFile_list):
allocate `filename,' and handle changes to `cpio_readDir.'
* native/jni/native-lib/cpio.c (cpio_readDir): use `readdir_r' if
available; copy the filename into the destination buffer; return
an error code if readdir returns NULL, but errno is 0.
* native/jni/native-lib/cpio.h (cpio_readDir): change second
parameter to `const char *.'
Committed.
### Eclipse Workspace Patch 1.0
#P classpath
Index: native/jni/java-io/java_io_VMFile.c
===================================================================
RCS file: /cvsroot/classpath/classpath/native/jni/java-io/java_io_VMFile.c,v
retrieving revision 1.12
diff -u -r1.12 java_io_VMFile.c
--- native/jni/java-io/java_io_VMFile.c 21 Aug 2006 23:34:45 -0000 1.12
+++ native/jni/java-io/java_io_VMFile.c 23 Sep 2006 01:38:51 -0000
@@ -593,7 +593,7 @@
int result;
char **filelist;
void *handle;
- const char *filename;
+ const char *filename = (const char *) JCL_malloc (env, FILENAME_MAX);
unsigned long int filelist_count, max_filelist_count;
char **tmp_filelist;
jclass str_clazz;
@@ -630,7 +630,7 @@
max_filelist_count = REALLOC_SIZE;
/* read the files from the directory */
- result = cpio_readDir (handle, &filename);
+ result = cpio_readDir (handle, filename);
while (result == CPNATIVE_OK)
{
if ((strcmp (filename, ".") != 0) && (strcmp (filename, "..") != 0))
@@ -666,9 +666,11 @@
}
/* read next directory entry */
- result = cpio_readDir (handle, &filename);
+ result = cpio_readDir (handle, filename);
}
+ JCL_free (env, filename);
+
/* close directory */
result = cpio_closeDir (handle);
@@ -693,6 +695,9 @@
JCL_free (env, filelist);
return 0;
}
+
+ (*env)->DeleteLocalRef (env, str_clazz);
+
for (i = 0; i < filelist_count; i++)
{
/* create new string */
Index: native/jni/native-lib/cpio.h
===================================================================
RCS file: /cvsroot/classpath/classpath/native/jni/native-lib/cpio.h,v
retrieving revision 1.2
diff -u -r1.2 cpio.h
--- native/jni/native-lib/cpio.h 21 Aug 2006 23:34:46 -0000 1.2
+++ native/jni/native-lib/cpio.h 23 Sep 2006 01:38:51 -0000
@@ -79,6 +79,6 @@
JNIEXPORT int cpio_openDir (const char *dirname, void **handle);
JNIEXPORT int cpio_closeDir (void *handle);
-JNIEXPORT int cpio_readDir (void *handle, const char **filename);
+JNIEXPORT int cpio_readDir (void *handle, const char *filename);
#endif
Index: native/jni/native-lib/cpio.c
===================================================================
RCS file: /cvsroot/classpath/classpath/native/jni/native-lib/cpio.c,v
retrieving revision 1.3
diff -u -r1.3 cpio.c
--- native/jni/native-lib/cpio.c 31 Aug 2006 19:56:03 -0000 1.3
+++ native/jni/native-lib/cpio.c 23 Sep 2006 01:38:51 -0000
@@ -448,14 +448,28 @@
}
-int cpio_readDir (void *handle, const char **filename)
+int cpio_readDir (void *handle, const char *filename)
{
+#ifdef HAVE_READDIR_R
+ struct dirent dent;
+#endif /* HAVE_READDIR_R */
struct dirent *dBuf;
+#ifdef HAVE_READDIR_R
+ readdir_r ((DIR *) handle, &dent, &dBuf);
+#else
dBuf = readdir((DIR *)handle);
+#endif /* HAVE_READDIR_R */
+
if (dBuf == NULL)
- return errno;
+ {
+ /* Some OS's (OS X) return NULL on end-of-dir, but
+ don't set errno to anything. */
+ if (errno == 0)
+ return ENOENT; /* Whatever. */
+ return errno;
+ }
- *filename = dBuf->d_name;
+ strncpy (filename, dBuf->d_name, FILENAME_MAX);
return 0;
}
Index: configure.ac
===================================================================
RCS file: /cvsroot/classpath/classpath/configure.ac,v
retrieving revision 1.184
diff -u -r1.184 configure.ac
--- configure.ac 20 Sep 2006 21:39:41 -0000 1.184
+++ configure.ac 23 Sep 2006 01:38:51 -0000
@@ -375,7 +375,8 @@
mmap munmap mincore msync madvise getpagesize sysconf \
lstat readlink \
inet_aton inet_addr inet_pton \
- getifaddrs kqueue kevent epoll_create])
+ getifaddrs kqueue kevent epoll_create \
+ readdir_r ])
LIBMAGIC=
AC_CHECK_LIB(magic, magic_open, LIBMAGIC=-lmagic)