8234185: Cleanup usage of canonicalize function between libjava, hotspot and libinstrument

Reviewed-by: dholmes, alanb, sspitsyn
This commit is contained in:
Christoph Langer 2019-12-06 14:13:10 +01:00
parent b8dbdd1232
commit 547e472c42
9 changed files with 54 additions and 81 deletions

View file

@ -45,6 +45,7 @@
#include "jni.h"
#include "jni_util.h"
#include "jlong.h"
#include "jdk_util.h"
#include "io_util.h"
#include "io_util_md.h"
#include "java_io_FileSystem.h"
@ -91,8 +92,6 @@ Java_java_io_UnixFileSystem_initIDs(JNIEnv *env, jclass cls)
/* -- Path operations -- */
extern int canonicalize(char *path, const char *out, int len);
JNIEXPORT jstring JNICALL
Java_java_io_UnixFileSystem_canonicalize0(JNIEnv *env, jobject this,
jstring pathname)
@ -101,7 +100,7 @@ Java_java_io_UnixFileSystem_canonicalize0(JNIEnv *env, jobject this,
WITH_PLATFORM_STRING(env, pathname, path) {
char canonicalPath[PATH_MAX];
if (canonicalize((char *)path,
if (JDK_Canonicalize((char *)path,
canonicalPath, PATH_MAX) < 0) {
JNU_ThrowIOExceptionWithLastError(env, "Bad pathname");
} else {

View file

@ -37,6 +37,7 @@
#include <alloca.h>
#endif
#include "jdk_util.h"
/* Note: The comments in this file use the terminology
defined in the java.io.File class */
@ -186,33 +187,32 @@ collapse(char *path)
work, though once that's done we still must collapse any remaining "." and
".." names by hand. */
int
canonicalize(char *original, char *resolved, int len)
JNIEXPORT int
JDK_Canonicalize(const char *orig, char *out, int len)
{
if (len < PATH_MAX) {
errno = EINVAL;
return -1;
}
if (strlen(original) > PATH_MAX) {
if (strlen(orig) > PATH_MAX) {
errno = ENAMETOOLONG;
return -1;
}
/* First try realpath() on the entire path */
if (realpath(original, resolved)) {
if (realpath(orig, out)) {
/* That worked, so return it */
collapse(resolved);
collapse(out);
return 0;
}
else {
} else {
/* Something's bogus in the original path, so remove names from the end
until either some subpath works or we run out of names */
char *p, *end, *r = NULL;
char path[PATH_MAX + 1];
// strlen(original) <= PATH_MAX, see above
strncpy(path, original, PATH_MAX);
// strlen(orig) <= PATH_MAX, see above
strncpy(path, orig, PATH_MAX);
// append null for == case
path[PATH_MAX] = '\0';
end = path + strlen(path);
@ -225,21 +225,19 @@ canonicalize(char *original, char *resolved, int len)
/* Try realpath() on this subpath */
*p = '\0';
r = realpath(path, resolved);
r = realpath(path, out);
*p = (p == end) ? '\0' : '/';
if (r != NULL) {
/* The subpath has a canonical path */
break;
}
else if (errno == ENOENT || errno == ENOTDIR || errno == EACCES) {
} else if (errno == ENOENT || errno == ENOTDIR || errno == EACCES) {
/* If the lookup of a particular subpath fails because the file
does not exist, because it is of the wrong type, or because
access is denied, then remove its last name and try again.
Other I/O problems cause an error return. */
continue;
}
else {
} else {
return -1;
}
}
@ -259,13 +257,11 @@ canonicalize(char *original, char *resolved, int len)
strcpy(r + rn, p);
collapse(r);
return 0;
}
else {
} else {
/* Nothing resolved, so just return the original path */
strcpy(resolved, path);
collapse(resolved);
strcpy(out, path);
collapse(out);
return 0;
}
}
}