BUG per Alex Shinn: "Chibi tracks fd's with a weak hash table
internally, which your low-level wrappers are bypassing."  He's going
to meld that part of SRFI 170 with (chibi filesystem) "e.g. reusing
the same file-info record from stat so that the two libraries can
interoperate."  So we can wait on him or whomever to fix that.

Many more details from
https://srfi-email.schemers.org/srfi-170/msg/15185410/; note part is
obsolete with the elimination of file descriptor objects in the SRFI,
and close-fd.  Also, the fd->*port procedures have become one fd->port
one:

On Sun, Sep 13, 2020 at 7:00 AM Shiro Kawai [redacted]

> So it gets to the similar argument when we talked about the ownership of fd in fd->*-port.
> 
> There'll also be a thing that once we get fd->fdo (not in this srfi,
> but we need it eventually along the line), the runtime must bookkeep
> if other fdo is co-owning the fd; e.g. some kind of reference
> counting per fd, or having a table so that active fd and fdo is
> always 1:1.

Yes, Chibi uses reference counting for fdo's, but there's additional
bookkeeping to keep everything sane.

All fdo objects are stored in a global weak hash table.
fd->fdo will lookup this hash table to see if we already have the fdo.
The FFI implicitly uses fd->fdo for procedures returning an fd.

fd->*port will link the fdo to a field in the resulting port, so that it won't be gc'ed.
In addition, we increment the ref count in the fdo.
When a port is closed, it decements the ref count, and if the count reaches 0 we close the fdo.
Either of the fdo and/or the port can be flagged as non-closing, for objects not managed by Scheme.

close-fd closes absolutely, ignoring the count.

open-[binary-]input-file calls fopen and creates a port backed by a FILE*.
In the common case we don't care about the fdo, so it isn't reified by default.
However, if that FILE* was already backed by an fd with a known fdo, we link it.

In the event that an fd is closed outside of Scheme (via a native C call
that knows nothing about the Scheme fdos), and subsequently reallocated
while the fdo still exists, bad things will happen.  Don't do that.


Needed to match the SRFI:

For open-file and fd->port, add optional buffer-mode argument, and the
binary-input/output port-type option.

For fd->port, enforce the restrictions that "It is an error if a port
already exists that encapsulates fd, or if an attempt is made to use
fd->port twice on the same fd."

Finishing call-with-temporary-filename


Needed for correctness, not production quality without this:

errno manipulation needs to be done at the C level, for Chibi Scheme
might set errno between any of 1) manually setting errno, 2) a POSIX
call possibly setting it, and 3) retrieving its value.  See the SRFI
199 discussion for more details: https://srfi-email.schemers.org/srfi-199/

The functionality of retry-if-EINTR in common.scm needs to be done at
the C level, for Chibi Scheme might set errno between the retry of the
POSIX call and retrieving its value.  See the SRFI 199 discussion for
more details: https://srfi-email.schemers.org/srfi-199/


Suggested enhancements:

Make appropriate procedures thread safe or thread aware

Improve temp-file-prefix specification and perhaps implementation

Use [...]at functions to handle long file paths.

See (chibi io) for how to write a C function then wrap it in a stub

WRT to above, in general, a great deal of cleanup can be done, this
project was my (hga's) reintrodution to using Scheme in anger after
many decades, and my introduction to Chibi Scheme and its extremely
nifty autogenerating FFI (170.stub) and raw FFI (aux.c).
