[Dev-luatex] file permissions

Reinhard Kotucha reinhard.kotucha at web.de
Sun Feb 6 15:56:34 CET 2011

On 6 February 2011 Hans Hagen wrote:

 > On 5-2-2011 9:11, Reinhard Kotucha wrote:
 > > In order to test whether a file is readable I currently open this
 > > file in a protected call.  This is also possible for directories
 > > when I run lfs.dir() inside pcall().  It's not very elegant, and
 > > I still have no idea how to test whether a file is writable.  I
 > > could try to open the file in append mode, but I fear that if the
 > > file is writable, this test would change the time stamps.  Not
 > > very elegant either.
 > I use something
 > function file.is_writable(name)
 >      local d = string.match(name,"^(.+)[/\\].-$") or "."
 >      local a = lfs.attributes(name) or lfs.attributes(d)
 >      return a and string/sub(a.permissions,2,2) == "w"
 > end
 > function file.is_readable(name)
 >      local a = lfs.attributes(name)
 >      return a and string.sub(a.permissions,1,1) == "r"
 > end

Consider the following situation on Unix:

$ whoami
$ ls -l /etc/passwd
-rw-r--r-- 1 root root 2269 Dec 19 14:57 /etc/passwd
Your test suggests that the file is writable, but this is only true
for root, not for the user who is running the script.  You actually
have to determine whether you are the owner of the file, or, if the
file is group writable, whether you are member of this group, or
whether the file is writable by everybody.

Regarding Windows, I'm not sure.  As far as I can see from the Lua
sources, permission flags are directly derived from a stat() call.
This means that they are related to the file owner too, not to the
current user.

If there is a file which is owned by the admin and is _not_ writable
by normal users, does your test really return 'false' if run by a
non-privileged user on Windows?


Reinhard Kotucha                                      Phone: +49-511-3373112
Marschnerstr. 25
D-30167 Hannover                              mailto:reinhard.kotucha at web.de
Microsoft isn't the answer. Microsoft is the question, and the answer is NO.

More information about the dev-luatex mailing list