-
-
Notifications
You must be signed in to change notification settings - Fork 707
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 #9584 - Using dirEntries and chdir() can have unwanted results. #6125
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request and interest in making D better, @GallaFrancesco! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for submitting your first PR to Phobos!
Short initial feedback:
- no tabs
- mind @CyberShadow's comment about potential breakage
- Please add a test case
std/file.d
Outdated
@@ -4038,7 +4038,8 @@ foreach (d; dFiles) | |||
+/ | |||
auto dirEntries(string path, SpanMode mode, bool followSymlink = true) | |||
{ | |||
return DirIterator(path, mode, followSymlink); | |||
import std.path: absolutePath; | |||
return DirIterator(absolutePath(path), mode, followSymlink); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No tabs please (see https://dlang.org/dstyle.html)
std/file.d
Outdated
@@ -4057,7 +4058,7 @@ auto dirEntries(string path, SpanMode mode, bool followSymlink = true) | |||
.array; | |||
} | |||
|
|||
void main(string[] args) | |||
void main(string[] args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tab.
I've updated the source code removing tabs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but as I wrote in Bugzilla, I believe that this is the wrong approach.
If you don't want to implement the more correct solution (fdopendir
+ openat
), then I suggest that you work around this in your code.
@@ -4109,7 +4110,7 @@ auto dirEntries(string path, SpanMode mode, bool followSymlink = true) | |||
foreach (string name; dirEntries(testdir, SpanMode.breadth)) | |||
{ | |||
//writeln(name); | |||
assert(name.startsWith(testdir)); | |||
assert(name.startsWith(absolutePath(testdir))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not break existing tests!
Changing existing unit tests to conform to changes in the implementation is almost always a mistake.
@GallaFrancesco Have a look at the project tester (Jenkins) - this change actually breaks existing D code out there. |
@CyberShadow thank you for your reply. I will look into the approach you suggested ( |
Windows is the only non-POSIX system we care about. It doesn't look like Windows has native equivalents for
However, I can't recommend the above because on Windows, the current directory is a process-wide value, so it could interfere with other threads (which are presumably aware that the user changed the current directory after calling Another approach would be similar to your current implementation here, but would additionally remove the absolute path prefix in returned DirEntries, so as to not break existing code. I think we should do the Your example on Bugzilla also doesn't make sense - you're calling So... maybe improve the POSIX implementation, because that won't hurt and could bring other benefits, but as for fixing the Bugzilla issue as it was stated... that strikes me more of a kind of "shooting yourself in the foot" situation. If you expect to change directories during directory iteration, just pass an absolute path to I should add that as far as I can see, most D programs have no business at all in setting the current directory. For process creation, |
I agree with you, the situations in which one needs to Nevertheless, it seems to me that this behavior is poorly documented, which is the main reason I opened this PR in the first place. The POSIX solution looks feasible though, the only issue I encountered is the absence of the Would this require a PR to DRuntime? I'm not sure it is worth the minimal benefits brought by these changes. |
Improving our coverage of OS APIs is a worthwhile goal in itself. |
@thewilsonator wants to take a look at this one. |
After following issue 6138 on the bug tracker [edit: #9584 on GitHub], I am proposing this fix for the
std.file
module.All it does is calling
absolutePath
fromstd.path
before passing the argument todirEntries
.The overload of dirEntries which also accepts string patterns doesn't seem to be affected by this, therefore no changes were proposed.