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

PrintWriter.write() does not recognize runtime file permission changes

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Not an Issue
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      PrintWriter.write(), FileOutputStream.write writes to the file even when the write permission is removed at the runtime.

      Here a file is created a with read, write and execute permissions, and then all the three permissions are removed to avoid unexpected writes by any part of code. But the write methods in PrintWriter, FileOutputStream classes writes to the file without checking the runtime permission changes to the file.

      On execution of the following code snippet, the if conditon at Line 1 returns false indicating that file is not writable, but at Line 2 fos.write(2) is able to write into file without throwing any exception.
        
            try {
              File file = new File("fos");
              FileOutputStream fos = new FileOutputStream(file);
              file.setWritable(false,false);
              if (file.canWrite()) // Line 1
                  fos.write(1);
              fos.write(2); // Line 2
            }catch(IOException e){
               System.out.println(e);
            }
       Here file.canWrite() returns false ,but fos.write(2) writes to the file without any exception.

      File.set{Writable,Executable,Readable}() methods serves its purpose to max , if write methods in java.io recognizes the permissions dynamically.

      Please see the following code,version, Result.

      <CODE>

      import java.io.*;
      import static java.lang.System.out;
      class Test3 {
          public static void main(String ... args) throws IOException {
            
            
            File f = new File(args[0]);
            
                  
            PrintWriter fos = new PrintWriter ( new BufferedWriter(new FileWriter(f)));
            
            out.println("can Read " +f.canRead());
            out.println("can Write " + f.canWrite());
            out.println("can Execute "+ f.canExecute() );
            
            out.println("set Execute false " +f.setExecutable(false,false));
            out.println("set Readable false " + f.setReadable(false,false));
            out.println("set Writable false " + f.setWritable(false,false));
            
            out.println("can Read " +f.canRead());
            out.println("can Write " + f.canWrite());
            out.println("can Execute "+ f.canExecute() );
            
            fos.write("Test Write");
            fos.flush();
            fos.close();
            }
      }
      </CODE>
      <Result>
      bash-3.00$ java -version
      java version "1.6.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-rc-b68)
      Java HotSpot(TM) Client VM (build 1.6.0-rc-b68, mixed mode)
      bash-3.00$

      bash-3.00$ umask
      0000
      bash-3.00$ ls -l fos
      fos: No such file or directory
      bash-3.00$ java Test3 fos
      can Read true
      can Write true
      can Execute false
      set Execute false true
      set Readable false true
      set Writable false true
      can Read false
      can Write false
      can Execute false
      bash-3.00$ ls -l fos
      ---------- 1 rg157576 staff 10 Jan 23 2006 fos
      bash-3.00$ cat fos
      cat: cannot open fos
      bash-3.00$ chmod 777 fos
      bash-3.00$ cat fos
      Test Writebash-3.00$

      </Result>

        Attachments

          Activity

            People

            Assignee:
            sherman Xueming Shen
            Reporter:
            rgutupalsunw Rajendra Gutupalli (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: