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
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 
  28 /**
  29  * Linux specific system calls.
  30  */
  31 
  32 class LinuxNativeDispatcher extends UnixNativeDispatcher {
  33     private LinuxNativeDispatcher() { }
  34 
  35    /**
  36     * FILE *setmntent(const char *filename, const char *type);
  37     */
  38     static long setmntent(byte[] filename, byte[] type) throws UnixException {
  39         NativeBuffer pathBuffer = NativeBuffers.asNativeBuffer(filename);
  40         NativeBuffer typeBuffer = NativeBuffers.asNativeBuffer(type);
  41         try {
  42             return setmntent0(pathBuffer.address(), typeBuffer.address());
  43         } finally {
  44             typeBuffer.release();
  45             pathBuffer.release();
  46         }
  47     }
  48     private static native long setmntent0(long pathAddress, long typeAddress)
  49         throws UnixException;
  50 
  51     /**
  52      * int getmntent(FILE *fp, struct mnttab *mp, int len);
  53      */
  54 
  55     static int getmntent(long fp, UnixMountEntry entry, int buflen) throws UnixException {
  56         NativeBuffer buffer = NativeBuffers.getNativeBuffer(buflen);
  57         try {
  58             return getmntent0(fp, entry, buffer.address(), buflen);
  59         } finally {
  60             buffer.release();
  61         }
  62     }
  63 
  64     static native int getmntent0(long fp, UnixMountEntry entry, long buffer, int bufLen)
  65         throws UnixException;
  66 
  67     /**
  68      * int endmntent(FILE* filep);
  69      */
  70     static native void endmntent(long stream) throws UnixException;
  71 
  72     /**
  73      * ssize_t fgetxattr(int filedes, const char *name, void *value, size_t size);
  74      */
  75     static int fgetxattr(int filedes, byte[] name, long valueAddress,
  76                          int valueLen) throws UnixException
  77     {
  78         NativeBuffer buffer = NativeBuffers.asNativeBuffer(name);
  79         try {
  80             return fgetxattr0(filedes, buffer.address(), valueAddress, valueLen);
  81         } finally {
  82             buffer.release();
  83         }
  84     }
  85 
  86     private static native int fgetxattr0(int filedes, long nameAddress,
  87         long valueAddress, int valueLen) throws UnixException;
  88 
  89     /**
  90      *  fsetxattr(int filedes, const char *name, const void *value, size_t size, int flags);
  91      */
  92     static void fsetxattr(int filedes, byte[] name, long valueAddress,
  93         int valueLen) throws UnixException
  94     {
  95         NativeBuffer buffer = NativeBuffers.asNativeBuffer(name);
  96         try {
  97             fsetxattr0(filedes, buffer.address(), valueAddress, valueLen);
  98         } finally {
  99             buffer.release();
 100         }
 101     }
 102 
 103     private static native void fsetxattr0(int filedes, long nameAddress,
 104         long valueAddress, int valueLen) throws UnixException;
 105 
 106     /**
 107      * fremovexattr(int filedes, const char *name);
 108      */
 109     static void fremovexattr(int filedes, byte[] name) throws UnixException {
 110         NativeBuffer buffer = NativeBuffers.asNativeBuffer(name);
 111         try {
 112             fremovexattr0(filedes, buffer.address());
 113         } finally {
 114             buffer.release();
 115         }
 116     }
 117 
 118     private static native void fremovexattr0(int filedes, long nameAddress)
 119         throws UnixException;
 120 
 121     /**
 122      * size_t flistxattr(int filedes, const char *list, size_t size)
 123      */
 124     static native int flistxattr(int filedes, long listAddress, int size)
 125         throws UnixException;
 126 
 127     // initialize
 128     private static native void init();
 129 
 130     static {
 131         sun.security.action.LoadLibraryAction.privilegedLoadLibrary("nio");
 132         init();
 133     }
 134 }