--- old/src/hotspot/os/windows/os_windows.cpp 2017-12-19 16:04:55.611348279 -0800 +++ new/src/hotspot/os/windows/os_windows.cpp 2017-12-19 16:04:55.213310839 -0800 @@ -4394,13 +4394,51 @@ // Is a (classpath) directory empty? bool os::dir_is_empty(const char* path) { - WIN32_FIND_DATA fd; - HANDLE f = FindFirstFile(path, &fd); + bool is_empty = false; + char* search_path = (char*)os::malloc(strlen(path) + 3, mtInternal); + if (search_path == NULL) { + errno = ENOMEM; + return false; + } + strcpy(search_path, path); + // Append "*", or possibly "\\*", to path + if (path[1] == ':' && + (path[2] == '\0' || + (path[2] == '\\' && path[3] == '\0'))) { + // No '\\' needed for cases like "Z:" or "Z:\" + strcat(search_path, "*"); + } + else { + strcat(search_path, "\\*"); + } + errno_t err = ERROR_SUCCESS; + wchar_t* wpath = create_unc_path(search_path, err); + if (err != ERROR_SUCCESS) { + if (wpath != NULL) { + destroy_unc_path(wpath); + } + os::free(search_path); + errno = err; + return false; + } + WIN32_FIND_DATAW fd; + HANDLE f = FindFirstFileW(wpath, &fd); + destroy_unc_path(wpath); if (f == INVALID_HANDLE_VALUE) { - return true; + is_empty = true; + } else { + int num_files = 0; + do { + num_files++; + } while (FindNextFileW(f, &fd)); + // An empty directory contains only the current directory file + // and the previous directory file. + is_empty = ((wcscmp(fd.cFileName, L".") == 0) || + (wcscmp(fd.cFileName, L"..") == 0)) && (num_files == 2); + FindClose(f); } - FindClose(f); - return false; + free(search_path); + return is_empty; } // create binary file, rewriting existing file if required --- old/test/hotspot/jtreg/runtime/appcds/DirClasspathTest.java 2017-12-19 16:04:56.544436047 -0800 +++ new/test/hotspot/jtreg/runtime/appcds/DirClasspathTest.java 2017-12-19 16:04:56.109395126 -0800 @@ -34,8 +34,13 @@ import jdk.test.lib.Platform; import jdk.test.lib.process.OutputAnalyzer; import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; public class DirClasspathTest { + private static final int MAX_PATH = 260; + public static void main(String[] args) throws Exception { File dir = new File(System.getProperty("user.dir")); File emptydir = new File(dir, "emptydir"); @@ -43,16 +48,36 @@ // Empty dir in -cp: should be OK OutputAnalyzer output; - if (!Platform.isWindows()) { - // This block fails on Windows because of JDK-8192927 - output = TestCommon.dump(emptydir.getPath(), TestCommon.list("DoesntMatter"), "-Xlog:class+path=info"); - TestCommon.checkDump(output); + output = TestCommon.dump(emptydir.getPath(), TestCommon.list("DoesntMatter"), "-Xlog:class+path=info"); + TestCommon.checkDump(output); + + // Long path to empty dir in -cp: should be OK + Path classDir = Paths.get(System.getProperty("test.classes")); + Path destDir = classDir; + int subDirLen = MAX_PATH - classDir.toString().length() - 2; + if (subDirLen > 0) { + char[] chars = new char[subDirLen]; + Arrays.fill(chars, 'x'); + String subPath = new String(chars); + destDir = Paths.get(System.getProperty("test.classes"), subPath); } + File longDir = destDir.toFile(); + longDir.mkdir(); + File subDir = new File(longDir, "DoesntMatter"); + subDir.mkdir(); + output = TestCommon.dump(subDir.getPath(), TestCommon.list("DoesntMatter"), "-Xlog:class+path=info"); + TestCommon.checkDump(output); // Non-empty dir in -cp: should fail // is not empty because it has at least one subdirectory, i.e., output = TestCommon.dump(dir.getPath(), TestCommon.list("DoesntMatter"), "-Xlog:class+path=info"); output.shouldNotHaveExitValue(0); output.shouldContain("CDS allows only empty directories in archived classpaths"); + + // Long path to non-empty dir in -cp: should fail + // is not empty because it has at least one subdirectory, i.e., + output = TestCommon.dump(longDir.getPath(), TestCommon.list("DoesntMatter"), "-Xlog:class+path=info"); + output.shouldNotHaveExitValue(0); + output.shouldContain("CDS allows only empty directories in archived classpaths"); } }