iterate over a directory, dealing with permission errors

Paul Backus snarwin at gmail.com
Sat Jan 22 02:31:35 UTC 2022


On Saturday, 22 January 2022 at 02:09:39 UTC, Daniel Pflager 
wrote:
> https://forum.dlang.org/post/pldneecaysxueshzwopx@forum.dlang.org
>
> On Sunday, 23 May 2021 at 15:19:34 UTC, xray wrote:
>> On Sunday, 23 May 2021 at 15:13:31 UTC, Mike Parker wrote:
>>> On Sunday, 23 May 2021 at 15:12:22 UTC, Mike Parker wrote:
>>>
>>>> Are you calling it with `RangePrimitive.pop` or 
>>>> `RangePrimitive.popFront`?
>>>
>>> I meant, `RangePrimitive.front` or `RangePrimitive.popFront`.
>>
>> RangePrimitive.popFront
>>
>> In SpanMode.depth  I still get the FileException and in 
>> SpanMode.breadth, the program hangs for ever.
>
> [...]
>
>
> Test program (fixit.d):
>
> ```
> import std.exception;
> import std.file;
> import std.stdio;
>
> int main(string[] args) {
> 	// Use glob to search by glob pattern
> 	foreach (string name; dirEntries(args[1], SpanMode.breadth, 
> false).handle!(FileException, RangePrimitive.popFront, (e, r) 
> => DirEntry())) {
> 		writeln(name);
> 	}
> 	return 0;
> }
> ```

Unfortunately, there is no easy workaround for this. Because the 
exception is thrown by `popFront`, the front of the range can 
never actually be popped, and iteration cannot advance beyond the 
problematic element. Catching the exception and retrying 
`popFront` will just give the exact same error again, as you've 
seen.

Probably the best you can do (without implementing your own 
`dirEntries` iterator from scratch) is to use `SpanMode.shallow` 
to iterate over each individual directory with a separate loop. A 
permission error will still force you to skip the directory it 
occurs in, but you will be able to continue on to other files and 
directories adjacent to it.


More information about the Digitalmars-d mailing list