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

FileOutputStream fails with Access denied when file has "hidden" or "system" attributes set

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: P5
    • Resolution: Unresolved
    • Affects Version/s: 8u5
    • Fix Version/s: tbd
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_2003

      Description

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      Java is unable to write to files which have either or both of the Windows "hidden" or "system" file attributes set, and throws an exception when this is attempted.

      Such files are not considered read-only by other Windows programs or by the underlying operating system. In other programs these files may be written freely (if you know where they are), without needing to recreate the file from scratch, and while preserving the attributes.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.io.FileNotFoundException: <filename> (Access is denied)
              at java.io.FileOutputStream.open(Native Method)
              at java.io.FileOutputStream.<init>(FileOutputStream.java:206)
              at java.io.FileOutputStream.<init>(FileOutputStream.java:156)
              at java.io.FileWriter.<init>(FileWriter.java:90)
              at HiddenFileWritingBug.main(HiddenFileWritingBug.java:25)

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      /**
       * Demonstration of a Java bug with hidden and system files. The program generates a name
       * for a temporary file and writes to it successfully. Then it marks it hidden and system.
       * Thereafter, it is unable to write to the file any more, getting a FileNotFoundException
       * on the second FileWriter creation call.
       */
      class HiddenFileWritingBug {
      public static void main(String... args) throws Throwable {
      File file = File.createTempFile("foo", "bar");

      try (Writer out = new FileWriter(file)) {
      out.write("1");
      System.out.println("1 OK");
      } catch (IOException e) {
      System.out.println("1 failed");
      e.printStackTrace();
      }

      java.nio.file.Files.setAttribute(file.toPath(), "dos:hidden", true);
      java.nio.file.Files.setAttribute(file.toPath(), "dos:system", true);

      try (Writer out = new FileWriter(file)) {
      out.write("2");
      System.out.println("2 OK");
      } catch (IOException e) {
      System.out.println("2 failed");
      e.printStackTrace();
      }

      file.delete(); // cleanup
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      If writing to a file fails, delete it and try again.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated: