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

(fs) Files::move with REPLACE_EXISTING does nothing for hard-linked files

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Tried on Windows 10 with JDK 17 and JDK 19.0.1

      A DESCRIPTION OF THE PROBLEM :
      Files.move(source, target, StandardCopyOption.REPLACE_EXISTING) is IMHO expected to either succeed, then source is gone and continues to exist in target (similar to first invoking Files.deleteIfExists(target), followed by Files.move(source, target)), or fail with an exception. If source and target both point to the same file on a NTFS partition (are hardlinked), the Files.move command silently returns without exception and keeping source.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create file f2 as a hardlink to f1 on a NTFS volume. Try to move f2 onto f1 (or visa versa) with the StandardCopyOption.REPLACE_EXISTING.



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Only C:\temp\test\file should remain.
      ACTUAL -
      C:\temp\test\file and C:\temp\test\hardlink-to-file remain.

      ---------- BEGIN SOURCE ----------
      import java.io.*;
      import java.nio.file.*;

      public class MoveHardlink {
      public static void main(String[] args) throws IOException {
      final var tempPath = Paths.get("C:/temp/test");
      final var filePath = tempPath.resolve("file");
      final var hardlinkPath = tempPath.resolve("hardlink-to-file");
      if (!Files.isDirectory(tempPath)) {
      Files.createDirectories(tempPath);
      }
      Files.deleteIfExists(filePath);
      Files.deleteIfExists(hardlinkPath);
      Files.writeString(filePath, "original");
      Files.createLink(hardlinkPath, filePath);
      Files.move(hardlinkPath, filePath, StandardCopyOption.REPLACE_EXISTING);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Files.deleteIfExists(target);
      Files.move(source, target);

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: