Delphi's TFileAttribute is completey different on Windows and MacOS as the filesystems differ in especially in file permissions. One could check if a file is readonly on Windows like that:

function IsFileReadyOnly(const Filename: string);
  Attr: TFileAttribute;
  Attr := TFile.GetAttributes(Filename);
  Result := TFileAttribute.faReadOnly in Attr;

But there is no counterpart for faReadOnly on the posix declaration. But we could check if the file can be written, but by whom?

  • faOwnerWrite 1 - File can be written by its owner. But who is the owner?
  • faGroupWrite 2 - File can be written by its group. Are we member in this group?
  • faOthersWrite 3 - File can be written by others thus by everyone.

Even if we know that somebody can write the file we can not be sure we are the one. So a different approach is needed here. There is a function called access in unit Posix.Unistd which checks the accessibililty of the file:


function IsReadOnly(const FileName: string): Boolean;
  Result := access(PAnsiChar(UTF8Encode(FileName)), W_OK) = -1; // It's readonly if it's not writeable
The respective author is responsible for articles (snaptips) within this service.

Leave a comment

xYou need to be logged in to leave a comment