Fix ensure_dir: check that path is actually a directory path