Skip to content
Snippets Groups Projects
Commit 698f0e37 authored by Amos Shi's avatar Amos Shi
Browse files

8313674: (fc) java/nio/channels/FileChannel/BlockDeviceSize.java should test for more block devices

Reviewed-by: mbaesken
Backport-of: e91492ab4333c61f39b50eb428fa932131a5b908
parent 25537e9c
No related branches found
No related tags found
No related merge requests found
/* /*
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -24,25 +24,27 @@ ...@@ -24,25 +24,27 @@
/* @test /* @test
* @bug 8054029 * @bug 8054029
* @requires (os.family == "linux") * @requires (os.family == "linux")
* @summary Block devices should not report size=0 on Linux * @summary FileChannel.size() should be equal to RandomAccessFile.size() and > 0 for block devs on Linux
*/ */
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.file.AccessDeniedException; import java.nio.file.AccessDeniedException;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.util.List;
import static java.nio.file.StandardOpenOption.*; import static java.nio.file.StandardOpenOption.*;
public class BlockDeviceSize { public class BlockDeviceSize {
private static final String BLK_FNAME = "/dev/sda1"; private static final List<String> BLK_FNAMES = List.of("/dev/sda1", "/dev/nvme0n1", "/dev/xvda1") ;
private static final Path BLK_PATH = Paths.get(BLK_FNAME);
public static void main(String[] args) throws Throwable { public static void main(String[] args) throws Throwable {
try (FileChannel ch = FileChannel.open(BLK_PATH, READ); for (String blkFname: BLK_FNAMES) {
RandomAccessFile file = new RandomAccessFile(BLK_FNAME, "r")) { Path blkPath = Path.of(blkFname);
try (FileChannel ch = FileChannel.open(blkPath, READ);
RandomAccessFile file = new RandomAccessFile(blkFname, "r")) {
long size1 = ch.size(); long size1 = ch.size();
long size2 = file.length(); long size2 = file.length();
...@@ -50,14 +52,19 @@ public class BlockDeviceSize { ...@@ -50,14 +52,19 @@ public class BlockDeviceSize {
throw new RuntimeException("size differs when retrieved" + throw new RuntimeException("size differs when retrieved" +
" in different ways: " + size1 + " != " + size2); " in different ways: " + size1 + " != " + size2);
} }
if (size1 <= 0) {
throw new RuntimeException("size() for a block device size returns zero or a negative value");
}
System.out.println("OK"); System.out.println("OK");
} catch (NoSuchFileException nsfe) { } catch (NoSuchFileException nsfe) {
System.err.println("File " + BLK_FNAME + " not found." + System.err.println("File " + blkFname + " not found." +
" Skipping test"); " Skipping test");
} catch (AccessDeniedException ade) { } catch (AccessDeniedException ade) {
System.err.println("Access to " + BLK_FNAME + " is denied." + throw new RuntimeException("Access to " + blkFname + " is denied."
" Run test as root."); + " Run test as root.", ade);
}
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment