335 // SID_AND_ATTRIBUTES->pSid 336 long pSid = unsafe.getAddress(userBuffer.address()); 337 long pTrustee = BuildTrusteeWithSid(pSid); 338 try { 339 return GetEffectiveRightsFromAcl(pAcl, pTrustee); 340 } finally { 341 LocalFree(pTrustee); 342 } 343 } catch (WindowsException x) { 344 throw new IOException("Unable to get effective rights from ACL: " + 345 x.getMessage()); 346 } 347 } finally { 348 userBuffer.release(); 349 } 350 } finally { 351 aclBuffer.release(); 352 } 353 } 354 355 @Override 356 public void checkAccess(Path obj, AccessMode... modes) throws IOException { 357 WindowsPath file = WindowsPath.toWindowsPath(obj); 358 // if no access modes then simply file attributes 359 if (modes.length == 0) { 360 file.checkRead(); 361 try { 362 WindowsFileAttributes.get(file, true); 363 } catch (WindowsException exc) { 364 exc.rethrowAsIOException(file); 365 } 366 return; 367 } 368 369 boolean r = false; 370 boolean w = false; 371 boolean x = false; 372 for (AccessMode mode: modes) { 373 switch (mode) { 374 case READ : r = true; break; 375 case WRITE : w = true; break; 376 case EXECUTE : x = true; break; 377 default: throw new AssertionError("Should not get here"); 378 } 379 } 380 381 int mask = 0; 382 if (r) { 383 file.checkRead(); 384 mask |= FILE_READ_DATA; 385 } 386 if (w) { 387 file.checkWrite(); 388 mask |= FILE_WRITE_DATA; 389 } 390 if (x) { 391 SecurityManager sm = System.getSecurityManager(); 392 if (sm != null) 393 sm.checkExec(file.getPathForPermissionCheck()); 394 mask |= FILE_EXECUTE; 395 } 396 397 if ((getEffectiveAccess(file) & mask) == 0) 398 throw new AccessDeniedException( 399 file.getPathForExceptionMessage(), null, 400 "Effective permissions does not allow requested access"); | 335 // SID_AND_ATTRIBUTES->pSid 336 long pSid = unsafe.getAddress(userBuffer.address()); 337 long pTrustee = BuildTrusteeWithSid(pSid); 338 try { 339 return GetEffectiveRightsFromAcl(pAcl, pTrustee); 340 } finally { 341 LocalFree(pTrustee); 342 } 343 } catch (WindowsException x) { 344 throw new IOException("Unable to get effective rights from ACL: " + 345 x.getMessage()); 346 } 347 } finally { 348 userBuffer.release(); 349 } 350 } finally { 351 aclBuffer.release(); 352 } 353 } 354 355 /** 356 * Checks if the given file(or directory) exists and is readable. 357 */ 358 private void checkReadAccess(WindowsPath file) throws IOException { 359 try { 360 Set<OpenOption> opts = Collections.emptySet(); 361 FileChannel fc = WindowsChannelFactory 362 .newFileChannel(file.getPathForWin32Calls(), 363 file.getPathForPermissionCheck(), 364 opts, 365 0L); 366 fc.close(); 367 } catch (WindowsException exc) { 368 // Windows errors are very inconsistent when the file is a directory 369 // (ERROR_PATH_NOT_FOUND returned for root directories for example) 370 // so we retry by attempting to open it as a directory. 371 try { 372 new WindowsDirectoryStream(file, null).close(); 373 } catch (IOException ioe) { 374 // translate and throw original exception 375 exc.rethrowAsIOException(file); 376 } 377 } 378 } 379 380 @Override 381 public void checkAccess(Path obj, AccessMode... modes) throws IOException { 382 WindowsPath file = WindowsPath.toWindowsPath(obj); 383 384 boolean r = false; 385 boolean w = false; 386 boolean x = false; 387 for (AccessMode mode: modes) { 388 switch (mode) { 389 case READ : r = true; break; 390 case WRITE : w = true; break; 391 case EXECUTE : x = true; break; 392 default: throw new AssertionError("Should not get here"); 393 } 394 } 395 396 // special-case read access to avoid needing to determine effective 397 // access to file; default if modes not specified 398 if (!w && !x) { 399 checkReadAccess(file); 400 return; 401 } 402 403 int mask = 0; 404 if (r) { 405 file.checkRead(); 406 mask |= FILE_READ_DATA; 407 } 408 if (w) { 409 file.checkWrite(); 410 mask |= FILE_WRITE_DATA; 411 } 412 if (x) { 413 SecurityManager sm = System.getSecurityManager(); 414 if (sm != null) 415 sm.checkExec(file.getPathForPermissionCheck()); 416 mask |= FILE_EXECUTE; 417 } 418 419 if ((getEffectiveAccess(file) & mask) == 0) 420 throw new AccessDeniedException( 421 file.getPathForExceptionMessage(), null, 422 "Effective permissions does not allow requested access"); |