Skip to content

[WIP] Throw Exception if results directory cannot be created or file cannot be written #2188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions OpenSim/Actuators/Test/testMuscles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,9 @@ void simulateMuscle(

//An analysis only writes to a dir that exists, so create here.
if(printResults){
IO::makeDir("testMuscleResults");
muscleAnalysis->printResults(actuatorType, "testMuscleResults");
const std::string dirName = "testMuscleResults";
OPENSIM_THROW_IF(errno == ENOENT, UnableToCreateDirectory, dirName);
muscleAnalysis->printResults(actuatorType, dirName);
}

double muscleWork = muscWorkProbe->getProbeOutputs(si)(0);
Expand Down
13 changes: 13 additions & 0 deletions OpenSim/Common/FileAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ class FileIsEmpty : public IOError {
}
};

class UnableToCreateDirectory : public IOError {
public:
UnableToCreateDirectory(const std::string& file,
size_t line,
const std::string& func,
const std::string& directory) :
IOError(file, line, func) {
std::string msg = "Unable to create directory '" + directory + "'.";

addMessage(msg);
}
};

class FileExtensionNotFound : public InvalidArgument {
public:
FileExtensionNotFound(const std::string& file,
Expand Down
8 changes: 6 additions & 2 deletions OpenSim/Common/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2741,7 +2741,9 @@ print(const string &aFileName,const string &aMode, const string& aComment) const
{
// OPEN THE FILE
FILE *fp = IO::OpenFile(aFileName,aMode);
if(fp==NULL) return(false);
OPENSIM_THROW_IF(fp==NULL, Exception,
"Storage: Failed to open file '" + aFileName + "' for writing.\n"
+ "Verify that the destination directory is writable.");

// WRITE THE HEADER
int n=0,nTotal=0;
Expand Down Expand Up @@ -2819,7 +2821,9 @@ print(const string &aFileName,double aDT,const string &aMode) const
if (_fp!= NULL) fclose(_fp);
// OPEN THE FILE
FILE *fp = IO::OpenFile(aFileName,aMode);
if(fp==NULL) return(-1);
OPENSIM_THROW_IF(fp==NULL, Exception,
"Storage: Failed to open file '" + aFileName + "' for writing.\n"
+ "Verify that the destination directory is writable.");

// HOW MANY TIME STEPS?
double ti = getFirstTime();
Expand Down
6 changes: 4 additions & 2 deletions OpenSim/Examples/MuscleExample/mainFatigue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,10 @@ int main()
"tugOfWar_fatigue_forces.sto");

// Save the muscle analysis results
IO::makeDir("MuscleAnalysisResults");
muscAnalysis->printResults("fatigue", "MuscleAnalysisResults");
const std::string dirName = "MuscleAnalysisResults";
IO::makeDir(dirName);
OPENSIM_THROW_IF(errno == ENOENT, UnableToCreateDirectory, dirName);
muscAnalysis->printResults("fatigue", dirName);

// Save the OpenSim model to a file
osimModel.print("tugOfWar_fatigue_model.osim");
Expand Down
5 changes: 4 additions & 1 deletion OpenSim/Simulation/Model/AbstractTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,10 @@ printResults(const string &aBaseName,const string &aDir,double aDT,
const string &aExtension)
{
cout<<"Printing results of investigation "<<getName()<<" to "<<aDir<<"."<<endl;
IO::makeDir(aDir);
if (!aDir.empty()) {
IO::makeDir(aDir);
OPENSIM_THROW_IF(errno == ENOENT, UnableToCreateDirectory, aDir);
}
_model->updAnalysisSet().printResults(aBaseName,aDir,aDT,aExtension);
}

Expand Down
8 changes: 7 additions & 1 deletion OpenSim/Tools/CMCTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,13 @@ bool CMCTool::run()
cmcActSubsystem.setCompleteState( s );

// Set output file names so that files are flushed regularly in case we fail
IO::makeDir(getResultsDir()); // Create directory for output in case it doesn't exist
// Create directory for output in case it doesn't exist
if (!getResultsDir().empty()) {
IO::makeDir(getResultsDir());
OPENSIM_THROW_IF(errno == ENOENT, UnableToCreateDirectory,
getResultsDir());
}

manager.getStateStorage().setOutputFileName(getResultsDir() + "/" + getName() + "_states.sto");
try {
manager.initialize(s);
Expand Down
14 changes: 12 additions & 2 deletions OpenSim/Tools/InverseDynamicsTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,12 @@ bool InverseDynamicsTool::run()
genForceResults.setColumnLabels(labels);
genForceResults.setName("Inverse Dynamics Generalized Forces");

IO::makeDir(getResultsDir());
if (!getResultsDir().empty()) {
IO::makeDir(getResultsDir());
OPENSIM_THROW_IF(errno == ENOENT, UnableToCreateDirectory,
getResultsDir());
}

Storage::printResult(&genForceResults, _outputGenForceFileName, getResultsDir(), -1, ".sto");
IO::chDir(saveWorkingDirectory);

Expand All @@ -415,7 +420,12 @@ bool InverseDynamicsTool::run()
bodyForcesResults.setColumnLabels(body_force_labels);
bodyForcesResults.setName("Inverse Dynamics Body Forces at Specified Joints");

IO::makeDir(getResultsDir());
if (!getResultsDir().empty()) {
IO::makeDir(getResultsDir());
OPENSIM_THROW_IF(errno == ENOENT, UnableToCreateDirectory,
getResultsDir());
}

Storage::printResult(&bodyForcesResults, _outputBodyForcesAtJointsFileName, getResultsDir(), -1, ".sto");
IO::chDir(saveWorkingDirectory);
}
Expand Down
15 changes: 12 additions & 3 deletions OpenSim/Tools/InverseKinematicsTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,11 @@ bool InverseKinematicsTool::run()
modelMarkerErrors->setColumnLabels(labels);
modelMarkerErrors->setName("Model Marker Errors from IK");

IO::makeDir(getResultsDir());
if (!getResultsDir().empty()) {
IO::makeDir(getResultsDir());
OPENSIM_THROW_IF(errno == ENOENT, UnableToCreateDirectory,
getResultsDir());
}
string errorFileName = trialName + "_ik_marker_errors";
Storage::printResult(modelMarkerErrors, errorFileName,
getResultsDir(), -1, ".sto");
Expand All @@ -445,8 +449,13 @@ bool InverseKinematicsTool::run()
}
modelMarkerLocations->setColumnLabels(labels);
modelMarkerLocations->setName("Model Marker Locations from IK");

IO::makeDir(getResultsDir());

if (!getResultsDir().empty()) {
IO::makeDir(getResultsDir());
OPENSIM_THROW_IF(errno == ENOENT, UnableToCreateDirectory,
getResultsDir());
}

string markerFileName = trialName + "_ik_model_marker_locations";
Storage::printResult(modelMarkerLocations, markerFileName,
getResultsDir(), -1, ".sto");
Expand Down
8 changes: 7 additions & 1 deletion OpenSim/Tools/RRATool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,13 @@ bool RRATool::run()
cmcActSubsystem.setCompleteState( s );

// Set output file names so that files are flushed regularly in case we fail
IO::makeDir(getResultsDir()); // Create directory for output in case it doesn't exist
// Create directory for output in case it doesn't exist
if (!getResultsDir().empty()) {
IO::makeDir(getResultsDir());
OPENSIM_THROW_IF(errno == ENOENT, UnableToCreateDirectory,
getResultsDir());
}

manager.getStateStorage().setOutputFileName(getResultsDir() + "/" + getName() + "_states.sto");
try {
manager.initialize(s);
Expand Down