I am working fixing on some coverity issues and i am confused about how to solve toctou for stat a directory and make a directory.
////////////////////////////////////
// Make sure storage dir exists. If
// not, create it.
if( stat( dir, &statBuff ) != -1 )
{
if( S_ISDIR( statBuff.st_mode ) )
{
if( (dirPtr = opendir( fabcSsrDbStorageDir )) == NULL)
{
}
closedir(dirPtr);
}
}
else
{
////////////////////////////////////
// dir directory
// does not exist- create it
// now.
if( (mkdir( dir, S_IRWXU )) != 0 )
{
}
}
Please give your suggestions Thanks
Just do mkdir()
. If the directory already exists, EEXIST
will tell you. Accept either zero or this return-code to indicate that you have, one way or the other, now accomplished your objective. "Race conditions" cease to be an issue since mkdir()
has taken care of that for you.