private boolean makeFtpDirectoryRecursive()

in wagon-providers/wagon-ftp/src/main/java/org/apache/maven/wagon/providers/ftp/FtpWagon.java [603:665]


    private boolean makeFtpDirectoryRecursive(String fileName, RepositoryPermissions permissions) throws IOException {
        if (fileName == null
                || fileName.length() == 0
                || fileName.replace('/', '_').trim().length() == 0) // if a string is '/', '//' or similar
        {
            return false;
        }

        int slashPos = fileName.indexOf("/");
        String oldPwd = null;
        boolean ok = true;

        if (slashPos == 0) {
            // this is an absolute directory
            oldPwd = ftp.printWorkingDirectory();

            // start with the root
            ftp.changeWorkingDirectory("/");
            fileName = fileName.substring(1);

            // look up the next path separator
            slashPos = fileName.indexOf("/");
        }

        if (slashPos >= 0 && slashPos < (fileName.length() - 1)) // not only a slash at the end, like in 'newDir/'
        {
            if (oldPwd == null) {
                oldPwd = ftp.printWorkingDirectory();
            }

            String nextDir = fileName.substring(0, slashPos);

            boolean changedDir = false;
            // we only create the nextDir if it doesn't yet exist
            if (!ftp.changeWorkingDirectory(nextDir)) {
                ok &= ftp.makeDirectory(nextDir);
            } else {
                changedDir = true;
            }

            if (ok) {
                // set the permissions for the freshly created directory
                setPermissions(permissions);

                if (!changedDir) {
                    ftp.changeWorkingDirectory(nextDir);
                }

                // now create the deeper directories
                final String remainingDirs = fileName.substring(slashPos + 1);
                ok &= makeFtpDirectoryRecursive(remainingDirs, permissions);
            }
        } else {
            ok = ftp.makeDirectory(fileName);
        }

        if (oldPwd != null) {
            // change back to the old working directory
            ftp.changeWorkingDirectory(oldPwd);
        }

        return ok;
    }