Skip to content

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

Merged
andrewrk merged 4 commits intoziglang:masterfrom
amiralawi:fix-std.fs.Dir.makePath-silent-failure
Jan 8, 2024
Merged

fix std.fs.Dir.makePath silent failure#16878
andrewrk merged 4 commits intoziglang:masterfrom
amiralawi:fix-std.fs.Dir.makePath-silent-failure

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
Member

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
Member

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.

rwsalie pushed a commit to rwsalie/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