test/jdk/java/nio/file/attribute/BasicFileAttributeView/CreationTime.java
author bpb
Wed, 25 Sep 2019 11:44:52 -0700
changeset 58341 21a03fa2f6b6
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231174: (fs) FileTime should have 100ns resolution (win) Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17154
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
     1
/*
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
     4
 *
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
     8
 *
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    13
 * accompanied this code).
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    14
 *
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    18
 *
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    21
 * questions.
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    22
 */
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    23
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    24
/* @test
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    25
 * @bug 8011536
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    26
 * @summary Basic test for creationTime attribute on platforms/file systems
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    27
 *     that support it.
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    28
 * @library ../..
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    29
 */
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    30
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    31
import java.nio.file.Path;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    32
import java.nio.file.Files;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    33
import java.nio.file.attribute.*;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    34
import java.time.Instant;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    35
import java.io.IOException;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    36
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    37
public class CreationTime {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    38
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    39
    private static final java.io.PrintStream err = System.err;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    40
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    41
    /**
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    42
     * Reads the creationTime attribute
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    43
     */
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    44
    private static FileTime creationTime(Path file) throws IOException {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    45
        return Files.readAttributes(file, BasicFileAttributes.class).creationTime();
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    46
    }
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    47
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    48
    /**
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    49
     * Sets the creationTime attribute
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    50
     */
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    51
    private static void setCreationTime(Path file, FileTime time) throws IOException {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    52
        BasicFileAttributeView view =
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    53
            Files.getFileAttributeView(file, BasicFileAttributeView.class);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    54
        view.setTimes(null, null, time);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    55
    }
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    56
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    57
    static void test(Path top) throws IOException {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    58
        Path file = Files.createFile(top.resolve("foo"));
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    59
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    60
        /**
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    61
         * Check that creationTime reported
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    62
         */
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    63
        FileTime creationTime = creationTime(file);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    64
        Instant now = Instant.now();
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    65
        if (Math.abs(creationTime.toMillis()-now.toEpochMilli()) > 10000L) {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    66
            err.println("File creation time reported as: " + creationTime);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    67
            throw new RuntimeException("Expected to be close to: " + now);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    68
        }
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    69
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    70
        /**
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    71
         * Is the creationTime attribute supported here?
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    72
         */
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    73
        boolean supportsCreationTimeRead = false;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    74
        boolean supportsCreationTimeWrite = false;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    75
        String os = System.getProperty("os.name");
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    76
        if (os.contains("OS X") && Files.getFileStore(file).type().equals("hfs")) {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    77
            supportsCreationTimeRead = true;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    78
        } else if (os.startsWith("Windows")) {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    79
            String type = Files.getFileStore(file).type();
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    80
            if (type.equals("NTFS") || type.equals("FAT")) {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    81
                supportsCreationTimeRead = true;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    82
                supportsCreationTimeWrite = true;
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    83
            }
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    84
        }
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    85
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    86
        /**
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    87
         * If the creation-time attribute is supported then change the file's
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    88
         * last modified and check that it doesn't change the creation-time.
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    89
         */
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    90
        if (supportsCreationTimeRead) {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    91
            // change modified time by +1 hour
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    92
            Instant plusHour = Instant.now().plusSeconds(60L * 60L);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    93
            Files.setLastModifiedTime(file, FileTime.from(plusHour));
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    94
            FileTime current = creationTime(file);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    95
            if (!current.equals(creationTime))
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    96
                throw new RuntimeException("Creation time should not have changed");
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    97
        }
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    98
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
    99
        /**
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   100
         * If the creation-time attribute is supported and can be changed then
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   101
         * check that the change is effective.
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   102
         */
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   103
        if (supportsCreationTimeWrite) {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   104
            // change creation time by -1 hour
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   105
            Instant minusHour = Instant.now().minusSeconds(60L * 60L);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   106
            creationTime = FileTime.from(minusHour);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   107
            setCreationTime(file, creationTime);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   108
            FileTime current = creationTime(file);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   109
            if (Math.abs(creationTime.toMillis()-current.toMillis()) > 1000L)
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   110
                throw new RuntimeException("Creation time not changed");
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   111
        }
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   112
    }
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   113
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   114
    public static void main(String[] args) throws IOException {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   115
        // create temporary directory to run tests
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   116
        Path dir = TestUtil.createTemporaryDirectory();
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   117
        try {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   118
            test(dir);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   119
        } finally {
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   120
            TestUtil.removeAll(dir);
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   121
        }
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   122
    }
e8991539c4d7 8011536: (fs) BasicFileAttributes.creationTime() should return birth time (mac)
alanb
parents:
diff changeset
   123
}