www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - iteration over directories is unsafe

reply forkit <forkit gmail.com> writes:
It is not possible to do a simple iteration over directories in 
 safe mode.

Really? I have to resort to unsafe??


//----

module test;
 safe: // nope. no can do.

import std;

void main()
{
     auto dFiles = dirEntries("", "*.{d,di}", SpanMode.depth);
     foreach(d; dFiles)
         writeln(d.name);
}

//----
Feb 05 2022
parent bauss <jj_1337 live.dk> writes:
On Saturday, 5 February 2022 at 23:26:21 UTC, forkit wrote:
 It is not possible to do a simple iteration over directories in 
  safe mode.

 Really? I have to resort to unsafe??


 //----

 module test;
  safe: // nope. no can do.

 import std;

 void main()
 {
     auto dFiles = dirEntries("", "*.{d,di}", SpanMode.depth);
     foreach(d; dFiles)
         writeln(d.name);
 }

 //----
Well it can't be "truly" safe since it requires a call to the system to retrieve the directory entries. While yeah, from common sense it's most likely safe from the perspective of a user, then it's not true safety as there are no safety guarantees as it's an external call. I would argue that D should move towards marking certain system functions as trusted.
Feb 07 2022