mirror of
https://github.com/zeek/zeek.git
synced 2025-10-09 10:08:20 +00:00
Fix race condition in ensure_dir()
If something else created the dir between the stat() and mkdir(), it previously reported that as a failure.
This commit is contained in:
parent
dfc34563b5
commit
d60f16c229
1 changed files with 14 additions and 19 deletions
33
src/util.cc
33
src/util.cc
|
@ -901,31 +901,26 @@ bool ensure_intermediate_dirs(const char* dirname)
|
||||||
|
|
||||||
bool ensure_dir(const char *dirname)
|
bool ensure_dir(const char *dirname)
|
||||||
{
|
{
|
||||||
|
if ( mkdir(dirname, 0700) == 0 )
|
||||||
|
return true;
|
||||||
|
|
||||||
|
auto mkdir_errno = errno;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if ( stat(dirname, &st) < 0 )
|
|
||||||
{
|
|
||||||
if ( errno != ENOENT )
|
|
||||||
{
|
|
||||||
reporter->Warning("can't stat directory %s: %s",
|
|
||||||
dirname, strerror(errno));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( mkdir(dirname, 0700) < 0 )
|
if ( stat(dirname, &st) == -1 )
|
||||||
{
|
|
||||||
reporter->Warning("can't create directory %s: %s",
|
|
||||||
dirname, strerror(errno));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else if ( ! S_ISDIR(st.st_mode) )
|
|
||||||
{
|
{
|
||||||
reporter->Warning("%s exists but is not a directory", dirname);
|
// Show the original failure reason for mkdir() since nothing's there
|
||||||
|
// or we can't even tell what is now.
|
||||||
|
reporter->Warning("can't create directory %s: %s",
|
||||||
|
dirname, strerror(mkdir_errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
if ( S_ISDIR(st.st_mode) )
|
||||||
|
return true;
|
||||||
|
|
||||||
|
reporter->Warning("%s exists but is not a directory", dirname);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_dir(const std::string& path)
|
bool is_dir(const std::string& path)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue