diff --git a/libs/openFrameworks/utils/ofFileUtils.cpp b/libs/openFrameworks/utils/ofFileUtils.cpp index e1b40c7c774..9f1cf016336 100644 --- a/libs/openFrameworks/utils/ofFileUtils.cpp +++ b/libs/openFrameworks/utils/ofFileUtils.cpp @@ -894,7 +894,7 @@ ofDirectory::ofDirectory(const std::filesystem::path & path){ void ofDirectory::open(const std::filesystem::path & path){ originalDirectory = ofFilePath::getPathForDirectory(path.string()); files.clear(); - myDir = std::filesystem::path(ofToDataPath(originalDirectory)); + myDir = std::filesystem::path(ofToDataPath(originalDirectory)); } //------------------------------------------------------------------------------------------------------------ @@ -908,9 +908,9 @@ bool ofDirectory::create(bool recursive){ if(!myDir.string().empty()){ try{ if(recursive){ - std::filesystem::create_directories(myDir); + std::filesystem::create_directories(myDir); }else{ - std::filesystem::create_directory(myDir); + std::filesystem::create_directory(myDir); } } catch(std::exception & except){ @@ -1057,9 +1057,9 @@ bool ofDirectory::remove(bool recursive){ try{ if(recursive){ - std::filesystem::remove_all(myDir); + std::filesystem::remove_all(std::filesystem::canonical(myDir)); }else{ - std::filesystem::remove(myDir); + std::filesystem::remove(std::filesystem::canonical(myDir)); } }catch(std::exception & except){ ofLogError("ofDirectory") << "remove(): unable to remove file/directory: " << except.what(); @@ -1523,14 +1523,17 @@ string ofFilePath::makeRelative(const std::string & from, const std::string & to // Find common base for( std::filesystem::path::const_iterator toEnd( pathTo.end() ), fromEnd( pathFrom.end() ) ; itrFrom != fromEnd && itrTo != toEnd && *itrFrom == *itrTo; ++itrFrom, ++itrTo ); // Navigate backwards in directory to reach previously found base - for( std::filesystem::path::const_iterator fromEnd( pathFrom.end() ); itrFrom != fromEnd; ++itrFrom ) - { - if( (*itrFrom) != "." ) + for( std::filesystem::path::const_iterator fromEnd( pathFrom.end() ); itrFrom != fromEnd; ++itrFrom ){ + if( (*itrFrom) != "." ){ ret /= ".."; + } } // Now navigate down the directory branch - for( ; itrTo != pathTo.end() ; ++itrTo ) - ret /= *itrTo; + for( ; itrTo != pathTo.end() ; ++itrTo ){ + if( itrTo->string() != "."){ + ret /= *itrTo; + } + } return ret.string(); } diff --git a/tests/utils/fileUtils/src/main.cpp b/tests/utils/fileUtils/src/main.cpp index cb801ca659d..d95dd7d3e72 100644 --- a/tests/utils/fileUtils/src/main.cpp +++ b/tests/utils/fileUtils/src/main.cpp @@ -7,6 +7,8 @@ std::filesystem::path initial_cwd; class ofApp: public ofxUnitTestsApp{ void run(){ ofDirectory dir("."); + dir.create(true); + dir.exists(); for(auto f: dir){ f.setWriteable(true); if(f.isDirectory()){ @@ -201,6 +203,7 @@ class ofApp: public ofxUnitTestsApp{ test_eq(ofToDataPath(""), "../../../data/", "#4563 test3"); #endif + //======================================================================== // clean test files dir.open("."); @@ -212,6 +215,14 @@ class ofApp: public ofxUnitTestsApp{ f.remove(); } } + + //======================================================================== + ofLogNotice() << "#4564"; + dir.remove(true); + ofDirectory currentVideoDirectory(ofToDataPath("../../../video", true)); + auto path = currentVideoDirectory.path(); + std::string pathEnd("data/../../../video/"); + test_eq(path.substr(path.size()-pathEnd.size()), pathEnd, "#4564"); } };