Using DMD Version 2.065, Windows 7 x86
A previous similar issue has been fixed on Linux.
http://d.puremagic.com/issues/show_bug.cgi?id=8298
To reproduce this try iterating over c:\windows it will yield an access denied exception. Alternatively create some directory in a sub directory and remove your access to it.
The error I got was: std.file.FileException@std\file.d(2519): c:/windows\CSC\v2.0.6: Access is denied
dirIterator throws an exception when it tries to traverse into a directory for which it does not have any read permissions.
This issue also exists on Linux/posix systems.
Comment #3 by timothee.cour2 — 2016-01-11T04:08:15Z
ping on this.
I made local modifications to my git repo as this error was a blocker:
auto h = directory.length ? opendir(directory.tempCString()) : opendir(".");
if(!h) {
auto s = strerror(errno).to!string;
import std.stdio;
stderr.writeln("ERROR: ", __FILE__,":",__LINE__," ",directory , " ",s);
return false;
}
Comment #4 by b2.temp — 2016-03-23T01:51:59Z
*** Issue 12513 has been marked as a duplicate of this issue. ***
Comment #5 by b2.temp — 2016-03-23T02:03:41Z
It seems to be by design. By hand it's possible to catch silently (e.g the /srv/tftpboot folder on linux).
void scan(string root)
{
foreach(DirEntry entry; dirEntries(root, SpanMode.shallow))
{
try
{
writeln(entry.name);
if (entry.isDir) scan(entry.name);
}
catch (FileException fe) {continue;}
}
}
void main()
{
scan("/srv/");
}�
The error happens in "bool stepIn(string directory)�" (std.file) because the folders are only put on a stack if they have a valid handle.
What could be done is to put them on the stack, always, and to test later, when the stack is used if the handle of each item is valid, but then the control is lost over when and why a problem happens. The iterator could even return a DirEntry with a special flag (hasNoHandle) to denote that an error occured.
Comment #6 by jason — 2016-03-23T18:23:20Z
Could we not take something out of python's book (or some other language):
os.walk https://docs.python.org/2/library/os.html
(1) This [os.walk] gives an opportunity to have an error handler during the walk.
(2) alternatively a parameter that accepts a range that becomes a list of errors.
(3) something else...
#2 doesn't seem that flexible looking at it, #1 would seem to be the way, as it acts as a predicate in python too, if an exception is thrown from the error handler the walk is stopped, otherwise it is not.
Often times I would like to ignore errors but log them. Other times better to stop the traversal.
Comment #7 by andre.artus — 2018-10-31T03:39:41Z
I would really like to see either a complementary function, or a parameter, or any other solution to this problem. At the moment I have to maintain a parallel implementation of dirEntries. While I'm sure it exists for someone, I don't have a use case favouring the current behaviour.
Most of my utilities where using dirEntries would be useful scan the full filesystem. I cannot even exclude these directories by name as the crash happens before I get the result.
auto dFiles = dirEntries(path,"*.{d,di}",SpanMode.depth);
foreach (d; dFiles) {
// nothing happening here
}
std.file.FileException@std\file.d(4573): [Folder]: Access is denied.
Comment #8 by ckatko — 2022-12-23T15:55:31Z
This issue from 2014 is still not resolved?
The function as-is, is completely broken (and nearly useless) if it cannot handle a simple permissions issue. That means every single program using this function is subject to breaking if a file/folder with inaccessible permissions is placed in a desired search directory. Even if you catch exceptions overall, it's still going to terminate the loop from searching, breaking whatever file listing the program depended on.
And even if that was acceptable, it's still not documented that this happens.
This definitely needs addressed as it's a fundamental building block of D's standard library. You are basically expected to use this for any enumerated file access.
Comment #9 by dlang-bot — 2022-12-27T11:09:59Z
@ntrel updated dlang/phobos pull request #8656 "Document dirEntries throws without read permission" mentioning this issue:
- Document dirEntries throws without read permission
Part of:
Issue 12391 - dirEntries throws in foreach
Add example showing how to handle subdirectory read permission failures.
https://github.com/dlang/phobos/pull/8656
Comment #10 by jason — 2022-12-28T18:57:53Z
I see that some documentation has been added to this, which is helpful. I do think however that SpanMode.depth is somewhat unhelpful to have, given that there is no possibility of handling errors and continuing; some people may miss the fact that it behaves in the way it does.
It may be better to remove the depth mode altogether if the API isn't going to be adjusted in any way, but equally I don't see why this API does not follow that of countless other languages that have solved the same problem as I previously mentioned. Here is rust's effort if the python one was unsound in some way. https://rust-lang-nursery.github.io/rust-cookbook/file/dir.html#recursively-find-duplicate-file-names
Comment #11 by dlang-bot — 2023-01-05T09:00:43Z
dlang/phobos pull request #8656 "Document dirEntries throws without read permission" was merged into master:
- 3aa94941b3a963d84f910bd31b2dd16e0b19665a by Nick Treleaven:
Document dirEntries throws without read permission
Part of:
Issue 12391 - dirEntries throws in foreach
Add example showing how to handle subdirectory read permission failures.
https://github.com/dlang/phobos/pull/8656
Comment #12 by dlang-bot — 2023-01-13T22:27:11Z
@dukc updated dlang/phobos pull request #8657 "Fixed many issues in grapheme walker" mentioning this issue:
- Document dirEntries throws without read permission
Part of:
Issue 12391 - dirEntries throws in foreach
Add example showing how to handle subdirectory read permission failures.
https://github.com/dlang/phobos/pull/8657
Comment #13 by robert.schadek — 2024-12-01T16:20:33Z