Skip to content

Commit

Permalink
Improve portability of SystemError (#10726)
Browse files Browse the repository at this point in the history
straight-shoota authored May 29, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 41bb8de commit a47491f
Showing 16 changed files with 115 additions and 88 deletions.
2 changes: 1 addition & 1 deletion src/crystal/system/unix/file.cr
Original file line number Diff line number Diff line change
@@ -127,7 +127,7 @@ module Crystal::System::File
end
end

raise ::File::Error.from_errno("Cannot read link", Errno::ENAMETOOLONG, file: path)
raise ::File::Error.from_os_error("Cannot read link", Errno::ENAMETOOLONG, file: path)
end

def self.rename(old_filename, new_filename)
8 changes: 4 additions & 4 deletions src/crystal/system/unix/process.cr
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ struct Crystal::System::Process

def self.pgid(pid)
# Disallow users from depending on ppid(0) instead of `pgid`
raise RuntimeError.from_errno("getpgid", Errno::EINVAL) if pid == 0
raise RuntimeError.from_os_error("getpgid", Errno::EINVAL) if pid == 0

ret = LibC.getpgid(pid)
raise RuntimeError.from_errno("getpgid") if ret < 0
@@ -107,7 +107,7 @@ struct Crystal::System::Process
# error:
errno = Errno.value
LibC.pthread_sigmask(LibC::SIG_SETMASK, pointerof(oldmask), nil)
raise RuntimeError.from_errno("fork", errno)
raise RuntimeError.from_os_error("fork", errno)
else
# parent:
LibC.pthread_sigmask(LibC::SIG_SETMASK, pointerof(oldmask), nil)
@@ -220,9 +220,9 @@ struct Crystal::System::Process
private def self.raise_exception_from_errno(command, errno = Errno.value)
case errno
when Errno::EACCES, Errno::ENOENT
raise ::File::Error.from_errno("Error executing process", errno, file: command)
raise ::File::Error.from_os_error("Error executing process", errno, file: command)
else
raise IO::Error.from_errno("Error executing process: '#{command}'", errno)
raise IO::Error.from_os_error("Error executing process: '#{command}'", errno)
end
end

14 changes: 7 additions & 7 deletions src/crystal/system/unix/pthread.cr
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ class Thread
}, self.as(Void*))

if ret != 0
raise RuntimeError.from_errno("pthread_create", Errno.new(ret))
raise RuntimeError.from_os_error("pthread_create", Errno.new(ret))
end
end

@@ -66,7 +66,7 @@ class Thread

