Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Oct 29, 2023
1 parent ad61842 commit 29db367
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
56 changes: 31 additions & 25 deletions ext/node/ops/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ use deno_core::ResourceHandleFd;
#[repr(u32)]
enum HandleType {
#[allow(dead_code)]
TCP = 0,
TTY,
Tcp = 0,
Tty,
#[allow(dead_code)]
UDP,
FILE,
PIPE,
UNKNOWN,
Udp,
File,
Pipe,
Unknown,
}

#[op2(fast)]
Expand All @@ -26,51 +26,57 @@ pub fn op_node_guess_handle_type(
let handle = state.resource_table.get_handle(rid)?;
let handle_type = match handle {
ResourceHandle::Fd(handle) => guess_handle_type(handle),
_ => HandleType::UNKNOWN,
_ => HandleType::Unknown,
};

Ok(handle_type as u32)
}

#[cfg(windows)]
fn guess_handle_type(handle: ResourceHandleFd) -> HandleType {
use winapi::um::{
consoleapi::GetConsoleMode,
fileapi::GetFileType,
winbase::{FILE_TYPE_CHAR, FILE_TYPE_DISK, FILE_TYPE_PIPE},
};
use winapi::um::consoleapi::GetConsoleMode;
use winapi::um::fileapi::GetFileType;
use winapi::um::winbase::FILE_TYPE_CHAR;
use winapi::um::winbase::FILE_TYPE_DISK;
use winapi::um::winbase::FILE_TYPE_PIPE;

let mut mode = 0;
// SAFETY: Call to win32 fileapi. `handle` is a valid fd.
match unsafe { GetFileType(handle) } {
FILE_TYPE_DISK => HandleType::FILE,
FILE_TYPE_DISK => HandleType::File,
FILE_TYPE_CHAR => {
// SAFETY: Call to win32 consoleapi. `handle` is a valid fd.
// `mode` is a valid pointer.
if unsafe { GetConsoleMode(handle, &mut mode) } == 1 {
HandleType::TTY
HandleType::Tty
} else {
HandleType::FILE
HandleType::File
}
}
FILE_TYPE_PIPE => HandleType::PIPE,
_ => HandleType::UNKNOWN,
FILE_TYPE_PIPE => HandleType::Pipe,
_ => HandleType::Unknown,
}
}

#[cfg(unix)]
fn guess_handle_type(handle: ResourceHandleFd) -> HandleType {
use std::io::IsTerminal;
// SAFETY: The resource remains open for the for the duration of borrow_raw
if unsafe { std::os::fd::BorrowedFd::borrow_raw(fd).is_terminal() } {
return HandleType::TTY;
if unsafe { std::os::fd::BorrowedFd::borrow_raw(handle).is_terminal() } {
return HandleType::Tty;
}

// SAFETY: It is safe to zero-initialize a `libc::stat` struct.
let mut s = unsafe { std::mem::zeroed() };
if libc::fstat(handle, &mut s) == 1 {
return HandleType::UNKNOWN;
// SAFETY: Call to libc
if unsafe { libc::fstat(handle, &mut s) } == 1 {
return HandleType::Unknown;
}

match s.st_mode & 61440 {
libc::S_IFREG | libc::S_IFCHR => HandleType::FILE,
libc::S_IFIFO => HandleType::PIPE,
libc::S_IFSOCK => HandleType::TCP,
_ => HandleType::UNKNOWN,
libc::S_IFREG | libc::S_IFCHR => HandleType::File,
libc::S_IFIFO => HandleType::Pipe,
libc::S_IFSOCK => HandleType::Tcp,
_ => HandleType::Unknown,
}
}
4 changes: 2 additions & 2 deletions ext/node/polyfills/_process/streams.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function createWritableStdioStream(writer, name) {
write(buf, enc, cb) {
if (!writer) {
this.destroy(
new Error(`Deno.${name} is not available in this environment`)
new Error(`Deno.${name} is not available in this environment`),
);
return;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ const _read = function (size) {
},
(error) => {
this.destroy(error);
}
},
);
};

Expand Down
2 changes: 1 addition & 1 deletion ext/node/polyfills/internal_binding/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
const core = globalThis.Deno.core;
const ops = core.ops;

const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
const handleTypes = ["TCP", "TTY", "UDP", "FILE", "PIPE", "UNKNOWN"];
export function guessHandleType(fd: number): string {
const type = ops.op_node_guess_handle_type(fd);
return handleTypes[type];
Expand Down

0 comments on commit 29db367

Please sign in to comment.