diff --git a/src/java.base/windows/classes/sun/nio/fs/WindowsFileSystemProvider.java b/src/java.base/windows/classes/sun/nio/fs/WindowsFileSystemProvider.java index 84658566873..964e338466d 100644 --- a/src/java.base/windows/classes/sun/nio/fs/WindowsFileSystemProvider.java +++ b/src/java.base/windows/classes/sun/nio/fs/WindowsFileSystemProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -374,8 +374,19 @@ public void checkAccess(Path obj, AccessMode... modes) throws IOException { } } + // check file exists only + if (!(r || w || x)) { + file.checkRead(); + try { + WindowsFileAttributes.get(file, true); + return; + } catch (WindowsException exc) { + exc.rethrowAsIOException(file); + } + } + // special-case read access to avoid needing to determine effective - // access to file; default if modes not specified + // access to file if (!w && !x) { checkReadAccess(file); return; diff --git a/test/jdk/java/nio/file/Files/Misc.java b/test/jdk/java/nio/file/Files/Misc.java index 0bfc0c410dc..b02a39e98be 100644 --- a/test/jdk/java/nio/file/Files/Misc.java +++ b/test/jdk/java/nio/file/Files/Misc.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -22,17 +22,29 @@ */ /* @test - * @bug 4313887 6838333 8005566 8032220 8215467 8255576 + * @bug 4313887 6838333 8005566 8032220 8215467 8255576 8286160 * @summary Unit test for miscellenous methods in java.nio.file.Files - * @library .. + * @library .. /test/lib + * @build jdk.test.lib.Platform + * @run main Misc */ -import java.nio.file.*; +import java.io.IOException; +import java.io.File; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.AclEntry; +import java.nio.file.attribute.AclEntryPermission; +import java.nio.file.attribute.AclEntryType; +import java.nio.file.attribute.AclFileAttributeView; +import java.nio.file.attribute.DosFileAttributeView; +import java.nio.file.attribute.UserPrincipal; +import java.util.List; +import jdk.test.lib.Platform; + import static java.nio.file.Files.*; import static java.nio.file.LinkOption.*; -import java.nio.file.attribute.*; -import java.io.IOException; -import java.util.*; public class Misc { @@ -78,7 +90,7 @@ static void testCreateDirectories(Path tmpdir) throws IOException { } catch (IOException x) { } // the root directory always exists - Path root = Paths.get("/"); + Path root = Path.of("/"); Files.createDirectories(root); Files.createDirectories(root.toAbsolutePath()); } @@ -93,7 +105,7 @@ static void testIsHidden(Path tmpdir) throws IOException { assertTrue(!isHidden(tmpdir)); Path file = tmpdir.resolve(".foo"); - if (System.getProperty("os.name").startsWith("Windows")) { + if (Platform.isWindows()) { createFile(file); try { setAttribute(file, "dos:hidden", true); @@ -286,6 +298,13 @@ static void testAccessMethods(Path tmpdir) throws IOException { assertTrue(exists(tmpdir)); assertTrue(!notExists(tmpdir)); + if (Platform.isWindows()) { + Path pageFile = Path.of("C:\\pagefile.sys"); + if (pageFile.toFile().exists()) { + System.out.printf("Check page file %s%n", pageFile); + assertTrue(exists(pageFile)); + } + } // sym link exists if (TestUtil.supportsLinks(tmpdir)) { @@ -351,7 +370,7 @@ static void testAccessMethods(Path tmpdir) throws IOException { /** * Test: Windows DOS read-only attribute */ - if (System.getProperty("os.name").startsWith("Windows")) { + if (Platform.isWindows()) { setAttribute(file, "dos:readonly", true); try { assertTrue(!isWritable(file)); @@ -381,10 +400,10 @@ static void assertTrue(boolean okay) { } private static boolean isRoot() { - if (System.getProperty("os.name").startsWith("Windows")) + if (Platform.isWindows()) return false; - Path passwd = Paths.get("/etc/passwd"); + Path passwd = Path.of("/etc/passwd"); return Files.isWritable(passwd); } }