0N/A/*
2362N/A * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A @bug 4759207 4403166 4165006 4403166 6182812 6274272
0N/A @summary Test to see if win32 path length can be greater than 260
0N/A */
0N/A
0N/Aimport java.io.*;
0N/A
0N/Apublic class MaxPathLength {
0N/A private static String sep = File.separator;
0N/A private static String pathComponent = sep +
0N/A "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
0N/A private static String fileName =
0N/A "areallylongfilenamethatsforsur";
0N/A private static boolean isWindows = false;
0N/A private static long totalSpace;
0N/A private static long freeSpace;
0N/A private static long usableSpace;
0N/A private static long ONEMEGA = 1024*1024;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A String osName = System.getProperty("os.name");
0N/A if (osName.startsWith("Windows")) {
0N/A isWindows = true;
0N/A if (osName.startsWith("Windows 9") ||
0N/A osName.startsWith("Windows Me"))
0N/A return; // win9x/Me cannot handle long paths
0N/A }
0N/A
0N/A if (osName.startsWith("SunOS")) {
0N/A return; // We don't run this test on Solaris either.
0N/A // Some Solaris machines have very "slow" disk
0N/A // access performance which causes this one
0N/A // to timeout.
0N/A }
0N/A
0N/A if (isWindows) {
0N/A File f = new File(".");
0N/A totalSpace = f.getTotalSpace()/ONEMEGA;
0N/A freeSpace = f.getFreeSpace()/ONEMEGA;
0N/A usableSpace = f.getUsableSpace()/ONEMEGA;
0N/A }
0N/A
0N/A for (int i = 4; i < 7; i++) {
0N/A String name = fileName;
0N/A while (name.length() < 256) {
0N/A testLongPath (i, name, false);
0N/A testLongPath (i, name, true);
0N/A name += "A";
0N/A }
0N/A }
0N/A
0N/A // Testing below will not be run if "-extra" is not
0N/A if (args.length == 0 ||
0N/A !"-extra".equals(args[0]) ||
0N/A !isWindows)
0N/A return;
0N/A
0N/A /* Testings below should not be run on a remote
0N/A dir that exists on a Solaris machine */
0N/A for (int i = 20; i < 21; i++) {
0N/A String name = fileName;
0N/A while (name.length() < 256) {
0N/A testLongPath (i, name, false);
0N/A testLongPath (i, name, true);
0N/A name += "A";
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static int lastMax = 0;
0N/A static void testLongPath(int max, String fn,
0N/A boolean tryAbsolute) throws Exception {
0N/A String[] created = new String[max];
0N/A String pathString = ".";
0N/A for (int i = 0; i < max -1; i++) {
0N/A pathString = pathString + pathComponent;
0N/A created[max - 1 -i] = pathString;
0N/A
0N/A }
0N/A File dirFile = new File(pathString);
0N/A File f = new File(pathString + sep + fn);
0N/A
0N/A String tPath = f.getPath();
0N/A if (tryAbsolute) {
0N/A tPath = f.getCanonicalPath();
0N/A }
0N/A created[0] = tPath;
0N/A
0N/A //for getCanonicalPath testing on win32
0N/A File fu = new File(pathString + sep + fn.toUpperCase());
0N/A
0N/A if (dirFile.exists()) {
0N/A System.err.println("Warning: Test directory structure exists already!");
0N/A return;
0N/A }
0N/A boolean couldMakeTestDirectory = dirFile.mkdirs();
0N/A if (!couldMakeTestDirectory) {
0N/A throw new RuntimeException ("Could not create test directory structure");
0N/A }
0N/A try {
0N/A if (tryAbsolute)
0N/A dirFile = new File(dirFile.getCanonicalPath());
0N/A if (!dirFile.isDirectory())
0N/A throw new RuntimeException ("File.isDirectory() failed");
0N/A if (isWindows && lastMax != max) {
0N/A long diff = totalSpace - dirFile.getTotalSpace()/ONEMEGA;
0N/A if (diff < -5 || diff > 5)
0N/A throw new RuntimeException ("File.getTotalSpace() failed");
0N/A diff = freeSpace - dirFile.getFreeSpace()/ONEMEGA;
0N/A if (diff < -5 || diff > 5)
0N/A throw new RuntimeException ("File.getFreeSpace() failed");
0N/A diff = usableSpace - dirFile.getUsableSpace()/ONEMEGA;
0N/A if (diff < -5 || diff > 5)
0N/A throw new RuntimeException ("File.getUsableSpace() failed");
0N/A lastMax = max;
0N/A }
0N/A f = new File(tPath);
0N/A if (!f.createNewFile()) {
0N/A throw new RuntimeException ("File.createNewFile() failed");
0N/A }
0N/A if (!f.exists())
0N/A throw new RuntimeException ("File.exists() failed");
0N/A if (!f.isFile())
0N/A throw new RuntimeException ("File.isFile() failed");
0N/A if (!f.canRead())
0N/A throw new RuntimeException ("File.canRead() failed");
0N/A if (!f.canWrite())
0N/A throw new RuntimeException ("File.canWrite() failed");
0N/A if (!f.delete())
0N/A throw new RuntimeException ("File.delete() failed");
0N/A FileOutputStream fos = new FileOutputStream(f);
0N/A fos.write(1);
0N/A fos.close();
0N/A if (f.length() != 1)
0N/A throw new RuntimeException ("File.length() failed");
0N/A long time = System.currentTimeMillis();
0N/A if (!f.setLastModified(time))
0N/A throw new RuntimeException ("File.setLastModified() failed");
0N/A if (f.lastModified() == 0) {
0N/A throw new RuntimeException ("File.lastModified() failed");
0N/A }
0N/A String[] list = dirFile.list();
0N/A if (list == null || !fn.equals(list[0])) {
0N/A throw new RuntimeException ("File.list() failed");
0N/A }
0N/A
0N/A File[] flist = dirFile.listFiles();
0N/A if (flist == null || !fn.equals(flist[0].getName()))
0N/A throw new RuntimeException ("File.listFiles() failed");
0N/A
0N/A if (isWindows &&
0N/A !fu.getCanonicalPath().equals(f.getCanonicalPath()))
0N/A throw new RuntimeException ("getCanonicalPath() failed");
0N/A
0N/A char[] cc = tPath.toCharArray();
0N/A cc[cc.length-1] = 'B';
0N/A File nf = new File(new String(cc));
0N/A if (!f.renameTo(nf)) {
0N/A /*there is a known issue that renameTo fails if
0N/A (1)the path is a UNC path and
0N/A (2)the path length is bigger than 1092
0N/A so don't stop if above are true
0N/A */
0N/A String abPath = f.getAbsolutePath();
0N/A if (!abPath.startsWith("\\\\") ||
0N/A abPath.length() < 1093) {
0N/A throw new RuntimeException ("File.renameTo() failed for lenth="
0N/A + abPath.length());
0N/A }
0N/A return;
0N/A }
0N/A if (!nf.canRead())
0N/A throw new RuntimeException ("Renamed file is not readable");
0N/A if (!nf.canWrite())
0N/A throw new RuntimeException ("Renamed file is not writable");
0N/A if (nf.length() != 1)
0N/A throw new RuntimeException ("Renamed file's size is not correct");
0N/A nf.renameTo(f);
0N/A /* add a script to test these two if we got a regression later
0N/A if (!f.setReadOnly())
0N/A throw new RuntimeException ("File.setReadOnly() failed");
0N/A f.deleteOnExit();
0N/A */
0N/A } finally {
0N/A // Clean up
0N/A for (int i = 0; i < max; i++) {
0N/A pathString = created[i];
0N/A // Only works with completex canonical paths
0N/A File df = new File(pathString);
0N/A pathString = df.getCanonicalPath();
0N/A df = new File(pathString);
0N/A if (!df.delete())
0N/A System.out.printf("Delete failed->%s\n", pathString);
0N/A }
0N/A }
0N/A }
0N/A}