www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to skip permission denied exceptions if iterate through

reply "Clas Onnebrink" <clas gmx.net> writes:
Hi,

Im a newbie in D and I have a small problem. Thats my method

  //*********************************
    private void searchAndRunAction() {
       foreach (entry;dirEntries(paramPath,
                                 paramSearchType,
                                 (paramSwitchRecursiveOn ? 
SpanMode.breadth : SpanMode.shallow)))
       {
          filesCount++;

          if (isDuplicate(entry.name)) {
             filesDupCount++;
             if (paramSwitchDeleteOn)
                remove(entry.name);
          }

          printProgress(entry.name);
       }
    }

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 ../../../../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 ../../../../src/libphobos/src/std/file.d(2353): 
/media/clas/Elements2/lost+found: Permission denied
std.file.FileException ../../../../src/libphobos/src/std/file.d(2353):: 
command not found

My question: How to skip any exceptions in dirEntries. I tried it 
with filter.

LINQ-Expressions?

greets

clas
Jan 24 2014
next sibling parent reply "Rikki Cattermole" <alphaglosined gmail.com> writes:
On Friday, 24 January 2014 at 23:46:04 UTC, Clas Onnebrink wrote:
 Hi,

 Im a newbie in D and I have a small problem. Thats my method

  //*********************************
    private void searchAndRunAction() {
       foreach (entry;dirEntries(paramPath,
                                 paramSearchType,
                                 (paramSwitchRecursiveOn ? 
 SpanMode.breadth : SpanMode.shallow)))
       {
          filesCount++;

          if (isDuplicate(entry.name)) {
             filesDupCount++;
             if (paramSwitchDeleteOn)
                remove(entry.name);
          }

          printProgress(entry.name);
       }
    }

 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 ../../../../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 ../../../../src/libphobos/src/std/file.d(2353): 
 /media/clas/Elements2/lost+found: Permission denied
 std.file.FileException ../../../../src/libphobos/src/std/file.d(2353):: 
 command not found

 My question: How to skip any exceptions in dirEntries. I tried 
 it with filter.

 LINQ-Expressions?

 greets

 clas
One way would just be to catch the exceptions. Note you may want to activate debug mode to know what functions are actually failing. A FileException is being fired when it cannot do a file operation as detailed in phobos documentation.
Jan 24 2014
parent "Clas Onnebrink" <clas gmx.net> writes:
On Saturday, 25 January 2014 at 02:26:20 UTC, Rikki Cattermole 
wrote:
 On Friday, 24 January 2014 at 23:46:04 UTC, Clas Onnebrink 
 wrote:
 Hi,

 Im a newbie in D and I have a small problem. Thats my method

 //*********************************
   private void searchAndRunAction() {
      foreach (entry;dirEntries(paramPath,
                                paramSearchType,
                                (paramSwitchRecursiveOn ? 
 SpanMode.breadth : SpanMode.shallow)))
      {
         filesCount++;

         if (isDuplicate(entry.name)) {
            filesDupCount++;
            if (paramSwitchDeleteOn)
               remove(entry.name);
         }

         printProgress(entry.name);
      }
   }

 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 ../../../../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 ../../../../src/libphobos/src/std/file.d(2353): 
 /media/clas/Elements2/lost+found: Permission denied
 std.file.FileException ../../../../src/libphobos/src/std/file.d(2353):: 
 command not found

 My question: How to skip any exceptions in dirEntries. I tried 
 it with filter.

 LINQ-Expressions?

 greets

 clas
One way would just be to catch the exceptions. Note you may want to activate debug mode to know what functions are actually failing. A FileException is being fired when it cannot do a file operation as detailed in phobos documentation.
To catch the exception dont solve it. Because if dirEntries throws an exception then it doesnt going on through the directories. I need dirEntries step over directories where I have no permissions whithout raise exceptions.
Jan 25 2014
prev sibling parent "simendsjo" <simendsjo gmail.com> writes:
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 ../../../../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 ../../../../src/libphobos/src/std/file.d(2353): 
 /media/clas/Elements2/lost+found: Permission denied
 std.file.FileException ../../../../src/libphobos/src/std/file.d(2353):: 
 command not found

 My question: How to skip any exceptions in dirEntries. I tried 
 it with filter.

 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 */ } }
Jan 25 2014