mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge pull request #6231 from Asmod4n/add_close_fd_to_io
Add a way to let other gems handle closing of fds in mruby-io
This commit is contained in:
@@ -41,7 +41,9 @@ struct mrb_io {
|
||||
writable:1,
|
||||
eof:1,
|
||||
sync:1,
|
||||
is_socket:1;
|
||||
is_socket:1,
|
||||
close_fd:1,
|
||||
close_fd2:1;
|
||||
};
|
||||
|
||||
#define MRB_O_RDONLY 0x0000
|
||||
|
||||
@@ -343,6 +343,8 @@ io_alloc(mrb_state *mrb)
|
||||
fptr->sync = 0;
|
||||
fptr->eof = 0;
|
||||
fptr->is_socket = 0;
|
||||
fptr->close_fd = 1;
|
||||
fptr->close_fd2 = 1;
|
||||
return fptr;
|
||||
}
|
||||
|
||||
@@ -761,13 +763,13 @@ fptr_finalize(mrb_state *mrb, struct mrb_io *fptr, int quiet)
|
||||
if (fptr->fd >= limit) {
|
||||
#ifdef _WIN32
|
||||
if (fptr->is_socket) {
|
||||
if (closesocket(fptr->fd) != 0) {
|
||||
if (fptr->close_fd && closesocket(fptr->fd) != 0) {
|
||||
saved_errno = WSAGetLastError();
|
||||
}
|
||||
fptr->fd = -1;
|
||||
}
|
||||
#endif
|
||||
if (fptr->fd != -1) {
|
||||
if (fptr->fd != -1 && fptr->close_fd) {
|
||||
if (close(fptr->fd) == -1) {
|
||||
saved_errno = errno;
|
||||
}
|
||||
@@ -776,7 +778,7 @@ fptr_finalize(mrb_state *mrb, struct mrb_io *fptr, int quiet)
|
||||
}
|
||||
|
||||
if (fptr->fd2 >= limit) {
|
||||
if (close(fptr->fd2) == -1) {
|
||||
if (fptr->close_fd2 && close(fptr->fd2) == -1) {
|
||||
if (saved_errno == 0) {
|
||||
saved_errno = errno;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user