How to get the permission f a file ?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get the permission f a file ?

fredvs
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  
Reply | Threaded
Open this post in threaded view
|

Re: How to get the permission f a file ?

Bart-48
On 6/17/15, fredvs <[hidden email]> 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to get the permission f a file ?

Ewald-2
In reply to this post by fredvs
On 06/17/2015 04:42 PM, fredvs wrote:
> But 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

--
Ewald

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to get the permission f a file ?

Karoly Balogh (Charlie/SGR)
In reply to this post by fredvs
Hi,

On Wed, 17 Jun 2015, 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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to get the permission f a file ?

fredvs
In reply to this post by Ewald-2
@ 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
Reply | Threaded
Open this post in threaded view
|

Re: How to get the permission f a file ?

Graeme Geldenhuys-6
On 2015-06-17 16:00, 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/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to get the permission f a file ?

fredvs

> 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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal