Recent

Author Topic: manipulating file permissions and extended attributes on macOS  (Read 487 times)

inferno

  • New Member
  • *
  • Posts: 10
manipulating file permissions and extended attributes on macOS
« on: November 24, 2019, 01:40:00 am »
Hi all,
I need to read and edit files/directories permissions and extended attributes (both POSIX and ACL) on macOS platform (64bit Mojave 10.14 and Catalina/10.15) using OS calls, not CLI utilities (like chmod or xattr).
For extended attributes I found this solution https://forum.lazarus.freepascal.org/index.php?topic=24096.0 but SysCall unit is not available on macOS. Any idea how to call syscall() on macOS? It is available in /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/unistd.h and also codes including those related to extended attributes are available in /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/syscall.h

Code: Pascal  [Select]
  1. #define SYS_getxattr 234
  2. #define SYS_fgetxattr 235
  3. #define SYS_setxattr 236
  4. #define SYS_fsetxattr 237
  5. #define SYS_removexattr 238
  6. #define SYS_fremovexattr 239
  7. #define SYS_listxattr 240
  8. #define SYS_flistxattr 241

Any chance to port syscall.pp, syscall.inc, syscallh.inc and some other necessary *.inc files from FreeBSD to macOS/Darwin? I found that do_syscall() implementation (syscall.inc, syscallh.inc) for other operating systems are the assembler code...

For POSIX/ACL permissions there ara available C functions, but again - no units on macOS platform :(

https://man.openbsd.org/FreeBSD-11.1/getmode.3
https://man.openbsd.org/FreeBSD-11.1/acl_set.3

For POSIX permissions I found some useful functions in BaseUnix unit which is fortunetly available on macOS: https://www.freepascal.org/docs-html/rtl/baseunix/fpaccess.html , https://www.freepascal.org/docs-html/rtl/baseunix/fpfstat.html , https://www.freepascal.org/docs-html/rtl/baseunix/fpchmod.html , https://www.freepascal.org/docs-html/rtl/baseunix/fpchown.html. But all of them use UID/GID only. Any idea how to get real names from UID/GID?

Also I though about another approach - calling all the above functions directly as a C code. I found this: ftp://ftp.freepascal.org/fpc/docs-pdf/CinFreePascal.pdf. Is it possible do use this technique on macOS?

Best regards,
Inferno
« Last Edit: November 24, 2019, 01:45:06 am by inferno »

wittbo

  • Jr. Member
  • **
  • Posts: 79
Re: manipulating file permissions and extended attributes on macOS
« Reply #1 on: November 25, 2019, 08:00:33 pm »
I don't have any experience with SysCall or something else like this.

I have been used  RunCommand (on MacOS!), which can execute every shell command. No problem.
https://www.freepascal.org/docs-html/fcl/process/runcommand.html
https://wiki.freepascal.org/Executing_External_Programs
-wittbo-
Lazarus 2.0.4  with FPC 3.0.4a
MBAir with MacOS 10.14.5
iMac with MacOS 10.13.6

inferno

  • New Member
  • *
  • Posts: 10
Re: manipulating file permissions and extended attributes on macOS
« Reply #2 on: November 26, 2019, 01:06:16 pm »
Thank wittbo, this approach works well also for me but I'm looking for a solution based on direct system function calls. It looks the only way is binding to macOS sytem libraries based on the Darwin headers...

Best regards,
Inferno

trev

  • Sr. Member
  • ****
  • Posts: 258
  • Former Delphi 7 and Delphi 10.2 User
Re: manipulating file permissions and extended attributes on macOS
« Reply #3 on: December 03, 2019, 01:48:35 am »
But all of them use UID/GID only. Any idea how to get real names from UID/GID?

To convert UID to login name, use the id command:

Code: [Select]
$ id -n -u 1001
trev
o Lazarus v2.1.0 r61775, FPC v3.3.1 r42640, macOS 10.14.6 (with sup update), Xcode 10.3
o Lazarus v2.1.0 r61574, FPC v3.3.1 r42318, FreeBSD 12.0 (Parallels VM)
o Lazarus v2.1.0 r61574, FPC v3.0.4, Ubuntu 18.04 (Parallels VM)