test/java/io/File/SymLinks.java

Print this page




  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 6595866
  26  * @summary Test java.io.File operations with sym links
  27  */
  28 
  29 import java.io.*;
  30 import java.nio.file.Path;
  31 import java.nio.file.attribute.*;
  32 import static java.nio.file.LinkOption.*;
  33 
  34 public class SymLinks {
  35     final static PrintStream out = System.out;
  36 
  37     final static File top = new File(System.getProperty("test.dir", "."));
  38 
  39     // files used by the test
  40 
  41     final static File file              = new File(top, "foofile");
  42     final static File link2file         = new File(top, "link2file");
  43     final static File link2link2file    = new File(top, "link2link2file");
  44 
  45     final static File dir               = new File(top, "foodir");
  46     final static File link2dir          = new File(top, "link2dir");
  47     final static File link2link2dir     = new File(top, "link2link2dir");
  48 
  49     final static File link2nobody       = new File(top, "link2nobody");
  50     final static File link2link2nobody  = new File(top, "link2link2nobody");


  63         mklink(link2file, file);
  64         mklink(link2link2file, link2file);
  65 
  66         // link2link2dir -> link2dir -> dir
  67         assertTrue(dir.mkdir());
  68         mklink(link2dir, dir);
  69         mklink(link2link2dir, link2dir);
  70 
  71         // link2link2nobody -> link2nobody -> <does-not-exist>
  72         mklink(link2nobody, new File(top, "DoesNotExist"));
  73         mklink(link2link2nobody, link2nobody);
  74     }
  75 
  76     /**
  77      * Remove files, directories, and sym links used by test.
  78      */
  79     static void cleanup() throws IOException {
  80         if (file != null)
  81             file.delete();
  82         if (link2file != null)
  83             link2file.toPath().deleteIfExists();
  84         if (link2link2file != null)
  85             link2link2file.toPath().deleteIfExists();
  86         if (dir != null)
  87             dir.delete();
  88         if (link2dir != null)
  89             link2dir.toPath().deleteIfExists();
  90         if (link2link2dir != null)
  91             link2link2dir.toPath().deleteIfExists();
  92         if (link2nobody != null)
  93             link2nobody.toPath().deleteIfExists();
  94         if (link2link2nobody != null)
  95             link2link2nobody.toPath().deleteIfExists();
  96     }
  97 
  98     /**
  99      * Creates a sym link source->target
 100      */
 101     static void mklink(File source, File target) throws IOException {
 102         source.toPath().createSymbolicLink(target.toPath());
 103     }
 104 
 105     /**
 106      * Returns true if the "link" exists and is a sym link.
 107      */
 108     static boolean isSymLink(File link) {
 109          try {
 110             BasicFileAttributes attrs =
 111                 Attributes.readBasicFileAttributes(link.toPath(), NOFOLLOW_LINKS);
 112             return attrs.isSymbolicLink();
 113          } catch (IOException x) {
 114              return false;
 115          }
 116     }
 117 
 118     /**
 119      * Returns the last modified time of a sym link.
 120      */
 121     static long lastModifiedOfSymLink(File link) throws IOException {
 122         BasicFileAttributes attrs =
 123             Attributes.readBasicFileAttributes(link.toPath(), NOFOLLOW_LINKS);
 124         assertTrue(attrs.isSymbolicLink());
 125         return attrs.lastModifiedTime().toMillis();
 126     }
 127 
 128     /**
 129      * Returns true if sym links are supported on the file system where
 130      * "dir" exists.
 131      */
 132     static boolean supportsSymLinks(File dir) {
 133         Path link = dir.toPath().resolve("link");
 134         Path target = dir.toPath().resolve("target");
 135         try {
 136             link.createSymbolicLink(target);
 137             link.delete();
 138             return true;
 139         } catch (UnsupportedOperationException x) {
 140             return false;
 141         } catch (IOException x) {
 142             return false;
 143         }
 144     }
 145 
 146     static void assertTrue(boolean v) {
 147         if (!v) throw new RuntimeException("Test failed");
 148     }
 149 
 150     static void assertFalse(boolean v) {
 151         assertTrue(!v);
 152     }
 153 
 154     static void header(String h) {
 155         out.println();
 156         out.println();
 157         out.println("-- " + h + " --");


 207             assertTrue(link.delete());
 208             assertTrue(!isSymLink(link));
 209             assertTrue(link2file.exists());
 210 
 211             mklink(link, dir);
 212             assertTrue(link.delete());
 213             assertTrue(!isSymLink(link));
 214             assertTrue(dir.exists());
 215 
 216             mklink(link, link2dir);
 217             assertTrue(link.delete());
 218             assertTrue(!isSymLink(link));
 219             assertTrue(link2dir.exists());
 220 
 221             mklink(link, link2nobody);
 222             assertTrue(link.delete());
 223             assertTrue(!isSymLink(link));
 224             assertTrue(isSymLink(link2nobody));
 225 
 226         } finally {
 227             link.toPath().deleteIfExists();
 228         }
 229 
 230         header("renameTo");
 231 
 232         File newlink = new File(top, "newlink");
 233         assertTrue(link2file.renameTo(newlink));
 234         try {
 235             assertTrue(file.exists());
 236             assertTrue(isSymLink(newlink));
 237             assertTrue(!isSymLink(link2file));
 238         } finally {
 239             newlink.renameTo(link2file);  // restore link
 240         }
 241 
 242         assertTrue(link2dir.renameTo(newlink));
 243         try {
 244             assertTrue(dir.exists());
 245             assertTrue(isSymLink(newlink));
 246             assertTrue(!isSymLink(link2dir));
 247         } finally {


 270             // files that are not directories
 271             assertTrue(link2file.list() == null);
 272             assertTrue(link2nobody.list() == null);
 273 
 274         } finally {
 275             entry.delete();
 276         }
 277 
 278         header("isXXX");
 279 
 280         assertTrue(file.isFile());
 281         assertTrue(link2file.isFile());
 282         assertTrue(link2link2file.isFile());
 283 
 284         assertTrue(dir.isDirectory());
 285         assertTrue(link2dir.isDirectory());
 286         assertTrue(link2link2dir.isDirectory());
 287 
 288         // on Windows we test with the DOS hidden attribute set
 289         if (System.getProperty("os.name").startsWith("Windows")) {
 290             DosFileAttributeView view = file.toPath()
 291                 .getFileAttributeView(DosFileAttributeView.class);
 292             view.setHidden(true);
 293             try {
 294                 assertTrue(file.isHidden());
 295                 assertTrue(link2file.isHidden());
 296                 assertTrue(link2link2file.isHidden());
 297             } finally {
 298                 view.setHidden(false);
 299             }
 300             assertFalse(file.isHidden());
 301             assertFalse(link2file.isHidden());
 302             assertFalse(link2link2file.isHidden());
 303         }
 304 
 305         header("length");
 306 
 307         long len = file.length();
 308         assertTrue(len > 0L);
 309         // these tests should follow links
 310         assertTrue(link2file.length() == len);
 311         assertTrue(link2link2file.length() == len);




  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 6595866
  26  * @summary Test java.io.File operations with sym links
  27  */
  28 
  29 import java.io.*;
  30 import java.nio.file.*;
  31 import java.nio.file.attribute.*;
  32 import static java.nio.file.LinkOption.*;
  33 
  34 public class SymLinks {
  35     final static PrintStream out = System.out;
  36 
  37     final static File top = new File(System.getProperty("test.dir", "."));
  38 
  39     // files used by the test
  40 
  41     final static File file              = new File(top, "foofile");
  42     final static File link2file         = new File(top, "link2file");
  43     final static File link2link2file    = new File(top, "link2link2file");
  44 
  45     final static File dir               = new File(top, "foodir");
  46     final static File link2dir          = new File(top, "link2dir");
  47     final static File link2link2dir     = new File(top, "link2link2dir");
  48 
  49     final static File link2nobody       = new File(top, "link2nobody");
  50     final static File link2link2nobody  = new File(top, "link2link2nobody");


  63         mklink(link2file, file);
  64         mklink(link2link2file, link2file);
  65 
  66         // link2link2dir -> link2dir -> dir
  67         assertTrue(dir.mkdir());
  68         mklink(link2dir, dir);
  69         mklink(link2link2dir, link2dir);
  70 
  71         // link2link2nobody -> link2nobody -> <does-not-exist>
  72         mklink(link2nobody, new File(top, "DoesNotExist"));
  73         mklink(link2link2nobody, link2nobody);
  74     }
  75 
  76     /**
  77      * Remove files, directories, and sym links used by test.
  78      */
  79     static void cleanup() throws IOException {
  80         if (file != null)
  81             file.delete();
  82         if (link2file != null)
  83             Files.deleteIfExists(link2file.toPath());
  84         if (link2link2file != null)
  85             Files.deleteIfExists(link2link2file.toPath());
  86         if (dir != null)
  87             dir.delete();
  88         if (link2dir != null)
  89             Files.deleteIfExists(link2dir.toPath());
  90         if (link2link2dir != null)
  91             Files.deleteIfExists(link2link2dir.toPath());
  92         if (link2nobody != null)
  93             Files.deleteIfExists(link2nobody.toPath());
  94         if (link2link2nobody != null)
  95             Files.deleteIfExists(link2link2nobody.toPath());
  96     }
  97 
  98     /**
  99      * Creates a sym link source->target
 100      */
 101     static void mklink(File source, File target) throws IOException {
 102         Files.createSymbolicLink(source.toPath(), target.toPath());
 103     }
 104 
 105     /**
 106      * Returns true if the "link" exists and is a sym link.
 107      */
 108     static boolean isSymLink(File link) {
 109          return Files.isSymbolicLink(link.toPath());





 110     }

 111 
 112     /**
 113      * Returns the last modified time of a sym link.
 114      */
 115     static long lastModifiedOfSymLink(File link) throws IOException {
 116         BasicFileAttributes attrs =
 117             Files.readAttributes(link.toPath(), BasicFileAttributes.class, NOFOLLOW_LINKS);
 118         assertTrue(attrs.isSymbolicLink());
 119         return attrs.lastModifiedTime().toMillis();
 120     }
 121 
 122     /**
 123      * Returns true if sym links are supported on the file system where
 124      * "dir" exists.
 125      */
 126     static boolean supportsSymLinks(File dir) {
 127         Path link = dir.toPath().resolve("link");
 128         Path target = dir.toPath().resolve("target");
 129         try {
 130             Files.createSymbolicLink(link, target);
 131             Files.delete(link);
 132             return true;
 133         } catch (UnsupportedOperationException x) {
 134             return false;
 135         } catch (IOException x) {
 136             return false;
 137         }
 138     }
 139 
 140     static void assertTrue(boolean v) {
 141         if (!v) throw new RuntimeException("Test failed");
 142     }
 143 
 144     static void assertFalse(boolean v) {
 145         assertTrue(!v);
 146     }
 147 
 148     static void header(String h) {
 149         out.println();
 150         out.println();
 151         out.println("-- " + h + " --");


 201             assertTrue(link.delete());
 202             assertTrue(!isSymLink(link));
 203             assertTrue(link2file.exists());
 204 
 205             mklink(link, dir);
 206             assertTrue(link.delete());
 207             assertTrue(!isSymLink(link));
 208             assertTrue(dir.exists());
 209 
 210             mklink(link, link2dir);
 211             assertTrue(link.delete());
 212             assertTrue(!isSymLink(link));
 213             assertTrue(link2dir.exists());
 214 
 215             mklink(link, link2nobody);
 216             assertTrue(link.delete());
 217             assertTrue(!isSymLink(link));
 218             assertTrue(isSymLink(link2nobody));
 219 
 220         } finally {
 221             Files.deleteIfExists(link.toPath());
 222         }
 223 
 224         header("renameTo");
 225 
 226         File newlink = new File(top, "newlink");
 227         assertTrue(link2file.renameTo(newlink));
 228         try {
 229             assertTrue(file.exists());
 230             assertTrue(isSymLink(newlink));
 231             assertTrue(!isSymLink(link2file));
 232         } finally {
 233             newlink.renameTo(link2file);  // restore link
 234         }
 235 
 236         assertTrue(link2dir.renameTo(newlink));
 237         try {
 238             assertTrue(dir.exists());
 239             assertTrue(isSymLink(newlink));
 240             assertTrue(!isSymLink(link2dir));
 241         } finally {


 264             // files that are not directories
 265             assertTrue(link2file.list() == null);
 266             assertTrue(link2nobody.list() == null);
 267 
 268         } finally {
 269             entry.delete();
 270         }
 271 
 272         header("isXXX");
 273 
 274         assertTrue(file.isFile());
 275         assertTrue(link2file.isFile());
 276         assertTrue(link2link2file.isFile());
 277 
 278         assertTrue(dir.isDirectory());
 279         assertTrue(link2dir.isDirectory());
 280         assertTrue(link2link2dir.isDirectory());
 281 
 282         // on Windows we test with the DOS hidden attribute set
 283         if (System.getProperty("os.name").startsWith("Windows")) {
 284             DosFileAttributeView view = Files
 285                 .getFileAttributeView(file.toPath(), DosFileAttributeView.class);
 286             view.setHidden(true);
 287             try {
 288                 assertTrue(file.isHidden());
 289                 assertTrue(link2file.isHidden());
 290                 assertTrue(link2link2file.isHidden());
 291             } finally {
 292                 view.setHidden(false);
 293             }
 294             assertFalse(file.isHidden());
 295             assertFalse(link2file.isHidden());
 296             assertFalse(link2link2file.isHidden());
 297         }
 298 
 299         header("length");
 300 
 301         long len = file.length();
 302         assertTrue(len > 0L);
 303         // these tests should follow links
 304         assertTrue(link2file.length() == len);
 305         assertTrue(link2link2file.length() == len);