Implement __zipos_dup (#972)

* Implement __zipos_dup

Makes ZiposHandle reference-counted by an `rc` field in a union with its
freelist `next` pointer. The functions `__zipos_free` and `__zipos_keep`
function as incref/decref for it. Adds `__zipos_postdup` to fix metadata
on file descriptors after dup-like operations, and adds zipos support to
`sys_dup_nt` + `sys_close_nt`.

* Remove noop __zipos_postdup

rc is never a zipos file because it is always a previously unused file
descriptor. fd is never a zipos file because that case has been handled
above by __zipos_fcntl.
This commit is contained in:
Jōshin 2023-12-01 03:08:30 -05:00 committed by GitHub
parent 6556dd2673
commit d1a745c17c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 138 additions and 39 deletions

View file

@ -16,13 +16,30 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/createfileflags.internal.h"
#include "libc/calls/internal.h"
#include "libc/calls/syscall-nt.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/runtime/zipos.internal.h"
#include "libc/sysv/consts/f.h"
#include "libc/sysv/consts/fd.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/errfuns.h"
static int __zipos_dupfd(int fd, int cmd, int start) {
int rc;
if (start < 0) return einval();
if (IsWindows()) {
return sys_dup_nt(fd, -1, (cmd == F_DUPFD_CLOEXEC ? _O_CLOEXEC : 0),
start);
}
rc = sys_fcntl(fd, cmd, start, __sys_fcntl);
if (rc != -1) {
__zipos_postdup(fd, rc);
}
return rc;
}
int __zipos_fcntl(int fd, int cmd, uintptr_t arg) {
if (cmd == F_GETFD) {
if (g_fds.p[fd].flags & O_CLOEXEC) {
@ -38,6 +55,8 @@ int __zipos_fcntl(int fd, int cmd, uintptr_t arg) {
g_fds.p[fd].flags &= ~O_CLOEXEC;
return 0;
}
} else if (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC) {
return __zipos_dupfd(fd, cmd, arg);
} else {
return einval();
}