1113b25bccae5d0a5106fbd8b7c4d409e8b469c0

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);
var
  Attr: TFileAttribute;
begin
  Attr := TFile.GetAttributes(Filename);
  Result := TFileAttribute.faReadOnly in Attr;
end;

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:

uses
  Posix.Unistd;

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

Leave a comment

xYou need to be logged in to leave a comment

Login