Thread overview
DirEntries range seems impractical with foreach
Mar 17, 2014
Spacen Jasset
Mar 17, 2014
Ali Çehreli
Mar 17, 2014
Mike Wey
Mar 17, 2014
anonymous
Mar 18, 2014
Spacen Jasset
Mar 18, 2014
Spacen Jasset
March 17, 2014
While trying to use dirEntries range with foreach I encountered a seemingly unsurmountable problem. The problem is that an execption is thrown while calling .front() on the range that dirEntries returnes, which seems to mean it's impossible to use it with a for loop if you exepect any sort of 'access denied' or other file exception - which will be quite common. e.g. in c:\windows

Is there any way to fix the foreach loop version (a)


(a) Can't make this work
 foreach (DirEntry e; entries) {

    }
std.file.FileException@std\file.d(2262): c:/windows\CSC\v2.0.6: Access is denied


(b)This works
    auto entries = dirEntries(path, spanMode);
    while (!entries.empty) {
        try {
            DirEntry e = entries.front();
            //...
            entries.popFront();
        } catch (FileException e) {
            writeln("Skipping something");
        }
    }
March 17, 2014
On 03/17/2014 02:18 AM, Spacen Jasset wrote:
> While trying to use dirEntries range with foreach I encountered a
> seemingly unsurmountable problem. The problem is that an execption is
> thrown while calling .front()

There has been past discussions on this. What version of dmd are you using? The following fix may have already fixed this issue. If not, please open another bug report:

  http://d.puremagic.com/issues/show_bug.cgi?id=8298

Ali

March 17, 2014
On 03/17/2014 02:54 PM, Ali Çehreli wrote:
> On 03/17/2014 02:18 AM, Spacen Jasset wrote:
>> While trying to use dirEntries range with foreach I encountered a
>> seemingly unsurmountable problem. The problem is that an execption is
>> thrown while calling .front()
>
> There has been past discussions on this. What version of dmd are you
> using? The following fix may have already fixed this issue. If not,
> please open another bug report:
>
>    http://d.puremagic.com/issues/show_bug.cgi?id=8298
>
> Ali

The problem is that dirEntries is trying to transverse into a directory for which the application doesn't have any access permission, this is a different issue from 8298.

But should dirEntries simply skip the directories it doesn't have access to, or should the user be notified somehow?

-- 
Mike Wey
March 17, 2014
On Monday, 17 March 2014 at 09:18:36 UTC, Spacen Jasset wrote:
> While trying to use dirEntries range with foreach I encountered a seemingly unsurmountable problem. The problem is that an execption is thrown while calling .front() on the range that dirEntries returnes, which seems to mean it's impossible to use it with a for loop if you exepect any sort of 'access denied' or other file exception - which will be quite common. e.g. in c:\windows
>
> Is there any way to fix the foreach loop version (a)
>
>
> (a) Can't make this work
>  foreach (DirEntry e; entries) {
>
>     }
> std.file.FileException@std\file.d(2262): c:/windows\CSC\v2.0.6: Access is denied
>
>
> (b)This works
>     auto entries = dirEntries(path, spanMode);
>     while (!entries.empty) {
>         try {
>             DirEntry e = entries.front();
>             //...
>             entries.popFront();
>         } catch (FileException e) {
>             writeln("Skipping something");
>         }
>     }

b doesn't work in the general case, either. dirEntries throws
immediately when the very first entry is not accessible.
March 18, 2014
On Monday, 17 March 2014 at 20:39:45 UTC, anonymous wrote:
> On Monday, 17 March 2014 at 09:18:36 UTC, Spacen Jasset wrote:
>> While trying to use dirEntries range with foreach I encountered a seemingly unsurmountable problem. The problem is that an execption is thrown while calling .front() on the range that dirEntries returnes, which seems to mean it's impossible to use it with a for loop if you exepect any sort of 'access denied' or other file exception - which will be quite common. e.g. in c:\windows
>>
>> Is there any way to fix the foreach loop version (a)
>>
>>
>> (a) Can't make this work
>> foreach (DirEntry e; entries) {
>>
>>    }
>> std.file.FileException@std\file.d(2262): c:/windows\CSC\v2.0.6: Access is denied
>>
>>
>> (b)This works
>>    auto entries = dirEntries(path, spanMode);
>>    while (!entries.empty) {
>>        try {
>>            DirEntry e = entries.front();
>>            //...
>>            entries.popFront();
>>        } catch (FileException e) {
>>            writeln("Skipping something");
>>        }
>>    }
>
> b doesn't work in the general case, either. dirEntries throws
> immediately when the very first entry is not accessible.


I see. I raised a bug on this issue https://d.puremagic.com/issues/show_bug.cgi?id=12391 Perhaps there are some other libraries ideas that can be used.



March 18, 2014
On Tuesday, 18 March 2014 at 08:54:50 UTC, Spacen Jasset wrote:
> On Monday, 17 March 2014 at 20:39:45 UTC, anonymous wrote:
>> On Monday, 17 March 2014 at 09:18:36 UTC, Spacen Jasset wrote:
>>> While trying to use dirEntries range with foreach I encountered a seemingly unsurmountable problem. The problem is that an execption is thrown while calling .front() on the range that dirEntries returnes, which seems to mean it's impossible to use it with a for loop if you exepect any sort of 'access denied' or other file exception - which will be quite common. e.g. in c:\windows
>>>
>>> Is there any way to fix the foreach loop version (a)
>>>
>>>
>>> (a) Can't make this work
>>> foreach (DirEntry e; entries) {
>>>
>>>   }
>>> std.file.FileException@std\file.d(2262): c:/windows\CSC\v2.0.6: Access is denied
>>>
>>>
>>> (b)This works
>>>   auto entries = dirEntries(path, spanMode);
>>>   while (!entries.empty) {
>>>       try {
>>>           DirEntry e = entries.front();
>>>           //...
>>>           entries.popFront();
>>>       } catch (FileException e) {
>>>           writeln("Skipping something");
>>>       }
>>>   }
>>
>> b doesn't work in the general case, either. dirEntries throws
>> immediately when the very first entry is not accessible.
>
>
> I see. I raised a bug on this issue https://d.puremagic.com/issues/show_bug.cgi?id=12391 Perhaps there are some other libraries ideas that can be used.

Boost also has the same problems: http://www.cplusplus.com/forum/windows/99515/

It occurs to me that exceptions during wide filesystem traversal are usual rather than the exception.