Recent

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

inferno

  • New Member
  • *
  • Posts: 30
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

  • Full Member
  • ***
  • Posts: 142
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-
MBAir with MacOS 10.14.6 / Lazarus 2.0.10
iMac with MacOS 10.13.6 / Lazarus 2.0.2

inferno

  • New Member
  • *
  • Posts: 30
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1282
  • Former Delphi 1-7, 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
Lazarus 2.1 r64368 FPC 3.3.1 r48100 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.1 r64455 3.3.1 r48840   macOS 11.2.2 aarch64 Xcode 12.4
Lazarus 2.1 r61574 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 3.2.0 Win10 Parallels VM

 

TinyPortal © 2005-2018