class

IO::FileDescriptor

Inherits IO::Buffered / Crystal::System::FileDescriptor / IO / Reference / Object

An IO over a file descriptor.

Constructors

new(fd : Handle, blocking = nil, *, close_on_finalize = true)

Creates an IO::FileDescriptor from an existing system file descriptor or handle.

This adopts fd into the IO system that will reconfigure it as per the event loop runtime requirements.

NOTE: On Windows, the handle should have been created with FILE_FLAG_OVERLAPPED.

Source

Class methods

fcntl(fd, cmd, arg = 0)
Source
get_blocking(fd : Handle) : Bool

Returns whether the blocking mode of fd is blocking (true) or non blocking (false).

NOTE: Only implemented on UNIX targets. Raises on Windows.

Source
set_blocking(fd : Handle, value : Bool)

Changes the blocking mode of fd to be blocking (true) or non blocking (false).

NOTE: Only implemented on UNIX targets. Raises on Windows.

Source

Instance methods

blocking

Returns whether I/O operations on this file descriptor block the current thread. If false, operations might opt to suspend the current fiber instead.

This might be different from the internal file descriptor. For example, when STDIN is a terminal on Windows, this returns false since the underlying blocking reads are done on a completely separate thread.

Source
blocking=(value : Bool) : Nil

Changes the file descriptor's mode to blocking (true) or non blocking (false).

WARNING: The file descriptor has been configured to behave correctly with the event loop runtime requirements. Changing the blocking mode can cause the event loop to misbehave, for example block the entire program when a fiber tries to read from this file descriptor.

Source
close_on_exec=(value : Bool) : Bool
Source
close_on_exec?
Source
close_on_finalize=(close_on_finalize : Bool)

Whether or not to close the file descriptor when this object is finalized. Disabling this is useful in order to create an IO wrapper over a file descriptor returned from a C API that keeps ownership of the descriptor. Do note that, if the fd is closed by its owner at any point, any IO operations will then fail.

Source
close_on_finalize?

Whether or not to close the file descriptor when this object is finalized. Disabling this is useful in order to create an IO wrapper over a file descriptor returned from a C API that keeps ownership of the descriptor. Do note that, if the fd is closed by its owner at any point, any IO operations will then fail.

Source
closed?

Returns true if this IO is closed.

IO defines returns false, but including types may override.

Source
cooked

Yields self to the given block, enables character processing for the duration of the block, and returns the block's value.

The so called cooked mode is the standard behavior of a terminal, doing line wise editing by the terminal and only sending the input to the program on a newline.

Raises IO::Error if this IO is not a terminal device.

Source
cooked!

Enables character processing on this IO.

The so called cooked mode is the standard behavior of a terminal, doing line wise editing by the terminal and only sending the input to the program on a newline.

Raises IO::Error if this IO is not a terminal device.

Source
echo

Yields self to the given block, enables character echoing for the duration of the block, and returns the block's value.

This causes user input to be displayed as they are entered on the terminal.

Raises IO::Error if this IO is not a terminal device.

Source
echo!

Enables character echoing on this IO.

This causes user input to be displayed as they are entered on the terminal.

Raises IO::Error if this IO is not a terminal device.

Source
fcntl(cmd : Int, arg : Int = 0) : Int
Source
fd

Returns the raw file-descriptor handle. Its type is platform-specific.

The file-descriptor handle has been configured for the IO system requirements. If it must be in a specific mode or have a specific set of flags set, then they must be applied, even when when it feels redundant, because even the same target isn't guaranteed to have the same requirements at runtime.

Source
finalize

Finalizes the file descriptor resource.

This involves releasing the handle to the operating system, i.e. closing it. It does not implicitly call #flush, so data waiting in the buffer may be lost. It's recommended to always close the file descriptor explicitly via #close (or implicitly using the .open constructor).

Resource release can be disabled with close_on_finalize = false.

This method is a no-op if the file descriptor has already been closed.

Source
flock_exclusive(blocking = true, &)
Source
flock_exclusive(blocking : Bool = true) : Nil

Places an exclusive advisory lock. Only one process may hold an exclusive lock for a given file descriptor at a given time. IO::Error is raised if blocking is set to false and any existing lock is set.

