savonet / ocaml-posix

Bindings to the various POSIX APIs
https://www.liquidsoap.info/ocaml-posix/
MIT License
27 stars 2 forks source link

[WIP] Bindings for POSIX `stat` and `statvfs` #12

Open MisterDA opened 1 year ago

MisterDA commented 1 year ago

I'd like to have low-level ctypes bindings to stat, statfs, and statvfs. I'm not super familiar of how ctypes works, and I only have for now a macOS system (so, not POSIX), from which I've derived the constants, structures, and function prototypes. I hope I've somewhat cleared the path, and I would quite appreciate someone more experienced picking up the work. This patch doesn't build right now.

toots commented 1 year ago

That looks like a good start! The CI will tell you if it builds on ubuntu. You can also use a docker image to test your changes locally.

MisterDA commented 1 year ago

I've now used the definitions from POSIX sys/stat.h and sys/statvfs.h. Turns out statfs isn't in POSIX. I've removed Linux and macOS specific extensions (I've commented the code but I'll remove it later).

Two things still elude me: how to use Timespec.t (the ctypes type?) from posix-time2 in my ml and mli files, and how to expose the macros (typed mode_t -> int)

S_ISBLK(m)
    Test for a block special file.
S_ISCHR(m)
    Test for a character special file.
S_ISDIR(m)
    Test for a directory.
S_ISFIFO(m)
    Test for a pipe or FIFO special file.
S_ISREG(m)
    Test for a regular file.
S_ISLNK(m)
    Test for a symbolic link.
S_ISSOCK(m)
    Test for a socket. 

and these (typed ptr Stat.t -> int)

S_TYPEISMQ(buf)
    Test for a message queue.
S_TYPEISSEM(buf)
    Test for a semaphore.
S_TYPEISSHM(buf)
    Test for a shared memory object. 
S_TYPEISTMO(buf)
    Test macro for a typed memory object. 
toots commented 1 year ago

I've now used the definitions from POSIX sys/stat.h and sys/statvfs.h. Turns out statfs isn't in POSIX. I've removed Linux and macOS specific extensions (I've commented the code but I'll remove it later).

Two things still elude me: how to use Timespec.t (the ctypes type?) from posix-time2 in my ml and mli files, and how to expose the macros (typed mode_t -> int)

S_ISBLK(m)
    Test for a block special file.
S_ISCHR(m)
    Test for a character special file.
S_ISDIR(m)
    Test for a directory.
S_ISFIFO(m)
    Test for a pipe or FIFO special file.
S_ISREG(m)
    Test for a regular file.
S_ISLNK(m)
    Test for a symbolic link.
S_ISSOCK(m)
    Test for a socket. 

and these (typed ptr Stat.t -> int)

S_TYPEISMQ(buf)
    Test for a message queue.
S_TYPEISSEM(buf)
    Test for a semaphore.
S_TYPEISSHM(buf)
    Test for a shared memory object. 
S_TYPEISTMO(buf)
    Test macro for a typed memory object. 

Have you tried binding the macros like you would with a function? This could work. If not you can always define proper C function wrappers in the C header from the stubs generator.

As for the ctypes types, all the low-level libraries from the bindings should be available via sub-packages, for instance here:

(library
 (name posix_time2_types)
 (public_name posix-time2.types)
 (libraries posix-time2.constants posix-base ctypes.stubs))
toots commented 1 year ago

You might still be able to make the specific extension available BTW if you can make dune conditionally build them and reflect that in the opam package available clause.