@@current_key = begin
ret = LibC.pthread_key_create(out current_key, nil)
raise RuntimeError.from_errno("pthread_key_create", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_key_create", Errno.new(ret)) unless ret == 0
current_key
end

@@ -84,7 +84,7 @@ class Thread
# Associates the Thread object to the running system thread.
protected def self.current=(thread : Thread) : Thread
ret = LibC.pthread_setspecific(@@current_key, thread.as(Void*))
raise RuntimeError.from_errno("pthread_setspecific", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_setspecific", Errno.new(ret)) unless ret == 0
thread
end
{% else %}
@@ -144,23 +144,23 @@ class Thread
ret = LibC.pthread_attr_init(out attr)
unless ret == 0
LibC.pthread_attr_destroy(pointerof(attr))
raise RuntimeError.from_errno("pthread_attr_init", Errno.new(ret))
raise RuntimeError.from_os_error("pthread_attr_init", Errno.new(ret))
end

if LibC.pthread_attr_get_np(@th, pointerof(attr)) == 0
LibC.pthread_attr_getstack(pointerof(attr), pointerof(address), out _)
end
ret = LibC.pthread_attr_destroy(pointerof(attr))
raise RuntimeError.from_errno("pthread_attr_destroy", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_attr_destroy", Errno.new(ret)) unless ret == 0
{% elsif flag?(:linux) %}
if LibC.pthread_getattr_np(@th, out attr) == 0
LibC.pthread_attr_getstack(pointerof(attr), pointerof(address), out _)
end
ret = LibC.pthread_attr_destroy(pointerof(attr))
raise RuntimeError.from_errno("pthread_attr_destroy", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_attr_destroy", Errno.new(ret)) unless ret == 0
{% elsif flag?(:openbsd) %}
ret = LibC.pthread_stackseg_np(@th, out stack)
raise RuntimeError.from_errno("pthread_stackseg_np", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_stackseg_np", Errno.new(ret)) unless ret == 0

address =
if LibC.pthread_main_np == 1
12 changes: 6 additions & 6 deletions src/crystal/system/unix/pthread_condition_variable.cr
Original file line number Diff line number Diff line change
@@ -13,24 +13,24 @@ class Thread
{% end %}

ret = LibC.pthread_cond_init(out @cond, pointerof(attributes))
raise RuntimeError.from_errno("pthread_cond_init", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_cond_init", Errno.new(ret)) unless ret == 0

LibC.pthread_condattr_destroy(pointerof(attributes))
end

def signal
ret = LibC.pthread_cond_signal(self)
raise RuntimeError.from_errno("pthread_cond_signal", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_cond_signal", Errno.new(ret)) unless ret == 0
end

def broadcast
ret = LibC.pthread_cond_broadcast(self)
raise RuntimeError.from_errno("pthread_cond_broadcast", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_cond_broadcast", Errno.new(ret)) unless ret == 0
end

def wait(mutex : Thread::Mutex)
ret = LibC.pthread_cond_wait(self, mutex)
raise RuntimeError.from_errno("pthread_cond_wait", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_cond_wait", Errno.new(ret)) unless ret == 0
end

def wait(mutex : Thread::Mutex, time : Time::Span)
@@ -60,13 +60,13 @@ class Thread
when Errno::ETIMEDOUT
yield
else
raise RuntimeError.from_errno("pthread_cond_timedwait", errno)
raise RuntimeError.from_os_error("pthread_cond_timedwait", errno)
end
end

def finalize
ret = LibC.pthread_cond_destroy(self)
raise RuntimeError.from_errno("pthread_cond_broadcast", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_cond_broadcast", Errno.new(ret)) unless ret == 0
end

def to_unsafe
10 changes: 5 additions & 5 deletions src/crystal/system/unix/pthread_mutex.cr
Original file line number Diff line number Diff line change
@@ -10,14 +10,14 @@ class Thread
LibC.pthread_mutexattr_settype(pointerof(attributes), LibC::PTHREAD_MUTEX_ERRORCHECK)

ret = LibC.pthread_mutex_init(out @mutex, pointerof(attributes))
raise RuntimeError.from_errno("pthread_mutex_init", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_mutex_init", Errno.new(ret)) unless ret == 0

LibC.pthread_mutexattr_destroy(pointerof(attributes))
end

def lock
ret = LibC.pthread_mutex_lock(self)
raise RuntimeError.from_errno("pthread_mutex_lock", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_mutex_lock", Errno.new(ret)) unless ret == 0
end

def try_lock
@@ -27,13 +27,13 @@ class Thread
when Errno::EBUSY
false
else
raise RuntimeError.from_errno("pthread_mutex_trylock", ret)
raise RuntimeError.from_os_error("pthread_mutex_trylock", ret)
end
end

def unlock
ret = LibC.pthread_mutex_unlock(self)
raise RuntimeError.from_errno("pthread_mutex_unlock", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_mutex_unlock", Errno.new(ret)) unless ret == 0
end

def synchronize
@@ -45,7 +45,7 @@ class Thread

def finalize
ret = LibC.pthread_mutex_destroy(self)
raise RuntimeError.from_errno("pthread_mutex_destroy", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_mutex_destroy", Errno.new(ret)) unless ret == 0
end

def to_unsafe
6 changes: 3 additions & 3 deletions src/crystal/system/win32/dir.cr
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ module Crystal::System::Dir

def self.open(path : String) : DirHandle
unless ::Dir.exists? path
raise ::File::Error.from_errno("Error opening directory", Errno::ENOENT, file: path)
raise ::File::Error.from_os_error("Error opening directory", Errno::ENOENT, file: path)
end

DirHandle.new(LibC::INVALID_HANDLE_VALUE, to_windows_path(path + "\\*"))
@@ -31,7 +31,7 @@ module Crystal::System::Dir
if error == WinError::ERROR_FILE_NOT_FOUND
return nil
else
raise ::File::Error.from_winerror("Error reading directory entries", error, file: path)
raise ::File::Error.from_os_error("Error reading directory entries", error, file: path)
end
end
else
@@ -43,7 +43,7 @@ module Crystal::System::Dir
if error == WinError::ERROR_NO_MORE_FILES
return nil
else
raise ::File::Error.from_winerror("Error reading directory entries", error, file: path)
raise ::File::Error.from_os_error("Error reading directory entries", error, file: path)
end
end
end
2 changes: 1 addition & 1 deletion src/crystal/system/win32/env.cr
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ module Crystal::System::Env
when WinError::ERROR_ENVVAR_NOT_FOUND
return
else
raise RuntimeError.from_winerror("GetEnvironmentVariableW", last_error)
raise RuntimeError.from_os_error("GetEnvironmentVariableW", last_error)
end
end
end
4 changes: 2 additions & 2 deletions src/crystal/system/win32/file.cr
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ module Crystal::System::File
if NOT_FOUND_ERRORS.includes? error
return nil
else
raise ::File::Error.from_winerror(message, error, file: path)
raise ::File::Error.from_os_error(message, error, file: path)
end
end

@@ -179,7 +179,7 @@ module Crystal::System::File
end

unless exists? real_path
raise ::File::Error.from_errno("Error resolving real path", Errno::ENOENT, file: path)
raise ::File::Error.from_os_error("Error resolving real path", Errno::ENOENT, file: path)
end

real_path
2 changes: 1 addition & 1 deletion src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ module Crystal::System::FileDescriptor

if file_type == LibC::FILE_TYPE_UNKNOWN
error = WinError.value
raise IO::Error.from_winerror("Unable to get info", error) unless error == WinError::ERROR_SUCCESS
raise IO::Error.from_os_error("Unable to get info", error) unless error == WinError::ERROR_SUCCESS
end

if file_type == LibC::FILE_TYPE_DISK
2 changes: 1 addition & 1 deletion src/crystal/system/win32/process.cr
Original file line number Diff line number Diff line change
@@ -130,7 +130,7 @@ struct Crystal::System::Process
error = WinError.value
case error.to_errno
when Errno::EACCES, Errno::ENOENT
raise ::File::Error.from_winerror("Error executing process", error, file: command_args)
raise ::File::Error.from_os_error("Error executing process", error, file: command_args)
else
raise IO::Error.from_winerror("Error executing process: '#{command_args}'", error)
end
4 changes: 2 additions & 2 deletions src/errno.cr
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ enum Errno
Errno.new LibC.__error.value
{% elsif flag?(:win32) %}
ret = LibC._get_errno(out errno)
raise RuntimeError.from_errno("_get_errno", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("_get_errno", Errno.new(ret)) unless ret == 0
Errno.new errno
{% end %}
end
@@ -60,7 +60,7 @@ enum Errno
LibC.__error.value = errno.value
{% elsif flag?(:win32) %}
ret = LibC._set_errno(errno.value)
raise RuntimeError.from_errno("_set_errno", ret) unless ret == 0
raise RuntimeError.from_os_error("_set_errno", ret) unless ret == 0
{% end %}
errno
end
12 changes: 6 additions & 6 deletions src/file/error.cr
Original file line number Diff line number Diff line change
@@ -5,16 +5,16 @@ class File::Error < IO::Error
getter file : String
getter other : String?

private def self.new_from_errno(message, errno, **opts)
case errno
when Errno::ENOENT
private def self.new_from_os_error(message, os_error, **opts)
case os_error
when Errno::ENOENT, WinError::ERROR_FILE_NOT_FOUND, WinError::ERROR_PATH_NOT_FOUND
File::NotFoundError.new(message, **opts)
when Errno::EEXIST
when Errno::EEXIST, WinError::ERROR_ALREADY_EXISTS
File::AlreadyExistsError.new(message, **opts)
when Errno::EACCES
when Errno::EACCES, WinError::ERROR_PRIVILEGE_NOT_HELD
File::AccessDeniedError.new(message, **opts)
else
super message, errno, **opts
super message, os_error, **opts
end
end

2 changes: 1 addition & 1 deletion src/gc/boehm.cr
Original file line number Diff line number Diff line change
@@ -221,7 +221,7 @@ module GC
# :nodoc:
def self.pthread_join(thread : LibC::PthreadT) : Void*
ret = LibGC.pthread_join(thread, out value)
raise RuntimeError.from_errno("pthread_join", Errno.new(ret)) unless ret == 0
raise RuntimeError.from_os_error("pthread_join", Errno.new(ret)) unless ret == 0
value
end

8 changes: 4 additions & 4 deletions src/socket/common.cr
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ class Socket
wsa_version = 0x202
err = LibC.WSAStartup(wsa_version, out wsadata)
unless err.zero?
raise IO::Error.from_winerror("WSAStartup", WinError.new(err.to_u32))
raise IO::Error.from_os_error("WSAStartup", WinError.new(err.to_u32))
end

if wsadata.wVersion != wsa_version
@@ -53,14 +53,14 @@ class Socket
end

class Error < IO::Error
private def self.new_from_errno(message, errno, **opts)
case errno
private def self.new_from_os_error(message, os_error, **opts)
case os_error
when Errno::ECONNREFUSED
Socket::ConnectError.new(message, **opts)
when Errno::EADDRINUSE
Socket::BindError.new(message, **opts)
else
super message, errno, **opts
super message, os_error, **opts
end
end
end
Loading

0 comments on commit a47491f

Please sign in to comment.