/
file_descriptor.cr
188 lines (154 loc) · 3.42 KB
/
file_descriptor.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require "crystal/system/file_descriptor"
# An `IO` over a file descriptor.
class IO::FileDescriptor < IO
include Crystal::System::FileDescriptor
include IO::Buffered
# The raw file-descriptor. It is defined to be an `Int`, but its size is
# platform-specific.
def fd
@volatile_fd.get
end
def initialize(fd, blocking = nil)
@volatile_fd = Atomic.new(fd)
@closed = system_closed?
if blocking.nil?
blocking =
case system_info.type
when .pipe?, .socket?, .character_device?
false
else
true
end
end
unless blocking || {{flag?(:win32)}}
self.blocking = false
end
end
# :nodoc:
def self.from_stdio(fd)
Crystal::System::FileDescriptor.from_stdio(fd)
end
def blocking
system_blocking?
end
def blocking=(value)
self.system_blocking = value
end
def close_on_exec?
system_close_on_exec?
end
def close_on_exec=(value : Bool)
self.system_close_on_exec = value
end
{% unless flag?(:win32) %}
def self.fcntl(fd, cmd, arg = 0)
Crystal::System::FileDescriptor.fcntl(fd, cmd, arg)
end
def fcntl(cmd, arg = 0)
Crystal::System::FileDescriptor.fcntl(fd, cmd, arg)
end
{% end %}
def info
system_info
end
# 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"
# ```
def seek(offset, whence : Seek = Seek::Set)
check_open
flush
offset -= @in_buffer_rem.size if whence.current?
system_seek(offset, whence)
@in_buffer_rem = Bytes.empty
self
end
# Same as `seek` but yields to the block after seeking and eventually seeks
# back to the original position when the block returns.
def seek(offset, whence : Seek = Seek::Set)
original_pos = tell
begin
seek(offset, whence)
yield
ensure
seek(original_pos)
end
end
# Returns the current position (in bytes) in this `IO`.
#
# ```
# File.write("testfile", "hello")
#
# file = File.new("testfile")
# file.pos # => 0
# file.gets(2) # => "he"
# file.pos # => 2
# ```
def pos
check_open
system_pos - @in_buffer_rem.size
end
# 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"
# ```
def pos=(value)
seek value
value
end
def finalize
return if closed?
close rescue nil
end
def closed?
@closed
end
def tty?
system_tty?
end
def reopen(other : IO::FileDescriptor)
return other if self.fd == other.fd
system_reopen(other)
other
end
def inspect(io : IO) : Nil
io << "#<IO::FileDescriptor:"
if closed?
io << "(closed)"
else
io << " fd=" << fd
end
io << '>'
end
def pretty_print(pp)
pp.text inspect
end
private def unbuffered_rewind
self.pos = 0
end
private def unbuffered_close
return if @closed
# Set before the @closed state so the pending
# IO::Evented readers and writers can be cancelled
# knowing the IO is in a closed state.
@closed = true
system_close
end
private def unbuffered_flush
# Nothing
end
end