Избегайте символьных циклических каталогов в рекурсивном методе в Java - PullRequest
0 голосов
/ 21 января 2019

У меня есть этот код (адаптированный для этого вопроса), где я выполняю задачу Singularity с файлами .... Я перебираю каталоги, и у меня рекурсивный вызов ...

Вкл. Windows 10 Я получаю конкретную ситуацию ... Когда кажется, что каталог сам по себе является содержимым \o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\...\o.ide.13.0.0.1.42.170225.201\, и я хочу обнаружить их и избежать их.

private void recursiveSearch(String canonicalFolder, Path path) {
    try {
        if (path == null) {
            return;
        }

        if (path.toFile().isFile()) {
            // DO SOMETHING WITH THE FILE
        } else {
            // IS A DIRECTORY
            boolean exists = Files.exists(path, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});
            if (!exists) {
                return;
            }

            DirectoryStream<Path> dirStream = null;
            try {
                dirStream = Files.newDirectoryStream(path);
                try {
                    for (Path processPath : dirStream) {
                        recursiveSearch(canonicalFolder, processPath); // RECURSIVE CALL
                    }
                } catch (Exception ex) {
                }
                dirStream.close();
            } catch (Exception ex) {
            } finally {
                if (dirStream != null) {
                    try {
                        dirStream.close();
                    } catch (IOException ex) {
                    }
                }
            }
        }
    } catch (Exception ex) {
    }
}

Хочу избежать такой ситуации

C:\Users\desarrollador.ti\AppData\Roaming\SQL Developer\system18.2.0.183.1748\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201\o.ide.13.0.0.1.42.170225.201

enter image description here

Я пытался с этим кодом

boolean exists = Files.exists(path, new LinkOption[]{LinkOption.NOFOLLOW_LINKS});
if (!exists) {
    return;
}

И с этим кодом:

for (Path processPath : dirStream) {
    if (Files.isSymbolicLink(path) && Files.isSymbolicLink(processPath)) {
        System.out.println("Symbolic Founds!");
        System.out.println("path.toFile().getCanonicalPath():" + OneSearch.NL
                + "\t:" + path.toFile().getCanonicalPath() + OneSearch.NL
                + "processPath.toFile().getCanonicalPath():" + OneSearch.NL
                + "\t:" + processPath.toFile().getCanonicalPath()
        );
        if (Files.readSymbolicLink(path).toFile().getCanonicalFile().equals(
                Files.readSymbolicLink(processPath).toFile().getCanonicalFile())) {
            System.out.println("path.toFile().getCanonicalPath():" + OneSearch.NL
                    + "\t:" + path.toFile().getCanonicalPath() + OneSearch.NL
                    + "processPath.toFile().getCanonicalPath():" + OneSearch.NL
                    + "\t:" + processPath.toFile().getCanonicalPath()
            );
            continue;
        }
    }
    recursiveSearch(canonicalFolder, processPath); // RECURSIVE CALL
}

Есть ли другая альтернатива?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...