< prev index next >

src/java.base/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java

Print this page


   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 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import jdk.internal.misc.Unsafe;
  31 
  32 import static sun.nio.fs.WindowsConstants.*;
  33 
  34 /**
  35  * Win32 and library calls.
  36  */
  37 
  38 class WindowsNativeDispatcher {
  39     private WindowsNativeDispatcher() { }
  40 
  41     /**
  42      * HANDLE CreateEvent(
  43      *   LPSECURITY_ATTRIBUTES lpEventAttributes,
  44      *   BOOL bManualReset,
  45      *   BOOL bInitialState,
  46      *   PCTSTR lpName
  47      * );
  48      */
  49     static native long CreateEvent(boolean bManualReset, boolean bInitialState)
  50         throws WindowsException;
  51 
  52     /**
  53      * HANDLE CreateFile(


 905 
 906     /**
 907      */
 908     static long LookupPrivilegeValue(String name) throws WindowsException {
 909         NativeBuffer buffer = asNativeBuffer(name);
 910         try {
 911             return LookupPrivilegeValue0(buffer.address());
 912         } finally {
 913             buffer.release();
 914         }
 915     }
 916     private static native long LookupPrivilegeValue0(long lpName)
 917         throws WindowsException;
 918 
 919     /**
 920      * CreateSymbolicLink(
 921      *   LPCWSTR lpSymlinkFileName,
 922      *   LPCWSTR lpTargetFileName,
 923      *   DWORD dwFlags
 924      * )
 925      *
 926      * Creates a symbolic link, conditionally retrying with the addition of
 927      * the flag SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE if the initial
 928      * attempt fails with ERROR_PRIVILEGE_NOT_HELD. If the retry fails, throw
 929      * the original exception due to ERROR_PRIVILEGE_NOT_HELD. The retry will
 930      * succeed only on Windows build 14972 or later if Developer Mode is on.
 931      */
 932     static void CreateSymbolicLink(String link, String target, int flags)
 933         throws WindowsException
 934     {
 935         NativeBuffer linkBuffer = asNativeBuffer(link);
 936         NativeBuffer targetBuffer = asNativeBuffer(target);
 937         try {
 938             CreateSymbolicLink0(linkBuffer.address(), targetBuffer.address(),
 939                                 flags);
 940         } catch (WindowsException x) {
 941             if (x.lastError() == ERROR_PRIVILEGE_NOT_HELD) {
 942                 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
 943                 try {
 944                     CreateSymbolicLink0(linkBuffer.address(),
 945                                         targetBuffer.address(), flags);
 946                     return;
 947                 } catch (WindowsException ignored) {
 948                     // Will fail with ERROR_INVALID_PARAMETER for Windows
 949                     // builds older than 14972.
 950                 }
 951             }
 952             throw x;
 953         } finally {
 954             targetBuffer.release();
 955             linkBuffer.release();
 956         }
 957     }
 958     private static native void CreateSymbolicLink0(long linkAddress,
 959         long targetAddress, int flags) throws WindowsException;
 960 
 961     /**
 962      * CreateHardLink(
 963      *    LPCTSTR lpFileName,
 964      *    LPCTSTR lpExistingFileName,
 965      *    LPSECURITY_ATTRIBUTES lpSecurityAttributes
 966      * )
 967      */
 968     static void CreateHardLink(String newFile, String existingFile)
 969         throws WindowsException
 970     {
 971         NativeBuffer newFileBuffer = asNativeBuffer(newFile);
 972         NativeBuffer existingFileBuffer = asNativeBuffer(existingFile);


   1 /*
   2  * Copyright (c) 2008, 2017, 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 import java.security.AccessController;
  29 import java.security.PrivilegedAction;
  30 import jdk.internal.misc.Unsafe;
  31 


  32 /**
  33  * Win32 and library calls.
  34  */
  35 
  36 class WindowsNativeDispatcher {
  37     private WindowsNativeDispatcher() { }
  38 
  39     /**
  40      * HANDLE CreateEvent(
  41      *   LPSECURITY_ATTRIBUTES lpEventAttributes,
  42      *   BOOL bManualReset,
  43      *   BOOL bInitialState,
  44      *   PCTSTR lpName
  45      * );
  46      */
  47     static native long CreateEvent(boolean bManualReset, boolean bInitialState)
  48         throws WindowsException;
  49 
  50     /**
  51      * HANDLE CreateFile(


 903 
 904     /**
 905      */
 906     static long LookupPrivilegeValue(String name) throws WindowsException {
 907         NativeBuffer buffer = asNativeBuffer(name);
 908         try {
 909             return LookupPrivilegeValue0(buffer.address());
 910         } finally {
 911             buffer.release();
 912         }
 913     }
 914     private static native long LookupPrivilegeValue0(long lpName)
 915         throws WindowsException;
 916 
 917     /**
 918      * CreateSymbolicLink(
 919      *   LPCWSTR lpSymlinkFileName,
 920      *   LPCWSTR lpTargetFileName,
 921      *   DWORD dwFlags
 922      * )






 923      */
 924     static void CreateSymbolicLink(String link, String target, int flags)
 925         throws WindowsException
 926     {
 927         NativeBuffer linkBuffer = asNativeBuffer(link);
 928         NativeBuffer targetBuffer = asNativeBuffer(target);
 929         try {
 930             CreateSymbolicLink0(linkBuffer.address(), targetBuffer.address(),
 931                                 flags);













 932         } finally {
 933             targetBuffer.release();
 934             linkBuffer.release();
 935         }
 936     }
 937     private static native void CreateSymbolicLink0(long linkAddress,
 938         long targetAddress, int flags) throws WindowsException;
 939 
 940     /**
 941      * CreateHardLink(
 942      *    LPCTSTR lpFileName,
 943      *    LPCTSTR lpExistingFileName,
 944      *    LPSECURITY_ATTRIBUTES lpSecurityAttributes
 945      * )
 946      */
 947     static void CreateHardLink(String newFile, String existingFile)
 948         throws WindowsException
 949     {
 950         NativeBuffer newFileBuffer = asNativeBuffer(newFile);
 951         NativeBuffer existingFileBuffer = asNativeBuffer(existingFile);


< prev index next >