How to skip permission denied exceptions if iterate through directories?

simendsjo simendsjo at gmail.com
Sat Jan 25 06:00:01 PST 2014


On Friday, 24 January 2014 at 23:46:04 UTC, Clas Onnebrink wrote:
(...)
>
> I want work through a directory on my linux server but there 
> are some
> directories I have no permissions to access so I get following:
>
> ~/Projects/cltools/smdups $ source/smdups -r 
> -p=/media/clas/Elements2 -e=*.*
> std.file.FileException at ../../../../src/libphobos/src/std/file.d(2353): 
> /media/clas/Elements2/lost+found: Permission denied
> ----------------
> /home/clas/Projects/cltools/smdups/source/smdups() [0x43cb1d]
> /home/clas/Projects/cltools/smdups/source/smdups() [0x43eab4]
> /home/clas/Projects/cltools/smdups/source/smdups() [0x404768]
> /home/clas/Projects/cltools/smdups/source/smdups() [0x404116]
> /home/clas/Projects/cltools/smdups/source/smdups() [0x404d2d]
> /home/clas/Projects/cltools/smdups/source/smdups() [0x41a71f]
> /home/clas/Projects/cltools/smdups/source/smdups() [0x41ae7f]
> /home/clas/Projects/cltools/smdups/source/smdups() [0x41b0b0]
> /home/clas/Projects/cltools/smdups/source/smdups() [0x41ae7f]
> /home/clas/Projects/cltools/smdups/source/smdups() [0x41b018]
> /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) 
> [0x7f66a29c8de5]
> /home/clas/Projects/cltools/smdups/source/smdups() [0x403e63]
> ----------------
> ~/Projects/cltools/smdups $ 
> std.file.FileException at ../../../../src/libphobos/src/std/file.d(2353): 
> /media/clas/Elements2/lost+found: Permission denied
> std.file.FileException at ../../../../src/libphobos/src/std/file.d(2353):: 
> command not found
>
> My question: How to skip any exceptions in dirEntries. I tried 
> it with filter.
> But no chance. Is there a way to do it like in C# with 
> LINQ-Expressions?
>
> greets
>
> clas


This seems more difficult than i thought. Catching the exception 
doesn't help as there is no way to skip the item in question. The 
exception is being fired on popFront(), but I think the correct 
way would be to fire the exception on calling front() instead so 
you're able to skip to the next item.


import std.file, std.stdio;
void main() {
     auto dit = dirEntries("/tmp", SpanMode.breadth, true);
     while(!dit.empty) {
         try
             dit.popFront(); // Fill front()
         catch(Exception ex) {
             writeln("OOPS: ", ex);
             // We should be able to skip the file here
         }
         /* do something with dit.front */
     }
}


More information about the Digitalmars-d-learn mailing list