Check write permission of a folder or file

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Check write permission of a folder or file

Post by Martin Koob » Fri Dec 21, 2007 4:05 am

My stack saves data to an xml file using

put revXMLText(theprojID) into URL ("file:" & theFileName)

If the user opens a file from another user's shared folder then makes changes to the file and saves it does not work since they don't have write permission for that folder.

Is there a Revolution command to check the permissions of a file or folder so I can indicate that the file they opened is a read only file?

I see that the detailed folders list can get that info along with a lot of other info. Is this the only way or can you get the specific info on a particular folder directly.

I want to be able to indicate that it is a read only file and disable making changes to the data and disable saving.

Thanks.

Martin

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Dec 21, 2007 11:00 am

Hi Martin,

The detail folders is pretty direct imho.

You could also check the result after an attempt to write a file.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Post by Martin Koob » Fri Dec 28, 2007 11:24 pm

Thanks Mark

I can use the detailed folders command to find out the owner's, group and others access permissions are but on Mac OS X I can't find out who the owner is so I can't determine what the current user's access permissions are in relation to that folder.

I took another approach and used an applescript to get that info and pass it back to the rev handler.

The following applescript is in a custom property: sGetFolderPermissionsScript

tell application "Finder"
set theFolder to "**folderpath**" as alias
set folderOwner to the owner of theFolder
set ownerprivileges to owner privileges of theFolder
set everyonePrivileges to the everyones privileges of theFolder
--GROUP NAME AND PRIVILEGES NOT USED
--set groupname to the group of theFolder
--set groupprivileges to group privileges of theFolder

if folderOwner = (system attribute "USER") then
return "owner," & ownerprivileges
else
return folderOwner & "," & everyonePrivileges
end if

end tell

The following revolution script takes the files path and then passes the folder path to the applescript and then get the result of the applescript to then return the info on whether the file is read only or not for the user currently logged in.


on checkfilepermissions projectfilename
set the itemdelimiter to "/"
delete the last item of projectfilename

put revMacFromUnixPath(projectfilename) into MacFolderpath
put the sGetFolderPermissionsScript of Stack "Newproject" into thescript
replace "**folderpath**" with MacFolderpath in theScript
set the itemdelimiter to ","
do theScript as applescript

if item 2 of the result contains "write" then
return empty
else
return "This is a read only file."
end if

end checkfilepermissions

This seems to work but it would be nice to be able check the owner attribute using the detailed folders command and then determine if the current logged in user is the owner or not. It can be done for UNIX but not for Mac OS X. Is there a possibility that this will be added in the future?

Martin

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Dec 29, 2007 3:55 am

Martin,

Why do you need to know the owner? Isn't it enough to now the user's permissions? You only want to know whether the user has write permissions. If the user doesn't have write permissions, how does it help to know who has?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Post by Martin Koob » Sat Dec 29, 2007 5:10 pm

Mark

Maybe I am misunderstanding how permissions are reported.

Here is the scenario I am working with. On the Mac I have 2 users for example Teacher and Student. I am logged in as Student. I have read only access to the Teacher>Public folder but read and write access to Student>Public folder. If the script checks the access permissions using detailed folders it checks item 10 in the returned list to see the folder access permissions.
For Teacher>public folder 755 is returned (owner can read/write others can read only)
For Student>public folder 755 is returned (owner can read/write others can read only.

The result is the same even though Student's access level is different for the two folders.

This info alone does not tell me whether I when logged in as Student has write access to the Teacher public folder. I need to know if Student is "owner" or "other" in relation to that folder (or "group" but I will leave that aside for now). In this case I could check the file path and see that it is in the Teacher's folder but that approach may not work for all folders.

I don't necessarily need to know who the owner is but I need to know whether the user is "owner" or "other" in relation to that folder to determine the user's access level to that folder.

The reason I need to know the access level is if Student is opening a read/only document from the Teacher>Public folder I want to disable controls that would allow them to make changes to the document so they don't make changes thinking they can save it later.


Martin

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Mon Jan 07, 2008 2:39 pm

Hi Martin,

Try this:

Code: Select all

put shell("ls -l")
If you execute this from the message box, you see something similar to:

Code: Select all

total 384
drwxr-xr-x   10 Mark  admin    340 Dec 21 19:25 Documentation
-rw-r--r--    1 Mark  admin  42319 Dec 21 19:25 Engine Change Log.txt
drwxr-xr-x   11 Mark  admin    374 Dec 21 19:25 Externals
-rw-r--r--    1 Mark  admin   9462 Dec 21 19:25 IDE Change Log.txt
-rw-r--r--    1 Mark  admin  18641 Dec 21 19:25 License Agreement.txt
drwxr-xr-x   10 Mark  admin    340 Dec 21 19:25 Plugins
-rw-r--r--    1 Mark  admin  25026 Dec 21 19:25 Read_Me_First.txt
drwxr-xr-x    5 Mark  admin    170 Dec 21 19:25 Resources
drwxr-xr-x    3 Mark  admin    102 Dec 21 19:25 Revolution.app
drwxr-xr-x    5 Mark  admin    170 Dec 21 19:25 Runtime
drwxr-xr-x   49 Mark  admin   1666 Dec 21 19:25 Toolset
-rw-r--r--    1 Mark  admin  87858 Dec 21 19:25 Whats_New.txt
The first character of the first column indicates whether you're realing with a directory or file. The remainder of the first column shows the permission bit pattern for two files and one directory. The third column shows the owner to which the file (or directory) belongs, and the fourth column shows the group to which the file (or directory) belongs.

You can parse this information using words. E.g.

Code: Select all

put shell("ls -l") into myList
filter myList with "*Revolution.app"
put word 3 of myList
Using the listing above this would return "Mark", indicating that Mark is the owner of the directory Revolution.app.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply

Return to “Mac OS”