Skip to content
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

Make mkdir_p only attempt to create necessary directories #53

Merged
merged 1 commit into from
Oct 30, 2020

Conversation

jeremyevans
Copy link
Contributor

Previously, if creating the directory directly didn't work
and the directory didn't exist, mkdir_p would create all
directories from the root. This modifies the approach to
check whether the directory exists when walking up the
directory tree from the argument, and once you have found an
intermediate directory that exists, you only need to create
directories under it.

This approach has a couple advantages:

  1. It performs better when most directories in path already exist,
    and that will be true for most usage of mkdir_p, as mkdir_p is
    usually called with paths where the first few directories exist
    and only the last directory or last few directories do not.

  2. It works in file-system access limited environments such as
    when unveil(2) is used on OpenBSD. In these environments, if
    /foo/bar/baz exists and is unveiled, you can do
    mkdir /foo/bar/baz/xyz but mkdir /foo and mkdir /foo/bar raise
    Errno::ENOENT.

Previously, if creating the directory directly didn't work
and the directory didn't exist, mkdir_p would create all
directories from the root.  This modifies the approach to
check whether the directory exists when walking up the
directory tree from the argument, and once you have found an
intermediate directory that exists, you only need to create
directories under it.

This approach has a couple advantages:

1) It performs better when most directories in path already exist,
and that will be true for most usage of mkdir_p, as mkdir_p is
usually called with paths where the first few directories exist
and only the last directory or last few directories do not.

2) It works in file-system access limited environments such as
when unveil(2) is used on OpenBSD.  In these environments, if
/foo/bar/baz exists and is unveiled, you can do
`mkdir /foo/bar/baz/xyz` but `mkdir /foo` and `mkdir /foo/bar` raise
Errno::ENOENT.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant