class Files {

    public static Path createDirectories(final Path dir,
                                         final FileAttribute<?>... attrs)
            throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        checkNotNull("dir",
                     dir);

        final Path absoluteDir = dir.toAbsolutePath();

        if (!notExists(absoluteDir)) {
            throw new FileAlreadyExistsException(absoluteDir.toString());
        }

        Path parent = absoluteDir.getParent();

        while (parent != null) {
            try {
                providerOf(parent).checkAccess(parent);
                break;
            } catch (NoSuchFileException x) {
            }
            parent = parent.getParent();
        }

        if (parent == null) {
            throw new IOException("Root directory does not exist");
        }

        // create directories
        Path child = parent;
        for (final Path name : parent.relativize(dir)) {
            child = child.resolve(name);
            providerOf(child).createDirectory(child,
                                              attrs);
        }

        return dir;
    }

    private  Files();

}