From 3e2cd8ef6f1ce4f46719890134c8bba9dbdb19ba Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Tue, 11 Jun 2013 16:58:49 +0200 Subject: fix QFileSystemEngine::createDirectory race condition During a call to QDir::mkpath(), the same path could be created by another process, in which case the OS mkdir will fail with EEXIST. But the docs for mkpath() state that it's not an error if it already exists, whereas for mkdir() it is an error. So QFileSystemEngine::createDirectory should accept the EEXIST error silently if it occurs while creating the sequence of parent directories and the final leaf directory, but should fail if EEXIST happens when it was called from QDir::mkdir(), which is when the createParents parameter is false. We assume the operating system mkdir() and CreateDirectory() are atomic, so there should be no race condition in QDir::mkdir(). It's not necessary for mkpath() to call stat() at each level, only to check whether an existing entry is a directory or a file. Also added to the autotest to verify that if the path is an existing file, creating a dir with the same name will fail in either mkdir or mkpath. Task-number: QTBUG-30046 Change-Id: I926352f10654fdf3b322c8685bb85ad8b8844874 Reviewed-by: Oswald Buddenhagen --- src/corelib/io/qfilesystemengine_win.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'src/corelib/io/qfilesystemengine_win.cpp') diff --git a/src/corelib/io/qfilesystemengine_win.cpp b/src/corelib/io/qfilesystemengine_win.cpp index bee7689535..fdbd6e01e4 100644 --- a/src/corelib/io/qfilesystemengine_win.cpp +++ b/src/corelib/io/qfilesystemengine_win.cpp @@ -1044,14 +1044,13 @@ bool QFileSystemEngine::createDirectory(const QFileSystemEntry &entry, bool crea } if (slash) { QString chunk = dirName.left(slash); - bool existed = false; - if (!isDirPath(chunk, &existed)) { - if (!existed) { - if (!mkDir(chunk)) - return false; - } else { - return false; + if (!mkDir(chunk)) { + if (GetLastError() == ERROR_ALREADY_EXISTS) { + bool existed = false; + if (isDirPath(chunk, &existed) && existed) + continue; } + return false; } } } -- cgit v1.2.3