Commit 1e19cfd2 by Gary Benson Committed by Gary Benson

natFilePosix.cc (getCanonicalPath): Rewritten.


	* java/io/natFilePosix.cc (getCanonicalPath): Rewritten.
	* configure.ac: Remove realpath check and add checks for
	lstat and readlink.
	* configure: Rebuilt.
	* include/config.h.in: Likewise.
	* java/io/File.java: Javadoc fix.

From-SVN: r114434
parent 406af897
2006-06-06 Gary Benson <gbenson@redhat.com>
* java/io/natFilePosix.cc (getCanonicalPath): Rewritten.
* configure.ac: Remove realpath check and add checks for
lstat and readlink.
* configure: Rebuilt.
* include/config.h.in: Likewise.
* java/io/File.java: Javadoc fix.
2006-06-05 Andreas Schwab <schwab@suse.de> 2006-06-05 Andreas Schwab <schwab@suse.de>
PR libgcj/27860 PR libgcj/27860
......
...@@ -10396,7 +10396,7 @@ else ...@@ -10396,7 +10396,7 @@ else
for ac_func in strerror ioctl select fstat open fsync sleep opendir \ for ac_func in strerror ioctl select fstat open fsync sleep opendir \
gmtime_r localtime_r readdir_r getpwuid_r getcwd \ gmtime_r localtime_r readdir_r getpwuid_r getcwd \
access stat mkdir rename rmdir unlink realpath utime chmod \ access stat lstat mkdir rename rmdir unlink utime chmod readlink \
nl_langinfo setlocale \ nl_langinfo setlocale \
inet_pton uname inet_ntoa \ inet_pton uname inet_ntoa \
fork execvp pipe sigaction ftruncate mmap fork execvp pipe sigaction ftruncate mmap
......
...@@ -897,7 +897,7 @@ if test "x${with_newlib}" = "xyes"; then ...@@ -897,7 +897,7 @@ if test "x${with_newlib}" = "xyes"; then
else else
AC_CHECK_FUNCS([strerror ioctl select fstat open fsync sleep opendir \ AC_CHECK_FUNCS([strerror ioctl select fstat open fsync sleep opendir \
gmtime_r localtime_r readdir_r getpwuid_r getcwd \ gmtime_r localtime_r readdir_r getpwuid_r getcwd \
access stat mkdir rename rmdir unlink realpath utime chmod \ access stat lstat mkdir rename rmdir unlink utime chmod readlink \
nl_langinfo setlocale \ nl_langinfo setlocale \
inet_pton uname inet_ntoa \ inet_pton uname inet_ntoa \
fork execvp pipe sigaction ftruncate mmap]) fork execvp pipe sigaction ftruncate mmap])
......
...@@ -169,6 +169,9 @@ ...@@ -169,6 +169,9 @@
/* Define to 1 if you have the `localtime_r' function. */ /* Define to 1 if you have the `localtime_r' function. */
#undef HAVE_LOCALTIME_R #undef HAVE_LOCALTIME_R
/* Define to 1 if you have the `lstat' function. */
#undef HAVE_LSTAT
/* Define to 1 if you have the `memcpy' function. */ /* Define to 1 if you have the `memcpy' function. */
#undef HAVE_MEMCPY #undef HAVE_MEMCPY
...@@ -226,8 +229,8 @@ ...@@ -226,8 +229,8 @@
/* Define to 1 if you have the `readdir_r' function. */ /* Define to 1 if you have the `readdir_r' function. */
#undef HAVE_READDIR_R #undef HAVE_READDIR_R
/* Define to 1 if you have the `realpath' function. */ /* Define to 1 if you have the `readlink' function. */
#undef HAVE_REALPATH #undef HAVE_READLINK
/* Define to 1 if you have the `rename' function. */ /* Define to 1 if you have the `rename' function. */
#undef HAVE_RENAME #undef HAVE_RENAME
......
...@@ -508,9 +508,9 @@ public class File implements Serializable, Comparable ...@@ -508,9 +508,9 @@ public class File implements Serializable, Comparable
/** /**
* This method returns a canonical representation of the pathname of * This method returns a canonical representation of the pathname of
* this file. The actual form of the canonical representation is * this file. The actual form of the canonical representation is
* different. On the GNU system, the canonical form differs from the * system-dependent. On the GNU system, conversion to canonical
* absolute form in that all relative file references to "." and ".." * form involves the removal of redundant separators, references to
* are resolved and removed. * "." and "..", and symbolic links.
* <p> * <p>
* Note that this method, unlike the other methods which return path * Note that this method, unlike the other methods which return path
* names, can throw an IOException. This is because native method * names, can throw an IOException. This is because native method
......
...@@ -101,94 +101,167 @@ java::io::File::attr (jint query) ...@@ -101,94 +101,167 @@ java::io::File::attr (jint query)
#endif #endif
} }
// These two methods are used to maintain dynamically allocated
// buffers for getCanonicalPath without the overhead of calling
// realloc every time a buffer is modified. Buffers are sized
// at the smallest multiple of CHUNKSIZ that is greater than or
// equal to the desired length. The default CHUNKSIZ is 256,
// longer than most paths, so in most cases a getCanonicalPath
// will require only one malloc per buffer.
#define CHUNKLOG 8
#define CHUNKSIZ (1 << CHUNKLOG)
static int
nextChunkSize (int size)
{
return ((size >> CHUNKLOG) + ((size & (CHUNKSIZ - 1)) ? 1 : 0)) << CHUNKLOG;
}
static char *
maybeGrowBuf (char *buf, int *size, int required)
{
if (required > *size)
{
*size = nextChunkSize (required);
buf = (char *) _Jv_Realloc (buf, *size);
}
return buf;
}
// Return a canonical representation of the pathname of this file. On
// the GNU system this involves the removal of redundant separators,
// references to "." and "..", and symbolic links.
//
// The conversion proceeds on a component-by-component basis: symbolic
// links and references to ".." are resolved as and when they occur.
// This means that if "/foo/bar" is a symbolic link to "/baz" then the
// canonical form of "/foo/bar/.." is "/" and not "/foo".
//
// In order to mimic the behaviour of proprietary JVMs, non-existant
// path components are allowed (a departure from the normal GNU system
// convention). This means that if "/foo/bar" is a symbolic link to
// "/baz", the canonical form of "/non-existant-directory/../foo/bar"
// is "/baz".
jstring jstring
java::io::File::getCanonicalPath (void) java::io::File::getCanonicalPath (void)
{ {
// We use `+2' here because we might need to use `.' for our special jstring path = getAbsolutePath ();
// case.
char *buf = (char *) __builtin_alloca (JvGetStringUTFLength (path) + 2);
char buf2[MAXPATHLEN];
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
// Special case: treat "" the same as ".". int len = JvGetStringUTFLength (path);
if (total == 0) int srcl = nextChunkSize (len + 1);
buf[total++] = '.'; char *src = (char *) _Jv_Malloc (srcl);
JvGetStringUTFRegion (path, 0, path->length(), src);
src[len] = '\0';
int srci = 1;
buf[total] = '\0'; int dstl = nextChunkSize (2);
char *dst = (char *) _Jv_Malloc (dstl);
dst[0] = '/';
int dsti = 1;
bool fschecks = true;
#ifdef HAVE_REALPATH while (src[srci] != '\0')
if (realpath (buf, buf2) == NULL)
{ {
// If realpath failed, we have to come up with a canonical path // Skip slashes.
// anyway. We do this with purely textual manipulation. while (src[srci] == '/')
// FIXME: this isn't perfect. You can construct a case where srci++;
// we get a different answer from the JDK: int tmpi = srci;
// mkdir -p /tmp/a/b/c // Find next slash.
// ln -s /tmp/a/b /tmp/a/z while (src[srci] != '/' && src[srci] != '\0')
// ... getCanonicalPath("/tmp/a/z/c/nosuchfile") srci++;
// We will give /tmp/a/z/c/nosuchfile, while the JDK will if (srci == tmpi)
// give /tmp/a/b/c/nosuchfile. // We hit the end.
int out_idx; break;
if (buf[0] != '/') len = srci - tmpi;
// Handle "." and "..".
if (len == 1 && src[tmpi] == '.')
continue;
if (len == 2 && src[tmpi] == '.' && src[tmpi + 1] == '.')
{ {
// Not absolute, so start with current directory. while (dsti > 1 && dst[dsti - 1] != '/')
if (getcwd (buf2, sizeof (buf2)) == NULL) dsti--;
throw new IOException (); if (dsti != 1)
out_idx = strlen (buf2); dsti--;
// Reenable filesystem checking if disabled, as we might
// have reversed over whatever caused the problem before.
// At least one proprietary JVM has inconsistencies because
// it does not do this.
fschecks = true;
continue;
} }
else
{ // Handle real path components.
buf2[0] = '/'; dst = maybeGrowBuf (dst, &dstl, dsti + (dsti > 1 ? 1 : 0) + len + 1);
out_idx = 1; int dsti_save = dsti;
} if (dsti > 1)
int in_idx = 0; dst[dsti++] = '/';
while (buf[in_idx] != '\0') strncpy (&dst[dsti], &src[tmpi], len);
dsti += len;
if (fschecks == false)
continue;
#if defined (HAVE_LSTAT) && defined (HAVE_READLINK)
struct stat sb;
dst[dsti] = '\0';
if (::lstat (dst, &sb) == 0)
{ {
// Skip '/'s. if (S_ISLNK (sb.st_mode))
while (buf[in_idx] == '/')
++in_idx;
int elt_start = in_idx;
// Find next '/' or end of path.
while (buf[in_idx] != '\0' && buf[in_idx] != '/')
++in_idx;
if (in_idx == elt_start)
{
// An empty component means we've reached the end.
break;
}
int len = in_idx - elt_start;
if (len == 1 && buf[in_idx] == '.')
continue;
if (len == 2 && buf[in_idx] == '.' && buf[in_idx + 1] == '.')
{
// Found ".." component, lop off last part from existing
// buffer.
--out_idx;
while (out_idx > 0 && buf2[out_idx] != '/')
--out_idx;
// Can't go up past "/".
if (out_idx == 0)
++out_idx;
}
else
{ {
// Append a real path component to the output. int tmpl = CHUNKSIZ;
if (out_idx > 1) char *tmp = (char *) _Jv_Malloc (tmpl);
buf2[out_idx++] = '/';
strncpy (&buf2[out_idx], &buf[elt_start], len); while (1)
out_idx += len; {
tmpi = ::readlink (dst, tmp, tmpl);
if (tmpi < 1)
{
_Jv_Free (src);
_Jv_Free (dst);
_Jv_Free (tmp);
throw new IOException (
JvNewStringLatin1 ("readlink failed"));
}
if (tmpi < tmpl)
break;
tmpl += CHUNKSIZ;
tmp = (char *) _Jv_Realloc (tmp, tmpl);
}
// Prepend the link's path to src.
tmp = maybeGrowBuf (tmp, &tmpl, tmpi + strlen (&src[srci]) + 1);
strcpy(&tmp[tmpi], &src[srci]);
_Jv_Free (src);
src = tmp;
srcl = tmpl;
srci = 0;
// Either replace or append dst depending on whether the
// link is relative or absolute.
dsti = src[0] == '/' ? 1 : dsti_save;
} }
} }
else
buf2[out_idx] = '\0'; {
// Something doesn't exist, or we don't have permission to
// read it, or a previous path component is a directory, or
// a symlink is looped. Whatever, we can't check the
// filesystem any more.
fschecks = false;
}
#endif // HAVE_LSTAT && HAVE_READLINK
} }
dst[dsti] = '\0';
// FIXME: what encoding to assume for file names? This affects many // FIXME: what encoding to assume for file names? This affects many
// calls. // calls.
return JvNewStringUTF (buf2); path = JvNewStringUTF (dst);
#else _Jv_Free (src);
return JvNewStringUTF (buf); _Jv_Free (dst);
#endif return path;
} }
jboolean jboolean
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment