Skip to content

Commit

Permalink
8286160: (fs) Files.exists returns unexpected results with C:\pagefil…
Browse files Browse the repository at this point in the history
…e.sys because it's not readable

Reviewed-by: alanb
  • Loading branch information
Brian Burkhalter committed Jun 9, 2022
1 parent edff51e commit d482d7f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
@@ -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
Expand Down Expand Up @@ -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;
Expand Down
43 changes: 31 additions & 12 deletions 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
Expand All @@ -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 {

Expand Down Expand Up @@ -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());
}
Expand All @@ -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);
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
}

0 comments on commit d482d7f

Please sign in to comment.