Skip to content

Commit

Permalink
8264400: (fs) WindowsFileStore equality depends on how the FileStore …
Browse files Browse the repository at this point in the history
…was constructed

Backport-of: cc54de7
  • Loading branch information
GoeLin committed Jun 21, 2022
1 parent aa35f09 commit 97a472c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
36 changes: 27 additions & 9 deletions src/java.base/windows/classes/sun/nio/fs/WindowsFileStore.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2021, 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 @@ -25,9 +25,17 @@

package sun.nio.fs;

import java.nio.file.*;
import java.nio.file.attribute.*;
import java.nio.file.FileStore;
import java.nio.file.FileSystemException;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.FileAttributeView;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.FileStoreAttributeView;
import java.nio.file.attribute.UserDefinedFileAttributeView;
import java.io.IOException;
import java.util.Locale;

import static sun.nio.fs.WindowsConstants.*;
import static sun.nio.fs.WindowsNativeDispatcher.*;
Expand All @@ -44,6 +52,8 @@ class WindowsFileStore
private final int volType;
private final String displayName; // returned by toString

private int hashCode;

private WindowsFileStore(String root) throws WindowsException {
assert root.charAt(root.length()-1) == '\\';
this.root = root;
Expand Down Expand Up @@ -220,15 +230,23 @@ public boolean supportsFileAttributeView(String name) {
public boolean equals(Object ob) {
if (ob == this)
return true;
if (!(ob instanceof WindowsFileStore))
return false;
WindowsFileStore other = (WindowsFileStore)ob;
return root.equals(other.root);
if (ob instanceof WindowsFileStore) {
WindowsFileStore other = (WindowsFileStore)ob;
if (root.equals(other.root))
return true;
if (volType == DRIVE_FIXED && other.volumeType() == DRIVE_FIXED)
return root.equalsIgnoreCase(other.root);
}
return false;
}

@Override
public int hashCode() {
return root.hashCode();
if (hashCode == 0) { // Don't care about race
hashCode = (volType == DRIVE_FIXED) ?
root.toLowerCase(Locale.ROOT).hashCode() : root.hashCode();
}
return hashCode;
}

@Override
Expand All @@ -242,4 +260,4 @@ public String toString() {
sb.append(")");
return sb.toString();
}
}
}
15 changes: 12 additions & 3 deletions test/jdk/java/nio/file/FileStore/Basic.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2021, 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,7 +22,7 @@
*/

/* @test
* @bug 4313887 6873621 6979526 7006126 7020517
* @bug 4313887 6873621 6979526 7006126 7020517 8264400
* @summary Unit test for java.nio.file.FileStore
* @key intermittent
* @library .. /test/lib
Expand All @@ -36,9 +36,9 @@
import java.io.File;
import java.io.IOException;

import jdk.test.lib.Platform;
import jdk.test.lib.util.FileUtils;


public class Basic {

static final long G = 1024L * 1024L * 1024L;
Expand Down Expand Up @@ -80,6 +80,15 @@ static void doTests(Path dir) throws IOException {
assertTrue(store2.equals(store1));
assertTrue(store1.hashCode() == store2.hashCode());

if (Platform.isWindows()) {
/**
* Test: FileStore.equals() should not be case sensitive
*/
FileStore upper = Files.getFileStore(Path.of("C:\\"));
FileStore lower = Files.getFileStore(Path.of("c:\\"));
assertTrue(lower.equals(upper));
}

/**
* Test: File and FileStore attributes
*/
Expand Down

1 comment on commit 97a472c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.