openframeworks / openframeworks/openFrameworks

ofDirectory return wrong value

Open
#6,190 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

filesystem
Dominant language
C++
Stars
10.4k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
9

Description

In the documentation it says

bool ofDirectory::createDirectory(const filesystem::path &dirPath, bool bRelativeToData=true, bool recursive=false)

Returns: true if directory was created successfully

However when I look in the code it will always return true:

bool ofDirectory::createDirectory(const std::filesystem::path& _dirPath, bool bRelativeToData, bool recursive){
	auto dirPath = _dirPath;

	if(bRelativeToData){
		dirPath = ofToDataPath(dirPath);
	}
	
	// on OSX,std::filesystem::create_directories seems to return false *if* the path has folders that already exist
	// and true if it doesn't
	// so to avoid unnecessary warnings on OSX, we check if it exists here:
	
	bool bDoesExistAlready = ofDirectory::doesDirectoryExist(dirPath,false);
	
	if (!bDoesExistAlready){
		
		bool success = false;
		try{
			if(!recursive){
				success = std::filesystem::create_directory(dirPath);
			}else{
				success = std::filesystem::create_directories(dirPath);
			}
		} catch(std::exception & except){
			ofLogError("ofDirectory") << "createDirectory(): couldn't create directory \"" << dirPath << "\": " << except.what();
			return false;
		}
		return success;
	}
	
	// no need to create it - it already exists.
	return true;
}

Basically when read as bDoesExistAlready is true the return value should be false.

However this gets tricky if the creation of the directory fails, it will then also return false which would then have different meaning.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the ofDirectory::createDirectory implementation shown in the issue and compare it with the documented return value. Check how callers interpret the result and how the filesystem behavior differs when the directory already exists or creation fails. Done means the return semantics are decided consistently and the implementation and documentation agree.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.