Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8337275

Files.setTimes and creationTime APIs inconsistent

XMLWordPrintable

    • x86_64
    • linux_ubuntu

      ADDITIONAL SYSTEM INFORMATION :
      OS: Ubuntu 22.04
      JDK 17: openjdk version "17.0.11"
      JDK 8: openjdk version "1.8.0_412"

      A DESCRIPTION OF THE PROBLEM :
      I set the time attributes of a file to the same EPOCH values. When I read these attributes, the creation time and last modified time is not the same.

      I would except that these attributes are the same, even if creationTime is not supported on the specific OS.

      The behavior is different with Java 8 and Java 17. Java 8 works as expected.


      REGRESSION : Last worked in version 8

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      If I set a files time attributes to the same value and then read these attributes, I expect them to be the same.
      ACTUAL -
      Creation time differs from last modified time, but only with Java 17. Works as expected with Java 8.

      ---------- BEGIN SOURCE ----------
      package file.example;

      import java.io.File;
      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Paths;
      import java.nio.file.attribute.BasicFileAttributeView;
      import java.nio.file.attribute.BasicFileAttributes;
      import java.nio.file.attribute.FileTime;

      public class Main {

          private static final FileTime EPOCH = FileTime.fromMillis(0);

          public static void main(String[] args) {
              try {
                  File file = Paths.get("empty1.file").toFile();
                  Files.deleteIfExists(file.toPath());

                  if(!file.createNewFile()) {
                      throw new IOException("Could not create file " + file.getAbsolutePath());
                  }

                  Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class).setTimes(EPOCH, EPOCH, EPOCH);

                  FileTime creationTime = Files.readAttributes(file.toPath(), BasicFileAttributes.class).creationTime();
                  FileTime lastModifiedTime = Files.readAttributes(file.toPath(), BasicFileAttributes.class).lastModifiedTime();

                  // These times should be equal
                  // Works as expected with Java 8 but not with Java 17
                  System.out.println(creationTime);
                  System.out.println(lastModifiedTime);
                  if(!creationTime.equals(lastModifiedTime)) {
                      System.err.println("Creation and last modified time are not equal");
                  }
              } catch (IOException e) {
                  // This should not happen
                  System.err.println(e.getMessage());
              }
          }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: