test/jdk/java/nio/channels/FileChannel/BlockDeviceSize.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26618
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
     1
/*
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
     4
 *
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
     8
 *
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    13
 * accompanied this code).
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    14
 *
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    18
 *
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    21
 * questions.
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    22
 */
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    23
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    24
/* @test
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    25
 * @bug 8054029
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    26
 * @summary Block devices should not report size=0 on Linux
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    27
 */
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    28
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    29
import java.io.RandomAccessFile;
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    30
import java.nio.file.Path;
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    31
import java.nio.file.Paths;
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    32
import java.nio.channels.FileChannel;
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    33
import java.nio.file.AccessDeniedException;
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    34
import java.nio.file.NoSuchFileException;
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    35
import static java.nio.file.StandardOpenOption.*;
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    36
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    37
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    38
public class BlockDeviceSize {
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    39
    private static final String BLK_FNAME = "/dev/sda1";
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    40
    private static final Path BLK_PATH = Paths.get(BLK_FNAME);
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    41
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    42
    public static void main(String[] args) throws Throwable {
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    43
        try (FileChannel ch = FileChannel.open(BLK_PATH, READ);
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    44
             RandomAccessFile file = new RandomAccessFile(BLK_FNAME, "r")) {
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    45
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    46
            long size1 = ch.size();
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    47
            long size2 = file.length();
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    48
            if (size1 != size2) {
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    49
                throw new RuntimeException("size differs when retrieved" +
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    50
                        " in different ways: " + size1 + " != " + size2);
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    51
            }
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    52
            System.out.println("OK");
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    53
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    54
        } catch (NoSuchFileException nsfe) {
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    55
            System.err.println("File " + BLK_FNAME + " not found." +
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    56
                    " Skipping test");
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    57
        } catch (AccessDeniedException ade) {
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    58
            System.err.println("Access to " + BLK_FNAME + " is denied." +
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    59
                    " Run test as root.");
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    60
        }
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    61
    }
f30d461d3162 8054029: (fc) FileChannel.size() returns 0 for block devices on Linux
igerasim
parents:
diff changeset
    62
}