/** * Test if a path is a directory and writeable. * Path directories will be created before trying write test. * * @param pfad path to the directory * @return true if we can write a file there, false if not. */ public static boolean checkPathWriteable(@NotNull String pfad) { boolean result = false; if (pfad.isEmpty()) return false; Path path = Paths.get(pfad); try { System.out.println("checkPathWriteable path = " + path.toString()); final boolean isLink = Files.isSymbolicLink(path); boolean isDirectory = Files.isDirectory(path); // don't use LinkOption.NOFOLLOW_LINKS - would not recognize sym link to a directory System.out.println("isLink = " + isLink); System.out.println("isDirectory = " + isDirectory); if (!isLink && !isDirectory) { // path does not point to a symbolic link // we need this precondition on MS Windows and Linux: // here, Files.createDirectories will fail if path points to a sym link. path = Files.createDirectories(path); // path did not point to a directory before. Now, hopefully it does. But check to be safe. isDirectory = Files.isDirectory(path); } // else if (isLink && !isDirectory) // path points to a symbolic link to a file! --> create a directory with same name as a sym link? yikes. // else if (isDirectory) // path already isDirectory. No actual need to create it final boolean writableWorkAround = writableWorkAround(path); if (isDirectory && writableWorkAround) { var tmpPath = Files.createTempFile(path, "mediathek", "tmp"); Files.delete(tmpPath); result = true; } } catch (Exception e) { logger.error("checkPathWritable()", e); result = false; } return result; }