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

fix std.fs.Dir.makePath silent failure #16878

Merged

Conversation

amiralawi
Copy link
Contributor

std.fs.dir.makePath silently fails if one of the items in the path already exists. For example:

cwd.makePath("foo/bar/baz")

Silently failing is OK if "bar" is already a directory or a symlink that resolves to a directory - this is the intended use of makePath (like mkdir -p). But if bar is a file then the subdirectory baz cannot be created - the end result is that makePath doesn't do anything and never creates the directory baz which should be a detectable error.

The existing code had a TODO comment that did not specifically cover this error, but the solution for this silent failure also accomplishes the TODO task - the code now stats "foo" and returns an appropriate error if necessary. The new code also handles potential race condition if "bar" is deleted/permissions changed/etc in between the initial makeDir and statFile calls.

@squeek502
Copy link
Collaborator

squeek502 commented Aug 19, 2023

Would be good to add a test case to lib/std/fs/test.zig

@amiralawi
Copy link
Contributor Author

I updated the solution per your above comments and added a test to std/fs/test.zig. Tests on linux & windows OK - I don't have the capability to test on MacOS but don't anticipate that this should be significantly different.

@squeek502
Copy link
Collaborator

squeek502 commented Aug 31, 2023

Made a PR that will fix the WASI compilation error you're hitting: #17029

(it's unrelated to your changes, Dir.statFile was just broken when targeting WASI and linking libc and there were no test cases that used it)

std.fs.dir.makePath silently fails if one of the items in the path already exists. For example:

cwd.makePath("foo/bar/baz")
Silently failing is OK if "bar" is already a directory - this is the intended use of makePath (like mkdir -p). But if bar is a file then the subdirectory baz cannot be created - the end result is that makePath doesn't do anything which should be a detectable error because baz is never created.

The existing code had a TODO comment that did not specifically cover this error, but the solution for this silent failure also accomplishes the TODO task - the code now stats "foo" and returns an appropriate error. The new code also handles potential race condition if "bar" is deleted/permissions changed/etc in between the initial makeDir and statFile calls.
@squeek502 squeek502 force-pushed the fix-std.fs.Dir.makePath-silent-failure branch from d4a8bd0 to 3f6c199 Compare January 8, 2024 10:33
@andrewrk andrewrk merged commit 4cbf74b into ziglang:master Jan 8, 2024
@andrewrk
Copy link
Member

andrewrk commented Jan 8, 2024

Thank you @amiralawi, and @squeek502 for helping get this PR to completion.

bilaliscarioth pushed a commit to bilaliscarioth/zig that referenced this pull request Jan 27, 2024
std.fs.dir.makePath silently failed if one of the items in the path already exists. For example:

cwd.makePath("foo/bar/baz")
Silently failing is OK if "bar" is already a directory - this is the intended use of makePath (like mkdir -p). But if bar is a file then the subdirectory baz cannot be created - the end result is that makePath doesn't do anything which should be a detectable error because baz is never created.

The existing code had a TODO comment that did not specifically cover this error, but the solution for this silent failure also accomplishes the TODO task - the code now stats "foo" and returns an appropriate error. The new code also handles potential race condition if "bar" is deleted/permissions changed/etc in between the initial makeDir and statFile calls.
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