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

fileops: fix creation of directory in filesystem root #5131

Merged
merged 1 commit into from Jul 12, 2019

Conversation

pks-t
Copy link
Member

@pks-t pks-t commented Jun 21, 2019

In commit 45f24e7 (git_repository_init: stop traversing at
windows root, 2019-04-12), we have fixed git_futils_mkdir to
correctly handle the case where we create a directory in
Windows-style filesystem roots like "C:\repo".

The problem here is an off-by-one: previously, to that commit,
we've been checking wether the parent directory's length is equal
to the root directory's length incremented by one. When we call
the function with "/example", then the parent directory's length
("/") is 1, but the root directory offset is 0 as the path is
directly rooted without a drive prefix. This resulted in 1 == 0 + 1, which was true. With the change, we've stopped incrementing
the root directory length, and thus now compare 1 <= 0, which
is false.

The previous way of doing it was kind of finicky any non-obvious,
which is also why the error was introduced. So instead of just
re-adding the increment, let's explicitly add a condition that
aborts finding the parent if the current parent path is "/".


Fixes #5130

@pks-t
Copy link
Member Author

pks-t commented Jun 24, 2019

Funny. CI now fails on Linux because we now started correctly creating repos in a "/non/existing/path", where the first path component doesn't exist either. This is obviously the correct thing to do, because git_repository_init defaults to GIT_REPOSITORY_INIT_MKPATH to create leading directories. And Azure seems to be kind of lenient with regards to access rights because we can create "/non"

@pks-t pks-t force-pushed the pks/fileops-mkdir-in-root branch from 8dd032a to 06dea64 Compare June 24, 2019 16:11
@pks-t
Copy link
Member Author

pks-t commented Jun 24, 2019

@ethomson: are we executing tests as root? Otherwise we wouldn't be able to create the "/usr/non/" directory...

@ethomson
Copy link
Member

@ethomson: are we executing tests as root? Otherwise we wouldn't be able to create the "/usr/non/" directory...

We are.

@pks-t
Copy link
Member Author

pks-t commented Jun 24, 2019 via email

@ehuss
Copy link
Contributor

ehuss commented Jul 11, 2019

I'm curious what the status of this is. Is there anything I can do to help?

@pks-t pks-t force-pushed the pks/fileops-mkdir-in-root branch from 06dea64 to d1e06fe Compare July 11, 2019 19:29
@pks-t
Copy link
Member Author

pks-t commented Jul 11, 2019

I'm curious what the status of this is. Is there anything I can do to help?

Probably not too much, but in fact you've just caused me to rethink my approach to how to fix the tests for now. So thanks for asking :)

This was previously blocked on the fact that we were testing in Docker images as root user. But as my changes in libgit2/docker-build#1 weren't yet merged, I've decided to go an alternative route. Thinking about it now the previous test was arguably wrong anyway, so I think we're better off with the new tests now.

The only thing left now is a review to get this merged. It doesn't rely on the Docker PR anymore, even though I still think that we should avoid building and testing as root user.

@pks-t pks-t force-pushed the pks/fileops-mkdir-in-root branch 3 times, most recently from 029c0f1 to 24e9503 Compare July 11, 2019 20:31
In commit 45f24e7 (git_repository_init: stop traversing at
windows root, 2019-04-12), we have fixed `git_futils_mkdir` to
correctly handle the case where we create a directory in
Windows-style filesystem roots like "C:\repo".

The problem here is an off-by-one: previously, to that commit,
we've been checking wether the parent directory's length is equal
to the root directory's length incremented by one. When we call
the function with "/example", then the parent directory's length
("/") is 1, but the root directory offset is 0 as the path is
directly rooted without a drive prefix. This resulted in `1 == 0 +
1`, which was true. With the change, we've stopped incrementing
the root directory length, and thus now compare `1 <= 0`, which
is false.

The previous way of doing it was kind of finicky any non-obvious,
which is also why the error was introduced. So instead of just
re-adding the increment, let's explicitly add a condition that
aborts finding the parent if the current parent path is "/".

Making this change causes Azure Pipelines to fail the testcase
repo::init::nonexistent_paths on Unix-based systems. This is because we
have just fixed creating directories in the filesystem root, which
previously didn't work. As Docker-based tests are running as root user,
we are thus able to create the non-existing path and will now succeed to
create the repository that was expected to actually fail.

Let's split this up into three different tests:

- A test to verify that we do not create repos in a non-existing parent
  directoy if the flag `GIT_REPOSITORY_INIT_MKPATH` is not set.

- A test to verify that we fail if the root directory does not exist. As
  there is a common root directory on Unix-based systems that always
  exist, we can only test for this on Windows-based systems.

- A test to verify that we fail if trying to create a repository in an
  unwriteable parent directory. We can only test this if not running
  tests as root user, as CAP_DAC_OVERRIDE will cause us to ignore
  permissions when creating files.
@pks-t pks-t force-pushed the pks/fileops-mkdir-in-root branch from 24e9503 to 5ae22a6 Compare July 11, 2019 20:34
Copy link
Contributor

@tiennou tiennou left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for fixing it @pks-t (and thanks for reminding me about the docker-build repo).

@pks-t pks-t merged commit f92d495 into libgit2:master Jul 12, 2019
@pks-t pks-t deleted the pks/fileops-mkdir-in-root branch July 12, 2019 08:48
@pks-t
Copy link
Member Author

pks-t commented Jul 12, 2019

As always, thanks a lot for your review @tiennou!

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.

Cannot init repo in root directory with absolute path
4 participants