dirEntries() and exceptions

2017-11-24 Thread doc via Digitalmars-d-learn
I'm trying recursively find files, and have some trouble to catch 
exceptions if have no permission to read directory.


[code]
void main() {
import std.file,std.stdio;
auto farray = dirEntries("/tmp", "*.{d,py,pl,sh}", 
SpanMode.breadth);

foreach (f; farray){writeln(f);}}
[/code]

std.file.FileException@std/file.d(3798): 
/tmp/systemd-private-8338348a306b4d589e3f6ba2bfd0c8fe-systemd-timesyncd.service-3374MK: Permission denied


??:? @safe core.sys.posix.dirent.DIR* 
std.file.cenforce!(core.sys.posix.dirent.DIR*).cenforce(core.sys.posix.dirent.DIR*, lazy const(char)[], immutable(char)[], ulong) [0xd31f7ce7]
??:? bool std.file.DirIteratorImpl.stepIn(immutable(char)[]) 
[0xd31f7119]

??:? void std.file.DirIteratorImpl.popFront() [0xd31f7641]
??:? void std.file.DirIterator.popFront() [0xd31f78dc]
??:? _Dmain [0xd31f1ff2]


Example structure /tmp

.
├── 374e60aa-296f-4499-9d12-36626a105069
├── 6787feed-013f-4907-bf86-b18a5a7c966f
├── dub_platform_probe-0d2aed9d-4a4f-4da3-a9fe-af705fb4914c.d
├── dub_platform_probe-3e5246ea-7158-4184-865e-8f15ec149b6f.d
├── dub_platform_probe-724ee4cf-bdc9-4137-b25a-4893323922ac.d
├── dub_platform_probe-9a0c98e4-3077-460c-9309-b8066be1b353.d
├── dub_platform_probe-be30fcdc-3eb2-4a0c-88bd-49a8507eb48b.d
├── dub_platform_probe-dc6d01e2-4f5d-4e71-adde-70131b8a0587.d
├── dub_platform_probe-dcd282e3-1b9c-486a-84ee-5eee5c60790f.d
├── dub_platform_probe-e2dfd83f-5f21-401c-a450-20bf5e73a678.d
├── ls
├── ls.d
├── ls.o
├── 
systemd-private-8338348a306b4d589e3f6ba2bfd0c8fe-systemd-timesyncd.service-3374MK [error opening dir]

├── vscode-e6da53a1363a02042f56966c5464593959886044.sock

drwx-- 3 root root60 ноя 24 08:32  
systemd-private-8338348a306b4d589e3f6ba2bfd0c8fe-systemd-timesyncd.service-3374MK


This dir have permission 0700, only root can read in this dir. 
Also, in /tmp I'm create at root a new dir /tmp/www with 0700 
perm. But my program has exit when exception on 
/tmp/systed-private... and can't write any messages when can't 
read /tmp/www.


So, how I can skip any exceptions and continue program?

[code]
void main() {
import std.stdio, std.file;

try {
auto farray = dirEntries("/tmp", "*.{d,py,ph,sh}", 
SpanMode.breadth);

foreach (f; farray) {
writeln(f);
}
catch (FileException e) {
writeln(e.msg);
}
}
[/code]

This not work, after writeln(e.msg); program is exit.


Re: Head Const

2016-02-18 Thread Doc via Digitalmars-d
On Thursday, 18 February 2016 at 11:57:59 UTC, Jonathan M Davis 
wrote:




The more I look at it, the more I'm inclined to think that 
introducing @mutable for member variables with a corresponding, 
required attribute on the struct or class it's in (e.g. 
@has_mutable) is really what we need to be able to solve this 
problem and make D usable in some of these high performance 
cases that would be using the mutable keyword in C++. It solves 
the logical const problem without totally throwing away the 
compiler guarantees. Any type without @has_mutable functions as 
it always has, and the cases where @mutable/@has_mutable would 
be required would then work with const, gaining all of its 
benefits for the non-@mutable members, and it would allow the 
compiler to prevent you from doing stupid stuff like mutating 
immutable objects, because an object with @has_mutable couldn't 
be immutable.



- Jonathan M Davis


Could we use a special class Interface for this to limit the 
widespread use of a new keyword or attribute?


I.e. classes could implement a special mutable RefCount (as an 
example) interface. Only code that refers to the object by it's 
mutable interface would be allowed to jailbreak its overall 
constness, and only for those members defined in the mutable 
interface.


Maybe add a MutableInterface keyword or an attribute strictly 
valid in Interface declarations.


Just a late night brainstorm.

-Doc