Recent

Author Topic: Linux file attributes(permissions)  (Read 4716 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 405
Linux file attributes(permissions)
« on: August 15, 2015, 05:06:46 pm »
   I don't really understand the documentation. To set the attributes(permissions) most probably I have to use BaseUnix.fpChmod. The Execute attribute can be set for owner, group and others but to read the attribute of the file I have to use BaseUnix.fpAccess. fpAccess tests for the execute attribute but it does it only for "real user ID". What's the equivalent of "real user ID" within owner, group and others?
   I expect BaseUnix to be unavailable in windows. Within what ifdef do I have to enclose BaseUnix when I put it an the uses?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Linux file attributes(permissions)
« Reply #1 on: August 15, 2015, 06:07:37 pm »
ifdef:  Unix

Both FPAccess and FPChMod are basically unix calls. Just look at their man pages  (without the fp- prefix):

man 2  chmod

or

man 3 chmod

 (without 2 or 3 you get the manual page for the cmdline utility of the same name)

chmod sets the _filesystems_ permissions, access checks the actual permission

Real user id vs effective userid is mainly related to setuid utilities. See e.g. http://www.lst.de/~okir/blackhats/node23.html

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 1058
Re: Linux file attributes(permissions)
« Reply #2 on: August 17, 2015, 12:01:58 am »
To read the permissions of a file, you generally use fpstat (man 2 stat). fpaccess is a shortcut for getting the permissions of the file, your own effective user and possibly group id, checking whether this combination allows you access, and in possibly checking any additional file system specific access control mechanisms (e.g., OpenAFS even completely ignores standard Unix permission flags and uses its own access control lists instead).

lagprogramming

  • Sr. Member
  • ****
  • Posts: 405
Re: Linux file attributes(permissions)
« Reply #3 on: August 29, 2015, 04:32:10 pm »
Code: [Select]
function CopyFileAttributes(const sourcefile,destinationfile:string):boolean;
//Might require {$ifdef unix}baseunix{$endif} within uses
var fileinfo:{$ifdef unix}
             stat;
             {$endif}
             {$ifdef windows}
             longint;
             {$endif}
begin
if (fileexists(sourcefile) and fileexists(destinationfile)) then
   begin
   {$ifdef unix}
   exit((fpstat(sourcefile,fileinfo)=0)and(fpchmod(destinationfile,fileinfo.st_mode)=0));
   {$endif}
   {$ifdef windows}
   fileinfo:=filegetattr(sourcefile);
   if fileinfo>=0 then exit(filesetattr(destinationfile,fileinfo)=0);
   {$endif}
   end;
result:=false;
end;


  If fpc's rtl already has such a function, you're welcome to reply it's name. If not, I'm pretty sure that you might improve this one.
  In the future, I'd like a pair of cross platform functions like FileGetAttr/FileSetAttr. The fact that the pair would use data that makes sense only on the same file system, would not bother me.
« Last Edit: August 29, 2015, 07:20:33 pm by lagprogramming »

 

TinyPortal © 2005-2018