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

(fs) BasicFileAttributeView.setTimes uses microsecond precision with NOFOLLOW_LINKS

XMLWordPrintable

    • b24
    • x86_64
    • linux
    • Verified

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu Linux 24.04 LTS
      openjdk version "23" 2024-09-17
      OpenJDK Runtime Environment Corretto-23.0.0.37.1 (build 23+37-FR)
      OpenJDK 64-Bit Server VM Corretto-23.0.0.37.1 (build 23+37-FR, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      `BasicFileAttributeView#setTimes` supports nanosecond precision for regular files, but rounds timestamps to microseconds when applied to a symbolic links (or more precisely, when getFileAttributeView() is called with NOFOLLOW_LINKS).

      Consider the following example:
      ```
      var target = Path.of("target");
      var symlink = Path.of("symlink");
      Files.createFile(target);
      Files.createSymbolicLink(symlink, target);

      var newTime = FileTime.from(1730417633157646106L, NANOSECONDS);

      var symlinkView = Files.getFileAttributeView(symlink, BasicFileAttributeView.class, NOFOLLOW_LINKS);
      symlinkView.setTimes(newTime, newTime, null);
      var symlinkAttrs = symlinkView.readAttributes();
      System.out.println("Symlink: " + symlinkAttrs.lastAccessTime().to(NANOSECONDS)
              + " / " + symlinkAttrs.lastModifiedTime().to(NANOSECONDS));

      var targetView = Files.getFileAttributeView(symlink, BasicFileAttributeView.class);
      targetView.setTimes(newTime, newTime, null);
      var targetAttrs = targetView.readAttributes();
      System.out.println("Target: " + targetAttrs.lastAccessTime().to(NANOSECONDS) + " / " + targetAttrs.lastModifiedTime().to(NANOSECONDS));
      ```

      Outputs the following to the console:
      ```
      Symlink: 1730417633157646000 / 1730417633157646000
      Target: 1730417633157646106 / 1730417633157646106
      ```

      The reason for the lower precision on symbolic links is unclear, at least I can manually set the nanosecond timestamp using `touch`:
      ```
      $ touch -mhd "2024-10-31T23:33:53.157646106Z" symlink
      $ TZ=UTC stat -c %y symlink
      2024-10-31 23:33:53.157646106 +0000
      ```

      It would be nice if nanosecond precision were supported when using the NOFOLLOW_LINKS attribute as well.



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

              Created:
              Updated:
              Resolved: