Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Simplify git_futils_mkdir #1635

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

Merged
merged 2 commits into from
Jun 7, 2013
Merged

Simplify git_futils_mkdir #1635

merged 2 commits into from
Jun 7, 2013

Conversation

arrbee
Copy link
Member

@arrbee arrbee commented Jun 5, 2013

This routine was (is) pretty complicated, but given the recent changes, it seemed like it could be simplified a bit.

What I decided is that the specific error from p_mkdir doesn't really matter. If we get an error from that routine, then we'll always stat the path we attempted to create and see if it is already a directory and continue if so (provided GIT_MKDIR_EXCL isn't being used). I also use that stat data to avoid a chmod if it isn't needed. And lastly, I shifted where the final stat for GIT_MKDIR_VERIFY_DIR is done so that even if we skip over a leading drive letter or network mount point, we'll still verify that the location is an actual existing directory.

The bottom line is that this reduces the amount of code and, I think, makes it easier to follow. You have have to view the result instead of just viewing the diffs to be able to follow the changes. This is a place where side-by-side would really help.

This routine was (is) pretty complicated, but given the recent
changes, it seemed like it could be simplified a bit.
@arrbee
Copy link
Member Author

arrbee commented Jun 5, 2013

/cc @jamill - I know we're getting to a point of diminishing returns here, but what do you think about this simplification?

tail = &make_path.ptr[root];

/* is there any path to make? */
verify_final = ((flags & GIT_MKDIR_VERIFY_DIR) != 0) && (*tail != 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the second condition here (*tail != 0) meant to capture the case where the path is only the root? due to the above lines, tail is set to point to make_path.size - 1, so it will point to the char before the null char, and in Win32 will not be null (e.g. 'd:' will result in tail pointing to ':')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the check is actually supposed to be whether there is any path to be created after all once the GIT_MKDIR_SKIP_LAST flag has been taken into account. Due to that flag, it is possible that there is literally no directory to be created and the final verification should not be done. This happens in the test suite where we end up calling git_futils_mkpath2file("filename", ...) with a relative filename that doesn't have a leading directory at all.

That being said, I didn't notice that the below line that advances the tail past the min_root_len is not successfully preventing the mkdir on the root. Let me address that (and also make the "SKIP_LAST has left nothing to do" easier to understand).

@jamill
Copy link
Member

jamill commented Jun 5, 2013

@arrbee I like the general direction, however, on Win32 this can still call p_mkdir on the volume root.

There are two places where git_futils_mkdir should exit early or
at least do less.  The first is when using GIT_MKDIR_SKIP_LAST
and having that flag leave no directory left to create; it was
being handled previously, but the behavior was subtle.  Now I put
in a clear explicit check that exits early in that case.

The second is when there is no directory to create, but there is
a valid path that should be verified.  I shifted the logic a bit
so we'll be better about not entering the loop than that happens.
@arrbee
Copy link
Member Author

arrbee commented Jun 5, 2013

Okay, so I was able to get a bit further cleanup and, I think, make the logic clearer. Now if the path is brought to length zero by the GIT_MKDIR_SKIP_LAST option, we'll just exit immediately, which clarifies that particular case. Also, I made things more careful about finding where to begin calling p_mkdir in a way that I believe will do better. This allowed for a bit more simplification around when to verify the directory.

@jamill
Copy link
Member

jamill commented Jun 6, 2013

Thanks arrbee, this seems good to me!

vmg pushed a commit that referenced this pull request Jun 7, 2013
@vmg vmg merged commit 3de768c into libgit2:development Jun 7, 2013
@arrbee arrbee deleted the simplify-mkdir branch November 22, 2013 08:26
phatblat pushed a commit to phatblat/libgit2 that referenced this pull request Sep 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants