Thread overview
Are there any Phobos functions to check file permissions on Windows and Posix?
Sep 06, 2015
Gary Willoughby
Sep 06, 2015
Jonathan M Davis
Sep 07, 2015
FreeSlave
Sep 07, 2015
BBasile
Sep 08, 2015
Jay Norwood
Sep 08, 2015
Jesse Phillips
Sep 07, 2015
Kagamin
September 06, 2015
Are there any Phobos functions to check file permissions on Windows and Posix? For example, I want to check if a file is readable and/or writable in a cross-platform fashion. Does anyone have an example?
September 06, 2015
On Sunday, September 06, 2015 20:40:03 Gary Willoughby via Digitalmars-d-learn wrote:
> Are there any Phobos functions to check file permissions on Windows and Posix? For example, I want to check if a file is readable and/or writable in a cross-platform fashion. Does anyone have an example?

http://dlang.org/phobos/std_file.html#.getAttributes will get you all of the file attributes for a file, though you'll have to look at the Windows and POSIX documentation to know how to interpret that.

http://dlang.org/phobos/std_file.html#.DirEntry provides some file attributes in a cross-platform manner, but readability and writability aren't among them (though its attributes property will give you the same thing that getAttributes doe).

So, as it stands, it looks like if you want to check for readibility or writability, you'd have to look at the OS documentation for each OS to see how the attributes should be interpreted. So, while you don't have to deal with the C APIs themselves for this, and the function call itself is cross-platform, the result isn't, unfortunately. However, looking over the windows documentation, I suspect that writability is the only attribute which DirEntry doesn't have which could be done cross-platform but isn't. The types of attributes that Windows has are drastically different from those in *nix land, which makes treating some of this stuff in a cross-platform fashion quite difficult.

- Jonathan M Davis

September 07, 2015
On Sunday, 6 September 2015 at 23:05:29 UTC, Jonathan M Davis wrote:
>
> http://dlang.org/phobos/std_file.html#.getAttributes will get you all of the file attributes for a file, though you'll have to look at the Windows and POSIX documentation to know how to interpret that.
>
> - Jonathan M Davis

It will give you attributes from owner's point of view (i.e. what owner can, what users in the same group can, and what all other users can)

Not sure if phobos has functions to check if file is readable or writable by current user. You can use access on Posix: http://linux.die.net/man/2/access
September 07, 2015
On Sunday, 6 September 2015 at 20:40:04 UTC, Gary Willoughby wrote:
> Are there any Phobos functions to check file permissions on Windows and Posix? For example, I want to check if a file is readable and/or writable in a cross-platform fashion. Does anyone have an example?

Call fopen and check errno?
September 07, 2015
On Sunday, 6 September 2015 at 23:05:29 UTC, Jonathan M Davis wrote:
> [...] which makes treating some of this stuff in a cross-platform fashion quite difficult.

And even more with ACLs that it could be:

On windows, to know properly if something is readable or writable the attributes are not enough. One should also check the ACL:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa446659(v=vs.85).aspx

For example you can retieve the flags: archive/readonly/hidden/system/indexable(?) and even if it looks writable or readable, the file won't be open at all because the ACL for the file don't include the current user account.


September 08, 2015
On Monday, 7 September 2015 at 15:48:56 UTC, BBasile wrote:
> On Sunday, 6 September 2015 at 23:05:29 UTC, Jonathan M Davis For example you can retieve the flags: archive/readonly/hidden/system/indexable(?) and even if it looks writable or readable, the file won't be open at all because the ACL for the file don't include the current user account.

This guy enhanced the cross-platform fox toolkit capabilities.  It would be worth looking at it if you intend to write something similar.

See the section 7. Superior provision of host OS facilities portably

http://tnfox.sourceforge.net/TnFOX/html/main.html

September 08, 2015
On Monday, 7 September 2015 at 15:48:56 UTC, BBasile wrote:
> On Sunday, 6 September 2015 at 23:05:29 UTC, Jonathan M Davis wrote:
>> [...] which makes treating some of this stuff in a cross-platform fashion quite difficult.
>
> And even more with ACLs that it could be:

Not to mention, Windows locks files that are open and the recommended way to handle that is to open the file and see if you get an error.