jdk/src/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java
changeset 2057 3acf8e5e2ca0
child 2071 5e6af6d106cb
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.nio.fs;
       
    27 
       
    28 import java.security.AccessController;
       
    29 import java.security.PrivilegedAction;
       
    30 import sun.misc.Unsafe;
       
    31 
       
    32 /**
       
    33  * Win32 and library calls.
       
    34  */
       
    35 
       
    36 class WindowsNativeDispatcher {
       
    37     private WindowsNativeDispatcher() { }
       
    38 
       
    39     /**
       
    40      * HANDLE CreateFile(
       
    41      *   LPCTSTR lpFileName,
       
    42      *   DWORD dwDesiredAccess,
       
    43      *   DWORD dwShareMode,
       
    44      *   LPSECURITY_ATTRIBUTES lpSecurityAttributes,
       
    45      *   DWORD dwCreationDisposition,
       
    46      *   DWORD dwFlagsAndAttributes,
       
    47      *   HANDLE hTemplateFile
       
    48      * )
       
    49      */
       
    50     static long CreateFile(String path,
       
    51                            int dwDesiredAccess,
       
    52                            int dwShareMode,
       
    53                            long lpSecurityAttributes,
       
    54                            int dwCreationDisposition,
       
    55                            int dwFlagsAndAttributes)
       
    56         throws WindowsException
       
    57     {
       
    58         NativeBuffer buffer = asNativeBuffer(path);
       
    59         try {
       
    60             return CreateFile0(buffer.address(),
       
    61                                dwDesiredAccess,
       
    62                                dwShareMode,
       
    63                                lpSecurityAttributes,
       
    64                                dwCreationDisposition,
       
    65                                dwFlagsAndAttributes);
       
    66         } finally {
       
    67             buffer.release();
       
    68         }
       
    69     }
       
    70     static long CreateFile(String path,
       
    71                            int dwDesiredAccess,
       
    72                            int dwShareMode,
       
    73                            int dwCreationDisposition,
       
    74                            int dwFlagsAndAttributes)
       
    75         throws WindowsException
       
    76     {
       
    77         return CreateFile(path, dwDesiredAccess, dwShareMode, 0L,
       
    78                           dwCreationDisposition, dwFlagsAndAttributes);
       
    79     }
       
    80     private static native long CreateFile0(long lpFileName,
       
    81                                            int dwDesiredAccess,
       
    82                                            int dwShareMode,
       
    83                                            long lpSecurityAttributes,
       
    84                                            int dwCreationDisposition,
       
    85                                            int dwFlagsAndAttributes)
       
    86         throws WindowsException;
       
    87 
       
    88     /**
       
    89      * CloseHandle(
       
    90      *   HANDLE hObject
       
    91      * )
       
    92      */
       
    93     static native void CloseHandle(long handle);
       
    94 
       
    95     /**
       
    96      * DeleteFile(
       
    97      *   LPCTSTR lpFileName
       
    98      * )
       
    99      */
       
   100     static void DeleteFile(String path) throws WindowsException {
       
   101         NativeBuffer buffer = asNativeBuffer(path);
       
   102         try {
       
   103             DeleteFile0(buffer.address());
       
   104         } finally {
       
   105             buffer.release();
       
   106         }
       
   107     }
       
   108     private static native void DeleteFile0(long lpFileName)
       
   109         throws WindowsException;
       
   110 
       
   111     /**
       
   112      * CreateDirectory(
       
   113      *   LPCTSTR lpPathName,
       
   114      *   LPSECURITY_ATTRIBUTES lpSecurityAttributes
       
   115      * )
       
   116      */
       
   117     static void CreateDirectory(String path, long lpSecurityAttributes) throws WindowsException {
       
   118         NativeBuffer buffer = asNativeBuffer(path);
       
   119         try {
       
   120             CreateDirectory0(buffer.address(), lpSecurityAttributes);
       
   121         } finally {
       
   122             buffer.release();
       
   123         }
       
   124     }
       
   125     private static native void CreateDirectory0(long lpFileName, long lpSecurityAttributes)
       
   126         throws WindowsException;
       
   127 
       
   128     /**
       
   129      * RemoveDirectory(
       
   130      *   LPCTSTR lpPathName
       
   131      * )
       
   132      */
       
   133     static void RemoveDirectory(String path) throws WindowsException {
       
   134         NativeBuffer buffer = asNativeBuffer(path);
       
   135         try {
       
   136             RemoveDirectory0(buffer.address());
       
   137         } finally {
       
   138             buffer.release();
       
   139         }
       
   140     }
       
   141     private static native void RemoveDirectory0(long lpFileName)
       
   142         throws WindowsException;
       
   143 
       
   144     /**
       
   145      * Marks a file as a sparse file.
       
   146      *
       
   147      * DeviceIoControl(
       
   148      *   FSCTL_SET_SPARSE
       
   149      * )
       
   150      */
       
   151     static native void DeviceIoControlSetSparse(long handle)
       
   152         throws WindowsException;
       
   153 
       
   154     /**
       
   155      * Retrieves the reparse point data associated with the file or directory.
       
   156      *
       
   157      * DeviceIoControl(
       
   158      *   FSCTL_GET_REPARSE_POINT
       
   159      * )
       
   160      */
       
   161     static native void DeviceIoControlGetReparsePoint(long handle,
       
   162         long bufferAddress, int bufferSize) throws WindowsException;
       
   163 
       
   164     /**
       
   165      * HANDLE FindFirstFile(
       
   166      *   LPCTSTR lpFileName,
       
   167      *   LPWIN32_FIND_DATA lpFindFileData
       
   168      * )
       
   169      */
       
   170     static FirstFile FindFirstFile(String path) throws WindowsException {
       
   171         NativeBuffer buffer = asNativeBuffer(path);
       
   172         try {
       
   173             FirstFile data = new FirstFile();
       
   174             FindFirstFile0(buffer.address(), data);
       
   175             return data;
       
   176         } finally {
       
   177             buffer.release();
       
   178         }
       
   179     }
       
   180     static class FirstFile {
       
   181         private long handle;
       
   182         private String name;
       
   183 
       
   184         private FirstFile() { }
       
   185         public long handle()    { return handle; }
       
   186         public String name()    { return name; }
       
   187     }
       
   188     private static native void FindFirstFile0(long lpFileName, FirstFile obj)
       
   189         throws WindowsException;
       
   190 
       
   191     /**
       
   192      * HANDLE FindFirstFile(
       
   193      *   LPCTSTR lpFileName,
       
   194      *   LPWIN32_FIND_DATA lpFindFileData
       
   195      * )
       
   196      */
       
   197     static long FindFirstFile(String path, long address) throws WindowsException {
       
   198         NativeBuffer buffer = asNativeBuffer(path);
       
   199         try {
       
   200             return FindFirstFile1(buffer.address(), address);
       
   201         } finally {
       
   202             buffer.release();
       
   203         }
       
   204     }
       
   205     private static native long FindFirstFile1(long lpFileName, long address)
       
   206         throws WindowsException;
       
   207 
       
   208     /**
       
   209      * FindNextFile(
       
   210      *   HANDLE hFindFile,
       
   211      *   LPWIN32_FIND_DATA lpFindFileData
       
   212      * )
       
   213      *
       
   214      * @return  lpFindFileData->cFileName
       
   215      */
       
   216     static native String FindNextFile(long handle) throws WindowsException;
       
   217 
       
   218     /**
       
   219      * HANDLE FindFirstStreamW(
       
   220      *   LPCWSTR lpFileName,
       
   221      *   STREAM_INFO_LEVELS InfoLevel,
       
   222      *   LPVOID lpFindStreamData,
       
   223      *   DWORD dwFlags
       
   224      * )
       
   225      */
       
   226     static FirstStream FindFirstStream(String path) throws WindowsException {
       
   227         NativeBuffer buffer = asNativeBuffer(path);
       
   228         try {
       
   229             FirstStream data = new FirstStream();
       
   230             FindFirstStream0(buffer.address(), data);
       
   231             if (data.handle() == WindowsConstants.INVALID_HANDLE_VALUE)
       
   232                 return null;
       
   233             return data;
       
   234         } finally {
       
   235             buffer.release();
       
   236         }
       
   237     }
       
   238     static class FirstStream {
       
   239         private long handle;
       
   240         private String name;
       
   241 
       
   242         private FirstStream() { }
       
   243         public long handle()    { return handle; }
       
   244         public String name()    { return name; }
       
   245     }
       
   246     private static native void FindFirstStream0(long lpFileName, FirstStream obj)
       
   247         throws WindowsException;
       
   248 
       
   249     /*
       
   250      * FindNextStreamW(
       
   251      *   HANDLE hFindStream,
       
   252      *   LPVOID lpFindStreamData
       
   253      * )
       
   254      */
       
   255     static native String FindNextStream(long handle) throws WindowsException;
       
   256 
       
   257     /**
       
   258      * FindClose(
       
   259      *   HANDLE hFindFile
       
   260      * )
       
   261      */
       
   262     static native void FindClose(long handle) throws WindowsException;
       
   263 
       
   264     /**
       
   265      * GetFileInformationByHandle(
       
   266      *   HANDLE hFile,
       
   267      *   LPBY_HANDLE_FILE_INFORMATION lpFileInformation
       
   268      * )
       
   269      */
       
   270     static native void GetFileInformationByHandle(long handle, long address)
       
   271         throws WindowsException;
       
   272 
       
   273     /**
       
   274      * CopyFileEx(
       
   275      *   LPCWSTR lpExistingFileName
       
   276      *   LPCWSTR lpNewFileName,
       
   277      *   LPPROGRESS_ROUTINE lpProgressRoutine
       
   278      *   LPVOID lpData,
       
   279      *   LPBOOL pbCancel,
       
   280      *   DWORD dwCopyFlags
       
   281      * )
       
   282      */
       
   283     static void CopyFileEx(String source, String target, int flags,
       
   284                            long addressToPollForCancel)
       
   285         throws WindowsException
       
   286     {
       
   287         NativeBuffer sourceBuffer = asNativeBuffer(source);
       
   288         NativeBuffer targetBuffer = asNativeBuffer(target);
       
   289         try {
       
   290             CopyFileEx0(sourceBuffer.address(), targetBuffer.address(), flags,
       
   291                         addressToPollForCancel);
       
   292         } finally {
       
   293             targetBuffer.release();
       
   294             sourceBuffer.release();
       
   295         }
       
   296     }
       
   297     private static native void CopyFileEx0(long existingAddress, long newAddress,
       
   298         int flags, long addressToPollForCancel) throws WindowsException;
       
   299 
       
   300     /**
       
   301      * MoveFileEx(
       
   302      *   LPCTSTR lpExistingFileName,
       
   303      *   LPCTSTR lpNewFileName,
       
   304      *   DWORD dwFlags
       
   305      * )
       
   306      */
       
   307     static void MoveFileEx(String source, String target, int flags)
       
   308         throws WindowsException
       
   309     {
       
   310         NativeBuffer sourceBuffer = asNativeBuffer(source);
       
   311         NativeBuffer targetBuffer = asNativeBuffer(target);
       
   312         try {
       
   313             MoveFileEx0(sourceBuffer.address(), targetBuffer.address(), flags);
       
   314         } finally {
       
   315             targetBuffer.release();
       
   316             sourceBuffer.release();
       
   317         }
       
   318     }
       
   319     private static native void MoveFileEx0(long existingAddress, long newAddress,
       
   320         int flags) throws WindowsException;
       
   321 
       
   322     /**
       
   323      * DWORD GetFileAttributes(
       
   324      *   LPCTSTR lpFileName
       
   325      * )
       
   326      */
       
   327     static int GetFileAttributes(String path) throws WindowsException {
       
   328         NativeBuffer buffer = asNativeBuffer(path);
       
   329         try {
       
   330             return GetFileAttributes0(buffer.address());
       
   331         } finally {
       
   332             buffer.release();
       
   333         }
       
   334     }
       
   335     private static native int GetFileAttributes0(long lpFileName)
       
   336         throws WindowsException;
       
   337 
       
   338     /**
       
   339      * SetFileAttributes(
       
   340      *   LPCTSTR lpFileName,
       
   341      *   DWORD dwFileAttributes
       
   342      */
       
   343     static void SetFileAttributes(String path, int dwFileAttributes)
       
   344         throws WindowsException
       
   345     {
       
   346         NativeBuffer buffer = asNativeBuffer(path);
       
   347         try {
       
   348             SetFileAttributes0(buffer.address(), dwFileAttributes);
       
   349         } finally {
       
   350             buffer.release();
       
   351         }
       
   352     }
       
   353     private static native void SetFileAttributes0(long lpFileName,
       
   354         int dwFileAttributes) throws WindowsException;
       
   355 
       
   356     /**
       
   357      * GetFileAttributesEx(
       
   358      *   LPCTSTR lpFileName,
       
   359      *   GET_FILEEX_INFO_LEVELS fInfoLevelId,
       
   360      *   LPVOID lpFileInformation
       
   361      * );
       
   362      */
       
   363     static void GetFileAttributesEx(String path, long address) throws WindowsException {
       
   364         NativeBuffer buffer = asNativeBuffer(path);
       
   365         try {
       
   366             GetFileAttributesEx0(buffer.address(), address);
       
   367         } finally {
       
   368             buffer.release();
       
   369         }
       
   370     }
       
   371     private static native void GetFileAttributesEx0(long lpFileName, long address)
       
   372         throws WindowsException;
       
   373     /**
       
   374      * SetFileTime(
       
   375      *   HANDLE hFile,
       
   376      *   CONST FILETIME *lpCreationTime,
       
   377      *   CONST FILETIME *lpLastAccessTime,
       
   378      *   CONST FILETIME *lpLastWriteTime
       
   379      * )
       
   380      */
       
   381     static native void SetFileTime(long handle, long createTime,
       
   382         long lastAccessTime, long lastWriteTime) throws WindowsException;
       
   383 
       
   384     /**
       
   385      * SetEndOfFile(
       
   386      *   HANDLE hFile
       
   387      * )
       
   388      */
       
   389     static native void SetEndOfFile(long handle) throws WindowsException;
       
   390 
       
   391     /**
       
   392      * DWORD GetLogicalDrives(VOID)
       
   393      */
       
   394     static native int GetLogicalDrives() throws WindowsException;
       
   395 
       
   396     /**
       
   397      * GetVolumeInformation(
       
   398      *   LPCTSTR lpRootPathName,
       
   399      *   LPTSTR lpVolumeNameBuffer,
       
   400      *   DWORD nVolumeNameSize,
       
   401      *   LPDWORD lpVolumeSerialNumber,
       
   402      *   LPDWORD lpMaximumComponentLength,
       
   403      *   LPDWORD lpFileSystemFlags,
       
   404      *   LPTSTR lpFileSystemNameBuffer,
       
   405      *   DWORD nFileSystemNameSize
       
   406      * )
       
   407      */
       
   408     static VolumeInformation GetVolumeInformation(String root)
       
   409         throws WindowsException
       
   410     {
       
   411         NativeBuffer buffer = asNativeBuffer(root);
       
   412         try {
       
   413             VolumeInformation info = new VolumeInformation();
       
   414             GetVolumeInformation0(buffer.address(), info);
       
   415             return info;
       
   416         } finally {
       
   417             buffer.release();
       
   418         }
       
   419     }
       
   420     static class VolumeInformation {
       
   421         private String fileSystemName;
       
   422         private String volumeName;
       
   423         private int volumeSerialNumber;
       
   424         private int flags;
       
   425         private VolumeInformation() { }
       
   426 
       
   427         public String fileSystemName()      { return fileSystemName; }
       
   428         public String volumeName()          { return volumeName; }
       
   429         public int volumeSerialNumber()     { return volumeSerialNumber; }
       
   430         public int flags()                  { return flags; }
       
   431     }
       
   432     private static native void GetVolumeInformation0(long lpRoot,
       
   433                                                      VolumeInformation obj)
       
   434         throws WindowsException;
       
   435 
       
   436     /**
       
   437      * UINT GetDriveType(
       
   438      *   LPCTSTR lpRootPathName
       
   439      * )
       
   440      */
       
   441     static int GetDriveType(String root) throws WindowsException {
       
   442         NativeBuffer buffer = asNativeBuffer(root);
       
   443         try {
       
   444             return GetDriveType0(buffer.address());
       
   445         } finally {
       
   446             buffer.release();
       
   447         }
       
   448     }
       
   449     private static native int GetDriveType0(long lpRoot) throws WindowsException;
       
   450 
       
   451     /**
       
   452      * GetDiskFreeSpaceEx(
       
   453      *   LPCTSTR lpDirectoryName,
       
   454      *   PULARGE_INTEGER lpFreeBytesAvailableToCaller,
       
   455      *   PULARGE_INTEGER lpTotalNumberOfBytes,
       
   456      *   PULARGE_INTEGER lpTotalNumberOfFreeBytes
       
   457      * )
       
   458      */
       
   459     static DiskFreeSpace GetDiskFreeSpaceEx(String path)
       
   460         throws WindowsException
       
   461     {
       
   462         NativeBuffer buffer = asNativeBuffer(path);
       
   463         try {
       
   464             DiskFreeSpace space = new DiskFreeSpace();
       
   465             GetDiskFreeSpaceEx0(buffer.address(), space);
       
   466             return space;
       
   467         } finally {
       
   468             buffer.release();
       
   469         }
       
   470     }
       
   471     static class DiskFreeSpace {
       
   472         private long freeBytesAvailable;
       
   473         private long totalNumberOfBytes;
       
   474         private long totalNumberOfFreeBytes;
       
   475         private DiskFreeSpace() { }
       
   476 
       
   477         public long freeBytesAvailable()      { return freeBytesAvailable; }
       
   478         public long totalNumberOfBytes()      { return totalNumberOfBytes; }
       
   479         public long totalNumberOfFreeBytes()  { return totalNumberOfFreeBytes; }
       
   480     }
       
   481     private static native void GetDiskFreeSpaceEx0(long lpDirectoryName,
       
   482                                                    DiskFreeSpace obj)
       
   483         throws WindowsException;
       
   484 
       
   485 
       
   486     /**
       
   487      * GetVolumePathName(
       
   488      *   LPCTSTR lpszFileName,
       
   489      *   LPTSTR lpszVolumePathName,
       
   490      *   DWORD cchBufferLength
       
   491      * )
       
   492      *
       
   493      * @return  lpFileName
       
   494      */
       
   495     static String GetVolumePathName(String path) throws WindowsException {
       
   496         NativeBuffer buffer = asNativeBuffer(path);
       
   497         try {
       
   498             return GetVolumePathName0(buffer.address());
       
   499         } finally {
       
   500             buffer.release();
       
   501         }
       
   502     }
       
   503     private static native String GetVolumePathName0(long lpFileName)
       
   504         throws WindowsException;
       
   505 
       
   506 
       
   507     /**
       
   508      * InitializeSecurityDescriptor(
       
   509      *   PSECURITY_DESCRIPTOR pSecurityDescriptor,
       
   510      *   DWORD dwRevision
       
   511      * )
       
   512      */
       
   513     static native void InitializeSecurityDescriptor(long sdAddress)
       
   514         throws WindowsException;
       
   515 
       
   516     /**
       
   517      * InitializeAcl(
       
   518      *   PACL pAcl,
       
   519      *   DWORD nAclLength,
       
   520      *   DWORD dwAclRevision
       
   521      * )
       
   522      */
       
   523     static native void InitializeAcl(long aclAddress, int size)
       
   524          throws WindowsException;
       
   525 
       
   526     /**
       
   527      * GetFileSecurity(
       
   528      *   LPCTSTR lpFileName,
       
   529      *   SECURITY_INFORMATION RequestedInformation,
       
   530      *   PSECURITY_DESCRIPTOR pSecurityDescriptor,
       
   531      *   DWORD nLength,
       
   532      *   LPDWORD lpnLengthNeeded
       
   533      * )
       
   534      */
       
   535     static int GetFileSecurity(String path,
       
   536                                int requestedInformation,
       
   537                                long pSecurityDescriptor,
       
   538                                int nLength) throws WindowsException
       
   539     {
       
   540         NativeBuffer buffer = asNativeBuffer(path);
       
   541         try {
       
   542             return GetFileSecurity0(buffer.address(), requestedInformation,
       
   543                 pSecurityDescriptor, nLength);
       
   544         } finally {
       
   545             buffer.release();
       
   546         }
       
   547     }
       
   548     private static native int GetFileSecurity0(long lpFileName,
       
   549                                                int requestedInformation,
       
   550                                                long pSecurityDescriptor,
       
   551                                                int nLength) throws WindowsException;
       
   552 
       
   553     /**
       
   554      * SetFileSecurity(
       
   555      *   LPCTSTR lpFileName,
       
   556      *   SECURITY_INFORMATION SecurityInformation,
       
   557      *   PSECURITY_DESCRIPTOR pSecurityDescriptor
       
   558      * )
       
   559      */
       
   560     static void SetFileSecurity(String path,
       
   561                                 int securityInformation,
       
   562                                 long pSecurityDescriptor)
       
   563         throws WindowsException
       
   564     {
       
   565         NativeBuffer buffer = asNativeBuffer(path);
       
   566         try {
       
   567             SetFileSecurity0(buffer.address(), securityInformation,
       
   568                 pSecurityDescriptor);
       
   569         } finally {
       
   570             buffer.release();
       
   571         }
       
   572     }
       
   573     static native void SetFileSecurity0(long lpFileName, int securityInformation,
       
   574         long pSecurityDescriptor) throws WindowsException;
       
   575 
       
   576     /**
       
   577      * GetSecurityDescriptorOwner(
       
   578      *   PSECURITY_DESCRIPTOR pSecurityDescriptor
       
   579      *   PSID *pOwner,
       
   580      *   LPBOOL lpbOwnerDefaulted
       
   581      * )
       
   582      *
       
   583      * @return  pOwner
       
   584      */
       
   585     static native long GetSecurityDescriptorOwner(long pSecurityDescriptor)
       
   586         throws WindowsException;
       
   587 
       
   588     /**
       
   589      * SetSecurityDescriptorOwner(
       
   590      *   PSECURITY_DESCRIPTOR pSecurityDescriptor,
       
   591      *   PSID pOwner,
       
   592      *   BOOL bOwnerDefaulted
       
   593      * )
       
   594      */
       
   595     static native void SetSecurityDescriptorOwner(long pSecurityDescriptor,
       
   596                                                   long pOwner)
       
   597         throws WindowsException;
       
   598 
       
   599     /**
       
   600      * GetSecurityDescriptorDacl(
       
   601      *   PSECURITY_DESCRIPTOR pSecurityDescriptor,
       
   602      *   LPBOOL lpbDaclPresent,
       
   603      *   PACL *pDacl,
       
   604      *   LPBOOL lpbDaclDefaulted
       
   605      * )
       
   606      */
       
   607     static native long GetSecurityDescriptorDacl(long pSecurityDescriptor);
       
   608 
       
   609     /**
       
   610      * SetSecurityDescriptorDacl(
       
   611      *   PSECURITY_DESCRIPTOR pSecurityDescriptor,
       
   612      *   BOOL bDaclPresent,
       
   613      *   PACL pDacl,
       
   614      *   BOOL bDaclDefaulted
       
   615      * )
       
   616      */
       
   617     static native void SetSecurityDescriptorDacl(long pSecurityDescriptor, long pAcl)
       
   618         throws WindowsException;
       
   619 
       
   620 
       
   621     /**
       
   622      * GetAclInformation(
       
   623      *   PACL pAcl,
       
   624      *   LPVOID pAclInformation,
       
   625      *   DWORD nAclInformationLength,
       
   626      *   ACL_INFORMATION_CLASS dwAclInformationClass
       
   627      * )
       
   628      */
       
   629     static AclInformation GetAclInformation(long aclAddress) {
       
   630         AclInformation info = new AclInformation();
       
   631         GetAclInformation0(aclAddress, info);
       
   632         return info;
       
   633     }
       
   634     static class AclInformation {
       
   635         private int aceCount;
       
   636         private AclInformation() { }
       
   637 
       
   638         public int aceCount()   { return aceCount; }
       
   639     }
       
   640     private static native void GetAclInformation0(long aclAddress,
       
   641         AclInformation obj);
       
   642 
       
   643     /**
       
   644      * GetAce(
       
   645      *   PACL pAcl,
       
   646      *   DWORD dwAceIndex,
       
   647      *   LPVOID *pAce
       
   648      * )
       
   649      */
       
   650     static native long GetAce(long aclAddress, int aceIndex);
       
   651 
       
   652     /**
       
   653      * AddAccessAllowedAceEx(
       
   654      *   PACL pAcl,
       
   655      *   DWORD dwAceRevision,
       
   656      *   DWORD AceFlags,
       
   657      *   DWORD AccessMask,
       
   658      *   PSID pSid
       
   659      * )
       
   660      */
       
   661     static native void AddAccessAllowedAceEx(long aclAddress, int flags,
       
   662         int mask, long sidAddress) throws WindowsException;
       
   663 
       
   664     /**
       
   665      * AddAccessDeniedAceEx(
       
   666      *   PACL pAcl,
       
   667      *   DWORD dwAceRevision,
       
   668      *   DWORD AceFlags,
       
   669      *   DWORD AccessMask,
       
   670      *   PSID pSid
       
   671      * )
       
   672      */
       
   673     static native void AddAccessDeniedAceEx(long aclAddress, int flags,
       
   674         int mask, long sidAddress) throws WindowsException;
       
   675 
       
   676     /**
       
   677      * LookupAccountSid(
       
   678      *   LPCTSTR lpSystemName,
       
   679      *   PSID Sid,
       
   680      *   LPTSTR Name,
       
   681      *   LPDWORD cbName,
       
   682      *   LPTSTR ReferencedDomainName,
       
   683      *   LPDWORD cbReferencedDomainName,
       
   684      *   PSID_NAME_USE peUse
       
   685      * )
       
   686      */
       
   687     static Account LookupAccountSid(long sidAddress) throws WindowsException {
       
   688         Account acc = new Account();
       
   689         LookupAccountSid0(sidAddress, acc);
       
   690         return acc;
       
   691     }
       
   692     static class Account {
       
   693         private String domain;
       
   694         private String name;
       
   695         private int use;
       
   696         private Account() { }
       
   697 
       
   698         public String domain()  { return domain; }
       
   699         public String name()    { return name; }
       
   700         public int use()        { return use; }
       
   701     }
       
   702     private static native void LookupAccountSid0(long sidAddress, Account obj)
       
   703         throws WindowsException;
       
   704 
       
   705     /**
       
   706      * LookupAccountName(
       
   707      *   LPCTSTR lpSystemName,
       
   708      *   LPCTSTR lpAccountName,
       
   709      *   PSID Sid,
       
   710      *   LPDWORD cbSid,
       
   711      *   LPTSTR ReferencedDomainName,
       
   712      *   LPDWORD cbReferencedDomainName,
       
   713      *   PSID_NAME_USE peUse
       
   714      * )
       
   715      *
       
   716      * @return  cbSid
       
   717      */
       
   718     static int LookupAccountName(String accountName,
       
   719                                  long pSid,
       
   720                                  int cbSid) throws WindowsException
       
   721     {
       
   722         NativeBuffer buffer = asNativeBuffer(accountName);
       
   723         try {
       
   724             return LookupAccountName0(buffer.address(), pSid, cbSid);
       
   725         } finally {
       
   726             buffer.release();
       
   727         }
       
   728     }
       
   729     private static native int LookupAccountName0(long lpAccountName, long pSid,
       
   730         int cbSid) throws WindowsException;
       
   731 
       
   732     /**
       
   733      * DWORD GetLengthSid(
       
   734      *   PSID pSid
       
   735      * )
       
   736      */
       
   737     static native int GetLengthSid(long sidAddress);
       
   738 
       
   739     /**
       
   740      * ConvertSidToStringSid(
       
   741      *   PSID Sid,
       
   742      *   LPTSTR* StringSid
       
   743      * )
       
   744      *
       
   745      * @return  StringSid
       
   746      */
       
   747     static native String ConvertSidToStringSid(long sidAddress)
       
   748         throws WindowsException;
       
   749 
       
   750     /**
       
   751      * ConvertStringSidToSid(
       
   752      *   LPCTSTR StringSid,
       
   753      *   PSID* pSid
       
   754      * )
       
   755      *
       
   756      * @return  pSid
       
   757      */
       
   758     static long ConvertStringSidToSid(String sidString)
       
   759         throws WindowsException
       
   760     {
       
   761         NativeBuffer buffer = asNativeBuffer(sidString);
       
   762         try {
       
   763             return ConvertStringSidToSid0(buffer.address());
       
   764         } finally {
       
   765             buffer.release();
       
   766         }
       
   767     }
       
   768     private static native long ConvertStringSidToSid0(long lpStringSid)
       
   769         throws WindowsException;
       
   770 
       
   771     /**
       
   772      * HANDLE GetCurrentProcess(VOID)
       
   773      */
       
   774     static native long GetCurrentProcess();
       
   775 
       
   776     /**
       
   777      * HANDLE GetCurrentThread(VOID)
       
   778      */
       
   779     static native long GetCurrentThread();
       
   780 
       
   781     /**
       
   782      * OpenProcessToken(
       
   783      *   HANDLE ProcessHandle,
       
   784      *   DWORD DesiredAccess,
       
   785      *   PHANDLE TokenHandle
       
   786      * )
       
   787      */
       
   788     static native long OpenProcessToken(long hProcess, int desiredAccess)
       
   789         throws WindowsException;
       
   790 
       
   791     /**
       
   792      * OpenThreadToken(
       
   793      *   HANDLE ThreadHandle,
       
   794      *   DWORD DesiredAccess,
       
   795      *   BOOL OpenAsSelf,
       
   796      *   PHANDLE TokenHandle
       
   797      * )
       
   798      */
       
   799     static native long OpenThreadToken(long hThread, int desiredAccess,
       
   800         boolean openAsSelf) throws WindowsException;
       
   801 
       
   802     /**
       
   803      */
       
   804     static native long DuplicateTokenEx(long hThread, int desiredAccess)
       
   805         throws WindowsException;
       
   806 
       
   807     /**
       
   808      * SetThreadToken(
       
   809      *   PHANDLE Thread,
       
   810      *   HANDLE Token
       
   811      * )
       
   812      */
       
   813     static native void SetThreadToken(long thread, long hToken)
       
   814         throws WindowsException;
       
   815 
       
   816     /**
       
   817      * GetTokenInformation(
       
   818      *   HANDLE TokenHandle,
       
   819      *   TOKEN_INFORMATION_CLASS TokenInformationClass,
       
   820      *   LPVOID TokenInformation,
       
   821      *   DWORD TokenInformationLength,
       
   822      *   PDWORD ReturnLength
       
   823      * )
       
   824      */
       
   825     static native int GetTokenInformation(long token, int tokenInfoClass,
       
   826         long pTokenInfo, int tokenInfoLength) throws WindowsException;
       
   827 
       
   828     /**
       
   829      * AdjustTokenPrivileges(
       
   830      *   HANDLE TokenHandle,
       
   831      *   BOOL DisableAllPrivileges
       
   832      *   PTOKEN_PRIVILEGES NewState
       
   833      *   DWORD BufferLength
       
   834      *   PTOKEN_PRIVILEGES
       
   835      *   PDWORD ReturnLength
       
   836      * )
       
   837      */
       
   838     static native void AdjustTokenPrivileges(long token, long luid, int attributes)
       
   839         throws WindowsException;
       
   840 
       
   841     /**
       
   842      */
       
   843     static long LookupPrivilegeValue(String name) throws WindowsException {
       
   844         NativeBuffer buffer = asNativeBuffer(name);
       
   845         try {
       
   846             return LookupPrivilegeValue0(buffer.address());
       
   847         } finally {
       
   848             buffer.release();
       
   849         }
       
   850     }
       
   851     private static native long LookupPrivilegeValue0(long lpName)
       
   852         throws WindowsException;
       
   853 
       
   854     /**
       
   855      * BuildTrusteeWithSid(
       
   856      *   PTRUSTEE pTrustee,
       
   857      *   PSID pSid
       
   858      * )
       
   859      *
       
   860      * @return  pTrustee
       
   861      */
       
   862     static native long BuildTrusteeWithSid(long pSid);
       
   863 
       
   864     /**
       
   865      * GetEffectiveRightsFromAcl(
       
   866      *   PACL pacl,
       
   867      *   PTRUSTEE pTrustee,
       
   868      *   PACCESS_MASK pAccessRights
       
   869      * )
       
   870      *
       
   871      * @return  AccessRights
       
   872      */
       
   873     static native int GetEffectiveRightsFromAcl(long pAcl, long pTrustee)
       
   874         throws WindowsException;
       
   875 
       
   876     /**
       
   877      * CreateSymbolicLink(
       
   878      *   LPCWSTR lpSymlinkFileName,
       
   879      *   LPCWSTR lpTargetFileName,
       
   880      *   DWORD dwFlags
       
   881      * )
       
   882      */
       
   883     static void CreateSymbolicLink(String link, String target, int flags)
       
   884         throws WindowsException
       
   885     {
       
   886         NativeBuffer linkBuffer = asNativeBuffer(link);
       
   887         NativeBuffer targetBuffer = asNativeBuffer(target);
       
   888         try {
       
   889             CreateSymbolicLink0(linkBuffer.address(), targetBuffer.address(),
       
   890                                 flags);
       
   891         } finally {
       
   892             targetBuffer.release();
       
   893             linkBuffer.release();
       
   894         }
       
   895     }
       
   896     private static native void CreateSymbolicLink0(long linkAddress,
       
   897         long targetAddress, int flags) throws WindowsException;
       
   898 
       
   899     /**
       
   900      * CreateHardLink(
       
   901      *    LPCTSTR lpFileName,
       
   902      *    LPCTSTR lpExistingFileName,
       
   903      *    LPSECURITY_ATTRIBUTES lpSecurityAttributes
       
   904      * )
       
   905      */
       
   906     static void CreateHardLink(String newFile, String existingFile)
       
   907         throws WindowsException
       
   908     {
       
   909         NativeBuffer newFileBuffer = asNativeBuffer(newFile);
       
   910         NativeBuffer existingFileBuffer = asNativeBuffer(existingFile);
       
   911         try {
       
   912             CreateHardLink0(newFileBuffer.address(), existingFileBuffer.address());
       
   913         } finally {
       
   914             existingFileBuffer.release();
       
   915             newFileBuffer.release();
       
   916         }
       
   917     }
       
   918     private static native void CreateHardLink0(long newFileBuffer,
       
   919         long existingFiletBuffer) throws WindowsException;
       
   920 
       
   921     /**
       
   922      * GetFullPathName(
       
   923      *   LPCTSTR lpFileName,
       
   924      *   DWORD nBufferLength,
       
   925      *   LPTSTR lpBuffer,
       
   926      *   LPTSTR *lpFilePart
       
   927      * )
       
   928      */
       
   929     static String GetFullPathName(String path) throws WindowsException {
       
   930         NativeBuffer buffer = asNativeBuffer(path);
       
   931         try {
       
   932             return GetFullPathName0(buffer.address());
       
   933         } finally {
       
   934             buffer.release();
       
   935         }
       
   936     }
       
   937     private static native String GetFullPathName0(long pathAddress)
       
   938         throws WindowsException;
       
   939 
       
   940     /**
       
   941      * GetFinalPathNameByHandle(
       
   942      *   HANDLE hFile,
       
   943      *   LPTSTR lpszFilePath,
       
   944      *   DWORD cchFilePath,
       
   945      *   DWORD dwFlags
       
   946      * )
       
   947      */
       
   948     static native String GetFinalPathNameByHandle(long handle)
       
   949         throws WindowsException;
       
   950 
       
   951     /**
       
   952      * FormatMessage(
       
   953      *   DWORD dwFlags,
       
   954      *   LPCVOID lpSource,
       
   955      *   DWORD dwMessageId,
       
   956      *   DWORD dwLanguageId,
       
   957      *   LPTSTR lpBuffer,
       
   958      *   DWORD nSize,
       
   959      *   va_list *Arguments
       
   960      * )
       
   961      */
       
   962     static native String FormatMessage(int errorCode);
       
   963 
       
   964     /**
       
   965      * LocalFree(
       
   966      *   HLOCAL hMem
       
   967      * )
       
   968      */
       
   969     static native void LocalFree(long address);
       
   970 
       
   971     /**
       
   972      * HANDLE CreateIoCompletionPort (
       
   973      *   HANDLE FileHandle,
       
   974      *   HANDLE ExistingCompletionPort,
       
   975      *   DWORD CompletionKey,
       
   976      *   DWORD NumberOfConcurrentThreads
       
   977      * )
       
   978      */
       
   979     static native long CreateIoCompletionPort(long fileHandle, long existingPort,
       
   980         int completionKey) throws WindowsException;
       
   981 
       
   982 
       
   983     /**
       
   984      * GetQueuedCompletionStatus(
       
   985      *   HANDLE CompletionPort,
       
   986      *   LPDWORD lpNumberOfBytesTransferred,
       
   987      *   LPDWORD lpCompletionKey,
       
   988      *   LPOVERLAPPED *lpOverlapped,
       
   989      *   DWORD dwMilliseconds
       
   990      */
       
   991     static CompletionStatus GetQueuedCompletionStatus(long completionPort)
       
   992         throws WindowsException
       
   993     {
       
   994         CompletionStatus status = new CompletionStatus();
       
   995         GetQueuedCompletionStatus0(completionPort, status);
       
   996         return status;
       
   997     }
       
   998     static class CompletionStatus {
       
   999         private int error;
       
  1000         private int bytesTransferred;
       
  1001         private int completionKey;
       
  1002         private CompletionStatus() { }
       
  1003 
       
  1004         int error() { return error; }
       
  1005         int bytesTransferred() { return bytesTransferred; }
       
  1006         int completionKey() { return completionKey; }
       
  1007     }
       
  1008     private static native void GetQueuedCompletionStatus0(long completionPort,
       
  1009         CompletionStatus status) throws WindowsException;
       
  1010 
       
  1011     /**
       
  1012      * PostQueuedCompletionStatus(
       
  1013      *   HANDLE CompletionPort,
       
  1014      *   DWORD dwNumberOfBytesTransferred,
       
  1015      *   DWORD dwCompletionKey,
       
  1016      *   LPOVERLAPPED lpOverlapped
       
  1017      * )
       
  1018      */
       
  1019     static native void PostQueuedCompletionStatus(long completionPort,
       
  1020         int completionKey) throws WindowsException;
       
  1021 
       
  1022     /**
       
  1023      * ReadDirectoryChangesW(
       
  1024      *   HANDLE hDirectory,
       
  1025      *   LPVOID lpBuffer,
       
  1026      *   DWORD nBufferLength,
       
  1027      *   BOOL bWatchSubtree,
       
  1028      *   DWORD dwNotifyFilter,
       
  1029      *   LPDWORD lpBytesReturned,
       
  1030      *   LPOVERLAPPED lpOverlapped,
       
  1031      *   LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
       
  1032      * )
       
  1033      */
       
  1034     static native void ReadDirectoryChangesW(long hDirectory,
       
  1035                                              long bufferAddress,
       
  1036                                              int bufferLength,
       
  1037                                              boolean watchSubTree,
       
  1038                                              int filter,
       
  1039                                              long bytesReturnedAddress,
       
  1040                                              long pOverlapped)
       
  1041         throws WindowsException;
       
  1042 
       
  1043     /**
       
  1044      * BackupRead(
       
  1045      *   HANDLE hFile,
       
  1046      *   LPBYTE lpBuffer,
       
  1047      *   DWORD nNumberOfBytesToRead,
       
  1048      *   LPDWORD lpNumberOfBytesRead,
       
  1049      *   BOOL bAbort,
       
  1050      *   BOOL bProcessSecurity,
       
  1051      *   LPVOID* lpContext
       
  1052      * )
       
  1053      */
       
  1054     static BackupResult BackupRead(long hFile,
       
  1055                                    long bufferAddress,
       
  1056                                    int bufferSize,
       
  1057                                    boolean abort,
       
  1058                                    long context)
       
  1059         throws WindowsException
       
  1060     {
       
  1061         BackupResult result = new BackupResult();
       
  1062         BackupRead0(hFile, bufferAddress, bufferSize, abort, context, result);
       
  1063         return result;
       
  1064     }
       
  1065     static class BackupResult {
       
  1066         private int bytesTransferred;
       
  1067         private long context;
       
  1068         private BackupResult() { }
       
  1069 
       
  1070         int bytesTransferred() { return bytesTransferred; }
       
  1071         long context() { return context; }
       
  1072     }
       
  1073     private static native void BackupRead0(long hFile, long bufferAddress,
       
  1074         int bufferSize, boolean abort, long context, BackupResult result)
       
  1075         throws WindowsException;
       
  1076 
       
  1077     /**
       
  1078      * BackupSeek(
       
  1079      *   HANDLE hFile,
       
  1080      *   DWORD dwLowBytesToSeek,
       
  1081      *   DWORD dwHighBytesToSeek,
       
  1082      *   LPDWORD lpdwLowByteSeeked,
       
  1083      *   LPDWORD lpdwHighByteSeeked,
       
  1084      *   LPVOID* lpContext
       
  1085      * )
       
  1086      */
       
  1087     static native void BackupSeek(long hFile, long bytesToSeek, long context)
       
  1088         throws WindowsException;
       
  1089 
       
  1090 
       
  1091     // -- support for copying String with a NativeBuffer --
       
  1092 
       
  1093     private static final Unsafe unsafe = Unsafe.getUnsafe();
       
  1094 
       
  1095     static NativeBuffer asNativeBuffer(String s) {
       
  1096         int stringLengthInBytes = s.length() << 1;
       
  1097         int sizeInBytes = stringLengthInBytes + 2;  // char terminator
       
  1098 
       
  1099         // get a native buffer of sufficient size
       
  1100         NativeBuffer buffer = NativeBuffers.getNativeBufferFromCache(sizeInBytes);
       
  1101         if (buffer == null) {
       
  1102             buffer = NativeBuffers.allocNativeBuffer(sizeInBytes);
       
  1103         } else {
       
  1104             // buffer already contains the string contents
       
  1105             if (buffer.owner() == s)
       
  1106                 return buffer;
       
  1107         }
       
  1108 
       
  1109         // copy into buffer and zero terminate
       
  1110         char[] chars = s.toCharArray();
       
  1111         unsafe.copyMemory(chars, Unsafe.ARRAY_CHAR_BASE_OFFSET, null,
       
  1112             buffer.address(), (long)stringLengthInBytes);
       
  1113         unsafe.putChar(buffer.address() + stringLengthInBytes, (char)0);
       
  1114         buffer.setOwner(s);
       
  1115         return buffer;
       
  1116     }
       
  1117 
       
  1118     // -- native library initialization --
       
  1119 
       
  1120     private static native void initIDs();
       
  1121 
       
  1122     static {
       
  1123         AccessController.doPrivileged(new PrivilegedAction<Void>() {
       
  1124             public Void run() {
       
  1125                 // nio.dll has dependency on net.dll
       
  1126                 System.loadLibrary("net");
       
  1127                 System.loadLibrary("nio");
       
  1128                 return null;
       
  1129         }});
       
  1130         initIDs();
       
  1131     }
       
  1132 
       
  1133 }