< prev index next >

src/solaris/native/sun/nio/fs/LinuxNativeDispatcher.c

Print this page


   1 /*
   2  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 152 }
 153 
 154 JNIEXPORT jlong JNICALL
 155 Java_sun_nio_fs_LinuxNativeDispatcher_setmntent0(JNIEnv* env, jclass this, jlong pathAddress,
 156                                                  jlong modeAddress)
 157 {
 158     FILE* fp = NULL;
 159     const char* path = (const char*)jlong_to_ptr(pathAddress);
 160     const char* mode = (const char*)jlong_to_ptr(modeAddress);
 161 
 162     do {
 163         fp = setmntent(path, mode);
 164     } while (fp == NULL && errno == EINTR);
 165     if (fp == NULL) {
 166         throwUnixException(env, errno);
 167     }
 168     return ptr_to_jlong(fp);
 169 }
 170 
 171 JNIEXPORT jint JNICALL
 172 Java_sun_nio_fs_LinuxNativeDispatcher_getmntent(JNIEnv* env, jclass this,
 173     jlong value, jobject entry)
 174 {
 175     struct mntent ent;
 176     char buf[1024];
 177     int buflen = sizeof(buf);
 178     struct mntent* m;
 179     FILE* fp = jlong_to_ptr(value);
 180     jsize len;
 181     jbyteArray bytes;
 182     char* name;
 183     char* dir;
 184     char* fstype;
 185     char* options;
 186 
 187     m = getmntent_r(fp, &ent, (char*)&buf, buflen);
 188     if (m == NULL)
 189         return -1;
 190     name = m->mnt_fsname;
 191     dir = m->mnt_dir;
 192     fstype = m->mnt_type;
 193     options = m->mnt_opts;
 194 
 195     len = strlen(name);
 196     bytes = (*env)->NewByteArray(env, len);
 197     if (bytes == NULL)
 198         return -1;
 199     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)name);
 200     (*env)->SetObjectField(env, entry, entry_name, bytes);
 201 
 202     len = strlen(dir);
 203     bytes = (*env)->NewByteArray(env, len);
 204     if (bytes == NULL)
 205         return -1;
 206     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)dir);
 207     (*env)->SetObjectField(env, entry, entry_dir, bytes);


 213     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)fstype);
 214     (*env)->SetObjectField(env, entry, entry_fstype, bytes);
 215 
 216     len = strlen(options);
 217     bytes = (*env)->NewByteArray(env, len);
 218     if (bytes == NULL)
 219         return -1;
 220     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)options);
 221     (*env)->SetObjectField(env, entry, entry_options, bytes);
 222 
 223     return 0;
 224 }
 225 
 226 JNIEXPORT void JNICALL
 227 Java_sun_nio_fs_LinuxNativeDispatcher_endmntent(JNIEnv* env, jclass this, jlong stream)
 228 {
 229     FILE* fp = jlong_to_ptr(stream);
 230     /* FIXME - man page doesn't explain how errors are returned */
 231     endmntent(fp);
 232 }
































   1 /*
   2  * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 152 }
 153 
 154 JNIEXPORT jlong JNICALL
 155 Java_sun_nio_fs_LinuxNativeDispatcher_setmntent0(JNIEnv* env, jclass this, jlong pathAddress,
 156                                                  jlong modeAddress)
 157 {
 158     FILE* fp = NULL;
 159     const char* path = (const char*)jlong_to_ptr(pathAddress);
 160     const char* mode = (const char*)jlong_to_ptr(modeAddress);
 161 
 162     do {
 163         fp = setmntent(path, mode);
 164     } while (fp == NULL && errno == EINTR);
 165     if (fp == NULL) {
 166         throwUnixException(env, errno);
 167     }
 168     return ptr_to_jlong(fp);
 169 }
 170 
 171 JNIEXPORT jint JNICALL
 172 Java_sun_nio_fs_LinuxNativeDispatcher_getmntent0(JNIEnv* env, jclass this,
 173     jlong value, jobject entry, jlong buffer, jint bufLen)
 174 {
 175     struct mntent ent;
 176     char * buf = (char*)jlong_to_ptr(buffer);

 177     struct mntent* m;
 178     FILE* fp = jlong_to_ptr(value);
 179     jsize len;
 180     jbyteArray bytes;
 181     char* name;
 182     char* dir;
 183     char* fstype;
 184     char* options;
 185 
 186     m = getmntent_r(fp, &ent, buf, (int)bufLen);
 187     if (m == NULL)
 188         return -1;
 189     name = m->mnt_fsname;
 190     dir = m->mnt_dir;
 191     fstype = m->mnt_type;
 192     options = m->mnt_opts;
 193 
 194     len = strlen(name);
 195     bytes = (*env)->NewByteArray(env, len);
 196     if (bytes == NULL)
 197         return -1;
 198     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)name);
 199     (*env)->SetObjectField(env, entry, entry_name, bytes);
 200 
 201     len = strlen(dir);
 202     bytes = (*env)->NewByteArray(env, len);
 203     if (bytes == NULL)
 204         return -1;
 205     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)dir);
 206     (*env)->SetObjectField(env, entry, entry_dir, bytes);


 212     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)fstype);
 213     (*env)->SetObjectField(env, entry, entry_fstype, bytes);
 214 
 215     len = strlen(options);
 216     bytes = (*env)->NewByteArray(env, len);
 217     if (bytes == NULL)
 218         return -1;
 219     (*env)->SetByteArrayRegion(env, bytes, 0, len, (jbyte*)options);
 220     (*env)->SetObjectField(env, entry, entry_options, bytes);
 221 
 222     return 0;
 223 }
 224 
 225 JNIEXPORT void JNICALL
 226 Java_sun_nio_fs_LinuxNativeDispatcher_endmntent(JNIEnv* env, jclass this, jlong stream)
 227 {
 228     FILE* fp = jlong_to_ptr(stream);
 229     /* FIXME - man page doesn't explain how errors are returned */
 230     endmntent(fp);
 231 }
 232 
 233 /**
 234  * This function returns line length without NUL terminator or -1 on EOF.
 235  */
 236 JNIEXPORT jint JNICALL
 237 Java_sun_nio_fs_LinuxNativeDispatcher_getlinelen(JNIEnv* env, jclass this, jlong stream)
 238 {
 239     FILE* fp = jlong_to_ptr(stream);
 240     size_t lineSize = 0;
 241     char * lineBuffer = NULL;
 242     int saved_errno;
 243 
 244     ssize_t res = getline(&lineBuffer, &lineSize, fp);
 245     saved_errno = errno;
 246 
 247     /* Should free lineBuffer no matter result, according to man page */
 248     if (lineBuffer != NULL)
 249         free(lineBuffer);
 250 
 251     if (feof(fp))
 252         return -1;
 253 
 254     /* On successfull return res >= 0, otherwise res is -1 */
 255     if (res == -1)
 256         throwUnixException(env, saved_errno);
 257 
 258     if (res > INT_MAX)
 259         throwUnixException(env, EOVERFLOW);
 260 
 261     return (jint)res;
 262 }
 263 
< prev index next >