1 /*
   2  * Copyright (c) 2001, 2010, 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
  23  * questions.
  24  */
  25 
  26 #include "jni.h"
  27 #include "jni_util.h"
  28 #include "jvm.h"
  29 #include "io_util.h"
  30 #include "io_util_md.h"
  31 #include <string.h>
  32 
  33 #ifdef MACOSX
  34 
  35 #include <CoreFoundation/CoreFoundation.h>
  36 
  37 static inline char *convertToNFD(const char *path, char *buf, size_t bufsize)
  38 {
  39     CFMutableStringRef mutable = CFStringCreateMutable(NULL, 0);
  40     CFStringAppendCString(mutable, path, kCFStringEncodingUTF8);
  41     CFStringNormalize(mutable, kCFStringNormalizationFormD);
  42 
  43     CFStringGetCString(mutable, buf, bufsize, kCFStringEncodingUTF8);
  44 
  45     CFRelease(mutable);
  46     return buf;
  47 }
  48 
  49 /* Converts the path to NFD form if it was in NFC form. Returns a pointer to
  50  * the converting string which could be buf (if the converstion took place) or
  51  * origPath if no conversion was needed
  52  */
  53 __private_extern__
  54 char* convertToNFDIfNeeded(const char *origPath, char *buf, size_t bufsize)
  55 {
  56     const char *current = origPath;
  57     int c;
  58     for (c = *current; c != 0; current++, c = *current) {
  59         if (c < 0) {
  60             // Need to convert
  61             return convertToNFD(origPath, buf, bufsize);
  62         }
  63     }
  64 
  65     return (char *)origPath;
  66 }
  67 
  68 #endif
  69 
  70 void
  71 fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
  72 {
  73     WITH_PLATFORM_STRING(env, path, ps) {
  74         FD fd;
  75 
  76 #if defined(__linux__) || defined(_ALLBSD_SOURCE)
  77         /* Remove trailing slashes, since the kernel won't */
  78         char *p = (char *)ps + strlen(ps) - 1;
  79         while ((p > ps) && (*p == '/'))
  80             *p-- = '\0';
  81 #endif
  82         fd = JVM_Open(ps, flags, 0666);
  83         if (fd >= 0) {
  84             SET_FD(this, fd, fid);
  85         } else {
  86             throwFileNotFoundException(env, path);
  87         }
  88     } END_PLATFORM_STRING(env, ps);
  89 }
  90 
  91 
  92 void
  93 fileClose(JNIEnv *env, jobject this, jfieldID fid)
  94 {
  95     FD fd = GET_FD(this, fid);
  96     if (fd == -1) {
  97         return;
  98     }
  99 
 100     /* Set the fd to -1 before closing it so that the timing window
 101      * of other threads using the wrong fd (closed but recycled fd,
 102      * that gets re-opened with some other filename) is reduced.
 103      * Practically the chance of its occurance is low, however, we are
 104      * taking extra precaution over here.
 105      */
 106     SET_FD(this, -1, fid);
 107 
 108     /*
 109      * Don't close file descriptors 0, 1, or 2. If we close these stream
 110      * then a subsequent file open or socket will use them. Instead we
 111      * just redirect these file descriptors to /dev/null.
 112      */
 113     if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
 114         int devnull = open("/dev/null", O_WRONLY);
 115         if (devnull < 0) {
 116             SET_FD(this, fd, fid); // restore fd
 117             JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
 118         } else {
 119             dup2(devnull, fd);
 120             close(devnull);
 121         }
 122     } else if (JVM_Close(fd) == -1) {
 123         JNU_ThrowIOExceptionWithLastError(env, "close failed");
 124     }
 125 }