Page 1 of 1

Get Directory Permissions

Posted: 01 May 2020 19:36
by tatewise
Does anyone know a way to get the current permissions for a directory/folder in Lua?
I need to know if a directory/folder can be written, i.e. files saved or deleted inside that directory/folder.

There is one constraint ~ the Date modified of the directory/folder must NOT be affected.
So that rules out saving & deleting a temporary file, as it updates the Date modified!

I have looked at the lfs and pl library modules, but neither seem to offer that feature.

Re: Get Directory Permissions

Posted: 02 May 2020 07:30
by Mark1834
Mike - this looks like it might be relevant (not checked it myself)

https://stackoverflow.com/questions/164 ... ble-in-lua

Re: Get Directory Permissions

Posted: 02 May 2020 10:57
by tatewise
Thank you Mike, but firstly that is not talking about file permissions but only file existence.
Sencondly, the LuaFileSystem (lfs) does not provide file/folder permissions, the luaposix library is not available for FH, and access no longer exists. Unless, that is, anyone knows different.

Re: Get Directory Permissions

Posted: 02 May 2020 12:38
by ColeValleyGirl
lfs.attributes (filepath [, request_name | result_table])
Returns a table with the file attributes corresponding to filepath (or nil followed by an error message and a system-dependent error code in case of error). If the second optional argument is given and is a string, then only the value of the named attribute is returned (this use is equivalent to lfs.attributes(filepath)[request_name], but the table is not created and only one attribute is retrieved from the O.S.). if a table is passed as the second argument, it (result_table) is filled with attributes and returned instead of a new table. The attributes are described as follows; attribute mode is a string, all the others are numbers, and the time related attributes use the same time reference of os.time:
<snip>
permissions
file permissions string
Does this not work on folders then?

Re: Get Directory Permissions

Posted: 02 May 2020 13:35
by tatewise
No, the lfs provided by require("lfs") in FH Lua does not return the permissions attribute for anything.

Re: Get Directory Permissions

Posted: 03 May 2020 08:33
by Mark1834
Curious - I think you might have misread the linked page, Mike (or equally I might have misunderstood your question ;) ).

If I run the code snippet in Linux, it behaves exactly as described, with the illustrated table including the default permissions for a newly created test directory (full rwx for owner, read-only for others).

Linux.PNG
Linux.PNG (10.21 KiB) Viewed 6847 times
However, the same code in FH generates a slightly different table for my Charts folder, with the permissions line missing.

W10.PNG
W10.PNG (10.99 KiB) Viewed 6847 times
So LuaFileSystem can return permissions in Linux but not in FH. I don't know if that is a limitation of Windows generally or the Lua version supplied with FH, but file permissions generally seem much more opaque in Windows compared with Linux.

Re: Get Directory Permissions

Posted: 03 May 2020 09:40
by tatewise
Yes, it is curious!
The LuaFileSystem Manual mentions many variants and exceptions for Unix and Windows but the permissions attribute appears to be unconditional.
The LuaFileSystem History does not mention permissions being added so presumably has always existed.
Yet the FH version does not support the permissions attribute!

Re: Get Directory Permissions

Posted: 03 May 2020 11:12
by mjashby
Mike,

Not sure if the following link to a discussion thread helps or hinders, but seems to provide some explanation the lack of a direct functionality in Lua: https://stackoverflow.com/questions/283 ... -directory

Unfortunately, so far, I'm lacking any significant understanding of Lua scripting, so forgive me if it's of no help.

Mervyn

Re: Get Directory Permissions

Posted: 03 May 2020 11:58
by tatewise
Thank you Mervyn, but again that just identifies the existence of a directory and NOT its permissions such as Write access.
I know all about what LuaFileSystem (lfs) can and cannot do.

Re: Get Directory Permissions

Posted: 03 May 2020 15:03
by mjashby
Mike,

I was sure that would be the case, but the thread I linked to seems to confirm that there is no functionality in Lua to get data on Directories directly, so an external function has to be written and called.

Mervyn

Re: Get Directory Permissions

Posted: 03 May 2020 15:09
by tatewise
Yes, and it is just such an external library function that I am searching for without success.
The LuaFileSystem (lfs) is an external library function that gets tantalisingly close.

Re: Get Directory Permissions

Posted: 03 May 2020 16:09
by ColeValleyGirl
The code is in the lfs library -- look for perm2string -- it even tests for Windows versus Linux.

I'm going to guess that it's a library version problem. The capability existed at least 5 years ago but wasn't documented then. That code is version 1.8 and 11 days old... I'm going to guess we've got version 1.5 based on the timestamp on my lfs.dll

Re: Get Directory Permissions

Posted: 03 May 2020 17:03
by tatewise
Thanks Helen. Looks like you are correct. It's the old problem of FH V6 not disclosing which versions of libraries it offers.
Hopefully, FH V7 will offer the latest version of lfs and put me out of my misery :D

Re: Get Directory Permissions

Posted: 07 May 2020 16:47
by Ron Melby

Re: Get Directory Permissions

Posted: 07 May 2020 17:28
by tatewise
Thank you Ron, that may be a possibility.

Re: Get Directory Permissions

Posted: 07 May 2020 21:17
by Ron Melby
I dont know what it does to last access date. It is distressing that one has to wend thru so many warps and woofs to get permissions, and many other simple but necessary things.

One stupid thing that bothers me, is that when you right click on something ... and it somehow varies, and say you want to open with EditPadLite or some other editor (say its a saved table) there are two possible open with screens that come up, one is quite easy (but you can't be a casual user, you have to stick keys in the registry) to remove the always open with.... the other I can find no way to default to unchecked, even after talking to M$. and of course once you hit the ok, forgetting to uncheck the box, its a horror to unwind.

Re: Get Directory Permissions

Posted: 07 May 2020 21:47
by Ron Melby
https://docs.microsoft.com/en-us/archiv ... ermissions there is this, and you are handy with the old Registry......