Source
flock_shared(blocking = true, &)
Source
flock_shared(blocking : Bool = true) : Nil

Places a shared advisory lock. More than one process may hold a shared lock for a given file descriptor at a given time. IO::Error is raised if blocking is set to false and an existing exclusive lock is set.

Source
flock_unlock

Removes an existing advisory lock held by this process.

Source
fsync(flush_metadata : Bool = true) : Nil

Flushes all data written to this File Descriptor to the disk device so that all changed information can be retrieved even if the system crashes or is rebooted. The call blocks until the device reports that the transfer has completed. To reduce disk activity the flush_metadata parameter can be set to false, then the syscall fdatasync will be used and only data required for subsequent data retrieval is flushed. Metadata such as modified time and access time is not written.

NOTE: Metadata is flushed even when flush_metadata is false on Windows and DragonFly BSD.

Source
info

Returns a File::Info object for this file descriptor, or raises IO::Error in case of an error.

Certain fields like the file size may not be updated until an explicit flush.

File.write("testfile", "abc")

file = File.new("testfile", "a")
file.info.size # => 3
file << "defgh"
file.info.size # => 3
file.flush
file.info.size # => 8

Use File.info if the file is not open and a path to the file is available.

Source
inspect(io : IO) : Nil

Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
Source
noecho

Yields self to the given block, disables character echoing for the duration of the block, and returns the block's value.

This will prevent displaying back to the user what they enter on the terminal.

Raises IO::Error if this IO is not a terminal device.

print "Enter password: "
password = STDIN.noecho &.gets.try &.chomp
puts
Source
noecho!

Disables character echoing on this IO.

This will prevent displaying back to the user what they enter on the terminal.

Raises IO::Error if this IO is not a terminal device.

Source
pos=(value)

Sets the current position (in bytes) in this IO.

File.write("testfile", "hello")

file = File.new("testfile")
file.pos = 3
file.gets_to_end # => "lo"
Source
pretty_print(pp)
Source
raw

Yields self to the given block, enables raw mode for the duration of the block, and returns the block's value.

In raw mode every keypress is directly sent to the program, no interpretation is done by the terminal. On Windows, this also enables ANSI input escape sequences.

Raises IO::Error if this IO is not a terminal device.

Source
raw!

Enables raw mode on this IO.

In raw mode every keypress is directly sent to the program, no interpretation is done by the terminal. On Windows, this also enables ANSI input escape sequences.

Raises IO::Error if this IO is not a terminal device.

Source
read_timeout

The time to wait when reading before raising an IO::TimeoutError.

Source
read_timeout=(read_timeout : Number) : Number

Sets the number of seconds to wait when reading before raising an IO::TimeoutError.

Source
read_timeout=(read_timeout : Time::Span | Nil)

The time to wait when reading before raising an IO::TimeoutError.

Source
seek(offset, whence : Seek = Seek::Set)

Seeks to a given offset (in bytes) according to the whence argument. Returns self.

File.write("testfile", "abc")

file = File.new("testfile")
file.gets(3) # => "abc"
file.seek(1, IO::Seek::Set)
file.gets(2) # => "bc"
file.seek(-1, IO::Seek::Current)
file.gets(1) # => "c"
Source
seek(offset, whence : Seek = Seek::Set, &)

Same as seek but yields to the block after seeking and eventually seeks back to the original position when the block returns.

Source
tty?

Returns true if this IO is associated with a terminal device (tty), false otherwise.

IO returns false, but including types may override.

STDIN.tty?          # => true
IO::Memory.new.tty? # => false
Source
write_timeout

Sets the time to wait when writing before raising an IO::TimeoutError.

Source
write_timeout=(write_timeout : Number) : Number

Sets the number of seconds to wait when writing before raising an IO::TimeoutError.

Source
write_timeout=(write_timeout : Time::Span | Nil)

Sets the time to wait when writing before raising an IO::TimeoutError.

Source

Macros

cooked_from_tc_mode!

DEPRECATED

Source
noecho_from_tc_mode!

DEPRECATED

Source
raw_from_tc_mode!

DEPRECATED

Source