提交 af1ce33a 编写于 作者: V vkempik

8229872: (fs) Increase buffer size used with getmntent

Summary: Dynamically allocate memory for getmntent
Reviewed-by: yan
上级 aa251cfb
...@@ -153,8 +153,9 @@ SUNWprivate_1.1 { ...@@ -153,8 +153,9 @@ SUNWprivate_1.1 {
Java_sun_nio_fs_LinuxNativeDispatcher_fsetxattr0; Java_sun_nio_fs_LinuxNativeDispatcher_fsetxattr0;
Java_sun_nio_fs_LinuxNativeDispatcher_fremovexattr0; Java_sun_nio_fs_LinuxNativeDispatcher_fremovexattr0;
Java_sun_nio_fs_LinuxNativeDispatcher_setmntent0; Java_sun_nio_fs_LinuxNativeDispatcher_setmntent0;
Java_sun_nio_fs_LinuxNativeDispatcher_getmntent; Java_sun_nio_fs_LinuxNativeDispatcher_getmntent0;
Java_sun_nio_fs_LinuxNativeDispatcher_endmntent; Java_sun_nio_fs_LinuxNativeDispatcher_endmntent;
Java_sun_nio_fs_LinuxNativeDispatcher_getlinelen;
Java_sun_nio_fs_UnixNativeDispatcher_init; Java_sun_nio_fs_UnixNativeDispatcher_init;
Java_sun_nio_fs_UnixNativeDispatcher_getcwd; Java_sun_nio_fs_UnixNativeDispatcher_getcwd;
Java_sun_nio_fs_UnixNativeDispatcher_strerror; Java_sun_nio_fs_UnixNativeDispatcher_strerror;
...@@ -176,6 +177,7 @@ SUNWprivate_1.1 { ...@@ -176,6 +177,7 @@ SUNWprivate_1.1 {
Java_sun_nio_fs_UnixNativeDispatcher_close; Java_sun_nio_fs_UnixNativeDispatcher_close;
Java_sun_nio_fs_UnixNativeDispatcher_read; Java_sun_nio_fs_UnixNativeDispatcher_read;
Java_sun_nio_fs_UnixNativeDispatcher_write; Java_sun_nio_fs_UnixNativeDispatcher_write;
Java_sun_nio_fs_UnixNativeDispatcher_rewind;
Java_sun_nio_fs_UnixNativeDispatcher_fopen0; Java_sun_nio_fs_UnixNativeDispatcher_fopen0;
Java_sun_nio_fs_UnixNativeDispatcher_fclose; Java_sun_nio_fs_UnixNativeDispatcher_fclose;
Java_sun_nio_fs_UnixNativeDispatcher_opendir0; Java_sun_nio_fs_UnixNativeDispatcher_opendir0;
......
/* /*
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -79,10 +79,26 @@ class LinuxFileSystem extends UnixFileSystem { ...@@ -79,10 +79,26 @@ class LinuxFileSystem extends UnixFileSystem {
ArrayList<UnixMountEntry> entries = new ArrayList<>(); ArrayList<UnixMountEntry> entries = new ArrayList<>();
try { try {
long fp = setmntent(Util.toBytes(fstab), Util.toBytes("r")); long fp = setmntent(Util.toBytes(fstab), Util.toBytes("r"));
int maxLineSize = 1024;
try {
for (;;) {
int lineSize = getlinelen(fp);
if (lineSize == -1)
break;
if (lineSize > maxLineSize)
maxLineSize = lineSize;
}
} catch (UnixException x) {
// nothing we need to do
} finally {
rewind(fp);
}
try { try {
for (;;) { for (;;) {
UnixMountEntry entry = new UnixMountEntry(); UnixMountEntry entry = new UnixMountEntry();
int res = getmntent(fp, entry); // count in NUL character at the end
int res = getmntent(fp, entry, maxLineSize + 1);
if (res < 0) if (res < 0)
break; break;
entries.add(entry); entries.add(entry);
......
/* /*
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -54,7 +54,17 @@ class LinuxNativeDispatcher extends UnixNativeDispatcher { ...@@ -54,7 +54,17 @@ class LinuxNativeDispatcher extends UnixNativeDispatcher {
/** /**
* int getmntent(FILE *fp, struct mnttab *mp, int len); * int getmntent(FILE *fp, struct mnttab *mp, int len);
*/ */
static native int getmntent(long fp, UnixMountEntry entry)
static int getmntent(long fp, UnixMountEntry entry, int buflen) throws UnixException {
NativeBuffer buffer = NativeBuffers.getNativeBuffer(buflen);
try {
return getmntent0(fp, entry, buffer.address(), buflen);
} finally {
buffer.release();
}
}
static native int getmntent0(long fp, UnixMountEntry entry, long buffer, int bufLen)
throws UnixException; throws UnixException;
/** /**
...@@ -62,6 +72,11 @@ class LinuxNativeDispatcher extends UnixNativeDispatcher { ...@@ -62,6 +72,11 @@ class LinuxNativeDispatcher extends UnixNativeDispatcher {
*/ */
static native void endmntent(long stream) throws UnixException; static native void endmntent(long stream) throws UnixException;
/**
* ssize_t getline(char **lineptr, size_t *n, FILE *stream);
*/
static native int getlinelen(long stream) throws UnixException;
/** /**
* ssize_t fgetxattr(int filedes, const char *name, void *value, size_t size); * ssize_t fgetxattr(int filedes, const char *name, void *value, size_t size);
*/ */
......
/* /*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -116,6 +116,11 @@ class UnixNativeDispatcher { ...@@ -116,6 +116,11 @@ class UnixNativeDispatcher {
*/ */
static native void fclose(long stream) throws UnixException; static native void fclose(long stream) throws UnixException;
/**
* void rewind(FILE* stream);
*/
static native void rewind(long stream) throws UnixException;
/** /**
* link(const char* existing, const char* new) * link(const char* existing, const char* new)
*/ */
......
/* /*
* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -169,12 +169,11 @@ Java_sun_nio_fs_LinuxNativeDispatcher_setmntent0(JNIEnv* env, jclass this, jlong ...@@ -169,12 +169,11 @@ Java_sun_nio_fs_LinuxNativeDispatcher_setmntent0(JNIEnv* env, jclass this, jlong
} }
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_sun_nio_fs_LinuxNativeDispatcher_getmntent(JNIEnv* env, jclass this, Java_sun_nio_fs_LinuxNativeDispatcher_getmntent0(JNIEnv* env, jclass this,
jlong value, jobject entry) jlong value, jobject entry, jlong buffer, jint bufLen)
{ {
struct mntent ent; struct mntent ent;
char buf[1024]; char * buf = (char*)jlong_to_ptr(buffer);
int buflen = sizeof(buf);
struct mntent* m; struct mntent* m;
FILE* fp = jlong_to_ptr(value); FILE* fp = jlong_to_ptr(value);
jsize len; jsize len;
...@@ -184,7 +183,7 @@ Java_sun_nio_fs_LinuxNativeDispatcher_getmntent(JNIEnv* env, jclass this, ...@@ -184,7 +183,7 @@ Java_sun_nio_fs_LinuxNativeDispatcher_getmntent(JNIEnv* env, jclass this,
char* fstype; char* fstype;
char* options; char* options;
m = getmntent_r(fp, &ent, (char*)&buf, buflen); m = getmntent_r(fp, &ent, buf, (int)bufLen);
if (m == NULL) if (m == NULL)
return -1; return -1;
name = m->mnt_fsname; name = m->mnt_fsname;
...@@ -230,3 +229,37 @@ Java_sun_nio_fs_LinuxNativeDispatcher_endmntent(JNIEnv* env, jclass this, jlong ...@@ -230,3 +229,37 @@ Java_sun_nio_fs_LinuxNativeDispatcher_endmntent(JNIEnv* env, jclass this, jlong
/* FIXME - man page doesn't explain how errors are returned */ /* FIXME - man page doesn't explain how errors are returned */
endmntent(fp); endmntent(fp);
} }
/**
* This function returns line length without NUL terminator or -1 on EOF.
* Since getline is missing on Solaris10 this function was moved from
* UnixNativeDispatcher to LinuxNativeDispatcher as part of backport form jdk11.
*/
JNIEXPORT jint JNICALL
Java_sun_nio_fs_LinuxNativeDispatcher_getlinelen(JNIEnv* env, jclass this, jlong stream)
{
FILE* fp = jlong_to_ptr(stream);
size_t lineSize = 0;
char * lineBuffer = NULL;
int saved_errno;
ssize_t res = getline(&lineBuffer, &lineSize, fp);
saved_errno = errno;
/* Should free lineBuffer no matter result, according to man page */
if (lineBuffer != NULL)
free(lineBuffer);
if (feof(fp))
return -1;
/* On successfull return res >= 0, otherwise res is -1 */
if (res == -1)
throwUnixException(env, saved_errno);
if (res > INT_MAX)
throwUnixException(env, EOVERFLOW);
return (jint)res;
}
/* /*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -367,6 +367,20 @@ Java_sun_nio_fs_UnixNativeDispatcher_fclose(JNIEnv* env, jclass this, jlong stre ...@@ -367,6 +367,20 @@ Java_sun_nio_fs_UnixNativeDispatcher_fclose(JNIEnv* env, jclass this, jlong stre
} }
} }
JNIEXPORT void JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_rewind(JNIEnv* env, jclass this, jlong stream)
{
FILE* fp = jlong_to_ptr(stream);
int saved_errno;
errno = 0;
rewind(fp);
saved_errno = errno;
if (ferror(fp)) {
throwUnixException(env, saved_errno);
}
}
JNIEXPORT jint JNICALL JNIEXPORT jint JNICALL
Java_sun_nio_fs_UnixNativeDispatcher_open0(JNIEnv* env, jclass this, Java_sun_nio_fs_UnixNativeDispatcher_open0(JNIEnv* env, jclass this,
jlong pathAddress, jint oflags, jint mode) jlong pathAddress, jint oflags, jint mode)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册