Skip to content

Commit ace3c61

Browse files
authored
Pass fd implicitly to System::File and System::Socket [fixup #16137] (#16183)
I missed a couple cases in #16137 in preparation of #16127 where we need a system indirection around the `fd`, so #16128 will be able to count a reference: - `Crystal::System::File#system_chown` - `Crystal::System::Socket#system_accept` Note that by itself the change has no impact or interest.
1 parent 5915496 commit ace3c61

File tree

11 files changed

+33
-13
lines changed

11 files changed

+33
-13
lines changed

src/crystal/system/socket.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ module Crystal::System::Socket
1414

1515
# private def system_listen(backlog)
1616

17+
private def system_accept : {Handle, Bool}?
18+
event_loop.accept(self)
19+
end
20+
1721
private def system_send_to(bytes : Bytes, addr : ::Socket::Address)
1822
event_loop.send_to(self, bytes, addr)
1923
end

src/crystal/system/unix/file.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ module Crystal::System::File
108108
raise ::File::Error.from_errno("Error changing owner", file: path) if ret == -1
109109
end
110110

111-
def self.fchown(path, fd, uid : Int, gid : Int)
111+
private def system_chown(uid : Int, gid : Int)
112112
ret = LibC.fchown(fd, uid, gid)
113113
raise ::File::Error.from_errno("Error changing owner", file: path) if ret == -1
114114
end
@@ -119,7 +119,7 @@ module Crystal::System::File
119119
end
120120
end
121121

122-
private def system_chmod(path, mode)
122+
private def system_chmod(mode)
123123
if LibC.fchmod(fd, mode) == -1
124124
raise ::File::Error.from_errno("Error changing permissions", file: path)
125125
end
@@ -196,7 +196,7 @@ module Crystal::System::File
196196
end
197197
end
198198

199-
private def system_utime(atime : ::Time, mtime : ::Time, filename : String) : Nil
199+
private def system_utime(atime : ::Time, mtime : ::Time) : Nil
200200
ret = {% if LibC.has_method?("futimens") %}
201201
timespecs = uninitialized LibC::Timespec[2]
202202
timespecs[0] = Crystal::System::Time.to_timespec(atime)
@@ -212,7 +212,7 @@ module Crystal::System::File
212212
{% end %}
213213

214214
if ret != 0
215-
raise ::File::Error.from_errno("Error setting time on file", file: filename)
215+
raise ::File::Error.from_errno("Error setting time on file", file: path)
216216
end
217217
end
218218

src/crystal/system/unix/socket.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ module Crystal::System::Socket
4747
end
4848
end
4949

50+
private def system_accept : {Handle, Bool}?
51+
event_loop.accept(self)
52+
end
53+
5054
private def system_close_read
5155
if LibC.shutdown(fd, LibC::SHUT_RD) != 0
5256
raise ::Socket::Error.from_errno("shutdown read")

src/crystal/system/wasi/file.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ module Crystal::System::File
1313
raise NotImplementedError.new "Crystal::System::File.chown"
1414
end
1515

16+
private def system_chown(uid : Int, gid : Int)
17+
raise NotImplementedError.new "Crystal::System::File#system_chown"
18+
end
19+
1620
def self.realpath(path)
1721
raise NotImplementedError.new "Crystal::System::File.realpath"
1822
end

src/crystal/system/wasi/socket.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ module Crystal::System::Socket
2121
raise NotImplementedError.new "Crystal::System::Socket#system_listen"
2222
end
2323

24+
private def system_accept : {Handle, Bool}?
25+
raise NotImplementedError.new "Crystal::System::Socket#system_accept"
26+
end
27+
2428
private def system_close_read
2529
if LibC.shutdown(fd, LibC::SHUT_RD) != 0
2630
raise ::Socket::Error.from_errno("shutdown read")

src/crystal/system/win32/file.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ module Crystal::System::File
222222
raise NotImplementedError.new("File.chown")
223223
end
224224

225-
def self.fchown(path : String, fd : Int, uid : Int32, gid : Int32) : Nil
225+
private def system_chown(uid : Int, gid : Int) : Nil
226226
raise NotImplementedError.new("File#chown")
227227
end
228228

@@ -253,7 +253,7 @@ module Crystal::System::File
253253
end
254254
end
255255

256-
private def system_chmod(path : String, mode : Int32 | ::File::Permissions) : Nil
256+
private def system_chmod(mode : Int32 | ::File::Permissions) : Nil
257257
mode = ::File::Permissions.new(mode) unless mode.is_a? ::File::Permissions
258258
handle = windows_handle
259259

@@ -475,7 +475,7 @@ module Crystal::System::File
475475
end
476476
end
477477

478-
private def system_utime(access_time : ::Time, modification_time : ::Time, path : String) : Nil
478+
private def system_utime(access_time : ::Time, modification_time : ::Time) : Nil
479479
Crystal::System::File.utime(windows_handle, access_time, modification_time, path)
480480
end
481481

src/crystal/system/win32/socket.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ module Crystal::System::Socket
180180
end
181181
end
182182

183+
private def system_accept : {Handle, Bool}?
184+
event_loop.accept(self)
185+
end
186+
183187
def system_accept(& : Handle -> Bool) : {Handle, Bool}?
184188
client_socket, blocking = Crystal::EventLoop.current.socket(family, type, protocol, nil)
185189
initialize_handle(client_socket)

src/file.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ class File < IO::FileDescriptor
778778
# file.chown(gid: 100)
779779
# ```
780780
def chown(uid : Int = -1, gid : Int = -1) : Nil
781-
Crystal::System::File.fchown(@path, fd, uid, gid)
781+
system_chown(uid, gid)
782782
end
783783

784784
# Changes the permissions of the specified file.
@@ -791,12 +791,12 @@ class File < IO::FileDescriptor
791791
# file.info.permissions.value # => 0o700
792792
# ```
793793
def chmod(permissions : Int | Permissions) : Nil
794-
system_chmod(@path, permissions)
794+
system_chmod(permissions)
795795
end
796796

797797
# Sets the access and modification times
798798
def utime(atime : Time, mtime : Time) : Nil
799-
system_utime(atime, mtime, @path)
799+
system_utime(atime, mtime)
800800
end
801801

802802
# Attempts to set the access and modification times

src/socket.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class Socket < IO
244244
# end
245245
# ```
246246
def accept? : Socket?
247-
if rs = Crystal::EventLoop.current.accept(self)
247+
if rs = system_accept
248248
sock = Socket.new(handle: rs[0], family: family, type: type, protocol: protocol, blocking: rs[1])
249249
unless (blocking = system_blocking?) == rs[1]
250250
# FIXME: unlike the overloads in TCPServer and UNIXServer, this version

src/socket/tcp_server.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class TCPServer < TCPSocket
115115
# end
116116
# ```
117117
def accept? : TCPSocket?
118-
if rs = Crystal::EventLoop.current.accept(self)
118+
if rs = system_accept
119119
sock = TCPSocket.new(handle: rs[0], family: family, type: type, protocol: protocol, blocking: rs[1])
120120
sock.sync = sync?
121121
sock

0 commit comments

Comments
 (0)