Check if a File/DirEntry is Readable

Jonathan M Davis jmdavisProg at gmx.com
Sun Sep 22 14:07:57 PDT 2013


On Sunday, September 22, 2013 19:46:50 Nordlöw wrote:
> I'm missing a function in std.file that checks if the
> attributes() member of DirEntry means that the it is readable by
> a specific user id (defaulted to the current user). Has anybody
> written such a logic? I know that such a function is not
> difficult to write (based on the contents of attributes()). But
> why isn't such a very often used function in the standard library?

If you want to know whether a file is readable, then use the file's attributes. 
You can either get them via std.file.getAttributes or DirEntry's attributes 
property. Then check for whatever bit needs to be set for that specific system 
for the attribute that you want. The getAttributes documentation gives links 
to where the system-specific attribut stuff can be found:

http://dlang.org/phobos/std_file.html#getAttributes

For instance, on Linux, for checking user read permissions, you'd do something 
like

    import core.sys.posix.sys.stat;
    import std.file;
    import std.stdio;
    auto canRead = cast(bool)(getAttributes(filename) & S_IRUSR);
    auto de = DirEntry(filename);
    auto canRead2 = cast(bool)(de.attributes & S_IRUSR);

But file permissions are incredibly system-specific, so I don't know if it makes 
sense to put them in the standard library or not. For instance, there are 3 
different types of read permissions on Linux but only one on Windows. And you 
can get weird combinations on Linux like having a file that is not user-
readable but is world-readable, meaning that the user who owns it can still 
read it in spite of the fact that they were not given user read permissions.

So, maybe it makes sense to come up with some incredibly generic file 
permissions for std.file (e.g. readable and writable), ignoring all of the 
system-specific permissions involved with that and find a way to sort out all of 
the permissions quirks for each system to give a generic readable or writable. 
But the permissions on different file systems are inherently different, so I 
don't know how much sense it does or doesn't ultimately make to try and come 
up with a generic way to present them.

> Note that I've been given the advice in a previous D-post not to
> rely on file exceptions for checking for file/directory
> readability.

The main thing to avoid is doing operations which will throw exceptions on 
failure when failure is likely. So, for instance, if you're dealing with a file 
that may or may not exist, checking whether it exitsts first will avoid an 
unnecessary exception. On the other hand, if the file is supposed to be there, 
and it's should rarely (if ever) happen that it's not, then you can skip the 
existance check, because the exception is unlikely to occur.

In the case of read/write permissions, it's usually the case that the program 
has permissions to read or write any files that it's asked to mess with - 
especially if you're talking about read permissions. It's quite rare that you 
don't have permissions to read a file. That being the case, in most situations, 
I don't think that it's worth checking for read/write permissions first. It 
really comes down to high likely it is that the operation will fail and end up 
throwing an exception.

- Jonathan M Davis


More information about the Digitalmars-d mailing list