Hello.
With fpchmod(), it is possible to change the permission of a file.
Perfect.
But how to check if a file has executable permission (and if not, change it with fpchmod(thefile S_IRWXU);) ?
Many thanks.
Fre;D
-----
Many thanks ;-)

Mailing lists / fpc-pascal / 2015-06
7 messages · 6 people ·
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hello.
With fpchmod(), it is possible to change the permission of a file.
Perfect.
But how to check if a file has executable permission (and if not, change it with fpchmod(thefile S_IRWXU);) ?
Many thanks.
Fre;D
-----
Many thanks ;-)
fredvs wroteBut 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
Hello. With fpchmod(), it is possible to change the permission of a file. Perfect. But…
fredvs wroteBut how to check if a file has executable permission (and if not, change it with fpchmod(thefile S_IRWXU);) ?
stat?
See:
http://linux.die.net/man/2/stat
http://www.freepascal.org/docs-html/rtl/baseunix/fpstat.html http://www.freepascal.org/docs-html/rtl/baseunix/stat.html
Hello. With fpchmod(), it is possible to change the permission of a file. Perfect. But…
Hi,
fredvs wroteBut 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
stat? See: http://linux.die.net/man/2/stat http://www.freepascal.org/docs-html/rtl/baseuni…
@ Bart and Ewald. => excellent, many thanks.
And you give me choice...;-)
I will study your tips and then choose one of them...
Many thanks.
Fre;D
-----
Many thanks ;-)
fredvs wroteAnd 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 -
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