3

I'm missing a function in std.file that checks if the attributes() member of DirEntry means that it is readable by a specific user id (defaulted to the current user). Has anybody written such a logic? And why isn't such a very often used function in the standard library?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Nordlöw
  • 11,838
  • 10
  • 52
  • 99
  • What sort of application are you writing in which you think it is correct to not rely on exceptions for the case when a file exists but is not readable? – Vladimir Panteleev Sep 22 '13 at 18:22
  • I got this advice from andralex in a previous D-post on stackoverflow. – Nordlöw Sep 22 '13 at 18:27
  • Are you referring to [this post](http://stackoverflow.com/a/18905577/21501)? Because to me it seems like Andrei was saying the contrary - use exception handling. – Vladimir Panteleev Sep 22 '13 at 18:29

1 Answers1

4

I know that such a function is not difficult to write.

Really? I look forward to seeing your implementation which works on all systems that D supports. That includes support for Windows and Linux ACLs, not to mention security software such as SELinux.

I'm fairly sure that the universal correct way to check if opening a file will succeed is to try opening the file. If you'll settle for an approximation, and you only care about Linux, you could use the C eaccess function - however, it is not part of POSIX, and will only work on Linux. If your program will never run as root, you could use access which is part of POSIX. Personally, I wouldn't go down this route without a very good reason. If you're worried about performance (and you expect to have a lot of files that are there but are not readable for some reason), you could use the C function (fopen or POSIX open), however I'd only do this if profiling actually displays a bottleneck caused by exception handling. Not to mention, you need to be ready for an exception to be thrown anyway, as discussed above and in your previous question on this topic.

Just use exception handling, unless you really know what you're doing.

Vladimir Panteleev
  • 24,651
  • 6
  • 70
  • 114
  • 1
    Ok. Sorry, your're absolutely right. A bit sloppy of me. I removed the line about "not being difficult" from my post. Thx. – Nordlöw Sep 22 '13 at 19:35