Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-06

How to get the permission f a file ?

7 messages · 6 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bart
to fredvs
fredvs wrote
But how to check if a file has executable permission (and if not, change it with fpchmod(thefile S_IRWXU);) ?
function FileIsExecutable(const AFilename: string): boolean;
var
  Info : Stat;
begin
  // first check AFilename is not a directory and then check if executable
  Result:= (FpStat(AFilename,info{%H-})-1) and FPS_ISREG(info.st_mode) and
           (BaseUnix.FpAccess(AFilename,BaseUnix.X_OK)=0);
end;

(From LazFileUtils unit of Lazarus)

Bart

Karoly Balogh (Charlie/SGR
to fredvs: Hello. With fpchmod(), it is possible to change the permission of a file. Perfect. But…

Hi,

fredvs wrote
But how to check if a file has executable permission (and if not, change it with fpchmod(thefile S_IRWXU);) ? From the top of my head: fpStat(), and in the returned stat structure,

look for the mode field (or st_mode?). There are more useful bits in there. There is also fpFStat() and fpLStat() (for symlinks).

There are also several S_* constants in BaseUnix unit for easier testing of various permission combinations.

Charlie

Graeme Geldenhuys
to fredvs
fredvs wrote
And you give me choice...;-) I will study your tips and then choose one of them...

Fred, the tiSetFileReadOnly() I sent you the other day already uses fpstat() before checking/setting the read-only flag. So you are halfway there. ;-)

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/
Fred van Stappen
to Graeme Geldenhuys
Fred, the tiSetFileReadOnly() I sent you the other day already uses fpstat() before checking/setting the read-only flag. So you are halfway there. ;-)

Yep, many thanks. ;-)

PS: Bart's tip works like lovely charm... =>

function FileIsExecutable(const AFilename: string): boolean;
var
  Info : Stat;
begin
  // first check AFilename is not a directory and then check if executable
  Result:= (FpStat(AFilename,info{%H-})-1) and FPS_ISREG(info.st_mode) and
           (BaseUnix.FpAccess(AFilename,BaseUnix.X_OK)=0);
end;

Fre;D