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

File.mkdirs() method doesn't behave well when given /../

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 7
    • core-libs
    • Cause Known
    • generic
    • generic

      On Solaris or Linux, if you do:
         rm -f -r bozo_land
         mkdir -p bozo_land/abc/../def/../ghi
      what do you expect? Turns out you get all the necessary directories created:
         bozo_land/abc, bozo_land/def, and bozo_land/ghi

      With java's java.io.File mkdirs() method, it seems rather... what can I say, confused???

      import java.io.File;
      public class Bug {
         public static void main(String args[]) {
           File f = new File("bozo_land/abc/../def/../ghi");
           System.out.println("f = new File(" + f.getPath() + ")");
           System.out.println("First try: f.mkdirs()=" + f.mkdirs());
           System.out.println("First try: f.exists()=" + f.exists());
           System.out.println("Second try: f.mkdirs()=" + f.mkdirs());
           System.out.println("Second try: f.exists()=" + f.exists());
           f = new File("bozo_land");
           System.out.println("f = new File(" + f.getPath() + ")");
           System.out.println("f.exists()=" + f.exists());
           f = new File("bozo_land/abc");
           System.out.println("f = new File(" + f.getPath() + ")");
           System.out.println("f.exists()=" + f.exists());
           f = new File("bozo_land/abc/..");
           System.out.println("f = new File(" + f.getPath() + ")");
           System.out.println("f.exists()=" + f.exists());
           f = new File("bozo_land/abc/../def");
           System.out.println("f = new File(" + f.getPath() + ")");
           System.out.println("f.exists()=" + f.exists());
           f = new File("bozo_land/abc/../def/..");
           System.out.println("f = new File(" + f.getPath() + ")");
           System.out.println("f.exists()=" + f.exists());
           f = new File("bozo_land/abc/../def/../ghi");
           System.out.println("f = new File(" + f.getPath() + ")");
           System.out.println("f.exists()=" + f.exists());
        }
      }

      Most java implementations do this:

      bonsai<12> rm -f -r bozo_land/

      bonsai<13> java Bug
      f = new File(bozo_land/abc/../def/../ghi)
      First try: f.mkdirs()=true <--- It thinks it created the path ok
      First try: f.exists()=false <--- But it says it does not exist because abc and def were not created
      Second try: f.mkdirs()=false <--- Probably because it thinks it was created already (ghi exists)
      Second try: f.exists()=false
      f = new File(bozo_land)
      f.exists()=true
      f = new File(bozo_land/abc)
      f.exists()=false
      f = new File(bozo_land/abc/..)
      f.exists()=false
      f = new File(bozo_land/abc/../def)
      f.exists()=false
      f = new File(bozo_land/abc/../def/..)
      f.exists()=false
      f = new File(bozo_land/abc/../def/../ghi)
      f.exists()=false


      The javadoc on mkdirs() says:
          true if and only if the directory was created, along with all necessary parent directories; false otherwise

      Granted, abc and def are not technically parent directories. But if after a successful mkdirs(), it still does not exist, that seems bad.

            Unassigned Unassigned
            ohair Kelly Ohair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: