Skip to content

Filesystem component, filesystem.php, mkdir function : bug when race condition #11626

@kcassam

Description

@kcassam

Sometimes, the Filesystem->mkdir() function was giving me a 'file exists' error.

I made some search and found this was a race conditon situation : the directory was created by a concurrent script between is_dir() and mkdir().

This 'bug' is described here : https://www.drupal.org/node/1642532
With a proposed solution here : https://www.drupal.org/files/1642532-drush-mkdir-race-condition.patch

On stackoverflow, I found somebody who had the same problem and I gave a solution here : http://stackoverflow.com/questions/19964287/mkdir-function-throw-exception-file-exists-even-after-checking-that-directory/25219407#25219407

You can check the function createDirectory here as well : https://github.com/KnpLabs/Gaufrette/blob/master/src/Gaufrette/Adapter/Local.php#L260
So this is what I would do :

public function mkdir($dirs, $mode = 0777)
{
    foreach ($this->toIterator($dirs) as $dir) {
        if (is_dir($dir)) {
            continue;
        }

        if (true !== @mkdir($dir, $mode, true)) {
            if (!is_dir($dir)) {
                // The directory was not created by a concurrent process. Let's throw an exception with a developer friendly error message
                $error = error_get_last();
                throw new IOException(sprintf('Failed to create "%s", error message is "%s".', $dir, $error['message']), 0, null, $dir);
            }
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions