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

(fs) Add support for BasicFileAttributes.creationTime() for Linux

XMLWordPrintable

    • b20
    • generic
    • linux

        On Linux (kernel version 4.11 and better, glibc 2.28 and better) there is a Linux specific statx system call that supports reading the birth time of a file. We should add specific code on that platform to support reading the creation time for files on Linux. Windows and MacOSX already support this on the common filesystems.

        This code:

        import java.nio.file.*;
        import java.nio.file.attribute.BasicFileAttributes;

        public class LastModified {
            public static void main(String[] args) throws Exception {
                var attrs = Files.readAttributes(Path.of(args[0]), BasicFileAttributes.class);

                System.out.printf(
                    "created: %s\nmodified: %s\n%n",
                    attrs.creationTime(),
                    attrs.lastModifiedTime()
                );
            }
        }

        ... shows the last modified time as the creation time on Linux. Example sequence:

        $ echo testme > testing.txt
        $ touch -m -t 200001010101 testing.txt
        $ java LastModified testing.txt
        created: 2000-01-01T00:01:00Z
        modified: 2000-01-01T00:01:00Z
        $ stat testing.txt | grep Birth
         Birth: 2023-09-14 17:43:04.893739324 +0200

        The expected output of the above program would be something resembling the "Birth:" time as shown by the native 'stat' tool (which internally uses 'statx').

              sgehwolf Severin Gehwolf
              sgehwolf Severin Gehwolf
              Votes:
              0 Vote for this issue
              Watchers:
              11 Start watching this issue

                Created:
                Updated:
                Resolved: