Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/sun/nio/fs/WindowsNativeDispatcher.java
32288 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.security.AccessController;28import java.security.PrivilegedAction;29import sun.misc.Unsafe;3031/**32* Win32 and library calls.33*/3435class WindowsNativeDispatcher {36private WindowsNativeDispatcher() { }3738/**39* HANDLE CreateEvent(40* LPSECURITY_ATTRIBUTES lpEventAttributes,41* BOOL bManualReset,42* BOOL bInitialState,43* PCTSTR lpName44* );45*/46static native long CreateEvent(boolean bManualReset, boolean bInitialState)47throws WindowsException;4849/**50* HANDLE CreateFile(51* LPCTSTR lpFileName,52* DWORD dwDesiredAccess,53* DWORD dwShareMode,54* LPSECURITY_ATTRIBUTES lpSecurityAttributes,55* DWORD dwCreationDisposition,56* DWORD dwFlagsAndAttributes,57* HANDLE hTemplateFile58* )59*/60static long CreateFile(String path,61int dwDesiredAccess,62int dwShareMode,63long lpSecurityAttributes,64int dwCreationDisposition,65int dwFlagsAndAttributes)66throws WindowsException67{68NativeBuffer buffer = asNativeBuffer(path);69try {70return CreateFile0(buffer.address(),71dwDesiredAccess,72dwShareMode,73lpSecurityAttributes,74dwCreationDisposition,75dwFlagsAndAttributes);76} finally {77buffer.release();78}79}80static long CreateFile(String path,81int dwDesiredAccess,82int dwShareMode,83int dwCreationDisposition,84int dwFlagsAndAttributes)85throws WindowsException86{87return CreateFile(path, dwDesiredAccess, dwShareMode, 0L,88dwCreationDisposition, dwFlagsAndAttributes);89}90private static native long CreateFile0(long lpFileName,91int dwDesiredAccess,92int dwShareMode,93long lpSecurityAttributes,94int dwCreationDisposition,95int dwFlagsAndAttributes)96throws WindowsException;9798/**99* CloseHandle(100* HANDLE hObject101* )102*/103static native void CloseHandle(long handle);104105/**106* DeleteFile(107* LPCTSTR lpFileName108* )109*/110static void DeleteFile(String path) throws WindowsException {111NativeBuffer buffer = asNativeBuffer(path);112try {113DeleteFile0(buffer.address());114} finally {115buffer.release();116}117}118private static native void DeleteFile0(long lpFileName)119throws WindowsException;120121/**122* CreateDirectory(123* LPCTSTR lpPathName,124* LPSECURITY_ATTRIBUTES lpSecurityAttributes125* )126*/127static void CreateDirectory(String path, long lpSecurityAttributes) throws WindowsException {128NativeBuffer buffer = asNativeBuffer(path);129try {130CreateDirectory0(buffer.address(), lpSecurityAttributes);131} finally {132buffer.release();133}134}135private static native void CreateDirectory0(long lpFileName, long lpSecurityAttributes)136throws WindowsException;137138/**139* RemoveDirectory(140* LPCTSTR lpPathName141* )142*/143static void RemoveDirectory(String path) throws WindowsException {144NativeBuffer buffer = asNativeBuffer(path);145try {146RemoveDirectory0(buffer.address());147} finally {148buffer.release();149}150}151private static native void RemoveDirectory0(long lpFileName)152throws WindowsException;153154/**155* Marks a file as a sparse file.156*157* DeviceIoControl(158* FSCTL_SET_SPARSE159* )160*/161static native void DeviceIoControlSetSparse(long handle)162throws WindowsException;163164/**165* Retrieves the reparse point data associated with the file or directory.166*167* DeviceIoControl(168* FSCTL_GET_REPARSE_POINT169* )170*/171static native void DeviceIoControlGetReparsePoint(long handle,172long bufferAddress, int bufferSize) throws WindowsException;173174/**175* HANDLE FindFirstFile(176* LPCTSTR lpFileName,177* LPWIN32_FIND_DATA lpFindFileData178* )179*/180static FirstFile FindFirstFile(String path) throws WindowsException {181NativeBuffer buffer = asNativeBuffer(path);182try {183FirstFile data = new FirstFile();184FindFirstFile0(buffer.address(), data);185return data;186} finally {187buffer.release();188}189}190static class FirstFile {191private long handle;192private String name;193private int attributes;194195private FirstFile() { }196public long handle() { return handle; }197public String name() { return name; }198public int attributes() { return attributes; }199}200private static native void FindFirstFile0(long lpFileName, FirstFile obj)201throws WindowsException;202203/**204* HANDLE FindFirstFile(205* LPCTSTR lpFileName,206* LPWIN32_FIND_DATA lpFindFileData207* )208*/209static long FindFirstFile(String path, long address) throws WindowsException {210NativeBuffer buffer = asNativeBuffer(path);211try {212return FindFirstFile1(buffer.address(), address);213} finally {214buffer.release();215}216}217private static native long FindFirstFile1(long lpFileName, long address)218throws WindowsException;219220/**221* FindNextFile(222* HANDLE hFindFile,223* LPWIN32_FIND_DATA lpFindFileData224* )225*226* @return lpFindFileData->cFileName or null227*/228static native String FindNextFile(long handle, long address)229throws WindowsException;230231/**232* HANDLE FindFirstStreamW(233* LPCWSTR lpFileName,234* STREAM_INFO_LEVELS InfoLevel,235* LPVOID lpFindStreamData,236* DWORD dwFlags237* )238*/239static FirstStream FindFirstStream(String path) throws WindowsException {240NativeBuffer buffer = asNativeBuffer(path);241try {242FirstStream data = new FirstStream();243FindFirstStream0(buffer.address(), data);244if (data.handle() == WindowsConstants.INVALID_HANDLE_VALUE)245return null;246return data;247} finally {248buffer.release();249}250}251static class FirstStream {252private long handle;253private String name;254255private FirstStream() { }256public long handle() { return handle; }257public String name() { return name; }258}259private static native void FindFirstStream0(long lpFileName, FirstStream obj)260throws WindowsException;261262/*263* FindNextStreamW(264* HANDLE hFindStream,265* LPVOID lpFindStreamData266* )267*/268static native String FindNextStream(long handle) throws WindowsException;269270/**271* FindClose(272* HANDLE hFindFile273* )274*/275static native void FindClose(long handle) throws WindowsException;276277/**278* GetFileInformationByHandle(279* HANDLE hFile,280* LPBY_HANDLE_FILE_INFORMATION lpFileInformation281* )282*/283static native void GetFileInformationByHandle(long handle, long address)284throws WindowsException;285286/**287* CopyFileEx(288* LPCWSTR lpExistingFileName289* LPCWSTR lpNewFileName,290* LPPROGRESS_ROUTINE lpProgressRoutine291* LPVOID lpData,292* LPBOOL pbCancel,293* DWORD dwCopyFlags294* )295*/296static void CopyFileEx(String source, String target, int flags,297long addressToPollForCancel)298throws WindowsException299{300NativeBuffer sourceBuffer = asNativeBuffer(source);301NativeBuffer targetBuffer = asNativeBuffer(target);302try {303CopyFileEx0(sourceBuffer.address(), targetBuffer.address(), flags,304addressToPollForCancel);305} finally {306targetBuffer.release();307sourceBuffer.release();308}309}310private static native void CopyFileEx0(long existingAddress, long newAddress,311int flags, long addressToPollForCancel) throws WindowsException;312313/**314* MoveFileEx(315* LPCTSTR lpExistingFileName,316* LPCTSTR lpNewFileName,317* DWORD dwFlags318* )319*/320static void MoveFileEx(String source, String target, int flags)321throws WindowsException322{323NativeBuffer sourceBuffer = asNativeBuffer(source);324NativeBuffer targetBuffer = asNativeBuffer(target);325try {326MoveFileEx0(sourceBuffer.address(), targetBuffer.address(), flags);327} finally {328targetBuffer.release();329sourceBuffer.release();330}331}332private static native void MoveFileEx0(long existingAddress, long newAddress,333int flags) throws WindowsException;334335/**336* DWORD GetFileAttributes(337* LPCTSTR lpFileName338* )339*/340static int GetFileAttributes(String path) throws WindowsException {341NativeBuffer buffer = asNativeBuffer(path);342try {343return GetFileAttributes0(buffer.address());344} finally {345buffer.release();346}347}348private static native int GetFileAttributes0(long lpFileName)349throws WindowsException;350351/**352* SetFileAttributes(353* LPCTSTR lpFileName,354* DWORD dwFileAttributes355*/356static void SetFileAttributes(String path, int dwFileAttributes)357throws WindowsException358{359NativeBuffer buffer = asNativeBuffer(path);360try {361SetFileAttributes0(buffer.address(), dwFileAttributes);362} finally {363buffer.release();364}365}366private static native void SetFileAttributes0(long lpFileName,367int dwFileAttributes) throws WindowsException;368369/**370* GetFileAttributesEx(371* LPCTSTR lpFileName,372* GET_FILEEX_INFO_LEVELS fInfoLevelId,373* LPVOID lpFileInformation374* );375*/376static void GetFileAttributesEx(String path, long address) throws WindowsException {377NativeBuffer buffer = asNativeBuffer(path);378try {379GetFileAttributesEx0(buffer.address(), address);380} finally {381buffer.release();382}383}384private static native void GetFileAttributesEx0(long lpFileName, long address)385throws WindowsException;386/**387* SetFileTime(388* HANDLE hFile,389* CONST FILETIME *lpCreationTime,390* CONST FILETIME *lpLastAccessTime,391* CONST FILETIME *lpLastWriteTime392* )393*/394static native void SetFileTime(long handle,395long createTime,396long lastAccessTime,397long lastWriteTime)398throws WindowsException;399400/**401* SetEndOfFile(402* HANDLE hFile403* )404*/405static native void SetEndOfFile(long handle) throws WindowsException;406407/**408* DWORD GetLogicalDrives(VOID)409*/410static native int GetLogicalDrives() throws WindowsException;411412/**413* GetVolumeInformation(414* LPCTSTR lpRootPathName,415* LPTSTR lpVolumeNameBuffer,416* DWORD nVolumeNameSize,417* LPDWORD lpVolumeSerialNumber,418* LPDWORD lpMaximumComponentLength,419* LPDWORD lpFileSystemFlags,420* LPTSTR lpFileSystemNameBuffer,421* DWORD nFileSystemNameSize422* )423*/424static VolumeInformation GetVolumeInformation(String root)425throws WindowsException426{427NativeBuffer buffer = asNativeBuffer(root);428try {429VolumeInformation info = new VolumeInformation();430GetVolumeInformation0(buffer.address(), info);431return info;432} finally {433buffer.release();434}435}436static class VolumeInformation {437private String fileSystemName;438private String volumeName;439private int volumeSerialNumber;440private int flags;441private VolumeInformation() { }442443public String fileSystemName() { return fileSystemName; }444public String volumeName() { return volumeName; }445public int volumeSerialNumber() { return volumeSerialNumber; }446public int flags() { return flags; }447}448private static native void GetVolumeInformation0(long lpRoot,449VolumeInformation obj)450throws WindowsException;451452/**453* UINT GetDriveType(454* LPCTSTR lpRootPathName455* )456*/457static int GetDriveType(String root) throws WindowsException {458NativeBuffer buffer = asNativeBuffer(root);459try {460return GetDriveType0(buffer.address());461} finally {462buffer.release();463}464}465private static native int GetDriveType0(long lpRoot) throws WindowsException;466467/**468* GetDiskFreeSpaceEx(469* LPCTSTR lpDirectoryName,470* PULARGE_INTEGER lpFreeBytesAvailableToCaller,471* PULARGE_INTEGER lpTotalNumberOfBytes,472* PULARGE_INTEGER lpTotalNumberOfFreeBytes473* )474*/475static DiskFreeSpace GetDiskFreeSpaceEx(String path)476throws WindowsException477{478NativeBuffer buffer = asNativeBuffer(path);479try {480DiskFreeSpace space = new DiskFreeSpace();481GetDiskFreeSpaceEx0(buffer.address(), space);482return space;483} finally {484buffer.release();485}486}487static class DiskFreeSpace {488private long freeBytesAvailable;489private long totalNumberOfBytes;490private long totalNumberOfFreeBytes;491private DiskFreeSpace() { }492493public long freeBytesAvailable() { return freeBytesAvailable; }494public long totalNumberOfBytes() { return totalNumberOfBytes; }495public long totalNumberOfFreeBytes() { return totalNumberOfFreeBytes; }496}497private static native void GetDiskFreeSpaceEx0(long lpDirectoryName,498DiskFreeSpace obj)499throws WindowsException;500501502/**503* GetVolumePathName(504* LPCTSTR lpszFileName,505* LPTSTR lpszVolumePathName,506* DWORD cchBufferLength507* )508*509* @return lpFileName510*/511static String GetVolumePathName(String path) throws WindowsException {512NativeBuffer buffer = asNativeBuffer(path);513try {514return GetVolumePathName0(buffer.address());515} finally {516buffer.release();517}518}519private static native String GetVolumePathName0(long lpFileName)520throws WindowsException;521522523/**524* InitializeSecurityDescriptor(525* PSECURITY_DESCRIPTOR pSecurityDescriptor,526* DWORD dwRevision527* )528*/529static native void InitializeSecurityDescriptor(long sdAddress)530throws WindowsException;531532/**533* InitializeAcl(534* PACL pAcl,535* DWORD nAclLength,536* DWORD dwAclRevision537* )538*/539static native void InitializeAcl(long aclAddress, int size)540throws WindowsException;541542/**543* GetFileSecurity(544* LPCTSTR lpFileName,545* SECURITY_INFORMATION RequestedInformation,546* PSECURITY_DESCRIPTOR pSecurityDescriptor,547* DWORD nLength,548* LPDWORD lpnLengthNeeded549* )550*/551static int GetFileSecurity(String path,552int requestedInformation,553long pSecurityDescriptor,554int nLength) throws WindowsException555{556NativeBuffer buffer = asNativeBuffer(path);557try {558return GetFileSecurity0(buffer.address(), requestedInformation,559pSecurityDescriptor, nLength);560} finally {561buffer.release();562}563}564private static native int GetFileSecurity0(long lpFileName,565int requestedInformation,566long pSecurityDescriptor,567int nLength) throws WindowsException;568569/**570* SetFileSecurity(571* LPCTSTR lpFileName,572* SECURITY_INFORMATION SecurityInformation,573* PSECURITY_DESCRIPTOR pSecurityDescriptor574* )575*/576static void SetFileSecurity(String path,577int securityInformation,578long pSecurityDescriptor)579throws WindowsException580{581NativeBuffer buffer = asNativeBuffer(path);582try {583SetFileSecurity0(buffer.address(), securityInformation,584pSecurityDescriptor);585} finally {586buffer.release();587}588}589static native void SetFileSecurity0(long lpFileName, int securityInformation,590long pSecurityDescriptor) throws WindowsException;591592/**593* GetSecurityDescriptorOwner(594* PSECURITY_DESCRIPTOR pSecurityDescriptor595* PSID *pOwner,596* LPBOOL lpbOwnerDefaulted597* )598*599* @return pOwner600*/601static native long GetSecurityDescriptorOwner(long pSecurityDescriptor)602throws WindowsException;603604/**605* SetSecurityDescriptorOwner(606* PSECURITY_DESCRIPTOR pSecurityDescriptor,607* PSID pOwner,608* BOOL bOwnerDefaulted609* )610*/611static native void SetSecurityDescriptorOwner(long pSecurityDescriptor,612long pOwner)613throws WindowsException;614615/**616* GetSecurityDescriptorDacl(617* PSECURITY_DESCRIPTOR pSecurityDescriptor,618* LPBOOL lpbDaclPresent,619* PACL *pDacl,620* LPBOOL lpbDaclDefaulted621* )622*/623static native long GetSecurityDescriptorDacl(long pSecurityDescriptor);624625/**626* SetSecurityDescriptorDacl(627* PSECURITY_DESCRIPTOR pSecurityDescriptor,628* BOOL bDaclPresent,629* PACL pDacl,630* BOOL bDaclDefaulted631* )632*/633static native void SetSecurityDescriptorDacl(long pSecurityDescriptor, long pAcl)634throws WindowsException;635636637/**638* GetAclInformation(639* PACL pAcl,640* LPVOID pAclInformation,641* DWORD nAclInformationLength,642* ACL_INFORMATION_CLASS dwAclInformationClass643* )644*/645static AclInformation GetAclInformation(long aclAddress) {646AclInformation info = new AclInformation();647GetAclInformation0(aclAddress, info);648return info;649}650static class AclInformation {651private int aceCount;652private AclInformation() { }653654public int aceCount() { return aceCount; }655}656private static native void GetAclInformation0(long aclAddress,657AclInformation obj);658659/**660* GetAce(661* PACL pAcl,662* DWORD dwAceIndex,663* LPVOID *pAce664* )665*/666static native long GetAce(long aclAddress, int aceIndex);667668/**669* AddAccessAllowedAceEx(670* PACL pAcl,671* DWORD dwAceRevision,672* DWORD AceFlags,673* DWORD AccessMask,674* PSID pSid675* )676*/677static native void AddAccessAllowedAceEx(long aclAddress, int flags,678int mask, long sidAddress) throws WindowsException;679680/**681* AddAccessDeniedAceEx(682* PACL pAcl,683* DWORD dwAceRevision,684* DWORD AceFlags,685* DWORD AccessMask,686* PSID pSid687* )688*/689static native void AddAccessDeniedAceEx(long aclAddress, int flags,690int mask, long sidAddress) throws WindowsException;691692/**693* LookupAccountSid(694* LPCTSTR lpSystemName,695* PSID Sid,696* LPTSTR Name,697* LPDWORD cbName,698* LPTSTR ReferencedDomainName,699* LPDWORD cbReferencedDomainName,700* PSID_NAME_USE peUse701* )702*/703static Account LookupAccountSid(long sidAddress) throws WindowsException {704Account acc = new Account();705LookupAccountSid0(sidAddress, acc);706return acc;707}708static class Account {709private String domain;710private String name;711private int use;712private Account() { }713714public String domain() { return domain; }715public String name() { return name; }716public int use() { return use; }717}718private static native void LookupAccountSid0(long sidAddress, Account obj)719throws WindowsException;720721/**722* LookupAccountName(723* LPCTSTR lpSystemName,724* LPCTSTR lpAccountName,725* PSID Sid,726* LPDWORD cbSid,727* LPTSTR ReferencedDomainName,728* LPDWORD cbReferencedDomainName,729* PSID_NAME_USE peUse730* )731*732* @return cbSid733*/734static int LookupAccountName(String accountName,735long pSid,736int cbSid) throws WindowsException737{738NativeBuffer buffer = asNativeBuffer(accountName);739try {740return LookupAccountName0(buffer.address(), pSid, cbSid);741} finally {742buffer.release();743}744}745private static native int LookupAccountName0(long lpAccountName, long pSid,746int cbSid) throws WindowsException;747748/**749* DWORD GetLengthSid(750* PSID pSid751* )752*/753static native int GetLengthSid(long sidAddress);754755/**756* ConvertSidToStringSid(757* PSID Sid,758* LPTSTR* StringSid759* )760*761* @return StringSid762*/763static native String ConvertSidToStringSid(long sidAddress)764throws WindowsException;765766/**767* ConvertStringSidToSid(768* LPCTSTR StringSid,769* PSID* pSid770* )771*772* @return pSid773*/774static long ConvertStringSidToSid(String sidString)775throws WindowsException776{777NativeBuffer buffer = asNativeBuffer(sidString);778try {779return ConvertStringSidToSid0(buffer.address());780} finally {781buffer.release();782}783}784private static native long ConvertStringSidToSid0(long lpStringSid)785throws WindowsException;786787/**788* HANDLE GetCurrentProcess(VOID)789*/790static native long GetCurrentProcess();791792/**793* HANDLE GetCurrentThread(VOID)794*/795static native long GetCurrentThread();796797/**798* OpenProcessToken(799* HANDLE ProcessHandle,800* DWORD DesiredAccess,801* PHANDLE TokenHandle802* )803*/804static native long OpenProcessToken(long hProcess, int desiredAccess)805throws WindowsException;806807/**808* OpenThreadToken(809* HANDLE ThreadHandle,810* DWORD DesiredAccess,811* BOOL OpenAsSelf,812* PHANDLE TokenHandle813* )814*/815static native long OpenThreadToken(long hThread, int desiredAccess,816boolean openAsSelf) throws WindowsException;817818/**819*/820static native long DuplicateTokenEx(long hThread, int desiredAccess)821throws WindowsException;822823/**824* SetThreadToken(825* PHANDLE Thread,826* HANDLE Token827* )828*/829static native void SetThreadToken(long thread, long hToken)830throws WindowsException;831832/**833* GetTokenInformation(834* HANDLE TokenHandle,835* TOKEN_INFORMATION_CLASS TokenInformationClass,836* LPVOID TokenInformation,837* DWORD TokenInformationLength,838* PDWORD ReturnLength839* )840*/841static native int GetTokenInformation(long token, int tokenInfoClass,842long pTokenInfo, int tokenInfoLength) throws WindowsException;843844/**845* AdjustTokenPrivileges(846* HANDLE TokenHandle,847* BOOL DisableAllPrivileges848* PTOKEN_PRIVILEGES NewState849* DWORD BufferLength850* PTOKEN_PRIVILEGES851* PDWORD ReturnLength852* )853*/854static native void AdjustTokenPrivileges(long token, long luid, int attributes)855throws WindowsException;856857858/**859* AccessCheck(860* PSECURITY_DESCRIPTOR pSecurityDescriptor,861* HANDLE ClientToken,862* DWORD DesiredAccess,863* PGENERIC_MAPPING GenericMapping,864* PPRIVILEGE_SET PrivilegeSet,865* LPDWORD PrivilegeSetLength,866* LPDWORD GrantedAccess,867* LPBOOL AccessStatus868* )869*/870static native boolean AccessCheck(long token, long securityInfo, int accessMask,871int genericRead, int genericWrite, int genericExecute, int genericAll)872throws WindowsException;873874/**875*/876static long LookupPrivilegeValue(String name) throws WindowsException {877NativeBuffer buffer = asNativeBuffer(name);878try {879return LookupPrivilegeValue0(buffer.address());880} finally {881buffer.release();882}883}884private static native long LookupPrivilegeValue0(long lpName)885throws WindowsException;886887/**888* CreateSymbolicLink(889* LPCWSTR lpSymlinkFileName,890* LPCWSTR lpTargetFileName,891* DWORD dwFlags892* )893*/894static void CreateSymbolicLink(String link, String target, int flags)895throws WindowsException896{897NativeBuffer linkBuffer = asNativeBuffer(link);898NativeBuffer targetBuffer = asNativeBuffer(target);899try {900CreateSymbolicLink0(linkBuffer.address(), targetBuffer.address(),901flags);902} finally {903targetBuffer.release();904linkBuffer.release();905}906}907private static native void CreateSymbolicLink0(long linkAddress,908long targetAddress, int flags) throws WindowsException;909910/**911* CreateHardLink(912* LPCTSTR lpFileName,913* LPCTSTR lpExistingFileName,914* LPSECURITY_ATTRIBUTES lpSecurityAttributes915* )916*/917static void CreateHardLink(String newFile, String existingFile)918throws WindowsException919{920NativeBuffer newFileBuffer = asNativeBuffer(newFile);921NativeBuffer existingFileBuffer = asNativeBuffer(existingFile);922try {923CreateHardLink0(newFileBuffer.address(), existingFileBuffer.address());924} finally {925existingFileBuffer.release();926newFileBuffer.release();927}928}929private static native void CreateHardLink0(long newFileBuffer,930long existingFiletBuffer) throws WindowsException;931932/**933* GetFullPathName(934* LPCTSTR lpFileName,935* DWORD nBufferLength,936* LPTSTR lpBuffer,937* LPTSTR *lpFilePart938* )939*/940static String GetFullPathName(String path) throws WindowsException {941NativeBuffer buffer = asNativeBuffer(path);942try {943return GetFullPathName0(buffer.address());944} finally {945buffer.release();946}947}948private static native String GetFullPathName0(long pathAddress)949throws WindowsException;950951/**952* GetFinalPathNameByHandle(953* HANDLE hFile,954* LPTSTR lpszFilePath,955* DWORD cchFilePath,956* DWORD dwFlags957* )958*/959static native String GetFinalPathNameByHandle(long handle)960throws WindowsException;961962/**963* FormatMessage(964* DWORD dwFlags,965* LPCVOID lpSource,966* DWORD dwMessageId,967* DWORD dwLanguageId,968* LPTSTR lpBuffer,969* DWORD nSize,970* va_list *Arguments971* )972*/973static native String FormatMessage(int errorCode);974975/**976* LocalFree(977* HLOCAL hMem978* )979*/980static native void LocalFree(long address);981982/**983* HANDLE CreateIoCompletionPort (984* HANDLE FileHandle,985* HANDLE ExistingCompletionPort,986* ULONG_PTR CompletionKey,987* DWORD NumberOfConcurrentThreads988* )989*/990static native long CreateIoCompletionPort(long fileHandle, long existingPort,991long completionKey) throws WindowsException;992993994/**995* GetQueuedCompletionStatus(996* HANDLE CompletionPort,997* LPDWORD lpNumberOfBytesTransferred,998* PULONG_PTR lpCompletionKey,999* LPOVERLAPPED *lpOverlapped,1000* DWORD dwMilliseconds1001*/1002static CompletionStatus GetQueuedCompletionStatus(long completionPort)1003throws WindowsException1004{1005CompletionStatus status = new CompletionStatus();1006GetQueuedCompletionStatus0(completionPort, status);1007return status;1008}1009static class CompletionStatus {1010private int error;1011private int bytesTransferred;1012private long completionKey;1013private CompletionStatus() { }10141015int error() { return error; }1016int bytesTransferred() { return bytesTransferred; }1017long completionKey() { return completionKey; }1018}1019private static native void GetQueuedCompletionStatus0(long completionPort,1020CompletionStatus status) throws WindowsException;10211022/**1023* PostQueuedCompletionStatus(1024* HANDLE CompletionPort,1025* DWORD dwNumberOfBytesTransferred,1026* ULONG_PTR dwCompletionKey,1027* LPOVERLAPPED lpOverlapped1028* )1029*/1030static native void PostQueuedCompletionStatus(long completionPort,1031long completionKey) throws WindowsException;10321033/**1034* ReadDirectoryChangesW(1035* HANDLE hDirectory,1036* LPVOID lpBuffer,1037* DWORD nBufferLength,1038* BOOL bWatchSubtree,1039* DWORD dwNotifyFilter,1040* LPDWORD lpBytesReturned,1041* LPOVERLAPPED lpOverlapped,1042* LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine1043* )1044*/1045static native void ReadDirectoryChangesW(long hDirectory,1046long bufferAddress,1047int bufferLength,1048boolean watchSubTree,1049int filter,1050long bytesReturnedAddress,1051long pOverlapped)1052throws WindowsException;105310541055/**1056* CancelIo(1057* HANDLE hFile1058* )1059*/1060static native void CancelIo(long hFile) throws WindowsException;10611062/**1063* GetOverlappedResult(1064* HANDLE hFile,1065* LPOVERLAPPED lpOverlapped,1066* LPDWORD lpNumberOfBytesTransferred,1067* BOOL bWait1068* );1069*/1070static native int GetOverlappedResult(long hFile, long lpOverlapped)1071throws WindowsException;10721073/**1074* BackupRead(1075* HANDLE hFile,1076* LPBYTE lpBuffer,1077* DWORD nNumberOfBytesToRead,1078* LPDWORD lpNumberOfBytesRead,1079* BOOL bAbort,1080* BOOL bProcessSecurity,1081* LPVOID* lpContext1082* )1083*/1084static BackupResult BackupRead(long hFile,1085long bufferAddress,1086int bufferSize,1087boolean abort,1088long context)1089throws WindowsException1090{1091BackupResult result = new BackupResult();1092BackupRead0(hFile, bufferAddress, bufferSize, abort, context, result);1093return result;1094}1095static class BackupResult {1096private int bytesTransferred;1097private long context;1098private BackupResult() { }10991100int bytesTransferred() { return bytesTransferred; }1101long context() { return context; }1102}1103private static native void BackupRead0(long hFile, long bufferAddress,1104int bufferSize, boolean abort, long context, BackupResult result)1105throws WindowsException;11061107/**1108* BackupSeek(1109* HANDLE hFile,1110* DWORD dwLowBytesToSeek,1111* DWORD dwHighBytesToSeek,1112* LPDWORD lpdwLowByteSeeked,1113* LPDWORD lpdwHighByteSeeked,1114* LPVOID* lpContext1115* )1116*/1117static native void BackupSeek(long hFile, long bytesToSeek, long context)1118throws WindowsException;111911201121// -- support for copying String with a NativeBuffer --11221123private static final Unsafe unsafe = Unsafe.getUnsafe();11241125static NativeBuffer asNativeBuffer(String s) throws WindowsException {1126if (s.length() > (Integer.MAX_VALUE - 2)/2) {1127throw new WindowsException1128("String too long to convert to native buffer");1129}11301131int stringLengthInBytes = s.length() << 1;1132int sizeInBytes = stringLengthInBytes + 2; // char terminator11331134// get a native buffer of sufficient size1135NativeBuffer buffer = NativeBuffers.getNativeBufferFromCache(sizeInBytes);1136if (buffer == null) {1137buffer = NativeBuffers.allocNativeBuffer(sizeInBytes);1138} else {1139// buffer already contains the string contents1140if (buffer.owner() == s)1141return buffer;1142}11431144// copy into buffer and zero terminate1145char[] chars = s.toCharArray();1146unsafe.copyMemory(chars, Unsafe.ARRAY_CHAR_BASE_OFFSET, null,1147buffer.address(), (long)stringLengthInBytes);1148unsafe.putChar(buffer.address() + stringLengthInBytes, (char)0);1149buffer.setOwner(s);1150return buffer;1151}11521153// -- native library initialization --11541155private static native void initIDs();11561157static {1158AccessController.doPrivileged(new PrivilegedAction<Void>() {1159public Void run() {1160// nio.dll has dependency on net.dll1161System.loadLibrary("net");1162System.loadLibrary("nio");1163return null;1164}});1165initIDs();1166}11671168}116911701171