26.50 Protection flags

The functions GetFileAttributes(), FileAttributes() and SetFileAttributes() allow you to get and set the protection flags of a file or directory. The flags are returned and set in a single table item called flags. This item contains bitmask which is a combination of the active flags.

As protection flags are dependent on the host file system, not all of the flags listed below are supported on all platforms. See the brackets for information on which platform supports which attributes.

The following flags are recognized by Hollywood:

 
#FILEATTR_READ_USR        [AmigaOS, macOS, Linux, iOS, Android]
#FILEATTR_WRITE_USR       [AmigaOS, macOS, Linux, iOS, Android]
#FILEATTR_DELETE_USR      [AmigaOS]
#FILEATTR_EXECUTE_USR     [AmigaOS, macOS, Linux, iOS, Android]
#FILEATTR_READ_GRP        [macOS, Linux, iOS, Android]
#FILEATTR_WRITE_GRP       [macOS, Linux, iOS, Android]
#FILEATTR_EXECUTE_GRP     [macOS, Linux, iOS, Android]
#FILEATTR_READ_OTH        [macOS, Linux, iOS, Android]
#FILEATTR_WRITE_OTH       [macOS, Linux, iOS, Android]
#FILEATTR_EXECUTE_OTH     [macOS, Linux, iOS, Android]
#FILEATTR_PURE            [AmigaOS]
#FILEATTR_ARCHIVE         [AmigaOS, Windows]
#FILEATTR_SCRIPT          [AmigaOS]
#FILEATTR_HIDDEN          [AmigaOS, Windows]
#FILEATTR_SYSTEM          [Windows]
#FILEATTR_READONLY        [Windows]

To set some of these attributes for a file, simply combine them using the bitwise Or operator. For example:

 
t = {}
t.flags = #FILEATTR_READ_USR | #FILEATTR_WRITE_USR
SetFileAttributes("test.txt", t)

The code above will give read and write permission to the file "test.txt". But please note, that this code would not work correctly under Windows because Windows does not know these two attributes.

To check if a flag is set, use the bitwise And operator. For example:

 
t = GetFileAttributes("test.txt")
If (t.flags & #FILEATTR_READ_USR)
   Print("#FILEATTR_READ_USR is set.")
EndIf

There is another flag named #FILEATTR_NORMAL. This flag has a special meaning and can only be used with SetFileAttributes() and it cannot be combinated with other flags. When you pass #FILEATTR_NORMAL to SetFileAttributes(), the file's protection flags will be reset to the operating system defaults, which differ from platform to platform.


Show TOC